Search in sources :

Example 16 with SaleOrderLine

use of com.axelor.apps.sale.db.SaleOrderLine in project axelor-open-suite by axelor.

the class IntercoServiceImpl method generateIntercoPurchaseFromSale.

@Override
@Transactional(rollbackOn = { Exception.class })
public PurchaseOrder generateIntercoPurchaseFromSale(SaleOrder saleOrder) throws AxelorException {
    PurchaseOrderService purchaseOrderService = Beans.get(PurchaseOrderService.class);
    Company intercoCompany = findIntercoCompany(saleOrder.getClientPartner());
    // create purchase order
    PurchaseOrder purchaseOrder = new PurchaseOrder();
    purchaseOrder.setCompany(intercoCompany);
    purchaseOrder.setContactPartner(saleOrder.getContactPartner());
    purchaseOrder.setCurrency(saleOrder.getCurrency());
    purchaseOrder.setDeliveryDate(saleOrder.getDeliveryDate());
    purchaseOrder.setOrderDate(saleOrder.getCreationDate());
    purchaseOrder.setPriceList(saleOrder.getPriceList());
    purchaseOrder.setTradingName(saleOrder.getTradingName());
    purchaseOrder.setPurchaseOrderLineList(new ArrayList<>());
    purchaseOrder.setPrintingSettings(Beans.get(TradingNameService.class).getDefaultPrintingSettings(null, intercoCompany));
    purchaseOrder.setStatusSelect(PurchaseOrderRepository.STATUS_DRAFT);
    purchaseOrder.setSupplierPartner(saleOrder.getCompany().getPartner());
    purchaseOrder.setTradingName(saleOrder.getTradingName());
    // in ati
    purchaseOrder.setInAti(saleOrder.getInAti());
    // copy payments
    PaymentMode intercoPaymentMode = Beans.get(PaymentModeService.class).reverseInOut(saleOrder.getPaymentMode());
    purchaseOrder.setPaymentMode(intercoPaymentMode);
    purchaseOrder.setPaymentCondition(saleOrder.getPaymentCondition());
    // copy delivery info
    purchaseOrder.setDeliveryDate(saleOrder.getDeliveryDate());
    purchaseOrder.setStockLocation(Beans.get(StockLocationService.class).getDefaultReceiptStockLocation(intercoCompany));
    purchaseOrder.setShipmentMode(saleOrder.getShipmentMode());
    purchaseOrder.setFreightCarrierMode(saleOrder.getFreightCarrierMode());
    // copy timetable info
    purchaseOrder.setExpectedRealisationDate(saleOrder.getExpectedRealisationDate());
    purchaseOrder.setAmountToBeSpreadOverTheTimetable(saleOrder.getAmountToBeSpreadOverTheTimetable());
    // create lines
    List<SaleOrderLine> saleOrderLineList = saleOrder.getSaleOrderLineList();
    if (saleOrderLineList != null) {
        for (SaleOrderLine saleOrderLine : saleOrderLineList) {
            this.createIntercoPurchaseLineFromSaleLine(saleOrderLine, purchaseOrder);
        }
    }
    purchaseOrder.setPrintingSettings(intercoCompany.getPrintingSettings());
    // compute the purchase order
    purchaseOrderService.computePurchaseOrder(purchaseOrder);
    purchaseOrder.setCreatedByInterco(true);
    Beans.get(PurchaseOrderRepository.class).save(purchaseOrder);
    if (Beans.get(AppSupplychainService.class).getAppSupplychain().getIntercoPurchaseOrderCreateRequested()) {
        Beans.get(PurchaseOrderService.class).requestPurchaseOrder(purchaseOrder);
    }
    saleOrder.setExternalReference(purchaseOrder.getPurchaseOrderSeq());
    purchaseOrder.setExternalReference(saleOrder.getSaleOrderSeq());
    return purchaseOrder;
}
Also used : Company(com.axelor.apps.base.db.Company) PaymentModeService(com.axelor.apps.account.service.payment.PaymentModeService) PurchaseOrderService(com.axelor.apps.purchase.service.PurchaseOrderService) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) PurchaseOrderRepository(com.axelor.apps.purchase.db.repo.PurchaseOrderRepository) PaymentMode(com.axelor.apps.account.db.PaymentMode) Transactional(com.google.inject.persist.Transactional)

Example 17 with SaleOrderLine

use of com.axelor.apps.sale.db.SaleOrderLine in project axelor-open-suite by axelor.

the class AccountingCutOffServiceImpl method generateProductMoveLine.

