Search in sources :

Example 1 with CostCalculationMaterial

use of com.qcadoo.mes.costCalculation.print.dto.CostCalculationMaterial in project mes by qcadoo.

the class CostCalculationMaterialsService method groupMaterialCosts.

private List<CostCalculationMaterial> groupMaterialCosts(List<CostCalculationMaterial> materialCosts) {
    List<CostCalculationMaterial> groupedMaterialCostsList = new ArrayList<>();
    Map<MaterialCostKey, CostCalculationMaterial> groupedMaterialCosts = new HashMap<>();
    for (CostCalculationMaterial costCalculationMaterial : materialCosts) {
        if (costCalculationMaterial.isDifferentProductsInDifferentSizes()) {
            groupedMaterialCostsList.add(costCalculationMaterial);
        } else {
            MaterialCostKey materialCostKey = new MaterialCostKey(costCalculationMaterial.getProductNumber(), costCalculationMaterial.getProductName(), costCalculationMaterial.getUnit(), costCalculationMaterial.getTechnologyInputProductType());
            if (groupedMaterialCosts.containsKey(materialCostKey)) {
                CostCalculationMaterial groupedMaterialCost = groupedMaterialCosts.get(materialCostKey);
                groupedMaterialCost.setProductQuantity(groupedMaterialCost.getProductQuantity().add(costCalculationMaterial.getProductQuantity(), numberService.getMathContext()));
                groupedMaterialCost.setCostForGivenQuantity(groupedMaterialCost.getCostForGivenQuantity().add(costCalculationMaterial.getCostForGivenQuantity(), numberService.getMathContext()));
                groupedMaterialCosts.put(materialCostKey, groupedMaterialCost);
            } else {
                groupedMaterialCosts.put(materialCostKey, costCalculationMaterial);
            }
        }
    }
    groupedMaterialCostsList.addAll(groupedMaterialCosts.values());
    groupedMaterialCostsList.sort(Comparator.comparing(CostCalculationMaterial::getCostForGivenQuantity));
    return groupedMaterialCostsList;
}
Also used : HashMap(java.util.HashMap) CostCalculationMaterial(com.qcadoo.mes.costCalculation.print.dto.CostCalculationMaterial) ArrayList(java.util.ArrayList) MaterialCostKey(com.qcadoo.mes.costCalculation.print.dto.MaterialCostKey)

Example 2 with CostCalculationMaterial

use of com.qcadoo.mes.costCalculation.print.dto.CostCalculationMaterial in project mes by qcadoo.

the class CostCalculationMaterialsService method getSortedMaterialsFromProductQuantities.

