Search in sources :

Example 1 with SaleOrderInvoiceService

use of com.axelor.apps.supplychain.service.SaleOrderInvoiceService in project axelor-open-suite by axelor.

the class SaleOrderController method changeWizardOperationDomain.

/**
 * Called on sale order invoicing wizard form. Call {@link
 * SaleOrderInvoiceService#getInvoicingWizardOperationDomain(SaleOrder)}
 *
 * @param request
 * @param response
 */
public void changeWizardOperationDomain(ActionRequest request, ActionResponse response) {
    SaleOrder saleOrder = request.getContext().asType(SaleOrder.class);
    List<Integer> operationSelectValues = Beans.get(SaleOrderInvoiceService.class).getInvoicingWizardOperationDomain(saleOrder);
    response.setAttr("operationSelect", "value", operationSelectValues.stream().min(Integer::compareTo).orElse(null));
    response.setAttr("operationSelect", "selection-in", operationSelectValues);
}
Also used : SaleOrderInvoiceService(com.axelor.apps.supplychain.service.SaleOrderInvoiceService) SaleOrder(com.axelor.apps.sale.db.SaleOrder)

Example 2 with SaleOrderInvoiceService

use of com.axelor.apps.supplychain.service.SaleOrderInvoiceService in project axelor-open-suite by axelor.

the class BatchOrderInvoicingSale method process.

