use of com.axelor.apps.purchase.service.PurchaseOrderService in project axelor-open-suite by axelor.
the class IntercoServiceImpl method generateIntercoPurchaseFromSale.
@Override
@Transactional(rollbackOn = { Exception.class })
public PurchaseOrder generateIntercoPurchaseFromSale(SaleOrder saleOrder) throws AxelorException {
PurchaseOrderService purchaseOrderService = Beans.get(PurchaseOrderService.class);
Company intercoCompany = findIntercoCompany(saleOrder.getClientPartner());
// create purchase order
PurchaseOrder purchaseOrder = new PurchaseOrder();
purchaseOrder.setCompany(intercoCompany);
purchaseOrder.setContactPartner(saleOrder.getContactPartner());
purchaseOrder.setCurrency(saleOrder.getCurrency());
purchaseOrder.setDeliveryDate(saleOrder.getDeliveryDate());
purchaseOrder.setOrderDate(saleOrder.getCreationDate());
purchaseOrder.setPriceList(saleOrder.getPriceList());
purchaseOrder.setTradingName(saleOrder.getTradingName());
purchaseOrder.setPurchaseOrderLineList(new ArrayList<>());
purchaseOrder.setPrintingSettings(Beans.get(TradingNameService.class).getDefaultPrintingSettings(null, intercoCompany));
purchaseOrder.setStatusSelect(PurchaseOrderRepository.STATUS_DRAFT);
purchaseOrder.setSupplierPartner(saleOrder.getCompany().getPartner());
purchaseOrder.setTradingName(saleOrder.getTradingName());
// in ati
purchaseOrder.setInAti(saleOrder.getInAti());
// copy payments
PaymentMode intercoPaymentMode = Beans.get(PaymentModeService.class).reverseInOut(saleOrder.getPaymentMode());
purchaseOrder.setPaymentMode(intercoPaymentMode);
purchaseOrder.setPaymentCondition(saleOrder.getPaymentCondition());
// copy delivery info
purchaseOrder.setDeliveryDate(saleOrder.getDeliveryDate());
purchaseOrder.setStockLocation(Beans.get(StockLocationService.class).getDefaultReceiptStockLocation(intercoCompany));
purchaseOrder.setShipmentMode(saleOrder.getShipmentMode());
purchaseOrder.setFreightCarrierMode(saleOrder.getFreightCarrierMode());
// copy timetable info
purchaseOrder.setExpectedRealisationDate(saleOrder.getExpectedRealisationDate());
purchaseOrder.setAmountToBeSpreadOverTheTimetable(saleOrder.getAmountToBeSpreadOverTheTimetable());
// create lines
List<SaleOrderLine> saleOrderLineList = saleOrder.getSaleOrderLineList();
if (saleOrderLineList != null) {
for (SaleOrderLine saleOrderLine : saleOrderLineList) {
this.createIntercoPurchaseLineFromSaleLine(saleOrderLine, purchaseOrder);
}
}
purchaseOrder.setPrintingSettings(intercoCompany.getPrintingSettings());
// compute the purchase order
purchaseOrderService.computePurchaseOrder(purchaseOrder);
purchaseOrder.setCreatedByInterco(true);
Beans.get(PurchaseOrderRepository.class).save(purchaseOrder);
if (Beans.get(AppSupplychainService.class).getAppSupplychain().getIntercoPurchaseOrderCreateRequested()) {
Beans.get(PurchaseOrderService.class).requestPurchaseOrder(purchaseOrder);
}
saleOrder.setExternalReference(purchaseOrder.getPurchaseOrderSeq());
purchaseOrder.setExternalReference(saleOrder.getSaleOrderSeq());
return purchaseOrder;
}
Aggregations