Search in sources :

Example 6 with BudgetDistribution

use of com.axelor.apps.account.db.BudgetDistribution in project axelor-open-suite by axelor.

the class BudgetSupplychainService method updateLines.

@Override
@Transactional
public List<BudgetLine> updateLines(Budget budget) {
    if (!Beans.get(AppSupplychainService.class).isApp("supplychain")) {
        return super.updateLines(budget);
    }
    if (budget.getBudgetLineList() != null && !budget.getBudgetLineList().isEmpty()) {
        for (BudgetLine budgetLine : budget.getBudgetLineList()) {
            budgetLine.setAmountCommitted(BigDecimal.ZERO);
            budgetLine.setAmountRealized(BigDecimal.ZERO);
        }
        List<BudgetDistribution> budgetDistributionList = null;
        budgetDistributionList = Beans.get(BudgetDistributionRepository.class).all().filter("self.budget.id = ?1 AND self.purchaseOrderLine.purchaseOrder.statusSelect in (?2,?3)", budget.getId(), PurchaseOrderRepository.STATUS_VALIDATED, PurchaseOrderRepository.STATUS_FINISHED).fetch();
        for (BudgetDistribution budgetDistribution : budgetDistributionList) {
            LocalDate orderDate = budgetDistribution.getPurchaseOrderLine().getPurchaseOrder().getOrderDate();
            if (orderDate != null) {
                for (BudgetLine budgetLine : budget.getBudgetLineList()) {
                    LocalDate fromDate = budgetLine.getFromDate();
                    LocalDate toDate = budgetLine.getToDate();
                    if ((fromDate != null && toDate != null) && (fromDate.isBefore(orderDate) || fromDate.isEqual(orderDate)) && (toDate.isAfter(orderDate) || toDate.isEqual(orderDate))) {
                        budgetLine.setAmountCommitted(budgetLine.getAmountCommitted().add(budgetDistribution.getAmount()));
                        budgetLineRepository.save(budgetLine);
                        break;
                    }
                }
            }
        }
        budgetDistributionList = Beans.get(BudgetDistributionRepository.class).all().filter("self.budget.id = ?1 AND (self.invoiceLine.invoice.statusSelect = ?2 OR self.invoiceLine.invoice.statusSelect = ?3)", budget.getId(), InvoiceRepository.STATUS_VALIDATED, InvoiceRepository.STATUS_VENTILATED).fetch();
        for (BudgetDistribution budgetDistribution : budgetDistributionList) {
            Optional<LocalDate> optionaldate = getDate(budgetDistribution);
            Invoice invoice = budgetDistribution.getInvoiceLine().getInvoice();
            optionaldate.ifPresent(date -> {
                for (BudgetLine budgetLine : budget.getBudgetLineList()) {
                    LocalDate fromDate = budgetLine.getFromDate();
                    LocalDate toDate = budgetLine.getToDate();
                    if (fromDate != null && toDate != null && (fromDate.isBefore(date) || fromDate.isEqual(date)) && (toDate.isAfter(date) || toDate.isEqual(date))) {
                        budgetLine.setAmountRealized(invoice.getOperationTypeSelect() == InvoiceRepository.OPERATION_TYPE_SUPPLIER_REFUND ? budgetLine.getAmountRealized().subtract(budgetDistribution.getAmount()) : budgetLine.getAmountRealized().add(budgetDistribution.getAmount()));
                        break;
                    }
                }
            });
        }
    }
    return budget.getBudgetLineList();
}
Also used : Invoice(com.axelor.apps.account.db.Invoice) BudgetLine(com.axelor.apps.account.db.BudgetLine) BudgetDistribution(com.axelor.apps.account.db.BudgetDistribution) LocalDate(java.time.LocalDate) Transactional(com.google.inject.persist.Transactional)

Example 7 with BudgetDistribution

use of com.axelor.apps.account.db.BudgetDistribution in project axelor-open-suite by axelor.

the class InvoiceLineSupplychainService method computeBudgetDistributionSumAmount.

