Search in sources :

Example 11 with PriceList

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 stock 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;
    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();
            if (commonSaleOrder == null) {
                saleOrderIsNull = 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 (invoiceTemp.getSaleOrder() != null) {
                saleOrderIsNull = 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 (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(SaleOrderInvoiceService.class).mergeInvoice(invoiceList, commonCompany, commonCurrency, commonPartner, commonContactPartner, commonPriceList, commonPaymentMode, commonPaymentCondition, commonSaleOrder);
        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) SaleOrderInvoiceService(com.axelor.apps.supplychain.service.SaleOrderInvoiceService) ArrayList(java.util.ArrayList) SaleOrder(com.axelor.apps.sale.db.SaleOrder) ActionViewBuilder(com.axelor.meta.schema.actions.ActionView.ActionViewBuilder) AxelorException(com.axelor.exception.AxelorException) Currency(com.axelor.apps.base.db.Currency) ArrayList(java.util.ArrayList) List(java.util.List) PriceList(com.axelor.apps.base.db.PriceList) 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 12 with PriceList

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<>();
    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 13 with PriceList

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

the class IntercoServiceImpl method generateIntercoInvoice.

@Override
public Invoice generateIntercoInvoice(Invoice invoice) throws AxelorException {
    PartnerService partnerService = Beans.get(PartnerService.class);
    InvoiceRepository invoiceRepository = Beans.get(InvoiceRepository.class);
    InvoiceService invoiceService = Beans.get(InvoiceService.class);
    boolean isPurchase;
    // set the status
    int generatedOperationTypeSelect;
    int priceListRepositoryType;
    switch(invoice.getOperationTypeSelect()) {
        case InvoiceRepository.OPERATION_TYPE_SUPPLIER_PURCHASE:
            generatedOperationTypeSelect = InvoiceRepository.OPERATION_TYPE_CLIENT_SALE;
            priceListRepositoryType = PriceListRepository.TYPE_SALE;
            isPurchase = false;
            break;
        case InvoiceRepository.OPERATION_TYPE_SUPPLIER_REFUND:
            generatedOperationTypeSelect = InvoiceRepository.OPERATION_TYPE_CLIENT_REFUND;
            priceListRepositoryType = PriceListRepository.TYPE_SALE;
            isPurchase = false;
            break;
        case InvoiceRepository.OPERATION_TYPE_CLIENT_SALE:
            generatedOperationTypeSelect = InvoiceRepository.OPERATION_TYPE_SUPPLIER_PURCHASE;
            priceListRepositoryType = PriceListRepository.TYPE_PURCHASE;
            isPurchase = true;
            break;
        case InvoiceRepository.OPERATION_TYPE_CLIENT_REFUND:
            generatedOperationTypeSelect = InvoiceRepository.OPERATION_TYPE_SUPPLIER_REFUND;
            priceListRepositoryType = PriceListRepository.TYPE_PURCHASE;
            isPurchase = true;
            break;
        default:
            throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.INVOICE_MISSING_TYPE), invoice);
    }
    Company intercoCompany = findIntercoCompany(invoice.getPartner());
    Partner intercoPartner = invoice.getCompany().getPartner();
    PaymentMode intercoPaymentMode = Beans.get(PaymentModeService.class).reverseInOut(invoice.getPaymentMode());
    Address intercoAddress = partnerService.getInvoicingAddress(intercoPartner);
    BankDetails intercoBankDetails = partnerService.getDefaultBankDetails(intercoPartner);
    AccountingSituation accountingSituation = Beans.get(AccountingSituationService.class).getAccountingSituation(intercoPartner, intercoCompany);
    PriceList intercoPriceList = Beans.get(PartnerPriceListService.class).getDefaultPriceList(intercoPartner, priceListRepositoryType);
    Invoice intercoInvoice = invoiceRepository.copy(invoice, true);
    intercoInvoice.setOperationTypeSelect(generatedOperationTypeSelect);
    intercoInvoice.setCompany(intercoCompany);
    intercoInvoice.setPartner(intercoPartner);
    intercoInvoice.setAddress(intercoAddress);
    intercoInvoice.setAddressStr(Beans.get(AddressService.class).computeAddressStr(intercoAddress));
    intercoInvoice.setPaymentMode(intercoPaymentMode);
    intercoInvoice.setBankDetails(intercoBankDetails);
    Set<Invoice> invoices = invoiceService.getDefaultAdvancePaymentInvoice(intercoInvoice);
    intercoInvoice.setAdvancePaymentInvoiceSet(invoices);
    if (accountingSituation != null) {
        intercoInvoice.setInvoiceAutomaticMail(accountingSituation.getInvoiceAutomaticMail());
        intercoInvoice.setInvoiceMessageTemplate(accountingSituation.getInvoiceMessageTemplate());
        intercoInvoice.setPfpValidatorUser(accountingSituation.getPfpValidatorUser());
    }
    intercoInvoice.setPriceList(intercoPriceList);
    intercoInvoice.setInvoicesCopySelect((intercoPartner.getInvoicesCopySelect() == 0) ? DEFAULT_INVOICE_COPY : intercoPartner.getInvoicesCopySelect());
    intercoInvoice.setCreatedByInterco(true);
    intercoInvoice.setInterco(false);
    intercoInvoice.setPrintingSettings(intercoCompany.getPrintingSettings());
    if (intercoInvoice.getInvoiceLineList() != null) {
        for (InvoiceLine invoiceLine : intercoInvoice.getInvoiceLineList()) {
            invoiceLine.setInvoice(intercoInvoice);
            createIntercoInvoiceLine(invoiceLine, isPurchase);
        }
    }
    invoiceService.compute(intercoInvoice);
    intercoInvoice.setExternalReference(invoice.getInvoiceId());
    intercoInvoice = invoiceRepository.save(intercoInvoice);
    if (Beans.get(AppSupplychainService.class).getAppSupplychain().getIntercoInvoiceCreateValidated()) {
        Beans.get(InvoiceService.class).validate(intercoInvoice);
    }
    invoice.setExternalReference(intercoInvoice.getInvoiceId());
    return intercoInvoice;
}
Also used : AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) PaymentModeService(com.axelor.apps.account.service.payment.PaymentModeService) Invoice(com.axelor.apps.account.db.Invoice) Address(com.axelor.apps.base.db.Address) PartnerPriceListService(com.axelor.apps.base.service.PartnerPriceListService) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) BankDetails(com.axelor.apps.base.db.BankDetails) InvoiceService(com.axelor.apps.account.service.invoice.InvoiceService) PartnerService(com.axelor.apps.base.service.PartnerService) AccountingSituation(com.axelor.apps.account.db.AccountingSituation) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) Partner(com.axelor.apps.base.db.Partner) PriceList(com.axelor.apps.base.db.PriceList) AccountingSituationService(com.axelor.apps.account.service.AccountingSituationService) PaymentMode(com.axelor.apps.account.db.PaymentMode)

