Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 1 | // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
| 2 | // License: GNU General Public License v3. See license.txt |
| 3 | |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 4 | // get tax rate |
| 5 | cur_frm.cscript.account_head = function(doc, cdt, cdn) { |
| 6 | var d = locals[cdt][cdn]; |
| 7 | if(!d.charge_type && d.account_head){ |
| 8 | msgprint("Please select Charge Type first"); |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 9 | frappe.model.set_value(cdt, cdn, "account_head", ""); |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 10 | } else if(d.account_head && d.charge_type!=="Actual") { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 11 | frappe.call({ |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 12 | type:"GET", |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 13 | method: "erpnext.controllers.accounts_controller.get_tax_rate", |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 14 | args: {"account_head":d.account_head}, |
| 15 | callback: function(r) { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 16 | frappe.model.set_value(cdt, cdn, "rate", r.message || 0); |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 17 | } |
| 18 | }) |
| 19 | } |
| 20 | } |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 21 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 22 | cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) { |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 23 | var d = locals[cdt][cdn]; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 24 | var msg = ""; |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 25 | if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) { |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 26 | msg = __("Please select Charge Type first"); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 27 | d.row_id = ""; |
| 28 | d.rate = d.tax_amount = 0.0; |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 29 | } else if((d.charge_type == 'Actual' || d.charge_type == 'On Net Total') && d.row_id) { |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 30 | msg = __("Can refer row only if the charge type is 'On Previous Row Amount' or 'Previous Row Total'"); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 31 | d.row_id = ""; |
| 32 | } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) { |
| 33 | if (d.idx == 1) { |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 34 | msg = __("Cannot select charge type as 'On Previous Row Amount' or 'On Previous Row Total' for first row"); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 35 | d.charge_type = ''; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 36 | } else if (!d.row_id) { |
| 37 | msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]); |
| 38 | d.row_id = ""; |
| 39 | } else if(d.row_id && d.row_id >= d.idx) { |
| 40 | msg = __("Cannot refer row number greater than or equal to current row number for this Charge type"); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 41 | d.row_id = ""; |
| 42 | } |
| 43 | } |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 44 | if(msg) { |
| 45 | validated = false; |
| 46 | refresh_field("taxes"); |
| 47 | frappe.throw(msg); |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | |
| 52 | cur_frm.cscript.validate_inclusive_tax = function(tax) { |
| 53 | var actual_type_error = function() { |
| 54 | var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx]) |
| 55 | frappe.throw(msg); |
| 56 | }; |
| 57 | |
| 58 | var on_previous_row_error = function(row_range) { |
| 59 | var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included", |
| 60 | [tax.idx, __(tax.doctype), tax.charge_type, row_range]) |
| 61 | frappe.throw(msg); |
| 62 | }; |
| 63 | |
| 64 | if(cint(tax.included_in_print_rate)) { |
| 65 | if(tax.charge_type == "Actual") { |
| 66 | // inclusive tax cannot be of type Actual |
| 67 | actual_type_error(); |
| 68 | } else if(tax.charge_type == "On Previous Row Amount" && |
| 69 | !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)) { |
| 70 | // referred row should also be an inclusive tax |
| 71 | on_previous_row_error(tax.row_id); |
| 72 | } else if(tax.charge_type == "On Previous Row Total") { |
| 73 | var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id), |
| 74 | function(t) { return cint(t.included_in_print_rate) ? null : t; }); |
| 75 | if(taxes_not_included.length > 0) { |
| 76 | // all rows above this tax should be inclusive |
| 77 | on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id); |
| 78 | } |
| 79 | } |
| 80 | } |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) { |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 84 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 85 | }); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 86 | |
| 87 | frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) { |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 88 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 89 | }); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 90 | |
| 91 | frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) { |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 92 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 93 | }); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 94 | |
| 95 | frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) { |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 96 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 97 | }); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 98 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 99 | frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) { |
| 100 | var tax = frappe.get_doc(cdt, cdn); |
| 101 | try { |
| 102 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
| 103 | cur_frm.cscript.validate_inclusive_tax(tax); |
| 104 | } catch(e) { |
| 105 | tax.included_in_print_rate = 0; |
| 106 | refresh_field("included_in_print_rate", tax.name, tax.parentfield); |
| 107 | throw e; |
| 108 | } |
| 109 | }); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 110 | |
| 111 | cur_frm.set_query("account_head", "taxes", function(doc) { |
| 112 | if(cur_frm.cscript.tax_table == "Sales Taxes and Charges") { |
| 113 | var account_type = ["Tax", "Chargeable", "Expense Account"]; |
| 114 | } else { |
| 115 | var account_type = ["Tax", "Chargeable", "Income Account"]; |
| 116 | } |
| 117 | |
| 118 | return { |
| 119 | query: "erpnext.controllers.queries.tax_account_query", |
| 120 | filters: { |
| 121 | "account_type": account_type, |
| 122 | "company": doc.company |
| 123 | } |
| 124 | } |
| 125 | }); |
| 126 | |
| 127 | cur_frm.set_query("cost_center", "taxes", function(doc) { |
| 128 | return { |
| 129 | filters: { |
| 130 | 'company': doc.company, |
| 131 | 'group_or_ledger': "Ledger" |
| 132 | } |
| 133 | } |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 134 | }); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 135 | |
| 136 | // For customizing print |
Nabin Hait | f0bc9b6 | 2015-02-23 01:40:01 +0530 | [diff] [blame] | 137 | cur_frm.pformat.total = function(doc) { return ''; } |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 138 | cur_frm.pformat.discount_amount = function(doc) { return ''; } |
| 139 | cur_frm.pformat.grand_total = function(doc) { return ''; } |
| 140 | cur_frm.pformat.rounded_total = function(doc) { return ''; } |
| 141 | cur_frm.pformat.in_words = function(doc) { return ''; } |
| 142 | |
| 143 | cur_frm.pformat.taxes= function(doc){ |
| 144 | //function to make row of table |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 145 | var make_row = function(title, val, bold, is_negative) { |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 146 | var bstart = '<b>'; var bend = '</b>'; |
| 147 | return '<tr><td style="width:50%;">' + (bold?bstart:'') + title + (bold?bend:'') + '</td>' |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 148 | + '<td style="width:50%;text-align:right;">' + (is_negative ? '- ' : '') |
| 149 | + format_currency(val, doc.currency) + '</td></tr>'; |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 150 | } |
| 151 | |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 152 | function print_hide(fieldname) { |
| 153 | var doc_field = frappe.meta.get_docfield(doc.doctype, fieldname, doc.name); |
| 154 | return doc_field.print_hide; |
| 155 | } |
| 156 | |
| 157 | out =''; |
| 158 | if (!doc.print_without_amount) { |
| 159 | var cl = doc.taxes || []; |
| 160 | |
| 161 | // outer table |
| 162 | var out='<div><table class="noborder" style="width:100%"><tr><td style="width: 60%"></td><td>'; |
| 163 | |
| 164 | // main table |
| 165 | |
| 166 | out +='<table class="noborder" style="width:100%">'; |
| 167 | |
Nabin Hait | f0bc9b6 | 2015-02-23 01:40:01 +0530 | [diff] [blame] | 168 | if(!print_hide('total')) { |
| 169 | out += make_row('Total', doc.total, 1); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 170 | } |
| 171 | |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 172 | // Discount Amount on net total |
| 173 | if(!print_hide('discount_amount') && doc.apply_discount_on == "Net Total" && doc.discount_amount) |
| 174 | out += make_row('Discount Amount', doc.discount_amount, 0, 1); |
| 175 | |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 176 | // add rows |
| 177 | if(cl.length){ |
| 178 | for(var i=0;i<cl.length;i++) { |
Nabin Hait | 755ff60 | 2015-02-23 01:12:09 +0530 | [diff] [blame] | 179 | if(cl[i].tax_amount!=0 && !cl[i].included_in_print_rate) |
| 180 | out += make_row(cl[i].description, cl[i].tax_amount, 0); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 184 | // Discount Amount on grand total |
| 185 | if(!print_hide('discount_amount') && doc.apply_discount_on == "Grand Total" && doc.discount_amount) |
| 186 | out += make_row('Discount Amount', doc.discount_amount, 0, 1); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 187 | |
| 188 | // grand total |
| 189 | if(!print_hide('grand_total')) |
| 190 | out += make_row('Grand Total', doc.grand_total, 1); |
| 191 | |
| 192 | if(!print_hide('rounded_total')) |
| 193 | out += make_row('Rounded Total', doc.rounded_total, 1); |
| 194 | |
| 195 | if(doc.in_words && !print_hide('in_words')) { |
| 196 | out +='</table></td></tr>'; |
| 197 | out += '<tr><td colspan = "2">'; |
| 198 | out += '<table><tr><td style="width:25%;"><b>In Words</b></td>'; |
| 199 | out += '<td style="width:50%;">' + doc.in_words + '</td></tr>'; |
| 200 | } |
| 201 | out += '</table></td></tr></table></div>'; |
| 202 | } |
| 203 | return out; |
| 204 | } |