Search in sources :

Example 36 with Currency

use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.

the class PurchaseOrderLineServiceImpl method getUnitPrice.

/**
 * A function used to get the unit price of a purchase order line, either in ati or wt
 *
 * @param purchaseOrder the purchase order containing the purchase order line
 * @param purchaseOrderLine
 * @param taxLine the tax applied to the unit price
 * @param resultInAti whether or not you want the result to be in ati
 * @return the unit price of the purchase order line or null if the product is not available for
 *     purchase at the supplier of the purchase order
 * @throws AxelorException
 */
private BigDecimal getUnitPrice(PurchaseOrder purchaseOrder, PurchaseOrderLine purchaseOrderLine, TaxLine taxLine, boolean resultInAti) throws AxelorException {
    BigDecimal purchasePrice = new BigDecimal(0);
    Currency purchaseCurrency = null;
    Product product = purchaseOrderLine.getProduct();
    SupplierCatalog supplierCatalog = supplierCatalogService.getSupplierCatalog(product, purchaseOrder.getSupplierPartner(), purchaseOrder.getCompany());
    if (supplierCatalog != null) {
        purchasePrice = supplierCatalog.getPrice();
        purchaseCurrency = supplierCatalog.getSupplierPartner().getCurrency();
    } else {
        if (product != null) {
            purchasePrice = (BigDecimal) productCompanyService.get(product, "purchasePrice", purchaseOrder.getCompany());
            purchaseCurrency = (Currency) productCompanyService.get(product, "purchaseCurrency", purchaseOrder.getCompany());
        }
    }
    Boolean inAti = (Boolean) productCompanyService.get(product, "inAti", purchaseOrder.getCompany());
    BigDecimal price = (inAti == resultInAti) ? purchasePrice : this.convertUnitPrice(inAti, taxLine, purchasePrice);
    return currencyService.getAmountCurrencyConvertedAtDate(purchaseCurrency, purchaseOrder.getCurrency(), price, purchaseOrder.getOrderDate()).setScale(appBaseService.getNbDecimalDigitForUnitPrice(), RoundingMode.HALF_UP);
}
Also used : Currency(com.axelor.apps.base.db.Currency) Product(com.axelor.apps.base.db.Product) SupplierCatalog(com.axelor.apps.purchase.db.SupplierCatalog) BigDecimal(java.math.BigDecimal)

Example 37 with Currency

use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.

the class BatchBankPaymentServiceImpl method createBankOrderFromMonthlyPaymentScheduleLines.

@Override
@Transactional(rollbackOn = { Exception.class })
public BankOrder createBankOrderFromMonthlyPaymentScheduleLines(Batch batch) throws AxelorException, JAXBException, IOException, DatatypeConfigurationException {
    AccountingBatch accountingBatch = batch.getAccountingBatch();
    LocalDate bankOrderDate = accountingBatch.getDueDate();
    Company senderCompany = accountingBatch.getCompany();
    BankDetails senderBankDetails = accountingBatch.getBankDetails();
    if (senderBankDetails == null) {
        senderBankDetails = accountingBatch.getCompany().getDefaultBankDetails();
    }
    PaymentMode paymentMode = accountingBatch.getPaymentMode();
    Currency currency = senderCompany.getCurrency();
    int partnerType = BankOrderRepository.PARTNER_TYPE_CUSTOMER;
    String senderReference = "";
    String senderLabel = "";
    if (bankOrderDate == null) {
        bankOrderDate = appBaseService.getTodayDate(senderCompany);
    }
    BankOrder bankOrder = bankOrderCreateService.createBankOrder(paymentMode, partnerType, bankOrderDate, senderCompany, senderBankDetails, currency, senderReference, senderLabel, BankOrderRepository.TECHNICAL_ORIGIN_AUTOMATIC);
    bankOrder = JPA.save(bankOrder);
    List<PaymentScheduleLine> paymentScheduleLineList;
    int offset = 0;
    try {
        while (!(paymentScheduleLineList = fetchPaymentScheduleLineDoneList(batch, offset)).isEmpty()) {
            bankOrder = bankOrderRepo.find(bankOrder.getId());
            for (PaymentScheduleLine paymentScheduleLine : paymentScheduleLineList) {
                PaymentSchedule paymentSchedule = paymentScheduleLine.getPaymentSchedule();
                Partner partner = paymentSchedule.getPartner();
                BankDetails bankDetails = paymentScheduleService.getBankDetails(paymentSchedule);
                BigDecimal amount = paymentScheduleLine.getInTaxAmount();
                String receiverReference = paymentScheduleLine.getName();
                String receiverLabel = paymentScheduleLine.getDebitNumber();
                BankOrderLine bankOrderLine = bankOrderLineService.createBankOrderLine(paymentMode.getBankOrderFileFormat(), null, partner, bankDetails, amount, currency, bankOrderDate, receiverReference, receiverLabel, paymentScheduleLine);
                bankOrder.addBankOrderLineListItem(bankOrderLine);
            }
            bankOrder = JPA.save(bankOrder);
            offset += paymentScheduleLineList.size();
            JPA.clear();
        }
    } catch (Exception e) {
        bankOrder = bankOrderRepo.find(bankOrder.getId());
        bankOrderRepo.remove(bankOrder);
        throw e;
    }
    bankOrder = bankOrderRepo.find(bankOrder.getId());
    bankOrder = bankOrderRepo.save(bankOrder);
    bankOrderService.confirm(bankOrder);
    batch = batchRepo.find(batch.getId());
    batch.setBankOrder(bankOrder);
    return bankOrder;
}
Also used : Company(com.axelor.apps.base.db.Company) BankOrderLine(com.axelor.apps.bankpayment.db.BankOrderLine) PaymentScheduleLine(com.axelor.apps.account.db.PaymentScheduleLine) PaymentSchedule(com.axelor.apps.account.db.PaymentSchedule) BankDetails(com.axelor.apps.base.db.BankDetails) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) Currency(com.axelor.apps.base.db.Currency) AccountingBatch(com.axelor.apps.account.db.AccountingBatch) BankOrder(com.axelor.apps.bankpayment.db.BankOrder) Partner(com.axelor.apps.base.db.Partner) PaymentMode(com.axelor.apps.account.db.PaymentMode) Transactional(com.google.inject.persist.Transactional)

