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 { |
Shreya Shah | dcbc428 | 2017-11-27 11:48:09 +0530 | [diff] [blame] | 23 | var account_type = ["Tax", "Chargeable", "Income Account", "Expenses Included In Valuation"]; |
Rushabh Mehta | c712cdb | 2015-05-03 22:20:44 +0530 | [diff] [blame] | 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 | }); |
Rushabh Mehta | c712cdb | 2015-05-03 22:20:44 +0530 | [diff] [blame] | 34 | } |
| 35 | }, |
| 36 | validate: function(frm) { |
| 37 | // neither is absolutely mandatory |
Nabin Hait | 0e1540b | 2015-05-05 17:26:35 +0530 | [diff] [blame] | 38 | if(frm.get_docfield("taxes")) { |
| 39 | frm.get_docfield("taxes", "rate").reqd = 0; |
| 40 | frm.get_docfield("taxes", "tax_amount").reqd = 0; |
| 41 | } |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 42 | |
Rushabh Mehta | c712cdb | 2015-05-03 22:20:44 +0530 | [diff] [blame] | 43 | }, |
| 44 | taxes_on_form_rendered: function(frm) { |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 45 | erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.open_grid_row()); |
Nabin Hait | 041a5c2 | 2018-08-01 18:07:39 +0530 | [diff] [blame] | 46 | }, |
| 47 | |
| 48 | allocate_advances_automatically: function(frm) { |
| 49 | if(frm.doc.allocate_advances_automatically) { |
| 50 | frappe.call({ |
| 51 | doc: frm.doc, |
| 52 | method: "set_advances", |
| 53 | callback: function(r, rt) { |
| 54 | refresh_field("advances"); |
| 55 | } |
| 56 | }) |
| 57 | } |
marination | 6ef057a | 2019-11-18 15:55:32 +0530 | [diff] [blame] | 58 | } |
Rushabh Mehta | c712cdb | 2015-05-03 22:20:44 +0530 | [diff] [blame] | 59 | }); |
| 60 | |
Rohit Waghchaure | 70be24d | 2016-08-08 22:54:38 +0530 | [diff] [blame] | 61 | frappe.ui.form.on('Sales Invoice Payment', { |
| 62 | mode_of_payment: function(frm, cdt, cdn) { |
| 63 | var d = locals[cdt][cdn]; |
| 64 | get_payment_mode_account(frm, d.mode_of_payment, function(account){ |
| 65 | frappe.model.set_value(cdt, cdn, 'account', account) |
| 66 | }) |
| 67 | } |
tunde | e7da1c5 | 2017-09-06 12:36:45 +0100 | [diff] [blame] | 68 | }); |
| 69 | |
| 70 | frappe.ui.form.on("Sales Invoice", { |
| 71 | payment_terms_template: function() { |
| 72 | cur_frm.trigger("disable_due_date"); |
| 73 | } |
| 74 | }); |
Rohit Waghchaure | 70be24d | 2016-08-08 22:54:38 +0530 | [diff] [blame] | 75 | |
| 76 | frappe.ui.form.on('Purchase Invoice', { |
| 77 | mode_of_payment: function(frm) { |
| 78 | get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){ |
| 79 | frm.set_value('cash_bank_account', account); |
| 80 | }) |
tunde | e7da1c5 | 2017-09-06 12:36:45 +0100 | [diff] [blame] | 81 | }, |
| 82 | |
| 83 | payment_terms_template: function() { |
| 84 | cur_frm.trigger("disable_due_date"); |
Rohit Waghchaure | 70be24d | 2016-08-08 22:54:38 +0530 | [diff] [blame] | 85 | } |
tunde | e7da1c5 | 2017-09-06 12:36:45 +0100 | [diff] [blame] | 86 | }); |
| 87 | |
| 88 | frappe.ui.form.on("Payment Schedule", { |
| 89 | payment_schedule_remove: function() { |
| 90 | cur_frm.trigger("disable_due_date"); |
| 91 | }, |
| 92 | |
| 93 | }); |
Rohit Waghchaure | 70be24d | 2016-08-08 22:54:38 +0530 | [diff] [blame] | 94 | |
| 95 | frappe.ui.form.on('Payment Entry', { |
| 96 | mode_of_payment: function(frm) { |
| 97 | get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){ |
| 98 | var payment_account_field = frm.doc.payment_type == "Receive" ? "paid_to" : "paid_from"; |
| 99 | frm.set_value(payment_account_field, account); |
| 100 | }) |
| 101 | } |
| 102 | }) |
| 103 | |
Kanchan Chauhan | b40c0a9 | 2016-08-26 11:11:17 +0530 | [diff] [blame] | 104 | frappe.ui.form.on('Salary Structure', { |
| 105 | mode_of_payment: function(frm) { |
| 106 | get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){ |
| 107 | frm.set_value("payment_account", account); |
| 108 | }) |
| 109 | } |
| 110 | }) |
| 111 | |
Makarand Bauskar | 8f507a9 | 2017-07-13 11:44:29 +0530 | [diff] [blame] | 112 | var get_payment_mode_account = function(frm, mode_of_payment, callback) { |
| 113 | if(!frm.doc.company) { |
marination | 8370d9b | 2020-09-24 11:51:03 +0530 | [diff] [blame] | 114 | frappe.throw({message:__("Please select a Company first."), title: __("Mandatory")}); |
Makarand Bauskar | 8f507a9 | 2017-07-13 11:44:29 +0530 | [diff] [blame] | 115 | } |
| 116 | |
Makarand Bauskar | ad7eb9d | 2017-07-14 17:31:36 +0530 | [diff] [blame] | 117 | if(!mode_of_payment) { |
| 118 | return; |
| 119 | } |
| 120 | |
Rohit Waghchaure | 70be24d | 2016-08-08 22:54:38 +0530 | [diff] [blame] | 121 | return frappe.call({ |
| 122 | method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account", |
| 123 | args: { |
| 124 | "mode_of_payment": mode_of_payment, |
| 125 | "company": frm.doc.company |
| 126 | }, |
| 127 | callback: function(r, rt) { |
| 128 | if(r.message) { |
| 129 | callback(r.message.account) |
| 130 | } |
| 131 | } |
| 132 | }); |
| 133 | } |
| 134 | |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 135 | cur_frm.cscript.account_head = function(doc, cdt, cdn) { |
| 136 | var d = locals[cdt][cdn]; |
| 137 | if(!d.charge_type && d.account_head){ |
Nabin Hait | e6d65bc | 2018-02-14 17:44:06 +0530 | [diff] [blame] | 138 | frappe.msgprint(__("Please select Charge Type first")); |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 139 | frappe.model.set_value(cdt, cdn, "account_head", ""); |
pateljannat | 7e4d115 | 2020-11-11 09:52:27 +0530 | [diff] [blame] | 140 | } else if (d.account_head) { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 141 | frappe.call({ |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 142 | type:"GET", |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 143 | method: "erpnext.controllers.accounts_controller.get_tax_rate", |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 144 | args: {"account_head":d.account_head}, |
| 145 | callback: function(r) { |
pateljannat | 7e4d115 | 2020-11-11 09:52:27 +0530 | [diff] [blame] | 146 | if (d.charge_type!=="Actual") { |
pateljannat | 403822a | 2020-11-05 16:24:33 +0530 | [diff] [blame] | 147 | frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0); |
pateljannat | 5b02d32 | 2020-11-05 18:04:14 +0530 | [diff] [blame] | 148 | } |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 149 | frappe.model.set_value(cdt, cdn, "description", r.message.account_name); |
Rushabh Mehta | f56d73c | 2013-10-10 16:35:09 +0530 | [diff] [blame] | 150 | } |
| 151 | }) |
| 152 | } |
| 153 | } |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 154 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 155 | cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) { |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 156 | var d = locals[cdt][cdn]; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 157 | var msg = ""; |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 158 | |
| 159 | if(d.account_head && !d.description) { |
| 160 | // set description from account head |
| 161 | d.description = d.account_head.split(' - ').slice(0, -1).join(' - '); |
| 162 | } |
| 163 | |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 164 | if(!d.charge_type && (d.row_id || d.rate || d.tax_amount)) { |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 165 | msg = __("Please select Charge Type first"); |
Nabin Hait | 2b019ed | 2015-02-22 23:03:07 +0530 | [diff] [blame] | 166 | d.row_id = ""; |
| 167 | d.rate = d.tax_amount = 0.0; |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 168 | } 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] | 169 | 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] | 170 | d.row_id = ""; |
| 171 | } else if((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) { |
| 172 | if (d.idx == 1) { |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 173 | 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] | 174 | d.charge_type = ''; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 175 | } else if (!d.row_id) { |
| 176 | msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]); |
| 177 | d.row_id = ""; |
| 178 | } else if(d.row_id && d.row_id >= d.idx) { |
| 179 | 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] | 180 | d.row_id = ""; |
| 181 | } |
| 182 | } |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 183 | if(msg) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 184 | frappe.validated = false; |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 185 | refresh_field("taxes"); |
| 186 | frappe.throw(msg); |
| 187 | } |
| 188 | |
| 189 | } |
| 190 | |
| 191 | cur_frm.cscript.validate_inclusive_tax = function(tax) { |
| 192 | var actual_type_error = function() { |
| 193 | var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx]) |
| 194 | frappe.throw(msg); |
| 195 | }; |
| 196 | |
| 197 | var on_previous_row_error = function(row_range) { |
| 198 | var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included", |
| 199 | [tax.idx, __(tax.doctype), tax.charge_type, row_range]) |
| 200 | frappe.throw(msg); |
| 201 | }; |
| 202 | |
| 203 | if(cint(tax.included_in_print_rate)) { |
| 204 | if(tax.charge_type == "Actual") { |
| 205 | // inclusive tax cannot be of type Actual |
| 206 | actual_type_error(); |
| 207 | } else if(tax.charge_type == "On Previous Row Amount" && |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 208 | !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate) |
| 209 | ) { |
| 210 | // referred row should also be an inclusive tax |
| 211 | on_previous_row_error(tax.row_id); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 212 | } else if(tax.charge_type == "On Previous Row Total") { |
| 213 | var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id), |
| 214 | function(t) { return cint(t.included_in_print_rate) ? null : t; }); |
| 215 | if(taxes_not_included.length > 0) { |
| 216 | // all rows above this tax should be inclusive |
| 217 | on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id); |
| 218 | } |
Nabin Hait | 72c359a | 2015-02-24 16:05:19 +0530 | [diff] [blame] | 219 | } else if(tax.category == "Valuation") { |
| 220 | frappe.throw(__("Valuation type charges can not marked as Inclusive")); |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 221 | } |
| 222 | } |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 223 | } |
| 224 | |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 225 | if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) { |
| 226 | erpnext.taxes.flags[cur_frm.cscript.tax_table] = true; |
Nabin Hait | ce24512 | 2015-02-22 20:14:49 +0530 | [diff] [blame] | 227 | |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 228 | 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] | 229 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 230 | }); |
| 231 | |
| 232 | frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) { |
| 233 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
| 234 | }); |
| 235 | |
| 236 | frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) { |
| 237 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
| 238 | }); |
| 239 | |
| 240 | 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] | 241 | frm.cscript.validate_taxes_and_charges(cdt, cdn); |
| 242 | var open_form = frm.open_grid_row(); |
| 243 | if(open_form) { |
| 244 | erpnext.taxes.set_conditional_mandatory_rate_or_amount(open_form); |
| 245 | } else { |
| 246 | // apply in current row |
Rushabh Mehta | 43ef4e9 | 2017-07-03 11:53:07 +0530 | [diff] [blame] | 247 | erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_row(cdn)); |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 248 | } |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 249 | }); |
| 250 | |
| 251 | frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) { |
| 252 | var tax = frappe.get_doc(cdt, cdn); |
| 253 | try { |
| 254 | cur_frm.cscript.validate_taxes_and_charges(cdt, cdn); |
| 255 | cur_frm.cscript.validate_inclusive_tax(tax); |
| 256 | } catch(e) { |
| 257 | tax.included_in_print_rate = 0; |
| 258 | refresh_field("included_in_print_rate", tax.name, tax.parentfield); |
| 259 | throw e; |
| 260 | } |
| 261 | }); |
| 262 | } |
| 263 | |
Rushabh Mehta | 2cb7a9f | 2016-06-15 16:45:03 +0530 | [diff] [blame] | 264 | erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(grid_row) { |
| 265 | if(grid_row) { |
| 266 | if(grid_row.doc.charge_type==="Actual") { |
| 267 | grid_row.toggle_editable("tax_amount", true); |
| 268 | grid_row.toggle_reqd("tax_amount", true); |
| 269 | grid_row.toggle_editable("rate", false); |
| 270 | grid_row.toggle_reqd("rate", false); |
| 271 | } else { |
| 272 | grid_row.toggle_editable("rate", true); |
| 273 | grid_row.toggle_reqd("rate", true); |
| 274 | grid_row.toggle_editable("tax_amount", false); |
| 275 | grid_row.toggle_reqd("tax_amount", false); |
| 276 | } |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 277 | } |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 278 | } |