blob: 95c7963c4e57937c0b8816ae891e5c1fd6a00cc4 [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 Doshifaefeaa2014-06-24 18:53:04 +053010 this._super();
11
Anand Doshi3543f302013-05-24 19:25:01 +053012 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053013 var today = get_today(),
Nabin Hait73083dc2014-03-14 15:10:42 +053014 currency = frappe.defaults.get_user_default("currency");
Rushabh Mehtaff938022014-04-15 18:40:00 +053015
Anand Doshi3543f302013-05-24 19:25:01 +053016 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053017 posting_date: today,
18 due_date: today,
19 transaction_date: today,
20 currency: currency,
21 price_list_currency: currency,
22 status: "Draft",
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);
Rushabh Mehtaff938022014-04-15 18:40:00 +053027 });
nabinhaitbc408d82014-07-07 16:51:52 +053028
nabinhaitb7392642014-07-28 14:55:08 +053029 if(this.frm.doc.company && !this.frm.doc.amended_from) {
nabinhaitbc408d82014-07-07 16:51:52 +053030 cur_frm.script_manager.trigger("company");
31 }
Anand Doshi3543f302013-05-24 19:25:01 +053032 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +053033
Anand Doshi1e4fb682013-08-23 12:56:33 +053034 if(this.other_fname) {
Anand Doshia91c95f2013-08-23 13:00:47 +053035 this[this.other_fname + "_remove"] = this.calculate_taxes_and_totals;
36 }
Rushabh Mehtaff938022014-04-15 18:40:00 +053037
Anand Doshia91c95f2013-08-23 13:00:47 +053038 if(this.fname) {
39 this[this.fname + "_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +053040 }
Anand Doshi3543f302013-05-24 19:25:01 +053041 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053042
Anand Doshi5e4e5d72013-08-06 17:23:44 +053043 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053044 var me = this;
Anand Doshi221dec82014-06-03 14:12:49 +053045 if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc[this.fname] && !this.frm.doc.is_pos) {
Anand Doshib054eb72014-04-22 20:35:22 +053046 this.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +053047 }
Rushabh Mehtab6843f22014-06-04 13:10:41 +053048 if(frappe.meta.get_docfield(this.tname, "item_code")) {
Rushabh Mehta880b3ef2014-06-06 11:45:11 +053049 cur_frm.get_field(this.fname).grid.set_multiple_add("item_code", "qty");
Rushabh Mehtab6843f22014-06-04 13:10:41 +053050 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053051 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053052
Anand Doshi3543f302013-05-24 19:25:01 +053053 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +053054 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053055 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053056 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053057 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053058
59 // Show POS button only if it is enabled from features setup
60 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
Rushabh Mehtae34090a2013-11-15 11:30:23 +053061 this.make_pos_btn();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053062 },
63
Rushabh Mehtae34090a2013-11-15 11:30:23 +053064 make_pos_btn: function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053065 if(!this.pos_active) {
Pratik Vyasb52618c2014-04-14 16:25:30 +053066 var btn_label = __("POS View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053067 icon = "icon-desktop";
68 } else {
Pratik Vyasb52618c2014-04-14 16:25:30 +053069 var btn_label = __(this.frm.doctype) + __(" View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053070 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053071 }
72 var me = this;
Rushabh Mehtaff938022014-04-15 18:40:00 +053073
Rushabh Mehtae34090a2013-11-15 11:30:23 +053074 this.$pos_btn = this.frm.appframe.add_button(btn_label, function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053075 me.toggle_pos();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053076 }, icon);
77 },
78
79 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053080 // Check whether it is Selling or Buying cycle
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053081 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053082 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
Rushabh Mehtaff938022014-04-15 18:40:00 +053083
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053084 if (!price_list)
Pratik Vyasb52618c2014-04-14 16:25:30 +053085 msgprint(__("Please select Price List"))
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053086 else {
87 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
88
89 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053090 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053091 this.frm.layout.add_view("pos");
92 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
93 }
94
95 // toggle view
96 this.frm.layout.set_view(this.pos_active ? "" : "pos");
97 this.pos_active = !this.pos_active;
98
99 // refresh
100 if(this.pos_active)
101 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530102 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530103 }
Anand Doshi3543f302013-05-24 19:25:01 +0530104 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530105
106
Nabin Hait139dc7b2014-02-12 14:53:18 +0530107 item_code: function(doc, cdt, cdn) {
108 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530109 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530110 if(item.item_code || item.barcode || item.serial_no) {
111 if(!this.validate_company_and_party()) {
112 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
113 } else {
114 return this.frm.call({
115 method: "erpnext.stock.get_item_details.get_item_details",
116 child: item,
117 args: {
118 args: {
119 item_code: item.item_code,
120 barcode: item.barcode,
121 serial_no: item.serial_no,
122 warehouse: item.warehouse,
Nabin Hait444f9562014-06-20 15:59:49 +0530123 parenttype: me.frm.doc.doctype,
124 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530125 customer: me.frm.doc.customer,
126 supplier: me.frm.doc.supplier,
127 currency: me.frm.doc.currency,
128 conversion_rate: me.frm.doc.conversion_rate,
129 price_list: me.frm.doc.selling_price_list ||
130 me.frm.doc.buying_price_list,
131 price_list_currency: me.frm.doc.price_list_currency,
132 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
133 company: me.frm.doc.company,
134 order_type: me.frm.doc.order_type,
135 is_pos: cint(me.frm.doc.is_pos),
136 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Hait444f9562014-06-20 15:59:49 +0530137 transaction_date: me.frm.doc.transaction_date,
138 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
139 doctype: item.doctype,
140 name: item.name
Nabin Hait139dc7b2014-02-12 14:53:18 +0530141 }
142 },
143 callback: function(r) {
144 if(!r.exc) {
145 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
146 }
147 }
148 });
149 }
150 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530151 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530152
153 serial_no: function(doc, cdt, cdn) {
154 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530155 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530156
157 if (item.serial_no) {
158 if (!item.item_code) {
159 this.frm.script_manager.trigger("item_code", cdt, cdn);
160 }
161 else {
162 var sr_no = [];
163
164 // Replacing all occurences of comma with carriage return
165 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
166
167 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530168
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530169 // Trim each string and push unique string to new list
170 for (var x=0; x<=serial_nos.length - 1; x++) {
171 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
172 sr_no.push(serial_nos[x].trim());
173 }
174 }
175
176 // Add the new list to the serial no. field in grid with each in new line
177 item.serial_no = "";
178 for (var x=0; x<=sr_no.length - 1; x++)
179 item.serial_no += sr_no[x] + '\n';
180
181 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530182 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530183 }
184 }
185 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530186
Anand Doshi923d41d2013-05-28 17:23:36 +0530187 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530188 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530189 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530190
Anand Doshi3543f302013-05-24 19:25:01 +0530191 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530192 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530193 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530194 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530195 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530196 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530197
Akhilesh Darjee78378272013-10-11 19:06:30 +0530198 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530199 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530200 }
201 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530202 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530203 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530204
Nabin Hait48a0bd32013-07-23 15:31:39 +0530205 this.frm.script_manager.trigger("currency");
Nabin Hait444f9562014-06-20 15:59:49 +0530206 this.apply_pricing_rule();
Anand Doshi3543f302013-05-24 19:25:01 +0530207 }
208 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530209
Anand Doshi3543f302013-05-24 19:25:01 +0530210 get_company_currency: function() {
211 return erpnext.get_currency(this.frm.doc.company);
212 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530213
Anand Doshi3543f302013-05-24 19:25:01 +0530214 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530215 var me = this;
216 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530217
Anand Doshi61a2f682013-06-21 17:55:31 +0530218 var company_currency = this.get_company_currency();
219 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530220 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530221 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530222 me.frm.set_value("conversion_rate", exchange_rate);
223 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530224 });
225 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530226 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530227 }
228 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530229
Anand Doshi61a2f682013-06-21 17:55:31 +0530230 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530231 if(this.frm.doc.currency === this.get_company_currency()) {
232 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530233 }
234 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530235 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
236 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
237 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530238 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530239 if(this.frm.doc.ignore_pricing_rule) {
240 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530241 } else if (!this.in_apply_price_list){
242 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530243 }
244
Nabin Haita3dd72a2014-05-28 12:49:20 +0530245 }
Anand Doshi3543f302013-05-24 19:25:01 +0530246 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530247
Anand Doshi61a2f682013-06-21 17:55:31 +0530248 get_exchange_rate: function(from_currency, to_currency, callback) {
249 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530250 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530251 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530252 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530253 });
254 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530255
Anand Doshi3543f302013-05-24 19:25:01 +0530256 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530257 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530258 this.set_dynamic_labels();
Anand Doshi51f722d2014-07-04 15:44:26 +0530259 this.set_plc_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530260 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530261
Anand Doshi3543f302013-05-24 19:25:01 +0530262 plc_conversion_rate: function() {
Anand Doshi51f722d2014-07-04 15:44:26 +0530263 this.set_plc_conversion_rate();
264 if(!this.in_apply_price_list) {
265 this.apply_price_list();
266 }
267 },
268
269 set_plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530270 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
271 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530272 }
273 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530274 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530275 }
Anand Doshi3543f302013-05-24 19:25:01 +0530276 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530277
Anand Doshi3543f302013-05-24 19:25:01 +0530278 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530279 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530280 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530281
Anand Doshi652bc072014-04-16 15:21:46 +0530282 // tax rate
283 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530284 this.calculate_taxes_and_totals();
285 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530286
Anand Doshi923d41d2013-05-28 17:23:36 +0530287 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530288 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530289 try {
290 this.validate_on_previous_row(tax);
291 this.calculate_taxes_and_totals();
292 } catch(e) {
293 tax.row_id = null;
294 refresh_field("row_id", tax.name, tax.parentfield);
295 throw e;
296 }
297 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530298
Anand Doshi61a2f682013-06-21 17:55:31 +0530299 set_dynamic_labels: function() {
300 // What TODO? should we make price list system non-mandatory?
301 this.frm.toggle_reqd("plc_conversion_rate",
302 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530303
Anand Doshi61a2f682013-06-21 17:55:31 +0530304 var company_currency = this.get_company_currency();
305 this.change_form_labels(company_currency);
306 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530307 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530308 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530309
Anand Doshi923d41d2013-05-28 17:23:36 +0530310 recalculate: function() {
311 this.calculate_taxes_and_totals();
312 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530313
Anand Doshi923d41d2013-05-28 17:23:36 +0530314 recalculate_values: function() {
315 this.calculate_taxes_and_totals();
316 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530317
Anand Doshid0b00722013-05-28 18:54:48 +0530318 calculate_charges: function() {
319 this.calculate_taxes_and_totals();
320 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530321
Nabin Hait444f9562014-06-20 15:59:49 +0530322 ignore_pricing_rule: function() {
323 this.apply_pricing_rule();
324 },
325
326 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530327 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530328 return this.frm.call({
329 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
330 args: { args: this._get_args(item) },
331 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530332 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530333 me._set_values_for_item_list(r.message);
334 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
335 }
336 }
337 });
338 },
339
340 _get_args: function(item) {
341 var me = this;
342 return {
343 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530344 "customer": me.frm.doc.customer,
345 "customer_group": me.frm.doc.customer_group,
346 "territory": me.frm.doc.territory,
347 "supplier": me.frm.doc.supplier,
348 "supplier_type": me.frm.doc.supplier_type,
349 "currency": me.frm.doc.currency,
350 "conversion_rate": me.frm.doc.conversion_rate,
351 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
352 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
353 "company": me.frm.doc.company,
354 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
355 "campaign": me.frm.doc.campaign,
356 "sales_partner": me.frm.doc.sales_partner,
357 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
358 "parenttype": me.frm.doc.doctype,
359 "parent": me.frm.doc.name
360 };
Nabin Hait444f9562014-06-20 15:59:49 +0530361 },
362
363 _get_item_list: function(item) {
364 var item_list = [];
365 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530366 if (d.item_code) {
367 item_list.push({
368 "doctype": d.doctype,
369 "name": d.name,
370 "item_code": d.item_code,
371 "item_group": d.item_group,
372 "brand": d.brand,
373 "qty": d.qty
374 });
375 }
Nabin Hait444f9562014-06-20 15:59:49 +0530376 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530377
Nabin Hait444f9562014-06-20 15:59:49 +0530378 if (item) {
379 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530380 } else {
Nabin Hait444f9562014-06-20 15:59:49 +0530381 $.each(this.get_item_doclist(), function(i, d) {
382 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530383 });
384 }
Nabin Hait444f9562014-06-20 15:59:49 +0530385 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530386 },
387
Anand Doshidffec8f2014-07-01 17:45:15 +0530388 _set_values_for_item_list: function(children) {
389 $.each(children, function(i, d) {
390 $.each(d, function(k, v) {
391 if (["doctype", "name"].indexOf(k)===-1) {
392 frappe.model.set_value(d.doctype, d.name, k, v);
393 }
394 });
395 });
396 },
397
398 apply_price_list: function() {
399 var me = this;
400 return this.frm.call({
401 method: "erpnext.stock.get_item_details.apply_price_list",
402 args: { args: this._get_args() },
403 callback: function(r) {
404 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530405 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530406 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
407 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530408 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +0530409 me._set_values_for_item_list(r.message.children);
410 }
411 }
412 });
413 },
414
Anand Doshi3543f302013-05-24 19:25:01 +0530415 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530416 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530417 try {
418 this.validate_on_previous_row(tax);
419 this.validate_inclusive_tax(tax);
420 this.calculate_taxes_and_totals();
421 } catch(e) {
422 tax.included_in_print_rate = 0;
423 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
424 throw e;
425 }
426 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530427
Anand Doshi3543f302013-05-24 19:25:01 +0530428 validate_on_previous_row: function(tax) {
429 // validate if a valid row id is mentioned in case of
430 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530431 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530432 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530433 var msg = __("Please specify a valid Row ID for row {0} in table {1}", [tax.idx, __(tax.doctype)])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530434 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530435 }
436 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530437
Anand Doshi3543f302013-05-24 19:25:01 +0530438 validate_inclusive_tax: function(tax) {
439 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530440
Anand Doshi3543f302013-05-24 19:25:01 +0530441 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530442 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530443 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530444 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530445
Anand Doshi3543f302013-05-24 19:25:01 +0530446 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530447 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
448 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530449 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530450 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530451
Anand Doshi3543f302013-05-24 19:25:01 +0530452 if(cint(tax.included_in_print_rate)) {
453 if(tax.charge_type == "Actual") {
454 // inclusive tax cannot be of type Actual
455 actual_type_error();
456 } else if(tax.charge_type == "On Previous Row Amount" &&
457 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
458 // referred row should also be an inclusive tax
459 on_previous_row_error(tax.row_id);
460 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530461 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530462 function(t) { return cint(t.included_in_print_rate) ? null : t; });
463 if(taxes_not_included.length > 0) {
464 // all rows above this tax should be inclusive
465 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
466 }
467 }
468 }
469 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530470
Anand Doshi3543f302013-05-24 19:25:01 +0530471 _load_item_tax_rate: function(item_tax_rate) {
472 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
473 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530474
Anand Doshi3543f302013-05-24 19:25:01 +0530475 _get_tax_rate: function(tax, item_tax_map) {
476 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
477 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
478 tax.rate;
479 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530480
Anand Doshi3543f302013-05-24 19:25:01 +0530481 get_item_wise_taxes_html: function() {
482 var item_tax = {};
483 var tax_accounts = [];
484 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530485
Anand Doshi3543f302013-05-24 19:25:01 +0530486 $.each(this.get_tax_doclist(), function(i, tax) {
487 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530488 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530489 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530490 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530491 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530492 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530493 var tax_rate = "";
494 if(tax_data[0] != null) {
495 tax_rate = (tax.charge_type === "Actual") ?
496 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
497 (flt(tax_data[0], tax_rate_precision) + "%");
498 }
499 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
500 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530501
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530502 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530503 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530504 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530505 }
Anand Doshi3543f302013-05-24 19:25:01 +0530506 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530507 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530508 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530509
510 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530511 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530512
Anand Doshi179e3772013-09-05 16:53:57 +0530513 var distinct_item_names = [];
514 var distinct_items = [];
515 $.each(this.get_item_doclist(), function(i, item) {
516 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
517 distinct_item_names.push(item.item_code || item.item_name);
518 distinct_items.push(item);
519 }
520 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530521
Anand Doshi179e3772013-09-05 16:53:57 +0530522 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530523 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530524 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530525 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
526 item_name: item.item_name,
527 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530528 return item_tax_record[head[0]] ?
529 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530530 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530531 }).join("\n")
532 });
533 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530534
Anand Doshi53b73422013-05-31 11:50:01 +0530535 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530536 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
537 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530538 <thead><tr>' + headings + '</tr></thead> \
539 <tbody>' + rows + '</tbody> \
540 </table></div>';
541 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530542
Nabin Hait139dc7b2014-02-12 14:53:18 +0530543 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530544 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530545 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530546
Nabin Hait0f231062014-02-20 11:27:47 +0530547 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530548 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530549 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530550 msgprint(__("Please specify") + ": " +
551 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530552 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530553 valid = false;
554 }
Anand Doshi3543f302013-05-24 19:25:01 +0530555 }
556 });
557 return valid;
558 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530559
Anand Doshi3543f302013-05-24 19:25:01 +0530560 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530561 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530562 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530563
Anand Doshi3543f302013-05-24 19:25:01 +0530564 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530565 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530566 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530567
Anand Doshi3543f302013-05-24 19:25:01 +0530568 validate_conversion_rate: function() {
569 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530570 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530571 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530572 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530573
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530574 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530575 frappe.throw(repl('%(conversion_rate_label)s' +
576 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
577 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530578 {
579 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530580 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530581 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530582 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530583 }
Anand Doshi3543f302013-05-24 19:25:01 +0530584 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530585
Anand Doshi3543f302013-05-24 19:25:01 +0530586 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530587 this.discount_amount_applied = false;
588 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530589 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530590 this.apply_discount_amount();
591 },
592
593 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530594 this.validate_conversion_rate();
595 this.frm.item_doclist = this.get_item_doclist();
596 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530597
Anand Doshi3543f302013-05-24 19:25:01 +0530598 this.calculate_item_values();
599 this.initialize_taxes();
600 this.determine_exclusive_rate && this.determine_exclusive_rate();
601 this.calculate_net_total();
602 this.calculate_taxes();
603 this.calculate_totals();
604 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530605
Anand Doshi3543f302013-05-24 19:25:01 +0530606 this.show_item_wise_taxes();
607 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530608
Anand Doshi3543f302013-05-24 19:25:01 +0530609 initialize_taxes: function() {
610 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530611
Anand Doshi3543f302013-05-24 19:25:01 +0530612 $.each(this.frm.tax_doclist, function(i, tax) {
613 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530614 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530615 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530616 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
617
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530618 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530619 tax_fields.push("tax_amount");
620
621 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530622
Anand Doshi3543f302013-05-24 19:25:01 +0530623 me.validate_on_previous_row(tax);
624 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530625 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530626 });
627 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530628
Anand Doshi3543f302013-05-24 19:25:01 +0530629 calculate_taxes: function() {
630 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530631 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530632
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530633 // maintain actual tax rate based on idx
634 $.each(this.frm.tax_doclist, function(i, tax) {
635 if (tax.charge_type == "Actual") {
636 actual_tax_dict[tax.idx] = flt(tax.rate);
637 }
638 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530639
Anand Doshi3543f302013-05-24 19:25:01 +0530640 $.each(this.frm.item_doclist, function(n, item) {
641 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530642
Anand Doshi3543f302013-05-24 19:25:01 +0530643 $.each(me.frm.tax_doclist, function(i, tax) {
644 // tax_amount represents the amount of tax for the current step
645 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530646
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530647 // Adjust divisional loss to the last item
648 if (tax.charge_type == "Actual") {
649 actual_tax_dict[tax.idx] -= current_tax_amount;
650 if (n == me.frm.item_doclist.length - 1) {
651 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530652 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530653 }
654
Anand Doshi3543f302013-05-24 19:25:01 +0530655 // store tax_amount for current item as it will be used for
656 // charge type = 'On Previous Row Amount'
657 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530658
Anand Doshi3543f302013-05-24 19:25:01 +0530659 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530660 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530661 tax.tax_amount += current_tax_amount;
662
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530663 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530664
Anand Doshi3543f302013-05-24 19:25:01 +0530665 // for buying
666 if(tax.category) {
667 // if just for valuation, do not add the tax amount in total
668 // hence, setting it as 0 for further steps
669 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530670
Anand Doshi3543f302013-05-24 19:25:01 +0530671 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
672 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530673
Anand Doshi3543f302013-05-24 19:25:01 +0530674 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530675 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530676 // item's amount, previously applied tax and the current tax on that item
677 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530678 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530679 precision("total", tax));
680 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530681 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530682 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
683 precision("total", tax));
684 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530685
Anand Doshi3543f302013-05-24 19:25:01 +0530686 // in tax.total, accumulate grand total for each item
687 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530688
689 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530690 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530691 me.round_off_totals(tax);
692
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530693 // adjust Discount Amount loss in last tax iteration
694 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
695 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530696 }
Anand Doshi3543f302013-05-24 19:25:01 +0530697 });
698 });
699 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530700
701 round_off_totals: function(tax) {
702 tax.total = flt(tax.total, precision("total", tax));
703 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530704 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530705 precision("tax_amount", tax));
706 },
707
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530708 adjust_discount_amount_loss: function(tax) {
709 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530710 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530711 discount_amount_loss, precision("tax_amount", tax));
712 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530713 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530714
Anand Doshi3543f302013-05-24 19:25:01 +0530715 get_current_tax_amount: function(item, tax, item_tax_map) {
716 var tax_rate = this._get_tax_rate(tax, item_tax_map);
717 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530718
Anand Doshi3543f302013-05-24 19:25:01 +0530719 if(tax.charge_type == "Actual") {
720 // distribute the tax amount proportionally to each item row
721 var actual = flt(tax.rate, precision("tax_amount", tax));
722 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530723 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530724 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530725
Anand Doshi3543f302013-05-24 19:25:01 +0530726 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530727 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530728
Anand Doshi3543f302013-05-24 19:25:01 +0530729 } else if(tax.charge_type == "On Previous Row Amount") {
730 current_tax_amount = (tax_rate / 100.0) *
731 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530732
Anand Doshi3543f302013-05-24 19:25:01 +0530733 } else if(tax.charge_type == "On Previous Row Total") {
734 current_tax_amount = (tax_rate / 100.0) *
735 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530736 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530737
Anand Doshi53b73422013-05-31 11:50:01 +0530738 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530739
Anand Doshi53b73422013-05-31 11:50:01 +0530740 // store tax breakup for each item
741 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530742
Anand Doshi53b73422013-05-31 11:50:01 +0530743 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530744 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530745
Anand Doshi3543f302013-05-24 19:25:01 +0530746 _cleanup: function() {
747 $.each(this.frm.tax_doclist, function(i, tax) {
748 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530749 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530750 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530751
Anand Doshi3543f302013-05-24 19:25:01 +0530752 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
753 });
754 },
Anand Doshifc777182013-05-27 19:29:07 +0530755
Anand Doshi652bc072014-04-16 15:21:46 +0530756 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530757 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530758 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530759 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530760 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
761 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530762
Anand Doshi652bc072014-04-16 15:21:46 +0530763 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530764 }
765 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530766
Anand Doshi3543f302013-05-24 19:25:01 +0530767 _set_in_company_currency: function(item, print_field, base_field) {
768 // set values in base currency
769 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
770 precision(base_field, item));
771 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530772
Anand Doshibf3e54a2013-06-05 14:11:32 +0530773 get_terms: function() {
774 var me = this;
775 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530776 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530777 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530778 args: {
779 doctype: "Terms and Conditions",
780 fieldname: "terms",
781 filters: { name: this.frm.doc.tc_name },
782 },
783 });
784 }
785 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530786
787 taxes_and_charges: function() {
788 var me = this;
789 if(this.frm.doc.taxes_and_charges) {
790 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530791 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
792 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530793 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
794 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530795 "master_name": this.frm.doc.taxes_and_charges,
796 "tax_parentfield": this.other_fname
797 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530798 callback: function(r) {
799 if(!r.exc) {
Nabin Hait6b039142014-05-02 15:45:10 +0530800 me.frm.set_value(me.other_fname, r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530801 me.calculate_taxes_and_totals();
802 }
803 }
804 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530805 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530806 },
807
808 show_item_wise_taxes: function() {
809 if(this.frm.fields_dict.other_charges_calculation) {
810 $(this.get_item_wise_taxes_html())
811 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
812 }
813 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530814});