Search in sources :

Example 16 with Currency

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

the class PayVoucherElementToPayService method updateAmountToPayCurrency.

@Transactional(rollbackOn = AxelorException.class)
public void updateAmountToPayCurrency(PayVoucherElementToPay elementToPay) throws AxelorException {
    Currency paymentVoucherCurrency = elementToPay.getPaymentVoucher().getCurrency();
    BigDecimal amountToPayCurrency = currencyService.getAmountCurrencyConvertedAtDate(elementToPay.getCurrency(), paymentVoucherCurrency, elementToPay.getAmountToPay(), elementToPay.getPaymentVoucher().getPaymentDate());
    elementToPay.setAmountToPayCurrency(amountToPayCurrency);
    elementToPay.setRemainingAmountAfterPayment(elementToPay.getRemainingAmount().subtract(elementToPay.getAmountToPay()));
    payVoucherElementToPayRepo.save(elementToPay);
}
Also used : Currency(com.axelor.apps.base.db.Currency) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 17 with Currency

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

the class PaymentVoucherLoadService method mustBeBalanced.

public boolean mustBeBalanced(MoveLine moveLineToPay, PaymentVoucher paymentVoucher, BigDecimal amountToPay) {
    Invoice invoice = moveLineToPay.getMove().getInvoice();
    Currency invoiceCurrency = invoice.getCurrency();
    Currency paymentVoucherCurrency = paymentVoucher.getCurrency();
    // Alors on solde la facture
    if (paymentVoucherCurrency.equals(invoiceCurrency) && invoice.getAmountPaid().add(amountToPay).compareTo(invoice.getInTaxTotal()) == 0) {
        // SOLDER
        return true;
    }
    return false;
}
Also used : Invoice(com.axelor.apps.account.db.Invoice) Currency(com.axelor.apps.base.db.Currency)

Example 18 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<>();
    List<Long> invoiceIdList = new ArrayList<>();
    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;
    Invoice invoiceTemp;
    int count = 1;
    for (Long invoiceId : invoiceIdList) {
        invoiceTemp = Beans.get(InvoiceRepository.class).find(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();
        } 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;
            }
        }
        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 (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(InvoiceService.class).mergeInvoiceProcess(invoiceList, commonCompany, commonCurrency, commonPartner, commonContactPartner, commonPriceList, commonPaymentMode, commonPaymentCondition);
        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) InvoiceService(com.axelor.apps.account.service.invoice.InvoiceService) ActionViewBuilder(com.axelor.meta.schema.actions.ActionView.ActionViewBuilder) AxelorException(com.axelor.exception.AxelorException) Currency(com.axelor.apps.base.db.Currency) List(java.util.List) PriceList(com.axelor.apps.base.db.PriceList) ArrayList(java.util.ArrayList) Map(java.util.Map) Partner(com.axelor.apps.base.db.Partner) PriceList(com.axelor.apps.base.db.PriceList) Wizard(com.axelor.apps.base.db.Wizard) PaymentMode(com.axelor.apps.account.db.PaymentMode)

Example 19 with Currency

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

the class InvoicePaymentToolServiceImpl method computeAmountPaid.

protected BigDecimal computeAmountPaid(Invoice invoice) throws AxelorException {
    BigDecimal amountPaid = BigDecimal.ZERO;
    if (invoice.getInvoicePaymentList() == null) {
        return amountPaid;
    }
    CurrencyService currencyService = Beans.get(CurrencyService.class);
    Currency invoiceCurrency = invoice.getCurrency();
    for (InvoicePayment invoicePayment : invoice.getInvoicePaymentList()) {
        if (invoicePayment.getStatusSelect() == InvoicePaymentRepository.STATUS_VALIDATED) {
            log.debug("Amount paid without move : {}", invoicePayment.getAmount());
            amountPaid = amountPaid.add(currencyService.getAmountCurrencyConvertedAtDate(invoicePayment.getCurrency(), invoiceCurrency, invoicePayment.getAmount(), invoicePayment.getPaymentDate()));
        }
    }
    boolean isMinus = moveToolService.isMinus(invoice);
    if (isMinus) {
        amountPaid = amountPaid.negate();
    }
    log.debug("Amount paid total : {}", amountPaid);
    return amountPaid;
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) Currency(com.axelor.apps.base.db.Currency) CurrencyService(com.axelor.apps.base.service.CurrencyService) BigDecimal(java.math.BigDecimal)

