Search in sources :

Example 41 with StockMove

use of com.axelor.apps.stock.db.StockMove in project axelor-open-suite by axelor.

the class SaleOrderStockServiceImpl method createStocksMovesFromSaleOrder.

@Override
@Transactional(rollbackOn = { Exception.class })
public List<Long> createStocksMovesFromSaleOrder(SaleOrder saleOrder) throws AxelorException {
    if (!this.isSaleOrderWithProductsToDeliver(saleOrder)) {
        return null;
    }
    if (saleOrder.getStockLocation() == null) {
        throw new AxelorException(saleOrder, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.SO_MISSING_STOCK_LOCATION), saleOrder.getSaleOrderSeq());
    }
    List<Long> stockMoveList = new ArrayList<>();
    Map<LocalDate, List<SaleOrderLine>> saleOrderLinePerDateMap = getAllSaleOrderLinePerDate(saleOrder);
    for (LocalDate estimatedDeliveryDate : saleOrderLinePerDateMap.keySet().stream().filter(x -> x != null).sorted((x, y) -> x.compareTo(y)).collect(Collectors.toList())) {
        List<SaleOrderLine> saleOrderLineList = saleOrderLinePerDateMap.get(estimatedDeliveryDate);
        Optional<StockMove> stockMove = createStockMove(saleOrder, estimatedDeliveryDate, saleOrderLineList);
        stockMove.map(StockMove::getId).ifPresent(stockMoveList::add);
    }
    Optional<List<SaleOrderLine>> saleOrderLineList = Optional.ofNullable(saleOrderLinePerDateMap.get(null));
    if (saleOrderLineList.isPresent()) {
        Optional<StockMove> stockMove = createStockMove(saleOrder, null, saleOrderLineList.get());
        stockMove.map(StockMove::getId).ifPresent(stockMoveList::add);
    }
    return stockMoveList;
}
Also used : Company(com.axelor.apps.base.db.Company) StockMoveLineRepository(com.axelor.apps.stock.db.repo.StockMoveLineRepository) IExceptionMessage(com.axelor.apps.supplychain.exception.IExceptionMessage) ProductRepository(com.axelor.apps.base.db.repo.ProductRepository) Inject(com.google.inject.Inject) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) StockMoveService(com.axelor.apps.stock.service.StockMoveService) ProductCompanyService(com.axelor.apps.base.service.ProductCompanyService) Transactional(com.google.inject.persist.Transactional) ArrayList(java.util.ArrayList) SaleOrderLineRepository(com.axelor.apps.sale.db.repo.SaleOrderLineRepository) BigDecimal(java.math.BigDecimal) SaleOrderRepository(com.axelor.apps.sale.db.repo.SaleOrderRepository) AxelorException(com.axelor.exception.AxelorException) TaxLine(com.axelor.apps.account.db.TaxLine) UnitConversionService(com.axelor.apps.base.service.UnitConversionService) StockLocation(com.axelor.apps.stock.db.StockLocation) Map(java.util.Map) I18n(com.axelor.i18n.I18n) SaleOrder(com.axelor.apps.sale.db.SaleOrder) StockMoveLineService(com.axelor.apps.stock.service.StockMoveLineService) PartnerStockSettings(com.axelor.apps.stock.db.PartnerStockSettings) RoundingMode(java.math.RoundingMode) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) StockMove(com.axelor.apps.stock.db.StockMove) StockMoveRepository(com.axelor.apps.stock.db.repo.StockMoveRepository) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) StockConfigService(com.axelor.apps.stock.service.config.StockConfigService) Collectors(java.util.stream.Collectors) SupplyChainConfig(com.axelor.apps.supplychain.db.SupplyChainConfig) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) List(java.util.List) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) Beans(com.axelor.inject.Beans) Product(com.axelor.apps.base.db.Product) Unit(com.axelor.apps.base.db.Unit) PartnerStockSettingsService(com.axelor.apps.stock.service.PartnerStockSettingsService) LocalDate(java.time.LocalDate) Optional(java.util.Optional) SupplyChainConfigRepository(com.axelor.apps.supplychain.db.repo.SupplyChainConfigRepository) AppSupplychainService(com.axelor.apps.supplychain.service.app.AppSupplychainService) Partner(com.axelor.apps.base.db.Partner) SupplyChainConfigService(com.axelor.apps.supplychain.service.config.SupplyChainConfigService) AxelorException(com.axelor.exception.AxelorException) StockMove(com.axelor.apps.stock.db.StockMove) ArrayList(java.util.ArrayList) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) LocalDate(java.time.LocalDate) ArrayList(java.util.ArrayList) List(java.util.List) Transactional(com.google.inject.persist.Transactional)