protected MoveLine generateProductMoveLine(Move move, StockMoveLine stockMoveLine, String origin, boolean isPurchase, boolean recoveredTax, boolean ati, String moveDescription, boolean isReverse, LocalDate originDate) throws AxelorException {
    SaleOrderLine saleOrderLine = stockMoveLine.getSaleOrderLine();
    PurchaseOrderLine purchaseOrderLine = stockMoveLine.getPurchaseOrderLine();
    Company company = move.getCompany();
    LocalDate moveDate = move.getDate();
    Partner partner = move.getPartner();
    boolean isFixedAssets = false;
    BigDecimal amountInCurrency = null;
    BigDecimal totalQty = null;
    BigDecimal notInvoicedQty = null;
    if (isPurchase && purchaseOrderLine != null) {
        totalQty = purchaseOrderLine.getQty();
        notInvoicedQty = unitConversionService.convert(stockMoveLine.getUnit(), purchaseOrderLine.getUnit(), stockMoveLine.getRealQty().subtract(stockMoveLine.getQtyInvoiced()), stockMoveLine.getRealQty().scale(), purchaseOrderLine.getProduct());
        isFixedAssets = purchaseOrderLine.getFixedAssets();
        if (ati && !recoveredTax) {
            amountInCurrency = purchaseOrderLine.getInTaxTotal();
        } else {
            amountInCurrency = purchaseOrderLine.getExTaxTotal();
        }
    }
    if (!isPurchase && saleOrderLine != null) {
        totalQty = saleOrderLine.getQty();
        notInvoicedQty = unitConversionService.convert(stockMoveLine.getUnit(), saleOrderLine.getUnit(), stockMoveLine.getRealQty().subtract(stockMoveLine.getQtyInvoiced()), stockMoveLine.getRealQty().scale(), saleOrderLine.getProduct());
        if (ati) {
            amountInCurrency = saleOrderLine.getInTaxTotal();
        } else {
            amountInCurrency = saleOrderLine.getExTaxTotal();
        }
    }
    if (totalQty == null || BigDecimal.ZERO.compareTo(totalQty) == 0) {
        return null;
    }
    BigDecimal qtyRate = notInvoicedQty.divide(totalQty, 10, RoundingMode.HALF_UP);
    amountInCurrency = amountInCurrency.multiply(qtyRate).setScale(2, RoundingMode.HALF_UP);
    if (amountInCurrency == null || amountInCurrency.compareTo(BigDecimal.ZERO) == 0) {
        return null;
    }
    Product product = stockMoveLine.getProduct();
    Account account = accountManagementAccountService.getProductAccount(product, company, partner.getFiscalPosition(), isPurchase, isFixedAssets);
    boolean isDebit = false;
    if ((isPurchase && amountInCurrency.compareTo(BigDecimal.ZERO) == 1) || !isPurchase && amountInCurrency.compareTo(BigDecimal.ZERO) == -1) {
        isDebit = true;
    }
    if (isReverse) {
        isDebit = !isDebit;
    }
    MoveLine moveLine = moveLineService.createMoveLine(move, partner, account, amountInCurrency, isDebit, originDate, ++counter, origin, moveDescription);
    moveLine.setDate(moveDate);
    moveLine.setDueDate(moveDate);
    getAndComputeAnalyticDistribution(product, move, moveLine);
    move.addMoveLineListItem(moveLine);
    if (recoveredTax) {
        TaxLine taxLine = accountManagementAccountService.getTaxLine(originDate, product, company, partner.getFiscalPosition(), isPurchase);
        if (taxLine != null) {
            moveLine.setTaxLine(taxLine);
            moveLine.setTaxRate(taxLine.getValue());
            moveLine.setTaxCode(taxLine.getTax().getCode());
            if (taxLine.getValue().compareTo(BigDecimal.ZERO) != 0) {
                generateTaxMoveLine(move, moveLine, origin, isPurchase, isFixedAssets, moveDescription);
            }
        }
    }
    return moveLine;
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) Account(com.axelor.apps.account.db.Account) Company(com.axelor.apps.base.db.Company) MoveLine(com.axelor.apps.account.db.MoveLine) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) Product(com.axelor.apps.base.db.Product) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) LocalDate(java.time.LocalDate) Partner(com.axelor.apps.base.db.Partner) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine)

Example 18 with SaleOrderLine

use of com.axelor.apps.sale.db.SaleOrderLine in project axelor-open-suite by axelor.

the class ProductStockLocationServiceImpl method getSaleOrderQty.

