Search in sources :

Example 11 with InvoiceLineGenerator

use of com.axelor.apps.account.service.invoice.generator.InvoiceLineGenerator in project axelor-open-suite by axelor.

the class PurchaseOrderInvoiceProjectServiceImpl method createInvoiceLine.

@Override
public List<InvoiceLine> createInvoiceLine(Invoice invoice, PurchaseOrderLine purchaseOrderLine) throws AxelorException {
    Product product = purchaseOrderLine.getProduct();
    Company company = purchaseOrderLine.getPurchaseOrder() != null ? purchaseOrderLine.getPurchaseOrder().getCompany() : null;
    BigDecimal price = (BigDecimal) productCompanyService.get(product, "costPrice", company);
    BigDecimal discountAmount = price;
    int discountTypeSelect = 1;
    if (invoice.getPartner().getChargeBackPurchaseSelect() == PartnerRepository.CHARGING_BACK_TYPE_PRICE_LIST) {
        PriceList priceList = Beans.get(PartnerPriceListService.class).getDefaultPriceList(invoice.getPartner(), PriceListRepository.TYPE_SALE);
        if (priceList != null) {
            PriceListLine priceListLine = purchaseOrderLineServiceImpl.getPriceListLine(purchaseOrderLine, priceList, price);
            if (priceListLine != null) {
                discountTypeSelect = priceListLine.getTypeSelect();
            }
            if ((appBusinessProjectService.getAppBase().getComputeMethodDiscountSelect() == AppBaseRepository.INCLUDE_DISCOUNT_REPLACE_ONLY && discountTypeSelect == PriceListLineRepository.TYPE_REPLACE) || appBusinessProjectService.getAppBase().getComputeMethodDiscountSelect() == AppBaseRepository.INCLUDE_DISCOUNT) {
                Map<String, Object> discounts = priceListService.getDiscounts(priceList, priceListLine, price);
                if (discounts != null) {
                    discountAmount = (BigDecimal) discounts.get("discountAmount");
                    price = priceListService.computeDiscount(price, (int) discounts.get("discountTypeSelect"), discountAmount);
                }
            } else {
                Map<String, Object> discounts = priceListService.getDiscounts(priceList, priceListLine, price);
                if (discounts != null) {
                    discountAmount = (BigDecimal) discounts.get("discountAmount");
                    if (discounts.get("price") != null) {
                        price = (BigDecimal) discounts.get("price");
                    }
                }
            }
        }
        InvoiceLineGenerator invoiceLineGenerator = new InvoiceLineGenerator(invoice, product, (String) productCompanyService.get(product, "name", company), price, price, price, purchaseOrderLine.getDescription(), purchaseOrderLine.getQty(), purchaseOrderLine.getUnit(), null, InvoiceLineGenerator.DEFAULT_SEQUENCE, discountAmount, discountTypeSelect, null, null, false) {

            @Override
            public List<InvoiceLine> creates() throws AxelorException {
                InvoiceLine invoiceLine = this.createInvoiceLine();
                invoiceLine.setProject(purchaseOrderLine.getProject());
                List<InvoiceLine> invoiceLines = new ArrayList<InvoiceLine>();
                invoiceLines.add(invoiceLine);
                return invoiceLines;
            }
        };
        return invoiceLineGenerator.creates();
    } else if (invoice.getPartner().getChargeBackPurchaseSelect() == PartnerRepository.CHARGING_BACK_TYPE_PERCENTAGE) {
        price = price.multiply(invoice.getPartner().getChargeBackPurchase().divide(new BigDecimal(100), appBusinessProjectService.getNbDecimalDigitForUnitPrice(), BigDecimal.ROUND_HALF_UP)).setScale(appBusinessProjectService.getNbDecimalDigitForUnitPrice(), BigDecimal.ROUND_HALF_UP);
        InvoiceLineGenerator invoiceLineGenerator = new InvoiceLineGenerator(invoice, product, (String) productCompanyService.get(product, "name", company), price, price, price, purchaseOrderLine.getDescription(), purchaseOrderLine.getQty(), purchaseOrderLine.getUnit(), null, InvoiceLineGenerator.DEFAULT_SEQUENCE, discountAmount, discountTypeSelect, null, null, false) {

            @Override
            public List<InvoiceLine> creates() throws AxelorException {
                InvoiceLine invoiceLine = this.createInvoiceLine();
                invoiceLine.setProject(purchaseOrderLine.getProject());
                List<InvoiceLine> invoiceLines = new ArrayList<InvoiceLine>();
                invoiceLines.add(invoiceLine);
                return invoiceLines;
            }
        };
        return invoiceLineGenerator.creates();
    } else {
        InvoiceLineGeneratorSupplyChain invoiceLineGenerator = new InvoiceLineGeneratorSupplyChain(invoice, product, purchaseOrderLine.getProductName(), purchaseOrderLine.getDescription(), purchaseOrderLine.getQty(), purchaseOrderLine.getUnit(), purchaseOrderLine.getSequence(), false, null, purchaseOrderLine, null) {

            @Override
            public List<InvoiceLine> creates() throws AxelorException {
                InvoiceLine invoiceLine = this.createInvoiceLine();
                invoiceLine.setProject(purchaseOrderLine.getProject());
                List<InvoiceLine> invoiceLines = new ArrayList<InvoiceLine>();
                invoiceLines.add(invoiceLine);
                return invoiceLines;
            }
        };
        return invoiceLineGenerator.creates();
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) PartnerPriceListService(com.axelor.apps.base.service.PartnerPriceListService) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) ArrayList(java.util.ArrayList) Product(com.axelor.apps.base.db.Product) InvoiceLineGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceLineGenerator) BigDecimal(java.math.BigDecimal) PriceListLine(com.axelor.apps.base.db.PriceListLine) InvoiceLineGeneratorSupplyChain(com.axelor.apps.supplychain.service.invoice.generator.InvoiceLineGeneratorSupplyChain) ArrayList(java.util.ArrayList) List(java.util.List) PriceList(com.axelor.apps.base.db.PriceList) PriceList(com.axelor.apps.base.db.PriceList)

Aggregations

InvoiceLine (com.axelor.apps.account.db.InvoiceLine)11 InvoiceLineGenerator (com.axelor.apps.account.service.invoice.generator.InvoiceLineGenerator)11 ArrayList (java.util.ArrayList)11 Product (com.axelor.apps.base.db.Product)6 InvoiceLineGeneratorSupplyChain (com.axelor.apps.supplychain.service.invoice.generator.InvoiceLineGeneratorSupplyChain)5 BigDecimal (java.math.BigDecimal)5 AxelorException (com.axelor.exception.AxelorException)4 TaxLine (com.axelor.apps.account.db.TaxLine)2 PriceListLine (com.axelor.apps.base.db.PriceListLine)2 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)2 List (java.util.List)2 Account (com.axelor.apps.account.db.Account)1 FiscalPosition (com.axelor.apps.account.db.FiscalPosition)1 InvoiceLineRepository (com.axelor.apps.account.db.repo.InvoiceLineRepository)1 FiscalPositionAccountService (com.axelor.apps.account.service.FiscalPositionAccountService)1 Company (com.axelor.apps.base.db.Company)1 PriceList (com.axelor.apps.base.db.PriceList)1 PartnerPriceListService (com.axelor.apps.base.service.PartnerPriceListService)1 UnitConversionService (com.axelor.apps.base.service.UnitConversionService)1 AccountManagementService (com.axelor.apps.base.service.tax.AccountManagementService)1