Search in sources :

Example 1 with UnitCostCalcLine

use of com.axelor.apps.production.db.UnitCostCalcLine in project axelor-open-suite by axelor.

the class UnitCostCalculationServiceImpl method calculationProductProcess.

@Transactional(rollbackOn = { Exception.class })
protected void calculationProductProcess(UnitCostCalculation unitCostCalculation, Product product) throws AxelorException {
    int level = this.productMap.get(product.getId()).intValue();
    log.debug("Unit cost price calculation for product : {}, level : {}", product.getCode(), level);
    int origin = unitCostCalculation.getAllBomLevels() ? CostSheetService.ORIGIN_BULK_UNIT_COST_CALCULATION : CostSheetService.ORIGIN_BILL_OF_MATERIAL;
    BillOfMaterial billOfMaterial = product.getDefaultBillOfMaterial();
    CostSheet costSheet = costSheetService.computeCostPrice(billOfMaterial, origin, unitCostCalculation);
    UnitCostCalcLine unitCostCalcLine = unitCostCalcLineService.createUnitCostCalcLine(product, billOfMaterial.getCompany(), level, costSheet);
    unitCostCalculation.addUnitCostCalcLineListItem(unitCostCalcLine);
    unitCostCalculationRepository.save(unitCostCalculation);
}
Also used : BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) UnitCostCalcLine(com.axelor.apps.production.db.UnitCostCalcLine) CostSheet(com.axelor.apps.production.db.CostSheet) Transactional(com.google.inject.persist.Transactional)

Example 2 with UnitCostCalcLine

use of com.axelor.apps.production.db.UnitCostCalcLine in project axelor-open-suite by axelor.

the class CostSheetLineServiceImpl method createConsumedProductWasteCostSheetLine.

public CostSheetLine createConsumedProductWasteCostSheetLine(Company company, Product product, Unit unit, int bomLevel, CostSheetLine parentCostSheetLine, BigDecimal consumptionQty, BigDecimal wasteRate, int origin, UnitCostCalculation unitCostCalculation) throws AxelorException {
    Product parentProduct = parentCostSheetLine.getProduct();
    BigDecimal qty = consumptionQty.multiply(wasteRate).divide(new BigDecimal("100"), appBaseService.getNbDecimalDigitForQty(), BigDecimal.ROUND_HALF_UP);
    BigDecimal costPrice = null;
    switch(origin) {
        case CostSheetService.ORIGIN_BULK_UNIT_COST_CALCULATION:
            BillOfMaterial componentDefaultBillOfMaterial = product.getDefaultBillOfMaterial();
            if (componentDefaultBillOfMaterial != null) {
                UnitCostCalcLine unitCostCalcLine = unitCostCalcLineServiceImpl.getUnitCostCalcLine(unitCostCalculation, product);
                if (unitCostCalcLine != null) {
                    costPrice = unitCostCalcLine.getComputedCost();
                    break;
                }
            }
            costPrice = this.getComponentCostPrice(product, parentProduct.getBomCompValuMethodSelect(), company);
            break;
        case CostSheetService.ORIGIN_BILL_OF_MATERIAL:
            costPrice = this.getComponentCostPrice(product, parentProduct.getBomCompValuMethodSelect(), company);
            break;
        default:
            costPrice = BigDecimal.ZERO;
    }
    costPrice = unitConversionService.convert(unit, product.getUnit(), costPrice, appProductionService.getNbDecimalDigitForUnitPrice(), product).multiply(qty);
    return this.createCostSheetLine(product.getName(), product.getCode(), bomLevel, qty, costPrice.setScale(appProductionService.getNbDecimalDigitForUnitPrice(), RoundingMode.HALF_UP), product.getCostSheetGroup(), product, CostSheetLineRepository.TYPE_CONSUMED_PRODUCT_WASTE, CostSheetLineRepository.TYPE_CONSUMED_PRODUCT_WASTE, unit, null, parentCostSheetLine);
}
Also used : BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) UnitCostCalcLine(com.axelor.apps.production.db.UnitCostCalcLine) Product(com.axelor.apps.base.db.Product) BigDecimal(java.math.BigDecimal)

