Search in sources :

Example 1 with WeightedAveragePriceService

use of com.axelor.apps.stock.service.WeightedAveragePriceService in project axelor-open-suite by axelor.

the class ProductStockRepository method save.

public Product save(Product product) {
    WeightedAveragePriceService weightedAveragePriceService = Beans.get(WeightedAveragePriceService.class);
    Set<MetaField> specificProductFieldSet = appBaseService.getAppBase().getCompanySpecificProductFieldsSet();
    if (!specificProductFieldSet.isEmpty() && appBaseService.getAppBase().getEnableMultiCompany()) {
        ArrayList<Company> productCompanyList = new ArrayList<>();
        if (product.getProductCompanyList() != null) {
            for (ProductCompany productCompany : product.getProductCompanyList()) {
                productCompanyList.add(productCompany.getCompany());
            }
        }
        Mapper mapper = Mapper.of(Product.class);
        List<StockConfig> stockConfigList = Beans.get(StockConfigRepository.class).all().fetch();
        for (StockConfig stockConfig : stockConfigList) {
            if (stockConfig.getCompany() != null && !productCompanyList.contains(stockConfig.getCompany()) && stockConfig.getReceiptDefaultStockLocation() != null && (stockConfig.getCompany().getArchived() == null || !stockConfig.getCompany().getArchived())) {
                ProductCompany productCompany = new ProductCompany();
                for (MetaField specificField : specificProductFieldSet) {
                    mapper.set(productCompany, specificField.getName(), mapper.get(product, specificField.getName()));
                }
                // specific case for avgPrice per company
                productCompany.setAvgPrice(weightedAveragePriceService.computeAvgPriceForCompany(product, stockConfig.getCompany()));
                productCompany.setCompany(stockConfig.getCompany());
                productCompany.setProduct(product);
                product.addProductCompanyListItem(productCompany);
            }
        }
    }
    return super.save(product);
}
Also used : Mapper(com.axelor.db.mapper.Mapper) Company(com.axelor.apps.base.db.Company) ProductCompany(com.axelor.apps.base.db.ProductCompany) StockConfig(com.axelor.apps.stock.db.StockConfig) MetaField(com.axelor.meta.db.MetaField) WeightedAveragePriceService(com.axelor.apps.stock.service.WeightedAveragePriceService) ArrayList(java.util.ArrayList) ProductCompany(com.axelor.apps.base.db.ProductCompany)

Aggregations

Company (com.axelor.apps.base.db.Company)1 ProductCompany (com.axelor.apps.base.db.ProductCompany)1 StockConfig (com.axelor.apps.stock.db.StockConfig)1 WeightedAveragePriceService (com.axelor.apps.stock.service.WeightedAveragePriceService)1 Mapper (com.axelor.db.mapper.Mapper)1 MetaField (com.axelor.meta.db.MetaField)1 ArrayList (java.util.ArrayList)1