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