2015-03-03 09:25:30 +00:00
|
|
|
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2015-01-22 11:43:13 +00:00
|
|
|
// License: GNU General Public License v3. See license.txt
|
|
|
|
|
2021-05-07 09:51:33 +00:00
|
|
|
erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
|
2021-02-19 09:00:23 +00:00
|
|
|
setup() {
|
|
|
|
this.fetch_round_off_accounts();
|
|
|
|
}
|
2020-02-18 06:58:41 +00:00
|
|
|
|
2021-02-11 14:41:58 +00:00
|
|
|
apply_pricing_rule_on_item(item) {
|
2018-05-28 14:37:08 +00:00
|
|
|
let effective_item_rate = item.price_list_rate;
|
2020-10-26 05:47:04 +00:00
|
|
|
let item_rate = item.rate;
|
2020-03-30 17:14:13 +00:00
|
|
|
if (in_list(["Sales Order", "Quotation"], item.parenttype) && item.blanket_order_rate) {
|
2018-05-28 14:37:08 +00:00
|
|
|
effective_item_rate = item.blanket_order_rate;
|
|
|
|
}
|
2021-06-05 07:51:03 +00:00
|
|
|
if (item.margin_type == "Percentage") {
|
2018-05-28 14:37:08 +00:00
|
|
|
item.rate_with_margin = flt(effective_item_rate)
|
|
|
|
+ flt(effective_item_rate) * ( flt(item.margin_rate_or_amount) / 100);
|
2017-04-10 13:45:57 +00:00
|
|
|
} else {
|
2018-05-28 14:37:08 +00:00
|
|
|
item.rate_with_margin = flt(effective_item_rate) + flt(item.margin_rate_or_amount);
|
2016-04-09 09:01:09 +00:00
|
|
|
}
|
2018-11-13 13:11:58 +00:00
|
|
|
item.base_rate_with_margin = flt(item.rate_with_margin) * flt(this.frm.doc.conversion_rate);
|
2016-04-09 09:01:09 +00:00
|
|
|
|
2020-10-26 05:47:04 +00:00
|
|
|
item_rate = flt(item.rate_with_margin , precision("rate", item));
|
2016-04-09 09:01:09 +00:00
|
|
|
|
2021-06-05 07:51:03 +00:00
|
|
|
if (item.discount_percentage) {
|
2018-08-14 05:21:48 +00:00
|
|
|
item.discount_amount = flt(item.rate_with_margin) * flt(item.discount_percentage) / 100;
|
2019-03-18 09:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (item.discount_amount) {
|
2020-10-26 05:47:04 +00:00
|
|
|
item_rate = flt((item.rate_with_margin) - (item.discount_amount), precision('rate', item));
|
2021-02-11 14:41:58 +00:00
|
|
|
item.discount_percentage = 100 * flt(item.discount_amount) / flt(item.rate_with_margin);
|
2016-04-09 09:01:09 +00:00
|
|
|
}
|
2020-10-26 05:47:04 +00:00
|
|
|
|
|
|
|
frappe.model.set_value(item.doctype, item.name, "rate", item_rate);
|
2016-04-09 09:01:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 13:53:53 +00:00
|
|
|
async calculate_taxes_and_totals(update_paid_amount) {
|
2015-01-22 11:43:13 +00:00
|
|
|
this.discount_amount_applied = false;
|
|
|
|
this._calculate_taxes_and_totals();
|
2016-04-09 09:01:09 +00:00
|
|
|
this.calculate_discount_amount();
|
2015-01-22 11:43:13 +00:00
|
|
|
|
2022-08-21 12:21:05 +00:00
|
|
|
// # 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;
|
|
|
|
}
|
|
|
|
|
2022-03-27 07:32:31 +00:00
|
|
|
await this.calculate_shipping_charges();
|
2022-03-10 15:26:36 +00:00
|
|
|
|
2022-11-23 12:22:12 +00:00
|
|
|
// Advance calculation applicable to Sales/Purchase Invoice
|
|
|
|
if (
|
|
|
|
in_list(["Sales Invoice", "POS Invoice", "Purchase Invoice"], this.frm.doc.doctype)
|
|
|
|
&& this.frm.doc.docstatus < 2
|
|
|
|
&& !this.frm.doc.is_return
|
|
|
|
) {
|
2017-05-30 07:24:42 +00:00
|
|
|
this.calculate_total_advance(update_paid_amount);
|
2015-01-22 11:43:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 12:22:12 +00:00
|
|
|
if (
|
|
|
|
in_list(["Sales Invoice", "POS Invoice"], this.frm.doc.doctype)
|
2022-12-12 13:39:00 +00:00
|
|
|
&& this.frm.doc.is_pos
|
2022-11-23 12:22:12 +00:00
|
|
|
&& this.frm.doc.is_return
|
|
|
|
) {
|
|
|
|
this.set_total_amount_to_default_mop();
|
2021-08-10 10:27:30 +00:00
|
|
|
this.calculate_paid_amount();
|
2020-02-25 07:51:16 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:43:13 +00:00
|
|
|
// Sales person's commission
|
2022-11-23 12:22:12 +00:00
|
|
|
if (in_list(["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"], this.frm.doc.doctype)) {
|
2015-01-22 11:43:13 +00:00
|
|
|
this.calculate_commission();
|
|
|
|
this.calculate_contribution();
|
|
|
|
}
|
|
|
|
|
2019-09-11 05:19:33 +00:00
|
|
|
// Update paid amount on return/debit note creation
|
2022-11-23 12:22:12 +00:00
|
|
|
if (
|
|
|
|
this.frm.doc.doctype === "Purchase Invoice"
|
|
|
|
&& this.frm.doc.is_return
|
|
|
|
&& (this.frm.doc.grand_total > this.frm.doc.paid_amount)
|
|
|
|
) {
|
2019-09-11 05:19:33 +00:00
|
|
|
this.frm.doc.paid_amount = flt(this.frm.doc.grand_total, precision("grand_total"));
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:43:13 +00:00
|
|
|
this.frm.refresh_fields();
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:41:20 +00:00
|
|
|
calculate_discount_amount() {
|
2017-07-25 09:56:01 +00:00
|
|
|
if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount")) {
|
|
|
|
this.set_discount_amount();
|
2016-04-09 09:01:09 +00:00
|
|
|
this.apply_discount_amount();
|
2017-07-25 09:56:01 +00:00
|
|
|
}
|
2016-04-09 09:01:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:43:13 +00:00
|
|
|
_calculate_taxes_and_totals() {
|
2023-01-25 07:40:03 +00:00
|
|
|
const is_quotation = this.frm.doc.doctype == "Quotation";
|
|
|
|
this.frm.doc._items = is_quotation ? this.filtered_items() : this.frm.doc.items;
|
|
|
|
|
2021-07-19 16:15:33 +00:00
|
|
|
this.validate_conversion_rate();
|
|
|
|
this.calculate_item_values();
|
|
|
|
this.initialize_taxes();
|
|
|
|
this.determine_exclusive_rate();
|
|
|
|
this.calculate_net_total();
|
|
|
|
this.calculate_taxes();
|
|
|
|
this.manipulate_grand_total_for_inclusive_tax();
|
|
|
|
this.calculate_totals();
|
|
|
|
this._cleanup();
|
2021-07-23 11:22:42 +00:00
|
|
|
}
|
2015-01-22 11:43:13 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
validate_conversion_rate() {
|
2016-08-29 19:11:33 +00:00
|
|
|
this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, (cur_frm) ? precision("conversion_rate") : 9);
|
2015-02-20 09:10:35 +00:00
|
|
|
var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
|
|
|
|
this.frm.doc.name);
|
2017-08-16 07:46:46 +00:00
|
|
|
var company_currency = this.get_company_currency();
|
2015-02-20 09:10:35 +00:00
|
|
|
|
|
|
|
if(!this.frm.doc.conversion_rate) {
|
2015-05-19 06:59:56 +00:00
|
|
|
if(this.frm.doc.currency == company_currency) {
|
|
|
|
this.frm.set_value("conversion_rate", 1);
|
|
|
|
} else {
|
2020-10-26 05:47:04 +00:00
|
|
|
const subs = [conversion_rate_label, this.frm.doc.currency, company_currency];
|
|
|
|
const err_message = __('{0} is mandatory. Maybe Currency Exchange record is not created for {1} to {2}', subs);
|
2018-10-03 05:07:41 +00:00
|
|
|
frappe.throw(err_message);
|
2015-05-19 06:59:56 +00:00
|
|
|
}
|
2015-02-20 09:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 10:54:49 +00:00
|
|
|
calculate_item_values() {
|
|
|
|
var me = this;
|
|
|
|
if (!this.discount_amount_applied) {
|
2023-01-25 07:40:03 +00:00
|
|
|
for (const item of this.frm.doc._items || []) {
|
2015-01-23 10:54:49 +00:00
|
|
|
frappe.model.round_floats_in(item);
|
2015-02-20 09:10:35 +00:00
|
|
|
item.net_rate = item.rate;
|
2023-01-20 14:05:27 +00:00
|
|
|
item.qty = item.qty === undefined ? (me.frm.doc.is_return ? -1 : 1) : item.qty;
|
2023-02-11 03:34:00 +00:00
|
|
|
|
|
|
|
if (!(me.frm.doc.is_return || me.frm.doc.is_debit_note)) {
|
|
|
|
item.net_amount = item.amount = flt(item.rate * item.qty, precision("amount", item));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let qty = item.qty || 1;
|
|
|
|
qty = me.frm.doc.is_return ? -1 * qty : qty;
|
|
|
|
item.net_amount = item.amount = flt(item.rate * qty, precision("amount", item));
|
|
|
|
}
|
|
|
|
|
2015-01-23 10:54:49 +00:00
|
|
|
item.item_tax_amount = 0.0;
|
2018-02-16 09:15:40 +00:00
|
|
|
item.total_weight = flt(item.weight_per_unit * item.stock_qty);
|
2015-01-23 10:54:49 +00:00
|
|
|
|
2015-02-22 14:44:49 +00:00
|
|
|
me.set_in_company_currency(item, ["price_list_rate", "rate", "amount", "net_rate", "net_amount"]);
|
2023-01-20 14:05:27 +00:00
|
|
|
}
|
2015-01-23 10:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
set_in_company_currency(doc, fields) {
|
2015-02-22 14:44:49 +00:00
|
|
|
var me = this;
|
2015-02-20 09:10:35 +00:00
|
|
|
$.each(fields, function(i, f) {
|
|
|
|
doc["base_"+f] = flt(flt(doc[f], precision(f, doc)) * me.frm.doc.conversion_rate, precision("base_" + f, doc));
|
2017-07-31 12:37:45 +00:00
|
|
|
});
|
2015-02-22 14:44:49 +00:00
|
|
|
}
|
2015-02-20 09:10:35 +00:00
|
|
|
|
2015-01-22 11:43:13 +00:00
|
|
|
initialize_taxes() {
|
|
|
|
var me = this;
|
|
|
|
|
|
|
|
$.each(this.frm.doc["taxes"] || [], function(i, tax) {
|
2021-10-19 08:35:44 +00:00
|
|
|
if (!tax.dont_recompute_tax) {
|
|
|
|
tax.item_wise_tax_detail = {};
|
|
|
|
}
|
2017-05-30 07:24:42 +00:00
|
|
|
var tax_fields = ["total", "tax_amount_after_discount_amount",
|
2015-01-22 11:43:13 +00:00
|
|
|
"tax_amount_for_current_item", "grand_total_for_current_item",
|
2017-07-31 12:37:45 +00:00
|
|
|
"tax_fraction_for_current_item", "grand_total_fraction_for_current_item"];
|
2015-01-22 11:43:13 +00:00
|
|
|
|
2015-02-22 19:36:00 +00:00
|
|
|
if (cstr(tax.charge_type) != "Actual" &&
|
2017-05-30 07:24:42 +00:00
|
|
|
!(me.discount_amount_applied && me.frm.doc.apply_discount_on=="Grand Total")) {
|
|
|
|
tax_fields.push("tax_amount");
|
|
|
|
}
|
2015-01-22 11:43:13 +00:00
|
|
|
|
2017-07-31 12:37:45 +00:00
|
|
|
$.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0; });
|
2015-01-22 11:43:13 +00:00
|
|
|
|
2016-04-09 09:01:09 +00:00
|
|
|
if (!this.discount_amount_applied && cur_frm) {
|
2015-02-28 13:41:51 +00:00
|
|
|
cur_frm.cscript.validate_taxes_and_charges(tax.doctype, tax.name);
|
|
|
|
me.validate_inclusive_tax(tax);
|
|
|
|
}
|
2015-01-22 11:43:13 +00:00
|
|
|
frappe.model.round_floats_in(tax);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-19 09:00:23 +00:00
|
|
|
fetch_round_off_accounts() {
|
|
|
|
let me = this;
|
|
|
|
frappe.flags.round_off_applicable_accounts = [];
|
|
|
|
|
2021-03-08 12:49:53 +00:00
|
|
|
if (me.frm.doc.company) {
|
|
|
|
return frappe.call({
|
|
|
|
"method": "erpnext.controllers.taxes_and_totals.get_round_off_applicable_accounts",
|
|
|
|
"args": {
|
|
|
|
"company": me.frm.doc.company,
|
|
|
|
"account_list": frappe.flags.round_off_applicable_accounts
|
|
|
|
},
|
2021-05-07 09:51:33 +00:00
|
|
|
callback(r) {
|
2021-05-21 11:33:09 +00:00
|
|
|
if (r.message) {
|
|
|
|
frappe.flags.round_off_applicable_accounts.push(...r.message);
|
|
|
|
}
|
2021-03-08 12:49:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-02-19 09:00:23 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 10:54:49 +00:00
|
|
|
determine_exclusive_rate() {
|
2015-02-22 14:44:49 +00:00
|
|
|
var me = this;
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
var has_inclusive_tax = false;
|
|
|
|
$.each(me.frm.doc["taxes"] || [], function(i, row) {
|
|
|
|
if(cint(row.included_in_print_rate)) has_inclusive_tax = true;
|
2017-07-31 12:37:45 +00:00
|
|
|
});
|
2015-02-23 10:31:33 +00:00
|
|
|
if(has_inclusive_tax==false) return;
|
2015-01-23 10:54:49 +00:00
|
|
|
|
2023-01-25 07:40:03 +00:00
|
|
|
$.each(me.frm.doc._items || [], function(n, item) {
|
2015-01-23 10:54:49 +00:00
|
|
|
var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
|
|
|
|
var cumulated_tax_fraction = 0.0;
|
2020-08-11 15:04:57 +00:00
|
|
|
var total_inclusive_tax_amount_per_qty = 0;
|
2015-01-23 10:54:49 +00:00
|
|
|
$.each(me.frm.doc["taxes"] || [], function(i, tax) {
|
2020-08-11 15:04:57 +00:00
|
|
|
var current_tax_fraction = me.get_current_tax_fraction(tax, item_tax_map);
|
|
|
|
tax.tax_fraction_for_current_item = current_tax_fraction[0];
|
|
|
|
var inclusive_tax_amount_per_qty = current_tax_fraction[1];
|
2015-01-23 10:54:49 +00:00
|
|
|
|
|
|
|
if(i==0) {
|
|
|
|
tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item;
|
|
|
|
} else {
|
|
|
|
tax.grand_total_fraction_for_current_item =
|
|
|
|
me.frm.doc["taxes"][i-1].grand_total_fraction_for_current_item +
|
|
|
|
tax.tax_fraction_for_current_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
cumulated_tax_fraction += tax.tax_fraction_for_current_item;
|
2020-08-11 15:04:57 +00:00
|
|
|
total_inclusive_tax_amount_per_qty += inclusive_tax_amount_per_qty * flt(item.qty);
|
2015-01-23 10:54:49 +00:00
|
|
|
});
|
|
|
|
|
2020-08-11 15:04:57 +00:00
|
|
|
if(!me.discount_amount_applied && item.qty && (total_inclusive_tax_amount_per_qty || cumulated_tax_fraction)) {
|
|
|
|
var amount = flt(item.amount) - total_inclusive_tax_amount_per_qty;
|
|
|
|
item.net_amount = flt(amount / (1 + cumulated_tax_fraction));
|
2018-10-18 12:27:56 +00:00
|
|
|
item.net_rate = item.qty ? flt(item.net_amount / item.qty, precision("net_rate", item)) : 0;
|
2015-01-23 10:54:49 +00:00
|
|
|
|
2015-02-22 14:44:49 +00:00
|
|
|
me.set_in_company_currency(item, ["net_rate", "net_amount"]);
|
2015-01-23 10:54:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get_current_tax_fraction(tax, item_tax_map) {
|
|
|
|
// Get tax fraction for calculating tax exclusive amount
|
|
|
|
// from tax inclusive amount
|
|
|
|
var current_tax_fraction = 0.0;
|
2020-08-11 15:04:57 +00:00
|
|
|
var inclusive_tax_amount_per_qty = 0;
|
2015-01-23 10:54:49 +00:00
|
|
|
|
|
|
|
if(cint(tax.included_in_print_rate)) {
|
|
|
|
var tax_rate = this._get_tax_rate(tax, item_tax_map);
|
|
|
|
|
|
|
|
if(tax.charge_type == "On Net Total") {
|
|
|
|
current_tax_fraction = (tax_rate / 100.0);
|
|
|
|
|
|
|
|
} else if(tax.charge_type == "On Previous Row Amount") {
|
|
|
|
current_tax_fraction = (tax_rate / 100.0) *
|
|
|
|
this.frm.doc["taxes"][cint(tax.row_id) - 1].tax_fraction_for_current_item;
|
|
|
|
|
|
|
|
} else if(tax.charge_type == "On Previous Row Total") {
|
|
|
|
current_tax_fraction = (tax_rate / 100.0) *
|
|
|
|
this.frm.doc["taxes"][cint(tax.row_id) - 1].grand_total_fraction_for_current_item;
|
2020-08-11 15:04:57 +00:00
|
|
|
} else if (tax.charge_type == "On Item Quantity") {
|
|
|
|
inclusive_tax_amount_per_qty = flt(tax_rate);
|
2015-01-23 10:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 15:04:57 +00:00
|
|
|
if(tax.add_deduct_tax && tax.add_deduct_tax == "Deduct") {
|
|
|
|
current_tax_fraction *= -1;
|
2020-08-11 16:32:12 +00:00
|
|
|
inclusive_tax_amount_per_qty *= -1;
|
2015-05-15 06:32:01 +00:00
|
|
|
}
|
2020-08-11 15:04:57 +00:00
|
|
|
return [current_tax_fraction, inclusive_tax_amount_per_qty];
|
2015-01-23 10:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_get_tax_rate(tax, item_tax_map) {
|
2020-09-15 04:12:51 +00:00
|
|
|
return (Object.keys(item_tax_map).indexOf(tax.account_head) != -1) ?
|
|
|
|
flt(item_tax_map[tax.account_head], precision("rate", tax)) : tax.rate;
|
2015-01-23 10:54:49 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 12:25:50 +00:00
|
|
|
calculate_net_total() {
|
|
|
|
var me = this;
|
2018-05-28 06:19:08 +00:00
|
|
|
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;
|
2015-02-12 12:25:50 +00:00
|
|
|
|
2023-01-25 07:40:03 +00:00
|
|
|
$.each(this.frm.doc._items || [], function(i, item) {
|
2015-02-22 20:10:01 +00:00
|
|
|
me.frm.doc.total += item.amount;
|
2018-05-28 06:19:08 +00:00
|
|
|
me.frm.doc.total_qty += item.qty;
|
2015-02-22 20:10:01 +00:00
|
|
|
me.frm.doc.base_total += item.base_amount;
|
2015-02-20 09:10:35 +00:00
|
|
|
me.frm.doc.net_total += item.net_amount;
|
|
|
|
me.frm.doc.base_net_total += item.base_net_amount;
|
2017-12-14 12:36:02 +00:00
|
|
|
});
|
2021-11-16 15:09:58 +00:00
|
|
|
}
|
2015-02-12 12:25:50 +00:00
|
|
|
|
2021-11-16 13:36:49 +00:00
|
|
|
calculate_shipping_charges() {
|
2022-04-04 14:35:10 +00:00
|
|
|
// Do not apply shipping rule for POS
|
|
|
|
if (this.frm.doc.is_pos) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-22 20:10:01 +00:00
|
|
|
frappe.model.round_floats_in(this.frm.doc, ["total", "base_total", "net_total", "base_net_total"]);
|
2021-11-16 15:09:58 +00:00
|
|
|
if (frappe.meta.get_docfield(this.frm.doc.doctype, "shipping_rule", this.frm.doc.name)) {
|
2022-03-27 07:32:31 +00:00
|
|
|
return this.shipping_rule();
|
2021-11-09 09:16:45 +00:00
|
|
|
}
|
2015-02-12 12:25:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 05:44:40 +00:00
|
|
|
add_taxes_from_item_tax_template(item_tax_map) {
|
2021-06-04 17:23:26 +00:00
|
|
|
let me = this;
|
|
|
|
|
2021-06-05 07:51:03 +00:00
|
|
|
if (item_tax_map && cint(frappe.defaults.get_default("add_taxes_from_item_tax_template"))) {
|
|
|
|
if (typeof (item_tax_map) == "string") {
|
2021-06-04 17:23:26 +00:00
|
|
|
item_tax_map = JSON.parse(item_tax_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
$.each(item_tax_map, function(tax, rate) {
|
|
|
|
let found = (me.frm.doc.taxes || []).find(d => d.account_head === tax);
|
2021-06-05 07:51:03 +00:00
|
|
|
if (!found) {
|
2021-06-04 17:23:26 +00:00
|
|
|
let child = frappe.model.add_child(me.frm.doc, "taxes");
|
|
|
|
child.charge_type = "On Net Total";
|
|
|
|
child.account_head = tax;
|
|
|
|
child.rate = 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-06-16 05:44:40 +00:00
|
|
|
}
|
2021-06-04 17:23:26 +00:00
|
|
|
|
2015-01-22 11:43:13 +00:00
|
|
|
calculate_taxes() {
|
|
|
|
var me = this;
|
2017-09-19 09:23:16 +00:00
|
|
|
this.frm.doc.rounding_adjustment = 0;
|
2015-01-22 11:43:13 +00:00
|
|
|
var actual_tax_dict = {};
|
|
|
|
|
|
|
|
// maintain actual tax rate based on idx
|
|
|
|
$.each(this.frm.doc["taxes"] || [], function(i, tax) {
|
|
|
|
if (tax.charge_type == "Actual") {
|
2015-02-20 09:10:35 +00:00
|
|
|
actual_tax_dict[tax.idx] = flt(tax.tax_amount, precision("tax_amount", tax));
|
2015-01-22 11:43:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-25 07:40:03 +00:00
|
|
|
$.each(this.frm.doc._items || [], function(n, item) {
|
2015-01-22 11:43:13 +00:00
|
|
|
var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
|
|
|
|
$.each(me.frm.doc["taxes"] || [], function(i, tax) {
|
|
|
|
// tax_amount represents the amount of tax for the current step
|
|
|
|
var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
|
|
|
|
|
|
|
|
// Adjust divisional loss to the last item
|
|
|
|
if (tax.charge_type == "Actual") {
|
|
|
|
actual_tax_dict[tax.idx] -= current_tax_amount;
|
2023-01-25 07:40:03 +00:00
|
|
|
if (n == me.frm.doc._items.length - 1) {
|
2017-07-31 12:37:45 +00:00
|
|
|
current_tax_amount += actual_tax_dict[tax.idx];
|
2015-01-22 11:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-22 17:33:07 +00:00
|
|
|
// accumulate tax amount into tax.tax_amount
|
2015-02-22 19:36:00 +00:00
|
|
|
if (tax.charge_type != "Actual" &&
|
2017-05-30 07:24:42 +00:00
|
|
|
!(me.discount_amount_applied && me.frm.doc.apply_discount_on=="Grand Total")) {
|
|
|
|
tax.tax_amount += current_tax_amount;
|
|
|
|
}
|
2015-02-22 17:33:07 +00:00
|
|
|
|
2015-01-22 11:43:13 +00:00
|
|
|
// store tax_amount for current item as it will be used for
|
|
|
|
// charge type = 'On Previous Row Amount'
|
|
|
|
tax.tax_amount_for_current_item = current_tax_amount;
|
|
|
|
|
2015-02-22 17:33:07 +00:00
|
|
|
// tax amount after discount amount
|
2015-01-22 11:43:13 +00:00
|
|
|
tax.tax_amount_after_discount_amount += current_tax_amount;
|
|
|
|
|
|
|
|
// for buying
|
|
|
|
if(tax.category) {
|
|
|
|
// if just for valuation, do not add the tax amount in total
|
|
|
|
// hence, setting it as 0 for further steps
|
|
|
|
current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
|
|
|
|
|
|
|
|
current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// note: grand_total_for_current_item contains the contribution of
|
|
|
|
// item's amount, previously applied tax and the current tax on that item
|
|
|
|
if(i==0) {
|
2017-07-31 12:37:45 +00:00
|
|
|
tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount);
|
2015-01-22 11:43:13 +00:00
|
|
|
} else {
|
|
|
|
tax.grand_total_for_current_item =
|
2017-07-31 12:37:45 +00:00
|
|
|
flt(me.frm.doc["taxes"][i-1].grand_total_for_current_item + current_tax_amount);
|
2015-01-22 11:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set precision in the last item iteration
|
2023-01-25 07:40:03 +00:00
|
|
|
if (n == me.frm.doc._items.length - 1) {
|
2015-01-22 11:43:13 +00:00
|
|
|
me.round_off_totals(tax);
|
2021-04-14 05:50:27 +00:00
|
|
|
me.set_in_company_currency(tax,
|
|
|
|
["tax_amount", "tax_amount_after_discount_amount"]);
|
|
|
|
|
|
|
|
me.round_off_base_values(tax);
|
2015-01-22 11:43:13 +00:00
|
|
|
|
2017-07-31 12:37:45 +00:00
|
|
|
// in tax.total, accumulate grand total for each item
|
2017-08-01 11:05:22 +00:00
|
|
|
me.set_cumulative_total(i, tax);
|
2017-07-31 12:37:45 +00:00
|
|
|
|
2021-04-14 05:50:27 +00:00
|
|
|
me.set_in_company_currency(tax, ["total"]);
|
2017-07-31 12:37:45 +00:00
|
|
|
|
2015-01-22 11:43:13 +00:00
|
|
|
// adjust Discount Amount loss in last tax iteration
|
2016-04-29 11:52:42 +00:00
|
|
|
if ((i == me.frm.doc["taxes"].length - 1) && me.discount_amount_applied
|
2017-09-19 09:23:16 +00:00
|
|
|
&& me.frm.doc.apply_discount_on == "Grand Total" && me.frm.doc.discount_amount) {
|
|
|
|
me.frm.doc.rounding_adjustment = flt(me.frm.doc.grand_total -
|
|
|
|
flt(me.frm.doc.discount_amount) - tax.total, precision("rounding_adjustment"));
|
|
|
|
}
|
2015-01-22 11:43:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-31 12:37:45 +00:00
|
|
|
set_cumulative_total(row_idx, tax) {
|
2017-09-27 06:19:13 +00:00
|
|
|
var tax_amount = tax.tax_amount_after_discount_amount;
|
|
|
|
if (tax.category == 'Valuation') {
|
|
|
|
tax_amount = 0;
|
|
|
|
}
|
|
|
|
|
2017-09-21 10:45:30 +00:00
|
|
|
if (tax.add_deduct_tax == "Deduct") { tax_amount = -1*tax_amount; }
|
|
|
|
|
2017-07-31 12:37:45 +00:00
|
|
|
if(row_idx==0) {
|
2017-09-21 10:45:30 +00:00
|
|
|
tax.total = flt(this.frm.doc.net_total + tax_amount, precision("total", tax));
|
2017-07-31 12:37:45 +00:00
|
|
|
} else {
|
2017-09-21 10:45:30 +00:00
|
|
|
tax.total = flt(this.frm.doc["taxes"][row_idx-1].total + tax_amount, precision("total", tax));
|
2017-07-31 12:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 10:54:49 +00:00
|
|
|
_load_item_tax_rate(item_tax_rate) {
|
|
|
|
return item_tax_rate ? JSON.parse(item_tax_rate) : {};
|
2015-01-22 11:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get_current_tax_amount(item, tax, item_tax_map) {
|
|
|
|
var tax_rate = this._get_tax_rate(tax, item_tax_map);
|
|
|
|
var current_tax_amount = 0.0;
|
|
|
|
|
2020-06-19 09:06:56 +00:00
|
|
|
// To set row_id by default as previous row.
|
|
|
|
if(["On Previous Row Amount", "On Previous Row Total"].includes(tax.charge_type)) {
|
2020-06-19 09:40:15 +00:00
|
|
|
if (tax.idx === 1) {
|
|
|
|
frappe.throw(
|
|
|
|
__("Cannot select charge type as 'On Previous Row Amount' or 'On Previous Row Total' for first row"));
|
2020-06-19 09:06:56 +00:00
|
|
|
}
|
|
|
|
if (!tax.row_id) {
|
|
|
|
tax.row_id = tax.idx - 1;
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 11:43:13 +00:00
|
|
|
if(tax.charge_type == "Actual") {
|
|
|
|
// distribute the tax amount proportionally to each item row
|
2015-02-20 09:10:35 +00:00
|
|
|
var actual = flt(tax.tax_amount, precision("tax_amount", tax));
|
|
|
|
current_tax_amount = this.frm.doc.net_total ?
|
|
|
|
((item.net_amount / this.frm.doc.net_total) * actual) : 0.0;
|
2015-01-22 11:43:13 +00:00
|
|
|
|
|
|
|
} else if(tax.charge_type == "On Net Total") {
|
2015-02-20 09:10:35 +00:00
|
|
|
current_tax_amount = (tax_rate / 100.0) * item.net_amount;
|
2015-01-22 11:43:13 +00:00
|
|
|
} else if(tax.charge_type == "On Previous Row Amount") {
|
|
|
|
current_tax_amount = (tax_rate / 100.0) *
|
2020-06-19 09:06:56 +00:00
|
|
|
this.frm.doc["taxes"][cint(tax.row_id) - 1].tax_amount_for_current_item;
|
2015-01-22 11:43:13 +00:00
|
|
|
|
|
|
|
} else if(tax.charge_type == "On Previous Row Total") {
|
|
|
|
current_tax_amount = (tax_rate / 100.0) *
|
2020-06-19 09:06:56 +00:00
|
|
|
this.frm.doc["taxes"][cint(tax.row_id) - 1].grand_total_for_current_item;
|
2020-08-11 15:04:57 +00:00
|
|
|
} else if (tax.charge_type == "On Item Quantity") {
|
|
|
|
current_tax_amount = tax_rate * item.qty;
|
2015-01-22 11:43:13 +00:00
|
|
|
}
|
2021-02-19 09:00:23 +00:00
|
|
|
|
2021-10-19 08:35:44 +00:00
|
|
|
if (!tax.dont_recompute_tax) {
|
|
|
|
this.set_item_wise_tax(item, tax, tax_rate, current_tax_amount);
|
|
|
|
}
|
2015-01-22 11:43:13 +00:00
|
|
|
|
|
|
|
return current_tax_amount;
|
|
|
|
}
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
set_item_wise_tax(item, tax, tax_rate, current_tax_amount) {
|
|
|
|
// store tax breakup for each item
|
2018-06-11 08:01:33 +00:00
|
|
|
let tax_detail = tax.item_wise_tax_detail;
|
2018-06-18 12:10:42 +00:00
|
|
|
let key = item.item_code || item.item_name;
|
2018-06-11 08:01:33 +00:00
|
|
|
|
2021-06-04 17:23:26 +00:00
|
|
|
if(typeof (tax_detail) == "string") {
|
|
|
|
tax.item_wise_tax_detail = JSON.parse(tax.item_wise_tax_detail);
|
|
|
|
tax_detail = tax.item_wise_tax_detail;
|
|
|
|
}
|
|
|
|
|
2018-06-11 08:01:33 +00:00
|
|
|
let item_wise_tax_amount = current_tax_amount * this.frm.doc.conversion_rate;
|
|
|
|
if (tax_detail && tax_detail[key])
|
|
|
|
item_wise_tax_amount += tax_detail[key][1];
|
|
|
|
|
|
|
|
tax_detail[key] = [tax_rate, flt(item_wise_tax_amount, precision("base_tax_amount", tax))];
|
2015-02-20 09:10:35 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 10:54:49 +00:00
|
|
|
round_off_totals(tax) {
|
2021-04-14 05:50:27 +00:00
|
|
|
if (frappe.flags.round_off_applicable_accounts.includes(tax.account_head)) {
|
|
|
|
tax.tax_amount= Math.round(tax.tax_amount);
|
|
|
|
tax.tax_amount_after_discount_amount = Math.round(tax.tax_amount_after_discount_amount);
|
|
|
|
}
|
|
|
|
|
2015-01-23 10:54:49 +00:00
|
|
|
tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
|
2015-02-22 14:44:49 +00:00
|
|
|
tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, precision("tax_amount", tax));
|
2015-01-23 10:54:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 09:51:33 +00:00
|
|
|
round_off_base_values(tax) {
|
2021-04-14 05:50:27 +00:00
|
|
|
if (frappe.flags.round_off_applicable_accounts.includes(tax.account_head)) {
|
|
|
|
tax.base_tax_amount= Math.round(tax.base_tax_amount);
|
|
|
|
tax.base_tax_amount_after_discount_amount = Math.round(tax.base_tax_amount_after_discount_amount);
|
|
|
|
}
|
2021-05-07 09:51:33 +00:00
|
|
|
}
|
2021-04-14 05:50:27 +00:00
|
|
|
|
2015-03-17 05:20:47 +00:00
|
|
|
manipulate_grand_total_for_inclusive_tax() {
|
|
|
|
var me = this;
|
|
|
|
// if fully inclusive taxes and diff
|
2015-03-23 09:03:22 +00:00
|
|
|
if (this.frm.doc["taxes"] && this.frm.doc["taxes"].length) {
|
2017-09-19 09:23:16 +00:00
|
|
|
var any_inclusive_tax = false;
|
|
|
|
$.each(this.frm.doc.taxes || [], function(i, d) {
|
|
|
|
if(cint(d.included_in_print_rate)) any_inclusive_tax = true;
|
|
|
|
});
|
|
|
|
if (any_inclusive_tax) {
|
2015-03-17 05:20:47 +00:00
|
|
|
var last_tax = me.frm.doc["taxes"].slice(-1)[0];
|
2017-09-19 09:23:16 +00:00
|
|
|
var non_inclusive_tax_amount = frappe.utils.sum($.map(this.frm.doc.taxes || [],
|
|
|
|
function(d) {
|
|
|
|
if(!d.included_in_print_rate) {
|
|
|
|
return flt(d.tax_amount_after_discount_amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
));
|
|
|
|
var diff = me.frm.doc.total + non_inclusive_tax_amount
|
|
|
|
- flt(last_tax.total, precision("grand_total"));
|
2015-03-17 05:20:47 +00:00
|
|
|
|
2019-12-25 08:29:24 +00:00
|
|
|
if(me.discount_amount_applied && me.frm.doc.discount_amount) {
|
|
|
|
diff -= flt(me.frm.doc.discount_amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
diff = flt(diff, precision("rounding_adjustment"));
|
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
if ( diff && Math.abs(diff) <= (5.0 / Math.pow(10, precision("tax_amount", last_tax))) ) {
|
2019-12-25 08:29:24 +00:00
|
|
|
me.frm.doc.rounding_adjustment = diff;
|
2015-03-17 05:20:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 10:54:49 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
calculate_totals() {
|
2017-09-19 09:23:16 +00:00
|
|
|
// Changing sequence can cause rounding_adjustmentng issue and on-screen discrepency
|
2015-02-20 09:10:35 +00:00
|
|
|
var me = this;
|
|
|
|
var tax_count = this.frm.doc["taxes"] ? this.frm.doc["taxes"].length : 0;
|
2017-09-19 09:23:16 +00:00
|
|
|
this.frm.doc.grand_total = flt(tax_count
|
|
|
|
? this.frm.doc["taxes"][tax_count - 1].total + flt(this.frm.doc.rounding_adjustment)
|
|
|
|
: this.frm.doc.net_total);
|
2015-02-20 09:10:35 +00:00
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
if(in_list(["Quotation", "Sales Order", "Delivery Note", "Sales Invoice", "POS Invoice"], this.frm.doc.doctype)) {
|
2015-02-22 17:33:07 +00:00
|
|
|
this.frm.doc.base_grand_total = (this.frm.doc.total_taxes_and_charges) ?
|
2015-02-20 09:10:35 +00:00
|
|
|
flt(this.frm.doc.grand_total * this.frm.doc.conversion_rate) : this.frm.doc.base_net_total;
|
|
|
|
} else {
|
|
|
|
// other charges added/deducted
|
|
|
|
this.frm.doc.taxes_and_charges_added = this.frm.doc.taxes_and_charges_deducted = 0.0;
|
|
|
|
if(tax_count) {
|
|
|
|
$.each(this.frm.doc["taxes"] || [], function(i, tax) {
|
2015-02-22 14:44:49 +00:00
|
|
|
if (in_list(["Valuation and Total", "Total"], tax.category)) {
|
2015-02-20 09:10:35 +00:00
|
|
|
if(tax.add_deduct_tax == "Add") {
|
2015-07-31 11:23:13 +00:00
|
|
|
me.frm.doc.taxes_and_charges_added += flt(tax.tax_amount_after_discount_amount);
|
2015-02-20 09:10:35 +00:00
|
|
|
} else {
|
2015-07-31 11:23:13 +00:00
|
|
|
me.frm.doc.taxes_and_charges_deducted += flt(tax.tax_amount_after_discount_amount);
|
2015-02-20 09:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-31 12:37:45 +00:00
|
|
|
});
|
2015-02-20 09:10:35 +00:00
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
frappe.model.round_floats_in(this.frm.doc,
|
|
|
|
["taxes_and_charges_added", "taxes_and_charges_deducted"]);
|
2015-02-20 09:10:35 +00:00
|
|
|
}
|
|
|
|
|
2015-02-22 17:33:07 +00:00
|
|
|
this.frm.doc.base_grand_total = flt((this.frm.doc.taxes_and_charges_added || this.frm.doc.taxes_and_charges_deducted) ?
|
2015-02-20 09:10:35 +00:00
|
|
|
flt(this.frm.doc.grand_total * this.frm.doc.conversion_rate) : this.frm.doc.base_net_total);
|
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
this.set_in_company_currency(this.frm.doc,
|
|
|
|
["taxes_and_charges_added", "taxes_and_charges_deducted"]);
|
2015-02-20 09:10:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
this.frm.doc.total_taxes_and_charges = flt(this.frm.doc.grand_total - this.frm.doc.net_total
|
|
|
|
- flt(this.frm.doc.rounding_adjustment), precision("total_taxes_and_charges"));
|
2015-02-20 09:10:35 +00:00
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
this.set_in_company_currency(this.frm.doc, ["total_taxes_and_charges", "rounding_adjustment"]);
|
2015-02-22 17:33:07 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
// Round grand total as per precision
|
|
|
|
frappe.model.round_floats_in(this.frm.doc, ["grand_total", "base_grand_total"]);
|
|
|
|
|
|
|
|
// rounded totals
|
2017-11-17 06:57:43 +00:00
|
|
|
this.set_rounded_total();
|
2017-09-19 09:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_rounded_total() {
|
2017-11-17 06:57:43 +00:00
|
|
|
var disable_rounded_total = 0;
|
|
|
|
if(frappe.meta.get_docfield(this.frm.doc.doctype, "disable_rounded_total", this.frm.doc.name)) {
|
|
|
|
disable_rounded_total = this.frm.doc.disable_rounded_total;
|
|
|
|
} else if (frappe.sys_defaults.disable_rounded_total) {
|
|
|
|
disable_rounded_total = frappe.sys_defaults.disable_rounded_total;
|
|
|
|
}
|
2018-01-12 10:58:16 +00:00
|
|
|
|
|
|
|
if (cint(disable_rounded_total)) {
|
2017-11-17 06:57:43 +00:00
|
|
|
this.frm.doc.rounded_total = 0;
|
|
|
|
this.frm.doc.base_rounded_total = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
if(frappe.meta.get_docfield(this.frm.doc.doctype, "rounded_total", this.frm.doc.name)) {
|
2016-04-29 11:52:42 +00:00
|
|
|
this.frm.doc.rounded_total = round_based_on_smallest_currency_fraction(this.frm.doc.grand_total,
|
2016-01-20 09:16:26 +00:00
|
|
|
this.frm.doc.currency, precision("rounded_total"));
|
2017-11-17 06:57:43 +00:00
|
|
|
this.frm.doc.rounding_adjustment += flt(this.frm.doc.rounded_total - this.frm.doc.grand_total,
|
|
|
|
precision("rounding_adjustment"));
|
|
|
|
|
2017-11-22 10:42:20 +00:00
|
|
|
this.set_in_company_currency(this.frm.doc, ["rounding_adjustment", "rounded_total"]);
|
2015-02-20 09:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 10:54:49 +00:00
|
|
|
|
2015-01-22 11:43:13 +00:00
|
|
|
_cleanup() {
|
2015-02-20 09:10:35 +00:00
|
|
|
this.frm.doc.base_in_words = this.frm.doc.in_words = "";
|
2023-01-25 07:40:03 +00:00
|
|
|
let items = this.frm.doc._items;
|
2015-01-22 11:43:13 +00:00
|
|
|
|
2023-01-25 07:40:03 +00:00
|
|
|
if(items && items.length) {
|
|
|
|
if(!frappe.meta.get_docfield(items[0].doctype, "item_tax_amount", this.frm.doctype)) {
|
|
|
|
$.each(items || [], function(i, item) {
|
2015-01-22 11:43:13 +00:00
|
|
|
delete item["item_tax_amount"];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.frm.doc["taxes"] && this.frm.doc["taxes"].length) {
|
|
|
|
var temporary_fields = ["tax_amount_for_current_item", "grand_total_for_current_item",
|
2017-07-31 12:37:45 +00:00
|
|
|
"tax_fraction_for_current_item", "grand_total_fraction_for_current_item"];
|
2015-01-22 11:43:13 +00:00
|
|
|
|
|
|
|
if(!frappe.meta.get_docfield(this.frm.doc["taxes"][0].doctype, "tax_amount_after_discount_amount", this.frm.doctype)) {
|
|
|
|
temporary_fields.push("tax_amount_after_discount_amount");
|
|
|
|
}
|
|
|
|
|
|
|
|
$.each(this.frm.doc["taxes"] || [], function(i, tax) {
|
|
|
|
$.each(temporary_fields, function(i, fieldname) {
|
|
|
|
delete tax[fieldname];
|
|
|
|
});
|
|
|
|
|
2021-10-19 08:35:44 +00:00
|
|
|
if (!tax.dont_recompute_tax) {
|
|
|
|
tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
|
|
|
|
}
|
2015-01-22 11:43:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 09:56:01 +00:00
|
|
|
set_discount_amount() {
|
|
|
|
if(this.frm.doc.additional_discount_percentage) {
|
|
|
|
this.frm.doc.discount_amount = flt(flt(this.frm.doc[frappe.scrub(this.frm.doc.apply_discount_on)])
|
|
|
|
* this.frm.doc.additional_discount_percentage / 100, precision("discount_amount"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:43:13 +00:00
|
|
|
apply_discount_amount() {
|
|
|
|
var me = this;
|
|
|
|
var distributed_amount = 0.0;
|
2016-04-09 09:01:09 +00:00
|
|
|
this.frm.doc.base_discount_amount = 0.0;
|
2015-01-22 11:43:13 +00:00
|
|
|
|
|
|
|
if (this.frm.doc.discount_amount) {
|
2015-02-23 10:31:33 +00:00
|
|
|
if(!this.frm.doc.apply_discount_on)
|
|
|
|
frappe.throw(__("Please select Apply Discount On"));
|
2016-06-15 11:15:03 +00:00
|
|
|
|
|
|
|
this.frm.doc.base_discount_amount = flt(this.frm.doc.discount_amount * this.frm.doc.conversion_rate,
|
2016-04-09 09:01:09 +00:00
|
|
|
precision("base_discount_amount"));
|
2015-01-22 11:43:13 +00:00
|
|
|
|
2022-08-21 12:21:05 +00:00
|
|
|
if (this.frm.doc.apply_discount_on == "Grand Total" && this.frm.doc.is_cash_or_non_trade_discount) {
|
2022-08-23 03:42:20 +00:00
|
|
|
return;
|
2022-08-21 12:21:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
var total_for_discount_amount = this.get_total_for_discount_amount();
|
2017-07-31 12:37:45 +00:00
|
|
|
var net_total = 0;
|
2015-01-22 11:43:13 +00:00
|
|
|
// calculate item amount after Discount Amount
|
2015-02-20 09:10:35 +00:00
|
|
|
if (total_for_discount_amount) {
|
2023-01-25 07:40:03 +00:00
|
|
|
$.each(this.frm.doc._items || [], function(i, item) {
|
2015-02-20 09:10:35 +00:00
|
|
|
distributed_amount = flt(me.frm.doc.discount_amount) * item.net_amount / total_for_discount_amount;
|
2017-07-31 12:37:45 +00:00
|
|
|
item.net_amount = flt(item.net_amount - distributed_amount,
|
|
|
|
precision("base_amount", item));
|
|
|
|
net_total += item.net_amount;
|
|
|
|
|
|
|
|
// discount amount rounding loss adjustment if no taxes
|
2019-05-30 10:20:46 +00:00
|
|
|
if ((!(me.frm.doc.taxes || []).length || total_for_discount_amount==me.frm.doc.net_total || (me.frm.doc.apply_discount_on == "Net Total"))
|
2023-01-25 07:40:03 +00:00
|
|
|
&& i == (me.frm.doc._items || []).length - 1) {
|
2017-07-31 12:37:45 +00:00
|
|
|
var discount_amount_loss = flt(me.frm.doc.net_total - net_total
|
|
|
|
- me.frm.doc.discount_amount, precision("net_total"));
|
|
|
|
item.net_amount = flt(item.net_amount + discount_amount_loss,
|
|
|
|
precision("net_amount", item));
|
|
|
|
}
|
2018-10-18 12:27:56 +00:00
|
|
|
item.net_rate = item.qty ? flt(item.net_amount / item.qty, precision("net_rate", item)) : 0;
|
2015-02-20 09:10:35 +00:00
|
|
|
me.set_in_company_currency(item, ["net_rate", "net_amount"]);
|
2015-01-22 11:43:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.discount_amount_applied = true;
|
|
|
|
this._calculate_taxes_and_totals();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
get_total_for_discount_amount() {
|
2015-02-22 19:36:00 +00:00
|
|
|
if(this.frm.doc.apply_discount_on == "Net Total") {
|
2017-07-31 12:37:45 +00:00
|
|
|
return this.frm.doc.net_total;
|
2015-02-20 09:10:35 +00:00
|
|
|
} else {
|
|
|
|
var total_actual_tax = 0.0;
|
|
|
|
var actual_taxes_dict = {};
|
2015-01-22 11:43:13 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
$.each(this.frm.doc["taxes"] || [], function(i, tax) {
|
2020-08-11 15:04:57 +00:00
|
|
|
if (in_list(["Actual", "On Item Quantity"], tax.charge_type)) {
|
2017-12-12 13:20:05 +00:00
|
|
|
var tax_amount = (tax.category == "Valuation") ? 0.0 : tax.tax_amount;
|
|
|
|
tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
|
|
|
|
actual_taxes_dict[tax.idx] = tax_amount;
|
|
|
|
} else if (actual_taxes_dict[tax.row_id] !== null) {
|
2017-05-30 07:24:42 +00:00
|
|
|
var actual_tax_amount = flt(actual_taxes_dict[tax.row_id]) * flt(tax.rate) / 100;
|
2015-02-20 09:10:35 +00:00
|
|
|
actual_taxes_dict[tax.idx] = actual_tax_amount;
|
|
|
|
}
|
|
|
|
});
|
2015-01-22 11:43:13 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
$.each(actual_taxes_dict, function(key, value) {
|
2015-02-22 17:33:07 +00:00
|
|
|
if (value) total_actual_tax += value;
|
2015-02-20 09:10:35 +00:00
|
|
|
});
|
2020-08-19 09:29:46 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
return flt(this.frm.doc.grand_total - total_actual_tax, precision("grand_total"));
|
|
|
|
}
|
2015-01-22 11:43:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 10:54:49 +00:00
|
|
|
calculate_total_advance(update_paid_amount) {
|
2015-02-20 09:10:35 +00:00
|
|
|
var total_allocated_amount = frappe.utils.sum($.map(this.frm.doc["advances"] || [], function(adv) {
|
2017-07-31 12:37:45 +00:00
|
|
|
return flt(adv.allocated_amount, precision("allocated_amount", adv));
|
2015-02-20 09:10:35 +00:00
|
|
|
}));
|
|
|
|
this.frm.doc.total_advance = flt(total_allocated_amount, precision("total_advance"));
|
2015-01-22 11:43:13 +00:00
|
|
|
|
2022-03-25 12:32:14 +00:00
|
|
|
if (this.frm.doc.write_off_outstanding_amount_automatically) {
|
|
|
|
this.frm.doc.write_off_amount = 0;
|
|
|
|
}
|
|
|
|
|
2015-01-23 10:54:49 +00:00
|
|
|
this.calculate_outstanding_amount(update_paid_amount);
|
2022-03-24 07:26:22 +00:00
|
|
|
this.calculate_write_off_amount();
|
2015-08-27 06:58:36 +00:00
|
|
|
}
|
2016-04-29 11:52:42 +00:00
|
|
|
|
2020-12-11 16:00:39 +00:00
|
|
|
is_internal_invoice() {
|
|
|
|
if (['Sales Invoice', 'Purchase Invoice'].includes(this.frm.doc.doctype)) {
|
|
|
|
if (this.frm.doc.company === this.frm.doc.represents_company) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-27 06:58:36 +00:00
|
|
|
calculate_outstanding_amount(update_paid_amount) {
|
|
|
|
// NOTE:
|
2018-07-06 07:06:57 +00:00
|
|
|
// paid_amount and write_off_amount is only for POS/Loyalty Point Redemption Invoice
|
2015-08-27 06:58:36 +00:00
|
|
|
// total_advance is only for non POS Invoice
|
2020-07-23 13:21:26 +00:00
|
|
|
if(in_list(["Sales Invoice", "POS Invoice"], this.frm.doc.doctype) && this.frm.doc.is_return){
|
2017-07-31 12:37:45 +00:00
|
|
|
this.calculate_paid_amount();
|
2016-08-24 20:39:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 16:00:39 +00:00
|
|
|
if (this.frm.doc.is_return || (this.frm.doc.docstatus > 0) || this.is_internal_invoice()) return;
|
2016-04-29 11:52:42 +00:00
|
|
|
|
2015-08-27 06:58:36 +00:00
|
|
|
frappe.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount"]);
|
2016-04-29 11:52:42 +00:00
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
if(in_list(["Sales Invoice", "POS Invoice", "Purchase Invoice"], this.frm.doc.doctype)) {
|
2022-01-06 13:28:49 +00:00
|
|
|
let grand_total = this.frm.doc.rounded_total || this.frm.doc.grand_total;
|
|
|
|
let base_grand_total = this.frm.doc.base_rounded_total || this.frm.doc.base_grand_total;
|
2016-04-29 11:52:42 +00:00
|
|
|
|
2017-11-17 06:57:43 +00:00
|
|
|
if(this.frm.doc.party_account_currency == this.frm.doc.currency) {
|
|
|
|
var total_amount_to_pay = flt((grand_total - this.frm.doc.total_advance
|
|
|
|
- this.frm.doc.write_off_amount), precision("grand_total"));
|
|
|
|
} else {
|
|
|
|
var total_amount_to_pay = flt(
|
2022-01-06 13:28:49 +00:00
|
|
|
(flt(base_grand_total, precision("base_grand_total"))
|
2017-11-17 06:57:43 +00:00
|
|
|
- this.frm.doc.total_advance - this.frm.doc.base_write_off_amount),
|
|
|
|
precision("base_grand_total")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
frappe.model.round_floats_in(this.frm.doc, ["paid_amount"]);
|
2015-08-27 06:58:36 +00:00
|
|
|
this.set_in_company_currency(this.frm.doc, ["paid_amount"]);
|
2016-04-09 09:01:09 +00:00
|
|
|
|
|
|
|
if(this.frm.refresh_field){
|
|
|
|
this.frm.refresh_field("paid_amount");
|
|
|
|
this.frm.refresh_field("base_paid_amount");
|
|
|
|
}
|
2016-06-15 11:15:03 +00:00
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
if(in_list(["Sales Invoice", "POS Invoice"], this.frm.doc.doctype)) {
|
2018-07-06 07:06:57 +00:00
|
|
|
let total_amount_for_payment = (this.frm.doc.redeem_loyalty_points && this.frm.doc.loyalty_amount)
|
|
|
|
? flt(total_amount_to_pay - this.frm.doc.loyalty_amount, precision("base_grand_total"))
|
|
|
|
: total_amount_to_pay;
|
|
|
|
this.set_default_payment(total_amount_for_payment, update_paid_amount);
|
2017-07-31 12:37:45 +00:00
|
|
|
this.calculate_paid_amount();
|
2016-04-09 09:01:09 +00:00
|
|
|
}
|
2017-07-31 12:37:45 +00:00
|
|
|
this.calculate_change_amount();
|
2016-04-29 11:52:42 +00:00
|
|
|
|
|
|
|
var paid_amount = (this.frm.doc.party_account_currency == this.frm.doc.currency) ?
|
2015-11-13 07:33:10 +00:00
|
|
|
this.frm.doc.paid_amount : this.frm.doc.base_paid_amount;
|
2016-08-30 20:34:37 +00:00
|
|
|
this.frm.doc.outstanding_amount = flt(total_amount_to_pay - flt(paid_amount) +
|
2016-08-29 19:11:33 +00:00
|
|
|
flt(this.frm.doc.change_amount * this.frm.doc.conversion_rate), precision("outstanding_amount"));
|
2017-07-31 12:37:45 +00:00
|
|
|
}
|
2016-04-09 09:01:09 +00:00
|
|
|
}
|
2016-06-15 11:15:03 +00:00
|
|
|
|
2021-08-17 05:23:19 +00:00
|
|
|
set_total_amount_to_default_mop() {
|
2022-01-06 13:28:49 +00:00
|
|
|
let grand_total = this.frm.doc.rounded_total || this.frm.doc.grand_total;
|
|
|
|
let base_grand_total = this.frm.doc.base_rounded_total || this.frm.doc.base_grand_total;
|
2020-02-25 07:51:16 +00:00
|
|
|
|
2022-11-23 12:22:12 +00:00
|
|
|
if (this.frm.doc.party_account_currency == this.frm.doc.currency) {
|
|
|
|
var total_amount_to_pay = flt(
|
|
|
|
grand_total - this.frm.doc.total_advance - this.frm.doc.write_off_amount,
|
|
|
|
precision("grand_total")
|
|
|
|
);
|
2020-02-25 07:51:16 +00:00
|
|
|
} else {
|
|
|
|
var total_amount_to_pay = flt(
|
2022-11-23 12:22:12 +00:00
|
|
|
(
|
|
|
|
flt(
|
|
|
|
base_grand_total,
|
|
|
|
precision("base_grand_total")
|
|
|
|
)
|
|
|
|
- this.frm.doc.total_advance - this.frm.doc.base_write_off_amount
|
|
|
|
),
|
2020-02-25 07:51:16 +00:00
|
|
|
precision("base_grand_total")
|
|
|
|
);
|
|
|
|
}
|
2022-11-23 12:22:12 +00:00
|
|
|
|
2020-09-10 13:58:46 +00:00
|
|
|
this.frm.doc.payments.find(pay => {
|
|
|
|
if (pay.default) {
|
|
|
|
pay.amount = total_amount_to_pay;
|
|
|
|
}
|
|
|
|
});
|
2022-11-23 12:22:12 +00:00
|
|
|
|
2020-09-10 13:58:46 +00:00
|
|
|
this.frm.refresh_fields();
|
2020-02-25 07:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_default_payment(total_amount_to_pay, update_paid_amount) {
|
2016-07-19 14:34:44 +00:00
|
|
|
var me = this;
|
2017-05-30 07:24:42 +00:00
|
|
|
var payment_status = true;
|
2020-02-25 07:51:16 +00:00
|
|
|
if(this.frm.doc.is_pos && (update_paid_amount===undefined || update_paid_amount)) {
|
|
|
|
$.each(this.frm.doc['payments'] || [], function(index, data) {
|
2017-07-24 12:23:10 +00:00
|
|
|
if(data.default && payment_status && total_amount_to_pay > 0) {
|
2022-05-25 06:10:49 +00:00
|
|
|
let base_amount, amount;
|
|
|
|
|
|
|
|
if (me.frm.doc.party_account_currency == me.frm.doc.currency) {
|
|
|
|
// if customer/supplier currency is same as company currency
|
|
|
|
// total_amount_to_pay is already in customer/supplier currency
|
|
|
|
// so base_amount has to be calculated using total_amount_to_pay
|
|
|
|
base_amount = flt(total_amount_to_pay * me.frm.doc.conversion_rate, precision("base_amount", data));
|
|
|
|
amount = flt(total_amount_to_pay, precision("amount", data));
|
|
|
|
} else {
|
|
|
|
base_amount = flt(total_amount_to_pay, precision("base_amount", data));
|
|
|
|
amount = flt(total_amount_to_pay / me.frm.doc.conversion_rate, precision("amount", data));
|
|
|
|
}
|
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
frappe.model.set_value(data.doctype, data.name, "base_amount", base_amount);
|
|
|
|
frappe.model.set_value(data.doctype, data.name, "amount", amount);
|
2016-07-19 14:34:44 +00:00
|
|
|
payment_status = false;
|
2022-05-25 06:10:49 +00:00
|
|
|
|
2020-02-25 07:51:16 +00:00
|
|
|
} else if(me.frm.doc.paid_amount) {
|
2020-07-23 13:21:26 +00:00
|
|
|
frappe.model.set_value(data.doctype, data.name, "amount", 0.0);
|
2016-07-19 14:34:44 +00:00
|
|
|
}
|
2017-07-31 12:37:45 +00:00
|
|
|
});
|
2016-07-19 14:34:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 07:51:16 +00:00
|
|
|
calculate_paid_amount() {
|
2016-04-09 09:01:09 +00:00
|
|
|
var me = this;
|
2017-05-30 07:24:42 +00:00
|
|
|
var paid_amount = 0.0;
|
|
|
|
var base_paid_amount = 0.0;
|
2017-01-17 06:41:57 +00:00
|
|
|
if(this.frm.doc.is_pos) {
|
|
|
|
$.each(this.frm.doc['payments'] || [], function(index, data){
|
2020-07-23 13:21:26 +00:00
|
|
|
data.base_amount = flt(data.amount * me.frm.doc.conversion_rate, precision("base_amount", data));
|
2017-01-17 06:41:57 +00:00
|
|
|
paid_amount += data.amount;
|
|
|
|
base_paid_amount += data.base_amount;
|
2017-07-31 12:37:45 +00:00
|
|
|
});
|
2017-05-16 05:59:57 +00:00
|
|
|
} else if(!this.frm.doc.is_return){
|
|
|
|
this.frm.doc.payments = [];
|
2017-01-17 06:41:57 +00:00
|
|
|
}
|
2018-07-06 07:06:57 +00:00
|
|
|
if (this.frm.doc.redeem_loyalty_points && this.frm.doc.loyalty_amount) {
|
|
|
|
base_paid_amount += this.frm.doc.loyalty_amount;
|
|
|
|
paid_amount += flt(this.frm.doc.loyalty_amount / me.frm.doc.conversion_rate, precision("paid_amount"));
|
|
|
|
}
|
2016-06-15 11:15:03 +00:00
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
this.frm.set_value('paid_amount', flt(paid_amount, precision("paid_amount")));
|
|
|
|
this.frm.set_value('base_paid_amount', flt(base_paid_amount, precision("base_paid_amount")));
|
2016-04-09 09:01:09 +00:00
|
|
|
}
|
2016-06-15 11:15:03 +00:00
|
|
|
|
2016-04-09 09:01:09 +00:00
|
|
|
calculate_change_amount(){
|
2016-08-02 11:11:10 +00:00
|
|
|
this.frm.doc.change_amount = 0.0;
|
2017-05-30 10:05:01 +00:00
|
|
|
this.frm.doc.base_change_amount = 0.0;
|
2020-07-23 13:21:26 +00:00
|
|
|
if(in_list(["Sales Invoice", "POS Invoice"], this.frm.doc.doctype)
|
2017-11-17 06:57:43 +00:00
|
|
|
&& this.frm.doc.paid_amount > this.frm.doc.grand_total && !this.frm.doc.is_return) {
|
|
|
|
|
2017-07-31 12:37:45 +00:00
|
|
|
var payment_types = $.map(this.frm.doc.payments, function(d) { return d.type; });
|
2017-05-30 10:05:01 +00:00
|
|
|
if (in_list(payment_types, 'Cash')) {
|
2018-02-05 12:43:29 +00:00
|
|
|
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;
|
|
|
|
|
2022-03-24 07:26:22 +00:00
|
|
|
this.frm.doc.change_amount = flt(this.frm.doc.paid_amount - grand_total,
|
|
|
|
precision("change_amount"));
|
2017-07-31 12:37:45 +00:00
|
|
|
|
|
|
|
this.frm.doc.base_change_amount = flt(this.frm.doc.base_paid_amount -
|
2022-03-24 07:26:22 +00:00
|
|
|
base_grand_total, precision("base_change_amount"));
|
2017-05-30 10:05:01 +00:00
|
|
|
}
|
2016-04-09 09:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-04 09:26:15 +00:00
|
|
|
|
2022-03-27 13:48:32 +00:00
|
|
|
calculate_write_off_amount() {
|
2022-03-27 13:50:37 +00:00
|
|
|
if(this.frm.doc.write_off_outstanding_amount_automatically) {
|
2022-03-25 12:32:14 +00:00
|
|
|
this.frm.doc.write_off_amount = flt(this.frm.doc.outstanding_amount, precision("write_off_amount"));
|
2016-08-05 10:11:36 +00:00
|
|
|
this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate,
|
|
|
|
precision("base_write_off_amount"));
|
2022-03-25 12:32:14 +00:00
|
|
|
|
|
|
|
this.calculate_outstanding_amount(false);
|
2016-08-05 10:11:36 +00:00
|
|
|
}
|
2022-03-24 07:26:22 +00:00
|
|
|
|
2016-08-04 09:26:15 +00:00
|
|
|
}
|
2023-01-25 07:40:03 +00:00
|
|
|
|
|
|
|
filtered_items() {
|
2023-01-26 09:06:25 +00:00
|
|
|
return this.frm.doc.items.filter(item => !item["is_alternative"]);
|
2023-01-25 07:40:03 +00:00
|
|
|
}
|
2017-08-01 10:50:59 +00:00
|
|
|
};
|