blob: 84c717676c71e328c9aae4103bf1b5da96835632 [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,
30 "company": doc.company
31 }
32 }
33 });
Anuja Pawar0e337be2021-08-10 17:26:35 +053034 frm.set_query("cost_center", "taxes", function(doc) {
35 return {
36 filters: {
37 "company": doc.company,
38 "is_group": 0
39 }
40 };
41 });
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053042 }
43 },
44 validate: function(frm) {
45 // neither is absolutely mandatory
Nabin Hait0e1540b2015-05-05 17:26:35 +053046 if(frm.get_docfield("taxes")) {
47 frm.get_docfield("taxes", "rate").reqd = 0;
48 frm.get_docfield("taxes", "tax_amount").reqd = 0;
49 }
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +053050
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053051 },
52 taxes_on_form_rendered: function(frm) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +053053 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.open_grid_row());
Nabin Hait041a5c22018-08-01 18:07:39 +053054 },
55
56 allocate_advances_automatically: function(frm) {
57 if(frm.doc.allocate_advances_automatically) {
58 frappe.call({
59 doc: frm.doc,
60 method: "set_advances",
61 callback: function(r, rt) {
62 refresh_field("advances");
63 }
64 })
65 }
marination6ef057a2019-11-18 15:55:32 +053066 }
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053067});
68
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053069frappe.ui.form.on('Sales Invoice Payment', {
70 mode_of_payment: function(frm, cdt, cdn) {
71 var d = locals[cdt][cdn];
72 get_payment_mode_account(frm, d.mode_of_payment, function(account){
73 frappe.model.set_value(cdt, cdn, 'account', account)
74 })
75 }
tundee7da1c52017-09-06 12:36:45 +010076});
77
78frappe.ui.form.on("Sales Invoice", {
79 payment_terms_template: function() {
80 cur_frm.trigger("disable_due_date");
81 }
82});
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053083
84frappe.ui.form.on('Purchase Invoice', {
85 mode_of_payment: function(frm) {
86 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
87 frm.set_value('cash_bank_account', account);
88 })
tundee7da1c52017-09-06 12:36:45 +010089 },
90
91 payment_terms_template: function() {
92 cur_frm.trigger("disable_due_date");
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053093 }
tundee7da1c52017-09-06 12:36:45 +010094});
95
96frappe.ui.form.on("Payment Schedule", {
97 payment_schedule_remove: function() {
98 cur_frm.trigger("disable_due_date");
99 },
100
101});
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530102
103frappe.ui.form.on('Payment Entry', {
104 mode_of_payment: function(frm) {
105 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
106 var payment_account_field = frm.doc.payment_type == "Receive" ? "paid_to" : "paid_from";
107 frm.set_value(payment_account_field, account);
108 })
109 }
110})
111
Kanchan Chauhanb40c0a92016-08-26 11:11:17 +0530112frappe.ui.form.on('Salary Structure', {
113 mode_of_payment: function(frm) {
114 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
115 frm.set_value("payment_account", account);
116 })
117 }
118})
119
Makarand Bauskar8f507a92017-07-13 11:44:29 +0530120var get_payment_mode_account = function(frm, mode_of_payment, callback) {
121 if(!frm.doc.company) {
marination8370d9b2020-09-24 11:51:03 +0530122 frappe.throw({message:__("Please select a Company first."), title: __("Mandatory")});
Makarand Bauskar8f507a92017-07-13 11:44:29 +0530123 }
124
Makarand Bauskarad7eb9d2017-07-14 17:31:36 +0530125 if(!mode_of_payment) {
126 return;
127 }
128
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530129 return frappe.call({
130 method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account",
131 args: {
132 "mode_of_payment": mode_of_payment,
133 "company": frm.doc.company
134 },
135 callback: function(r, rt) {
136 if(r.message) {
137 callback(r.message.account)
138 }
139 }
140 });
141}
142
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530143cur_frm.cscript.account_head = function(doc, cdt, cdn) {
144 var d = locals[cdt][cdn];
145 if(!d.charge_type && d.account_head){
Nabin Haite6d65bc2018-02-14 17:44:06 +0530146 frappe.msgprint(__("Please select Charge Type first"));
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530147 frappe.model.set_value(cdt, cdn, "account_head", "");
pateljannat7e4d1152020-11-11 09:52:27 +0530148 } else if (d.account_head) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530149 frappe.call({
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530150 type:"GET",
Nabin Haitce245122015-02-22 20:14:49 +0530151 method: "erpnext.controllers.accounts_controller.get_tax_rate",
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530152 args: {"account_head":d.account_head},
153 callback: function(r) {
pateljannat7e4d1152020-11-11 09:52:27 +0530154 if (d.charge_type!=="Actual") {
pateljannat403822a2020-11-05 16:24:33 +0530155 frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0);
pateljannat5b02d322020-11-05 18:04:14 +0530156 }
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530157 frappe.model.set_value(cdt, cdn, "description", r.message.account_name);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530158 }
159 })
160 }
161}
Nabin Haitce245122015-02-22 20:14:49 +0530162
Nabin Hait613d0812015-02-23 11:58:15 +0530163cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530164 var d = locals[cdt][cdn];
Nabin Hait613d0812015-02-23 11:58:15 +0530165 var msg = "";
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530166
Deepesh Gargfd380d32021-05-13 14:04:51 +0530167 if (d.account_head && !d.description) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530168 // set description from account head
169 d.description = d.account_head.split(' - ').slice(0, -1).join(' - ');
170 }
171
Deepesh Gargfd380d32021-05-13 14:04:51 +0530172 if (!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
Nabin Hait613d0812015-02-23 11:58:15 +0530173 msg = __("Please select Charge Type first");
Nabin Hait2b019ed2015-02-22 23:03:07 +0530174 d.row_id = "";
175 d.rate = d.tax_amount = 0.0;
Deepesh Gargfd380d32021-05-13 14:04:51 +0530176 } 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 +0530177 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 +0530178 d.row_id = "";
Deepesh Gargfd380d32021-05-13 14:04:51 +0530179 } 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 +0530180 if (d.idx == 1) {
Nabin Hait613d0812015-02-23 11:58:15 +0530181 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 +0530182 d.charge_type = '';
Nabin Hait613d0812015-02-23 11:58:15 +0530183 } else if (!d.row_id) {
184 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
185 d.row_id = "";
Deepesh Gargfd380d32021-05-13 14:04:51 +0530186 } else if (d.row_id && d.row_id >= d.idx) {
Nabin Hait613d0812015-02-23 11:58:15 +0530187 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
Nabin Haitce245122015-02-22 20:14:49 +0530188 d.row_id = "";
189 }
190 }
Deepesh Gargfd380d32021-05-13 14:04:51 +0530191 if (msg) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530192 frappe.validated = false;
Nabin Hait613d0812015-02-23 11:58:15 +0530193 refresh_field("taxes");
194 frappe.throw(msg);
195 }
196
197}
198
199cur_frm.cscript.validate_inclusive_tax = function(tax) {
200 var actual_type_error = function() {
201 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
202 frappe.throw(msg);
203 };
204
205 var on_previous_row_error = function(row_range) {
206 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
207 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
208 frappe.throw(msg);
209 };
210
211 if(cint(tax.included_in_print_rate)) {
212 if(tax.charge_type == "Actual") {
213 // inclusive tax cannot be of type Actual
214 actual_type_error();
215 } else if(tax.charge_type == "On Previous Row Amount" &&
Faris Ansariab74ca72017-05-30 12:54:42 +0530216 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)
217 ) {
218 // referred row should also be an inclusive tax
219 on_previous_row_error(tax.row_id);
Nabin Hait613d0812015-02-23 11:58:15 +0530220 } else if(tax.charge_type == "On Previous Row Total") {
221 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
222 function(t) { return cint(t.included_in_print_rate) ? null : t; });
223 if(taxes_not_included.length > 0) {
224 // all rows above this tax should be inclusive
225 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
226 }
Nabin Hait72c359a2015-02-24 16:05:19 +0530227 } else if(tax.category == "Valuation") {
228 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Nabin Hait613d0812015-02-23 11:58:15 +0530229 }
230 }
Nabin Haitce245122015-02-22 20:14:49 +0530231}
232
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530233if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
234 erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
Nabin Haitce245122015-02-22 20:14:49 +0530235
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530236 frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
Nabin Hait613d0812015-02-23 11:58:15 +0530237 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530238 });
239
240 frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
241 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
242 });
243
244 frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
245 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
246 });
247
248 frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530249 frm.cscript.validate_taxes_and_charges(cdt, cdn);
250 var open_form = frm.open_grid_row();
251 if(open_form) {
252 erpnext.taxes.set_conditional_mandatory_rate_or_amount(open_form);
253 } else {
254 // apply in current row
Rushabh Mehta43ef4e92017-07-03 11:53:07 +0530255 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_row(cdn));
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530256 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530257 });
258
259 frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
260 var tax = frappe.get_doc(cdt, cdn);
261 try {
262 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
263 cur_frm.cscript.validate_inclusive_tax(tax);
264 } catch(e) {
265 tax.included_in_print_rate = 0;
266 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
267 throw e;
268 }
269 });
270}
271
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530272erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(grid_row) {
273 if(grid_row) {
274 if(grid_row.doc.charge_type==="Actual") {
275 grid_row.toggle_editable("tax_amount", true);
276 grid_row.toggle_reqd("tax_amount", true);
277 grid_row.toggle_editable("rate", false);
278 grid_row.toggle_reqd("rate", false);
279 } else {
280 grid_row.toggle_editable("rate", true);
281 grid_row.toggle_reqd("rate", true);
282 grid_row.toggle_editable("tax_amount", false);
283 grid_row.toggle_reqd("tax_amount", false);
284 }
Nabin Hait613d0812015-02-23 11:58:15 +0530285 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530286}