Example 42 with StockMove

use of com.axelor.apps.stock.db.StockMove in project axelor-open-suite by axelor.

the class SaleOrderWorkflowServiceSupplychainImpl method completeSaleOrder.

@Override
@Transactional(rollbackOn = { AxelorException.class, RuntimeException.class })
public void completeSaleOrder(SaleOrder saleOrder) throws AxelorException {
    if (!Beans.get(AppSupplychainService.class).isApp("supplychain")) {
        super.completeSaleOrder(saleOrder);
        return;
    }
    List<StockMove> stockMoves = Beans.get(StockMoveRepository.class).all().filter("self.originId = ? AND self.originTypeSelect = ?", saleOrder.getId(), "com.axelor.apps.sale.db.SaleOrder").fetch();
    if (!stockMoves.isEmpty()) {
        for (StockMove stockMove : stockMoves) {
            Integer statusSelect = stockMove.getStatusSelect();
            if (statusSelect == StockMoveRepository.STATUS_DRAFT || statusSelect == StockMoveRepository.STATUS_PLANNED) {
                throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.SALE_ORDER_COMPLETE_MANUALLY));
            }
        }
    }
    super.completeSaleOrder(saleOrder);
}
Also used : AxelorException(com.axelor.exception.AxelorException) StockMove(com.axelor.apps.stock.db.StockMove) StockMoveRepository(com.axelor.apps.stock.db.repo.StockMoveRepository) Transactional(com.google.inject.persist.Transactional)

Example 43 with StockMove

use of com.axelor.apps.stock.db.StockMove in project axelor-open-suite by axelor.

the class DeclarationOfExchangesExporterGoods method exportLineToCsv.

