Skip to content

RM#93645 Fix timetable amount computation #14410

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 1 commit into from
May 28, 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 @@ -20,6 +20,7 @@

import com.axelor.apps.account.db.Invoice;
import com.axelor.apps.base.AxelorException;
import com.axelor.apps.base.db.Currency;
import com.axelor.apps.supplychain.db.Timetable;
import com.axelor.apps.supplychain.db.TimetableTemplate;
import com.google.inject.persist.Transactional;
Expand All @@ -39,4 +40,7 @@ public List<Timetable> applyTemplate(
throws AxelorException;

void deleteInvoiceTimeTable(Invoice invoice);

BigDecimal computeAmount(
Timetable timetable, List<Timetable> timetableList, BigDecimal exTaxTotal, Currency currency);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import com.axelor.apps.account.db.repo.InvoiceRepository;
import com.axelor.apps.account.service.PaymentConditionToolService;
import com.axelor.apps.base.AxelorException;
import com.axelor.apps.base.db.Currency;
import com.axelor.apps.base.db.repo.TraceBackRepository;
import com.axelor.apps.base.service.app.AppBaseService;
import com.axelor.apps.purchase.db.PurchaseOrder;
import com.axelor.apps.sale.db.SaleOrder;
import com.axelor.apps.sale.db.repo.SaleOrderRepository;
Expand All @@ -38,10 +40,12 @@
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;

public class TimetableServiceImpl implements TimetableService {

Expand Down Expand Up @@ -142,4 +146,37 @@ public void deleteInvoiceTimeTable(Invoice invoice) {
.setParameter("invoiceId", invoice.getId())
.executeUpdate();
}

@Override
public BigDecimal computeAmount(
Timetable timetable,
List<Timetable> timetableList,
BigDecimal exTaxTotal,
Currency currency) {
BigDecimal percentage = timetable.getPercentage();
BigDecimal result =
exTaxTotal
.multiply(percentage)
.divide(
BigDecimal.valueOf(100), AppBaseService.COMPUTATION_SCALING, RoundingMode.HALF_UP)
.setScale(currency.getNumberOfDecimals(), RoundingMode.HALF_UP);

if (CollectionUtils.isEmpty(timetableList)) {
return result;
}
List<Timetable> computeTimetableList = new ArrayList<>(timetableList);
computeTimetableList.add(timetable);
if (computeTimetableList.stream()
.map(Timetable::getPercentage)
.reduce(BigDecimal.ZERO, BigDecimal::add)
.compareTo(new BigDecimal(100))
== 0) {
return exTaxTotal.subtract(
timetableList.stream()
.map(Timetable::getAmount)
.reduce(BigDecimal.ZERO, BigDecimal::add));
} else {
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,32 @@ public void applyTemplate(ActionRequest request, ActionResponse response) {
TraceBackService.trace(response, e);
}
}

public void percentageOnChange(ActionRequest request, ActionResponse response) {
Context context = request.getContext();
Context parentContext = context.getParent();
Timetable timetable = context.asType(Timetable.class);
TimetableService timetableService = Beans.get(TimetableService.class);
if (parentContext != null && parentContext.getContextClass().equals(SaleOrder.class)) {
SaleOrder saleOrder = parentContext.asType(SaleOrder.class);
response.setValue(
"amount",
timetableService.computeAmount(
timetable,
saleOrder.getTimetableList(),
saleOrder.getExTaxTotal(),
saleOrder.getCurrency()));
}

if (parentContext != null && parentContext.getContextClass().equals(PurchaseOrder.class)) {
PurchaseOrder purchaseOrder = parentContext.asType(PurchaseOrder.class);
response.setValue(
"amount",
timetableService.computeAmount(
timetable,
purchaseOrder.getTimetableList(),
purchaseOrder.getExTaxTotal(),
purchaseOrder.getCurrency()));
}
}
}
20 changes: 9 additions & 11 deletions axelor-supplychain/src/main/resources/views/Timetable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
onChange="action-timetable-group-on-amount-change"
x-scale="saleOrder.currency.numberOfDecimals"/>
<field name="percentage" readonlyIf="invoiced" required="true"
onChange="action-timetable-record-on-percentage-change"/>
onChange="action-timetable-method-percentage-onchange"/>
<field name="comments" readonlyIf="invoiced" width="300%"/>
<field name="invoiced" readonly="true"/>
<button name="generateInvoiceBtn" readonlyIf="!id || invoiced" help="Generate Invoice"
Expand Down Expand Up @@ -40,7 +40,7 @@
onChange="action-timetable-group-on-amount-change"
x-scale="saleOrder.currency.numberOfDecimals"/>
<field name="percentage" colSpan="4" required="true"
onChange="action-timetable-record-on-percentage-change"/>
onChange="action-timetable-method-percentage-onchange"/>
<button name="generateInvoiceBtn" colOffset="9" colSpan="3" title="Generate Invoice"
onClick="save, action-timetable-method-generate-invoice"
if="__config__.app.isApp('invoice') &amp;&amp; __config__.app.getApp('supplychain')?.getAllowTimetableInvoicing()"
Expand All @@ -62,7 +62,7 @@
onChange="action-timetable-group-on-amount-change"
x-scale="purchaseOrder.currency.numberOfDecimals"/>
<field name="percentage" readonlyIf="invoiced" required="true"
onChange="action-timetable-record-on-percentage-change"/>
onChange="action-timetable-method-percentage-onchange"/>
<field name="comments" readonlyIf="invoiced" width="300%"/>
<field name="invoiced" readonly="true"/>
<button name="generateInvoiceBtn" readonlyIf="!id || invoiced" help="Generate Invoice"
Expand Down Expand Up @@ -92,7 +92,7 @@
onChange="action-timetable-group-on-amount-change"
x-scale="saleOrder.currency.numberOfDecimals"/>
<field name="percentage" colSpan="4" required="true"
onChange="action-timetable-record-on-percentage-change"/>
onChange="action-timetable-method-percentage-onchange"/>
<field name="invoiced" hideIf="!invoice"/>
<field name="comments" colSpan="12"/>
<field name="saleOrder.currency.numberOfDecimals" hidden="true"/>
Expand All @@ -118,15 +118,13 @@
expr="eval: (__parent__.exTaxTotal != null &amp;&amp; __parent__.exTaxTotal &gt; 0) ? amount * 100 / __parent__.exTaxTotal : 0"/>
</action-record>

<action-record name="action-timetable-record-on-percentage-change"
model="com.axelor.apps.supplychain.db.Timetable">
<field name="amount"
expr="eval: (__parent__.exTaxTotal != null) ? (__parent__.exTaxTotal * percentage / 100).setScale(__parent__?.currency?.numberOfDecimals, java.math.RoundingMode.HALF_UP) : 0"
if="__parent__ != null &amp;&amp; (__parent__._model == 'com.axelor.apps.sale.db.SaleOrder' || __parent__._model == 'com.axelor.apps.purchase.db.PurchaseOrder')"/>
</action-record>

<action-method name="action-timetable-method-generate-invoice">
<call class="com.axelor.apps.supplychain.web.TimetableController" method="generateInvoice"/>
</action-method>

<action-method name="action-timetable-method-percentage-onchange">
<call class="com.axelor.apps.supplychain.web.TimetableController"
method="percentageOnChange"/>
</action-method>

</object-views>
Loading