Example 38 with Currency

use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.

the class InvoiceController method mergeInvoice.

// Generate single invoice from several
@SuppressWarnings({ "rawtypes", "unchecked" })
public void mergeInvoice(ActionRequest request, ActionResponse response) {
    List<Invoice> invoiceList = new ArrayList<Invoice>();
    List<Long> invoiceIdList = new ArrayList<Long>();
    boolean fromPopup = false;
    if (request.getContext().get("invoiceToMerge") != null) {
        if (request.getContext().get("invoiceToMerge") instanceof List) {
            // No confirmation popup, invoices are content in a parameter list
            List<Map> invoiceMap = (List<Map>) request.getContext().get("invoiceToMerge");
            for (Map map : invoiceMap) {
                invoiceIdList.add(new Long((Integer) map.get("id")));
            }
        } else {
            // After confirmation popup, invoice's id are in a string separated by ","
            String invoiceIdListStr = (String) request.getContext().get("invoiceToMerge");
            for (String invoiceId : invoiceIdListStr.split(",")) {
                invoiceIdList.add(new Long(invoiceId));
            }
            fromPopup = true;
        }
    }
    // Check if company, currency and partner are the same for all selected invoices
    Company commonCompany = null;
    Currency commonCurrency = null;
    Partner commonPartner = null;
    PaymentCondition commonPaymentCondition = null;
    // Useful to determine if a difference exists between payment conditions of all invoices
    boolean existPaymentConditionDiff = false;
    Partner commonContactPartner = null;
    // Useful to determine if a difference exists between contact partners of all purchase orders
    boolean existContactPartnerDiff = false;
    PriceList commonPriceList = null;
    // Useful to determine if a difference exists between price lists of all purchase orders
    boolean existPriceListDiff = false;
    PaymentMode commonPaymentMode = null;
    // Useful to determine if a difference exists between locations of all purchase orders
    boolean existPaymentModeDiff = false;
    SaleOrder commonSaleOrder = null;
    // Useful to check if all sale orders are null (since this field is not required)
    boolean saleOrderIsNull = false;
    Project commonProject = null;
    // Useful to check if all projects are null (since this field is not required)
    boolean projectIsNull = false;
    Invoice invoiceTemp;
    int count = 1;
    for (Long invoiceId : invoiceIdList) {
        invoiceTemp = JPA.em().find(Invoice.class, invoiceId);
        invoiceList.add(invoiceTemp);
        if (count == 1) {
            commonCompany = invoiceTemp.getCompany();
            commonCurrency = invoiceTemp.getCurrency();
            commonPartner = invoiceTemp.getPartner();
            commonPaymentCondition = invoiceTemp.getPaymentCondition();
            commonContactPartner = invoiceTemp.getContactPartner();
            commonPriceList = invoiceTemp.getPriceList();
            commonPaymentMode = invoiceTemp.getPaymentMode();
            commonSaleOrder = invoiceTemp.getSaleOrder();
            commonProject = invoiceTemp.getProject();
            if (commonSaleOrder == null) {
                saleOrderIsNull = true;
            }
            if (commonProject == null) {
                projectIsNull = true;
            }
        } else {
            if (commonCompany != null && !commonCompany.equals(invoiceTemp.getCompany())) {
                commonCompany = null;
            }
            if (commonCurrency != null && !commonCurrency.equals(invoiceTemp.getCurrency())) {
                commonCurrency = null;
            }
            if (commonPartner != null && !commonPartner.equals(invoiceTemp.getPartner())) {
                commonPartner = null;
            }
            if (commonPaymentCondition != null && !commonPaymentCondition.equals(invoiceTemp.getPaymentCondition())) {
                commonPaymentCondition = null;
                existPaymentConditionDiff = true;
            }
            if (commonContactPartner != null && !commonContactPartner.equals(invoiceTemp.getContactPartner())) {
                commonContactPartner = null;
                existContactPartnerDiff = true;
            }
            if (commonPriceList != null && !commonPriceList.equals(invoiceTemp.getPriceList())) {
                commonPriceList = null;
                existPriceListDiff = true;
            }
            if (commonPaymentMode != null && !commonPaymentMode.equals(invoiceTemp.getPaymentMode())) {
                commonPaymentMode = null;
                existPaymentModeDiff = true;
            }
            if (commonSaleOrder != null && !commonSaleOrder.equals(invoiceTemp.getSaleOrder())) {
                commonSaleOrder = null;
            }
            if (commonProject != null && !commonProject.equals(invoiceTemp.getProject())) {
                commonProject = null;
            }
            if (invoiceTemp.getSaleOrder() != null) {
                saleOrderIsNull = false;
            }
            if (invoiceTemp.getProject() != null) {
                projectIsNull = false;
            }
        }
        count++;
    }
    StringBuilder fieldErrors = new StringBuilder();
    if (commonCurrency == null) {
        fieldErrors.append(I18n.get(IExceptionMessage.INVOICE_MERGE_ERROR_CURRENCY));
    }
    if (commonCompany == null) {
        if (fieldErrors.length() > 0) {
            fieldErrors.append("<br/>");
        }
        fieldErrors.append(I18n.get(IExceptionMessage.INVOICE_MERGE_ERROR_COMPANY));
    }
    if (commonPartner == null) {
        if (fieldErrors.length() > 0) {
            fieldErrors.append("<br/>");
        }
        fieldErrors.append(I18n.get(IExceptionMessage.INVOICE_MERGE_ERROR_PARTNER));
    }
    if (commonSaleOrder == null && saleOrderIsNull == false) {
        if (fieldErrors.length() > 0) {
            fieldErrors.append("<br/>");
        }
        fieldErrors.append(I18n.get(IExceptionMessage.INVOICE_MERGE_ERROR_SALEORDER));
    }
    if (commonProject == null && projectIsNull == false) {
        if (fieldErrors.length() > 0) {
            fieldErrors.append("<br/>");
        }
        fieldErrors.append(I18n.get(IExceptionMessage.INVOICE_MERGE_ERROR_PROJECT));
    }
    if (fieldErrors.length() > 0) {
        response.setFlash(fieldErrors.toString());
        return;
    }
    // content in parameters
    if (request.getContext().get("contactPartner") != null) {
        commonContactPartner = JPA.em().find(Partner.class, new Long((Integer) ((Map) request.getContext().get("contactPartner")).get("id")));
    }
    if (request.getContext().get("priceList") != null) {
        commonPriceList = JPA.em().find(PriceList.class, new Long((Integer) ((Map) request.getContext().get("priceList")).get("id")));
    }
    if (request.getContext().get("paymentMode") != null) {
        commonPaymentMode = JPA.em().find(PaymentMode.class, new Long((Integer) ((Map) request.getContext().get("paymentMode")).get("id")));
    }
    if (request.getContext().get("paymentCondition") != null) {
        commonPaymentCondition = JPA.em().find(PaymentCondition.class, new Long((Integer) ((Map) request.getContext().get("paymentCondition")).get("id")));
    }
    if (!fromPopup && (existPaymentConditionDiff || existContactPartnerDiff || existPriceListDiff || existPaymentModeDiff)) {
        // Need to display intermediate screen to select some values
        ActionViewBuilder confirmView = ActionView.define("Confirm merge invoice").model(Wizard.class.getName()).add("form", "customer-invoices-merge-confirm-form").param("popup", "true").param("show-toolbar", "false").param("show-confirm", "false").param("popup-save", "false").param("forceEdit", "true");
        if (existContactPartnerDiff) {
            confirmView.context("contextContactPartnerToCheck", "true");
            confirmView.context("contextPartnerId", commonPartner.getId().toString());
        }
        if (existPriceListDiff) {
            confirmView.context("contextPriceListToCheck", "true");
        }
        if (existPaymentModeDiff) {
            confirmView.context("contextPaymentModeToCheck", "true");
        }
        if (existPaymentConditionDiff) {
            confirmView.context("contextPaymentConditionToCheck", "true");
        }
        confirmView.context("invoiceToMerge", Joiner.on(",").join(invoiceIdList));
        response.setView(confirmView.map());
        return;
    }
    try {
        Invoice invoice = Beans.get(SaleOrderInvoiceProjectServiceImpl.class).mergeInvoice(invoiceList, commonCompany, commonCurrency, commonPartner, commonContactPartner, commonPriceList, commonPaymentMode, commonPaymentCondition, commonSaleOrder, commonProject);
        if (invoice != null) {
            // Open the generated invoice in a new tab
            response.setView(ActionView.define("Invoice").model(Invoice.class.getName()).add("grid", "invoice-grid").add("form", "invoice-form").param("search-filters", "customer-invoices-filters").param("forceEdit", "true").context("_showRecord", String.valueOf(invoice.getId())).map());
            response.setCanClose(true);
        }
    } catch (Exception e) {
        response.setFlash(e.getLocalizedMessage());
    }
}
Also used : PaymentCondition(com.axelor.apps.account.db.PaymentCondition) Company(com.axelor.apps.base.db.Company) Invoice(com.axelor.apps.account.db.Invoice) ArrayList(java.util.ArrayList) SaleOrderInvoiceProjectServiceImpl(com.axelor.apps.businessproject.service.SaleOrderInvoiceProjectServiceImpl) Currency(com.axelor.apps.base.db.Currency) ArrayList(java.util.ArrayList) List(java.util.List) PriceList(com.axelor.apps.base.db.PriceList) Partner(com.axelor.apps.base.db.Partner) PriceList(com.axelor.apps.base.db.PriceList) SaleOrder(com.axelor.apps.sale.db.SaleOrder) ActionViewBuilder(com.axelor.meta.schema.actions.ActionView.ActionViewBuilder) AxelorException(com.axelor.exception.AxelorException) InvoiceServiceProject(com.axelor.apps.businessproject.service.InvoiceServiceProject) Project(com.axelor.apps.project.db.Project) Map(java.util.Map) Wizard(com.axelor.apps.base.db.Wizard) PaymentMode(com.axelor.apps.account.db.PaymentMode)

