package com.axelor.apps.stock.service;

import com.axelor.apps.base.db.Company;
import com.axelor.apps.base.db.Product;
import com.axelor.apps.stock.db.StockProductionRequest;
import com.axelor.apps.stock.db.StockProductionRequestLine;
import com.google.common.base.Preconditions;
import java.math.BigDecimal;

public class StockProductionRequestLineService {

  public void setProductInfo(
      StockProductionRequest productionRequest,
      StockProductionRequestLine productionRequestLine,
      Company company) {
    Preconditions.checkNotNull(productionRequestLine);
    Preconditions.checkNotNull(company);
    Product product = productionRequestLine.getProduct();
    if (product == null) {
      return;
    }
    productionRequestLine.setUnit(product.getUnit());
    productionRequestLine.setProductName(product.getName());
  }

  public StockProductionRequestLine compute(
      StockProductionRequestLine productionRequestLine, StockProductionRequest productionRequest) {

    BigDecimal unitPriceUntaxed = BigDecimal.ZERO;
    if (productionRequestLine.getProduct() != null && productionRequest != null) {
      unitPriceUntaxed = productionRequestLine.getProduct().getAvgPrice();
    }
    productionRequestLine.setUnitPriceUntaxed(unitPriceUntaxed);
    productionRequestLine.setUnitPriceTaxed(unitPriceUntaxed);
    productionRequestLine.setCompanyUnitPriceUntaxed(unitPriceUntaxed);
    return productionRequestLine;
  }
}
