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;
}
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);
}
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);
}
}
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);
}
}
Aggregations