public List<CostCalculationMaterial> getSortedMaterialsFromProductQuantities(final Entity costCalculation, final Entity technology) {
    List<CostCalculationMaterial> materialCosts = Lists.newArrayList();
    BigDecimal quantity = costCalculation.getDecimalField(CostCalculationFields.QUANTITY);
    Map<OperationProductComponentHolder, BigDecimal> materialQuantitiesByOPC = getNeededProductQuantitiesByOPC(costCalculation, technology, quantity);
    DataDefinition operationProductComponentDD = dataDefinitionService.get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_OPERATION_PRODUCT_IN_COMPONENT);
    String technologyNumber = technology.getStringField(TechnologyFields.NUMBER);
    String finalProductNumber = technology.getBelongsToField(TechnologyFields.PRODUCT).getStringField(ProductFields.NUMBER);
    for (Map.Entry<OperationProductComponentHolder, BigDecimal> neededProductQuantity : materialQuantitiesByOPC.entrySet()) {
        Entity product = neededProductQuantity.getKey().getProduct();
        Entity operationProductComponent = operationProductComponentDD.get(neededProductQuantity.getKey().getOperationProductComponentId());
        BigDecimal costPerUnit = productsCostCalculationService.calculateOperationProductCostPerUnit(costCalculation, product, operationProductComponent);
        BigDecimal productQuantity = neededProductQuantity.getValue();
        BigDecimal costForGivenQuantity = costPerUnit.multiply(BigDecimalUtils.convertNullToZero(productQuantity), numberService.getMathContext());
        if (operationProductComponent.getBooleanField(OperationProductInComponentFields.DIFFERENT_PRODUCTS_IN_DIFFERENT_SIZES)) {
            List<Entity> productsBySize = operationProductComponent.getHasManyField(OperationProductInComponentFields.PRODUCT_BY_SIZE_GROUPS);
            if (!operationProductComponent.getBooleanField(OperationProductInComponentFields.VARIOUS_QUANTITIES_IN_PRODUCTS_BY_SIZE)) {
                productQuantity = costCalculation.getDecimalField(CostCalculationFields.QUANTITY).multiply(productsBySize.stream().findFirst().get().getDecimalField(ProductBySizeGroupFields.QUANTITY), numberService.getMathContext());
            }
            BigDecimal sumOfCosts = BigDecimal.ZERO;
            for (Entity pbs : productsBySize) {
                Entity p = pbs.getBelongsToField(ProductBySizeGroupFields.PRODUCT);
                BigDecimal costPerUnitPBS = productsCostCalculationService.calculateProductCostPerUnit(p, costCalculation.getStringField(CostCalculationFields.MATERIAL_COSTS_USED), costCalculation.getBooleanField(CostCalculationFields.USE_NOMINAL_COST_PRICE_NOT_SPECIFIED));
                BigDecimal q = costCalculation.getDecimalField(CostCalculationFields.QUANTITY).multiply(pbs.getDecimalField(ProductBySizeGroupFields.QUANTITY), numberService.getMathContext());
                BigDecimal costPBS = numberService.setScaleWithDefaultMathContext(costPerUnitPBS.multiply(q));
                sumOfCosts = sumOfCosts.add(costPBS, numberService.getMathContext());
            }
            costForGivenQuantity = sumOfCosts.divide(new BigDecimal(productsBySize.size()), numberService.getMathContext());
        }
        String technologyInputProductTypeName = "";
        Entity technologyInputProductType = operationProductComponent.getBelongsToField(OperationProductInComponentFields.TECHNOLOGY_INPUT_PRODUCT_TYPE);
        if (technologyInputProductType != null) {
            technologyInputProductTypeName = technologyInputProductType.getStringField(TechnologyInputProductTypeFields.NAME);
        }
        CostCalculationMaterial costCalculationMaterial = new CostCalculationMaterial();
        costCalculationMaterial.setTechnologyNumber(technologyNumber);
        costCalculationMaterial.setFinalProductNumber(finalProductNumber);
        costCalculationMaterial.setProductQuantity(productQuantity);
        costCalculationMaterial.setCostPerUnit(costPerUnit);
        costCalculationMaterial.setCostForGivenQuantity(costForGivenQuantity);
        costCalculationMaterial.setTechnologyInputProductType(technologyInputProductTypeName);
        costCalculationMaterial.setDifferentProductsInDifferentSizes(operationProductComponent.getBooleanField(OperationProductInComponentFields.DIFFERENT_PRODUCTS_IN_DIFFERENT_SIZES));
        if (product != null) {
            costCalculationMaterial.setUnit(product.getStringField(ProductFields.UNIT));
            costCalculationMaterial.setProductNumber(product.getStringField(ProductFields.NUMBER));
            costCalculationMaterial.setProductName(product.getStringField(ProductFields.NAME));
        } else {
            costCalculationMaterial.setUnit(operationProductComponent.getStringField(OperationProductInComponentFields.GIVEN_UNIT));
            costCalculationMaterial.setProductNumber("");
            costCalculationMaterial.setProductName("");
        }
        if (operationProductComponent.getBooleanField(OperationProductInComponentFields.VARIOUS_QUANTITIES_IN_PRODUCTS_BY_SIZE)) {
            BigDecimal sumOfQuantity = operationProductComponent.getHasManyField(OperationProductInComponentFields.PRODUCT_BY_SIZE_GROUPS).stream().map(q -> q.getDecimalField(ProductBySizeGroupFields.QUANTITY)).reduce(BigDecimal.ZERO, BigDecimal::add);
            costForGivenQuantity = costForGivenQuantity.divide(sumOfQuantity, numberService.getMathContext());
            costCalculationMaterial.setProductQuantity(null);
            costCalculationMaterial.setCostPerUnit(costForGivenQuantity);
        }
        materialCosts.add(costCalculationMaterial);
    }
    return groupMaterialCosts(materialCosts);
}
Also used : OperationProductComponentHolder(com.qcadoo.mes.technologies.dto.OperationProductComponentHolder) TechnologyFields(com.qcadoo.mes.technologies.constants.TechnologyFields) DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) Autowired(org.springframework.beans.factory.annotation.Autowired) BigDecimalUtils(com.qcadoo.model.api.BigDecimalUtils) HashMap(java.util.HashMap) ProductQuantitiesWithComponentsService(com.qcadoo.mes.technologies.ProductQuantitiesWithComponentsService) ArrayList(java.util.ArrayList) CostCalculationFields(com.qcadoo.mes.costCalculation.constants.CostCalculationFields) BigDecimal(java.math.BigDecimal) Lists(com.google.common.collect.Lists) PluginManager(com.qcadoo.plugin.api.PluginManager) MrpAlgorithm(com.qcadoo.mes.technologies.constants.MrpAlgorithm) Service(org.springframework.stereotype.Service) Map(java.util.Map) ProductQuantitiesService(com.qcadoo.mes.technologies.ProductQuantitiesService) OperationProductInComponentFields(com.qcadoo.mes.technologies.constants.OperationProductInComponentFields) CostCalculationMaterial(com.qcadoo.mes.costCalculation.print.dto.CostCalculationMaterial) OperationProductComponentHolder(com.qcadoo.mes.technologies.dto.OperationProductComponentHolder) DataDefinition(com.qcadoo.model.api.DataDefinition) ProductBySizeGroupFields(com.qcadoo.mes.technologies.constants.ProductBySizeGroupFields) Entity(com.qcadoo.model.api.Entity) List(java.util.List) TechnologyInputProductTypeFields(com.qcadoo.mes.technologies.constants.TechnologyInputProductTypeFields) NumberService(com.qcadoo.model.api.NumberService) ProductFields(com.qcadoo.mes.basic.constants.ProductFields) TechnologiesConstants(com.qcadoo.mes.technologies.constants.TechnologiesConstants) Comparator(java.util.Comparator) MaterialCostKey(com.qcadoo.mes.costCalculation.print.dto.MaterialCostKey) Entity(com.qcadoo.model.api.Entity) CostCalculationMaterial(com.qcadoo.mes.costCalculation.print.dto.CostCalculationMaterial) DataDefinition(com.qcadoo.model.api.DataDefinition) HashMap(java.util.HashMap) Map(java.util.Map) BigDecimal(java.math.BigDecimal)

