Skip to content

Commit

Permalink
fix: Cash and non trade discount calculation
Browse files Browse the repository at this point in the history
(cherry picked from commit 3b15966)
  • Loading branch information
deepeshgarg007 authored and mergify[bot] committed Aug 23, 2022
1 parent d46ce05 commit 20e9599
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
2 changes: 1 addition & 1 deletion erpnext/accounts/doctype/gl_entry/gl_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def update_outstanding_amt(
if against_voucher_type in ["Sales Invoice", "Purchase Invoice", "Fees"]:
ref_doc = frappe.get_doc(against_voucher_type, against_voucher)

# Didn't use db_set for optimisation purpose
# Didn't use db_set for optimization purpose
ref_doc.outstanding_amount = bal
frappe.db.set_value(against_voucher_type, against_voucher, "outstanding_amount", bal)

Expand Down
4 changes: 4 additions & 0 deletions erpnext/accounts/doctype/sales_invoice/sales_invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,13 @@ erpnext.accounts.SalesInvoiceController = erpnext.selling.SellingController.exte

is_cash_or_non_trade_discount() {
this.frm.set_df_property("additional_discount_account", "hidden", 1 - this.frm.doc.is_cash_or_non_trade_discount);
this.frm.set_df_property("additional_discount_account", "reqd", this.frm.doc.is_cash_or_non_trade_discount);

if (!this.frm.doc.is_cash_or_non_trade_discount) {
this.frm.set_value("additional_discount_account", "");
}

this.calculate_taxes_and_totals();
}

});
Expand Down
16 changes: 0 additions & 16 deletions erpnext/accounts/doctype/sales_invoice/sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,22 +1065,6 @@ def make_customer_gl_entry(self, gl_entries):
)
)

if self.apply_discount_on == "Grand Total" and self.get("is_cash_or_discount_account"):
gl_entries.append(
self.get_gl_dict(
{
"account": self.additional_discount_account,
"against": self.debit_to,
"debit": self.base_discount_amount,
"debit_in_account_currency": self.discount_amount,
"cost_center": self.cost_center,
"project": self.project,
},
self.currency,
item=self,
)
)

def make_tax_gl_entries(self, gl_entries):
for tax in self.get("taxes"):
amount, base_amount = self.get_tax_amounts(tax, self.enable_discount_accounting)
Expand Down
44 changes: 24 additions & 20 deletions erpnext/controllers/accounts_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,17 +1105,17 @@ def make_discount_gl_entries(self, gl_entries):
frappe.db.get_single_value("Accounts Settings", "enable_discount_accounting")
)

if enable_discount_accounting:
if self.doctype == "Purchase Invoice":
dr_or_cr = "credit"
rev_dr_cr = "debit"
supplier_or_customer = self.supplier
if self.doctype == "Purchase Invoice":
dr_or_cr = "credit"
rev_dr_cr = "debit"
supplier_or_customer = self.supplier

else:
dr_or_cr = "debit"
rev_dr_cr = "credit"
supplier_or_customer = self.customer
else:
dr_or_cr = "debit"
rev_dr_cr = "credit"
supplier_or_customer = self.customer

if enable_discount_accounting:
for item in self.get("items"):
if item.get("discount_amount") and item.get("discount_account"):
discount_amount = item.discount_amount * item.qty
Expand Down Expand Up @@ -1169,18 +1169,22 @@ def make_discount_gl_entries(self, gl_entries):
)
)

if self.get("discount_amount") and self.get("additional_discount_account"):
gl_entries.append(
self.get_gl_dict(
{
"account": self.additional_discount_account,
"against": supplier_or_customer,
dr_or_cr: self.discount_amount,
"cost_center": self.cost_center,
},
item=self,
)
if (
(enable_discount_accounting or self.is_cash_or_non_trade_discount)
and self.get("additional_discount_account")
and self.get("discount_amount")
):
gl_entries.append(
self.get_gl_dict(
{
"account": self.additional_discount_account,
"against": supplier_or_customer,
dr_or_cr: self.discount_amount,
"cost_center": self.cost_center,
},
item=self,
)
)

def validate_multiple_billing(self, ref_dt, item_ref_dn, based_on, parentfield):
from erpnext.controllers.status_updater import get_allowance_for
Expand Down
16 changes: 9 additions & 7 deletions erpnext/controllers/taxes_and_totals.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def calculate(self):
self.set_discount_amount()
self.apply_discount_amount()

# Update grand total as per cash and non trade discount
if self.doc.apply_discount_on == "Grand Total" and self.doc.get("is_cash_or_non_trade_discount"):
self.doc.grand_total -= self.doc.discount_amount
self.doc.base_grand_total -= self.doc.base_discount_amount

self.calculate_shipping_charges()

if self.doc.doctype in ["Sales Invoice", "Purchase Invoice"]:
Expand Down Expand Up @@ -500,9 +505,6 @@ def calculate_totals(self):
else:
self.doc.grand_total = flt(self.doc.net_total)

if self.doc.apply_discount_on == "Grand Total" and self.doc.get("is_cash_or_non_trade_discount"):
self.doc.grand_total -= self.doc.discount_amount

if self.doc.get("taxes"):
self.doc.total_taxes_and_charges = flt(
self.doc.grand_total - self.doc.net_total - flt(self.doc.rounding_adjustment),
Expand Down Expand Up @@ -597,16 +599,16 @@ def apply_discount_amount(self):
if not self.doc.apply_discount_on:
frappe.throw(_("Please select Apply Discount On"))

self.doc.base_discount_amount = flt(
self.doc.discount_amount * self.doc.conversion_rate, self.doc.precision("base_discount_amount")
)

if self.doc.apply_discount_on == "Grand Total" and self.doc.get(
"is_cash_or_non_trade_discount"
):
self.discount_amount_applied = True
return

self.doc.base_discount_amount = flt(
self.doc.discount_amount * self.doc.conversion_rate, self.doc.precision("base_discount_amount")
)

total_for_discount_amount = self.get_total_for_discount_amount()
taxes = self.doc.get("taxes")
net_total = 0
Expand Down
10 changes: 10 additions & 0 deletions erpnext/public/js/controllers/taxes_and_totals.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ erpnext.taxes_and_totals = erpnext.payments.extend({
this._calculate_taxes_and_totals();
this.calculate_discount_amount();

// # Update grand total as per cash and non trade discount
if (this.frm.doc.apply_discount_on == "Grand Total" && this.frm.doc.is_cash_or_non_trade_discount) {
this.frm.doc.grand_total -= this.frm.doc.discount_amount;
this.frm.doc.base_grand_total -= this.frm.doc.base_discount_amount;
}

await this.calculate_shipping_charges();

// Advance calculation applicable to Sales /Purchase Invoice
Expand Down Expand Up @@ -611,6 +617,10 @@ erpnext.taxes_and_totals = erpnext.payments.extend({
this.frm.doc.base_discount_amount = flt(this.frm.doc.discount_amount * this.frm.doc.conversion_rate,
precision("base_discount_amount"));

if (this.frm.doc.apply_discount_on == "Grand Total" && this.frm.doc.is_cash_or_non_trade_discount) {
return
}

var total_for_discount_amount = this.get_total_for_discount_amount();
var net_total = 0;
// calculate item amount after Discount Amount
Expand Down

0 comments on commit 20e9599

Please sign in to comment.