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