Example 3 with CostCalculationMaterial

use of com.qcadoo.mes.costCalculation.print.dto.CostCalculationMaterial in project mes by qcadoo.

the class CostCalculationXlsService method addExtraSheets.

@Override
protected void addExtraSheets(final HSSFWorkbook workbook, Entity entity, Locale locale) {
    List<CostCalculationMaterial> materialCosts = Lists.newArrayList();
    List<ComponentsCalculationHolder> componentCosts = Lists.newArrayList();
    List<Entity> calculationOperationComponents = Lists.newArrayList();
    List<Entity> calculationResults = Lists.newArrayList();
    Map<Long, Boolean> hasComponents = Maps.newHashMap();
    boolean includeComponents = entity.getBooleanField(CostCalculationFields.INCLUDE_COMPONENTS);
    HSSFSheet sheet = workbook.getSheetAt(0);
    final FontsContainer fontsContainer = new FontsContainer(sheet.getWorkbook());
    final StylesContainer stylesContainer = new StylesContainer(sheet.getWorkbook(), fontsContainer);
    for (Entity technology : entity.getHasManyField(CostCalculationFields.TECHNOLOGIES)) {
        List<CostCalculationMaterial> technologyMaterialCosts = costCalculationMaterialsService.getSortedMaterialsFromProductQuantities(entity, technology);
        materialCosts.addAll(technologyMaterialCosts);
        BigDecimal technologyMaterialsCostsSum = BigDecimal.ZERO;
        boolean noMaterialPrice = false;
        for (CostCalculationMaterial technologyMaterialCost : technologyMaterialCosts) {
            BigDecimal costForGivenQuantity = technologyMaterialCost.getCostForGivenQuantity();
            if (BigDecimalUtils.valueEquals(costForGivenQuantity, BigDecimal.ZERO)) {
                noMaterialPrice = true;
            }
            technologyMaterialsCostsSum = technologyMaterialsCostsSum.add(costForGivenQuantity, numberService.getMathContext());
        }
        BigDecimal labourCost;
        if (SourceOfOperationCosts.STANDARD_LABOR_COSTS.equals(SourceOfOperationCosts.parseString(entity.getStringField(CostCalculationFields.SOURCE_OF_OPERATION_COSTS)))) {
            labourCost = entity.getBelongsToField(CostCalculationFields.STANDARD_LABOR_COST).getDecimalField(StandardLaborCostFields.LABOR_COST);
        } else {
            labourCost = operationsCostCalculationService.calculateOperationsCost(entity, technology);
            List<Entity> technologyCalculationOperationComponents = entity.getHasManyField(CostCalculationFields.CALCULATION_OPERATION_COMPONENTS);
            technologyCalculationOperationComponents.forEach(e -> e.setField(CalculationOperationComponentFields.TECHNOLOGY, technology));
            calculationOperationComponents.addAll(technologyCalculationOperationComponents);
        }
        calculationResults.add(costCalculationService.createCalculationResults(entity, technology, technologyMaterialsCostsSum, labourCost, noMaterialPrice));
    }
    if (includeComponents) {
        for (Entity technology : entity.getHasManyField(CostCalculationFields.TECHNOLOGIES)) {
            Collection<ComponentsCalculationHolder> technologyComponentCosts = costCalculationComponentsService.getComponentCosts(entity, technology, calculationOperationComponents);
            componentCosts.addAll(technologyComponentCosts);
            hasComponents.put(technology.getId(), !technologyComponentCosts.isEmpty());
        }
        createComponentCosts(entity, componentCosts);
    }
    createCalculationResultsSheet(sheet, entity, calculationResults, hasComponents, stylesContainer, locale);
    createMaterialCostsSheet(materialCosts, createSheet(workbook, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialCosts", locale)), locale);
    createMaterialsBySizeSheet(entity, createSheet(workbook, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialsBySize", locale)), locale);
    if (!SourceOfOperationCosts.STANDARD_LABOR_COSTS.equals(SourceOfOperationCosts.parseString(entity.getStringField(CostCalculationFields.SOURCE_OF_OPERATION_COSTS)))) {
        createLabourCostSheet(calculationOperationComponents, createSheet(workbook, translationService.translate("costCalculation.costCalculation.report.xls.sheet.labourCost", locale)), locale);
    }
    if (includeComponents) {
        createComponentCostsSheet(componentCosts, createSheet(workbook, translationService.translate("costCalculation.costCalculation.report.xls.sheet.componentCosts", locale)), locale);
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) CostCalculationMaterial(com.qcadoo.mes.costCalculation.print.dto.CostCalculationMaterial) BigDecimal(java.math.BigDecimal) ComponentsCalculationHolder(com.qcadoo.mes.costCalculation.print.dto.ComponentsCalculationHolder) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet)

Example 4 with CostCalculationMaterial

use of com.qcadoo.mes.costCalculation.print.dto.CostCalculationMaterial in project mes by qcadoo.

the class CostCalculationXlsService method createMaterialCostsSheet.

private void createMaterialCostsSheet(List<CostCalculationMaterial> materialCosts, HSSFSheet sheet, Locale locale) {
    final FontsContainer fontsContainer = new FontsContainer(sheet.getWorkbook());
    final StylesContainer stylesContainer = new StylesContainer(sheet.getWorkbook(), fontsContainer);
    final int rowOffset = 1;
    HSSFRow row = sheet.createRow(0);
    createHeaderCell(stylesContainer, row, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialCosts.technologyNumber", locale), 0);
    createHeaderCell(stylesContainer, row, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialCosts.productNumber", locale), 1);
    createHeaderCell(stylesContainer, row, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialCosts.technologyInputProductType", locale), 2);
    createHeaderCell(stylesContainer, row, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialCosts.differentProductsInDifferentSizes", locale), 3);
    createHeaderCell(stylesContainer, row, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialCosts.componentNumber", locale), 4);
    createHeaderCell(stylesContainer, row, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialCosts.componentName", locale), 5);
    createHeaderCell(stylesContainer, row, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialCosts.quantity", locale), 6);
    createHeaderCell(stylesContainer, row, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialCosts.unit", locale), 7);
    createHeaderCell(stylesContainer, row, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialCosts.costPerUnit", locale), 8);
    createHeaderCell(stylesContainer, row, translationService.translate("costCalculation.costCalculation.report.xls.sheet.materialCosts.cost", locale), 9);
    int rowCounter = 0;
    for (CostCalculationMaterial materialCost : materialCosts) {
        row = sheet.createRow(rowOffset + rowCounter);
        createRegularCell(stylesContainer, row, 0, materialCost.getTechnologyNumber());
        createRegularCell(stylesContainer, row, 1, materialCost.getFinalProductNumber());
        createRegularCell(stylesContainer, row, 2, materialCost.getTechnologyInputProductType());
        createRegularCell(stylesContainer, row, 3, materialCost.isDifferentProductsInDifferentSizes() ? translationService.translate("qcadooView.true", locale) : translationService.translate("qcadooView.false", locale));
        createRegularCell(stylesContainer, row, 4, materialCost.getProductNumber());
        createRegularCell(stylesContainer, row, 5, materialCost.getProductName());
        createNumericWithNullCell(stylesContainer, row, 6, materialCost.getProductQuantity());
        createRegularCell(stylesContainer, row, 7, materialCost.getUnit());
        createNumericCell(stylesContainer, row, 8, materialCost.getCostPerUnit());
        createNumericCell(stylesContainer, row, 9, materialCost.getCostForGivenQuantity());
        rowCounter++;
    }
    for (int i = 0; i <= 9; i++) {
        sheet.autoSizeColumn(i, false);
    }
}
Also used : CostCalculationMaterial(com.qcadoo.mes.costCalculation.print.dto.CostCalculationMaterial) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow)

Aggregations

CostCalculationMaterial (com.qcadoo.mes.costCalculation.print.dto.CostCalculationMaterial)4 MaterialCostKey (com.qcadoo.mes.costCalculation.print.dto.MaterialCostKey)2 Entity (com.qcadoo.model.api.Entity)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Lists (com.google.common.collect.Lists)1 ProductFields (com.qcadoo.mes.basic.constants.ProductFields)1 CostCalculationFields (com.qcadoo.mes.costCalculation.constants.CostCalculationFields)1 ComponentsCalculationHolder (com.qcadoo.mes.costCalculation.print.dto.ComponentsCalculationHolder)1 ProductQuantitiesService (com.qcadoo.mes.technologies.ProductQuantitiesService)1 ProductQuantitiesWithComponentsService (com.qcadoo.mes.technologies.ProductQuantitiesWithComponentsService)1 MrpAlgorithm (com.qcadoo.mes.technologies.constants.MrpAlgorithm)1 OperationProductInComponentFields (com.qcadoo.mes.technologies.constants.OperationProductInComponentFields)1 ProductBySizeGroupFields (com.qcadoo.mes.technologies.constants.ProductBySizeGroupFields)1 TechnologiesConstants (com.qcadoo.mes.technologies.constants.TechnologiesConstants)1 TechnologyFields (com.qcadoo.mes.technologies.constants.TechnologyFields)1 TechnologyInputProductTypeFields (com.qcadoo.mes.technologies.constants.TechnologyInputProductTypeFields)1 OperationProductComponentHolder (com.qcadoo.mes.technologies.dto.OperationProductComponentHolder)1 BigDecimalUtils (com.qcadoo.model.api.BigDecimalUtils)1