Example 3 with UnitCostCalcLine

use of com.axelor.apps.production.db.UnitCostCalcLine in project axelor-open-suite by axelor.

the class CostSheetLineServiceImpl method createConsumedProductCostSheetLine.

public CostSheetLine createConsumedProductCostSheetLine(Company company, Product product, Unit unit, int bomLevel, CostSheetLine parentCostSheetLine, BigDecimal consumptionQty, int origin, UnitCostCalculation unitCostCalculation) throws AxelorException {
    Product parentProduct = parentCostSheetLine.getProduct();
    BigDecimal costPrice = null;
    switch(origin) {
        case CostSheetService.ORIGIN_MANUF_ORDER:
            costPrice = this.getComponentCostPrice(product, parentProduct.getManufOrderCompValuMethodSelect(), company);
            break;
        case CostSheetService.ORIGIN_BULK_UNIT_COST_CALCULATION:
            BillOfMaterial componentDefaultBillOfMaterial = product.getDefaultBillOfMaterial();
            if (componentDefaultBillOfMaterial != null) {
                UnitCostCalcLine unitCostCalcLine = unitCostCalcLineServiceImpl.getUnitCostCalcLine(unitCostCalculation, product);
                if (unitCostCalcLine != null) {
                    costPrice = unitCostCalcLine.getComputedCost();
                    break;
                }
            }
            costPrice = this.getComponentCostPrice(product, parentProduct.getBomCompValuMethodSelect(), company);
            break;
        // from its bill of materials
        case CostSheetService.ORIGIN_BILL_OF_MATERIAL:
            costPrice = this.getComponentCostPrice(product, parentProduct.getBomCompValuMethodSelect(), company);
            break;
        default:
            costPrice = BigDecimal.ZERO;
    }
    consumptionQty = consumptionQty.setScale(appBaseService.getNbDecimalDigitForQty(), RoundingMode.HALF_UP);
    costPrice = costPrice.multiply(consumptionQty);
    costPrice = unitConversionService.convert(unit, product.getUnit(), costPrice, appProductionService.getNbDecimalDigitForUnitPrice(), product);
    List<CostSheetLine> costSheetLineList = parentCostSheetLine.getCostSheetLineList() != null ? parentCostSheetLine.getCostSheetLineList() : new ArrayList<CostSheetLine>();
    for (CostSheetLine costSheetLine : costSheetLineList) {
        if (product.equals(costSheetLine.getProduct()) && unit.equals(costSheetLine.getUnit())) {
            BigDecimal qty = costSheetLine.getConsumptionQty().add(consumptionQty);
            costSheetLine.setConsumptionQty(qty);
            costSheetLine.setCostPrice(costPrice.add(costSheetLine.getCostPrice()).setScale(appProductionService.getNbDecimalDigitForUnitPrice(), BigDecimal.ROUND_HALF_UP));
            return costSheetLine;
        }
    }
    return this.createCostSheetLine(product.getName(), product.getCode(), bomLevel, consumptionQty, costPrice, product.getCostSheetGroup(), product, CostSheetLineRepository.TYPE_CONSUMED_PRODUCT, CostSheetLineRepository.TYPE_CONSUMED_PRODUCT, unit, null, parentCostSheetLine);
}
Also used : BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) CostSheetLine(com.axelor.apps.production.db.CostSheetLine) UnitCostCalcLine(com.axelor.apps.production.db.UnitCostCalcLine) Product(com.axelor.apps.base.db.Product) BigDecimal(java.math.BigDecimal)

Example 4 with UnitCostCalcLine

use of com.axelor.apps.production.db.UnitCostCalcLine in project axelor-open-suite by axelor.

the class UnitCostCalcLineServiceImpl method createUnitCostCalcLine.

