blob: 649eb454acc479a1df06365e3016ab341756660f [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 });
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053034 }
35 },
36 validate: function(frm) {
37 // neither is absolutely mandatory
Nabin Hait0e1540b2015-05-05 17:26:35 +053038 if(frm.get_docfield("taxes")) {
39 frm.get_docfield("taxes", "rate").reqd = 0;
40 frm.get_docfield("taxes", "tax_amount").reqd = 0;
41 }
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +053042
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053043 },
44 taxes_on_form_rendered: function(frm) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +053045 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.open_grid_row());
Nabin Hait041a5c22018-08-01 18:07:39 +053046 },
47
48 allocate_advances_automatically: function(frm) {
49 if(frm.doc.allocate_advances_automatically) {
50 frappe.call({
51 doc: frm.doc,
52 method: "set_advances",
53 callback: function(r, rt) {
54 refresh_field("advances");
55 }
56 })
57 }
marination6ef057a2019-11-18 15:55:32 +053058 }
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053059});
60
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053061frappe.ui.form.on('Sales Invoice Payment', {
62 mode_of_payment: function(frm, cdt, cdn) {
63 var d = locals[cdt][cdn];
64 get_payment_mode_account(frm, d.mode_of_payment, function(account){
65 frappe.model.set_value(cdt, cdn, 'account', account)
66 })
67 }
tundee7da1c52017-09-06 12:36:45 +010068});
69
70frappe.ui.form.on("Sales Invoice", {
71 payment_terms_template: function() {
72 cur_frm.trigger("disable_due_date");
73 }
74});
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053075
76frappe.ui.form.on('Purchase Invoice', {
77 mode_of_payment: function(frm) {
78 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
79 frm.set_value('cash_bank_account', account);
80 })
tundee7da1c52017-09-06 12:36:45 +010081 },
82
83 payment_terms_template: function() {
84 cur_frm.trigger("disable_due_date");
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053085 }
tundee7da1c52017-09-06 12:36:45 +010086});
87
88frappe.ui.form.on("Payment Schedule", {
89 payment_schedule_remove: function() {
90 cur_frm.trigger("disable_due_date");
91 },
92
93});
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053094
95frappe.ui.form.on('Payment Entry', {
96 mode_of_payment: function(frm) {
97 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
98 var payment_account_field = frm.doc.payment_type == "Receive" ? "paid_to" : "paid_from";
99 frm.set_value(payment_account_field, account);
100 })
101 }
102})
103
Kanchan Chauhanb40c0a92016-08-26 11:11:17 +0530104frappe.ui.form.on('Salary Structure', {
105 mode_of_payment: function(frm) {
106 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
107 frm.set_value("payment_account", account);
108 })
109 }
110})
111
Makarand Bauskar8f507a92017-07-13 11:44:29 +0530112var get_payment_mode_account = function(frm, mode_of_payment, callback) {
113 if(!frm.doc.company) {
marination8370d9b2020-09-24 11:51:03 +0530114 frappe.throw({message:__("Please select a Company first."), title: __("Mandatory")});
Makarand Bauskar8f507a92017-07-13 11:44:29 +0530115 }
116
Makarand Bauskarad7eb9d2017-07-14 17:31:36 +0530117 if(!mode_of_payment) {
118 return;
119 }
120
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530121 return frappe.call({
122 method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account",
123 args: {
124 "mode_of_payment": mode_of_payment,
125 "company": frm.doc.company
126 },
127 callback: function(r, rt) {
128 if(r.message) {
129 callback(r.message.account)
130 }
131 }
132 });
133}
134
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530135cur_frm.cscript.account_head = function(doc, cdt, cdn) {
136 var d = locals[cdt][cdn];
137 if(!d.charge_type && d.account_head){
Nabin Haite6d65bc2018-02-14 17:44:06 +0530138 frappe.msgprint(__("Please select Charge Type first"));
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530139 frappe.model.set_value(cdt, cdn, "account_head", "");
pateljannat7e4d1152020-11-11 09:52:27 +0530140 } else if (d.account_head) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530141 frappe.call({
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530142 type:"GET",
Nabin Haitce245122015-02-22 20:14:49 +0530143 method: "erpnext.controllers.accounts_controller.get_tax_rate",
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530144 args: {"account_head":d.account_head},
145 callback: function(r) {
pateljannat7e4d1152020-11-11 09:52:27 +0530146 if (d.charge_type!=="Actual") {
pateljannat403822a2020-11-05 16:24:33 +0530147 frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0);
pateljannat5b02d322020-11-05 18:04:14 +0530148 }
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530149 frappe.model.set_value(cdt, cdn, "description", r.message.account_name);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530150 }
151 })
152 }
153}
Nabin Haitce245122015-02-22 20:14:49 +0530154
Nabin Hait613d0812015-02-23 11:58:15 +0530155cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530156 var d = locals[cdt][cdn];
Nabin Hait613d0812015-02-23 11:58:15 +0530157 var msg = "";
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530158
159 if(d.account_head && !d.description) {
160 // set description from account head
161 d.description = d.account_head.split(' - ').slice(0, -1).join(' - ');
162 }
163
Nabin Haitce245122015-02-22 20:14:49 +0530164 if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
Nabin Hait613d0812015-02-23 11:58:15 +0530165 msg = __("Please select Charge Type first");
Nabin Hait2b019ed2015-02-22 23:03:07 +0530166 d.row_id = "";
167 d.rate = d.tax_amount = 0.0;
Nabin Haitce245122015-02-22 20:14:49 +0530168 } else if((d.charge_type == 'Actual' || d.charge_type == 'On Net Total') && d.row_id) {
Nabin Hait613d0812015-02-23 11:58:15 +0530169 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 +0530170 d.row_id = "";
171 } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
172 if (d.idx == 1) {
Nabin Hait613d0812015-02-23 11:58:15 +0530173 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 +0530174 d.charge_type = '';
Nabin Hait613d0812015-02-23 11:58:15 +0530175 } else if (!d.row_id) {
176 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
177 d.row_id = "";
178 } else if(d.row_id && d.row_id >= d.idx) {
179 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
Nabin Haitce245122015-02-22 20:14:49 +0530180 d.row_id = "";
181 }
182 }
Nabin Hait613d0812015-02-23 11:58:15 +0530183 if(msg) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530184 frappe.validated = false;
Nabin Hait613d0812015-02-23 11:58:15 +0530185 refresh_field("taxes");
186 frappe.throw(msg);
187 }
188
189}
190
191cur_frm.cscript.validate_inclusive_tax = function(tax) {
192 var actual_type_error = function() {
193 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
194 frappe.throw(msg);
195 };
196
197 var on_previous_row_error = function(row_range) {
198 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
199 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
200 frappe.throw(msg);
201 };
202
203 if(cint(tax.included_in_print_rate)) {
204 if(tax.charge_type == "Actual") {
205 // inclusive tax cannot be of type Actual
206 actual_type_error();
207 } else if(tax.charge_type == "On Previous Row Amount" &&
Faris Ansariab74ca72017-05-30 12:54:42 +0530208 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)
209 ) {
210 // referred row should also be an inclusive tax
211 on_previous_row_error(tax.row_id);
Nabin Hait613d0812015-02-23 11:58:15 +0530212 } else if(tax.charge_type == "On Previous Row Total") {
213 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
214 function(t) { return cint(t.included_in_print_rate) ? null : t; });
215 if(taxes_not_included.length > 0) {
216 // all rows above this tax should be inclusive
217 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
218 }
Nabin Hait72c359a2015-02-24 16:05:19 +0530219 } else if(tax.category == "Valuation") {
220 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Nabin Hait613d0812015-02-23 11:58:15 +0530221 }
222 }
Nabin Haitce245122015-02-22 20:14:49 +0530223}
224
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530225if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
226 erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
Nabin Haitce245122015-02-22 20:14:49 +0530227
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530228 frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
Nabin Hait613d0812015-02-23 11:58:15 +0530229 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530230 });
231
232 frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
233 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
234 });
235
236 frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
237 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
238 });
239
240 frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530241 frm.cscript.validate_taxes_and_charges(cdt, cdn);
242 var open_form = frm.open_grid_row();
243 if(open_form) {
244 erpnext.taxes.set_conditional_mandatory_rate_or_amount(open_form);
245 } else {
246 // apply in current row
Rushabh Mehta43ef4e92017-07-03 11:53:07 +0530247 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_row(cdn));
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530248 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530249 });
250
251 frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
252 var tax = frappe.get_doc(cdt, cdn);
253 try {
254 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
255 cur_frm.cscript.validate_inclusive_tax(tax);
256 } catch(e) {
257 tax.included_in_print_rate = 0;
258 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
259 throw e;
260 }
261 });
262}
263
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530264erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(grid_row) {
265 if(grid_row) {
266 if(grid_row.doc.charge_type==="Actual") {
267 grid_row.toggle_editable("tax_amount", true);
268 grid_row.toggle_reqd("tax_amount", true);
269 grid_row.toggle_editable("rate", false);
270 grid_row.toggle_reqd("rate", false);
271 } else {
272 grid_row.toggle_editable("rate", true);
273 grid_row.toggle_reqd("rate", true);
274 grid_row.toggle_editable("tax_amount", false);
275 grid_row.toggle_reqd("tax_amount", false);
276 }
Nabin Hait613d0812015-02-23 11:58:15 +0530277 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530278}
279
Nabin Haitce245122015-02-22 20:14:49 +0530280
281// For customizing print
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530282cur_frm.pformat.total = function(doc) { return ''; }
Nabin Haitce245122015-02-22 20:14:49 +0530283cur_frm.pformat.discount_amount = function(doc) { return ''; }
284cur_frm.pformat.grand_total = function(doc) { return ''; }
285cur_frm.pformat.rounded_total = function(doc) { return ''; }
286cur_frm.pformat.in_words = function(doc) { return ''; }
287
288cur_frm.pformat.taxes= function(doc){
289 //function to make row of table
Nabin Haitde9c8a92015-02-23 01:06:00 +0530290 var make_row = function(title, val, bold, is_negative) {
Nabin Haitce245122015-02-22 20:14:49 +0530291 var bstart = '<b>'; var bend = '</b>';
292 return '<tr><td style="width:50%;">' + (bold?bstart:'') + title + (bold?bend:'') + '</td>'
Nabin Haitde9c8a92015-02-23 01:06:00 +0530293 + '<td style="width:50%;text-align:right;">' + (is_negative ? '- ' : '')
294 + format_currency(val, doc.currency) + '</td></tr>';
Nabin Haitce245122015-02-22 20:14:49 +0530295 }
296
Nabin Haitce245122015-02-22 20:14:49 +0530297 function print_hide(fieldname) {
298 var doc_field = frappe.meta.get_docfield(doc.doctype, fieldname, doc.name);
299 return doc_field.print_hide;
300 }
301
302 out ='';
303 if (!doc.print_without_amount) {
304 var cl = doc.taxes || [];
305
306 // outer table
307 var out='<div><table class="noborder" style="width:100%"><tr><td style="width: 60%"></td><td>';
308
309 // main table
310
311 out +='<table class="noborder" style="width:100%">';
312
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530313 if(!print_hide('total')) {
314 out += make_row('Total', doc.total, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530315 }
316
Nabin Haitde9c8a92015-02-23 01:06:00 +0530317 // Discount Amount on net total
318 if(!print_hide('discount_amount') && doc.apply_discount_on == "Net Total" && doc.discount_amount)
319 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
320
Nabin Haitce245122015-02-22 20:14:49 +0530321 // add rows
322 if(cl.length){
323 for(var i=0;i<cl.length;i++) {
Nabin Hait755ff602015-02-23 01:12:09 +0530324 if(cl[i].tax_amount!=0 && !cl[i].included_in_print_rate)
325 out += make_row(cl[i].description, cl[i].tax_amount, 0);
Nabin Haitce245122015-02-22 20:14:49 +0530326 }
327 }
328
Nabin Haitde9c8a92015-02-23 01:06:00 +0530329 // Discount Amount on grand total
330 if(!print_hide('discount_amount') && doc.apply_discount_on == "Grand Total" && doc.discount_amount)
331 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530332
333 // grand total
334 if(!print_hide('grand_total'))
335 out += make_row('Grand Total', doc.grand_total, 1);
336
337 if(!print_hide('rounded_total'))
338 out += make_row('Rounded Total', doc.rounded_total, 1);
339
340 if(doc.in_words && !print_hide('in_words')) {
341 out +='</table></td></tr>';
342 out += '<tr><td colspan = "2">';
343 out += '<table><tr><td style="width:25%;"><b>In Words</b></td>';
344 out += '<td style="width:50%;">' + doc.in_words + '</td></tr>';
345 }
346 out += '</table></td></tr></table></div>';
347 }
348 return out;
marination6ef057a2019-11-18 15:55:32 +0530349}