blob: cb868b24f41a81edd57be6a912d3bb1a88af4589 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Anand Doshi3543f302013-05-24 19:25:01 +05303
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05304frappe.provide("erpnext");
5frappe.require("assets/erpnext/js/controllers/stock_controller.js");
Anand Doshi3543f302013-05-24 19:25:01 +05306
Anand Doshia648f462013-07-30 14:42:15 +05307erpnext.TransactionController = erpnext.stock.StockController.extend({
Anand Doshi3543f302013-05-24 19:25:01 +05308 onload: function() {
Anand Doshi1e4fb682013-08-23 12:56:33 +05309 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +053010 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053011 var today = get_today(),
Nabin Hait73083dc2014-03-14 15:10:42 +053012 currency = frappe.defaults.get_user_default("currency");
Anand Doshi3543f302013-05-24 19:25:01 +053013
14 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053015 posting_date: today,
16 due_date: today,
17 transaction_date: today,
18 currency: currency,
19 price_list_currency: currency,
20 status: "Draft",
Nabin Hait73083dc2014-03-14 15:10:42 +053021 company: frappe.defaults.get_user_default("company"),
22 fiscal_year: frappe.defaults.get_user_default("fiscal_year"),
Anand Doshifc777182013-05-27 19:29:07 +053023 is_subcontracted: "No",
Anand Doshi3543f302013-05-24 19:25:01 +053024 }, function(fieldname, value) {
25 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
26 me.frm.set_value(fieldname, value);
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053027 });
Anand Doshi3543f302013-05-24 19:25:01 +053028 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +053029
Anand Doshi1e4fb682013-08-23 12:56:33 +053030 if(this.other_fname) {
Anand Doshia91c95f2013-08-23 13:00:47 +053031 this[this.other_fname + "_remove"] = this.calculate_taxes_and_totals;
32 }
33
34 if(this.fname) {
35 this[this.fname + "_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +053036 }
Anand Doshi3543f302013-05-24 19:25:01 +053037 },
38
Anand Doshi5e4e5d72013-08-06 17:23:44 +053039 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053040 var me = this;
41 if(this.frm.doc.__islocal && this.frm.doc.company && !this.frm.doc.is_pos) {
Akhilesh Darjee04638a52013-10-17 13:19:46 +053042 if(!this.frm.doc.customer || !this.frm.doc.supplier) {
Akhilesh Darjee78378272013-10-11 19:06:30 +053043 return this.frm.call({
44 doc: this.frm.doc,
45 method: "onload_post_render",
46 freeze: true,
47 callback: function(r) {
48 // remove this call when using client side mapper
Akhilesh Darjee78378272013-10-11 19:06:30 +053049 me.set_dynamic_labels();
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +053050 me.calculate_taxes_and_totals();
Akhilesh Darjee78378272013-10-11 19:06:30 +053051 }
52 });
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053053 } else {
54 this.calculate_taxes_and_totals();
55 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053056 }
57 },
58
Anand Doshi3543f302013-05-24 19:25:01 +053059 refresh: function() {
60 this.frm.clear_custom_buttons();
61 erpnext.hide_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053062 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053063 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053064 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053065
66 // Show POS button only if it is enabled from features setup
67 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
Rushabh Mehtae34090a2013-11-15 11:30:23 +053068 this.make_pos_btn();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053069 },
70
Rushabh Mehtae34090a2013-11-15 11:30:23 +053071 make_pos_btn: function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053072 if(!this.pos_active) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053073 var btn_label = frappe._("POS View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053074 icon = "icon-desktop";
75 } else {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053076 var btn_label = frappe._(this.frm.doctype) + frappe._(" View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053077 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053078 }
79 var me = this;
Rushabh Mehtae34090a2013-11-15 11:30:23 +053080
81 this.$pos_btn = this.frm.appframe.add_button(btn_label, function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053082 me.toggle_pos();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053083 }, icon);
84 },
85
86 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053087 // Check whether it is Selling or Buying cycle
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053088 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053089 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
90
91 if (!price_list)
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053092 msgprint(frappe._("Please select Price List"))
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053093 else {
94 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
95
96 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053097 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053098 this.frm.layout.add_view("pos");
99 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
100 }
101
102 // toggle view
103 this.frm.layout.set_view(this.pos_active ? "" : "pos");
104 this.pos_active = !this.pos_active;
105
106 // refresh
107 if(this.pos_active)
108 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530109 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530110 }
Anand Doshi3543f302013-05-24 19:25:01 +0530111 },
Nabin Hait139dc7b2014-02-12 14:53:18 +0530112
113
114 item_code: function(doc, cdt, cdn) {
115 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530116 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530117 if(item.item_code || item.barcode || item.serial_no) {
118 if(!this.validate_company_and_party()) {
119 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
120 } else {
121 return this.frm.call({
122 method: "erpnext.stock.get_item_details.get_item_details",
123 child: item,
124 args: {
125 args: {
126 item_code: item.item_code,
127 barcode: item.barcode,
128 serial_no: item.serial_no,
129 warehouse: item.warehouse,
130 doctype: me.frm.doc.doctype,
131 docname: me.frm.doc.name,
132 customer: me.frm.doc.customer,
133 supplier: me.frm.doc.supplier,
134 currency: me.frm.doc.currency,
135 conversion_rate: me.frm.doc.conversion_rate,
136 price_list: me.frm.doc.selling_price_list ||
137 me.frm.doc.buying_price_list,
138 price_list_currency: me.frm.doc.price_list_currency,
139 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
140 company: me.frm.doc.company,
141 order_type: me.frm.doc.order_type,
142 is_pos: cint(me.frm.doc.is_pos),
143 is_subcontracted: me.frm.doc.is_subcontracted,
144 transaction_date: me.frm.doc.transaction_date
145 }
146 },
147 callback: function(r) {
148 if(!r.exc) {
149 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
150 }
151 }
152 });
153 }
154 }
155 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530156
157 serial_no: function(doc, cdt, cdn) {
158 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530159 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530160
161 if (item.serial_no) {
162 if (!item.item_code) {
163 this.frm.script_manager.trigger("item_code", cdt, cdn);
164 }
165 else {
166 var sr_no = [];
167
168 // Replacing all occurences of comma with carriage return
169 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
170
171 serial_nos = serial_nos.trim().split('\n');
172
173 // Trim each string and push unique string to new list
174 for (var x=0; x<=serial_nos.length - 1; x++) {
175 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
176 sr_no.push(serial_nos[x].trim());
177 }
178 }
179
180 // Add the new list to the serial no. field in grid with each in new line
181 item.serial_no = "";
182 for (var x=0; x<=sr_no.length - 1; x++)
183 item.serial_no += sr_no[x] + '\n';
184
185 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530186 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530187 }
188 }
189 },
Anand Doshi3543f302013-05-24 19:25:01 +0530190
Anand Doshi923d41d2013-05-28 17:23:36 +0530191 validate: function() {
192 this.calculate_taxes_and_totals();
193 },
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530194
Anand Doshi3543f302013-05-24 19:25:01 +0530195 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530196 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530197 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530198 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530199 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530200 }
201
Akhilesh Darjee78378272013-10-11 19:06:30 +0530202 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530203 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530204 }
205 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530206 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530207 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530208
Nabin Hait48a0bd32013-07-23 15:31:39 +0530209 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +0530210 }
211 },
212
213 get_company_currency: function() {
214 return erpnext.get_currency(this.frm.doc.company);
215 },
216
217 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530218 var me = this;
219 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530220
Anand Doshi61a2f682013-06-21 17:55:31 +0530221 var company_currency = this.get_company_currency();
222 if(this.frm.doc.currency !== company_currency) {
223 this.get_exchange_rate(this.frm.doc.currency, company_currency,
224 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530225 me.frm.set_value("conversion_rate", exchange_rate);
226 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530227 });
228 } else {
229 this.conversion_rate();
230 }
231 },
232
233 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530234 if(this.frm.doc.currency === this.get_company_currency()) {
235 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530236 }
237 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530238 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
239 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
240 }
Nabin Haitb338b6e2013-10-15 16:55:24 +0530241 if(flt(this.frm.doc.conversion_rate)>0.0) this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530242 },
243
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530244 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530245 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530246 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
247 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530248 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530249 method: "erpnext.setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530250 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530251 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530252 },
Anand Doshi3543f302013-05-24 19:25:01 +0530253 callback: function(r) {
254 if(!r.exc) {
255 me.price_list_currency();
256 }
257 }
258 });
259 }
260 },
261
Anand Doshi61a2f682013-06-21 17:55:31 +0530262 get_exchange_rate: function(from_currency, to_currency, callback) {
263 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530264 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530265 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530266 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530267 });
268 },
269
Anand Doshi3543f302013-05-24 19:25:01 +0530270 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530271 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530272 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530273
274 var company_currency = this.get_company_currency();
275 if(this.frm.doc.price_list_currency !== company_currency) {
276 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
277 function(exchange_rate) {
278 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530279 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530280 me.plc_conversion_rate();
281 }
282 });
283 } else {
284 this.plc_conversion_rate();
285 }
Anand Doshi3543f302013-05-24 19:25:01 +0530286 },
287
288 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530289 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
290 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530291 }
292 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530293 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
294 this.calculate_taxes_and_totals();
295 }
Anand Doshi3543f302013-05-24 19:25:01 +0530296 },
297
298 qty: function(doc, cdt, cdn) {
299 this.calculate_taxes_and_totals();
300 },
301
Anand Doshi923d41d2013-05-28 17:23:36 +0530302 tax_rate: function(doc, cdt, cdn) {
303 this.calculate_taxes_and_totals();
304 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530305
Anand Doshi923d41d2013-05-28 17:23:36 +0530306 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530307 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530308 try {
309 this.validate_on_previous_row(tax);
310 this.calculate_taxes_and_totals();
311 } catch(e) {
312 tax.row_id = null;
313 refresh_field("row_id", tax.name, tax.parentfield);
314 throw e;
315 }
316 },
317
Anand Doshi61a2f682013-06-21 17:55:31 +0530318 set_dynamic_labels: function() {
319 // What TODO? should we make price list system non-mandatory?
320 this.frm.toggle_reqd("plc_conversion_rate",
321 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
322
323 var company_currency = this.get_company_currency();
324 this.change_form_labels(company_currency);
325 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530326 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530327 },
328
Anand Doshi923d41d2013-05-28 17:23:36 +0530329 recalculate: function() {
330 this.calculate_taxes_and_totals();
331 },
332
333 recalculate_values: function() {
334 this.calculate_taxes_and_totals();
335 },
336
Anand Doshid0b00722013-05-28 18:54:48 +0530337 calculate_charges: function() {
338 this.calculate_taxes_and_totals();
339 },
340
Anand Doshi3543f302013-05-24 19:25:01 +0530341 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530342 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530343 try {
344 this.validate_on_previous_row(tax);
345 this.validate_inclusive_tax(tax);
346 this.calculate_taxes_and_totals();
347 } catch(e) {
348 tax.included_in_print_rate = 0;
349 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
350 throw e;
351 }
352 },
353
354 validate_on_previous_row: function(tax) {
355 // validate if a valid row id is mentioned in case of
356 // On Previous Row Amount and On Previous Row Total
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530357 if(([frappe._("On Previous Row Amount"), frappe._("On Previous Row Total")].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530358 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530359 var msg = repl(frappe._("Row") + " # %(idx)s [%(doctype)s]: " +
360 frappe._("Please specify a valid") + " %(row_id_label)s", {
Anand Doshi3543f302013-05-24 19:25:01 +0530361 idx: tax.idx,
362 doctype: tax.doctype,
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530363 row_id_label: frappe.meta.get_label(tax.doctype, "row_id", tax.name)
Anand Doshi3543f302013-05-24 19:25:01 +0530364 });
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530365 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530366 }
367 },
368
369 validate_inclusive_tax: function(tax) {
370 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
371
372 var actual_type_error = function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530373 var msg = repl(frappe._("For row") + " # %(idx)s [%(doctype)s]: " +
Anand Doshi3543f302013-05-24 19:25:01 +0530374 "%(charge_type_label)s = \"%(charge_type)s\" " +
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530375 frappe._("cannot be included in Item's rate"), {
Anand Doshi3543f302013-05-24 19:25:01 +0530376 idx: tax.idx,
377 doctype: tax.doctype,
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530378 charge_type_label: frappe.meta.get_label(tax.doctype, "charge_type", tax.name),
Anand Doshi3543f302013-05-24 19:25:01 +0530379 charge_type: tax.charge_type
380 });
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530381 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530382 };
383
384 var on_previous_row_error = function(row_range) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530385 var msg = repl(frappe._("For row") + " # %(idx)s [%(doctype)s]: " +
386 frappe._("to be included in Item's rate, it is required that: ") +
387 " [" + frappe._("Row") + " # %(row_range)s] " + frappe._("also be included in Item's rate"), {
Anand Doshi3543f302013-05-24 19:25:01 +0530388 idx: tax.idx,
389 doctype: tax.doctype,
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530390 charge_type_label: frappe.meta.get_label(tax.doctype, "charge_type", tax.name),
Anand Doshi3543f302013-05-24 19:25:01 +0530391 charge_type: tax.charge_type,
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530392 inclusive_label: frappe.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
Anand Doshi3543f302013-05-24 19:25:01 +0530393 row_range: row_range,
394 });
395
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530396 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530397 };
398
399 if(cint(tax.included_in_print_rate)) {
400 if(tax.charge_type == "Actual") {
401 // inclusive tax cannot be of type Actual
402 actual_type_error();
403 } else if(tax.charge_type == "On Previous Row Amount" &&
404 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
405 // referred row should also be an inclusive tax
406 on_previous_row_error(tax.row_id);
407 } else if(tax.charge_type == "On Previous Row Total") {
408 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
409 function(t) { return cint(t.included_in_print_rate) ? null : t; });
410 if(taxes_not_included.length > 0) {
411 // all rows above this tax should be inclusive
412 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
413 }
414 }
415 }
416 },
417
418 _load_item_tax_rate: function(item_tax_rate) {
419 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
420 },
421
422 _get_tax_rate: function(tax, item_tax_map) {
423 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
424 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
425 tax.rate;
426 },
427
428 get_item_wise_taxes_html: function() {
429 var item_tax = {};
430 var tax_accounts = [];
431 var company_currency = this.get_company_currency();
432
433 $.each(this.get_tax_doclist(), function(i, tax) {
434 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530435 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530436 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530437 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530438 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530439 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530440 var tax_rate = "";
441 if(tax_data[0] != null) {
442 tax_rate = (tax.charge_type === "Actual") ?
443 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
444 (flt(tax_data[0], tax_rate_precision) + "%");
445 }
446 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
447 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530448
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530449 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530450 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530451 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530452 }
Anand Doshi3543f302013-05-24 19:25:01 +0530453 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530454 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530455 });
456
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530457 var headings = $.map([frappe._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530458 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530459
460 var distinct_item_names = [];
461 var distinct_items = [];
462 $.each(this.get_item_doclist(), function(i, item) {
463 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
464 distinct_item_names.push(item.item_code || item.item_name);
465 distinct_items.push(item);
466 }
467 });
Anand Doshi3543f302013-05-24 19:25:01 +0530468
Anand Doshi179e3772013-09-05 16:53:57 +0530469 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530470 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530471 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530472 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
473 item_name: item.item_name,
474 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530475 return item_tax_record[head[0]] ?
476 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530477 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530478 }).join("\n")
479 });
480 }).join("\n");
481
Anand Doshi53b73422013-05-31 11:50:01 +0530482 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530483 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
484 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530485 <thead><tr>' + headings + '</tr></thead> \
486 <tbody>' + rows + '</tbody> \
487 </table></div>';
488 },
489
Nabin Hait139dc7b2014-02-12 14:53:18 +0530490 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530491 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530492 var valid = true;
Nabin Hait139dc7b2014-02-12 14:53:18 +0530493
Nabin Hait0f231062014-02-20 11:27:47 +0530494 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530495 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530496 if (!me.frm.doc[fieldname]) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530497 msgprint(frappe._("Please specify") + ": " +
498 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
499 ". " + frappe._("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530500 valid = false;
501 }
Anand Doshi3543f302013-05-24 19:25:01 +0530502 }
503 });
504 return valid;
505 },
506
507 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530508 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530509 },
510
511 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530512 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530513 },
514
515 validate_conversion_rate: function() {
516 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530517 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530518 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530519 var company_currency = this.get_company_currency();
Anand Doshi3543f302013-05-24 19:25:01 +0530520
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530521 if(!this.frm.doc.conversion_rate) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530522 frappe.throw(repl('%(conversion_rate_label)s' +
523 frappe._(' is mandatory. Maybe Currency Exchange record is not created for ') +
524 '%(from_currency)s' + frappe._(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530525 {
526 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530527 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530528 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530529 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530530 }
Anand Doshi3543f302013-05-24 19:25:01 +0530531 },
532
533 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530534 this.discount_amount_applied = false;
535 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530536 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530537 this.apply_discount_amount();
538 },
539
540 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530541 this.validate_conversion_rate();
542 this.frm.item_doclist = this.get_item_doclist();
543 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530544
Anand Doshi3543f302013-05-24 19:25:01 +0530545 this.calculate_item_values();
546 this.initialize_taxes();
547 this.determine_exclusive_rate && this.determine_exclusive_rate();
548 this.calculate_net_total();
549 this.calculate_taxes();
550 this.calculate_totals();
551 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530552
Anand Doshi3543f302013-05-24 19:25:01 +0530553 this.show_item_wise_taxes();
554 },
555
556 initialize_taxes: function() {
557 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530558
Anand Doshi3543f302013-05-24 19:25:01 +0530559 $.each(this.frm.tax_doclist, function(i, tax) {
560 tax.item_wise_tax_detail = {};
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530561 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530562 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530563 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
564
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530565 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530566 tax_fields.push("tax_amount");
567
568 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Anand Doshi3543f302013-05-24 19:25:01 +0530569
570 me.validate_on_previous_row(tax);
571 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530572 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530573 });
574 },
575
576 calculate_taxes: function() {
577 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530578 var actual_tax_dict = {};
Anand Doshi3543f302013-05-24 19:25:01 +0530579
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530580 // maintain actual tax rate based on idx
581 $.each(this.frm.tax_doclist, function(i, tax) {
582 if (tax.charge_type == "Actual") {
583 actual_tax_dict[tax.idx] = flt(tax.rate);
584 }
585 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530586
Anand Doshi3543f302013-05-24 19:25:01 +0530587 $.each(this.frm.item_doclist, function(n, item) {
588 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530589
Anand Doshi3543f302013-05-24 19:25:01 +0530590 $.each(me.frm.tax_doclist, function(i, tax) {
591 // tax_amount represents the amount of tax for the current step
592 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530593
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530594 // Adjust divisional loss to the last item
595 if (tax.charge_type == "Actual") {
596 actual_tax_dict[tax.idx] -= current_tax_amount;
597 if (n == me.frm.item_doclist.length - 1) {
598 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530599 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530600 }
601
Anand Doshi3543f302013-05-24 19:25:01 +0530602 // store tax_amount for current item as it will be used for
603 // charge type = 'On Previous Row Amount'
604 tax.tax_amount_for_current_item = current_tax_amount;
605
606 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530607 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530608 tax.tax_amount += current_tax_amount;
609
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530610 tax.tax_amount_after_discount_amount += current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530611
Anand Doshi3543f302013-05-24 19:25:01 +0530612 // for buying
613 if(tax.category) {
614 // if just for valuation, do not add the tax amount in total
615 // hence, setting it as 0 for further steps
616 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
617
618 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
619 }
620
621 // Calculate tax.total viz. grand total till that step
622 // note: grand_total_for_current_item contains the contribution of
623 // item's amount, previously applied tax and the current tax on that item
624 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530625 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530626 precision("total", tax));
627 } else {
628 tax.grand_total_for_current_item =
629 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
630 precision("total", tax));
631 }
632
633 // in tax.total, accumulate grand total for each item
634 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530635
636 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530637 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530638 me.round_off_totals(tax);
639
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530640 // adjust Discount Amount loss in last tax iteration
641 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
642 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530643 }
Anand Doshi3543f302013-05-24 19:25:01 +0530644 });
645 });
646 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530647
648 round_off_totals: function(tax) {
649 tax.total = flt(tax.total, precision("total", tax));
650 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530651 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530652 precision("tax_amount", tax));
653 },
654
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530655 adjust_discount_amount_loss: function(tax) {
656 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
657 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
658 discount_amount_loss, precision("tax_amount", tax));
659 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530660 },
Anand Doshi3543f302013-05-24 19:25:01 +0530661
662 get_current_tax_amount: function(item, tax, item_tax_map) {
663 var tax_rate = this._get_tax_rate(tax, item_tax_map);
664 var current_tax_amount = 0.0;
665
666 if(tax.charge_type == "Actual") {
667 // distribute the tax amount proportionally to each item row
668 var actual = flt(tax.rate, precision("tax_amount", tax));
669 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530670 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530671 0.0;
672
673 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530674 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530675
676 } else if(tax.charge_type == "On Previous Row Amount") {
677 current_tax_amount = (tax_rate / 100.0) *
678 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
679
680 } else if(tax.charge_type == "On Previous Row Total") {
681 current_tax_amount = (tax_rate / 100.0) *
682 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530683 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530684
Anand Doshi53b73422013-05-31 11:50:01 +0530685 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
686
687 // store tax breakup for each item
688 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
689
690 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530691 },
692
693 _cleanup: function() {
694 $.each(this.frm.tax_doclist, function(i, tax) {
695 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
696 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
697 function(i, fieldname) { delete tax[fieldname]; });
698
699 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
700 });
701 },
Anand Doshifc777182013-05-27 19:29:07 +0530702
703 calculate_total_advance: function(parenttype, advance_parentfield) {
704 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530705 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530706 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530707 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
708 ), precision("total_advance"));
709
710 this.calculate_outstanding_amount();
711 }
712 },
Anand Doshi3543f302013-05-24 19:25:01 +0530713
714 _set_in_company_currency: function(item, print_field, base_field) {
715 // set values in base currency
716 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
717 precision(base_field, item));
718 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530719
720 get_terms: function() {
721 var me = this;
722 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530723 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530724 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530725 args: {
726 doctype: "Terms and Conditions",
727 fieldname: "terms",
728 filters: { name: this.frm.doc.tc_name },
729 },
730 });
731 }
732 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530733
734 taxes_and_charges: function() {
735 var me = this;
736 if(this.frm.doc.taxes_and_charges) {
737 return this.frm.call({
738 doc: this.frm.doc,
739 method: "get_other_charges",
740 callback: function(r) {
741 if(!r.exc) {
742 me.calculate_taxes_and_totals();
743 }
744 }
745 });
746 }
747 },
748
749 show_item_wise_taxes: function() {
750 if(this.frm.fields_dict.other_charges_calculation) {
751 $(this.get_item_wise_taxes_html())
752 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
753 }
754 },
Anand Doshi3543f302013-05-24 19:25:01 +0530755});