Search in sources :

Example 6 with InvoiceLine

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

the class FixedAssetServiceImpl method createFixedAssets.

@Override
@Transactional(rollbackOn = { Exception.class })
public List<FixedAsset> createFixedAssets(Invoice invoice) throws AxelorException {
    List<FixedAsset> fixedAssetList = new ArrayList<>();
    if (invoice == null || CollectionUtils.isEmpty(invoice.getInvoiceLineList())) {
        return fixedAssetList;
    }
    AccountConfig accountConfig = accountConfigService.getAccountConfig(invoice.getCompany());
    for (InvoiceLine invoiceLine : invoice.getInvoiceLineList()) {
        if (accountConfig.getFixedAssetCatReqOnInvoice() && invoiceLine.getFixedAssets() && invoiceLine.getFixedAssetCategory() == null) {
            throw new AxelorException(invoiceLine, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.INVOICE_LINE_ERROR_FIXED_ASSET_CATEGORY), invoiceLine.getProductName());
        }
        if (!invoiceLine.getFixedAssets() || invoiceLine.getFixedAssetCategory() == null) {
            continue;
        }
        FixedAsset fixedAsset = new FixedAsset();
        fixedAsset.setFixedAssetCategory(invoiceLine.getFixedAssetCategory());
        if (fixedAsset.getFixedAssetCategory().getIsValidateFixedAsset()) {
            fixedAsset.setStatusSelect(FixedAssetRepository.STATUS_VALIDATED);
        } else {
            fixedAsset.setStatusSelect(FixedAssetRepository.STATUS_DRAFT);
        }
        fixedAsset.setAcquisitionDate(invoice.getInvoiceDate());
        fixedAsset.setFirstDepreciationDate(invoice.getInvoiceDate());
        fixedAsset.setReference(invoice.getInvoiceId());
        fixedAsset.setName(invoiceLine.getProductName() + " (" + invoiceLine.getQty() + ")");
        fixedAsset.setCompany(fixedAsset.getFixedAssetCategory().getCompany());
        fixedAsset.setJournal(fixedAsset.getFixedAssetCategory().getJournal());
        fixedAsset.setComputationMethodSelect(fixedAsset.getFixedAssetCategory().getComputationMethodSelect());
        fixedAsset.setDegressiveCoef(fixedAsset.getFixedAssetCategory().getDegressiveCoef());
        fixedAsset.setNumberOfDepreciation(fixedAsset.getFixedAssetCategory().getNumberOfDepreciation());
        fixedAsset.setPeriodicityInMonth(fixedAsset.getFixedAssetCategory().getPeriodicityInMonth());
        fixedAsset.setDurationInMonth(fixedAsset.getFixedAssetCategory().getDurationInMonth());
        fixedAsset.setGrossValue(invoiceLine.getCompanyExTaxTotal());
        fixedAsset.setPartner(invoice.getPartner());
        fixedAsset.setPurchaseAccount(invoiceLine.getAccount());
        fixedAsset.setInvoiceLine(invoiceLine);
        this.generateAndComputeLines(fixedAsset);
        fixedAssetList.add(fixedAssetRepo.save(fixedAsset));
    }
    return fixedAssetList;
}
Also used : AxelorException(com.axelor.exception.AxelorException) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) ArrayList(java.util.ArrayList) FixedAsset(com.axelor.apps.account.db.FixedAsset) AccountConfig(com.axelor.apps.account.db.AccountConfig) Transactional(com.google.inject.persist.Transactional)

Example 7 with InvoiceLine

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

the class TimesheetServiceImpl method createInvoiceLines.