public void computeBudgetDistributionSumAmount(InvoiceLine invoiceLine, Invoice invoice) {
    List<BudgetDistribution> budgetDistributionList = invoiceLine.getBudgetDistributionList();
    PurchaseOrderLine purchaseOrderLine = invoiceLine.getPurchaseOrderLine();
    BigDecimal budgetDistributionSumAmount = BigDecimal.ZERO;
    LocalDate computeDate = invoice.getInvoiceDate();
    if (purchaseOrderLine != null && purchaseOrderLine.getPurchaseOrder().getOrderDate() != null) {
        computeDate = purchaseOrderLine.getPurchaseOrder().getOrderDate();
    }
    if (budgetDistributionList != null && !budgetDistributionList.isEmpty()) {
        for (BudgetDistribution budgetDistribution : budgetDistributionList) {
            budgetDistributionSumAmount = budgetDistributionSumAmount.add(budgetDistribution.getAmount());
            Beans.get(BudgetSupplychainService.class).computeBudgetDistributionSumAmount(budgetDistribution, computeDate);
        }
    }
    invoiceLine.setBudgetDistributionSumAmount(budgetDistributionSumAmount);
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) BudgetDistribution(com.axelor.apps.account.db.BudgetDistribution) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 8 with BudgetDistribution

use of com.axelor.apps.account.db.BudgetDistribution in project axelor-open-suite by axelor.

the class PurchaseOrderLineServiceSupplychainImpl method computeBudgetDistributionSumAmount.

public void computeBudgetDistributionSumAmount(PurchaseOrderLine purchaseOrderLine, PurchaseOrder purchaseOrder) {
    List<BudgetDistribution> budgetDistributionList = purchaseOrderLine.getBudgetDistributionList();
    BigDecimal budgetDistributionSumAmount = BigDecimal.ZERO;
    LocalDate computeDate = purchaseOrder.getOrderDate();
    if (budgetDistributionList != null && !budgetDistributionList.isEmpty()) {
        for (BudgetDistribution budgetDistribution : budgetDistributionList) {
            budgetDistributionSumAmount = budgetDistributionSumAmount.add(budgetDistribution.getAmount());
            Beans.get(BudgetSupplychainService.class).computeBudgetDistributionSumAmount(budgetDistribution, computeDate);
        }
    }
    purchaseOrderLine.setBudgetDistributionSumAmount(budgetDistributionSumAmount);
}
Also used : BudgetDistribution(com.axelor.apps.account.db.BudgetDistribution) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 9 with BudgetDistribution

use of com.axelor.apps.account.db.BudgetDistribution in project axelor-open-suite by axelor.

the class PurchaseOrderServiceSupplychainImpl method generateBudgetDistribution.

@Transactional
@Override
public void generateBudgetDistribution(PurchaseOrder purchaseOrder) {
    if (purchaseOrder.getPurchaseOrderLineList() != null) {
        for (PurchaseOrderLine purchaseOrderLine : purchaseOrder.getPurchaseOrderLineList()) {
            if (purchaseOrderLine.getBudget() != null && (purchaseOrderLine.getBudgetDistributionList() == null || purchaseOrderLine.getBudgetDistributionList().isEmpty())) {
                BudgetDistribution budgetDistribution = new BudgetDistribution();
                budgetDistribution.setBudget(purchaseOrderLine.getBudget());
                budgetDistribution.setAmount(purchaseOrderLine.getCompanyExTaxTotal());
                purchaseOrderLine.addBudgetDistributionListItem(budgetDistribution);
            }
        }
    // purchaseOrderRepo.save(purchaseOrder);
    }
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) BudgetDistribution(com.axelor.apps.account.db.BudgetDistribution) Transactional(com.google.inject.persist.Transactional)

Example 10 with BudgetDistribution

use of com.axelor.apps.account.db.BudgetDistribution in project axelor-open-suite by axelor.

the class PurchaseOrderServiceSupplychainImpl method requestPurchaseOrder.