Example 39 with Currency

use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.

the class SopServiceImpl method setSalesForecast.

@Transactional
protected void setSalesForecast(SopLine sopLine, ProductCategory category, Company company) throws AxelorException {
    sopLine = sopLineRepo.find(sopLine.getId());
    LocalDate fromDate = sopLine.getPeriod().getFromDate();
    LocalDate toDate = sopLine.getPeriod().getToDate();
    Year year = sopLine.getSop().getYearbasedHistoric();
    if (year != null) {
        fromDate = fromDate.withYear(year.getFromDate().getYear());
        toDate = toDate.withYear(year.getToDate().getYear());
    }
    Currency actualCurrency = company.getCurrency();
    ArrayList<Integer> statusList = new ArrayList<Integer>();
    statusList.add(SaleOrderRepository.STATUS_ORDER_COMPLETED);
    statusList.add(SaleOrderRepository.STATUS_ORDER_CONFIRMED);
    BigDecimal exTaxSum = BigDecimal.ZERO;
    Query<SaleOrderLine> query = saleOrderLineRepo.all().filter("self.saleOrder.company = ?1 AND self.saleOrder.statusSelect in (?2) AND self.product.productCategory = ?3", company, statusList, category).order("id");
    int offset = 0;
    List<SaleOrderLine> saleOrderLineList;
    while (!(saleOrderLineList = query.fetch(FETCH_LIMIT, offset)).isEmpty()) {
        offset += FETCH_LIMIT;
        actualCurrency = currencyRepo.find(actualCurrency.getId());
        for (SaleOrderLine saleOrderLine : saleOrderLineList) {
            LocalDate usedDate = saleOrderLine.getDesiredDelivDate() != null ? saleOrderLine.getDesiredDelivDate() : saleOrderLine.getEstimatedDelivDate() != null ? saleOrderLine.getEstimatedDelivDate() : saleOrderLine.getSaleOrder().getDeliveryDate() != null ? saleOrderLine.getSaleOrder().getDeliveryDate() : saleOrderLine.getSaleOrder().getConfirmationDateTime().toLocalDate();
            if (usedDate.isAfter(fromDate) && usedDate.isBefore(toDate)) {
                if (saleOrderLine.getSaleOrder().getCurrency().equals(actualCurrency)) {
                    exTaxSum = exTaxSum.add(saleOrderLine.getExTaxTotal().multiply(sopLine.getSop().getGrowthCoef())).setScale(2, RoundingMode.HALF_UP);
                } else {
                    exTaxSum = exTaxSum.add(currencyService.getAmountCurrencyConvertedAtDate(saleOrderLine.getSaleOrder().getCurrency(), actualCurrency, saleOrderLine.getExTaxTotal(), today).multiply(sopLine.getSop().getGrowthCoef()).setScale(2, RoundingMode.HALF_UP));
                }
            }
        }
        JPA.clear();
    }
    sopLine = sopLineRepo.find(sopLine.getId());
    sopLine.setSopSalesForecast(exTaxSum);
    sopLineRepo.save(sopLine);
}
Also used : Year(com.axelor.apps.base.db.Year) Currency(com.axelor.apps.base.db.Currency) ArrayList(java.util.ArrayList) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 40 with Currency

