Skip to content

81057 - Shipment : Create a shipment line when we select a freight ca… #14355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import com.axelor.apps.sale.service.saleorderline.tax.SaleOrderLineTaxService;
import com.axelor.apps.supplychain.service.AnalyticLineModelService;
import com.axelor.apps.supplychain.service.app.AppSupplychainService;
import com.axelor.apps.supplychain.service.pricing.FreightCarrierApplyPricingService;
import com.axelor.apps.supplychain.service.pricing.FreightCarrierPricingService;
import com.axelor.apps.supplychain.service.saleorderline.SaleOrderLineAnalyticService;
import com.axelor.apps.supplychain.service.saleorderline.SaleOrderLineProductSupplychainServiceImpl;
import com.axelor.studio.db.AppProduction;
Expand Down Expand Up @@ -81,6 +83,8 @@ public SaleOrderLineProductProductionServiceImpl(
AnalyticLineModelService analyticLineModelService,
AppSupplychainService appSupplychainService,
SaleOrderLineAnalyticService saleOrderLineAnalyticService,
FreightCarrierPricingService freightCarrierPricingService,
FreightCarrierApplyPricingService freightCarrierApplyPricingService,
AppProductionService appProductionService,
SaleOrderLineBomService saleOrderLineBomService,
SaleOrderLineDetailsBomService saleOrderLineDetailsBomService,
Expand All @@ -103,7 +107,9 @@ public SaleOrderLineProductProductionServiceImpl(
blockingService,
analyticLineModelService,
appSupplychainService,
saleOrderLineAnalyticService);
saleOrderLineAnalyticService,
freightCarrierPricingService,
freightCarrierApplyPricingService);
this.appProductionService = appProductionService;
this.saleOrderLineBomService = saleOrderLineBomService;
this.saleOrderLineDetailsBomService = saleOrderLineDetailsBomService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ protected void addLineAndComputeOrder(ShippableOrder shippableOrder, Product shi
purchaseOrderService.computePurchaseOrder(purchaseOrder);
}

@Override
protected void updateLineAndComputeOrder(
ShippableOrder shippableOrder, Product shippingCostProduct) throws AxelorException {
PurchaseOrder purchaseOrder = getPurchaseOrder(shippableOrder);
if (purchaseOrder == null) {
return;
}

purchaseOrderService.computePurchaseOrder(purchaseOrder);
}

