Search in sources :

Example 1 with ProductCompany

use of com.axelor.apps.base.db.ProductCompany 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)

Example 2 with ProductCompany

use of com.axelor.apps.base.db.ProductCompany in project axelor-open-suite by axelor.

the class WeightedAveragePriceServiceImpl method computeAvgPriceForProduct.

@Override
@Transactional
public void computeAvgPriceForProduct(Product product) throws AxelorException {
    Boolean avgPriceHandledByCompany = false;
    Set<MetaField> companySpecificFields = appBaseService.getAppBase().getCompanySpecificProductFieldsSet();
    for (MetaField field : companySpecificFields) {
        if (field.getName().equals("avgPrice")) {
            avgPriceHandledByCompany = true;
            break;
        }
    }
    if (avgPriceHandledByCompany && product.getProductCompanyList() != null && !product.getProductCompanyList().isEmpty()) {
        for (ProductCompany productCompany : product.getProductCompanyList()) {
            Company company = productCompany.getCompany();
            BigDecimal productAvgPrice = this.computeAvgPriceForCompany(product, company);
            if (productAvgPrice.compareTo(BigDecimal.ZERO) == 0) {
                continue;
            }
            productCompanyService.set(product, "avgPrice", productAvgPrice, company);
            if ((Integer) productCompanyService.get(product, "costTypeSelect", company) == ProductRepository.COST_TYPE_AVERAGE_PRICE) {
                productCompanyService.set(product, "costPrice", productAvgPrice, company);
                if ((Boolean) productCompanyService.get(product, "autoUpdateSalePrice", company)) {
                    Beans.get(ProductService.class).updateSalePrice(product, company);
                }
            }
        }
    } else {
        BigDecimal productAvgPrice = this.computeAvgPriceForCompany(product, null);
        if (productAvgPrice.compareTo(BigDecimal.ZERO) == 0) {
            return;
        }
        product.setAvgPrice(productAvgPrice);
        if (product.getCostTypeSelect() == ProductRepository.COST_TYPE_AVERAGE_PRICE) {
            product.setCostPrice(productAvgPrice);
            if (product.getAutoUpdateSalePrice()) {
                Beans.get(ProductService.class).updateSalePrice(product, null);
            }
        }
    }
    productRepo.save(product);
}
Also used : Company(com.axelor.apps.base.db.Company) ProductCompany(com.axelor.apps.base.db.ProductCompany) MetaField(com.axelor.meta.db.MetaField) ProductService(com.axelor.apps.base.service.ProductService) ProductCompany(com.axelor.apps.base.db.ProductCompany) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Aggregations

Company (com.axelor.apps.base.db.Company)2 ProductCompany (com.axelor.apps.base.db.ProductCompany)2 MetaField (com.axelor.meta.db.MetaField)2 ProductService (com.axelor.apps.base.service.ProductService)1 StockConfig (com.axelor.apps.stock.db.StockConfig)1 WeightedAveragePriceService (com.axelor.apps.stock.service.WeightedAveragePriceService)1 Mapper (com.axelor.db.mapper.Mapper)1 Transactional (com.google.inject.persist.Transactional)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1