use of com.axelor.apps.base.db.PriceList 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<>();
List<Long> purchaseOrderIdList = new ArrayList<>();
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 and company 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;
StockLocation commonLocation = null;
// Useful to determine if a difference exists between stock locations of all
// purchase orders
boolean existLocationDiff = false;
boolean allTradingNamesAreNull = true;
PurchaseOrder purchaseOrderTemp;
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();
commonLocation = purchaseOrderTemp.getStockLocation();
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 (!Objects.equals(commonTradingName, purchaseOrderTemp.getTradingName())) {
commonTradingName = null;
allTradingNamesAreNull = false;
}
if (commonContactPartner != null && !commonContactPartner.equals(purchaseOrderTemp.getContactPartner())) {
commonContactPartner = null;
existContactPartnerDiff = true;
}
if (commonPriceList != null && !commonPriceList.equals(purchaseOrderTemp.getPriceList())) {
commonPriceList = null;
existPriceListDiff = true;
}
if (commonLocation != null && !commonLocation.equals(purchaseOrderTemp.getStockLocation())) {
commonLocation = null;
existLocationDiff = true;
}
}
count++;
}
StringBuilder fieldErrors = new StringBuilder();
if (commonCurrency == null) {
fieldErrors.append(I18n.get(com.axelor.apps.purchase.exception.IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_CURRENCY));
}
if (commonSupplierPartner == null) {
if (fieldErrors.length() > 0) {
fieldErrors.append("<br/>");
}
fieldErrors.append(I18n.get(com.axelor.apps.purchase.exception.IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_SUPPLIER_PARTNER));
}
if (commonCompany == null) {
if (fieldErrors.length() > 0) {
fieldErrors.append("<br/>");
}
fieldErrors.append(I18n.get(com.axelor.apps.purchase.exception.IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_COMPANY));
}
if (commonTradingName == null && !allTradingNamesAreNull) {
fieldErrors.append(I18n.get(com.axelor.apps.purchase.exception.IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_TRADING_NAME));
}
if (fieldErrors.length() > 0) {
response.setFlash(fieldErrors.toString());
return;
}
// 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("stockLocation") != null) {
commonLocation = JPA.em().find(StockLocation.class, new Long((Integer) ((Map) request.getContext().get("stockLocation")).get("id")));
}
if (!fromPopup && (existContactPartnerDiff || existPriceListDiff || existLocationDiff)) {
// 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());
}
if (existLocationDiff) {
confirmView.context("contextLocationToCheck", "true");
}
confirmView.context("purchaseOrderToMerge", Joiner.on(",").join(purchaseOrderIdList));
response.setView(confirmView.map());
return;
}
try {
PurchaseOrder purchaseOrder = Beans.get(PurchaseOrderSupplychainService.class).mergePurchaseOrders(purchaseOrderList, commonCurrency, commonSupplierPartner, commonCompany, commonLocation, 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.PriceList in project axelor-open-suite by axelor.
the class PartnerPriceListServiceImpl method getDefaultPriceList.
@Override
public PriceList getDefaultPriceList(Partner partner, int priceListTypeSelect) {
if (partner == null) {
return null;
}
partner = Beans.get(PartnerRepository.class).find(partner.getId());
PartnerPriceList partnerPriceList = getPartnerPriceList(partner, priceListTypeSelect);
if (partnerPriceList == null) {
return null;
}
Set<PriceList> priceListSet = partnerPriceList.getPriceListSet();
if (priceListSet == null) {
return null;
}
List<PriceList> priceLists = priceListSet.stream().filter(priceList -> (priceList.getApplicationBeginDate() == null || priceList.getApplicationBeginDate().compareTo(appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null))) <= 0) && (priceList.getApplicationEndDate() == null || priceList.getApplicationEndDate().compareTo(appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null))) >= 0)).collect(Collectors.toList());
if (priceLists.size() == 1) {
return priceLists.get(0);
} else {
return null;
}
}
use of com.axelor.apps.base.db.PriceList in project axelor-open-suite by axelor.
the class PartnerPriceListServiceImpl method getPriceListDomain.
public String getPriceListDomain(Partner partner, int priceListTypeSelect) {
if (partner == null) {
return "self.id IN (0)";
}
// get all non exclusive partner price lists
List<PartnerPriceList> partnerPriceLists = Beans.get(PartnerPriceListRepository.class).all().filter("self.typeSelect = :_priceListTypeSelect " + "AND self.isExclusive = false").bind("_priceListTypeSelect", priceListTypeSelect).fetch();
// get (maybe exclusive) list for the partner
PartnerPriceList partnerPriceList = getPartnerPriceList(partner, priceListTypeSelect);
if (partnerPriceList != null && partnerPriceList.getIsExclusive()) {
partnerPriceLists.add(partnerPriceList);
}
if (partnerPriceLists.isEmpty()) {
return "self.id IN (0)";
}
List<PriceList> priceLists = partnerPriceLists.stream().flatMap(partnerPriceList1 -> partnerPriceList1.getPriceListSet().stream()).filter(priceList -> priceList.getIsActive() && (priceList.getApplicationBeginDate() == null || priceList.getApplicationBeginDate().compareTo(appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null))) <= 0) && (priceList.getApplicationEndDate() == null || priceList.getApplicationEndDate().compareTo(appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null))) >= 0)).collect(Collectors.toList());
return "self.id IN (" + StringTool.getIdListString(priceLists) + ")";
}
use of com.axelor.apps.base.db.PriceList in project axelor-open-suite by axelor.
the class PartnerServiceImpl method getSalePriceList.
/**
* Search for the sale price list for the current date in the partner.
*
* @param partner
* @return the sale price list for the partner null if no active price list has been found
*/
@Override
public PriceList getSalePriceList(Partner partner) {
PartnerPriceList partnerPriceList = partner.getSalePartnerPriceList();
if (partnerPriceList == null) {
return null;
}
Set<PriceList> priceListSet = partnerPriceList.getPriceListSet();
if (priceListSet == null) {
return null;
}
LocalDate today = Beans.get(AppBaseService.class).getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null));
List<PriceList> candidatePriceListList = new ArrayList<>();
for (PriceList priceList : priceListSet) {
LocalDate beginDate = priceList.getApplicationBeginDate() != null ? priceList.getApplicationBeginDate() : LocalDate.MIN;
LocalDate endDate = priceList.getApplicationEndDate() != null ? priceList.getApplicationEndDate() : LocalDate.MAX;
if (beginDate.compareTo(today) <= 0 && today.compareTo(endDate) <= 0) {
candidatePriceListList.add(priceList);
}
}
// if we found multiple price list, then the user will have to select one
if (candidatePriceListList.size() == 1) {
return candidatePriceListList.get(0);
} else {
return null;
}
}
use of com.axelor.apps.base.db.PriceList 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());
}
}
Aggregations