Example 14 with PriceList

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

the class SaleOrderController method createSaleOrder.

@SuppressWarnings("unchecked")
public void createSaleOrder(ActionRequest request, ActionResponse response) throws AxelorException {
    SaleOrder origin = Beans.get(SaleOrderRepository.class).find(Long.parseLong(request.getContext().get("_idCopy").toString()));
    if (origin != null) {
        LinkedHashMap<String, Object> wizardCurrencyContext = (LinkedHashMap<String, Object>) request.getContext().get("_wizardCurrency");
        Integer wizardCurrencyId = (Integer) wizardCurrencyContext.get("id");
        Currency wizardCurrency = Beans.get(CurrencyRepository.class).find(Long.valueOf(wizardCurrencyId));
        PriceList wizardPriceList = null;
        if (request.getContext().get("_wizardPriceList") != null) {
            LinkedHashMap<String, Object> wizardPriceListContext = (LinkedHashMap<String, Object>) request.getContext().get("_wizardPriceList");
            Integer wizardPriceListId = (Integer) wizardPriceListContext.get("id");
            wizardPriceList = Beans.get(PriceListRepository.class).find(Long.valueOf(wizardPriceListId));
        }
        SaleOrder copy = Beans.get(SaleOrderCreateService.class).createSaleOrder(origin, wizardCurrency, wizardPriceList);
        response.setValues(Mapper.toMap(copy));
    }
}
Also used : SaleOrderCreateService(com.axelor.apps.sale.service.saleorder.SaleOrderCreateService) Currency(com.axelor.apps.base.db.Currency) CurrencyRepository(com.axelor.apps.base.db.repo.CurrencyRepository) SaleOrderRepository(com.axelor.apps.sale.db.repo.SaleOrderRepository) SaleOrder(com.axelor.apps.sale.db.SaleOrder) PriceList(com.axelor.apps.base.db.PriceList) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with PriceList

