use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class ManufOrderServiceImpl method merge.
@Transactional(rollbackOn = { Exception.class })
public void merge(List<Long> ids) throws AxelorException {
if (!canMerge(ids)) {
throw new AxelorException(ManufOrder.class, TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.MANUF_ORDER_NO_GENERATION));
}
List<ManufOrder> manufOrderList = manufOrderRepo.all().filter("self.id in (" + Joiner.on(",").join(ids) + ")").fetch();
/* Init all the necessary values to create the new Manuf Order */
Product product = manufOrderList.get(0).getProduct();
StockLocation stockLocation = manufOrderList.get(0).getWorkshopStockLocation();
Company company = manufOrderList.get(0).getCompany();
BillOfMaterial billOfMaterial = manufOrderList.stream().filter(x -> x.getBillOfMaterial().getVersionNumber() == 1).findFirst().get().getBillOfMaterial();
int priority = manufOrderList.stream().mapToInt(ManufOrder::getPrioritySelect).max().orElse(2);
Unit unit = billOfMaterial.getUnit();
BigDecimal qty = BigDecimal.ZERO;
String note = "";
ManufOrder mergedManufOrder = new ManufOrder();
for (ManufOrder manufOrder : manufOrderList) {
manufOrder.setStatusSelect(ManufOrderRepository.STATUS_MERGED);
manufOrder.setManufOrderMergeResult(mergedManufOrder);
for (ProductionOrder productionOrder : manufOrder.getProductionOrderSet()) {
mergedManufOrder.addProductionOrderSetItem(productionOrder);
}
for (SaleOrder saleOrder : manufOrder.getSaleOrderSet()) {
mergedManufOrder.addSaleOrderSetItem(saleOrder);
}
/*
* If unit are the same, then add the qty If not, convert the unit and get the converted qty
*/
if (manufOrder.getUnit().equals(unit)) {
qty = qty.add(manufOrder.getQty());
} else {
BigDecimal qtyConverted = Beans.get(UnitConversionService.class).convert(manufOrder.getUnit(), unit, manufOrder.getQty(), appBaseService.getNbDecimalDigitForQty(), null);
qty = qty.add(qtyConverted);
}
if (manufOrder.getNote() != null && !manufOrder.getNote().equals("")) {
note += manufOrder.getManufOrderSeq() + " : " + manufOrder.getNote() + "\n";
}
}
Optional<LocalDateTime> minDate = manufOrderList.stream().filter(mo -> mo.getPlannedStartDateT() != null).map(ManufOrder::getPlannedStartDateT).min(LocalDateTime::compareTo);
minDate.ifPresent(mergedManufOrder::setPlannedStartDateT);
/* Update the created manuf order */
mergedManufOrder.setStatusSelect(ManufOrderRepository.STATUS_DRAFT);
mergedManufOrder.setProduct(product);
mergedManufOrder.setUnit(unit);
mergedManufOrder.setWorkshopStockLocation(stockLocation);
mergedManufOrder.setQty(qty);
mergedManufOrder.setBillOfMaterial(billOfMaterial);
mergedManufOrder.setCompany(company);
mergedManufOrder.setPrioritySelect(priority);
mergedManufOrder.setProdProcess(billOfMaterial.getProdProcess());
mergedManufOrder.setNote(note);
/*
* Check the config to see if you directly plan the created manuf order or just prefill the
* operations
*/
if (appProductionService.isApp("production") && appProductionService.getAppProduction().getIsManufOrderPlannedAfterMerge()) {
manufOrderWorkflowService.plan(mergedManufOrder);
} else {
preFillOperations(mergedManufOrder);
}
manufOrderRepo.save(mergedManufOrder);
}
use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class ProductionOrderSaleOrderServiceImpl method generateManufOrders.
@Override
public ProductionOrder generateManufOrders(ProductionOrder productionOrder, SaleOrderLine saleOrderLine) throws AxelorException {
Product product = saleOrderLine.getProduct();
if (saleOrderLine.getSaleSupplySelect() == ProductRepository.SALE_SUPPLY_PRODUCE && product != null && product.getProductTypeSelect().equals(ProductRepository.PRODUCT_TYPE_STORABLE)) {
BillOfMaterial billOfMaterial = saleOrderLine.getBillOfMaterial();
if (billOfMaterial == null) {
billOfMaterial = product.getDefaultBillOfMaterial();
}
if (billOfMaterial == null && product.getParentProduct() != null) {
billOfMaterial = product.getParentProduct().getDefaultBillOfMaterial();
}
if (billOfMaterial == null) {
throw new AxelorException(saleOrderLine, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.PRODUCTION_ORDER_SALES_ORDER_NO_BOM), product.getName(), product.getCode());
}
if (billOfMaterial.getProdProcess() == null) {
return null;
}
Unit unit = saleOrderLine.getProduct().getUnit();
BigDecimal qty = saleOrderLine.getQty();
if (unit != null && !unit.equals(saleOrderLine.getUnit())) {
qty = unitConversionService.convert(saleOrderLine.getUnit(), unit, qty, qty.scale(), saleOrderLine.getProduct());
}
return generateManufOrders(productionOrder, billOfMaterial, qty, LocalDateTime.now(), saleOrderLine.getSaleOrder());
}
return null;
}
use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class ManufOrderWorkflowService method createPurchaseOrderLineProduction.
private void createPurchaseOrderLineProduction(OperationOrder operationOrder, PurchaseOrder purchaseOrder) throws AxelorException {
UnitConversionService unitConversionService = Beans.get(UnitConversionService.class);
PurchaseOrderLineService purchaseOrderLineService = Beans.get(PurchaseOrderLineService.class);
PurchaseOrderLine purchaseOrderLine;
BigDecimal quantity = BigDecimal.ONE;
Unit startUnit = Beans.get(UnitRepository.class).all().filter("self.name = 'Hour' AND self.unitTypeSelect = 3").fetchOne();
for (ProdHumanResource humanResource : operationOrder.getProdHumanResourceList()) {
Product product = humanResource.getProduct();
Unit purchaseUnit = product.getPurchasesUnit();
if (purchaseUnit != null) {
quantity = unitConversionService.convert(startUnit, purchaseUnit, new BigDecimal(humanResource.getDuration() / 3600), 0, humanResource.getProduct());
}
purchaseOrderLine = purchaseOrderLineService.createPurchaseOrderLine(purchaseOrder, product, null, null, quantity, purchaseUnit);
purchaseOrder.getPurchaseOrderLineList().add(purchaseOrderLine);
}
}
use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class CostSheetServiceImpl method getTotalQtyPerProductAndUnit.
protected Map<List<Object>, BigDecimal> getTotalQtyPerProductAndUnit(List<StockMoveLine> stockMoveLineList, LocalDate calculationDate, LocalDate previousCostSheetDate, int calculationType) {
Map<List<Object>, BigDecimal> stockMoveLinePerProductAndUnitMap = new HashMap<>();
if (stockMoveLineList == null) {
return stockMoveLinePerProductAndUnitMap;
}
for (StockMoveLine stockMoveLine : stockMoveLineList) {
StockMove stockMove = stockMoveLine.getStockMove();
if (stockMove == null || StockMoveRepository.STATUS_REALIZED != stockMoveLine.getStockMove().getStatusSelect()) {
continue;
}
if ((calculationType == CostSheetRepository.CALCULATION_PARTIAL_END_OF_PRODUCTION || calculationType == CostSheetRepository.CALCULATION_END_OF_PRODUCTION) && previousCostSheetDate != null && !previousCostSheetDate.isBefore(stockMove.getRealDate())) {
continue;
} else if (calculationType == CostSheetRepository.CALCULATION_WORK_IN_PROGRESS && calculationDate.isBefore(stockMove.getRealDate())) {
continue;
}
Product productKey = stockMoveLine.getProduct();
Unit unitKey = stockMoveLine.getUnit();
List<Object> keys = new ArrayList<Object>();
keys.add(productKey);
keys.add(unitKey);
BigDecimal qty = stockMoveLinePerProductAndUnitMap.get(keys);
if (qty == null) {
qty = BigDecimal.ZERO;
}
stockMoveLinePerProductAndUnitMap.put(keys, qty.add(stockMoveLine.getRealQty()));
}
return stockMoveLinePerProductAndUnitMap;
}
use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class CostSheetServiceImpl method computeConsumedProduct.
protected void computeConsumedProduct(int bomLevel, LocalDate previousCostSheetDate, CostSheetLine parentCostSheetLine, List<StockMoveLine> consumedStockMoveLineList, List<ProdProduct> toConsumeProdProductList, BigDecimal ratio) throws AxelorException {
CostSheet parentCostSheet = parentCostSheetLine.getCostSheet();
int calculationTypeSelect = parentCostSheet.getCalculationTypeSelect();
LocalDate calculationDate = parentCostSheet.getCalculationDate();
Map<List<Object>, BigDecimal> consumedStockMoveLinePerProductAndUnit = getTotalQtyPerProductAndUnit(consumedStockMoveLineList, calculationDate, previousCostSheetDate, calculationTypeSelect);
for (List<Object> keys : consumedStockMoveLinePerProductAndUnit.keySet()) {
Iterator<Object> iterator = keys.iterator();
Product product = (Product) iterator.next();
Unit unit = (Unit) iterator.next();
BigDecimal realQty = consumedStockMoveLinePerProductAndUnit.get(keys);
if (product == null) {
continue;
}
BigDecimal valuationQty = BigDecimal.ZERO;
if (calculationTypeSelect == CostSheetRepository.CALCULATION_WORK_IN_PROGRESS) {
BigDecimal plannedConsumeQty = computeTotalQtyPerUnit(toConsumeProdProductList, product, unit);
valuationQty = realQty.subtract(plannedConsumeQty.multiply(ratio));
}
valuationQty = valuationQty.setScale(appBaseService.getNbDecimalDigitForQty(), RoundingMode.HALF_UP);
if (valuationQty.compareTo(BigDecimal.ZERO) == 0) {
continue;
}
costSheetLineService.createConsumedProductCostSheetLine(parentCostSheet.getManufOrder().getCompany(), product, unit, bomLevel, parentCostSheetLine, valuationQty, CostSheetService.ORIGIN_MANUF_ORDER, null);
}
}
Aggregations