Search in sources :

Example 51 with PurchaseOrder

use of com.axelor.apps.purchase.db.PurchaseOrder in project axelor-open-suite by axelor.

the class SaleOrderController method generatePurchaseOrdersFromSelectedSOLines.

@SuppressWarnings({ "unchecked" })
public void generatePurchaseOrdersFromSelectedSOLines(ActionRequest request, ActionResponse response) {
    SaleOrder saleOrder = request.getContext().asType(SaleOrder.class);
    try {
        if (saleOrder.getId() != null) {
            Partner supplierPartner = null;
            List<Long> saleOrderLineIdSelected = new ArrayList<>();
            Boolean isDirectOrderLocation = false;
            Boolean noProduct = true;
            Map<String, Object> values = getSelectedId(request, response, saleOrder);
            supplierPartner = (Partner) values.get("supplierPartner");
            saleOrderLineIdSelected = (List<Long>) values.get("saleOrderLineIdSelected");
            isDirectOrderLocation = (Boolean) values.get("isDirectOrderLocation");
            if (supplierPartner == null) {
                saleOrderLineIdSelected = new ArrayList<>();
                for (SaleOrderLine saleOrderLine : saleOrder.getSaleOrderLineList()) {
                    if (saleOrderLine.isSelected()) {
                        if (supplierPartner == null) {
                            supplierPartner = saleOrderLine.getSupplierPartner();
                        }
                        if (saleOrderLine.getProduct() != null) {
                            noProduct = false;
                        }
                        saleOrderLineIdSelected.add(saleOrderLine.getId());
                    }
                }
                if (saleOrderLineIdSelected.isEmpty() || noProduct) {
                    response.setFlash(I18n.get(IExceptionMessage.SO_LINE_PURCHASE_AT_LEAST_ONE));
                } else {
                    response.setView(ActionView.define("SaleOrder").model(SaleOrder.class.getName()).add("form", "sale-order-generate-po-select-supplierpartner-form").param("popup", "true").param("show-toolbar", "false").param("show-confirm", "false").param("popup-save", "false").param("forceEdit", "true").context("_showRecord", String.valueOf(saleOrder.getId())).context("supplierPartnerId", ((supplierPartner != null) ? supplierPartner.getId() : 0L)).context("saleOrderLineIdSelected", Joiner.on(",").join(saleOrderLineIdSelected)).map());
                }
            } else {
                List<SaleOrderLine> saleOrderLinesSelected = JPA.all(SaleOrderLine.class).filter("self.id IN (:saleOderLineIdList)").bind("saleOderLineIdList", saleOrderLineIdSelected).fetch();
                PurchaseOrder purchaseOrder = Beans.get(SaleOrderPurchaseService.class).createPurchaseOrder(supplierPartner, saleOrderLinesSelected, Beans.get(SaleOrderRepository.class).find(saleOrder.getId()));
                response.setView(ActionView.define(I18n.get("Purchase order")).model(PurchaseOrder.class.getName()).add("form", "purchase-order-form").param("forceEdit", "true").context("_showRecord", String.valueOf(purchaseOrder.getId())).map());
                if (isDirectOrderLocation == false) {
                    response.setCanClose(true);
                }
            }
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : ArrayList(java.util.ArrayList) SaleOrder(com.axelor.apps.sale.db.SaleOrder) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) AxelorException(com.axelor.exception.AxelorException) SaleOrderPurchaseService(com.axelor.apps.supplychain.service.SaleOrderPurchaseService) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) Partner(com.axelor.apps.base.db.Partner)

Example 52 with PurchaseOrder

use of com.axelor.apps.purchase.db.PurchaseOrder in project axelor-open-suite by axelor.

the class TimetableController method generateInvoice.

public void generateInvoice(ActionRequest request, ActionResponse response) throws AxelorException {
    Timetable timetable = request.getContext().asType(Timetable.class);
    timetable = Beans.get(TimetableRepository.class).find(timetable.getId());
    Context parentContext = request.getContext().getParent();
    if (parentContext != null && parentContext.getContextClass().equals(SaleOrder.class)) {
        SaleOrder saleOrder = parentContext.asType(SaleOrder.class);
        if (saleOrder.getStatusSelect() < SaleOrderRepository.STATUS_ORDER_CONFIRMED) {
            response.setAlert(I18n.get(IExceptionMessage.TIMETABLE_SALE_ORDER_NOT_CONFIRMED));
            return;
        }
    }
    if (parentContext != null && parentContext.getContextClass().equals(PurchaseOrder.class)) {
        PurchaseOrder purchaseOrder = parentContext.asType(PurchaseOrder.class);
        if (purchaseOrder.getStatusSelect() < PurchaseOrderRepository.STATUS_VALIDATED) {
            response.setAlert(I18n.get(IExceptionMessage.TIMETABLE_PURCHASE_OREDR_NOT_VALIDATED));
            return;
        }
    }
    if (timetable.getInvoice() != null) {
        response.setAlert(I18n.get(IExceptionMessage.TIMETABLE_INVOICE_ALREADY_GENERATED));
        return;
    }
    Invoice invoice = timetableService.generateInvoice(timetable);
    response.setReload(true);
    response.setView(ActionView.define(I18n.get("Invoice generated")).model("com.axelor.apps.account.db.Invoice").add("form", "invoice-form").add("grid", "invoice-grid").param("search-filters", "customer-invoices-filters").param("forceEdit", "true").context("_showRecord", invoice.getId().toString()).map());
}
Also used : Context(com.axelor.rpc.Context) Timetable(com.axelor.apps.supplychain.db.Timetable) Invoice(com.axelor.apps.account.db.Invoice) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) SaleOrder(com.axelor.apps.sale.db.SaleOrder)

