blob: 2ea4bacc4cca70a3256e9286f2097930e9828032 [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
8
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +05309cur_frm.cscript.account_head = function(doc, cdt, cdn) {
10 var d = locals[cdt][cdn];
11 if(!d.charge_type && d.account_head){
12 msgprint("Please select Charge Type first");
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053013 frappe.model.set_value(cdt, cdn, "account_head", "");
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053014 } else if(d.account_head && d.charge_type!=="Actual") {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053015 frappe.call({
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053016 type:"GET",
Nabin Haitce245122015-02-22 20:14:49 +053017 method: "erpnext.controllers.accounts_controller.get_tax_rate",
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053018 args: {"account_head":d.account_head},
19 callback: function(r) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053020 frappe.model.set_value(cdt, cdn, "rate", r.message || 0);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053021 }
22 })
23 }
24}
Nabin Haitce245122015-02-22 20:14:49 +053025
Nabin Hait613d0812015-02-23 11:58:15 +053026cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +053027 var d = locals[cdt][cdn];
Nabin Hait613d0812015-02-23 11:58:15 +053028 var msg = "";
Nabin Haitce245122015-02-22 20:14:49 +053029 if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
Nabin Hait613d0812015-02-23 11:58:15 +053030 msg = __("Please select Charge Type first");
Nabin Hait2b019ed2015-02-22 23:03:07 +053031 d.row_id = "";
32 d.rate = d.tax_amount = 0.0;
Nabin Haitce245122015-02-22 20:14:49 +053033 } else if((d.charge_type == 'Actual' || d.charge_type == 'On Net Total') && d.row_id) {
Nabin Hait613d0812015-02-23 11:58:15 +053034 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 +053035 d.row_id = "";
36 } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
37 if (d.idx == 1) {
Nabin Hait613d0812015-02-23 11:58:15 +053038 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 +053039 d.charge_type = '';
Nabin Hait613d0812015-02-23 11:58:15 +053040 } else if (!d.row_id) {
41 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
42 d.row_id = "";
43 } else if(d.row_id && d.row_id >= d.idx) {
44 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
Nabin Haitce245122015-02-22 20:14:49 +053045 d.row_id = "";
46 }
47 }
Nabin Hait613d0812015-02-23 11:58:15 +053048 if(msg) {
49 validated = false;
50 refresh_field("taxes");
51 frappe.throw(msg);
52 }
53
54}
55
56cur_frm.cscript.validate_inclusive_tax = function(tax) {
57 var actual_type_error = function() {
58 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
59 frappe.throw(msg);
60 };
61
62 var on_previous_row_error = function(row_range) {
63 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
64 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
65 frappe.throw(msg);
66 };
67
68 if(cint(tax.included_in_print_rate)) {
69 if(tax.charge_type == "Actual") {
70 // inclusive tax cannot be of type Actual
71 actual_type_error();
72 } else if(tax.charge_type == "On Previous Row Amount" &&
73 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)) {
74 // referred row should also be an inclusive tax
75 on_previous_row_error(tax.row_id);
76 } else if(tax.charge_type == "On Previous Row Total") {
77 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
78 function(t) { return cint(t.included_in_print_rate) ? null : t; });
79 if(taxes_not_included.length > 0) {
80 // all rows above this tax should be inclusive
81 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
82 }
Nabin Hait72c359a2015-02-24 16:05:19 +053083 } else if(tax.category == "Valuation") {
84 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Nabin Hait613d0812015-02-23 11:58:15 +053085 }
86 }
Nabin Haitce245122015-02-22 20:14:49 +053087}
88
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053089if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
90 erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
Nabin Haitce245122015-02-22 20:14:49 +053091
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053092 frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
Nabin Hait613d0812015-02-23 11:58:15 +053093 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053094 });
95
96 frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
97 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
98 });
99
100 frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
101 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
102 });
103
104 frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
105 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
106 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm);
107 });
108
109 frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
110 var tax = frappe.get_doc(cdt, cdn);
111 try {
112 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
113 cur_frm.cscript.validate_inclusive_tax(tax);
114 } catch(e) {
115 tax.included_in_print_rate = 0;
116 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
117 throw e;
118 }
119 });
120}
121
122erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(frm) {
123 var grid_row = frm.open_grid_row();
124 if(grid_row.doc.charge_type==="Actual") {
125 grid_row.toggle_display("tax_amount", true);
126 grid_row.toggle_reqd("tax_amount", true);
127 grid_row.toggle_display("rate", false);
128 grid_row.toggle_reqd("rate", false);
129 } else {
130 grid_row.toggle_display("rate", true);
131 grid_row.toggle_reqd("rate", true);
132 grid_row.toggle_display("tax_amount", false);
133 grid_row.toggle_reqd("tax_amount", false);
Nabin Hait613d0812015-02-23 11:58:15 +0530134 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530135}
136
137// setup conditional mandatory for tax and rates
138frappe.ui.form.on(cur_frm.doctype, "taxes_on_form_rendered", function(frm) {
139 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm);
Nabin Hait613d0812015-02-23 11:58:15 +0530140});
Nabin Haitce245122015-02-22 20:14:49 +0530141
Rushabh Mehta8c84fca2015-03-02 16:29:56 +0530142// setup queries for taxes
143frappe.ui.form.on(cur_frm.doctype, "onload", function(frm) {
144 if(frm.get_field("taxes")) {
145 frm.set_query("account_head", "taxes", function(doc) {
146 if(frm.cscript.tax_table == "Sales Taxes and Charges") {
147 var account_type = ["Tax", "Chargeable", "Expense Account"];
148 } else {
149 var account_type = ["Tax", "Chargeable", "Income Account"];
150 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530151
Rushabh Mehta8c84fca2015-03-02 16:29:56 +0530152 return {
153 query: "erpnext.controllers.queries.tax_account_query",
154 filters: {
155 "account_type": account_type,
156 "company": doc.company
157 }
158 }
159 });
Nabin Haitce245122015-02-22 20:14:49 +0530160
Rushabh Mehta8c84fca2015-03-02 16:29:56 +0530161 frm.set_query("cost_center", "taxes", function(doc) {
162 return {
163 filters: {
164 'company': doc.company,
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530165 "is_group": 0
Rushabh Mehta8c84fca2015-03-02 16:29:56 +0530166 }
167 }
168 });
Nabin Haitce245122015-02-22 20:14:49 +0530169 }
170});
171
Rushabh Mehta8c84fca2015-03-02 16:29:56 +0530172
Nabin Haitce245122015-02-22 20:14:49 +0530173
174// For customizing print
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530175cur_frm.pformat.total = function(doc) { return ''; }
Nabin Haitce245122015-02-22 20:14:49 +0530176cur_frm.pformat.discount_amount = function(doc) { return ''; }
177cur_frm.pformat.grand_total = function(doc) { return ''; }
178cur_frm.pformat.rounded_total = function(doc) { return ''; }
179cur_frm.pformat.in_words = function(doc) { return ''; }
180
181cur_frm.pformat.taxes= function(doc){
182 //function to make row of table
Nabin Haitde9c8a92015-02-23 01:06:00 +0530183 var make_row = function(title, val, bold, is_negative) {
Nabin Haitce245122015-02-22 20:14:49 +0530184 var bstart = '<b>'; var bend = '</b>';
185 return '<tr><td style="width:50%;">' + (bold?bstart:'') + title + (bold?bend:'') + '</td>'
Nabin Haitde9c8a92015-02-23 01:06:00 +0530186 + '<td style="width:50%;text-align:right;">' + (is_negative ? '- ' : '')
187 + format_currency(val, doc.currency) + '</td></tr>';
Nabin Haitce245122015-02-22 20:14:49 +0530188 }
189
Nabin Haitce245122015-02-22 20:14:49 +0530190 function print_hide(fieldname) {
191 var doc_field = frappe.meta.get_docfield(doc.doctype, fieldname, doc.name);
192 return doc_field.print_hide;
193 }
194
195 out ='';
196 if (!doc.print_without_amount) {
197 var cl = doc.taxes || [];
198
199 // outer table
200 var out='<div><table class="noborder" style="width:100%"><tr><td style="width: 60%"></td><td>';
201
202 // main table
203
204 out +='<table class="noborder" style="width:100%">';
205
Nabin Haitf0bc9b62015-02-23 01:40:01 +0530206 if(!print_hide('total')) {
207 out += make_row('Total', doc.total, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530208 }
209
Nabin Haitde9c8a92015-02-23 01:06:00 +0530210 // Discount Amount on net total
211 if(!print_hide('discount_amount') && doc.apply_discount_on == "Net Total" && doc.discount_amount)
212 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
213
Nabin Haitce245122015-02-22 20:14:49 +0530214 // add rows
215 if(cl.length){
216 for(var i=0;i<cl.length;i++) {
Nabin Hait755ff602015-02-23 01:12:09 +0530217 if(cl[i].tax_amount!=0 && !cl[i].included_in_print_rate)
218 out += make_row(cl[i].description, cl[i].tax_amount, 0);
Nabin Haitce245122015-02-22 20:14:49 +0530219 }
220 }
221
Nabin Haitde9c8a92015-02-23 01:06:00 +0530222 // Discount Amount on grand total
223 if(!print_hide('discount_amount') && doc.apply_discount_on == "Grand Total" && doc.discount_amount)
224 out += make_row('Discount Amount', doc.discount_amount, 0, 1);
Nabin Haitce245122015-02-22 20:14:49 +0530225
226 // grand total
227 if(!print_hide('grand_total'))
228 out += make_row('Grand Total', doc.grand_total, 1);
229
230 if(!print_hide('rounded_total'))
231 out += make_row('Rounded Total', doc.rounded_total, 1);
232
233 if(doc.in_words && !print_hide('in_words')) {
234 out +='</table></td></tr>';
235 out += '<tr><td colspan = "2">';
236 out += '<table><tr><td style="width:25%;"><b>In Words</b></td>';
237 out += '<td style="width:50%;">' + doc.in_words + '</td></tr>';
238 }
239 out += '</table></td></tr></table></div>';
240 }
241 return out;
242}