Example 20 with Currency

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

the class CurrencyConversionLineController method convert.

@SuppressWarnings("unchecked")
public void convert(ActionRequest request, ActionResponse response) throws MalformedURLException, JSONException, AxelorException {
    Context context = request.getContext();
    Currency fromCurrency = null;
    Currency toCurrency = null;
    CurrencyRepository currencyRepository = Beans.get(CurrencyRepository.class);
    if (context.get("startCurrency") instanceof Currency) {
        fromCurrency = (Currency) context.get("startCurrency");
        toCurrency = (Currency) context.get("endCurrency");
    } else {
        Map<String, Object> startCurrency = (Map<String, Object>) context.get("startCurrency");
        Map<String, Object> endCurrency = (Map<String, Object>) context.get("endCurrency");
        fromCurrency = currencyRepository.find(Long.parseLong(startCurrency.get("id").toString()));
        toCurrency = currencyRepository.find(Long.parseLong(endCurrency.get("id").toString()));
    }
    CurrencyConversionLine prevLine = null;
    if (fromCurrency != null && toCurrency != null) {
        if (context.get("id") != null)
            prevLine = Beans.get(CurrencyConversionLineRepository.class).all().filter("startCurrency.id = ?1 AND endCurrency.id = ?2 AND id != ?3", fromCurrency.getId(), toCurrency.getId(), context.get("id")).order("-fromDate").fetchOne();
        else
            prevLine = Beans.get(CurrencyConversionLineRepository.class).all().filter("startCurrency.id = ?1 AND endCurrency.id = ?2", fromCurrency.getId(), toCurrency.getId()).order("-fromDate").fetchOne();
        LOG.debug("Previous currency conversion line: {}", prevLine);
        fromCurrency = currencyRepository.find(fromCurrency.getId());
        toCurrency = currencyRepository.find(toCurrency.getId());
        try {
            CurrencyConversionService currencyConversionService = Beans.get(CurrencyConversionFactory.class).getCurrencyConversionService();
            BigDecimal rate = currencyConversionService.convert(fromCurrency, toCurrency);
            if (rate.compareTo(new BigDecimal(-1)) == 0)
                response.setFlash(I18n.get(IExceptionMessage.CURRENCY_6));
            else {
                response.setValue("variations", "0");
                if (context.get("_model").equals("com.axelor.apps.base.db.Wizard"))
                    response.setValue("newExchangeRate", rate);
                else
                    response.setValue("exchangeRate", rate);
                response.setValue("fromDate", Beans.get(AppBaseService.class).getTodayDateTime());
                if (prevLine != null)
                    response.setValue("variations", currencyConversionService.getVariations(rate, prevLine.getExchangeRate()));
            }
        } catch (AxelorException axelorException) {
            response.setFlash(axelorException.getMessage());
            response.setCanClose(true);
        }
    }
}
Also used : Context(com.axelor.rpc.Context) AxelorException(com.axelor.exception.AxelorException) Currency(com.axelor.apps.base.db.Currency) CurrencyRepository(com.axelor.apps.base.db.repo.CurrencyRepository) CurrencyConversionLine(com.axelor.apps.base.db.CurrencyConversionLine) CurrencyConversionFactory(com.axelor.apps.base.service.currency.CurrencyConversionFactory) CurrencyConversionService(com.axelor.apps.base.service.currency.CurrencyConversionService) Map(java.util.Map) BigDecimal(java.math.BigDecimal)

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