blob: 3dfc8911fc464b1e7fea1a2ff4c3d90117ee1472 [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 });
34
35 frm.set_query("cost_center", "taxes", function(doc) {
36 return {
37 filters: {
38 'company': doc.company,
39 "is_group": 0
40 }
41 }
42 });
43 }
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 }
67 }
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) {
123 frappe.throw(__("Please select the Company first"));
124 }
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];
146 if(!d.charge_type && d.account_head){
Nabin Haite6d65bc2018-02-14 17:44:06 +0530147 frappe.msgprint(__("Please select Charge Type first"));
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530148 frappe.model.set_value(cdt, cdn, "account_head", "");
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530149 } else if(d.account_head && d.charge_type!=="Actual") {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530150 frappe.call({
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530151 type:"GET",
Nabin Haitce245122015-02-22 20:14:49 +0530152 method: "erpnext.controllers.accounts_controller.get_tax_rate",
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530153 args: {"account_head":d.account_head},
154 callback: function(r) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530155 frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0);
156 frappe.model.set_value(cdt, cdn, "description", r.message.account_name);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530157 }
158 })
rohitwaghchaure00656242018-05-02 12:20:55 +0530159 } else if (d.charge_type == 'Actual' && d.account_head) {
160 frappe.model.set_value(cdt, cdn, "description", d.account_head.split(' - ')[0]);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530161 }
162}
Nabin Haitce245122015-02-22 20:14:49 +0530163
Nabin Hait613d0812015-02-23 11:58:15 +0530164cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530165 var d = locals[cdt][cdn];
Nabin Hait613d0812015-02-23 11:58:15 +0530166 var msg = "";
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530167
168 if(d.account_head && !d.description) {
169 // set description from account head
170 d.description = d.account_head.split(' - ').slice(0, -1).join(' - ');
171 }
172
Nabin Haitce245122015-02-22 20:14:49 +0530173 if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
Nabin Hait613d0812015-02-23 11:58:15 +0530174 msg = __("Please select Charge Type first");
Nabin Hait2b019ed2015-02-22 23:03:07 +0530175 d.row_id = "";
176 d.rate = d.tax_amount = 0.0;
Nabin Haitce245122015-02-22 20:14:49 +0530177 } else if((d.charge_type == 'Actual' || d.charge_type == 'On Net Total') && d.row_id) {
Nabin Hait613d0812015-02-23 11:58:15 +0530178 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 +0530179 d.row_id = "";
180 } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
181 if (d.idx == 1) {
Nabin Hait613d0812015-02-23 11:58:15 +0530182 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 +0530183 d.charge_type = '';
Nabin Hait613d0812015-02-23 11:58:15 +0530184 } else if (!d.row_id) {
185 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
186 d.row_id = "";
187 } else if(d.row_id && d.row_id >= d.idx) {
188 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
Nabin Haitce245122015-02-22 20:14:49 +0530189 d.row_id = "";
190 }
191 }
Nabin Hait613d0812015-02-23 11:58:15 +0530192 if(msg) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530193 frappe.validated = false;
Nabin Hait613d0812015-02-23 11:58:15 +0530194 refresh_field("taxes");
195 frappe.throw(msg);
196 }
197
198}
199
200cur_frm.cscript.validate_inclusive_tax = function(tax) {
201 var actual_type_error = function() {
202 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
203 frappe.throw(msg);
204 };
205
206 var on_previous_row_error = function(row_range) {
207 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
208 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
209 frappe.throw(msg);
210 };
211
212 if(cint(tax.included_in_print_rate)) {
213 if(tax.charge_type == "Actual") {
214 // inclusive tax cannot be of type Actual
215 actual_type_error();
216 } else if(tax.charge_type == "On Previous Row Amount" &&
Faris Ansariab74ca72017-05-30 12:54:42 +0530217 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)
218 ) {
219 // referred row should also be an inclusive tax
220 on_previous_row_error(tax.row_id);
Nabin Hait613d0812015-02-23 11:58:15 +0530221 } else if(tax.charge_type == "On Previous Row Total") {
222 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
223 function(t) { return cint(t.included_in_print_rate) ? null : t; });
224 if(taxes_not_included.length > 0) {
225 // all rows above this tax should be inclusive
226 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
227 }
Nabin Hait72c359a2015-02-24 16:05:19 +0530228 } else if(tax.category == "Valuation") {
229 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Nabin Hait613d0812015-02-23 11:58:15 +0530230 }
231 }
Nabin Haitce245122015-02-22 20:14:49 +0530232}
233
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530234if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
235 erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
Nabin Haitce245122015-02-22 20:14:49 +0530236
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530237 frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
Nabin Hait613d0812015-02-23 11:58:15 +0530238 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530239 });
240
241 frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
242 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
243 });
244
245 frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
246 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
247 });
248
249 frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530250 frm.cscript.validate_taxes_and_charges(cdt, cdn);
251 var open_form = frm.open_grid_row();
252 if(open_form) {
253 erpnext.taxes.set_conditional_mandatory_rate_or_amount(open_form);
254 } else {
255 // apply in current row
Rushabh Mehta43ef4e92017-07-03 11:53:07 +0530256 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_row(cdn));
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530257 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530258 });
259
260 frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
261 var tax = frappe.get_doc(cdt, cdn);
262 try {
263 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
264 cur_frm.cscript.validate_inclusive_tax(tax);
265 } catch(e) {
266 tax.included_in_print_rate = 0;
267 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
268 throw e;
269 }
270 });
271}
272
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530273erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(grid_row) {
274 if(grid_row) {
275 if(grid_row.doc.charge_type==="Actual") {
276 grid_row.toggle_editable("tax_amount", true);
277 grid_row.toggle_reqd("tax_amount", true);
278 grid_row.toggle_editable("rate", false);
279 grid_row.toggle_reqd("rate", false);
280 } else {
281 grid_row.toggle_editable("rate", true);
282 grid_row.toggle_reqd("rate", true);
283 grid_row.toggle_editable("tax_amount", false);
284 grid_row.toggle_reqd("tax_amount", false);
285 }
Nabin Hait613d0812015-02-23 11:58:15 +0530286 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530287}
288
Nabin Haitce245122015-02-22 20:14:49 +0530289
290// For customizing print
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530291cur_frm.pformat.total = function(doc) { return ''; }
Nabin Haitce245122015-02-22 20:14:49 +0530292cur_frm.pformat.discount_amount = function(doc) { return ''; }
293cur_frm.pformat.grand_total = function(doc) { return ''; }
294cur_frm.pformat.rounded_total = function(doc) { return ''; }
295cur_frm.pformat.in_words = function(doc) { return ''; }
296
297cur_frm.pformat.taxes= function(doc){
298 //function to make row of table
Nabin Haitde9c8a92015-02-23 01:06:00 +0530299 var make_row = function(title, val, bold, is_negative) {
Nabin Haitce245122015-02-22 20:14:49 +0530300 var bstart = '<b>'; var bend = '</b>';
301 return '<tr><td style="width:50%;">' + (bold?bstart:'') + title + (bold?bend:'') + '</td>'
Nabin Haitde9c8a92015-02-23 01:06:00 +0530302 + '<td style="width:50%;text-align:right;">' + (is_negative ? '- ' : '')
303 + format_currency(val, doc.currency) + '</td></tr>';
Nabin Haitce245122015-02-22 20:14:49 +0530304 }
305
Nabin Haitce245122015-02-22 20:14:49 +0530306 function print_hide(fieldname) {
307 var doc_field = frappe.meta.get_docfield(doc.doctype, fieldname, doc.name);
308 return doc_field.print_hide;
309 }
310
311 out ='';
312 if (!doc.print_without_amount) {
313 var cl = doc.taxes || [];
314
315 // outer table
316 var out='<div><table class="noborder" style="width:100%"><tr><td style="width: 60%"></td><td>';
317
318 // main table
319
320 out +='<table class="noborder" style="width:100%">';
321
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530322 if(!print_hide('total')) {
323 out += make_row('Total', doc.total, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530324 }
325
Nabin Haitde9c8a92015-02-23 01:06:00 +0530326 // Discount Amount on net total
327 if(!print_hide('discount_amount') && doc.apply_discount_on == "Net Total" && doc.discount_amount)
328 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
329
Nabin Haitce245122015-02-22 20:14:49 +0530330 // add rows
331 if(cl.length){
332 for(var i=0;i<cl.length;i++) {
Nabin Hait755ff602015-02-23 01:12:09 +0530333 if(cl[i].tax_amount!=0 && !cl[i].included_in_print_rate)
334 out += make_row(cl[i].description, cl[i].tax_amount, 0);
Nabin Haitce245122015-02-22 20:14:49 +0530335 }
336 }
337
Nabin Haitde9c8a92015-02-23 01:06:00 +0530338 // Discount Amount on grand total
339 if(!print_hide('discount_amount') && doc.apply_discount_on == "Grand Total" && doc.discount_amount)
340 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530341
342 // grand total
343 if(!print_hide('grand_total'))
344 out += make_row('Grand Total', doc.grand_total, 1);
345
346 if(!print_hide('rounded_total'))
347 out += make_row('Rounded Total', doc.rounded_total, 1);
348
349 if(doc.in_words && !print_hide('in_words')) {
350 out +='</table></td></tr>';
351 out += '<tr><td colspan = "2">';
352 out += '<table><tr><td style="width:25%;"><b>In Words</b></td>';
353 out += '<td style="width:50%;">' + doc.in_words + '</td></tr>';
354 }
355 out += '</table></td></tr></table></div>';
356 }
357 return out;
358}