use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.

the class InvoiceServiceImpl method getDefaultAdvancePaymentInvoice.

@Override
public Set<Invoice> getDefaultAdvancePaymentInvoice(Invoice invoice) throws AxelorException {
    Set<Invoice> advancePaymentInvoices;
    Company company = invoice.getCompany();
    Currency currency = invoice.getCurrency();
    Partner partner = invoice.getPartner();
    if (company == null || currency == null || partner == null) {
        return new HashSet<>();
    }
    String filter = writeGeneralFilterForAdvancePayment();
    filter += " AND self.partner = :_partner AND self.currency = :_currency";
    advancePaymentInvoices = new HashSet<>(invoiceRepo.all().filter(filter).bind("_status", InvoiceRepository.STATUS_VALIDATED).bind("_operationSubType", InvoiceRepository.OPERATION_SUB_TYPE_ADVANCE).bind("_partner", partner).bind("_currency", currency).fetch());
    filterAdvancePaymentInvoice(invoice, advancePaymentInvoices);
    return advancePaymentInvoices;
}
Also used : Company(com.axelor.apps.base.db.Company) RefundInvoice(com.axelor.apps.account.service.invoice.generator.invoice.RefundInvoice) Invoice(com.axelor.apps.account.db.Invoice) Currency(com.axelor.apps.base.db.Currency) Partner(com.axelor.apps.base.db.Partner) HashSet(java.util.HashSet)

Aggregations

Currency (com.axelor.apps.base.db.Currency)41 Company (com.axelor.apps.base.db.Company)19 BigDecimal (java.math.BigDecimal)19 AxelorException (com.axelor.exception.AxelorException)17 Partner (com.axelor.apps.base.db.Partner)14 ArrayList (java.util.ArrayList)12 List (java.util.List)10 Invoice (com.axelor.apps.account.db.Invoice)9 PaymentMode (com.axelor.apps.account.db.PaymentMode)9 PriceList (com.axelor.apps.base.db.PriceList)9 Map (java.util.Map)9 SaleOrder (com.axelor.apps.sale.db.SaleOrder)8 LocalDate (java.time.LocalDate)8 BankDetails (com.axelor.apps.base.db.BankDetails)7 Wizard (com.axelor.apps.base.db.Wizard)7 ActionViewBuilder (com.axelor.meta.schema.actions.ActionView.ActionViewBuilder)7 Product (com.axelor.apps.base.db.Product)6 Transactional (com.google.inject.persist.Transactional)6 PaymentCondition (com.axelor.apps.account.db.PaymentCondition)4 BankOrder (com.axelor.apps.bankpayment.db.BankOrder)4