package com.axelor.apps.supplychain.service;

import com.axelor.apps.purchase.db.PurchaseOrderLineFile;
import com.axelor.apps.purchase.db.repo.PurchaseOrderLineFileRepository;
import com.axelor.apps.purchase.service.app.AppPurchaseService;
import com.axelor.auth.AuthUtils;
import com.axelor.exception.AxelorException;
import com.axelor.inject.Beans;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

public class PurchaseOrderLineFileServiceService {

  @Inject
  protected AppPurchaseService appPurchaseService;

  @Transactional(rollbackOn = { Exception.class })
  public void refuseCoa(PurchaseOrderLineFile purchaseOrderLineFile, String raison, String refusalOrigin)
      throws AxelorException {

      System.out.println("purchaseOrderLineFile************************************");
      System.out.println(refusalOrigin);
      System.out.println(raison);
      System.out.println("purchaseOrderLineFile************************************");

    switch (refusalOrigin) {
      case PurchaseOrderLineFileRepository.DT_STATUS_SELECT:
        purchaseOrderLineFile.setDtStatusSelect(4);
        purchaseOrderLineFile.setDt_refusal_date(appPurchaseService.getTodayDateTime().toLocalDateTime());
        purchaseOrderLineFile.setDt_validator_refusal_user(AuthUtils.getUser());
        purchaseOrderLineFile.setDt_refusal_reason(raison);
        break;
      case PurchaseOrderLineFileRepository.RD_STATUS_SELECT:
        purchaseOrderLineFile.setRdStatusSelect(4);
        purchaseOrderLineFile.setRd_refusal_date(appPurchaseService.getTodayDateTime().toLocalDateTime());
        purchaseOrderLineFile.setRd_refusal_user(AuthUtils.getUser());
        purchaseOrderLineFile.setRd_refusal_reason(raison);
        break;

      case PurchaseOrderLineFileRepository.QA_STATUS_SELECT:
        purchaseOrderLineFile.setQaStatusSelect(4);
        purchaseOrderLineFile.setQa_refusal_date(appPurchaseService.getTodayDateTime().toLocalDateTime());
        purchaseOrderLineFile.setQa_validator_refusal_user(AuthUtils.getUser());
        purchaseOrderLineFile.setQa_refusal_reason(raison);
        break;

      case PurchaseOrderLineFileRepository.QC_STATUS_SELECT:
        purchaseOrderLineFile.setQcStatusSelect(4);
        purchaseOrderLineFile.setQc_refusal_date(appPurchaseService.getTodayDateTime().toLocalDateTime());
        purchaseOrderLineFile.setQc_validator_refusal_user(AuthUtils.getUser());
        purchaseOrderLineFile.setQc_refusal_reason(raison);
        break;

      case PurchaseOrderLineFileRepository.PRODUCTION_STATUS_SELECT:
        purchaseOrderLineFile.setProductionStatusSelect(4);
        purchaseOrderLineFile.setProduction_refusal_date(appPurchaseService.getTodayDateTime().toLocalDateTime());
        purchaseOrderLineFile.setProduction_validator_refusal_user(AuthUtils.getUser());
        purchaseOrderLineFile.setProduction_refusal_reason(raison);
        break;

      default:
        break;
    }

    Beans.get(PurchaseOrderLineFileRepository.class).save(purchaseOrderLineFile);
  }
}
