blob: 47b88a002bcc8406a799ce174a238771ba50ae0c [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
Rushabh Mehtac712cdb2015-05-03 22:20:44 +05308frappe.ui.form.on(cur_frm.doctype, {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +05309 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 Mehtac712cdb2015-05-03 22:20:44 +053017 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 Shahdcbc4282017-11-27 11:48:09 +053023 var account_type = ["Tax", "Chargeable", "Income Account", "Expenses Included In Valuation"];
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053024 }
25
26 return {
27 query: "erpnext.controllers.queries.tax_account_query",
28 filters: {
29 "account_type": account_type,
Saqib Ansaria1e3ae82022-05-11 13:01:06 +053030 "company": doc.company,
31 "disabled": 0
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053032 }
33 }
34 });
Anuja Pawar0e337be2021-08-10 17:26:35 +053035 frm.set_query("cost_center", "taxes", function(doc) {
36 return {
37 filters: {
38 "company": doc.company,
39 "is_group": 0
40 }
41 };
42 });
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053043 }
44 },
45 validate: function(frm) {
46 // neither is absolutely mandatory
Nabin Hait0e1540b2015-05-05 17:26:35 +053047 if(frm.get_docfield("taxes")) {
48 frm.get_docfield("taxes", "rate").reqd = 0;
49 frm.get_docfield("taxes", "tax_amount").reqd = 0;
50 }
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +053051
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053052 },
53 taxes_on_form_rendered: function(frm) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +053054 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.open_grid_row());
Nabin Hait041a5c22018-08-01 18:07:39 +053055 },
56
57 allocate_advances_automatically: function(frm) {
Deepesh Gargfd3fb642023-04-05 12:02:44 +053058 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 Hait041a5c22018-08-01 18:07:39 +053066 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 }
marination6ef057a2019-11-18 15:55:32 +053075 }
Rushabh Mehtac712cdb2015-05-03 22:20:44 +053076});
77
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053078frappe.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 }
tundee7da1c52017-09-06 12:36:45 +010085});
86
87frappe.ui.form.on("Sales Invoice", {
88 payment_terms_template: function() {
89 cur_frm.trigger("disable_due_date");
90 }
91});
Rohit Waghchaure70be24d2016-08-08 22:54:38 +053092
93frappe.ui.form.on('Purchase Invoice', {
vishnuf2ceb002023-05-14 09:35:38 +000094 setup: (frm) => {
95 frm.make_methods = {
96 'Landed Cost Voucher': function () { frm.trigger('create_landedcost_voucher') },
97 }
98 },
99
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530100 mode_of_payment: function(frm) {
101 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
102 frm.set_value('cash_bank_account', account);
103 })
tundee7da1c52017-09-06 12:36:45 +0100104 },
105
106 payment_terms_template: function() {
107 cur_frm.trigger("disable_due_date");
vishnuf2ceb002023-05-14 09:35:38 +0000108 },
109
110 create_landedcost_voucher: function (frm) {
111 let lcv = frappe.model.get_new_doc('Landed Cost Voucher');
112 lcv.company = frm.doc.company;
113
114 let lcv_receipt = frappe.model.get_new_doc('Landed Cost Purchase Invoice');
115 lcv_receipt.receipt_document_type = 'Purchase Invoice';
116 lcv_receipt.receipt_document = frm.doc.name;
117 lcv_receipt.supplier = frm.doc.supplier;
118 lcv_receipt.grand_total = frm.doc.grand_total;
119 lcv.purchase_receipts = [lcv_receipt];
120
121 frappe.set_route("Form", lcv.doctype, lcv.name);
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530122 }
tundee7da1c52017-09-06 12:36:45 +0100123});
124
125frappe.ui.form.on("Payment Schedule", {
126 payment_schedule_remove: function() {
127 cur_frm.trigger("disable_due_date");
128 },
129
130});
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530131
132frappe.ui.form.on('Payment Entry', {
133 mode_of_payment: function(frm) {
134 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
135 var payment_account_field = frm.doc.payment_type == "Receive" ? "paid_to" : "paid_from";
136 frm.set_value(payment_account_field, account);
137 })
138 }
139})
140
Kanchan Chauhanb40c0a92016-08-26 11:11:17 +0530141frappe.ui.form.on('Salary Structure', {
142 mode_of_payment: function(frm) {
143 get_payment_mode_account(frm, frm.doc.mode_of_payment, function(account){
144 frm.set_value("payment_account", account);
145 })
146 }
147})
148
Makarand Bauskar8f507a92017-07-13 11:44:29 +0530149var get_payment_mode_account = function(frm, mode_of_payment, callback) {
150 if(!frm.doc.company) {
marination8370d9b2020-09-24 11:51:03 +0530151 frappe.throw({message:__("Please select a Company first."), title: __("Mandatory")});
Makarand Bauskar8f507a92017-07-13 11:44:29 +0530152 }
153
Makarand Bauskarad7eb9d2017-07-14 17:31:36 +0530154 if(!mode_of_payment) {
155 return;
156 }
157
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530158 return frappe.call({
159 method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account",
160 args: {
161 "mode_of_payment": mode_of_payment,
162 "company": frm.doc.company
163 },
164 callback: function(r, rt) {
165 if(r.message) {
166 callback(r.message.account)
167 }
168 }
169 });
170}
171
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530172cur_frm.cscript.account_head = function(doc, cdt, cdn) {
173 var d = locals[cdt][cdn];
Deepesh Garge6261072022-10-17 16:48:13 +0530174
175 if (doc.docstatus == 1) {
176 // Should not trigger any changes on change post submit
177 return;
178 }
179
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530180 if(!d.charge_type && d.account_head){
Nabin Haite6d65bc2018-02-14 17:44:06 +0530181 frappe.msgprint(__("Please select Charge Type first"));
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530182 frappe.model.set_value(cdt, cdn, "account_head", "");
pateljannat7e4d1152020-11-11 09:52:27 +0530183 } else if (d.account_head) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530184 frappe.call({
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530185 type:"GET",
Nabin Haitce245122015-02-22 20:14:49 +0530186 method: "erpnext.controllers.accounts_controller.get_tax_rate",
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530187 args: {"account_head":d.account_head},
188 callback: function(r) {
pateljannat7e4d1152020-11-11 09:52:27 +0530189 if (d.charge_type!=="Actual") {
pateljannat403822a2020-11-05 16:24:33 +0530190 frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0);
pateljannat5b02d322020-11-05 18:04:14 +0530191 }
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530192 frappe.model.set_value(cdt, cdn, "description", r.message.account_name);
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530193 }
194 })
195 }
196}
Nabin Haitce245122015-02-22 20:14:49 +0530197
Nabin Hait613d0812015-02-23 11:58:15 +0530198cur_frm.cscript.validate_taxes_and_charges = function(cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530199 var d = locals[cdt][cdn];
Nabin Hait613d0812015-02-23 11:58:15 +0530200 var msg = "";
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530201
Deepesh Gargfd380d32021-05-13 14:04:51 +0530202 if (d.account_head && !d.description) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530203 // set description from account head
204 d.description = d.account_head.split(' - ').slice(0, -1).join(' - ');
205 }
206
Deepesh Gargfd380d32021-05-13 14:04:51 +0530207 if (!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
Nabin Hait613d0812015-02-23 11:58:15 +0530208 msg = __("Please select Charge Type first");
Nabin Hait2b019ed2015-02-22 23:03:07 +0530209 d.row_id = "";
210 d.rate = d.tax_amount = 0.0;
Deepesh Gargfd380d32021-05-13 14:04:51 +0530211 } else if ((d.charge_type == 'Actual' || d.charge_type == 'On Net Total' || d.charge_type == 'On Paid Amount') && d.row_id) {
Nabin Hait613d0812015-02-23 11:58:15 +0530212 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 +0530213 d.row_id = "";
Deepesh Gargfd380d32021-05-13 14:04:51 +0530214 } else if ((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
Nabin Haitce245122015-02-22 20:14:49 +0530215 if (d.idx == 1) {
Nabin Hait613d0812015-02-23 11:58:15 +0530216 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 +0530217 d.charge_type = '';
Nabin Hait613d0812015-02-23 11:58:15 +0530218 } else if (!d.row_id) {
219 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
220 d.row_id = "";
Deepesh Gargfd380d32021-05-13 14:04:51 +0530221 } else if (d.row_id && d.row_id >= d.idx) {
Nabin Hait613d0812015-02-23 11:58:15 +0530222 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
Nabin Haitce245122015-02-22 20:14:49 +0530223 d.row_id = "";
224 }
225 }
Deepesh Gargfd380d32021-05-13 14:04:51 +0530226 if (msg) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530227 frappe.validated = false;
Nabin Hait613d0812015-02-23 11:58:15 +0530228 refresh_field("taxes");
229 frappe.throw(msg);
230 }
231
232}
233
234cur_frm.cscript.validate_inclusive_tax = function(tax) {
235 var actual_type_error = function() {
236 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
237 frappe.throw(msg);
238 };
239
240 var on_previous_row_error = function(row_range) {
241 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
242 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
243 frappe.throw(msg);
244 };
245
246 if(cint(tax.included_in_print_rate)) {
247 if(tax.charge_type == "Actual") {
248 // inclusive tax cannot be of type Actual
249 actual_type_error();
250 } else if(tax.charge_type == "On Previous Row Amount" &&
Faris Ansariab74ca72017-05-30 12:54:42 +0530251 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)
252 ) {
253 // referred row should also be an inclusive tax
254 on_previous_row_error(tax.row_id);
Nabin Hait613d0812015-02-23 11:58:15 +0530255 } else if(tax.charge_type == "On Previous Row Total") {
256 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
257 function(t) { return cint(t.included_in_print_rate) ? null : t; });
258 if(taxes_not_included.length > 0) {
259 // all rows above this tax should be inclusive
260 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
261 }
Nabin Hait72c359a2015-02-24 16:05:19 +0530262 } else if(tax.category == "Valuation") {
263 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Nabin Hait613d0812015-02-23 11:58:15 +0530264 }
265 }
Nabin Haitce245122015-02-22 20:14:49 +0530266}
267
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530268if(!erpnext.taxes.flags[cur_frm.cscript.tax_table]) {
269 erpnext.taxes.flags[cur_frm.cscript.tax_table] = true;
Nabin Haitce245122015-02-22 20:14:49 +0530270
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530271 frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
Nabin Hait613d0812015-02-23 11:58:15 +0530272 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530273 });
274
275 frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
276 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
277 });
278
279 frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
280 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
281 });
282
283 frappe.ui.form.on(cur_frm.cscript.tax_table, "charge_type", function(frm, cdt, cdn) {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530284 frm.cscript.validate_taxes_and_charges(cdt, cdn);
285 var open_form = frm.open_grid_row();
286 if(open_form) {
287 erpnext.taxes.set_conditional_mandatory_rate_or_amount(open_form);
288 } else {
289 // apply in current row
Rushabh Mehta43ef4e92017-07-03 11:53:07 +0530290 erpnext.taxes.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_row(cdn));
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530291 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530292 });
293
294 frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
295 var tax = frappe.get_doc(cdt, cdn);
296 try {
297 cur_frm.cscript.validate_taxes_and_charges(cdt, cdn);
298 cur_frm.cscript.validate_inclusive_tax(tax);
299 } catch(e) {
300 tax.included_in_print_rate = 0;
301 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
302 throw e;
303 }
304 });
305}
306
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530307erpnext.taxes.set_conditional_mandatory_rate_or_amount = function(grid_row) {
308 if(grid_row) {
309 if(grid_row.doc.charge_type==="Actual") {
310 grid_row.toggle_editable("tax_amount", true);
311 grid_row.toggle_reqd("tax_amount", true);
312 grid_row.toggle_editable("rate", false);
313 grid_row.toggle_reqd("rate", false);
314 } else {
315 grid_row.toggle_editable("rate", true);
316 grid_row.toggle_reqd("rate", true);
317 grid_row.toggle_editable("tax_amount", false);
318 grid_row.toggle_reqd("tax_amount", false);
319 }
Nabin Hait613d0812015-02-23 11:58:15 +0530320 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530321}