mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-18 12:39:18 +00:00
feat: Consider filtered items table in JS for totals computation
- Set `_items` as filtered rows if quotation else the entire table. Set at entry point of JS API - Use `_items` instead of `items` to compute taxes and charges. Exclude alternative item rows
This commit is contained in:
@@ -91,6 +91,9 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_calculate_taxes_and_totals() {
|
_calculate_taxes_and_totals() {
|
||||||
|
const is_quotation = this.frm.doc.doctype == "Quotation";
|
||||||
|
this.frm.doc._items = is_quotation ? this.filtered_items() : this.frm.doc.items;
|
||||||
|
|
||||||
this.validate_conversion_rate();
|
this.validate_conversion_rate();
|
||||||
this.calculate_item_values();
|
this.calculate_item_values();
|
||||||
this.initialize_taxes();
|
this.initialize_taxes();
|
||||||
@@ -122,7 +125,7 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
calculate_item_values() {
|
calculate_item_values() {
|
||||||
var me = this;
|
var me = this;
|
||||||
if (!this.discount_amount_applied) {
|
if (!this.discount_amount_applied) {
|
||||||
for (const item of this.frm.doc.items || []) {
|
for (const item of this.frm.doc._items || []) {
|
||||||
frappe.model.round_floats_in(item);
|
frappe.model.round_floats_in(item);
|
||||||
item.net_rate = item.rate;
|
item.net_rate = item.rate;
|
||||||
item.qty = item.qty === undefined ? (me.frm.doc.is_return ? -1 : 1) : item.qty;
|
item.qty = item.qty === undefined ? (me.frm.doc.is_return ? -1 : 1) : item.qty;
|
||||||
@@ -197,7 +200,7 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
});
|
});
|
||||||
if(has_inclusive_tax==false) return;
|
if(has_inclusive_tax==false) return;
|
||||||
|
|
||||||
$.each(me.frm.doc["items"] || [], function(n, item) {
|
$.each(me.frm.doc._items || [], function(n, item) {
|
||||||
var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
|
var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
|
||||||
var cumulated_tax_fraction = 0.0;
|
var cumulated_tax_fraction = 0.0;
|
||||||
var total_inclusive_tax_amount_per_qty = 0;
|
var total_inclusive_tax_amount_per_qty = 0;
|
||||||
@@ -268,7 +271,7 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
var me = this;
|
var me = this;
|
||||||
this.frm.doc.total_qty = this.frm.doc.total = this.frm.doc.base_total = this.frm.doc.net_total = this.frm.doc.base_net_total = 0.0;
|
this.frm.doc.total_qty = this.frm.doc.total = this.frm.doc.base_total = this.frm.doc.net_total = this.frm.doc.base_net_total = 0.0;
|
||||||
|
|
||||||
$.each(this.frm.doc["items"] || [], function(i, item) {
|
$.each(this.frm.doc._items || [], function(i, item) {
|
||||||
me.frm.doc.total += item.amount;
|
me.frm.doc.total += item.amount;
|
||||||
me.frm.doc.total_qty += item.qty;
|
me.frm.doc.total_qty += item.qty;
|
||||||
me.frm.doc.base_total += item.base_amount;
|
me.frm.doc.base_total += item.base_amount;
|
||||||
@@ -321,7 +324,7 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$.each(this.frm.doc["items"] || [], function(n, item) {
|
$.each(this.frm.doc._items || [], function(n, item) {
|
||||||
var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
|
var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
|
||||||
$.each(me.frm.doc["taxes"] || [], function(i, tax) {
|
$.each(me.frm.doc["taxes"] || [], function(i, tax) {
|
||||||
// tax_amount represents the amount of tax for the current step
|
// tax_amount represents the amount of tax for the current step
|
||||||
@@ -330,7 +333,7 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
// Adjust divisional loss to the last item
|
// Adjust divisional loss to the last item
|
||||||
if (tax.charge_type == "Actual") {
|
if (tax.charge_type == "Actual") {
|
||||||
actual_tax_dict[tax.idx] -= current_tax_amount;
|
actual_tax_dict[tax.idx] -= current_tax_amount;
|
||||||
if (n == me.frm.doc["items"].length - 1) {
|
if (n == me.frm.doc._items.length - 1) {
|
||||||
current_tax_amount += actual_tax_dict[tax.idx];
|
current_tax_amount += actual_tax_dict[tax.idx];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -367,7 +370,7 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set precision in the last item iteration
|
// set precision in the last item iteration
|
||||||
if (n == me.frm.doc["items"].length - 1) {
|
if (n == me.frm.doc._items.length - 1) {
|
||||||
me.round_off_totals(tax);
|
me.round_off_totals(tax);
|
||||||
me.set_in_company_currency(tax,
|
me.set_in_company_currency(tax,
|
||||||
["tax_amount", "tax_amount_after_discount_amount"]);
|
["tax_amount", "tax_amount_after_discount_amount"]);
|
||||||
@@ -590,10 +593,11 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
|
|
||||||
_cleanup() {
|
_cleanup() {
|
||||||
this.frm.doc.base_in_words = this.frm.doc.in_words = "";
|
this.frm.doc.base_in_words = this.frm.doc.in_words = "";
|
||||||
|
let items = this.frm.doc._items;
|
||||||
|
|
||||||
if(this.frm.doc["items"] && this.frm.doc["items"].length) {
|
if(items && items.length) {
|
||||||
if(!frappe.meta.get_docfield(this.frm.doc["items"][0].doctype, "item_tax_amount", this.frm.doctype)) {
|
if(!frappe.meta.get_docfield(items[0].doctype, "item_tax_amount", this.frm.doctype)) {
|
||||||
$.each(this.frm.doc["items"] || [], function(i, item) {
|
$.each(items || [], function(i, item) {
|
||||||
delete item["item_tax_amount"];
|
delete item["item_tax_amount"];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -646,7 +650,7 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
var net_total = 0;
|
var net_total = 0;
|
||||||
// calculate item amount after Discount Amount
|
// calculate item amount after Discount Amount
|
||||||
if (total_for_discount_amount) {
|
if (total_for_discount_amount) {
|
||||||
$.each(this.frm.doc["items"] || [], function(i, item) {
|
$.each(this.frm.doc._items || [], function(i, item) {
|
||||||
distributed_amount = flt(me.frm.doc.discount_amount) * item.net_amount / total_for_discount_amount;
|
distributed_amount = flt(me.frm.doc.discount_amount) * item.net_amount / total_for_discount_amount;
|
||||||
item.net_amount = flt(item.net_amount - distributed_amount,
|
item.net_amount = flt(item.net_amount - distributed_amount,
|
||||||
precision("base_amount", item));
|
precision("base_amount", item));
|
||||||
@@ -654,7 +658,7 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
|
|
||||||
// discount amount rounding loss adjustment if no taxes
|
// discount amount rounding loss adjustment if no taxes
|
||||||
if ((!(me.frm.doc.taxes || []).length || total_for_discount_amount==me.frm.doc.net_total || (me.frm.doc.apply_discount_on == "Net Total"))
|
if ((!(me.frm.doc.taxes || []).length || total_for_discount_amount==me.frm.doc.net_total || (me.frm.doc.apply_discount_on == "Net Total"))
|
||||||
&& i == (me.frm.doc.items || []).length - 1) {
|
&& i == (me.frm.doc._items || []).length - 1) {
|
||||||
var discount_amount_loss = flt(me.frm.doc.net_total - net_total
|
var discount_amount_loss = flt(me.frm.doc.net_total - net_total
|
||||||
- me.frm.doc.discount_amount, precision("net_total"));
|
- me.frm.doc.discount_amount, precision("net_total"));
|
||||||
item.net_amount = flt(item.net_amount + discount_amount_loss,
|
item.net_amount = flt(item.net_amount + discount_amount_loss,
|
||||||
@@ -883,4 +887,8 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filtered_items() {
|
||||||
|
return this.frm.doc.items.filter(item => !item["is_alternative_item"]);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user