use of com.axelor.apps.sale.db.SaleOrderLine in project axelor-open-suite by axelor.
the class SaleOrderMarginServiceImpl method computeMarginSaleOrder.
@Override
public void computeMarginSaleOrder(SaleOrder saleOrder) {
BigDecimal accountedRevenue = BigDecimal.ZERO;
BigDecimal totalCostPrice = BigDecimal.ZERO;
BigDecimal totalGrossProfit = BigDecimal.ZERO;
BigDecimal marginRate = BigDecimal.ZERO;
BigDecimal markup = BigDecimal.ZERO;
if (saleOrder.getSaleOrderLineList() != null && !saleOrder.getSaleOrderLineList().isEmpty()) {
for (SaleOrderLine saleOrderLineList : saleOrder.getSaleOrderLineList()) {
if (saleOrderLineList.getProduct() == null || saleOrderLineList.getSubTotalCostPrice().compareTo(BigDecimal.ZERO) == 0 || saleOrderLineList.getExTaxTotal().compareTo(BigDecimal.ZERO) == 0) {
} else {
accountedRevenue = accountedRevenue.add(saleOrderLineList.getCompanyExTaxTotal());
totalCostPrice = totalCostPrice.add(saleOrderLineList.getSubTotalCostPrice());
totalGrossProfit = totalGrossProfit.add(saleOrderLineList.getSubTotalGrossMargin());
marginRate = totalGrossProfit.multiply(new BigDecimal(100)).divide(accountedRevenue, 2, RoundingMode.HALF_UP);
markup = totalGrossProfit.multiply(new BigDecimal(100)).divide(totalCostPrice, 2, RoundingMode.HALF_UP);
}
}
}
saleOrder.setAccountedRevenue(accountedRevenue);
saleOrder.setTotalCostPrice(totalCostPrice);
saleOrder.setTotalGrossMargin(totalGrossProfit);
saleOrder.setMarginRate(marginRate);
saleOrder.setMarkup(markup);
}
use of com.axelor.apps.sale.db.SaleOrderLine in project axelor-open-suite by axelor.
the class SaleOrderServiceImpl method handleComplementaryProducts.
@Override
public List<SaleOrderLine> handleComplementaryProducts(SaleOrder saleOrder) throws AxelorException {
List<SaleOrderLine> saleOrderLineList = saleOrder.getSaleOrderLineList();
if (saleOrderLineList == null) {
saleOrderLineList = new ArrayList<>();
}
SaleOrderLine originSoLine = null;
for (SaleOrderLine soLine : saleOrderLineList) {
if (soLine.getIsComplementaryProductsUnhandledYet()) {
originSoLine = soLine;
if (originSoLine.getManualId() == null || originSoLine.getManualId().equals("")) {
this.setNewManualId(originSoLine);
}
break;
}
}
if (originSoLine != null && originSoLine.getProduct() != null && originSoLine.getSelectedComplementaryProductList() != null) {
SaleOrderLineService saleOrderLineService = Beans.get(SaleOrderLineService.class);
AppBaseService appBaseService = Beans.get(AppBaseService.class);
for (ComplementaryProductSelected compProductSelected : originSoLine.getSelectedComplementaryProductList()) {
// Search if there is already a line for this product to modify or remove
SaleOrderLine newSoLine = null;
for (SaleOrderLine soLine : saleOrderLineList) {
if (originSoLine.getManualId().equals(soLine.getParentId()) && soLine.getProduct() != null && soLine.getProduct().equals(compProductSelected.getProduct())) {
// exists and is no longer selected
if (compProductSelected.getIsSelected()) {
newSoLine = soLine;
} else {
saleOrderLineList.remove(soLine);
}
break;
}
}
if (newSoLine == null) {
if (compProductSelected.getIsSelected()) {
newSoLine = new SaleOrderLine();
newSoLine.setProduct(compProductSelected.getProduct());
newSoLine.setSaleOrder(saleOrder);
newSoLine.setQty(originSoLine.getQty().multiply(compProductSelected.getQty()).setScale(appBaseService.getNbDecimalDigitForQty(), RoundingMode.HALF_EVEN));
saleOrderLineService.computeProductInformation(newSoLine, newSoLine.getSaleOrder());
saleOrderLineService.computeValues(newSoLine.getSaleOrder(), newSoLine);
newSoLine.setParentId(originSoLine.getManualId());
int targetIndex = saleOrderLineList.indexOf(originSoLine) + 1;
saleOrderLineList.add(targetIndex, newSoLine);
}
} else {
newSoLine.setQty(originSoLine.getQty().multiply(compProductSelected.getQty()).setScale(appBaseService.getNbDecimalDigitForQty(), RoundingMode.HALF_EVEN));
saleOrderLineService.computeProductInformation(newSoLine, newSoLine.getSaleOrder());
saleOrderLineService.computeValues(newSoLine.getSaleOrder(), newSoLine);
}
}
originSoLine.setIsComplementaryProductsUnhandledYet(false);
}
for (int i = 0; i < saleOrderLineList.size(); i++) {
saleOrderLineList.get(i).setSequence(i);
}
return saleOrderLineList;
}
use of com.axelor.apps.sale.db.SaleOrderLine in project axelor-open-suite by axelor.
the class SaleOrderServiceImpl method addPack.
@Override
@Transactional
public SaleOrder addPack(SaleOrder saleOrder, Pack pack, BigDecimal packQty) {
List<PackLine> packLineList = pack.getComponents();
if (ObjectUtils.isEmpty(packLineList)) {
return saleOrder;
}
packLineList.sort(Comparator.comparing(PackLine::getSequence));
Integer sequence = -1;
List<SaleOrderLine> soLines = saleOrder.getSaleOrderLineList();
if (soLines != null && !soLines.isEmpty()) {
sequence = soLines.stream().mapToInt(SaleOrderLine::getSequence).max().getAsInt();
}
BigDecimal conversionRate = new BigDecimal(1.00);
if (pack.getCurrency() != null && !pack.getCurrency().getCode().equals(saleOrder.getCurrency().getCode())) {
try {
conversionRate = Beans.get(CurrencyConversionFactory.class).getCurrencyConversionService().convert(pack.getCurrency(), saleOrder.getCurrency());
} catch (MalformedURLException | JSONException | AxelorException e) {
TraceBackService.trace(e);
}
}
if (Boolean.FALSE.equals(pack.getDoNotDisplayHeaderAndEndPack())) {
if (saleOrderLineService.getPackLineTypes(packLineList) == null || !saleOrderLineService.getPackLineTypes(packLineList).contains(PackLineRepository.TYPE_START_OF_PACK)) {
sequence++;
}
soLines = saleOrderLineService.createNonStandardSOLineFromPack(pack, saleOrder, packQty, soLines, sequence);
}
SaleOrderLine soLine;
for (PackLine packLine : packLineList) {
if (packLine.getTypeSelect() != PackLineRepository.TYPE_NORMAL && Boolean.TRUE.equals(pack.getDoNotDisplayHeaderAndEndPack())) {
continue;
}
soLine = saleOrderLineService.createSaleOrderLine(packLine, saleOrder, packQty, conversionRate, ++sequence);
if (soLine != null) {
soLine.setSaleOrder(saleOrder);
soLines.add(soLine);
}
}
if (soLines != null && !soLines.isEmpty()) {
try {
saleOrder = Beans.get(SaleOrderComputeService.class).computeSaleOrder(saleOrder);
Beans.get(SaleOrderMarginService.class).computeMarginSaleOrder(saleOrder);
} catch (AxelorException e) {
TraceBackService.trace(e);
}
Beans.get(SaleOrderRepository.class).save(saleOrder);
}
return saleOrder;
}
use of com.axelor.apps.sale.db.SaleOrderLine in project axelor-open-suite by axelor.
the class SaleOrderServiceImpl method manageComplementaryProductSOLines.
@Override
public void manageComplementaryProductSOLines(SaleOrder saleOrder) throws AxelorException {
if (saleOrder.getClientPartner() == null || CollectionUtils.isEmpty(saleOrder.getSaleOrderLineList()) || CollectionUtils.isEmpty(saleOrder.getClientPartner().getComplementaryProductList())) {
return;
}
List<SaleOrderLine> newComplementarySOLines = new ArrayList<>();
List<ComplementaryProduct> complementaryProducts = saleOrder.getClientPartner().getComplementaryProductList();
for (SaleOrderLine saleOrderLine : saleOrder.getSaleOrderLineList()) {
if (saleOrderLine.getMainSaleOrderLine() != null) {
continue;
}
newComplementarySOLines.addAll(saleOrderLineService.manageComplementaryProductSaleOrderLine(saleOrderLine, saleOrder, complementaryProducts));
}
newComplementarySOLines.stream().forEach(line -> saleOrder.addSaleOrderLineListItem(line));
Beans.get(SaleOrderComputeService.class).computeSaleOrder(saleOrder);
}
use of com.axelor.apps.sale.db.SaleOrderLine in project axelor-open-suite by axelor.
the class SaleOrderComputeServiceImpl method resetPackTotal.
@Override
public void resetPackTotal(SaleOrder saleOrder) {
List<SaleOrderLine> saleOrderLineList = saleOrder.getSaleOrderLineList();
if (ObjectUtils.isEmpty(saleOrderLineList)) {
return;
}
for (SaleOrderLine saleOrderLine : saleOrderLineList) {
if (saleOrderLine.getTypeSelect() == SaleOrderLineRepository.TYPE_END_OF_PACK) {
saleOrderLine.setIsHideUnitAmounts(Boolean.FALSE);
saleOrderLine.setIsShowTotal(Boolean.FALSE);
saleOrderLine.setExTaxTotal(BigDecimal.ZERO);
saleOrderLine.setInTaxTotal(BigDecimal.ZERO);
}
}
saleOrder.setSaleOrderLineList(saleOrderLineList);
}
Aggregations