blob: a07f75d1c5d418b871586c304a54eed0ce15824a [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Nabin Haitce245122015-02-22 20:14:49 +05302// License: GNU General Public License v3. See license.txt
3
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +05304// get tax rate
Rushabh Mehta2a21bc92015-02-25 15:08:42 +05305frappe.provide("erpnext.taxes");
6frappe.provide("erpnext.taxes.flags");
7
Rushabh Mehtac712cdb2015-05-03 22:20:44 +05308frappe.ui.form.on(cur_frm.doctype, {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +05309 setup: function(frm) {
10 // set conditional display for rate column in taxes
11 $(frm.wrapper).on('grid-row-render', function(e, grid_row) {
12 if(in_list(['Sales Taxes and Charges', 'Purchase Taxes and Charges'], grid_row.doc.doctype)) {
13 erpnext.taxes.set_conditional_mandatory_rate_or_amount(grid_row);
14 }
15 });
16 },
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053017 onload: function(frm) {
18 if(frm.get_field("taxes")) {
19 frm.set_query("account_head", "taxes", function(doc) {
20 if(frm.cscript.tax_table == "Sales Taxes and Charges") {
21 var account_type = ["Tax", "Chargeable", "Expense Account"];
22 } else {
Shreya Shahdcbc4282017-11-27 11:48:09 +053023 var account_type = ["Tax", "Chargeable", "Income Account", "Expenses Included In Valuation"];
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053024 }
25
26 return {
27 query: "erpnext.controllers.queries.tax_account_query",
28 filters: {
29 "account_type": account_type,
Saqib Ansaria1e3ae82022-05-11 13:01:06 +053030 "company": doc.company,
31 "disabled": 0
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053032 }
33 }
34 });
Anuja Pawar0e337be2021-08-10 17:26:35 +053035 frm.set_query("cost_center", "taxes", function(doc) {
36 return {
37 filters: {
38 "company": doc.company,
39 "is_group": 0
40 }
41 };
42 });
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053043 }
44 },
45 validate: function(frm) {
46 // neither is absolutely mandatory
Nabin Hait0e1540b2015-05-05 17:26:35 +053047 if(frm.get_docfield("taxes")) {
48 frm.get_docfield("taxes", "rate").reqd = 0;
49 frm.get_docfield("taxes", "tax_amount").reqd = 0;
50 }
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +053051
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053052 },
53 taxes_on_form_rendered: function(frm) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +053054 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.open_grid_row());
Nabin Hait041a5c22018-08-01 18:07:39 +053055 },
56
57 allocate_advances_automatically: function(frm) {
58 if(frm.doc.allocate_advances_automatically) {
59 frappe.call({
60 doc: frm.doc,
61 method: "set_advances",
62 callback: function(r, rt) {
63 refresh_field("advances");
64 }
65 })
66 }
marination6ef057a2019-11-18 15:55:32 +053067 }
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053068});
69
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053070frappe.ui.form.on('Sales Invoice Payment', {
71 mode_of_payment: function(frm, cdt, cdn) {
72 var d = locals[cdt][cdn];
73 get_payment_mode_account(frm, d.mode_of_payment, function(account){
74 frappe.model.set_value(cdt, cdn, 'account', account)
75 })
76 }
tundee7da1c52017-09-06 12:36:45 +010077});
78
79frappe.ui.form.on("Sales Invoice", {
80 payment_terms_template: function() {
81 cur_frm.trigger("disable_due_date");
82 }
83});
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053084
85frappe.ui.form.on('Purchase Invoice', {
86 mode_of_payment: function(frm) {
87 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
88 frm.set_value('cash_bank_account', account);
89 })
tundee7da1c52017-09-06 12:36:45 +010090 },
91
92 payment_terms_template: function() {
93 cur_frm.trigger("disable_due_date");
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053094 }
tundee7da1c52017-09-06 12:36:45 +010095});
96
97frappe.ui.form.on("Payment Schedule", {
98 payment_schedule_remove: function() {
99 cur_frm.trigger("disable_due_date");
100 },
101
102});
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530103
104frappe.ui.form.on('Payment Entry', {
105 mode_of_payment: function(frm) {
106 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
107 var payment_account_field = frm.doc.payment_type == "Receive" ? "paid_to" : "paid_from";
108 frm.set_value(payment_account_field, account);
109 })
110 }
111})
112
Kanchan Chauhanb40c0a92016-08-26 11:11:17 +0530113frappe.ui.form.on('Salary Structure', {
114 mode_of_payment: function(frm) {
115 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
116 frm.set_value("payment_account", account);
117 })
118 }
119})
120
Makarand Bauskar8f507a92017-07-13 11:44:29 +0530121var get_payment_mode_account = function(frm, mode_of_payment, callback) {
122 if(!frm.doc.company) {
marination8370d9b2020-09-24 11:51:03 +0530123 frappe.throw({message:__("Please select a Company first."), title: __("Mandatory")});
Makarand Bauskar8f507a92017-07-13 11:44:29 +0530124 }
125
Makarand Bauskarad7eb9d2017-07-14 17:31:36 +0530126 if(!mode_of_payment) {
127 return;
128 }
129
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530130 return frappe.call({
131 method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account",
132 args: {
133 "mode_of_payment": mode_of_payment,
134 "company": frm.doc.company
135 },
136 callback: function(r, rt) {
137 if(r.message) {
138 callback(r.message.account)
139 }
140 }
141 });
142}
143
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530144cur_frm.cscript.account_head = function(doc, cdt, cdn) {
145 var d = locals[cdt][cdn];
Deepesh Garge6261072022-10-17 16:48:13 +0530146
147 if (doc.docstatus == 1) {
148 // Should not trigger any changes on change post submit
149 return;
150 }
151
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530152 if(!d.charge_type && d.account_head){
Nabin Haite6d65bc2018-02-14 17:44:06 +0530153 frappe.msgprint(__("Please select Charge Type first"));
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530154 frappe.model.set_value(cdt, cdn, "account_head", "");
pateljannat7e4d1152020-11-11 09:52:27 +0530155 } else if (d.account_head) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530156 frappe.call({
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530157 type:"GET",
Nabin Haitce245122015-02-22 20:14:49 +0530158 method: "erpnext.controllers.accounts_controller.get_tax_rate",
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530159 args: {"account_head":d.account_head},
160 callback: function(r) {
pateljannat7e4d1152020-11-11 09:52:27 +0530161 if (d.charge_type!=="Actual") {
pateljannat403822a2020-11-05 16:24:33 +0530162 frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0);
pateljannat5b02d322020-11-05 18:04:14 +0530163 }
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530164 frappe.model.set_value(cdt, cdn, "description", r.message.account_name);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530165 }
166 })
167 }
168}
Nabin Haitce245122015-02-22 20:14:49 +0530169
Nabin Hait613d0812015-02-23 11:58:15 +0530170cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530171 var d = locals[cdt][cdn];
Nabin Hait613d0812015-02-23 11:58:15 +0530172 var msg = "";
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530173
Deepesh Gargfd380d32021-05-13 14:04:51 +0530174 if (d.account_head && !d.description) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530175 // set description from account head
176 d.description = d.account_head.split(' - ').slice(0, -1).join(' - ');
177 }
178
Deepesh Gargfd380d32021-05-13 14:04:51 +0530179 if (!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
Nabin Hait613d0812015-02-23 11:58:15 +0530180 msg = __("Please select Charge Type first");
Nabin Hait2b019ed2015-02-22 23:03:07 +0530181 d.row_id = "";
182 d.rate = d.tax_amount = 0.0;
Deepesh Gargfd380d32021-05-13 14:04:51 +0530183 } else if ((d.charge_type == 'Actual' || d.charge_type == 'On Net Total' || d.charge_type == 'On Paid Amount') && d.row_id) {
Nabin Hait613d0812015-02-23 11:58:15 +0530184 msg = __("Can refer row only if the charge type is 'On Previous Row Amount' or 'Previous Row Total'");
Nabin Haitce245122015-02-22 20:14:49 +0530185 d.row_id = "";
Deepesh Gargfd380d32021-05-13 14:04:51 +0530186 } else if ((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
Nabin Haitce245122015-02-22 20:14:49 +0530187 if (d.idx == 1) {
Nabin Hait613d0812015-02-23 11:58:15 +0530188 msg = __("Cannot select charge type as 'On Previous Row Amount' or 'On Previous Row Total' for first row");
Nabin Haitce245122015-02-22 20:14:49 +0530189 d.charge_type = '';
Nabin Hait613d0812015-02-23 11:58:15 +0530190 } else if (!d.row_id) {
191 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
192 d.row_id = "";
Deepesh Gargfd380d32021-05-13 14:04:51 +0530193 } else if (d.row_id && d.row_id >= d.idx) {
Nabin Hait613d0812015-02-23 11:58:15 +0530194 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
Nabin Haitce245122015-02-22 20:14:49 +0530195 d.row_id = "";
196 }
197 }
Deepesh Gargfd380d32021-05-13 14:04:51 +0530198 if (msg) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530199 frappe.validated = false;
Nabin Hait613d0812015-02-23 11:58:15 +0530200 refresh_field("taxes");
201 frappe.throw(msg);
202 }
203
204}
205
206cur_frm.cscript.validate_inclusive_tax = function(tax) {
207 var actual_type_error = function() {
208 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
209 frappe.throw(msg);
210 };
211
212 var on_previous_row_error = function(row_range) {
213 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
214 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
215 frappe.throw(msg);
216 };
217
218 if(cint(tax.included_in_print_rate)) {
219 if(tax.charge_type == "Actual") {
220 // inclusive tax cannot be of type Actual
221 actual_type_error();
222 } else if(tax.charge_type == "On Previous Row Amount" &&
Faris Ansariab74ca72017-05-30 12:54:42 +0530223 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)
224 ) {
225 // referred row should also be an inclusive tax
226 on_previous_row_error(tax.row_id);
Nabin Hait613d0812015-02-23 11:58:15 +0530227 } else if(tax.charge_type == "On Previous Row Total") {
228 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
229 function(t) { return cint(t.included_in_print_rate) ? null : t; });
230 if(taxes_not_included.length > 0) {
231 // all rows above this tax should be inclusive
232 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
233 }
Nabin Hait72c359a2015-02-24 16:05:19 +0530234 } else if(tax.category == "Valuation") {
235 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Nabin Hait613d0812015-02-23 11:58:15 +0530236 }
237 }
Nabin Haitce245122015-02-22 20:14:49 +0530238}
239
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530240if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
241 erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
Nabin Haitce245122015-02-22 20:14:49 +0530242
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530243 frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
Nabin Hait613d0812015-02-23 11:58:15 +0530244 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530245 });
246
247 frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
248 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
249 });
250
251 frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
252 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
253 });
254
255 frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530256 frm.cscript.validate_taxes_and_charges(cdt, cdn);
257 var open_form = frm.open_grid_row();
258 if(open_form) {
259 erpnext.taxes.set_conditional_mandatory_rate_or_amount(open_form);
260 } else {
261 // apply in current row
Rushabh Mehta43ef4e92017-07-03 11:53:07 +0530262 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_row(cdn));
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530263 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530264 });
265
266 frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
267 var tax = frappe.get_doc(cdt, cdn);
268 try {
269 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
270 cur_frm.cscript.validate_inclusive_tax(tax);
271 } catch(e) {
272 tax.included_in_print_rate = 0;
273 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
274 throw e;
275 }
276 });
277}
278
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530279erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(grid_row) {
280 if(grid_row) {
281 if(grid_row.doc.charge_type==="Actual") {
282 grid_row.toggle_editable("tax_amount", true);
283 grid_row.toggle_reqd("tax_amount", true);
284 grid_row.toggle_editable("rate", false);
285 grid_row.toggle_reqd("rate", false);
286 } else {
287 grid_row.toggle_editable("rate", true);
288 grid_row.toggle_reqd("rate", true);
289 grid_row.toggle_editable("tax_amount", false);
290 grid_row.toggle_reqd("tax_amount", false);
291 }
Nabin Hait613d0812015-02-23 11:58:15 +0530292 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530293}