Example 53 with PurchaseOrder

use of com.axelor.apps.purchase.db.PurchaseOrder in project axelor-open-suite by axelor.

the class ImportSupplyChain method importPurchaseOrderFromSupplyChain.

@Transactional
public Object importPurchaseOrderFromSupplyChain(Object bean, Map<String, Object> values) {
    try {
        StockMoveService stockMoveService = Beans.get(StockMoveService.class);
        PurchaseOrder purchaseOrder = (PurchaseOrder) bean;
        int status = purchaseOrder.getStatusSelect();
        purchaseOrder = (PurchaseOrder) importPurchaseOrder.importPurchaseOrder(bean, values);
        for (PurchaseOrderLine line : purchaseOrder.getPurchaseOrderLineList()) {
            Product product = line.getProduct();
            if (product.getMassUnit() == null) {
                product.setMassUnit(stockConfigService.getStockConfig(purchaseOrder.getCompany()).getCustomsMassUnit());
            }
        }
        if (status == PurchaseOrderRepository.STATUS_VALIDATED || status == PurchaseOrderRepository.STATUS_FINISHED) {
            purchaseOrderWorkflowService.validatePurchaseOrder(purchaseOrder);
        }
        if (status == PurchaseOrderRepository.STATUS_FINISHED) {
            List<Long> idList = purchaseOrderStockServiceImpl.createStockMoveFromPurchaseOrder(purchaseOrder);
            for (Long id : idList) {
                StockMove stockMove = Beans.get(StockMoveRepository.class).find(id);
                stockMoveService.copyQtyToRealQty(stockMove);
                stockMoveService.realize(stockMove);
                stockMove.setRealDate(purchaseOrder.getDeliveryDate());
            }
            purchaseOrder.setValidationDate(purchaseOrder.getOrderDate());
            purchaseOrder.setValidatedByUser(AuthUtils.getUser());
            purchaseOrder.setSupplierPartner(purchaseOrderService.validateSupplier(purchaseOrder));
            Invoice invoice = Beans.get(PurchaseOrderInvoiceService.class).generateInvoice(purchaseOrder);
            String prefixSupplierSeq = "INV000";
            invoice.setSupplierInvoiceNb(prefixSupplierSeq + purchaseOrder.getImportId());
            invoice.setInternalReference(purchaseOrder.getInternalReference());
            LocalDate date;
            if (purchaseOrder.getValidationDate() != null) {
                date = purchaseOrder.getValidationDate();
            } else {
                date = Beans.get(AppBaseService.class).getTodayDate(purchaseOrder.getCompany());
            }
            invoice.setInvoiceDate(date);
            invoice.setOriginDate(date.minusDays(15));
            invoiceService.validateAndVentilate(invoice);
            purchaseOrderWorkflowService.finishPurchaseOrder(purchaseOrder);
        }
    } catch (Exception e) {
        TraceBackService.trace(e);
    }
    return null;
}
Also used : StockMoveService(com.axelor.apps.stock.service.StockMoveService) StockMove(com.axelor.apps.stock.db.StockMove) Invoice(com.axelor.apps.account.db.Invoice) Product(com.axelor.apps.base.db.Product) StockMoveRepository(com.axelor.apps.stock.db.repo.StockMoveRepository) LocalDate(java.time.LocalDate) AxelorException(com.axelor.exception.AxelorException) PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) ImportPurchaseOrder(com.axelor.apps.purchase.script.ImportPurchaseOrder) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) PurchaseOrderInvoiceService(com.axelor.apps.supplychain.service.PurchaseOrderInvoiceService) Transactional(com.google.inject.persist.Transactional)