@Override
public List<InvoiceLine> createInvoiceLines(Invoice invoice, List<TimesheetLine> timesheetLineList, int priority) throws AxelorException {
    List<InvoiceLine> invoiceLineList = new ArrayList<>();
    int count = 0;
    DateFormat ddmmFormat = new SimpleDateFormat("dd/MM");
    HashMap<String, Object[]> timeSheetInformationsMap = new HashMap<>();
    // Check if a consolidation by product and user must be done
    boolean consolidate = appHumanResourceService.getAppTimesheet().getConsolidateTSLine();
    for (TimesheetLine timesheetLine : timesheetLineList) {
        Object[] tabInformations = new Object[5];
        tabInformations[0] = timesheetLine.getProduct();
        tabInformations[1] = timesheetLine.getUser();
        // Start date
        tabInformations[2] = timesheetLine.getDate();
        // End date, useful only for consolidation
        tabInformations[3] = timesheetLine.getDate();
        tabInformations[4] = timesheetLine.getHoursDuration();
        String key = null;
        if (consolidate) {
            key = timesheetLine.getProduct().getId() + "|" + timesheetLine.getUser().getId();
            if (timeSheetInformationsMap.containsKey(key)) {
                tabInformations = timeSheetInformationsMap.get(key);
                // Update date
                if (timesheetLine.getDate().compareTo((LocalDate) tabInformations[2]) < 0) {
                    // If date is lower than start date then replace start date by this one
                    tabInformations[2] = timesheetLine.getDate();
                } else if (timesheetLine.getDate().compareTo((LocalDate) tabInformations[3]) > 0) {
                    // If date is upper than end date then replace end date by this one
                    tabInformations[3] = timesheetLine.getDate();
                }
                tabInformations[4] = ((BigDecimal) tabInformations[4]).add(timesheetLine.getHoursDuration());
            } else {
                timeSheetInformationsMap.put(key, tabInformations);
            }
        } else {
            key = String.valueOf(timesheetLine.getId());
            timeSheetInformationsMap.put(key, tabInformations);
        }
        timesheetLine.setInvoiced(true);
    }
    for (Object[] timesheetInformations : timeSheetInformationsMap.values()) {
        String strDate = null;
        Product product = (Product) timesheetInformations[0];
        User user = (User) timesheetInformations[1];
        LocalDate startDate = (LocalDate) timesheetInformations[2];
        LocalDate endDate = (LocalDate) timesheetInformations[3];
        BigDecimal hoursDuration = (BigDecimal) timesheetInformations[4];
        PriceList priceList = Beans.get(PartnerPriceListService.class).getDefaultPriceList(invoice.getPartner(), PriceListRepository.TYPE_SALE);
        if (consolidate) {
            strDate = ddmmFormat.format(startDate) + " - " + ddmmFormat.format(endDate);
        } else {
            strDate = ddmmFormat.format(startDate);
        }
        invoiceLineList.addAll(this.createInvoiceLine(invoice, product, user, strDate, hoursDuration, priority * 100 + count, priceList));
        count++;
    }
    return invoiceLineList;
}
Also used : User(com.axelor.auth.db.User) TimesheetLine(com.axelor.apps.hr.db.TimesheetLine) PartnerPriceListService(com.axelor.apps.base.service.PartnerPriceListService) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(com.axelor.apps.base.db.Product) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) PriceList(com.axelor.apps.base.db.PriceList)

Example 8 with InvoiceLine

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

the class TimesheetServiceImpl method createInvoiceLine.

