Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 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 |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 5 | frappe.provide("erpnext.taxes"); |
| 6 | frappe.provide("erpnext.taxes.flags"); |
| 7 | |
Rushabh Mehta | c712cdb | 2015-05-03 22:20:44 +0530 | [diff] [blame] | 8 | frappe.ui.form.on(cur_frm.doctype, { |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 9 | 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 Mehta | c712cdb | 2015-05-03 22:20:44 +0530 | [diff] [blame] | 17 | 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 Hait | 0e1540b | 2015-05-05 17:26:35 +0530 | [diff] [blame] | 47 | if(frm.get_docfield("taxes")) { |
| 48 | frm.get_docfield("taxes", "rate").reqd = 0; |
| 49 | frm.get_docfield("taxes", "tax_amount").reqd = 0; |
| 50 | } |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 51 | |
Rushabh Mehta | c712cdb | 2015-05-03 22:20:44 +0530 | [diff] [blame] | 52 | }, |
| 53 | taxes_on_form_rendered: function(frm) { |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 54 | erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.open_grid_row()); |
Rushabh Mehta | c712cdb | 2015-05-03 22:20:44 +0530 | [diff] [blame] | 55 | } |
| 56 | }); |
| 57 | |
Rohit Waghchaure | 70be24d | 2016-08-08 22:54:38 +0530 | [diff] [blame] | 58 | frappe.ui.form.on('Sales Invoice Payment', { |
| 59 | mode_of_payment: function(frm, cdt, cdn) { |
| 60 | var d = locals[cdt][cdn]; |
| 61 | get_payment_mode_account(frm, d.mode_of_payment, function(account){ |
| 62 | frappe.model.set_value(cdt, cdn, 'account', account) |
| 63 | }) |
| 64 | } |
| 65 | }) |
| 66 | |
| 67 | frappe.ui.form.on('Purchase Invoice', { |
| 68 | mode_of_payment: function(frm) { |
| 69 | get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){ |
| 70 | frm.set_value('cash_bank_account', account); |
| 71 | }) |
| 72 | } |
| 73 | }) |
| 74 | |
| 75 | frappe.ui.form.on('Payment Entry', { |
| 76 | mode_of_payment: function(frm) { |
| 77 | get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){ |
| 78 | var payment_account_field = frm.doc.payment_type == "Receive" ? "paid_to" : "paid_from"; |
| 79 | frm.set_value(payment_account_field, account); |
| 80 | }) |
| 81 | } |
| 82 | }) |
| 83 | |
| 84 | get_payment_mode_account = function(frm, mode_of_payment, callback){ |
| 85 | return frappe.call({ |
| 86 | method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account", |
| 87 | args: { |
| 88 | "mode_of_payment": mode_of_payment, |
| 89 | "company": frm.doc.company |
| 90 | }, |
| 91 | callback: function(r, rt) { |
| 92 | if(r.message) { |
| 93 | callback(r.message.account) |
| 94 | } |
| 95 | } |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 100 | cur_frm.cscript.account_head = function(doc, cdt, cdn) { |
| 101 | var d = locals[cdt][cdn]; |
| 102 | if(!d.charge_type && d.account_head){ |
| 103 | msgprint("Please select Charge Type first"); |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 104 | frappe.model.set_value(cdt, cdn, "account_head", ""); |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 105 | } else if(d.account_head && d.charge_type!=="Actual") { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 106 | frappe.call({ |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 107 | type:"GET", |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 108 | method: "erpnext.controllers.accounts_controller.get_tax_rate", |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 109 | args: {"account_head":d.account_head}, |
| 110 | callback: function(r) { |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 111 | frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0); |
| 112 | frappe.model.set_value(cdt, cdn, "description", r.message.account_name); |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 113 | } |
| 114 | }) |
| 115 | } |
| 116 | } |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 117 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 118 | cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) { |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 119 | var d = locals[cdt][cdn]; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 120 | var msg = ""; |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 121 | |
| 122 | if(d.account_head && !d.description) { |
| 123 | // set description from account head |
| 124 | d.description = d.account_head.split(' - ').slice(0, -1).join(' - '); |
| 125 | } |
| 126 | |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 127 | if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) { |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 128 | msg = __("Please select Charge Type first"); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 129 | d.row_id = ""; |
| 130 | d.rate = d.tax_amount = 0.0; |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 131 | } 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] | 132 | 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] | 133 | d.row_id = ""; |
| 134 | } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) { |
| 135 | if (d.idx == 1) { |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 136 | 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] | 137 | d.charge_type = ''; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 138 | } else if (!d.row_id) { |
| 139 | msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]); |
| 140 | d.row_id = ""; |
| 141 | } else if(d.row_id && d.row_id >= d.idx) { |
| 142 | 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] | 143 | d.row_id = ""; |
| 144 | } |
| 145 | } |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 146 | if(msg) { |
| 147 | validated = false; |
| 148 | refresh_field("taxes"); |
| 149 | frappe.throw(msg); |
| 150 | } |
| 151 | |
| 152 | } |
| 153 | |
| 154 | cur_frm.cscript.validate_inclusive_tax = function(tax) { |
| 155 | var actual_type_error = function() { |
| 156 | var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx]) |
| 157 | frappe.throw(msg); |
| 158 | }; |
| 159 | |
| 160 | var on_previous_row_error = function(row_range) { |
| 161 | var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included", |
| 162 | [tax.idx, __(tax.doctype), tax.charge_type, row_range]) |
| 163 | frappe.throw(msg); |
| 164 | }; |
| 165 | |
| 166 | if(cint(tax.included_in_print_rate)) { |
| 167 | if(tax.charge_type == "Actual") { |
| 168 | // inclusive tax cannot be of type Actual |
| 169 | actual_type_error(); |
| 170 | } else if(tax.charge_type == "On Previous Row Amount" && |
| 171 | !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)) { |
| 172 | // referred row should also be an inclusive tax |
| 173 | on_previous_row_error(tax.row_id); |
| 174 | } else if(tax.charge_type == "On Previous Row Total") { |
| 175 | var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id), |
| 176 | function(t) { return cint(t.included_in_print_rate) ? null : t; }); |
| 177 | if(taxes_not_included.length > 0) { |
| 178 | // all rows above this tax should be inclusive |
| 179 | on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id); |
| 180 | } |
Nabin Hait | 72c359a | 2015-02-24 16:05:19 +0530 | [diff] [blame] | 181 | } else if(tax.category == "Valuation") { |
| 182 | frappe.throw(__("Valuation type charges can not marked as Inclusive")); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 183 | } |
| 184 | } |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 185 | } |
| 186 | |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 187 | if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) { |
| 188 | erpnext.taxes.flags[cur_frm.cscript.tax_table] = true; |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 189 | |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 190 | 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] | 191 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 192 | }); |
| 193 | |
| 194 | frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) { |
| 195 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
| 196 | }); |
| 197 | |
| 198 | frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) { |
| 199 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
| 200 | }); |
| 201 | |
| 202 | frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) { |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 203 | frm.cscript.validate_taxes_and_charges(cdt, cdn); |
| 204 | var open_form = frm.open_grid_row(); |
| 205 | if(open_form) { |
| 206 | erpnext.taxes.set_conditional_mandatory_rate_or_amount(open_form); |
| 207 | } else { |
| 208 | // apply in current row |
| 209 | erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_grid_row(cdn)); |
| 210 | } |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 211 | }); |
| 212 | |
| 213 | frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) { |
| 214 | var tax = frappe.get_doc(cdt, cdn); |
| 215 | try { |
| 216 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
| 217 | cur_frm.cscript.validate_inclusive_tax(tax); |
| 218 | } catch(e) { |
| 219 | tax.included_in_print_rate = 0; |
| 220 | refresh_field("included_in_print_rate", tax.name, tax.parentfield); |
| 221 | throw e; |
| 222 | } |
| 223 | }); |
| 224 | } |
| 225 | |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 226 | erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(grid_row) { |
| 227 | if(grid_row) { |
| 228 | if(grid_row.doc.charge_type==="Actual") { |
| 229 | grid_row.toggle_editable("tax_amount", true); |
| 230 | grid_row.toggle_reqd("tax_amount", true); |
| 231 | grid_row.toggle_editable("rate", false); |
| 232 | grid_row.toggle_reqd("rate", false); |
| 233 | } else { |
| 234 | grid_row.toggle_editable("rate", true); |
| 235 | grid_row.toggle_reqd("rate", true); |
| 236 | grid_row.toggle_editable("tax_amount", false); |
| 237 | grid_row.toggle_reqd("tax_amount", false); |
| 238 | } |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 239 | } |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 240 | } |
| 241 | |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 242 | |
| 243 | // For customizing print |
Nabin Hait | f0bc9b6 | 2015-02-23 01:40:01 +0530 | [diff] [blame] | 244 | cur_frm.pformat.total = function(doc) { return ''; } |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 245 | cur_frm.pformat.discount_amount = function(doc) { return ''; } |
| 246 | cur_frm.pformat.grand_total = function(doc) { return ''; } |
| 247 | cur_frm.pformat.rounded_total = function(doc) { return ''; } |
| 248 | cur_frm.pformat.in_words = function(doc) { return ''; } |
| 249 | |
| 250 | cur_frm.pformat.taxes= function(doc){ |
| 251 | //function to make row of table |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 252 | var make_row = function(title, val, bold, is_negative) { |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 253 | var bstart = '<b>'; var bend = '</b>'; |
| 254 | return '<tr><td style="width:50%;">' + (bold?bstart:'') + title + (bold?bend:'') + '</td>' |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 255 | + '<td style="width:50%;text-align:right;">' + (is_negative ? '- ' : '') |
| 256 | + format_currency(val, doc.currency) + '</td></tr>'; |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 257 | } |
| 258 | |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 259 | function print_hide(fieldname) { |
| 260 | var doc_field = frappe.meta.get_docfield(doc.doctype, fieldname, doc.name); |
| 261 | return doc_field.print_hide; |
| 262 | } |
| 263 | |
| 264 | out =''; |
| 265 | if (!doc.print_without_amount) { |
| 266 | var cl = doc.taxes || []; |
| 267 | |
| 268 | // outer table |
| 269 | var out='<div><table class="noborder" style="width:100%"><tr><td style="width: 60%"></td><td>'; |
| 270 | |
| 271 | // main table |
| 272 | |
| 273 | out +='<table class="noborder" style="width:100%">'; |
| 274 | |
Nabin Hait | f0bc9b6 | 2015-02-23 01:40:01 +0530 | [diff] [blame] | 275 | if(!print_hide('total')) { |
| 276 | out += make_row('Total', doc.total, 1); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 277 | } |
| 278 | |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 279 | // Discount Amount on net total |
| 280 | if(!print_hide('discount_amount') && doc.apply_discount_on == "Net Total" && doc.discount_amount) |
| 281 | out += make_row('Discount Amount', doc.discount_amount, 0, 1); |
| 282 | |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 283 | // add rows |
| 284 | if(cl.length){ |
| 285 | for(var i=0;i<cl.length;i++) { |
Nabin Hait | 755ff60 | 2015-02-23 01:12:09 +0530 | [diff] [blame] | 286 | if(cl[i].tax_amount!=0 && !cl[i].included_in_print_rate) |
| 287 | out += make_row(cl[i].description, cl[i].tax_amount, 0); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 291 | // Discount Amount on grand total |
| 292 | if(!print_hide('discount_amount') && doc.apply_discount_on == "Grand Total" && doc.discount_amount) |
| 293 | out += make_row('Discount Amount', doc.discount_amount, 0, 1); |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 294 | |
| 295 | // grand total |
| 296 | if(!print_hide('grand_total')) |
| 297 | out += make_row('Grand Total', doc.grand_total, 1); |
| 298 | |
| 299 | if(!print_hide('rounded_total')) |
| 300 | out += make_row('Rounded Total', doc.rounded_total, 1); |
| 301 | |
| 302 | if(doc.in_words && !print_hide('in_words')) { |
| 303 | out +='</table></td></tr>'; |
| 304 | out += '<tr><td colspan = "2">'; |
| 305 | out += '<table><tr><td style="width:25%;"><b>In Words</b></td>'; |
| 306 | out += '<td style="width:50%;">' + doc.in_words + '</td></tr>'; |
| 307 | } |
| 308 | out += '</table></td></tr></table></div>'; |
| 309 | } |
| 310 | return out; |
| 311 | } |