Example 54 with PurchaseOrder

use of com.axelor.apps.purchase.db.PurchaseOrder in project axelor-open-suite by axelor.

the class PurchaseOrderController method updateCostPrice.

public void updateCostPrice(ActionRequest request, ActionResponse response) {
    try {
        PurchaseOrder purchaseOrder = request.getContext().asType(PurchaseOrder.class);
        Beans.get(PurchaseOrderService.class).updateCostPrice(Beans.get(PurchaseOrderRepository.class).find(purchaseOrder.getId()));
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : PurchaseOrderService(com.axelor.apps.purchase.service.PurchaseOrderService) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) AxelorException(com.axelor.exception.AxelorException)

Example 55 with PurchaseOrder

use of com.axelor.apps.purchase.db.PurchaseOrder in project axelor-open-suite by axelor.

the class PurchaseOrderController method showPurchaseOrder.

/**
 * Called from grid or form purchase order view, print selected purchase order.
 *
 * @param request
 * @param response
 * @return
 */
@SuppressWarnings("unchecked")
public void showPurchaseOrder(ActionRequest request, ActionResponse response) {
    Context context = request.getContext();
    String fileLink;
    String title;
    PurchaseOrderPrintService purchaseOrderPrintService = Beans.get(PurchaseOrderPrintService.class);
    try {
        if (!ObjectUtils.isEmpty(request.getContext().get("_ids"))) {
            List<Long> ids = Lists.transform((List) request.getContext().get("_ids"), new Function<Object, Long>() {

                @Nullable
                @Override
                public Long apply(@Nullable Object input) {
                    return Long.parseLong(input.toString());
                }
            });
            fileLink = purchaseOrderPrintService.printPurchaseOrders(ids);
            title = I18n.get("Purchase orders");
        } else if (context.get("id") != null) {
            PurchaseOrder purchaseOrder = Beans.get(PurchaseOrderRepository.class).find(Long.parseLong(context.get("id").toString()));
            title = purchaseOrderPrintService.getFileName(purchaseOrder);
            fileLink = purchaseOrderPrintService.printPurchaseOrder(purchaseOrder, ReportSettings.FORMAT_PDF);
            logger.debug("Printing " + title);
        } else {
            throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.NO_PURCHASE_ORDER_SELECTED_FOR_PRINTING));
        }
        response.setView(ActionView.define(title).add("html", fileLink).map());
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Context(com.axelor.rpc.Context) AxelorException(com.axelor.exception.AxelorException) PurchaseOrderPrintService(com.axelor.apps.purchase.service.print.PurchaseOrderPrintService) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) Nullable(javax.annotation.Nullable) AxelorException(com.axelor.exception.AxelorException)

Aggregations

PurchaseOrder (com.axelor.apps.purchase.db.PurchaseOrder)84 PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)26 AxelorException (com.axelor.exception.AxelorException)26 Transactional (com.google.inject.persist.Transactional)16 BigDecimal (java.math.BigDecimal)12 Company (com.axelor.apps.base.db.Company)11 Context (com.axelor.rpc.Context)10 Partner (com.axelor.apps.base.db.Partner)9 Product (com.axelor.apps.base.db.Product)9 ArrayList (java.util.ArrayList)9 Invoice (com.axelor.apps.account.db.Invoice)7 List (java.util.List)7 PurchaseOrderRepository (com.axelor.apps.purchase.db.repo.PurchaseOrderRepository)6 SaleOrder (com.axelor.apps.sale.db.SaleOrder)6 PurchaseOrderSupplychainService (com.axelor.apps.supplychain.service.PurchaseOrderSupplychainService)6 LocalDate (java.time.LocalDate)6 PurchaseOrderService (com.axelor.apps.purchase.service.PurchaseOrderService)5 StockMove (com.axelor.apps.stock.db.StockMove)5 Unit (com.axelor.apps.base.db.Unit)4 PurchaseOrderLineService (com.axelor.apps.purchase.service.PurchaseOrderLineService)4