protected BigDecimal getSaleOrderQty(Product product, Company company, StockLocation stockLocation) throws AxelorException {
    if (product == null || product.getUnit() == null) {
        return BigDecimal.ZERO;
    }
    Long companyId = 0L;
    Long stockLocationId = 0L;
    if (company != null) {
        companyId = company.getId();
    }
    if (stockLocation != null) {
        stockLocationId = stockLocation.getId();
    }
    String query = Beans.get(SaleOrderLineServiceSupplyChain.class).getSaleOrderLineListForAProduct(product.getId(), companyId, stockLocationId);
    List<SaleOrderLine> saleOrderLineList = Beans.get(SaleOrderLineRepository.class).all().filter(query).fetch();
    // Compute
    BigDecimal sumSaleOrderQty = BigDecimal.ZERO;
    if (!saleOrderLineList.isEmpty()) {
        Unit unitConversion = product.getUnit();
        for (SaleOrderLine saleOrderLine : saleOrderLineList) {
            BigDecimal productSaleOrderQty = saleOrderLine.getQty();
            if (saleOrderLine.getDeliveryState() == SaleOrderLineRepository.DELIVERY_STATE_PARTIALLY_DELIVERED) {
                productSaleOrderQty = productSaleOrderQty.subtract(saleOrderLine.getDeliveredQty());
            }
            productSaleOrderQty = unitConversionService.convert(saleOrderLine.getUnit(), unitConversion, productSaleOrderQty, productSaleOrderQty.scale(), product);
            sumSaleOrderQty = sumSaleOrderQty.add(productSaleOrderQty);
        }
    }
    return sumSaleOrderQty;
}
Also used : SaleOrderLineRepository(com.axelor.apps.sale.db.repo.SaleOrderLineRepository) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 19 with SaleOrderLine

use of com.axelor.apps.sale.db.SaleOrderLine in project axelor-open-suite by axelor.

the class SaleOrderLineProjectController method updateToInvoice.

/**
 * Invert value of 'toInvoice' field and save the record
 *
 * @param request
 * @param response
 */
@Transactional
public void updateToInvoice(ActionRequest request, ActionResponse response) {
    SaleOrderLineRepository saleOrderLineRepository = Beans.get(SaleOrderLineRepository.class);
    try {
        SaleOrderLine saleOrderLine = request.getContext().asType(SaleOrderLine.class);
        saleOrderLine = saleOrderLineRepository.find(saleOrderLine.getId());
        saleOrderLine.setToInvoice(!saleOrderLine.getToInvoice());
        saleOrderLineRepository.save(saleOrderLine);
        response.setValue("toInvoice", saleOrderLine.getToInvoice());
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : SaleOrderLineRepository(com.axelor.apps.sale.db.repo.SaleOrderLineRepository) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) AxelorException(com.axelor.exception.AxelorException) Transactional(com.google.inject.persist.Transactional)

Example 20 with SaleOrderLine

use of com.axelor.apps.sale.db.SaleOrderLine in project axelor-open-suite by axelor.

the class SaleOrderLineProjectController method updateAnalyticDistributionWithProject.

public void updateAnalyticDistributionWithProject(ActionRequest request, ActionResponse response) throws AxelorException {
    try {
        SaleOrderLine saleOrderLine = request.getContext().asType(SaleOrderLine.class);
        if (saleOrderLine.getAnalyticMoveLineList() == null) {
            return;
        }
        saleOrderLine = Beans.get(SaleOrderLineProjectService.class).updateAnalyticDistributionWithProject(saleOrderLine);
        response.setValue("analyticMoveLineList", saleOrderLine.getAnalyticMoveLineList());
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) AxelorException(com.axelor.exception.AxelorException)

Aggregations

SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)116 AxelorException (com.axelor.exception.AxelorException)41 BigDecimal (java.math.BigDecimal)39 SaleOrder (com.axelor.apps.sale.db.SaleOrder)33 ArrayList (java.util.ArrayList)24 Transactional (com.google.inject.persist.Transactional)23 Product (com.axelor.apps.base.db.Product)21 Context (com.axelor.rpc.Context)16 SaleOrderLineService (com.axelor.apps.sale.service.saleorder.SaleOrderLineService)14 PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)9 Partner (com.axelor.apps.base.db.Partner)8 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)8 TaxLine (com.axelor.apps.account.db.TaxLine)7 Unit (com.axelor.apps.base.db.Unit)7 HashMap (java.util.HashMap)7 AnalyticMoveLine (com.axelor.apps.account.db.AnalyticMoveLine)6 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)6 SaleOrderLineRepository (com.axelor.apps.sale.db.repo.SaleOrderLineRepository)6 ReservedQtyService (com.axelor.apps.supplychain.service.ReservedQtyService)6 List (java.util.List)6