@Override
protected String removeLineAndComputeOrder(ShippableOrder shippableOrder) throws AxelorException {
PurchaseOrder purchaseOrder = getPurchaseOrder(shippableOrder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,11 @@ public String createShipmentCostLine(ShippableOrder shippableOrder, ShipmentMode
return null;
}

List<CustomerShippingCarriagePaid> customerShippingCarriagePaidList =
getShippingCarriagePaidList(shippableOrder);

CustomerShippingCarriagePaid supplierShippingCarriagePaid =
customerShippingCarriagePaidList.stream()
.filter(carriage -> carriage.getShipmentMode().equals(shipmentMode))
.findFirst()
.orElse(null);

getCustomerShippingCarriagePaid(shippableOrder, shipmentMode);
Product shippingCostProduct =
shippingService.getShippingCostProduct(shipmentMode, supplierShippingCarriagePaid);

if (shippingCostProduct == null) {
return null;
}
Expand All @@ -68,13 +62,25 @@ public String createShipmentCostLine(ShippableOrder shippableOrder, ShipmentMode
}

if (alreadyHasShippingCostLine(shippableOrder, shippingCostProduct)) {
updateLineAndComputeOrder(shippableOrder, shippingCostProduct);
return null;
}

addLineAndComputeOrder(shippableOrder, shippingCostProduct);
return null;
}

protected CustomerShippingCarriagePaid getCustomerShippingCarriagePaid(
ShippableOrder shippableOrder, ShipmentMode shipmentMode) {
List<CustomerShippingCarriagePaid> customerShippingCarriagePaidList =
getShippingCarriagePaidList(shippableOrder);

return customerShippingCarriagePaidList.stream()
.filter(carriage -> carriage.getShipmentMode().equals(shipmentMode))
.findFirst()
.orElse(null);
}

protected BigDecimal getCarriagePaidThreshold(
ShipmentMode shipmentMode, CustomerShippingCarriagePaid customerShippingCarriagePaid) {
BigDecimal carriagePaidThreshold = shipmentMode.getCarriagePaidThreshold();
Expand Down Expand Up @@ -168,4 +174,7 @@ protected abstract String removeLineAndComputeOrder(ShippableOrder shippableOrde

protected abstract void addLineAndComputeOrder(
ShippableOrder shippableOrder, Product shippingCostProduct) throws AxelorException;

protected abstract void updateLineAndComputeOrder(
ShippableOrder shippableOrder, Product shippingCostProduct) throws AxelorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
public interface FreightCarrierApplyPricingService {

void applyPricing(Set<FreightCarrierPricing> freightCarrierPricingSet) throws AxelorException;

String applyPricing(FreightCarrierPricing freightCarrierPricing);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public void applyPricing(Set<FreightCarrierPricing> freightCarrierPricingSet)
}
}

protected String applyPricing(FreightCarrierPricing freightCarrierPricing) {
@Override
public String applyPricing(FreightCarrierPricing freightCarrierPricing) {
String errors = "";
Pricing pricing =
Optional.ofNullable(freightCarrierPricing)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.axelor.apps.supplychain.service.pricing;

import com.axelor.apps.base.AxelorException;
import com.axelor.apps.sale.db.SaleOrder;
import com.axelor.apps.stock.db.FreightCarrierMode;
import com.axelor.apps.supplychain.db.FreightCarrierPricing;
import java.util.List;
import java.util.Set;

public interface FreightCarrierPricingService {

void computeFreightCarrierPricing(
String computeFreightCarrierPricing(
List<FreightCarrierPricing> freightCarrierPricingList, Long saleOrderId)
throws AxelorException;

Set<FreightCarrierPricing> getFreightCarrierPricingSet(Long shipmentModeId, Long saleOrderId);

FreightCarrierPricing createFreightCarrierPricing(
FreightCarrierMode freightCarrierMode, SaleOrder saleOrder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.axelor.apps.stock.db.repo.FreightCarrierModeRepository;
import com.axelor.apps.supplychain.db.FreightCarrierPricing;
import com.axelor.apps.supplychain.exception.SupplychainExceptionMessage;
import com.axelor.apps.supplychain.service.saleorder.SaleOrderShipmentService;
import com.axelor.i18n.I18n;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
Expand All @@ -21,20 +22,23 @@ public class FreightCarrierPricingServiceImpl implements FreightCarrierPricingSe
protected final SaleOrderRepository saleOrderRepository;
protected final FreightCarrierModeRepository freightCarrierModeRepository;
protected final PartnerRepository partnerRepository;
protected final SaleOrderShipmentService saleOrderShipmentService;

@Inject
public FreightCarrierPricingServiceImpl(
SaleOrderRepository saleOrderRepository,
FreightCarrierModeRepository freightCarrierModeRepository,
PartnerRepository partnerRepository) {
PartnerRepository partnerRepository,
SaleOrderShipmentService saleOrderShipmentService) {
this.saleOrderRepository = saleOrderRepository;
this.freightCarrierModeRepository = freightCarrierModeRepository;
this.partnerRepository = partnerRepository;
this.saleOrderShipmentService = saleOrderShipmentService;
}

@Override
@Transactional(rollbackOn = Exception.class)
public void computeFreightCarrierPricing(
public String computeFreightCarrierPricing(
List<FreightCarrierPricing> freightCarrierPricingList, Long saleOrderId)
throws AxelorException {
SaleOrder saleOrder = saleOrderRepository.find(saleOrderId);
Expand All @@ -46,8 +50,12 @@ public void computeFreightCarrierPricing(
saleOrder.setCarrierPartner(
partnerRepository.find(freightCarrierPricing.getCarrierPartner().getId()));

String message =
saleOrderShipmentService.createShipmentCostLine(saleOrder, saleOrder.getShipmentMode());
saleOrderRepository.save(saleOrder);
return message;
}
return null;
}

public Set<FreightCarrierPricing> getFreightCarrierPricingSet(
Expand All @@ -72,7 +80,8 @@ public Set<FreightCarrierPricing> getFreightCarrierPricingSet(
return freightCarrierPricings;
}

protected FreightCarrierPricing createFreightCarrierPricing(
@Override
public FreightCarrierPricing createFreightCarrierPricing(
FreightCarrierMode freightCarrierMode, SaleOrder saleOrder) {
FreightCarrierPricing freightCarrierPricing = new FreightCarrierPricing();
freightCarrierPricing.setFreightCarrierMode(freightCarrierMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ protected void addLineAndComputeOrder(ShippableOrder shippableOrder, Product shi
computeSaleOrder(saleOrder);
}

@Override
protected void updateLineAndComputeOrder(
ShippableOrder shippableOrder, Product shippingCostProduct) throws AxelorException {
SaleOrder saleOrder = getSaleOrder(shippableOrder);
if (saleOrder == null) {
return;
}
SaleOrderLine saleOrderLine = createShippingCostLine(saleOrder, shippingCostProduct);
saleOrder
.getSaleOrderLineList()
.forEach(
line -> {
if (line.getProduct() != null && line.getProduct().getIsShippingCostsProduct()) {
line.setPrice(saleOrderLine.getPrice());
line.setProduct(saleOrderLine.getProduct());
}
});
computeSaleOrder(saleOrder);
}

@Override
protected String removeLineAndComputeOrder(ShippableOrder shippableOrder) throws AxelorException {
SaleOrder saleOrder = getSaleOrder(shippableOrder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@
import com.axelor.apps.sale.service.saleorderline.product.SaleOrderLineComplementaryProductService;
import com.axelor.apps.sale.service.saleorderline.product.SaleOrderLineProductServiceImpl;
import com.axelor.apps.sale.service.saleorderline.tax.SaleOrderLineTaxService;
import com.axelor.apps.supplychain.db.FreightCarrierPricing;
import com.axelor.apps.supplychain.model.AnalyticLineModel;
import com.axelor.apps.supplychain.service.AnalyticLineModelService;
import com.axelor.apps.supplychain.service.app.AppSupplychainService;
import com.axelor.apps.supplychain.service.pricing.FreightCarrierApplyPricingService;
import com.axelor.apps.supplychain.service.pricing.FreightCarrierPricingService;
import com.google.inject.Inject;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -54,6 +57,8 @@ public class SaleOrderLineProductSupplychainServiceImpl extends SaleOrderLinePro
protected AnalyticLineModelService analyticLineModelService;
protected AppSupplychainService appSupplychainService;
protected SaleOrderLineAnalyticService saleOrderLineAnalyticService;
protected FreightCarrierPricingService freightCarrierPricingService;
protected FreightCarrierApplyPricingService freightCarrierApplyPricingService;

@Inject
public SaleOrderLineProductSupplychainServiceImpl(
Expand All @@ -72,7 +77,9 @@ public SaleOrderLineProductSupplychainServiceImpl(
BlockingService blockingService,
AnalyticLineModelService analyticLineModelService,
AppSupplychainService appSupplychainService,
SaleOrderLineAnalyticService saleOrderLineAnalyticService) {
SaleOrderLineAnalyticService saleOrderLineAnalyticService,
FreightCarrierPricingService freightCarrierPricingService,
FreightCarrierApplyPricingService freightCarrierApplyPricingService) {
super(
appSaleService,
appBaseService,
Expand All @@ -90,6 +97,8 @@ public SaleOrderLineProductSupplychainServiceImpl(
this.analyticLineModelService = analyticLineModelService;
this.appSupplychainService = appSupplychainService;
this.saleOrderLineAnalyticService = saleOrderLineAnalyticService;
this.freightCarrierPricingService = freightCarrierPricingService;
this.freightCarrierApplyPricingService = freightCarrierApplyPricingService;
}

@Override
Expand All @@ -112,6 +121,7 @@ public Map<String, Object> computeProductInformationSupplychain(

saleOrderLineMap.putAll(
saleOrderLineAnalyticService.printAnalyticAccounts(saleOrder, saleOrderLine));
saleOrderLineMap.putAll(setShippingCostPrice(saleOrderLine, saleOrder));
} else {
return saleOrderLineMap;
}
Expand Down Expand Up @@ -203,4 +213,24 @@ protected Map<String, Object> resetProductInformationMap(SaleOrderLine line) {

return saleOrderLineMap;
}

protected Map<String, Object> setShippingCostPrice(
SaleOrderLine saleOrderLine, SaleOrder saleOrder) {
Map<String, Object> saleOrderLineMap = new HashMap<>();

if (saleOrder.getFreightCarrierMode() == null
|| !saleOrderLine.getProduct().getIsShippingCostsProduct()) {
return saleOrderLineMap;
}

FreightCarrierPricing freightCarrierPricing =
freightCarrierPricingService.createFreightCarrierPricing(
saleOrder.getFreightCarrierMode(), saleOrder);
freightCarrierApplyPricingService.applyPricing(freightCarrierPricing);
saleOrderLine.setPrice(freightCarrierPricing.getPricingAmount());

saleOrderLineMap.put("price", saleOrderLine.getPrice());

return saleOrderLineMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ public void selectFreightCarrierPricing(ActionRequest request, ActionResponse re
.collect(Collectors.toList());

if (context.get("_id") != null) {
Beans.get(FreightCarrierPricingService.class)
.computeFreightCarrierPricing(
freightCarrierPricingList, Long.valueOf(context.get("_id").toString()));
String message =
Beans.get(FreightCarrierPricingService.class)
.computeFreightCarrierPricing(
freightCarrierPricingList, Long.valueOf(context.get("_id").toString()));
if (message != null) {
response.setInfo(message);
}
response.setCanClose(true);
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,21 @@ public void createShipmentCostLine(ActionRequest request, ActionResponse respons
}
}

public void updateShipmentCostLine(ActionRequest request, ActionResponse response) {
try {
SaleOrder saleOrder = request.getContext().asType(SaleOrder.class);
ShipmentMode shipmentMode = saleOrder.getShipmentMode();
String message =
Beans.get(SaleOrderShipmentService.class).createShipmentCostLine(saleOrder, shipmentMode);
if (message != null) {
response.setInfo(message);
}
response.setValues(saleOrder);
} catch (Exception e) {
TraceBackService.trace(response, e);
}
}

/**
* Called from sale order form view, on invoiced partner select. Call {@link
* PartnerLinkService#computePartnerFilter}
Expand Down
9 changes: 9 additions & 0 deletions axelor-supplychain/src/main/resources/views/SaleOrder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
showIf="!$readonly() &amp;&amp; shipmentMode"/>
</insert>
</extend>
<extend target="//field[@name='freightCarrierMode']">
<attribute name="onChange"
value="action-sale-order-record-freight-carrier,supplychain-action-update-shipment-cost-line"/>
</extend>
</form>

<form name="sale-order-form-partner" title="Sale order"
Expand Down Expand Up @@ -329,6 +333,11 @@
method="createShipmentCostLine"/>
</action-method>

<action-method name="supplychain-action-update-shipment-cost-line">
<call class="com.axelor.apps.supplychain.web.SaleOrderController"
method="updateShipmentCostLine"/>
</action-method>

<action-method
name="action-sale-order-supplychain-method-set-invoiced-partner-domain">
<call class="com.axelor.apps.supplychain.web.SaleOrderController"
Expand Down
Loading