@Override
public List<InvoiceLine> createInvoiceLine(Invoice invoice, Product product, User user, String date, BigDecimal hoursDuration, int priority, PriceList priceList) throws AxelorException {
    int discountMethodTypeSelect = PriceListLineRepository.TYPE_DISCOUNT;
    int discountTypeSelect = PriceListLineRepository.AMOUNT_TYPE_NONE;
    if (product == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.TIMESHEET_PRODUCT));
    }
    BigDecimal price = (BigDecimal) productCompanyService.get(product, "salePrice", invoice.getCompany());
    BigDecimal discountAmount = BigDecimal.ZERO;
    BigDecimal priceDiscounted = price;
    BigDecimal qtyConverted = Beans.get(UnitConversionService.class).convert(appHumanResourceService.getAppBase().getUnitHours(), (Unit) productCompanyService.get(product, "unit", invoice.getCompany()), hoursDuration, AppBaseService.DEFAULT_NB_DECIMAL_DIGITS, product);
    if (priceList != null) {
        PriceListLine priceListLine = priceListService.getPriceListLine(product, qtyConverted, priceList, price);
        if (priceListLine != null) {
            discountMethodTypeSelect = priceListLine.getTypeSelect();
        }
        Map<String, Object> discounts = priceListService.getDiscounts(priceList, priceListLine, price);
        if (discounts != null) {
            discountAmount = (BigDecimal) discounts.get("discountAmount");
            discountTypeSelect = (int) discounts.get("discountTypeSelect");
            priceDiscounted = priceListService.computeDiscount(price, discountTypeSelect, discountAmount);
        }
        if ((appHumanResourceService.getAppBase().getComputeMethodDiscountSelect() == AppBaseRepository.INCLUDE_DISCOUNT_REPLACE_ONLY && discountMethodTypeSelect == PriceListLineRepository.TYPE_REPLACE) || appHumanResourceService.getAppBase().getComputeMethodDiscountSelect() == AppBaseRepository.INCLUDE_DISCOUNT) {
            discountTypeSelect = PriceListLineRepository.AMOUNT_TYPE_NONE;
            price = priceDiscounted;
        }
    }
    String description = user.getFullName();
    String productName = (String) productCompanyService.get(product, "name", invoice.getCompany());
    if (date != null) {
        productName += " " + "(" + date + ")";
    }
    InvoiceLineGenerator invoiceLineGenerator = new InvoiceLineGenerator(invoice, product, productName, price, price, priceDiscounted, description, qtyConverted, (Unit) productCompanyService.get(product, "unit", invoice.getCompany()), null, priority, discountAmount, discountTypeSelect, price.multiply(qtyConverted), null, false) {

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

Example 9 with InvoiceLine

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

the class InvoiceLineController method updatePrice.

/**
 * Update the ex. tax unit price of an invoice line from its in. tax unit price.
 *
 * @param request
 * @param response
 */
public void updatePrice(ActionRequest request, ActionResponse response) {
    Context context = request.getContext();
    InvoiceLine invoiceLine = context.asType(InvoiceLine.class);
    try {
        BigDecimal inTaxPrice = invoiceLine.getInTaxPrice();
        TaxLine taxLine = invoiceLine.getTaxLine();
        response.setValue("price", Beans.get(InvoiceLineService.class).convertUnitPrice(true, taxLine, inTaxPrice));
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Context(com.axelor.rpc.Context) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) TaxLine(com.axelor.apps.account.db.TaxLine)

Example 10 with InvoiceLine

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

the class InvoiceLineController method compute.

public void compute(ActionRequest request, ActionResponse response) throws AxelorException {
    Context context = request.getContext();
    InvoiceLine invoiceLine = context.asType(InvoiceLine.class);
    Invoice invoice = this.getInvoice(context);
    if (invoice == null || invoiceLine.getPrice() == null || invoiceLine.getInTaxPrice() == null || invoiceLine.getQty() == null) {
        return;
    }
    InvoiceLineService invoiceLineService = Beans.get(InvoiceLineService.class);
    BigDecimal exTaxTotal;
    BigDecimal companyExTaxTotal;
    BigDecimal inTaxTotal;
    BigDecimal companyInTaxTotal;
    BigDecimal priceDiscounted = invoiceLineService.computeDiscount(invoiceLine, invoice.getInAti());
    response.setValue("priceDiscounted", priceDiscounted);
    response.setAttr("priceDiscounted", "hidden", priceDiscounted.compareTo(invoice.getInAti() ? invoiceLine.getInTaxPrice() : invoiceLine.getPrice()) == 0);
    BigDecimal taxRate = BigDecimal.ZERO;
    if (invoiceLine.getTaxLine() != null) {
        taxRate = invoiceLine.getTaxLine().getValue();
        response.setValue("taxRate", taxRate);
        response.setValue("taxCode", invoiceLine.getTaxLine().getTax().getCode());
    }
    if (!invoice.getInAti()) {
        exTaxTotal = InvoiceLineManagement.computeAmount(invoiceLine.getQty(), priceDiscounted);
        inTaxTotal = exTaxTotal.add(exTaxTotal.multiply(taxRate));
    } else {
        inTaxTotal = InvoiceLineManagement.computeAmount(invoiceLine.getQty(), priceDiscounted);
        exTaxTotal = inTaxTotal.divide(taxRate.add(BigDecimal.ONE), 2, BigDecimal.ROUND_HALF_UP);
    }
    companyExTaxTotal = invoiceLineService.getCompanyExTaxTotal(exTaxTotal, invoice);
    companyInTaxTotal = invoiceLineService.getCompanyExTaxTotal(inTaxTotal, invoice);
    response.setValue("exTaxTotal", exTaxTotal);
    response.setValue("inTaxTotal", inTaxTotal);
    response.setValue("companyInTaxTotal", companyInTaxTotal);
    response.setValue("companyExTaxTotal", companyExTaxTotal);
}
Also used : Context(com.axelor.rpc.Context) Invoice(com.axelor.apps.account.db.Invoice) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) InvoiceLineService(com.axelor.apps.account.service.invoice.InvoiceLineService) BigDecimal(java.math.BigDecimal)

Aggregations

InvoiceLine (com.axelor.apps.account.db.InvoiceLine)80 ArrayList (java.util.ArrayList)36 Invoice (com.axelor.apps.account.db.Invoice)27 BigDecimal (java.math.BigDecimal)22 AxelorException (com.axelor.exception.AxelorException)20 Product (com.axelor.apps.base.db.Product)13 InvoiceLineGenerator (com.axelor.apps.account.service.invoice.generator.InvoiceLineGenerator)12 Transactional (com.google.inject.persist.Transactional)12 Context (com.axelor.rpc.Context)11 Account (com.axelor.apps.account.db.Account)8 InvoiceGenerator (com.axelor.apps.account.service.invoice.generator.InvoiceGenerator)8 TaxLine (com.axelor.apps.account.db.TaxLine)7 InvoiceLineService (com.axelor.apps.account.service.invoice.InvoiceLineService)7 RefundInvoice (com.axelor.apps.account.service.invoice.generator.invoice.RefundInvoice)6 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)6 InvoiceLineGeneratorSupplyChain (com.axelor.apps.supplychain.service.invoice.generator.InvoiceLineGeneratorSupplyChain)6 AnalyticMoveLine (com.axelor.apps.account.db.AnalyticMoveLine)5 InvoiceRepository (com.axelor.apps.account.db.repo.InvoiceRepository)5 HashSet (java.util.HashSet)5 List (java.util.List)5