blob: e19919d261b77e92b12e3207d7668d66960fc1d6 [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
Nabin Hait0e1540b2015-05-05 17:26:35 +053039 if(frm.get_docfield("taxes")) {
40 frm.get_docfield("taxes", "rate").reqd = 0;
41 frm.get_docfield("taxes", "tax_amount").reqd = 0;
42 }
43
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053044 },
45 taxes_on_form_rendered: function(frm) {
46 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm);
47 }
48});
49
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053050cur_frm.cscript.account_head = function(doc, cdt, cdn) {
51 var d = locals[cdt][cdn];
52 if(!d.charge_type && d.account_head){
53 msgprint("Please select Charge Type first");
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053054 frappe.model.set_value(cdt, cdn, "account_head", "");
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053055 } else if(d.account_head && d.charge_type!=="Actual") {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053056 frappe.call({
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053057 type:"GET",
Nabin Haitce245122015-02-22 20:14:49 +053058 method: "erpnext.controllers.accounts_controller.get_tax_rate",
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053059 args: {"account_head":d.account_head},
60 callback: function(r) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053061 frappe.model.set_value(cdt, cdn, "rate", r.message || 0);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053062 }
63 })
64 }
65}
Nabin Haitce245122015-02-22 20:14:49 +053066
Nabin Hait613d0812015-02-23 11:58:15 +053067cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +053068 var d = locals[cdt][cdn];
Nabin Hait613d0812015-02-23 11:58:15 +053069 var msg = "";
Nabin Haitce245122015-02-22 20:14:49 +053070 if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
Nabin Hait613d0812015-02-23 11:58:15 +053071 msg = __("Please select Charge Type first");
Nabin Hait2b019ed2015-02-22 23:03:07 +053072 d.row_id = "";
73 d.rate = d.tax_amount = 0.0;
Nabin Haitce245122015-02-22 20:14:49 +053074 } else if((d.charge_type == 'Actual' || d.charge_type == 'On Net Total') && d.row_id) {
Nabin Hait613d0812015-02-23 11:58:15 +053075 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 +053076 d.row_id = "";
77 } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
78 if (d.idx == 1) {
Nabin Hait613d0812015-02-23 11:58:15 +053079 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 +053080 d.charge_type = '';
Nabin Hait613d0812015-02-23 11:58:15 +053081 } else if (!d.row_id) {
82 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
83 d.row_id = "";
84 } else if(d.row_id && d.row_id >= d.idx) {
85 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
Nabin Haitce245122015-02-22 20:14:49 +053086 d.row_id = "";
87 }
88 }
Nabin Hait613d0812015-02-23 11:58:15 +053089 if(msg) {
90 validated = false;
91 refresh_field("taxes");
92 frappe.throw(msg);
93 }
94
95}
96
97cur_frm.cscript.validate_inclusive_tax = function(tax) {
98 var actual_type_error = function() {
99 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
100 frappe.throw(msg);
101 };
102
103 var on_previous_row_error = function(row_range) {
104 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
105 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
106 frappe.throw(msg);
107 };
108
109 if(cint(tax.included_in_print_rate)) {
110 if(tax.charge_type == "Actual") {
111 // inclusive tax cannot be of type Actual
112 actual_type_error();
113 } else if(tax.charge_type == "On Previous Row Amount" &&
114 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)) {
115 // referred row should also be an inclusive tax
116 on_previous_row_error(tax.row_id);
117 } else if(tax.charge_type == "On Previous Row Total") {
118 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
119 function(t) { return cint(t.included_in_print_rate) ? null : t; });
120 if(taxes_not_included.length > 0) {
121 // all rows above this tax should be inclusive
122 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
123 }
Nabin Hait72c359a2015-02-24 16:05:19 +0530124 } else if(tax.category == "Valuation") {
125 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Nabin Hait613d0812015-02-23 11:58:15 +0530126 }
127 }
Nabin Haitce245122015-02-22 20:14:49 +0530128}
129
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530130if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
131 erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
Nabin Haitce245122015-02-22 20:14:49 +0530132
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530133 frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
Nabin Hait613d0812015-02-23 11:58:15 +0530134 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530135 });
136
137 frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
138 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
139 });
140
141 frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
142 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
143 });
144
145 frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
146 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
147 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm);
148 });
149
150 frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
151 var tax = frappe.get_doc(cdt, cdn);
152 try {
153 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
154 cur_frm.cscript.validate_inclusive_tax(tax);
155 } catch(e) {
156 tax.included_in_print_rate = 0;
157 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
158 throw e;
159 }
160 });
161}
162
163erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(frm) {
164 var grid_row = frm.open_grid_row();
165 if(grid_row.doc.charge_type==="Actual") {
166 grid_row.toggle_display("tax_amount", true);
167 grid_row.toggle_reqd("tax_amount", true);
168 grid_row.toggle_display("rate", false);
169 grid_row.toggle_reqd("rate", false);
170 } else {
171 grid_row.toggle_display("rate", true);
172 grid_row.toggle_reqd("rate", true);
173 grid_row.toggle_display("tax_amount", false);
174 grid_row.toggle_reqd("tax_amount", false);
Nabin Hait613d0812015-02-23 11:58:15 +0530175 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530176}
177
Nabin Haitce245122015-02-22 20:14:49 +0530178
179// For customizing print
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530180cur_frm.pformat.total = function(doc) { return ''; }
Nabin Haitce245122015-02-22 20:14:49 +0530181cur_frm.pformat.discount_amount = function(doc) { return ''; }
182cur_frm.pformat.grand_total = function(doc) { return ''; }
183cur_frm.pformat.rounded_total = function(doc) { return ''; }
184cur_frm.pformat.in_words = function(doc) { return ''; }
185
186cur_frm.pformat.taxes= function(doc){
187 //function to make row of table
Nabin Haitde9c8a92015-02-23 01:06:00 +0530188 var make_row = function(title, val, bold, is_negative) {
Nabin Haitce245122015-02-22 20:14:49 +0530189 var bstart = '<b>'; var bend = '</b>';
190 return '<tr><td style="width:50%;">' + (bold?bstart:'') + title + (bold?bend:'') + '</td>'
Nabin Haitde9c8a92015-02-23 01:06:00 +0530191 + '<td style="width:50%;text-align:right;">' + (is_negative ? '- ' : '')
192 + format_currency(val, doc.currency) + '</td></tr>';
Nabin Haitce245122015-02-22 20:14:49 +0530193 }
194
Nabin Haitce245122015-02-22 20:14:49 +0530195 function print_hide(fieldname) {
196 var doc_field = frappe.meta.get_docfield(doc.doctype, fieldname, doc.name);
197 return doc_field.print_hide;
198 }
199
200 out ='';
201 if (!doc.print_without_amount) {
202 var cl = doc.taxes || [];
203
204 // outer table
205 var out='<div><table class="noborder" style="width:100%"><tr><td style="width: 60%"></td><td>';
206
207 // main table
208
209 out +='<table class="noborder" style="width:100%">';
210
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530211 if(!print_hide('total')) {
212 out += make_row('Total', doc.total, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530213 }
214
Nabin Haitde9c8a92015-02-23 01:06:00 +0530215 // Discount Amount on net total
216 if(!print_hide('discount_amount') && doc.apply_discount_on == "Net Total" && doc.discount_amount)
217 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
218
Nabin Haitce245122015-02-22 20:14:49 +0530219 // add rows
220 if(cl.length){
221 for(var i=0;i<cl.length;i++) {
Nabin Hait755ff602015-02-23 01:12:09 +0530222 if(cl[i].tax_amount!=0 && !cl[i].included_in_print_rate)
223 out += make_row(cl[i].description, cl[i].tax_amount, 0);
Nabin Haitce245122015-02-22 20:14:49 +0530224 }
225 }
226
Nabin Haitde9c8a92015-02-23 01:06:00 +0530227 // Discount Amount on grand total
228 if(!print_hide('discount_amount') && doc.apply_discount_on == "Grand Total" && doc.discount_amount)
229 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530230
231 // grand total
232 if(!print_hide('grand_total'))
233 out += make_row('Grand Total', doc.grand_total, 1);
234
235 if(!print_hide('rounded_total'))
236 out += make_row('Rounded Total', doc.rounded_total, 1);
237
238 if(doc.in_words && !print_hide('in_words')) {
239 out +='</table></td></tr>';
240 out += '<tr><td colspan = "2">';
241 out += '<table><tr><td style="width:25%;"><b>In Words</b></td>';
242 out += '<td style="width:50%;">' + doc.in_words + '</td></tr>';
243 }
244 out += '</table></td></tr></table></div>';
245 }
246 return out;
247}