blob: 212d2ae4f1b048c33d3d9a80e3d7efc4bdc77743 [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, {
9 onload: function(frm) {
10 if(frm.get_field("taxes")) {
11 frm.set_query("account_head", "taxes", function(doc) {
12 if(frm.cscript.tax_table == "Sales Taxes and Charges") {
13 var account_type = ["Tax", "Chargeable", "Expense Account"];
14 } else {
15 var account_type = ["Tax", "Chargeable", "Income Account"];
16 }
17
18 return {
19 query: "erpnext.controllers.queries.tax_account_query",
20 filters: {
21 "account_type": account_type,
22 "company": doc.company
23 }
24 }
25 });
26
27 frm.set_query("cost_center", "taxes", function(doc) {
28 return {
29 filters: {
30 'company': doc.company,
31 "is_group": 0
32 }
33 }
34 });
35 }
36 },
37 validate: function(frm) {
38 // neither is absolutely mandatory
39 // check mandatory based on charge_type
40 frm.get_docfield("taxes", "rate").reqd = 0;
41 frm.get_docfield("taxes", "tax_amount").reqd = 0;
42
Nabin Haitc91a9e52015-05-04 11:51:04 +053043 $.each(frm.doc.taxes || [], function(i, d) {
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053044 if(d.charge_type==="Actual") {
45 d.rate = 0;
46 if(!d.tax_amount) {
47 msgprint(__("Amount is mandatory for {0}", [d.account_head]));
48 validated = false;
49 }
50 } else {
51 d.tax_amount = 0;
52 if(!d.rate) {
53 msgprint(__("Rate is mandatory for {0}", [d.account_head]));
54 validated = false;
55 }
56 }
57 });
58 },
59 taxes_on_form_rendered: function(frm) {
60 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm);
61 }
62});
63
64
65
66
67
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053068
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053069cur_frm.cscript.account_head = function(doc, cdt, cdn) {
70 var d = locals[cdt][cdn];
71 if(!d.charge_type && d.account_head){
72 msgprint("Please select Charge Type first");
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053073 frappe.model.set_value(cdt, cdn, "account_head", "");
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053074 } else if(d.account_head && d.charge_type!=="Actual") {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053075 frappe.call({
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053076 type:"GET",
Nabin Haitce245122015-02-22 20:14:49 +053077 method: "erpnext.controllers.accounts_controller.get_tax_rate",
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053078 args: {"account_head":d.account_head},
79 callback: function(r) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053080 frappe.model.set_value(cdt, cdn, "rate", r.message || 0);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053081 }
82 })
83 }
84}
Nabin Haitce245122015-02-22 20:14:49 +053085
Nabin Hait613d0812015-02-23 11:58:15 +053086cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +053087 var d = locals[cdt][cdn];
Nabin Hait613d0812015-02-23 11:58:15 +053088 var msg = "";
Nabin Haitce245122015-02-22 20:14:49 +053089 if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
Nabin Hait613d0812015-02-23 11:58:15 +053090 msg = __("Please select Charge Type first");
Nabin Hait2b019ed2015-02-22 23:03:07 +053091 d.row_id = "";
92 d.rate = d.tax_amount = 0.0;
Nabin Haitce245122015-02-22 20:14:49 +053093 } else if((d.charge_type == 'Actual' || d.charge_type == 'On Net Total') && d.row_id) {
Nabin Hait613d0812015-02-23 11:58:15 +053094 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 +053095 d.row_id = "";
96 } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
97 if (d.idx == 1) {
Nabin Hait613d0812015-02-23 11:58:15 +053098 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 +053099 d.charge_type = '';
Nabin Hait613d0812015-02-23 11:58:15 +0530100 } else if (!d.row_id) {
101 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
102 d.row_id = "";
103 } else if(d.row_id && d.row_id >= d.idx) {
104 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
Nabin Haitce245122015-02-22 20:14:49 +0530105 d.row_id = "";
106 }
107 }
Nabin Hait613d0812015-02-23 11:58:15 +0530108 if(msg) {
109 validated = false;
110 refresh_field("taxes");
111 frappe.throw(msg);
112 }
113
114}
115
116cur_frm.cscript.validate_inclusive_tax = function(tax) {
117 var actual_type_error = function() {
118 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
119 frappe.throw(msg);
120 };
121
122 var on_previous_row_error = function(row_range) {
123 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
124 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
125 frappe.throw(msg);
126 };
127
128 if(cint(tax.included_in_print_rate)) {
129 if(tax.charge_type == "Actual") {
130 // inclusive tax cannot be of type Actual
131 actual_type_error();
132 } else if(tax.charge_type == "On Previous Row Amount" &&
133 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)) {
134 // referred row should also be an inclusive tax
135 on_previous_row_error(tax.row_id);
136 } else if(tax.charge_type == "On Previous Row Total") {
137 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
138 function(t) { return cint(t.included_in_print_rate) ? null : t; });
139 if(taxes_not_included.length > 0) {
140 // all rows above this tax should be inclusive
141 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
142 }
Nabin Hait72c359a2015-02-24 16:05:19 +0530143 } else if(tax.category == "Valuation") {
144 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Nabin Hait613d0812015-02-23 11:58:15 +0530145 }
146 }
Nabin Haitce245122015-02-22 20:14:49 +0530147}
148
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530149if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
150 erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
Nabin Haitce245122015-02-22 20:14:49 +0530151
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530152 frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
Nabin Hait613d0812015-02-23 11:58:15 +0530153 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530154 });
155
156 frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", 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, "tax_amount", function(frm, cdt, cdn) {
161 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
162 });
163
164 frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
165 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
166 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm);
167 });
168
169 frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
170 var tax = frappe.get_doc(cdt, cdn);
171 try {
172 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
173 cur_frm.cscript.validate_inclusive_tax(tax);
174 } catch(e) {
175 tax.included_in_print_rate = 0;
176 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
177 throw e;
178 }
179 });
180}
181
182erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(frm) {
183 var grid_row = frm.open_grid_row();
184 if(grid_row.doc.charge_type==="Actual") {
185 grid_row.toggle_display("tax_amount", true);
186 grid_row.toggle_reqd("tax_amount", true);
187 grid_row.toggle_display("rate", false);
188 grid_row.toggle_reqd("rate", false);
189 } else {
190 grid_row.toggle_display("rate", true);
191 grid_row.toggle_reqd("rate", true);
192 grid_row.toggle_display("tax_amount", false);
193 grid_row.toggle_reqd("tax_amount", false);
Nabin Hait613d0812015-02-23 11:58:15 +0530194 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530195}
196
Nabin Haitce245122015-02-22 20:14:49 +0530197
198// For customizing print
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530199cur_frm.pformat.total = function(doc) { return ''; }
Nabin Haitce245122015-02-22 20:14:49 +0530200cur_frm.pformat.discount_amount = function(doc) { return ''; }
201cur_frm.pformat.grand_total = function(doc) { return ''; }
202cur_frm.pformat.rounded_total = function(doc) { return ''; }
203cur_frm.pformat.in_words = function(doc) { return ''; }
204
205cur_frm.pformat.taxes= function(doc){
206 //function to make row of table
Nabin Haitde9c8a92015-02-23 01:06:00 +0530207 var make_row = function(title, val, bold, is_negative) {
Nabin Haitce245122015-02-22 20:14:49 +0530208 var bstart = '<b>'; var bend = '</b>';
209 return '<tr><td style="width:50%;">' + (bold?bstart:'') + title + (bold?bend:'') + '</td>'
Nabin Haitde9c8a92015-02-23 01:06:00 +0530210 + '<td style="width:50%;text-align:right;">' + (is_negative ? '- ' : '')
211 + format_currency(val, doc.currency) + '</td></tr>';
Nabin Haitce245122015-02-22 20:14:49 +0530212 }
213
Nabin Haitce245122015-02-22 20:14:49 +0530214 function print_hide(fieldname) {
215 var doc_field = frappe.meta.get_docfield(doc.doctype, fieldname, doc.name);
216 return doc_field.print_hide;
217 }
218
219 out ='';
220 if (!doc.print_without_amount) {
221 var cl = doc.taxes || [];
222
223 // outer table
224 var out='<div><table class="noborder" style="width:100%"><tr><td style="width: 60%"></td><td>';
225
226 // main table
227
228 out +='<table class="noborder" style="width:100%">';
229
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530230 if(!print_hide('total')) {
231 out += make_row('Total', doc.total, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530232 }
233
Nabin Haitde9c8a92015-02-23 01:06:00 +0530234 // Discount Amount on net total
235 if(!print_hide('discount_amount') && doc.apply_discount_on == "Net Total" && doc.discount_amount)
236 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
237
Nabin Haitce245122015-02-22 20:14:49 +0530238 // add rows
239 if(cl.length){
240 for(var i=0;i<cl.length;i++) {
Nabin Hait755ff602015-02-23 01:12:09 +0530241 if(cl[i].tax_amount!=0 && !cl[i].included_in_print_rate)
242 out += make_row(cl[i].description, cl[i].tax_amount, 0);
Nabin Haitce245122015-02-22 20:14:49 +0530243 }
244 }
245
Nabin Haitde9c8a92015-02-23 01:06:00 +0530246 // Discount Amount on grand total
247 if(!print_hide('discount_amount') && doc.apply_discount_on == "Grand Total" && doc.discount_amount)
248 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530249
250 // grand total
251 if(!print_hide('grand_total'))
252 out += make_row('Grand Total', doc.grand_total, 1);
253
254 if(!print_hide('rounded_total'))
255 out += make_row('Rounded Total', doc.rounded_total, 1);
256
257 if(doc.in_words && !print_hide('in_words')) {
258 out +='</table></td></tr>';
259 out += '<tr><td colspan = "2">';
260 out += '<table><tr><td style="width:25%;"><b>In Words</b></td>';
261 out += '<td style="width:50%;">' + doc.in_words + '</td></tr>';
262 }
263 out += '</table></td></tr></table></div>';
264 }
265 return out;
266}