use of com.axelor.apps.base.db.TradingName in project axelor-open-suite by axelor.
the class InvoiceGenerator method createInvoiceHeader.
protected Invoice createInvoiceHeader() throws AxelorException {
Invoice invoice = new Invoice();
invoice.setCompany(company);
invoice.setOperationTypeSelect(operationType);
if (partner == null) {
throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.INVOICE_GENERATOR_2), I18n.get(com.axelor.apps.base.exceptions.IExceptionMessage.EXCEPTION));
}
if (Beans.get(BlockingService.class).getBlocking(partner, company, BlockingRepository.INVOICING_BLOCKING) != null) {
throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.INVOICE_VALIDATE_BLOCKING));
}
invoice.setPartner(partner);
AccountingSituation accountingSituation = Beans.get(AccountingSituationService.class).getAccountingSituation(partner, company);
if (accountingSituation != null) {
invoice.setInvoiceAutomaticMail(accountingSituation.getInvoiceAutomaticMail());
invoice.setInvoiceMessageTemplate(accountingSituation.getInvoiceMessageTemplate());
invoice.setPfpValidatorUser(accountingSituation.getPfpValidatorUser());
}
if (paymentCondition == null) {
paymentCondition = InvoiceToolService.getPaymentCondition(invoice);
}
invoice.setPaymentCondition(paymentCondition);
if (paymentMode == null) {
paymentMode = InvoiceToolService.getPaymentMode(invoice);
}
invoice.setPaymentMode(paymentMode);
if (mainInvoicingAddress == null) {
mainInvoicingAddress = Beans.get(PartnerService.class).getInvoicingAddress(partner);
}
invoice.setAddress(mainInvoicingAddress);
invoice.setAddressStr(Beans.get(AddressService.class).computeAddressStr(invoice.getAddress()));
invoice.setContactPartner(contactPartner);
if (currency == null) {
currency = partner.getCurrency();
}
if (currency == null) {
throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.INVOICE_GENERATOR_6), I18n.get(com.axelor.apps.base.exceptions.IExceptionMessage.EXCEPTION));
}
invoice.setCurrency(currency);
invoice.setStatusSelect(InvoiceRepository.STATUS_DRAFT);
invoice.setPriceList(priceList);
invoice.setInternalReference(internalReference);
invoice.setExternalReference(externalReference);
invoice.setPrintingSettings(Beans.get(TradingNameService.class).getDefaultPrintingSettings(null, company));
invoice.setTradingName(tradingName);
if (groupProductsOnPrintings == null) {
groupProductsOnPrintings = partner.getGroupProductsOnPrintings();
}
invoice.setGroupProductsOnPrintings(groupProductsOnPrintings);
// Set ATI mode on invoice
AccountConfigService accountConfigService = Beans.get(AccountConfigService.class);
AccountConfig accountConfig = accountConfigService.getAccountConfig(company);
int atiChoice = accountConfig.getInvoiceInAtiSelect();
if (inAti == null) {
invoice.setInAti(accountConfigService.getInvoiceInAti(accountConfig));
} else if (atiChoice == AccountConfigRepository.INVOICE_ATI_DEFAULT || atiChoice == AccountConfigRepository.INVOICE_WT_DEFAULT) {
invoice.setInAti(inAti);
} else if (atiChoice == AccountConfigRepository.INVOICE_ATI_ALWAYS) {
invoice.setInAti(true);
} else {
invoice.setInAti(false);
}
if (partner.getFactorizedCustomer() && accountConfig.getFactorPartner() != null) {
List<BankDetails> bankDetailsList = accountConfig.getFactorPartner().getBankDetailsList();
companyBankDetails = bankDetailsList.stream().filter(bankDetails -> bankDetails.getIsDefault()).findFirst().orElse(null);
} else if (accountingSituation != null) {
if (paymentMode != null) {
if (paymentMode.equals(partner.getOutPaymentMode())) {
companyBankDetails = accountingSituation.getCompanyOutBankDetails();
} else if (paymentMode.equals(partner.getInPaymentMode())) {
companyBankDetails = accountingSituation.getCompanyInBankDetails();
}
}
}
if (companyBankDetails == null) {
companyBankDetails = company.getDefaultBankDetails();
List<BankDetails> allowedBDs = Beans.get(PaymentModeService.class).getCompatibleBankDetailsList(paymentMode, company);
if (!allowedBDs.contains(companyBankDetails)) {
companyBankDetails = null;
}
}
invoice.setCompanyBankDetails(companyBankDetails);
if (partner.getBankDetailsList() != null && invoice.getBankDetails() == null) {
invoice.setBankDetails(partner.getBankDetailsList().stream().filter(b -> b.getActive() && b.getIsDefault()).findFirst().orElse(null));
}
if (partner != null && !Strings.isNullOrEmpty(partner.getInvoiceComments())) {
invoice.setNote(partner.getInvoiceComments());
}
invoice.setInvoicesCopySelect(getInvoiceCopy());
initCollections(invoice);
return invoice;
}
use of com.axelor.apps.base.db.TradingName 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.TradingName in project axelor-open-suite by axelor.
the class PurchaseOrderLineServiceImpl method checkDifferentSupplier.
@Override
public void checkDifferentSupplier(PurchaseOrder purchaseOrder, PurchaseOrderLine purchaseOrderLine, ActionResponse response) {
if (!appBaseService.getAppBase().getEnableTradingNamesManagement()) {
return;
}
Product product = purchaseOrderLine.getProduct();
TradingName tradingName = purchaseOrder.getTradingName();
if (product == null || tradingName == null) {
return;
}
Partner supplierOnPurchaseOrder = purchaseOrder.getSupplierPartner();
Partner defaultSupplierOnProduct = product.getDefaultSupplierPartner();
if (defaultSupplierOnProduct == null) {
return;
}
if (supplierOnPurchaseOrder != defaultSupplierOnProduct) {
String message = String.format(I18n.get(IExceptionMessage.DIFFERENT_SUPPLIER));
String title = String.format("<span class='label %s'>%s</span>", ContextTool.SPAN_CLASS_WARNING, message);
response.setAttr("differentSupplierLabel", "title", title);
response.setAttr("differentSupplierLabel", "hidden", false);
} else {
response.setAttr("differentSupplierLabel", "hidden", true);
}
}
use of com.axelor.apps.base.db.TradingName 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.TradingName in project axelor-open-suite by axelor.
the class BatchDebtRecovery method debtRecoveryPartner.
public void debtRecoveryPartner() {
Company company = batch.getAccountingBatch().getCompany();
Set<TradingName> tradingNameSet = // Get the trading names for which to operate the debt recovery process
null;
if (appBaseService.getAppBase().getEnableTradingNamesManagement() && batch.getAccountingBatch().getIsDebtRecoveryByTradingName()) {
tradingNameSet = batch.getAccountingBatch().getTradingNameSet();
if (tradingNameSet == null || tradingNameSet.isEmpty()) {
tradingNameSet = company.getTradingNameSet();
}
}
Query<Partner> query = partnerRepository.all().filter("self.isContact = false " + "AND :_company MEMBER OF self.companySet " + "AND self.accountingSituationList IS NOT EMPTY " + "AND self.isCustomer = true " + "AND self.id NOT IN (" + Beans.get(BlockingService.class).listOfBlockedPartner(company, BlockingRepository.REMINDER_BLOCKING) + ")").bind("_company", company).order("id");
int offset = 0;
List<Partner> partnerList;
while (!(partnerList = query.fetch(FETCH_LIMIT, offset)).isEmpty()) {
findBatch();
for (Partner partner : partnerList) {
++offset;
boolean remindedOk;
// if recovery handled by trading name
if (tradingNameSet != null && !tradingNameSet.isEmpty()) {
boolean incrementPartner = false;
for (TradingName tradingName : tradingNameSet) {
try {
remindedOk = debtRecoveryService.debtRecoveryGenerate(partner, company, tradingName);
if (remindedOk) {
DebtRecovery debtRecovery = debtRecoveryService.getDebtRecovery(partner, company, tradingName);
addBatchToModel(debtRecovery);
incrementPartner = true;
}
// Catching exceptions
} catch (AxelorException e) {
TraceBackService.trace(new AxelorException(e, e.getCategory(), I18n.get("Partner") + " %s, " + I18n.get("Trading name") + " %s", partner.getName(), tradingName.getName()), ExceptionOriginRepository.DEBT_RECOVERY, batch.getId());
incrementAnomaly(partner);
break;
} catch (Exception e) {
TraceBackService.trace(new Exception(String.format(I18n.get("Partner") + " %s, " + I18n.get("Trading name") + " %s", partner.getName(), tradingName.getName()), e), ExceptionOriginRepository.DEBT_RECOVERY, batch.getId());
incrementAnomaly(partner);
break;
}
// \Catching exceptions
}
if (incrementPartner) {
incrementDone(partner);
}
} else {
// if recovery handled by company
try {
remindedOk = debtRecoveryService.debtRecoveryGenerate(partner, company, null);
if (remindedOk) {
DebtRecovery debtRecovery = debtRecoveryService.getDebtRecovery(partner, company);
addBatchToModel(debtRecovery);
incrementDone(partner);
}
// Catching exceptions
} catch (AxelorException e) {
TraceBackService.trace(new AxelorException(e, e.getCategory(), I18n.get("Partner") + " %s", partner.getName()), ExceptionOriginRepository.DEBT_RECOVERY, batch.getId());
incrementAnomaly(partner);
break;
} catch (Exception e) {
TraceBackService.trace(new Exception(String.format(I18n.get("Partner") + " %s", partner.getName()), e), ExceptionOriginRepository.DEBT_RECOVERY, batch.getId());
incrementAnomaly(partner);
break;
}
// \Catching exceptions
}
}
JPA.clear();
}
}
Aggregations