@Override
protected void process() {
    SupplychainBatch supplychainBatch = batch.getSupplychainBatch();
    List<String> filterList = new ArrayList<>();
    Query<SaleOrder> query = Beans.get(SaleOrderRepository.class).all();
    if (supplychainBatch.getCompany() != null) {
        filterList.add("self.company = :company");
        query.bind("company", supplychainBatch.getCompany());
    }
    if (supplychainBatch.getSalespersonOrBuyerSet() != null && !supplychainBatch.getSalespersonOrBuyerSet().isEmpty()) {
        filterList.add("self.salespersonUser IN (:salespersonSet)");
        query.bind("salespersonSet", supplychainBatch.getSalespersonOrBuyerSet());
    }
    if (supplychainBatch.getTeam() != null) {
        filterList.add("self.team = :team " + "OR self.team IS NULL AND self.salespersonUser IS NOT NULL AND self.salespersonUser.activeTeam = :team");
        query.bind("team", supplychainBatch.getTeam());
    }
    if (!Strings.isNullOrEmpty(supplychainBatch.getDeliveryOrReceiptState())) {
        List<Integer> delivereyStateList = StringTool.getIntegerList(supplychainBatch.getDeliveryOrReceiptState());
        filterList.add("self.deliveryState IN (:delivereyStateList)");
        query.bind("delivereyStateList", delivereyStateList);
    }
    if (!Strings.isNullOrEmpty(supplychainBatch.getStatusSelect())) {
        List<Integer> statusSelectList = StringTool.getIntegerList(supplychainBatch.getStatusSelect());
        filterList.add("self.statusSelect IN (:statusSelectList)");
        query.bind("statusSelectList", statusSelectList);
    }
    if (supplychainBatch.getOrderUpToDate() != null) {
        filterList.add("self.orderDate <= :orderUpToDate");
        query.bind("orderUpToDate", supplychainBatch.getOrderUpToDate());
    }
    filterList.add("self.amountInvoiced < self.exTaxTotal");
    filterList.add("NOT EXISTS (SELECT 1 FROM Invoice invoice WHERE invoice.statusSelect != :invoiceStatusSelect " + "AND (invoice.saleOrder = self " + "OR invoice.saleOrder IS NULL AND EXISTS (SELECT 1 FROM invoice.invoiceLineList invoiceLine " + "WHERE invoiceLine.saleOrderLine MEMBER OF self.saleOrderLineList)))");
    filterList.add("self.clientPartner.id NOT IN (" + Beans.get(BlockingService.class).listOfBlockedPartner(supplychainBatch.getCompany(), BlockingRepository.INVOICING_BLOCKING) + ")");
    query.bind("invoiceStatusSelect", InvoiceRepository.STATUS_CANCELED);
    List<Long> anomalyList = Lists.newArrayList(0L);
    filterList.add("self.id NOT IN (:anomalyList)");
    query.bind("anomalyList", anomalyList);
    String filter = filterList.stream().map(item -> String.format("(%s)", item)).collect(Collectors.joining(" AND "));
    query.filter(filter);
    SaleOrderInvoiceService saleOrderInvoiceService = Beans.get(SaleOrderInvoiceService.class);
    Set<Long> treatedSet = new HashSet<>();
    for (List<SaleOrder> saleOrderList; !(saleOrderList = query.fetch(FETCH_LIMIT)).isEmpty(); JPA.clear()) {
        for (SaleOrder saleOrder : saleOrderList) {
            if (treatedSet.contains(saleOrder.getId())) {
                throw new IllegalArgumentException("Invoice generation error");
            }
            treatedSet.add(saleOrder.getId());
            try {
                saleOrderInvoiceService.generateInvoice(saleOrder);
                incrementDone();
            } catch (Exception e) {
                incrementAnomaly();
                anomalyList.add(saleOrder.getId());
                query.bind("anomalyList", anomalyList);
                TraceBackService.trace(e, ExceptionOriginRepository.INVOICE_ORIGIN, batch.getId());
                e.printStackTrace();
                break;
            }
        }
    }
}
Also used : Query(com.axelor.db.Query) StringTool(com.axelor.apps.tool.StringTool) JPA(com.axelor.db.JPA) TraceBackService(com.axelor.exception.service.TraceBackService) Set(java.util.Set) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) BlockingRepository(com.axelor.apps.base.db.repo.BlockingRepository) SaleOrderRepository(com.axelor.apps.sale.db.repo.SaleOrderRepository) List(java.util.List) Lists(com.google.common.collect.Lists) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) Beans(com.axelor.inject.Beans) BlockingService(com.axelor.apps.base.service.BlockingService) ExceptionOriginRepository(com.axelor.exception.db.repo.ExceptionOriginRepository) SaleOrder(com.axelor.apps.sale.db.SaleOrder) SaleOrderInvoiceService(com.axelor.apps.supplychain.service.SaleOrderInvoiceService) SupplychainBatch(com.axelor.apps.supplychain.db.SupplychainBatch) SaleOrderInvoiceService(com.axelor.apps.supplychain.service.SaleOrderInvoiceService) ArrayList(java.util.ArrayList) SaleOrder(com.axelor.apps.sale.db.SaleOrder) SupplychainBatch(com.axelor.apps.supplychain.db.SupplychainBatch) SaleOrderRepository(com.axelor.apps.sale.db.repo.SaleOrderRepository) HashSet(java.util.HashSet)

Example 3 with SaleOrderInvoiceService

use of com.axelor.apps.supplychain.service.SaleOrderInvoiceService in project axelor-open-suite by axelor.

the class SaleOrderController method generateInvoice.

/**
 * Called from the sale order invoicing wizard. Call {@link
 * com.axelor.apps.supplychain.service.SaleOrderInvoiceService#generateInvoice(SaleOrder, int,
 * BigDecimal, boolean, Map)} } Return to the view the generated invoice.
 *
 * @param request
 * @param response
 */
