Dossier importation : divise la ligne bc en 2

This commit is contained in:
walid seghier
2022-09-18 10:13:37 +01:00
parent 5d709144fd
commit cfdd98edda
2 changed files with 98 additions and 0 deletions

View File

@@ -35,6 +35,12 @@ import com.axelor.inject.Beans;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import java.math.BigDecimal;
import com.axelor.apps.purchase.db.PurchaseOrder;
import com.axelor.apps.purchase.db.repo.PurchaseOrderLineRepository;
import com.axelor.apps.purchase.service.PurchaseOrderLineService;
import java.util.List;
import java.math.RoundingMode;
import com.axelor.apps.base.service.app.AppBaseService;
public class PurchaseOrderSupplierLineService {
@@ -42,6 +48,12 @@ public class PurchaseOrderSupplierLineService {
@Inject protected AppPurchaseService appPurchaseService;
@Inject private PurchaseOrderLineRepository purchaseOrderLineRepo;
@Inject private PurchaseOrderLineService purchaseOrderLineService;
@Inject protected AppBaseService appBaseService;
@Transactional(rollbackOn = {Exception.class})
public void accept(PurchaseOrderSupplierLine purchaseOrderSupplierLine) throws AxelorException {
@@ -95,4 +107,70 @@ public class PurchaseOrderSupplierLineService {
public void savePurchaseSupplierLine(PurchaseOrderSupplierLine purchaseOrderSupplierLine){
poSupplierLineRepo.save(purchaseOrderSupplierLine);
}
@Transactional
public void calculateAmountsTco(PurchaseOrderSupplierLine purchaseOrderSupplierLine) throws AxelorException{
BigDecimal ExTaxTotal = BigDecimal.ZERO;
BigDecimal taxTotal = BigDecimal.ZERO;
BigDecimal inTaxTotal = BigDecimal.ZERO;
ExTaxTotal = purchaseOrderSupplierLine.getPrice().multiply(purchaseOrderSupplierLine.getAvailableQty())
.setScale(AppBaseService.DEFAULT_NB_DECIMAL_DIGITS, RoundingMode.HALF_EVEN);
purchaseOrderSupplierLine.setExTaxTotal(ExTaxTotal);
if(purchaseOrderSupplierLine.getTaxLine().getValue() != null){
taxTotal = ExTaxTotal.multiply(purchaseOrderSupplierLine.getTaxLine().getValue())
.setScale(AppBaseService.DEFAULT_NB_DECIMAL_DIGITS, RoundingMode.HALF_EVEN);
purchaseOrderSupplierLine.setTaxTotal(taxTotal);
}
inTaxTotal = ExTaxTotal.add(taxTotal).setScale(AppBaseService.DEFAULT_NB_DECIMAL_DIGITS, RoundingMode.HALF_EVEN);;
purchaseOrderSupplierLine.setInTaxTotal(inTaxTotal);
poSupplierLineRepo.save(purchaseOrderSupplierLine);
}
@Transactional
public void splitPurchaseOrderLinesSpecialInto2(
PurchaseOrder purchaseOrder, PurchaseOrderLine purchaseOrderLine, BigDecimal splitQty) throws AxelorException{
BigDecimal totalQty = purchaseOrderLine.getQty();
totalQty = totalQty.subtract(splitQty);
if(totalQty.compareTo(BigDecimal.ZERO) > 0){
PurchaseOrderLine newLine = purchaseOrderLineRepo.copy(purchaseOrderLine, true);
newLine.setQty(splitQty);
purchaseOrderLineService.compute(newLine, purchaseOrder);
List<PurchaseOrderSupplierLine> purchaseOrderSupplierLineList;
purchaseOrderSupplierLineList = newLine.getPurchaseOrderSupplierLineList();
if (purchaseOrderSupplierLineList != null) {
for (PurchaseOrderSupplierLine purchaseOrderSupplierLine : purchaseOrderSupplierLineList) {
purchaseOrderSupplierLine.setAvailableQty(splitQty);
this.calculateAmountsTco(purchaseOrderSupplierLine);
}
}
purchaseOrder.addPurchaseOrderLineListItem(newLine);
PurchaseOrderLine newLine2 = purchaseOrderLineRepo.copy(purchaseOrderLine, true);
newLine2.setQty(totalQty);
purchaseOrderLineService.compute(newLine2, purchaseOrder);
List<PurchaseOrderSupplierLine> purchaseOrderSupplierLineList2;
purchaseOrderSupplierLineList2 = newLine2.getPurchaseOrderSupplierLineList();
if (purchaseOrderSupplierLineList2 != null) {
for (PurchaseOrderSupplierLine purchaseOrderSupplierLine2 : purchaseOrderSupplierLineList2) {
purchaseOrderSupplierLine2.setAvailableQty(totalQty);
this.calculateAmountsTco(purchaseOrderSupplierLine2);
}
}
purchaseOrder.addPurchaseOrderLineListItem(newLine2);
purchaseOrder.removePurchaseOrderLineListItem(purchaseOrderLine);
}
}
}

View File

@@ -36,6 +36,8 @@ import com.google.common.base.Strings;
import com.google.inject.Singleton;
import java.util.stream.Collectors;
import com.axelor.meta.schema.actions.ActionView;
import java.math.BigDecimal;
import com.axelor.exception.AxelorException;
@Singleton
@@ -151,4 +153,22 @@ public class PurchaseOrderSupplierLineController {
response.setAttr("supplierPartner", "domain", domain);
}
public void splitPurchaseOrderLinesInto2(ActionRequest request, ActionResponse response){
try {
Long Id = (Long) request.getContext().get("id");
PurchaseOrderLineRepository purchaseOrderLineRepository = Beans.get(PurchaseOrderLineRepository.class);
PurchaseOrderLine purchaseOrderLine = purchaseOrderLineRepository.find(Id);
PurchaseOrder purchaseOrder = purchaseOrderLine.getPurchaseOrder();
BigDecimal splitQty = new BigDecimal(request.getContext().get("splitQty").toString());
Beans.get(PurchaseOrderSupplierLineService.class)
.splitPurchaseOrderLinesSpecialInto2(purchaseOrder, purchaseOrderLine, splitQty);
} catch (Exception e) {
TraceBackService.trace(response, e);
}
}
}