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);
}
Aggregations