@SuppressWarnings(value = "unchecked")
public void generateInvoice(ActionRequest request, ActionResponse response) {
    Context context = request.getContext();
    try {
        SaleOrder saleOrder = context.asType(SaleOrder.class);
        int operationSelect = Integer.parseInt(context.get("operationSelect").toString());
        boolean isPercent = (Boolean) context.getOrDefault("isPercent", false);
        BigDecimal amountToInvoice = new BigDecimal(context.getOrDefault("amountToInvoice", "0").toString());
        SaleOrderInvoiceService saleOrderInvoiceService = Beans.get(SaleOrderInvoiceService.class);
        saleOrderInvoiceService.displayErrorMessageIfSaleOrderIsInvoiceable(saleOrder, amountToInvoice, isPercent);
        Map<Long, BigDecimal> qtyToInvoiceMap = new HashMap<>();
        List<Map<String, Object>> saleOrderLineListContext;
        saleOrderLineListContext = (List<Map<String, Object>>) request.getRawContext().get("saleOrderLineList");
        for (Map<String, Object> map : saleOrderLineListContext) {
            if (map.get(SO_LINES_WIZARD_QTY_TO_INVOICE_FIELD) != null) {
                BigDecimal qtyToInvoiceItem = new BigDecimal(map.get(SO_LINES_WIZARD_QTY_TO_INVOICE_FIELD).toString());
                if (qtyToInvoiceItem.compareTo(BigDecimal.ZERO) != 0) {
                    Long soLineId = Long.valueOf((Integer) map.get("id"));
                    qtyToInvoiceMap.put(soLineId, qtyToInvoiceItem);
                }
            }
        }
        // Information to send to the service to handle an invoicing on timetables
        List<Long> timetableIdList = new ArrayList<>();
        ArrayList<LinkedHashMap<String, Object>> uninvoicedTimetablesList = (context.get("uninvoicedTimetablesList") != null) ? (ArrayList<LinkedHashMap<String, Object>>) context.get("uninvoicedTimetablesList") : null;
        if (uninvoicedTimetablesList != null && !uninvoicedTimetablesList.isEmpty()) {
            for (LinkedHashMap<String, Object> timetable : uninvoicedTimetablesList) {
                if (timetable.get("toInvoice") != null && (boolean) timetable.get("toInvoice")) {
                    timetableIdList.add(Long.parseLong(timetable.get("id").toString()));
                }
            }
        }
        saleOrder = Beans.get(SaleOrderRepository.class).find(saleOrder.getId());
        Invoice invoice = saleOrderInvoiceService.generateInvoice(saleOrder, operationSelect, amountToInvoice, isPercent, qtyToInvoiceMap, timetableIdList);
        if (invoice != null) {
            response.setCanClose(true);
            response.setView(ActionView.define(I18n.get("Invoice generated")).model(Invoice.class.getName()).add("form", "invoice-form").add("grid", "invoice-grid").param("search-filters", "customer-invoices-filters").context("_showRecord", String.valueOf(invoice.getId())).context("_operationTypeSelect", InvoiceRepository.OPERATION_TYPE_CLIENT_SALE).context("todayDate", Beans.get(AppSupplychainService.class).getTodayDate(saleOrder.getCompany())).map());
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Context(com.axelor.rpc.Context) Invoice(com.axelor.apps.account.db.Invoice) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SaleOrderInvoiceService(com.axelor.apps.supplychain.service.SaleOrderInvoiceService) ArrayList(java.util.ArrayList) SaleOrder(com.axelor.apps.sale.db.SaleOrder) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

SaleOrder (com.axelor.apps.sale.db.SaleOrder)3 SaleOrderInvoiceService (com.axelor.apps.supplychain.service.SaleOrderInvoiceService)3 ArrayList (java.util.ArrayList)2 Invoice (com.axelor.apps.account.db.Invoice)1 InvoiceRepository (com.axelor.apps.account.db.repo.InvoiceRepository)1 BlockingRepository (com.axelor.apps.base.db.repo.BlockingRepository)1 BlockingService (com.axelor.apps.base.service.BlockingService)1 SaleOrderRepository (com.axelor.apps.sale.db.repo.SaleOrderRepository)1 SupplychainBatch (com.axelor.apps.supplychain.db.SupplychainBatch)1 StringTool (com.axelor.apps.tool.StringTool)1 JPA (com.axelor.db.JPA)1 Query (com.axelor.db.Query)1 AxelorException (com.axelor.exception.AxelorException)1 ExceptionOriginRepository (com.axelor.exception.db.repo.ExceptionOriginRepository)1 TraceBackService (com.axelor.exception.service.TraceBackService)1 Beans (com.axelor.inject.Beans)1 Context (com.axelor.rpc.Context)1 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 BigDecimal (java.math.BigDecimal)1