blob: c1c35536853945807d4b0646382a0635163ad8d8 [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 {
23 var account_type = ["Tax", "Chargeable", "Income Account"];
24 }
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());
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053055 }
56});
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 }
65})
66
67frappe.ui.form.on('Purchase Invoice', {
68 mode_of_payment: function(frm) {
69 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
70 frm.set_value('cash_bank_account', account);
71 })
72 }
73})
74
75frappe.ui.form.on('Payment Entry', {
76 mode_of_payment: function(frm) {
77 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
78 var payment_account_field = frm.doc.payment_type == "Receive" ? "paid_to" : "paid_from";
79 frm.set_value(payment_account_field, account);
80 })
81 }
82})
83
Kanchan Chauhanb40c0a92016-08-26 11:11:17 +053084frappe.ui.form.on('Salary Structure', {
85 mode_of_payment: function(frm) {
86 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
87 frm.set_value("payment_account", account);
88 })
89 }
90})
91
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053092get_payment_mode_account = function(frm, mode_of_payment, callback){
93 return frappe.call({
94 method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account",
95 args: {
96 "mode_of_payment": mode_of_payment,
97 "company": frm.doc.company
98 },
99 callback: function(r, rt) {
100 if(r.message) {
101 callback(r.message.account)
102 }
103 }
104 });
105}
106
107
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530108cur_frm.cscript.account_head = function(doc, cdt, cdn) {
109 var d = locals[cdt][cdn];
110 if(!d.charge_type && d.account_head){
111 msgprint("Please select Charge Type first");
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530112 frappe.model.set_value(cdt, cdn, "account_head", "");
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530113 } else if(d.account_head && d.charge_type!=="Actual") {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530114 frappe.call({
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530115 type:"GET",
Nabin Haitce245122015-02-22 20:14:49 +0530116 method: "erpnext.controllers.accounts_controller.get_tax_rate",
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530117 args: {"account_head":d.account_head},
118 callback: function(r) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530119 frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0);
120 frappe.model.set_value(cdt, cdn, "description", r.message.account_name);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530121 }
122 })
123 }
124}
Nabin Haitce245122015-02-22 20:14:49 +0530125
Nabin Hait613d0812015-02-23 11:58:15 +0530126cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530127 var d = locals[cdt][cdn];
Nabin Hait613d0812015-02-23 11:58:15 +0530128 var msg = "";
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530129
130 if(d.account_head && !d.description) {
131 // set description from account head
132 d.description = d.account_head.split(' - ').slice(0, -1).join(' - ');
133 }
134
Nabin Haitce245122015-02-22 20:14:49 +0530135 if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
Nabin Hait613d0812015-02-23 11:58:15 +0530136 msg = __("Please select Charge Type first");
Nabin Hait2b019ed2015-02-22 23:03:07 +0530137 d.row_id = "";
138 d.rate = d.tax_amount = 0.0;
Nabin Haitce245122015-02-22 20:14:49 +0530139 } else if((d.charge_type == 'Actual' || d.charge_type == 'On Net Total') && d.row_id) {
Nabin Hait613d0812015-02-23 11:58:15 +0530140 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 +0530141 d.row_id = "";
142 } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
143 if (d.idx == 1) {
Nabin Hait613d0812015-02-23 11:58:15 +0530144 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 +0530145 d.charge_type = '';
Nabin Hait613d0812015-02-23 11:58:15 +0530146 } else if (!d.row_id) {
147 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
148 d.row_id = "";
149 } else if(d.row_id && d.row_id >= d.idx) {
150 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
Nabin Haitce245122015-02-22 20:14:49 +0530151 d.row_id = "";
152 }
153 }
Nabin Hait613d0812015-02-23 11:58:15 +0530154 if(msg) {
155 validated = false;
156 refresh_field("taxes");
157 frappe.throw(msg);
158 }
159
160}
161
162cur_frm.cscript.validate_inclusive_tax = function(tax) {
163 var actual_type_error = function() {
164 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
165 frappe.throw(msg);
166 };
167
168 var on_previous_row_error = function(row_range) {
169 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
170 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
171 frappe.throw(msg);
172 };
173
174 if(cint(tax.included_in_print_rate)) {
175 if(tax.charge_type == "Actual") {
176 // inclusive tax cannot be of type Actual
177 actual_type_error();
178 } else if(tax.charge_type == "On Previous Row Amount" &&
179 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)) {
180 // referred row should also be an inclusive tax
181 on_previous_row_error(tax.row_id);
182 } else if(tax.charge_type == "On Previous Row Total") {
183 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
184 function(t) { return cint(t.included_in_print_rate) ? null : t; });
185 if(taxes_not_included.length > 0) {
186 // all rows above this tax should be inclusive
187 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
188 }
Nabin Hait72c359a2015-02-24 16:05:19 +0530189 } else if(tax.category == "Valuation") {
190 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Nabin Hait613d0812015-02-23 11:58:15 +0530191 }
192 }
Nabin Haitce245122015-02-22 20:14:49 +0530193}
194
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530195if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
196 erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
Nabin Haitce245122015-02-22 20:14:49 +0530197
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530198 frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
Nabin Hait613d0812015-02-23 11:58:15 +0530199 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530200 });
201
202 frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
203 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
204 });
205
206 frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
207 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
208 });
209
210 frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530211 frm.cscript.validate_taxes_and_charges(cdt, cdn);
212 var open_form = frm.open_grid_row();
213 if(open_form) {
214 erpnext.taxes.set_conditional_mandatory_rate_or_amount(open_form);
215 } else {
216 // apply in current row
217 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_grid_row(cdn));
218 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530219 });
220
221 frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
222 var tax = frappe.get_doc(cdt, cdn);
223 try {
224 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
225 cur_frm.cscript.validate_inclusive_tax(tax);
226 } catch(e) {
227 tax.included_in_print_rate = 0;
228 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
229 throw e;
230 }
231 });
232}
233
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530234erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(grid_row) {
235 if(grid_row) {
236 if(grid_row.doc.charge_type==="Actual") {
237 grid_row.toggle_editable("tax_amount", true);
238 grid_row.toggle_reqd("tax_amount", true);
239 grid_row.toggle_editable("rate", false);
240 grid_row.toggle_reqd("rate", false);
241 } else {
242 grid_row.toggle_editable("rate", true);
243 grid_row.toggle_reqd("rate", true);
244 grid_row.toggle_editable("tax_amount", false);
245 grid_row.toggle_reqd("tax_amount", false);
246 }
Nabin Hait613d0812015-02-23 11:58:15 +0530247 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530248}
249
Nabin Haitce245122015-02-22 20:14:49 +0530250
251// For customizing print
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530252cur_frm.pformat.total = function(doc) { return ''; }
Nabin Haitce245122015-02-22 20:14:49 +0530253cur_frm.pformat.discount_amount = function(doc) { return ''; }
254cur_frm.pformat.grand_total = function(doc) { return ''; }
255cur_frm.pformat.rounded_total = function(doc) { return ''; }
256cur_frm.pformat.in_words = function(doc) { return ''; }
257
258cur_frm.pformat.taxes= function(doc){
259 //function to make row of table
Nabin Haitde9c8a92015-02-23 01:06:00 +0530260 var make_row = function(title, val, bold, is_negative) {
Nabin Haitce245122015-02-22 20:14:49 +0530261 var bstart = '<b>'; var bend = '</b>';
262 return '<tr><td style="width:50%;">' + (bold?bstart:'') + title + (bold?bend:'') + '</td>'
Nabin Haitde9c8a92015-02-23 01:06:00 +0530263 + '<td style="width:50%;text-align:right;">' + (is_negative ? '- ' : '')
264 + format_currency(val, doc.currency) + '</td></tr>';
Nabin Haitce245122015-02-22 20:14:49 +0530265 }
266
Nabin Haitce245122015-02-22 20:14:49 +0530267 function print_hide(fieldname) {
268 var doc_field = frappe.meta.get_docfield(doc.doctype, fieldname, doc.name);
269 return doc_field.print_hide;
270 }
271
272 out ='';
273 if (!doc.print_without_amount) {
274 var cl = doc.taxes || [];
275
276 // outer table
277 var out='<div><table class="noborder" style="width:100%"><tr><td style="width: 60%"></td><td>';
278
279 // main table
280
281 out +='<table class="noborder" style="width:100%">';
282
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530283 if(!print_hide('total')) {
284 out += make_row('Total', doc.total, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530285 }
286
Nabin Haitde9c8a92015-02-23 01:06:00 +0530287 // Discount Amount on net total
288 if(!print_hide('discount_amount') && doc.apply_discount_on == "Net Total" && doc.discount_amount)
289 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
290
Nabin Haitce245122015-02-22 20:14:49 +0530291 // add rows
292 if(cl.length){
293 for(var i=0;i<cl.length;i++) {
Nabin Hait755ff602015-02-23 01:12:09 +0530294 if(cl[i].tax_amount!=0 && !cl[i].included_in_print_rate)
295 out += make_row(cl[i].description, cl[i].tax_amount, 0);
Nabin Haitce245122015-02-22 20:14:49 +0530296 }
297 }
298
Nabin Haitde9c8a92015-02-23 01:06:00 +0530299 // Discount Amount on grand total
300 if(!print_hide('discount_amount') && doc.apply_discount_on == "Grand Total" && doc.discount_amount)
301 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530302
303 // grand total
304 if(!print_hide('grand_total'))
305 out += make_row('Grand Total', doc.grand_total, 1);
306
307 if(!print_hide('rounded_total'))
308 out += make_row('Rounded Total', doc.rounded_total, 1);
309
310 if(doc.in_words && !print_hide('in_words')) {
311 out +='</table></td></tr>';
312 out += '<tr><td colspan = "2">';
313 out += '<table><tr><td style="width:25%;"><b>In Words</b></td>';
314 out += '<td style="width:50%;">' + doc.in_words + '</td></tr>';
315 }
316 out += '</table></td></tr></table></div>';
317 }
318 return out;
319}