use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.
the class InvoicePaymentCreateServiceImpl method createInvoicePayment.
/**
* @param invoice
* @param amount
* @param paymentDate
* @param paymentMove
* @return
*/
public InvoicePayment createInvoicePayment(Invoice invoice, BigDecimal amount, Move paymentMove) throws AxelorException {
LocalDate paymentDate = paymentMove.getDate();
BigDecimal amountConverted = currencyService.getAmountCurrencyConvertedAtDate(paymentMove.getCompanyCurrency(), paymentMove.getCurrency(), amount, paymentDate);
int typePaymentMove = this.determineType(paymentMove);
Currency currency = paymentMove.getCurrency();
if (currency == null) {
currency = paymentMove.getCompanyCurrency();
}
PaymentMode paymentMode;
InvoicePayment invoicePayment;
if (typePaymentMove == InvoicePaymentRepository.TYPE_REFUND_INVOICE || typePaymentMove == InvoicePaymentRepository.TYPE_INVOICE) {
paymentMode = null;
} else {
paymentMode = paymentMove.getPaymentMode();
}
invoicePayment = this.createInvoicePayment(invoice, amountConverted, paymentDate, currency, paymentMode, typePaymentMove);
invoicePayment.setMove(paymentMove);
invoicePayment.setStatusSelect(InvoicePaymentRepository.STATUS_VALIDATED);
PaymentVoucher paymentVoucher = paymentMove.getPaymentVoucher();
if (paymentVoucher != null) {
invoicePayment.setCompanyBankDetails(paymentVoucher.getCompanyBankDetails());
} else if (invoice.getSchedulePaymentOk() && invoice.getPaymentSchedule() != null) {
BankDetails companyBankDetails = invoice.getPaymentSchedule().getCompanyBankDetails();
invoicePayment.setCompanyBankDetails(companyBankDetails);
}
computeAdvancePaymentImputation(invoicePayment, paymentMove);
invoice.addInvoicePaymentListItem(invoicePayment);
invoicePaymentToolService.updateAmountPaid(invoice);
invoicePaymentRepository.save(invoicePayment);
return invoicePayment;
}
use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.
the class InvoicePaymentCreateServiceImpl method getInvoiceIdsToPay.
@Override
public List<Long> getInvoiceIdsToPay(List<Long> invoiceIdList) throws AxelorException {
Company company = null;
Currency currency = null;
List<Long> invoiceToPay = new ArrayList<>();
Boolean isActivatePassedForPayment = Beans.get(AppAccountService.class).getAppAccount().getActivatePassedForPayment();
for (Long invoiceId : invoiceIdList) {
Invoice invoice = Beans.get(InvoiceRepository.class).find(invoiceId);
if ((invoice.getStatusSelect() != InvoiceRepository.STATUS_VENTILATED && invoice.getOperationSubTypeSelect() != InvoiceRepository.OPERATION_SUB_TYPE_ADVANCE) || (invoice.getOperationSubTypeSelect() == InvoiceRepository.OPERATION_SUB_TYPE_ADVANCE && invoice.getStatusSelect() != InvoiceRepository.STATUS_VALIDATED)) {
continue;
}
if (invoice.getAmountRemaining().compareTo(BigDecimal.ZERO) <= 0) {
continue;
}
if (company == null) {
company = invoice.getCompany();
}
if (currency == null) {
currency = invoice.getCurrency();
}
if (invoice.getCompany() == null || company == null || !invoice.getCompany().equals(company)) {
throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.INVOICE_MERGE_ERROR_COMPANY));
}
if (invoice.getCurrency() == null || currency == null || !invoice.getCurrency().equals(currency)) {
throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.INVOICE_MERGE_ERROR_CURRENCY));
}
if (isActivatePassedForPayment && invoice.getPfpValidateStatusSelect() != InvoiceRepository.PFP_STATUS_VALIDATED) {
throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.INVOICE_MASS_PAYMENT_ERROR_PFP_LITIGATION));
}
invoiceToPay.add(invoiceId);
}
return invoiceToPay;
}
use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.
the class SaleOrderLineServiceImpl method getUnitPriceFromPackLine.
/**
* A method used to get the unit price of a sale order line from pack line, either in ati or wt
*
* @param saleOrder the sale order containing the sale order line
* @param saleOrderLine
* @param taxLine the tax applied to the unit price
* @param resultInAti whether you want the result in ati or not
* @return the unit price of the sale order line
* @throws AxelorException
*/
protected BigDecimal getUnitPriceFromPackLine(SaleOrder saleOrder, SaleOrderLine saleOrderLine, TaxLine taxLine, boolean resultInAti) throws AxelorException {
Product product = saleOrderLine.getProduct();
Boolean productInAti = (Boolean) productCompanyService.get(product, "inAti", saleOrder.getCompany());
BigDecimal productSalePrice = saleOrderLine.getPrice();
BigDecimal price = (productInAti == resultInAti) ? productSalePrice : this.convertUnitPrice(productInAti, taxLine, productSalePrice);
return currencyService.getAmountCurrencyConvertedAtDate((Currency) productCompanyService.get(product, "saleCurrency", saleOrder.getCompany()), saleOrder.getCurrency(), price, saleOrder.getCreationDate()).setScale(appSaleService.getNbDecimalDigitForUnitPrice(), RoundingMode.HALF_UP);
}
use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.
the class PurchaseOrderController method mergePurchaseOrder.
// Generate single purchase order from several
@SuppressWarnings({ "rawtypes", "unchecked" })
public void mergePurchaseOrder(ActionRequest request, ActionResponse response) {
List<PurchaseOrder> purchaseOrderList = new ArrayList<PurchaseOrder>();
List<Long> purchaseOrderIdList = new ArrayList<Long>();
boolean fromPopup = false;
if (request.getContext().get("purchaseOrderToMerge") != null) {
if (request.getContext().get("purchaseOrderToMerge") instanceof List) {
// No confirmation popup, purchase orders are content in a parameter list
List<Map> purchaseOrderMap = (List<Map>) request.getContext().get("purchaseOrderToMerge");
for (Map map : purchaseOrderMap) {
purchaseOrderIdList.add(new Long((Integer) map.get("id")));
}
} else {
// After confirmation popup, purchase order's id are in a string separated by ","
String purchaseOrderIdListStr = (String) request.getContext().get("purchaseOrderToMerge");
for (String purchaseOrderId : purchaseOrderIdListStr.split(",")) {
purchaseOrderIdList.add(new Long(purchaseOrderId));
}
fromPopup = true;
}
}
// Check if currency, supplierPartner, company and tradingName are the same for all selected
// purchase orders
Currency commonCurrency = null;
Partner commonSupplierPartner = null;
Company commonCompany = null;
Partner commonContactPartner = null;
TradingName commonTradingName = 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;
PurchaseOrder purchaseOrderTemp;
boolean allTradingNamesAreNull = true;
int count = 1;
for (Long purchaseOrderId : purchaseOrderIdList) {
purchaseOrderTemp = JPA.em().find(PurchaseOrder.class, purchaseOrderId);
purchaseOrderList.add(purchaseOrderTemp);
if (count == 1) {
commonCurrency = purchaseOrderTemp.getCurrency();
commonSupplierPartner = purchaseOrderTemp.getSupplierPartner();
commonCompany = purchaseOrderTemp.getCompany();
commonContactPartner = purchaseOrderTemp.getContactPartner();
commonPriceList = purchaseOrderTemp.getPriceList();
commonTradingName = purchaseOrderTemp.getTradingName();
allTradingNamesAreNull = commonTradingName == null;
} else {
if (commonCurrency != null && !commonCurrency.equals(purchaseOrderTemp.getCurrency())) {
commonCurrency = null;
}
if (commonSupplierPartner != null && !commonSupplierPartner.equals(purchaseOrderTemp.getSupplierPartner())) {
commonSupplierPartner = null;
}
if (commonCompany != null && !commonCompany.equals(purchaseOrderTemp.getCompany())) {
commonCompany = null;
}
if (commonContactPartner != null && !commonContactPartner.equals(purchaseOrderTemp.getContactPartner())) {
commonContactPartner = null;
existContactPartnerDiff = true;
}
if (commonPriceList != null && !commonPriceList.equals(purchaseOrderTemp.getPriceList())) {
commonPriceList = null;
existPriceListDiff = true;
}
if (!Objects.equals(commonTradingName, purchaseOrderTemp.getTradingName())) {
commonTradingName = null;
allTradingNamesAreNull = false;
}
}
count++;
}
StringBuilder fieldErrors = new StringBuilder();
if (commonCurrency == null) {
fieldErrors.append(I18n.get(IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_CURRENCY));
}
if (commonSupplierPartner == null) {
if (fieldErrors.length() > 0) {
fieldErrors.append("<br/>");
}
fieldErrors.append(I18n.get(IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_SUPPLIER_PARTNER));
}
if (commonCompany == null) {
if (fieldErrors.length() > 0) {
fieldErrors.append("<br/>");
}
fieldErrors.append(I18n.get(IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_COMPANY));
}
if (commonTradingName == null && !allTradingNamesAreNull) {
fieldErrors.append(I18n.get(IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_TRADING_NAME));
}
if (fieldErrors.length() > 0) {
response.setFlash(fieldErrors.toString());
return;
}
// Check if priceList or contactPartner are content in parameters
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("contactPartner") != null) {
commonContactPartner = JPA.em().find(Partner.class, new Long((Integer) ((Map) request.getContext().get("contactPartner")).get("id")));
}
if (!fromPopup && (existContactPartnerDiff || existPriceListDiff)) {
// Need to display intermediate screen to select some values
ActionViewBuilder confirmView = ActionView.define("Confirm merge purchase order").model(Wizard.class.getName()).add("form", "purchase-order-merge-confirm-form").param("popup", "true").param("show-toolbar", "false").param("show-confirm", "false").param("popup-save", "false").param("forceEdit", "true");
if (existPriceListDiff) {
confirmView.context("contextPriceListToCheck", "true");
}
if (existContactPartnerDiff) {
confirmView.context("contextContactPartnerToCheck", "true");
confirmView.context("contextPartnerId", commonSupplierPartner.getId().toString());
}
confirmView.context("purchaseOrderToMerge", Joiner.on(",").join(purchaseOrderIdList));
response.setView(confirmView.map());
return;
}
try {
PurchaseOrder purchaseOrder = Beans.get(PurchaseOrderService.class).mergePurchaseOrders(purchaseOrderList, commonCurrency, commonSupplierPartner, commonCompany, commonContactPartner, commonPriceList, commonTradingName);
if (purchaseOrder != null) {
// Open the generated purchase order in a new tab
response.setView(ActionView.define("Purchase order").model(PurchaseOrder.class.getName()).add("grid", "purchase-order-grid").add("form", "purchase-order-form").param("search-filters", "purchase-order-filters").param("forceEdit", "true").context("_showRecord", String.valueOf(purchaseOrder.getId())).map());
response.setCanClose(true);
}
} catch (Exception e) {
response.setFlash(e.getLocalizedMessage());
}
}
use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.
the class SaleOrderController method mergeSaleOrder.
@SuppressWarnings({ "rawtypes", "unchecked" })
public void mergeSaleOrder(ActionRequest request, ActionResponse response) {
List<SaleOrder> saleOrderList = new ArrayList<>();
List<Long> saleOrderIdList = new ArrayList<>();
boolean fromPopup = false;
String lineToMerge;
if (request.getContext().get("saleQuotationToMerge") != null) {
lineToMerge = "saleQuotationToMerge";
} else {
lineToMerge = "saleOrderToMerge";
}
if (request.getContext().get(lineToMerge) != null) {
if (request.getContext().get(lineToMerge) instanceof List) {
// No confirmation popup, sale orders are content in a parameter list
List<Map> saleOrderMap = (List<Map>) request.getContext().get(lineToMerge);
for (Map map : saleOrderMap) {
saleOrderIdList.add(new Long((Integer) map.get("id")));
}
} else {
// After confirmation popup, sale order's id are in a string separated by ","
String saleOrderIdListStr = (String) request.getContext().get(lineToMerge);
for (String saleOrderId : saleOrderIdListStr.split(",")) {
saleOrderIdList.add(new Long(saleOrderId));
}
fromPopup = true;
}
}
// Check if currency, clientPartner and company are the same for all selected
// sale orders
Currency commonCurrency = null;
Partner commonClientPartner = null;
Company commonCompany = null;
Partner commonContactPartner = null;
Team commonTeam = null;
// Useful to determine if a difference exists between teams of all sale orders
boolean existTeamDiff = false;
// Useful to determine if a difference exists between contact partners of all
// sale orders
boolean existContactPartnerDiff = false;
PriceList commonPriceList = null;
// Useful to determine if a difference exists between price lists of all sale
// orders
boolean existPriceListDiff = false;
StockLocation commonLocation = null;
// Useful to determine if a difference exists between stock locations of all
// sale orders
boolean existLocationDiff = false;
SaleOrder saleOrderTemp;
int count = 1;
for (Long saleOrderId : saleOrderIdList) {
saleOrderTemp = JPA.em().find(SaleOrder.class, saleOrderId);
saleOrderList.add(saleOrderTemp);
if (count == 1) {
commonCurrency = saleOrderTemp.getCurrency();
commonClientPartner = saleOrderTemp.getClientPartner();
commonCompany = saleOrderTemp.getCompany();
commonContactPartner = saleOrderTemp.getContactPartner();
commonTeam = saleOrderTemp.getTeam();
commonPriceList = saleOrderTemp.getPriceList();
commonLocation = saleOrderTemp.getStockLocation();
} else {
if (commonCurrency != null && !commonCurrency.equals(saleOrderTemp.getCurrency())) {
commonCurrency = null;
}
if (commonClientPartner != null && !commonClientPartner.equals(saleOrderTemp.getClientPartner())) {
commonClientPartner = null;
}
if (commonCompany != null && !commonCompany.equals(saleOrderTemp.getCompany())) {
commonCompany = null;
}
if (commonContactPartner != null && !commonContactPartner.equals(saleOrderTemp.getContactPartner())) {
commonContactPartner = null;
existContactPartnerDiff = true;
}
if (commonTeam != null && !commonTeam.equals(saleOrderTemp.getTeam())) {
commonTeam = null;
existTeamDiff = true;
}
if (commonPriceList != null && !commonPriceList.equals(saleOrderTemp.getPriceList())) {
commonPriceList = null;
existPriceListDiff = true;
}
if (commonLocation != null && !commonLocation.equals(saleOrderTemp.getStockLocation())) {
commonLocation = null;
existLocationDiff = true;
}
}
count++;
}
StringBuilder fieldErrors = new StringBuilder();
if (commonCurrency == null) {
fieldErrors.append(I18n.get(com.axelor.apps.sale.exception.IExceptionMessage.SALE_ORDER_MERGE_ERROR_CURRENCY));
}
if (commonClientPartner == null) {
if (fieldErrors.length() > 0) {
fieldErrors.append("<br/>");
}
fieldErrors.append(I18n.get(com.axelor.apps.sale.exception.IExceptionMessage.SALE_ORDER_MERGE_ERROR_CLIENT_PARTNER));
}
if (commonCompany == null) {
if (fieldErrors.length() > 0) {
fieldErrors.append("<br/>");
}
fieldErrors.append(I18n.get(com.axelor.apps.sale.exception.IExceptionMessage.SALE_ORDER_MERGE_ERROR_COMPANY));
}
if (fieldErrors.length() > 0) {
response.setFlash(fieldErrors.toString());
return;
}
// Check if priceList or contactPartner are content in parameters
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("contactPartner") != null) {
commonContactPartner = JPA.em().find(Partner.class, new Long((Integer) ((Map) request.getContext().get("contactPartner")).get("id")));
}
if (request.getContext().get("team") != null) {
commonTeam = JPA.em().find(Team.class, new Long((Integer) ((Map) request.getContext().get("team")).get("id")));
}
if (request.getContext().get("stockLocation") != null) {
commonLocation = JPA.em().find(StockLocation.class, new Long((Integer) ((Map) request.getContext().get("stockLocation")).get("id")));
}
if (!fromPopup && (existContactPartnerDiff || existPriceListDiff || existTeamDiff)) {
// Need to display intermediate screen to select some values
ActionViewBuilder confirmView = ActionView.define("Confirm merge sale order").model(Wizard.class.getName()).add("form", "sale-order-merge-confirm-form").param("popup", "true").param("show-toolbar", "false").param("show-confirm", "false").param("popup-save", "false").param("forceEdit", "true");
if (existPriceListDiff) {
confirmView.context("contextPriceListToCheck", "true");
}
if (existContactPartnerDiff) {
confirmView.context("contextContactPartnerToCheck", "true");
confirmView.context("contextPartnerId", commonClientPartner.getId().toString());
}
if (existTeamDiff) {
confirmView.context("contextTeamToCheck", "true");
}
if (existLocationDiff) {
confirmView.context("contextLocationToCheck", "true");
}
confirmView.context(lineToMerge, Joiner.on(",").join(saleOrderIdList));
response.setView(confirmView.map());
return;
}
try {
SaleOrder saleOrder = Beans.get(SaleOrderCreateServiceSupplychainImpl.class).mergeSaleOrders(saleOrderList, commonCurrency, commonClientPartner, commonCompany, commonLocation, commonContactPartner, commonPriceList, commonTeam);
if (saleOrder != null) {
// Open the generated sale order in a new tab
response.setView(ActionView.define("Sale order").model(SaleOrder.class.getName()).add("grid", "sale-order-grid").add("form", "sale-order-form").param("search-filters", "sale-order-filters").param("forceEdit", "true").context("_showRecord", String.valueOf(saleOrder.getId())).map());
response.setCanClose(true);
}
} catch (Exception e) {
response.setFlash(e.getLocalizedMessage());
}
}
Aggregations