@Override
@Transactional(rollbackOn = { Exception.class })
public void requestPurchaseOrder(PurchaseOrder purchaseOrder) throws AxelorException {
    if (!Beans.get(AppSupplychainService.class).isApp("supplychain")) {
        super.requestPurchaseOrder(purchaseOrder);
        return;
    }
    // budget control
    if (appAccountService.isApp("budget") && appAccountService.getAppBudget().getCheckAvailableBudget()) {
        List<PurchaseOrderLine> purchaseOrderLines = purchaseOrder.getPurchaseOrderLineList();
        Map<Budget, BigDecimal> amountPerBudget = new HashMap<>();
        if (appAccountService.getAppBudget().getManageMultiBudget()) {
            for (PurchaseOrderLine pol : purchaseOrderLines) {
                if (pol.getBudgetDistributionList() != null) {
                    for (BudgetDistribution bd : pol.getBudgetDistributionList()) {
                        Budget budget = bd.getBudget();
                        if (!amountPerBudget.containsKey(budget)) {
                            amountPerBudget.put(budget, bd.getAmount());
                        } else {
                            BigDecimal oldAmount = amountPerBudget.get(budget);
                            amountPerBudget.put(budget, oldAmount.add(bd.getAmount()));
                        }
                        isBudgetExceeded(budget, amountPerBudget.get(budget));
                    }
                }
            }
        } else {
            for (PurchaseOrderLine pol : purchaseOrderLines) {
                // getting Budget associated to POL
                Budget budget = pol.getBudget();
                if (!amountPerBudget.containsKey(budget)) {
                    amountPerBudget.put(budget, pol.getExTaxTotal());
                } else {
                    BigDecimal oldAmount = amountPerBudget.get(budget);
                    amountPerBudget.put(budget, oldAmount.add(pol.getExTaxTotal()));
                }
                isBudgetExceeded(budget, amountPerBudget.get(budget));
            }
        }
    }
    super.requestPurchaseOrder(purchaseOrder);
    int intercoPurchaseCreatingStatus = Beans.get(AppSupplychainService.class).getAppSupplychain().getIntercoPurchaseCreatingStatusSelect();
    if (purchaseOrder.getInterco() && intercoPurchaseCreatingStatus == PurchaseOrderRepository.STATUS_REQUESTED) {
        Beans.get(IntercoService.class).generateIntercoSaleFromPurchase(purchaseOrder);
    }
    if (purchaseOrder.getCreatedByInterco()) {
        fillIntercompanySaleOrderCounterpart(purchaseOrder);
    }
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) HashMap(java.util.HashMap) Budget(com.axelor.apps.account.db.Budget) BudgetDistribution(com.axelor.apps.account.db.BudgetDistribution) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Aggregations

BudgetDistribution (com.axelor.apps.account.db.BudgetDistribution)11 Transactional (com.google.inject.persist.Transactional)7 LocalDate (java.time.LocalDate)6 PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)5 BigDecimal (java.math.BigDecimal)5 BudgetLine (com.axelor.apps.account.db.BudgetLine)4 BudgetDistributionRepository (com.axelor.apps.account.db.repo.BudgetDistributionRepository)4 Budget (com.axelor.apps.account.db.Budget)3 Invoice (com.axelor.apps.account.db.Invoice)3 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)3 BudgetLineRepository (com.axelor.apps.account.db.repo.BudgetLineRepository)2 BudgetRepository (com.axelor.apps.account.db.repo.BudgetRepository)2 InvoiceRepository (com.axelor.apps.account.db.repo.InvoiceRepository)2 Beans (com.axelor.inject.Beans)2 Inject (com.google.inject.Inject)2 List (java.util.List)2 Optional (java.util.Optional)2 IExceptionMessage (com.axelor.apps.account.exception.IExceptionMessage)1 BudgetService (com.axelor.apps.account.service.BudgetService)1 PurchaseOrder (com.axelor.apps.purchase.db.PurchaseOrder)1