Skip to content
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

fix: Write Off not working when paid_amount is greater than grand_total #43303

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions erpnext/accounts/doctype/pos_invoice/pos_invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ erpnext.selling.POSInvoiceController = class POSInvoiceController extends erpnex
}

change_amount() {
if (this.frm.doc.paid_amount > this.frm.doc.grand_total) {
this.calculate_write_off_amount();
} else {
if (this.frm.doc.paid_amount <= this.frm.doc.grand_total) {
this.frm.set_value("change_amount", 0.0);
this.frm.set_value("base_change_amount", 0.0);
}
Expand All @@ -180,7 +178,10 @@ erpnext.selling.POSInvoiceController = class POSInvoiceController extends erpnex
this.frm.set_value(
"write_off_amount",
flt(
this.frm.doc.grand_total - this.frm.doc.paid_amount - this.frm.doc.total_advance,
this.frm.doc.grand_total -
this.frm.doc.paid_amount +
this.frm.doc.change_amount -
this.frm.doc.total_advance,
precision("write_off_amount")
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def merge_pos_invoice_into(self, invoice, data):

rounding_adjustment, base_rounding_adjustment = 0, 0
rounded_total, base_rounded_total = 0, 0
base_write_off_amount, write_off_amount = 0, 0

loyalty_amount_sum, loyalty_points_sum, idx = 0, 0, 1

Expand Down Expand Up @@ -247,8 +248,10 @@ def merge_pos_invoice_into(self, invoice, data):

rounding_adjustment += doc.rounding_adjustment
rounded_total += doc.rounded_total
write_off_amount += doc.write_off_amount
base_rounding_adjustment += doc.base_rounding_adjustment
base_rounded_total += doc.base_rounded_total
base_write_off_amount += doc.base_write_off_amount

if loyalty_points_sum:
invoice.redeem_loyalty_points = 1
Expand All @@ -262,6 +265,8 @@ def merge_pos_invoice_into(self, invoice, data):
invoice.set("base_rounding_adjustment", base_rounding_adjustment)
invoice.set("rounded_total", rounded_total)
invoice.set("base_rounded_total", base_rounded_total)
invoice.set("write_off_amount", write_off_amount)
invoice.set("base_write_off_amount", base_write_off_amount)
invoice.additional_discount_percentage = 0
invoice.discount_amount = 0.0
invoice.taxes_and_charges = None
Expand Down
14 changes: 5 additions & 9 deletions erpnext/accounts/doctype/sales_invoice/sales_invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,10 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends (
this.frm.set_value(
"write_off_amount",
flt(
this.frm.doc.grand_total - this.frm.doc.paid_amount - this.frm.doc.total_advance,
this.frm.doc.grand_total -
this.frm.doc.paid_amount +
this.frm.doc.change_amount -
this.frm.doc.total_advance,
precision("write_off_amount")
)
);
Expand All @@ -413,11 +416,6 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends (
this.frm.refresh_fields();
}

write_off_amount() {
this.set_in_company_currency(this.frm.doc, ["write_off_amount"]);
this.write_off_outstanding_amount_automatically();
}

items_add(doc, cdt, cdn) {
var row = frappe.get_doc(cdt, cdn);
this.frm.script_manager.copy_from_first_row("items", row, [
Expand Down Expand Up @@ -512,9 +510,7 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends (
}

change_amount() {
if (this.frm.doc.paid_amount > this.frm.doc.grand_total) {
this.calculate_write_off_amount();
} else {
if (this.frm.doc.paid_amount <= this.frm.doc.grand_total) {
this.frm.set_value("change_amount", 0.0);
this.frm.set_value("base_change_amount", 0.0);
}
Expand Down
12 changes: 9 additions & 3 deletions erpnext/controllers/taxes_and_totals.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,11 @@ def calculate_outstanding_amount(self):
write_off_limit = flt(
frappe.db.get_value("POS Profile", self.doc.pos_profile, "write_off_limit")
)
if write_off_limit and abs(self.doc.outstanding_amount) <= write_off_limit:
if (
self.doc.outstanding_amount
and write_off_limit
and abs(self.doc.outstanding_amount) <= write_off_limit
):
self.doc.write_off_outstanding_amount_automatically = 1

if (
Expand Down Expand Up @@ -897,11 +901,13 @@ def calculate_change_amount(self):
and any(d.type == "Cash" for d in self.doc.payments)
):
self.doc.change_amount = flt(
self.doc.paid_amount - grand_total, self.doc.precision("change_amount")
self.doc.paid_amount - grand_total + self.doc.write_off_amount,
self.doc.precision("change_amount"),
)

self.doc.base_change_amount = flt(
self.doc.base_paid_amount - base_grand_total, self.doc.precision("base_change_amount")
self.doc.base_paid_amount - base_grand_total + self.doc.base_write_off_amount,
self.doc.precision("base_change_amount"),
)

def calculate_write_off_amount(self):
Expand Down
27 changes: 19 additions & 8 deletions erpnext/public/js/controllers/taxes_and_totals.js
Original file line number Diff line number Diff line change
Expand Up @@ -919,27 +919,38 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
var grand_total = this.frm.doc.rounded_total || this.frm.doc.grand_total;
var base_grand_total = this.frm.doc.base_rounded_total || this.frm.doc.base_grand_total;

this.frm.doc.change_amount = flt(this.frm.doc.paid_amount - grand_total,
precision("change_amount"));
this.frm.doc.change_amount = flt(this.frm.doc.paid_amount - grand_total +
this.frm.doc.write_off_amount, precision("change_amount"));

this.frm.doc.base_change_amount = flt(this.frm.doc.base_paid_amount -
base_grand_total, precision("base_change_amount"));
base_grand_total + this.frm.doc.base_write_off_amount,
precision("base_change_amount"));
}
}
}

calculate_write_off_amount() {
if(this.frm.doc.write_off_outstanding_amount_automatically) {
if (this.frm.doc.paid_amount > this.frm.doc.grand_total){
this.frm.doc.write_off_amount = flt(this.frm.doc.grand_total - this.frm.doc.paid_amount
+ this.frm.doc.change_amount, precision("write_off_amount"));
}
else if(this.frm.doc.write_off_outstanding_amount_automatically) {
this.frm.doc.write_off_amount = flt(this.frm.doc.outstanding_amount, precision("write_off_amount"));
this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate,
precision("base_write_off_amount"));

this.calculate_outstanding_amount(false);
}
this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate,
precision("base_write_off_amount"));

this.calculate_outstanding_amount(false);

}

filtered_items() {
return this.frm.doc.items.filter(item => !item["is_alternative"]);
}


write_off_amount() {
this.set_in_company_currency(this.frm.doc, ["write_off_amount"]);
this.write_off_outstanding_amount_automatically();
}
};
Loading