protected String[] exportLineToCsv(StockMoveLine stockMoveLine, int lineNum) throws AxelorException {
    String[] data = new String[columnHeadersList.size()];
    StockMove stockMove = stockMoveLine.getStockMove();
    String customsCode = stockMoveLine.getCustomsCode();
    Product product = stockMoveLine.getProduct();
    if (StringUtils.isBlank(customsCode)) {
        if (product == null) {
            customsCode = I18n.get("Product is missing.");
        }
        if (product != null && product.getCustomsCodeNomenclature() != null) {
            customsCode = product.getCustomsCodeNomenclature().getCode();
        }
        if (StringUtils.isBlank(customsCode)) {
            customsCode = String.format(I18n.get("Customs code nomenclature is missing on product %s."), product.getCode());
        }
    }
    BigDecimal fiscalValue = stockMoveLine.getCompanyUnitPriceUntaxed().multiply(stockMoveLine.getRealQty()).setScale(0, RoundingMode.HALF_UP);
    // Only positive fiscal value should be take into account
    if (fiscalValue.compareTo(BigDecimal.ZERO) <= 0) {
        return new String[0];
    }
    Regime regime = stockMoveLine.getRegime();
    if (regime == null) {
        if (stockMove.getTypeSelect() == StockMoveRepository.TYPE_OUTGOING) {
            regime = Regime.EXONERATED_SHIPMENT_AND_TRANSFER;
        } else if (stockMove.getTypeSelect() == StockMoveRepository.TYPE_INCOMING) {
            regime = Regime.INTRACOMMUNITY_ACQUISITION_TAXABLE_IN_FRANCE;
        }
    }
    BigDecimal totalNetMass = stockMoveLine.getTotalNetMass().setScale(0, RoundingMode.HALF_UP);
    BigInteger supplementaryUnit = stockMoveLine.getRealQty().setScale(0, RoundingMode.CEILING).toBigInteger();
    NatureOfTransaction natTrans = stockMoveLine.getNatureOfTransaction();
    if (natTrans == null) {
        natTrans = stockMove.getIsReversion() ? NatureOfTransaction.RETURN_OF_GOODS : NatureOfTransaction.FIRM_PURCHASE_OR_SALE;
    }
    ModeOfTransport modeOfTransport = stockMove.getModeOfTransport();
    if (modeOfTransport == null) {
        modeOfTransport = ModeOfTransport.CONSIGNMENTS_BY_POST;
    }
    String srcDstCountry;
    String dept;
    try {
        Address partnerAddress = stockMoveToolService.getPartnerAddress(stockMoveLine.getStockMove());
        srcDstCountry = partnerAddress.getAddressL7Country().getAlpha2Code();
    } catch (AxelorException e) {
        srcDstCountry = e.getMessage();
    }
    try {
        Address companyAddress = stockMoveToolService.getCompanyAddress(stockMoveLine.getStockMove());
        dept = companyAddress.getCity().getDepartment().getCode();
    } catch (AxelorException e) {
        dept = e.getMessage();
    }
    String countryOrigCode;
    if (stockMoveLine.getCountryOfOrigin() != null) {
        countryOrigCode = stockMoveLine.getCountryOfOrigin().getAlpha2Code();
    } else {
        if (stockMove.getTypeSelect() == StockMoveRepository.TYPE_INCOMING) {
            countryOrigCode = srcDstCountry;
        } else {
            countryOrigCode = "";
        }
    }
    String taxNbr;
    if (stockMove.getTypeSelect() == StockMoveRepository.TYPE_OUTGOING && stockMoveLine.getRegime() != Regime.OTHER_EXPEDITIONS) {
        if (stockMove.getPartner() == null) {
            taxNbr = String.format(I18n.get("Partner is missing on stock move %s."), stockMove.getName());
        } else if (StringUtils.isBlank(stockMove.getPartner().getTaxNbr())) {
            taxNbr = String.format(I18n.get("Tax number is missing on partner %s."), stockMove.getPartner().getName());
        } else {
            taxNbr = stockMove.getPartner().getTaxNbr();
        }
    } else {
        taxNbr = "";
    }
    String partnerSeq = "";
    if (stockMove.getPartner() != null) {
        partnerSeq = stockMove.getPartner().getPartnerSeq();
    }
    String productCode = "";
    String productName = "";
    if (product != null) {
        productCode = product.getCode();
        productName = product.getName();
    }
    String invoiceId = "";
    Set<Invoice> invoiceSet = stockMove.getInvoiceSet();
    if (invoiceSet != null) {
        for (Invoice invoice : invoiceSet) {
            if (invoice.getStatusSelect() == InvoiceRepository.STATUS_VENTILATED) {
                invoiceId += invoice.getInvoiceId() + "|";
            }
        }
        if (invoiceId != null && !invoiceId.isEmpty()) {
            invoiceId = invoiceId.substring(0, invoiceId.length() - 1);
        }
    }
    data[columnHeadersList.indexOf(LINE_NUM)] = String.valueOf(lineNum);
    data[columnHeadersList.indexOf(NOMENCLATURE)] = customsCode;
    data[columnHeadersList.indexOf(SRC_DST_COUNTRY)] = srcDstCountry;
    data[columnHeadersList.indexOf(FISC_VAL)] = String.valueOf(fiscalValue);
    data[columnHeadersList.indexOf(REGIME)] = String.valueOf(regime.getValue());
    data[columnHeadersList.indexOf(MASS)] = String.valueOf(totalNetMass);
    data[columnHeadersList.indexOf(UNITS)] = String.valueOf(supplementaryUnit);
    data[columnHeadersList.indexOf(NAT_TRANS)] = String.valueOf(natTrans.getValue());
    data[columnHeadersList.indexOf(TRANSP)] = String.valueOf(modeOfTransport.getValue());
    data[columnHeadersList.indexOf(DEPT)] = dept;
    data[columnHeadersList.indexOf(COUNTRY_ORIG)] = countryOrigCode;
    data[columnHeadersList.indexOf(ACQUIRER)] = taxNbr;
    data[columnHeadersList.indexOf(PRODUCT_CODE)] = productCode;
    data[columnHeadersList.indexOf(PRODUCT_NAME)] = productName;
    data[columnHeadersList.indexOf(PARTNER_SEQ)] = partnerSeq;
    data[columnHeadersList.indexOf(INVOICE)] = invoiceId;
    return data;
}
Also used : Regime(com.axelor.apps.stock.db.Regime) AxelorException(com.axelor.exception.AxelorException) StockMove(com.axelor.apps.stock.db.StockMove) Invoice(com.axelor.apps.account.db.Invoice) Address(com.axelor.apps.base.db.Address) Product(com.axelor.apps.base.db.Product) NatureOfTransaction(com.axelor.apps.stock.db.NatureOfTransaction) BigDecimal(java.math.BigDecimal) ModeOfTransport(com.axelor.apps.stock.db.ModeOfTransport) BigInteger(java.math.BigInteger)

Example 44 with StockMove

