blob: 7879173cd11d624368c68852b6cdb1f484765653 [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");
Rushabh Mehta2a21bc92015-02-25 15:08:42 +05306
Deepesh Garg9e791ef2023-07-17 21:44:43 +05307erpnext.accounts.taxes = {
8 setup_tax_validations: function(doctype) {
9 let me = this;
10 frappe.ui.form.on(doctype, {
11 setup: function(frm) {
12 // set conditional display for rate column in taxes
13 $(frm.wrapper).on('grid-row-render', function(e, grid_row) {
14 if(in_list(['Sales Taxes and Charges', 'Purchase Taxes and Charges'], grid_row.doc.doctype)) {
15 me.set_conditional_mandatory_rate_or_amount(grid_row);
16 }
17 });
18 },
19 onload: function(frm) {
20 if(frm.get_field("taxes")) {
21 frm.set_query("account_head", "taxes", function(doc) {
22 if(frm.cscript.tax_table == "Sales Taxes and Charges") {
23 var account_type = ["Tax", "Chargeable", "Expense Account"];
24 } else {
25 var account_type = ["Tax", "Chargeable", "Income Account", "Expenses Included In Valuation"];
26 }
27
28 return {
29 query: "erpnext.controllers.queries.tax_account_query",
30 filters: {
31 "account_type": account_type,
32 "company": doc.company,
Deepesh Garg9e791ef2023-07-17 21:44:43 +053033 }
34 }
35 });
36 frm.set_query("cost_center", "taxes", function(doc) {
37 return {
38 filters: {
39 "company": doc.company,
40 "is_group": 0
41 }
42 };
43 });
44 }
45 },
46 validate: function(frm) {
47 // neither is absolutely mandatory
48 if(frm.get_docfield("taxes")) {
49 frm.get_docfield("taxes", "rate").reqd = 0;
50 frm.get_docfield("taxes", "tax_amount").reqd = 0;
51 }
52
53 },
54 taxes_on_form_rendered: function(frm) {
55 me.set_conditional_mandatory_rate_or_amount(frm.open_grid_row());
56 },
57 });
58 },
59
60 set_conditional_mandatory_rate_or_amount: function(grid_row) {
61 if(grid_row) {
62 if(grid_row.doc.charge_type==="Actual") {
63 grid_row.toggle_editable("tax_amount", true);
64 grid_row.toggle_reqd("tax_amount", true);
65 grid_row.toggle_editable("rate", false);
66 grid_row.toggle_reqd("rate", false);
67 } else {
68 grid_row.toggle_editable("rate", true);
69 grid_row.toggle_reqd("rate", true);
70 grid_row.toggle_editable("tax_amount", false);
71 grid_row.toggle_reqd("tax_amount", false);
72 }
73 }
74 },
75
76 validate_taxes_and_charges: function(cdt, cdn) {
77 let d = locals[cdt][cdn];
78 let msg = "";
79
80 if (d.account_head && !d.description) {
81 // set description from account head
82 d.description = d.account_head.split(' - ').slice(0, -1).join(' - ');
83 }
84
85 if (!d.charge_type && (d.row_id || d.rate || d.tax_amount)) {
86 msg = __("Please select Charge Type first");
87 d.row_id = "";
88 d.rate = d.tax_amount = 0.0;
89 } else if ((d.charge_type == 'Actual' || d.charge_type == 'On Net Total' || d.charge_type == 'On Paid Amount') && d.row_id) {
90 msg = __("Can refer row only if the charge type is 'On Previous Row Amount' or 'Previous Row Total'");
91 d.row_id = "";
92 } else if ((d.charge_type == 'On Previous Row Amount' || d.charge_type == 'On Previous Row Total') && d.row_id) {
93 if (d.idx == 1) {
94 msg = __("Cannot select charge type as 'On Previous Row Amount' or 'On Previous Row Total' for first row");
95 d.charge_type = '';
96 } else if (!d.row_id) {
97 msg = __("Please specify a valid Row ID for row {0} in table {1}", [d.idx, __(d.doctype)]);
98 d.row_id = "";
99 } else if (d.row_id && d.row_id >= d.idx) {
100 msg = __("Cannot refer row number greater than or equal to current row number for this Charge type");
101 d.row_id = "";
102 }
103 }
104 if (msg) {
105 frappe.validated = false;
106 refresh_field("taxes");
107 frappe.throw(msg);
108 }
109
110 },
111
112 setup_tax_filters: function(doctype) {
113 let me = this;
114 frappe.ui.form.on(doctype, {
115 account_head: function(frm, cdt, cdn) {
116 let d = locals[cdt][cdn];
117
Rishik Sahu5ebf7c82023-10-12 14:03:49 +0530118 if (d.docstatus == 1) {
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530119 // Should not trigger any changes on change post submit
120 return;
121 }
122
123 if(!d.charge_type && d.account_head){
124 frappe.msgprint(__("Please select Charge Type first"));
125 frappe.model.set_value(cdt, cdn, "account_head", "");
126 } else if (d.account_head) {
127 frappe.call({
128 type:"GET",
129 method: "erpnext.controllers.accounts_controller.get_tax_rate",
130 args: {"account_head":d.account_head},
131 callback: function(r) {
132 if (d.charge_type!=="Actual") {
133 frappe.model.set_value(cdt, cdn, "rate", r.message.tax_rate || 0);
134 }
135 frappe.model.set_value(cdt, cdn, "description", r.message.account_name);
136 }
137 })
138 }
139 },
140 row_id: function(frm, cdt, cdn) {
141 me.validate_taxes_and_charges(cdt, cdn);
142 },
143 rate: function(frm, cdt, cdn) {
144 me.validate_taxes_and_charges(cdt, cdn);
145 },
146 tax_amount: function(frm, cdt, cdn) {
147 me.validate_taxes_and_charges(cdt, cdn);
148 },
149 charge_type: function(frm, cdt, cdn) {
150 me.validate_taxes_and_charges(cdt, cdn);
151 let open_form = frm.open_grid_row();
152 if(open_form) {
153 me.set_conditional_mandatory_rate_or_amount(open_form);
154 } else {
155 // apply in current row
156 me.set_conditional_mandatory_rate_or_amount(frm.get_field('taxes').grid.get_row(cdn));
157 }
158 },
159 included_in_print_rate: function(frm, cdt, cdn) {
160 let tax = frappe.get_doc(cdt, cdn);
161 try {
162 me.validate_taxes_and_charges(cdt, cdn);
163 me.validate_inclusive_tax(tax);
164 } catch(e) {
165 tax.included_in_print_rate = 0;
166 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
167 throw e;
168 }
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530169 }
170 });
171 },
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530172
173 validate_inclusive_tax: function(tax) {
174 let actual_type_error = function() {
175 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
176 frappe.throw(msg);
177 };
178
179 let on_previous_row_error = function(row_range) {
180 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
181 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
182 frappe.throw(msg);
183 };
184
185 if(cint(tax.included_in_print_rate)) {
186 if(tax.charge_type == "Actual") {
187 // inclusive tax cannot be of type Actual
188 actual_type_error();
189 } else if(tax.charge_type == "On Previous Row Amount" &&
190 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)
191 ) {
192 // referred row should also be an inclusive tax
193 on_previous_row_error(tax.row_id);
194 } else if(tax.charge_type == "On Previous Row Total") {
195 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
196 function(t) { return cint(t.included_in_print_rate) ? null : t; });
197 if(taxes_not_included.length > 0) {
198 // all rows above this tax should be inclusive
199 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
Rushabh Mehtac712cdb2015-05-03 22:20:44 +0530200 }
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530201 } else if(tax.category == "Valuation") {
202 frappe.throw(__("Valuation type charges can not marked as Inclusive"));
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530203 }
204 }
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530205 }
Rohit Waghchaure70be24d2016-08-08 22:54:38 +0530206}
207
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530208erpnext.accounts.payment_triggers = {
209 setup: function(doctype) {
Deepesh Garg40772542023-07-22 23:07:18 +0530210 frappe.ui.form.on(doctype, {
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530211 allocate_advances_automatically(frm) {
212 frm.trigger('fetch_advances');
213 },
Deepesh Garge6261072022-10-17 16:48:13 +0530214
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530215 only_include_allocated_payments(frm) {
216 frm.trigger('fetch_advances');
217 },
Deepesh Garge6261072022-10-17 16:48:13 +0530218
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530219 fetch_advances(frm) {
220 if(frm.doc.allocate_advances_automatically) {
221 frappe.call({
222 doc: frm.doc,
223 method: "set_advances",
224 callback: function(r, rt) {
225 refresh_field("advances");
226 }
227 })
pateljannat5b02d322020-11-05 18:04:14 +0530228 }
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530229 }
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530230 });
231 },
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +0530232}
Nabin Haitce245122015-02-22 20:14:49 +0530233
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530234erpnext.accounts.pos = {
235 setup: function(doctype) {
236 frappe.ui.form.on(doctype, {
237 mode_of_payment: function(frm, cdt, cdn) {
238 var d = locals[cdt][cdn];
239 get_payment_mode_account(frm, d.mode_of_payment, function(account){
240 frappe.model.set_value(cdt, cdn, 'account', account)
241 })
Nabin Hait613d0812015-02-23 11:58:15 +0530242 }
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530243 });
244 },
245
246 get_payment_mode_account: function(frm, mode_of_payment, callback) {
247 if(!frm.doc.company) {
248 frappe.throw({message:__("Please select a Company first."), title: __("Mandatory")});
Nabin Hait613d0812015-02-23 11:58:15 +0530249 }
Nabin Haitce245122015-02-22 20:14:49 +0530250
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530251 if(!mode_of_payment) {
252 return;
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +0530253 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530254
Deepesh Garg9e791ef2023-07-17 21:44:43 +0530255 return frappe.call({
256 method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.get_bank_cash_account",
257 args: {
258 "mode_of_payment": mode_of_payment,
259 "company": frm.doc.company
260 },
261 callback: function(r, rt) {
262 if(r.message) {
263 callback(r.message.account)
264 }
265 }
266 });
Nabin Hait613d0812015-02-23 11:58:15 +0530267 }
Rushabh Mehta2a21bc92015-02-25 15:08:42 +0530268}