use of com.axelor.apps.base.db.PriceList 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<SaleOrder>();
    List<Long> saleOrderIdList = new ArrayList<Long>();
    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;
    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();
        } 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;
            }
        }
        count++;
    }
    StringBuilder fieldErrors = new StringBuilder();
    if (commonCurrency == null) {
        fieldErrors.append(I18n.get(IExceptionMessage.SALE_ORDER_MERGE_ERROR_CURRENCY));
    }
    if (commonClientPartner == null) {
        if (fieldErrors.length() > 0) {
            fieldErrors.append("<br/>");
        }
        fieldErrors.append(I18n.get(IExceptionMessage.SALE_ORDER_MERGE_ERROR_CLIENT_PARTNER));
    }
    if (commonCompany == null) {
        if (fieldErrors.length() > 0) {
            fieldErrors.append("<br/>");
        }
        fieldErrors.append(I18n.get(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 (!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");
        }
        confirmView.context(lineToMerge, Joiner.on(",").join(saleOrderIdList));
        response.setView(confirmView.map());
        return;
    }
    try {
        SaleOrder saleOrder = Beans.get(SaleOrderCreateService.class).mergeSaleOrders(saleOrderList, commonCurrency, commonClientPartner, commonCompany, 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());
    }
}
Also used : SaleOrderCreateService(com.axelor.apps.sale.service.saleorder.SaleOrderCreateService) Company(com.axelor.apps.base.db.Company) ArrayList(java.util.ArrayList) SaleOrder(com.axelor.apps.sale.db.SaleOrder) ActionViewBuilder(com.axelor.meta.schema.actions.ActionView.ActionViewBuilder) BirtException(org.eclipse.birt.core.exception.BirtException) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException) Currency(com.axelor.apps.base.db.Currency) List(java.util.List) PriceList(com.axelor.apps.base.db.PriceList) ArrayList(java.util.ArrayList) Team(com.axelor.team.db.Team) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Partner(com.axelor.apps.base.db.Partner) PriceList(com.axelor.apps.base.db.PriceList) Wizard(com.axelor.apps.base.db.Wizard)

Aggregations

PriceList (com.axelor.apps.base.db.PriceList)24 AxelorException (com.axelor.exception.AxelorException)13 ArrayList (java.util.ArrayList)12 Company (com.axelor.apps.base.db.Company)11 Partner (com.axelor.apps.base.db.Partner)11 List (java.util.List)11 Currency (com.axelor.apps.base.db.Currency)9 PriceListLine (com.axelor.apps.base.db.PriceListLine)7 Wizard (com.axelor.apps.base.db.Wizard)7 ActionViewBuilder (com.axelor.meta.schema.actions.ActionView.ActionViewBuilder)7 LocalDate (java.time.LocalDate)7 Map (java.util.Map)7 BigDecimal (java.math.BigDecimal)6 Invoice (com.axelor.apps.account.db.Invoice)5 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)5 PaymentMode (com.axelor.apps.account.db.PaymentMode)5 SaleOrder (com.axelor.apps.sale.db.SaleOrder)5 PaymentCondition (com.axelor.apps.account.db.PaymentCondition)4 PartnerPriceList (com.axelor.apps.base.db.PartnerPriceList)4 Product (com.axelor.apps.base.db.Product)4