use of com.axelor.apps.stock.db.StockMove in project axelor-open-suite by axelor.

the class StockMoveMultiInvoiceServiceImpl method fillReferenceInvoiceFromMultiOutStockMove.

/**
 * Fill external and internal reference in the given invoice, from the list of stock moves.
 *
 * @param stockMoveList
 * @param dummyInvoice
 */
protected void fillReferenceInvoiceFromMultiOutStockMove(List<StockMove> stockMoveList, Invoice dummyInvoice) {
    // Concat sequence, internal ref and external ref from all saleOrder
    List<String> externalRefList = new ArrayList<>();
    List<String> internalRefList = new ArrayList<>();
    for (StockMove stockMove : stockMoveList) {
        SaleOrder saleOrder = StockMoveRepository.ORIGIN_SALE_ORDER.equals(stockMove.getOriginTypeSelect()) && stockMove.getOriginId() != null ? saleOrderRepository.find(stockMove.getOriginId()) : null;
        if (saleOrder != null) {
            externalRefList.add(saleOrder.getExternalReference());
        }
        internalRefList.add(stockMove.getStockMoveSeq() + (saleOrder != null ? (":" + saleOrder.getSaleOrderSeq()) : ""));
    }
    String externalRef = String.join("|", externalRefList);
    String internalRef = String.join("|", internalRefList);
    dummyInvoice.setExternalReference(StringTool.cutTooLongString(externalRef));
    dummyInvoice.setInternalReference(StringTool.cutTooLongString(internalRef));
}
Also used : StockMove(com.axelor.apps.stock.db.StockMove) ArrayList(java.util.ArrayList) SaleOrder(com.axelor.apps.sale.db.SaleOrder)

Example 45 with StockMove

use of com.axelor.apps.stock.db.StockMove in project axelor-open-suite by axelor.

the class StockMoveMultiInvoiceServiceImpl method fillReferenceInvoiceFromMultiInStockMove.

/**
 * Fill external and internal reference in the given invoice, from the list of stock moves.
 *
 * @param stockMoveList
 * @param dummyInvoice
 */
protected void fillReferenceInvoiceFromMultiInStockMove(List<StockMove> stockMoveList, Invoice dummyInvoice) {
    // Concat sequence, internal ref and external ref from all saleOrder
    List<String> externalRefList = new ArrayList<>();
    List<String> internalRefList = new ArrayList<>();
    for (StockMove stockMove : stockMoveList) {
        PurchaseOrder purchaseOrder = StockMoveRepository.ORIGIN_PURCHASE_ORDER.equals(stockMove.getOriginTypeSelect()) && stockMove.getOriginId() != null ? purchaseOrderRepository.find(stockMove.getOriginId()) : null;
        if (purchaseOrder != null) {
            externalRefList.add(purchaseOrder.getExternalReference());
        }
        internalRefList.add(stockMove.getStockMoveSeq() + (purchaseOrder != null ? (":" + purchaseOrder.getPurchaseOrderSeq()) : ""));
    }
    String externalRef = String.join("|", externalRefList);
    String internalRef = String.join("|", internalRefList);
    dummyInvoice.setExternalReference(StringTool.cutTooLongString(externalRef));
    dummyInvoice.setInternalReference(StringTool.cutTooLongString(internalRef));
}
Also used : StockMove(com.axelor.apps.stock.db.StockMove) ArrayList(java.util.ArrayList) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder)

Aggregations

StockMove (com.axelor.apps.stock.db.StockMove)129 AxelorException (com.axelor.exception.AxelorException)57 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)50 ArrayList (java.util.ArrayList)36 StockMoveRepository (com.axelor.apps.stock.db.repo.StockMoveRepository)33 Transactional (com.google.inject.persist.Transactional)32 StockMoveService (com.axelor.apps.stock.service.StockMoveService)31 BigDecimal (java.math.BigDecimal)30 List (java.util.List)25 Company (com.axelor.apps.base.db.Company)23 Map (java.util.Map)21 Product (com.axelor.apps.base.db.Product)19 Invoice (com.axelor.apps.account.db.Invoice)18 StockLocation (com.axelor.apps.stock.db.StockLocation)18 Beans (com.axelor.inject.Beans)17 Optional (java.util.Optional)16 StockMoveLineService (com.axelor.apps.stock.service.StockMoveLineService)15 I18n (com.axelor.i18n.I18n)15 Inject (com.google.inject.Inject)14 HashMap (java.util.HashMap)14