blob: 3f664ae42155b0d505ba0a82353f517b26930c3a [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());
tundedba6f842017-09-07 06:51:07 +010055 }
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053056});
57
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053058frappe.ui.form.on('Sales Invoice Payment', {
59 mode_of_payment: function(frm, cdt, cdn) {
60 var d = locals[cdt][cdn];
61 get_payment_mode_account(frm, d.mode_of_payment, function(account){
62 frappe.model.set_value(cdt, cdn, 'account', account)
63 })
64 }
tundee7da1c52017-09-06 12:36:45 +010065});
66
67frappe.ui.form.on("Sales Invoice", {
68 payment_terms_template: function() {
69 cur_frm.trigger("disable_due_date");
70 }
71});
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053072
73frappe.ui.form.on('Purchase Invoice', {
74 mode_of_payment: function(frm) {
75 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
76 frm.set_value('cash_bank_account', account);
77 })
tundee7da1c52017-09-06 12:36:45 +010078 },
79
80 payment_terms_template: function() {
81 cur_frm.trigger("disable_due_date");
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053082 }
tundee7da1c52017-09-06 12:36:45 +010083});
84
85frappe.ui.form.on("Payment Schedule", {
86 payment_schedule_remove: function() {
87 cur_frm.trigger("disable_due_date");
88 },
89
90});
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053091
92frappe.ui.form.on('Payment Entry', {
93 mode_of_payment: function(frm) {
94 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
95 var payment_account_field = frm.doc.payment_type == "Receive" ? "paid_to" : "paid_from";
96 frm.set_value(payment_account_field, account);
97 })
98 }
99})
100
Kanchan Chauhanb40c0a92016-08-26 11:11:17 +0530101frappe.ui.form.on('Salary Structure', {
102 mode_of_payment: function(frm) {
103 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
104 frm.set_value("payment_account", account);
105 })
106 }
107})
108
Makarand Bauskar8f507a92017-07-13 11:44:29 +0530109var get_payment_mode_account = function(frm, mode_of_payment, callback) {
110 if(!frm.doc.company) {
111 frappe.throw(__("Please select the Company first"));
112 }
113
Makarand Bauskarad7eb9d2017-07-14 17:31:36 +0530114 if(!mode_of_payment) {
115 return;
116 }
117
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530118 return frappe.call({
119 method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account",
120 args: {
121 "mode_of_payment": mode_of_payment,
122 "company": frm.doc.company
123 },
124 callback: function(r, rt) {
125 if(r.message) {
126 callback(r.message.account)
127 }
128 }
129 });
130}
131
132
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530133cur_frm.cscript.account_head = function(doc, cdt, cdn) {
134 var d = locals[cdt][cdn];
135 if(!d.charge_type && d.account_head){
Nabin Haite6d65bc2018-02-14 17:44:06 +0530136 frappe.msgprint(__("Please select Charge Type first"));
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530137 frappe.model.set_value(cdt, cdn, "account_head", "");
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530138 } else if(d.account_head && d.charge_type!=="Actual") {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530139 frappe.call({
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530140 type:"GET",
Nabin Haitce245122015-02-22 20:14:49 +0530141 method: "erpnext.controllers.accounts_controller.get_tax_rate",
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530142 args: {"account_head":d.account_head},
143 callback: function(r) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530144 frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0);
145 frappe.model.set_value(cdt, cdn, "description", r.message.account_name);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530146 }
147 })
rohitwaghchaure00656242018-05-02 12:20:55 +0530148 } else if (d.charge_type == 'Actual' && d.account_head) {
149 frappe.model.set_value(cdt, cdn, "description", d.account_head.split(' - ')[0]);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530150 }
151}
Nabin Haitce245122015-02-22 20:14:49 +0530152
Nabin Hait613d0812015-02-23 11:58:15 +0530153cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530154 var d = locals[cdt][cdn];
Nabin Hait613d0812015-02-23 11:58:15 +0530155 var msg = "";
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530156
157 if(d.account_head && !d.description) {
158 // set description from account head
159 d.description = d.account_head.split(' - ').slice(0, -1).join(' - ');
160 }
161
Nabin Haitce245122015-02-22 20:14:49 +0530162 if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
Nabin Hait613d0812015-02-23 11:58:15 +0530163 msg = __("Please select Charge Type first");
Nabin Hait2b019ed2015-02-22 23:03:07 +0530164 d.row_id = "";
165 d.rate = d.tax_amount = 0.0;
Nabin Haitce245122015-02-22 20:14:49 +0530166 } else if((d.charge_type == 'Actual' || d.charge_type == 'On Net Total') && d.row_id) {
Nabin Hait613d0812015-02-23 11:58:15 +0530167 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 +0530168 d.row_id = "";
169 } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
170 if (d.idx == 1) {
Nabin Hait613d0812015-02-23 11:58:15 +0530171 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 +0530172 d.charge_type = '';
Nabin Hait613d0812015-02-23 11:58:15 +0530173 } else if (!d.row_id) {
174 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
175 d.row_id = "";
176 } else if(d.row_id && d.row_id >= d.idx) {
177 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
Nabin Haitce245122015-02-22 20:14:49 +0530178 d.row_id = "";
179 }
180 }
Nabin Hait613d0812015-02-23 11:58:15 +0530181 if(msg) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530182 frappe.validated = false;
Nabin Hait613d0812015-02-23 11:58:15 +0530183 refresh_field("taxes");
184 frappe.throw(msg);
185 }
186
187}
188
189cur_frm.cscript.validate_inclusive_tax = function(tax) {
190 var actual_type_error = function() {
191 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
192 frappe.throw(msg);
193 };
194
195 var on_previous_row_error = function(row_range) {
196 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
197 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
198 frappe.throw(msg);
199 };
200
201 if(cint(tax.included_in_print_rate)) {
202 if(tax.charge_type == "Actual") {
203 // inclusive tax cannot be of type Actual
204 actual_type_error();
205 } else if(tax.charge_type == "On Previous Row Amount" &&
Faris Ansariab74ca72017-05-30 12:54:42 +0530206 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)
207 ) {
208 // referred row should also be an inclusive tax
209 on_previous_row_error(tax.row_id);
Nabin Hait613d0812015-02-23 11:58:15 +0530210 } else if(tax.charge_type == "On Previous Row Total") {
211 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
212 function(t) { return cint(t.included_in_print_rate) ? null : t; });
213 if(taxes_not_included.length > 0) {
214 // all rows above this tax should be inclusive
215 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
216 }
Nabin Hait72c359a2015-02-24 16:05:19 +0530217 } else if(tax.category == "Valuation") {
218 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Nabin Hait613d0812015-02-23 11:58:15 +0530219 }
220 }
Nabin Haitce245122015-02-22 20:14:49 +0530221}
222
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530223if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
224 erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
Nabin Haitce245122015-02-22 20:14:49 +0530225
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530226 frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
Nabin Hait613d0812015-02-23 11:58:15 +0530227 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530228 });
229
230 frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
231 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
232 });
233
234 frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
235 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
236 });
237
238 frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530239 frm.cscript.validate_taxes_and_charges(cdt, cdn);
240 var open_form = frm.open_grid_row();
241 if(open_form) {
242 erpnext.taxes.set_conditional_mandatory_rate_or_amount(open_form);
243 } else {
244 // apply in current row
Rushabh Mehta43ef4e92017-07-03 11:53:07 +0530245 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_row(cdn));
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530246 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530247 });
248
249 frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
250 var tax = frappe.get_doc(cdt, cdn);
251 try {
252 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
253 cur_frm.cscript.validate_inclusive_tax(tax);
254 } catch(e) {
255 tax.included_in_print_rate = 0;
256 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
257 throw e;
258 }
259 });
260}
261
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530262erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(grid_row) {
263 if(grid_row) {
264 if(grid_row.doc.charge_type==="Actual") {
265 grid_row.toggle_editable("tax_amount", true);
266 grid_row.toggle_reqd("tax_amount", true);
267 grid_row.toggle_editable("rate", false);
268 grid_row.toggle_reqd("rate", false);
269 } else {
270 grid_row.toggle_editable("rate", true);
271 grid_row.toggle_reqd("rate", true);
272 grid_row.toggle_editable("tax_amount", false);
273 grid_row.toggle_reqd("tax_amount", false);
274 }
Nabin Hait613d0812015-02-23 11:58:15 +0530275 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530276}
277
Nabin Haitce245122015-02-22 20:14:49 +0530278
279// For customizing print
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530280cur_frm.pformat.total = function(doc) { return ''; }
Nabin Haitce245122015-02-22 20:14:49 +0530281cur_frm.pformat.discount_amount = function(doc) { return ''; }
282cur_frm.pformat.grand_total = function(doc) { return ''; }
283cur_frm.pformat.rounded_total = function(doc) { return ''; }
284cur_frm.pformat.in_words = function(doc) { return ''; }
285
286cur_frm.pformat.taxes= function(doc){
287 //function to make row of table
Nabin Haitde9c8a92015-02-23 01:06:00 +0530288 var make_row = function(title, val, bold, is_negative) {
Nabin Haitce245122015-02-22 20:14:49 +0530289 var bstart = '<b>'; var bend = '</b>';
290 return '<tr><td style="width:50%;">' + (bold?bstart:'') + title + (bold?bend:'') + '</td>'
Nabin Haitde9c8a92015-02-23 01:06:00 +0530291 + '<td style="width:50%;text-align:right;">' + (is_negative ? '- ' : '')
292 + format_currency(val, doc.currency) + '</td></tr>';
Nabin Haitce245122015-02-22 20:14:49 +0530293 }
294
Nabin Haitce245122015-02-22 20:14:49 +0530295 function print_hide(fieldname) {
296 var doc_field = frappe.meta.get_docfield(doc.doctype, fieldname, doc.name);
297 return doc_field.print_hide;
298 }
299
300 out ='';
301 if (!doc.print_without_amount) {
302 var cl = doc.taxes || [];
303
304 // outer table
305 var out='<div><table class="noborder" style="width:100%"><tr><td style="width: 60%"></td><td>';
306
307 // main table
308
309 out +='<table class="noborder" style="width:100%">';
310
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530311 if(!print_hide('total')) {
312 out += make_row('Total', doc.total, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530313 }
314
Nabin Haitde9c8a92015-02-23 01:06:00 +0530315 // Discount Amount on net total
316 if(!print_hide('discount_amount') && doc.apply_discount_on == "Net Total" && doc.discount_amount)
317 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
318
Nabin Haitce245122015-02-22 20:14:49 +0530319 // add rows
320 if(cl.length){
321 for(var i=0;i<cl.length;i++) {
Nabin Hait755ff602015-02-23 01:12:09 +0530322 if(cl[i].tax_amount!=0 && !cl[i].included_in_print_rate)
323 out += make_row(cl[i].description, cl[i].tax_amount, 0);
Nabin Haitce245122015-02-22 20:14:49 +0530324 }
325 }
326
Nabin Haitde9c8a92015-02-23 01:06:00 +0530327 // Discount Amount on grand total
328 if(!print_hide('discount_amount') && doc.apply_discount_on == "Grand Total" && doc.discount_amount)
329 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530330
331 // grand total
332 if(!print_hide('grand_total'))
333 out += make_row('Grand Total', doc.grand_total, 1);
334
335 if(!print_hide('rounded_total'))
336 out += make_row('Rounded Total', doc.rounded_total, 1);
337
338 if(doc.in_words && !print_hide('in_words')) {
339 out +='</table></td></tr>';
340 out += '<tr><td colspan = "2">';
341 out += '<table><tr><td style="width:25%;"><b>In Words</b></td>';
342 out += '<td style="width:50%;">' + doc.in_words + '</td></tr>';
343 }
344 out += '</table></td></tr></table></div>';
345 }
346 return out;
347}