blob: 202000fbe2008d5eeedfc9421200a370330a6be7 [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
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053058cur_frm.cscript.account_head = function(doc, cdt, cdn) {
59 var d = locals[cdt][cdn];
60 if(!d.charge_type && d.account_head){
61 msgprint("Please select Charge Type first");
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053062 frappe.model.set_value(cdt, cdn, "account_head", "");
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053063 } else if(d.account_head && d.charge_type!=="Actual") {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053064 frappe.call({
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053065 type:"GET",
Nabin Haitce245122015-02-22 20:14:49 +053066 method: "erpnext.controllers.accounts_controller.get_tax_rate",
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053067 args: {"account_head":d.account_head},
68 callback: function(r) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +053069 frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0);
70 frappe.model.set_value(cdt, cdn, "description", r.message.account_name);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053071 }
72 })
73 }
74}
Nabin Haitce245122015-02-22 20:14:49 +053075
Nabin Hait613d0812015-02-23 11:58:15 +053076cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +053077 var d = locals[cdt][cdn];
Nabin Hait613d0812015-02-23 11:58:15 +053078 var msg = "";
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +053079
80 if(d.account_head && !d.description) {
81 // set description from account head
82 d.description = d.account_head.split(' - ').slice(0, -1).join(' - ');
83 }
84
Nabin Haitce245122015-02-22 20:14:49 +053085 if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
Nabin Hait613d0812015-02-23 11:58:15 +053086 msg = __("Please select Charge Type first");
Nabin Hait2b019ed2015-02-22 23:03:07 +053087 d.row_id = "";
88 d.rate = d.tax_amount = 0.0;
Nabin Haitce245122015-02-22 20:14:49 +053089 } else if((d.charge_type == 'Actual' || d.charge_type == 'On Net Total') && d.row_id) {
Nabin Hait613d0812015-02-23 11:58:15 +053090 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 +053091 d.row_id = "";
92 } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
93 if (d.idx == 1) {
Nabin Hait613d0812015-02-23 11:58:15 +053094 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 +053095 d.charge_type = '';
Nabin Hait613d0812015-02-23 11:58:15 +053096 } else if (!d.row_id) {
97 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
98 d.row_id = "";
99 } else if(d.row_id && d.row_id >= d.idx) {
100 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
Nabin Haitce245122015-02-22 20:14:49 +0530101 d.row_id = "";
102 }
103 }
Nabin Hait613d0812015-02-23 11:58:15 +0530104 if(msg) {
105 validated = false;
106 refresh_field("taxes");
107 frappe.throw(msg);
108 }
109
110}
111
112cur_frm.cscript.validate_inclusive_tax = function(tax) {
113 var actual_type_error = function() {
114 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
115 frappe.throw(msg);
116 };
117
118 var on_previous_row_error = function(row_range) {
119 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
120 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
121 frappe.throw(msg);
122 };
123
124 if(cint(tax.included_in_print_rate)) {
125 if(tax.charge_type == "Actual") {
126 // inclusive tax cannot be of type Actual
127 actual_type_error();
128 } else if(tax.charge_type == "On Previous Row Amount" &&
129 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)) {
130 // referred row should also be an inclusive tax
131 on_previous_row_error(tax.row_id);
132 } else if(tax.charge_type == "On Previous Row Total") {
133 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
134 function(t) { return cint(t.included_in_print_rate) ? null : t; });
135 if(taxes_not_included.length > 0) {
136 // all rows above this tax should be inclusive
137 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
138 }
Nabin Hait72c359a2015-02-24 16:05:19 +0530139 } else if(tax.category == "Valuation") {
140 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Nabin Hait613d0812015-02-23 11:58:15 +0530141 }
142 }
Nabin Haitce245122015-02-22 20:14:49 +0530143}
144
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530145if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
146 erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
Nabin Haitce245122015-02-22 20:14:49 +0530147
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530148 frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
Nabin Hait613d0812015-02-23 11:58:15 +0530149 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530150 });
151
152 frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
153 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
154 });
155
156 frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
157 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
158 });
159
160 frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530161 frm.cscript.validate_taxes_and_charges(cdt, cdn);
162 var open_form = frm.open_grid_row();
163 if(open_form) {
164 erpnext.taxes.set_conditional_mandatory_rate_or_amount(open_form);
165 } else {
166 // apply in current row
167 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_grid_row(cdn));
168 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530169 });
170
171 frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
172 var tax = frappe.get_doc(cdt, cdn);
173 try {
174 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
175 cur_frm.cscript.validate_inclusive_tax(tax);
176 } catch(e) {
177 tax.included_in_print_rate = 0;
178 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
179 throw e;
180 }
181 });
182}
183
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530184erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(grid_row) {
185 if(grid_row) {
186 if(grid_row.doc.charge_type==="Actual") {
187 grid_row.toggle_editable("tax_amount", true);
188 grid_row.toggle_reqd("tax_amount", true);
189 grid_row.toggle_editable("rate", false);
190 grid_row.toggle_reqd("rate", false);
191 } else {
192 grid_row.toggle_editable("rate", true);
193 grid_row.toggle_reqd("rate", true);
194 grid_row.toggle_editable("tax_amount", false);
195 grid_row.toggle_reqd("tax_amount", false);
196 }
Nabin Hait613d0812015-02-23 11:58:15 +0530197 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530198}
199
Nabin Haitce245122015-02-22 20:14:49 +0530200
201// For customizing print
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530202cur_frm.pformat.total = function(doc) { return ''; }
Nabin Haitce245122015-02-22 20:14:49 +0530203cur_frm.pformat.discount_amount = function(doc) { return ''; }
204cur_frm.pformat.grand_total = function(doc) { return ''; }
205cur_frm.pformat.rounded_total = function(doc) { return ''; }
206cur_frm.pformat.in_words = function(doc) { return ''; }
207
208cur_frm.pformat.taxes= function(doc){
209 //function to make row of table
Nabin Haitde9c8a92015-02-23 01:06:00 +0530210 var make_row = function(title, val, bold, is_negative) {
Nabin Haitce245122015-02-22 20:14:49 +0530211 var bstart = '<b>'; var bend = '</b>';
212 return '<tr><td style="width:50%;">' + (bold?bstart:'') + title + (bold?bend:'') + '</td>'
Nabin Haitde9c8a92015-02-23 01:06:00 +0530213 + '<td style="width:50%;text-align:right;">' + (is_negative ? '- ' : '')
214 + format_currency(val, doc.currency) + '</td></tr>';
Nabin Haitce245122015-02-22 20:14:49 +0530215 }
216
Nabin Haitce245122015-02-22 20:14:49 +0530217 function print_hide(fieldname) {
218 var doc_field = frappe.meta.get_docfield(doc.doctype, fieldname, doc.name);
219 return doc_field.print_hide;
220 }
221
222 out ='';
223 if (!doc.print_without_amount) {
224 var cl = doc.taxes || [];
225
226 // outer table
227 var out='<div><table class="noborder" style="width:100%"><tr><td style="width: 60%"></td><td>';
228
229 // main table
230
231 out +='<table class="noborder" style="width:100%">';
232
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530233 if(!print_hide('total')) {
234 out += make_row('Total', doc.total, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530235 }
236
Nabin Haitde9c8a92015-02-23 01:06:00 +0530237 // Discount Amount on net total
238 if(!print_hide('discount_amount') && doc.apply_discount_on == "Net Total" && doc.discount_amount)
239 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
240
Nabin Haitce245122015-02-22 20:14:49 +0530241 // add rows
242 if(cl.length){
243 for(var i=0;i<cl.length;i++) {
Nabin Hait755ff602015-02-23 01:12:09 +0530244 if(cl[i].tax_amount!=0 && !cl[i].included_in_print_rate)
245 out += make_row(cl[i].description, cl[i].tax_amount, 0);
Nabin Haitce245122015-02-22 20:14:49 +0530246 }
247 }
248
Nabin Haitde9c8a92015-02-23 01:06:00 +0530249 // Discount Amount on grand total
250 if(!print_hide('discount_amount') && doc.apply_discount_on == "Grand Total" && doc.discount_amount)
251 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530252
253 // grand total
254 if(!print_hide('grand_total'))
255 out += make_row('Grand Total', doc.grand_total, 1);
256
257 if(!print_hide('rounded_total'))
258 out += make_row('Rounded Total', doc.rounded_total, 1);
259
260 if(doc.in_words && !print_hide('in_words')) {
261 out +='</table></td></tr>';
262 out += '<tr><td colspan = "2">';
263 out += '<table><tr><td style="width:25%;"><b>In Words</b></td>';
264 out += '<td style="width:50%;">' + doc.in_words + '</td></tr>';
265 }
266 out += '</table></td></tr></table></div>';
267 }
268 return out;
269}