public UnitCostCalcLine createUnitCostCalcLine(Product product, Company company, int maxLevel, CostSheet costSheet) throws AxelorException {
    UnitCostCalcLine unitCostCalcLine = new UnitCostCalcLine();
    unitCostCalcLine.setProduct(product);
    unitCostCalcLine.setCompany(company);
    unitCostCalcLine.setPreviousCost((BigDecimal) productCompanyService.get(product, "costPrice", company));
    unitCostCalcLine.setCostSheet(costSheet);
    unitCostCalcLine.setComputedCost(costSheet.getCostPrice());
    unitCostCalcLine.setCostToApply(costSheet.getCostPrice());
    unitCostCalcLine.setMaxLevel(maxLevel);
    return unitCostCalcLine;
}
Also used : UnitCostCalcLine(com.axelor.apps.production.db.UnitCostCalcLine)

Example 5 with UnitCostCalcLine

use of com.axelor.apps.production.db.UnitCostCalcLine in project axelor-open-suite by axelor.

the class UnitCostCalculationServiceImpl method exportUnitCostCalc.

@Override
public MetaFile exportUnitCostCalc(UnitCostCalculation unitCostCalculation, String fileName) throws IOException {
    List<String[]> list = new ArrayList<>();
    List<UnitCostCalcLine> unitCostCalcLineList = unitCostCalculation.getUnitCostCalcLineList();
    Collections.sort(unitCostCalcLineList, new Comparator<UnitCostCalcLine>() {

        @Override
        public int compare(UnitCostCalcLine unitCostCalcLine1, UnitCostCalcLine unitCostCalcLine2) {
            return unitCostCalcLine1.getProduct().getCode().compareTo(unitCostCalcLine2.getProduct().getCode());
        }
    });
    for (UnitCostCalcLine unitCostCalcLine : unitCostCalcLineList) {
        String[] item = new String[4];
        item[0] = unitCostCalcLine.getProduct() == null ? "" : unitCostCalcLine.getProduct().getCode();
        item[1] = unitCostCalcLine.getProduct() == null ? "" : unitCostCalcLine.getProduct().getName();
        item[2] = unitCostCalcLine.getComputedCost().toString();
        item[3] = unitCostCalcLine.getCostToApply().toString();
        list.add(item);
    }
    File file = MetaFiles.createTempFile(fileName, ".csv").toFile();
    log.debug("File located at: {}", file.getPath());
    String[] headers = { I18n.get("Product_code"), I18n.get("Product_name"), I18n.get("Product_currency"), I18n.get("Computed_cost"), I18n.get("Cost_to_apply") };
    CsvTool.csvWriter(file.getParent(), file.getName(), ';', '"', headers, list);
    try (InputStream is = new FileInputStream(file)) {
        DMSFile dmsFile = Beans.get(MetaFiles.class).attach(is, file.getName(), unitCostCalculation);
        return dmsFile.getMetaFile();
    }
}
Also used : MetaFiles(com.axelor.meta.MetaFiles) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) UnitCostCalcLine(com.axelor.apps.production.db.UnitCostCalcLine) DMSFile(com.axelor.dms.db.DMSFile) DMSFile(com.axelor.dms.db.DMSFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile)

Aggregations

UnitCostCalcLine (com.axelor.apps.production.db.UnitCostCalcLine)6 BillOfMaterial (com.axelor.apps.production.db.BillOfMaterial)3 Product (com.axelor.apps.base.db.Product)2 BigDecimal (java.math.BigDecimal)2 CostSheet (com.axelor.apps.production.db.CostSheet)1 CostSheetLine (com.axelor.apps.production.db.CostSheetLine)1 DMSFile (com.axelor.dms.db.DMSFile)1 MetaFiles (com.axelor.meta.MetaFiles)1 MetaFile (com.axelor.meta.db.MetaFile)1 Transactional (com.google.inject.persist.Transactional)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1