blob: 915bbd9eb3b5041270b20a6151ce725fccfbe527 [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
Rushabh Mehta23607f92014-08-08 17:23:40 +053060 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 var me = this;
Rushabh Mehta23607f92014-08-08 17:23:40 +053066 if(this.frm.doc.docstatus===0) {
67 if(!this.pos_active) {
68 var btn_label = __("POS View"),
69 icon = "icon-th";
70 } else {
71 var btn_label = __("Form View"),
72 icon = "icon-file-text";
73 }
Rushabh Mehtaff938022014-04-15 18:40:00 +053074
Rushabh Mehta23607f92014-08-08 17:23:40 +053075 if(erpnext.open_as_pos) {
76 me.toggle_pos(true);
77 erpnext.open_as_pos = false;
78 }
Rushabh Mehta72e17192014-08-08 15:30:49 +053079
Rushabh Mehta23607f92014-08-08 17:23:40 +053080 this.$pos_btn = this.frm.appframe.add_primary_action(btn_label, function() {
81 me.toggle_pos();
82 }, icon, "btn-default");
83 } else {
84 // hack: will avoid calling refresh from refresh
85 setTimeout(function() { me.toggle_pos(false); }, 100);
Rushabh Mehta72e17192014-08-08 15:30:49 +053086 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053087 },
88
89 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053090 // Check whether it is Selling or Buying cycle
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053091 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053092 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
Rushabh Mehtaff938022014-04-15 18:40:00 +053093
Rushabh Mehta23607f92014-08-08 17:23:40 +053094 if((show===true && this.pos_active) || (show===false && !this.pos_active))
95 return;
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053096
Rushabh Mehta23607f92014-08-08 17:23:40 +053097 if(show && !price_list) {
98 frappe.throw(__("Please select Price List"));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053099 }
Rushabh Mehta23607f92014-08-08 17:23:40 +0530100
101 // make pos
102 if(!this.frm.pos) {
103 this.frm.layout.add_view("pos");
104 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
105 }
106
107 // toggle view
108 this.frm.layout.set_view(this.pos_active ? "" : "pos");
109 this.pos_active = !this.pos_active;
110
111 // refresh
112 if(this.pos_active)
113 this.frm.pos.refresh();
114 this.frm.refresh();
Anand Doshi3543f302013-05-24 19:25:01 +0530115 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530116
117
Nabin Hait139dc7b2014-02-12 14:53:18 +0530118 item_code: function(doc, cdt, cdn) {
119 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530120 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530121 if(item.item_code || item.barcode || item.serial_no) {
122 if(!this.validate_company_and_party()) {
123 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
124 } else {
125 return this.frm.call({
126 method: "erpnext.stock.get_item_details.get_item_details",
127 child: item,
128 args: {
129 args: {
130 item_code: item.item_code,
131 barcode: item.barcode,
132 serial_no: item.serial_no,
133 warehouse: item.warehouse,
Nabin Hait444f9562014-06-20 15:59:49 +0530134 parenttype: me.frm.doc.doctype,
135 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530136 customer: me.frm.doc.customer,
137 supplier: me.frm.doc.supplier,
138 currency: me.frm.doc.currency,
139 conversion_rate: me.frm.doc.conversion_rate,
140 price_list: me.frm.doc.selling_price_list ||
141 me.frm.doc.buying_price_list,
142 price_list_currency: me.frm.doc.price_list_currency,
143 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
144 company: me.frm.doc.company,
145 order_type: me.frm.doc.order_type,
146 is_pos: cint(me.frm.doc.is_pos),
147 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Hait444f9562014-06-20 15:59:49 +0530148 transaction_date: me.frm.doc.transaction_date,
149 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
150 doctype: item.doctype,
151 name: item.name
Nabin Hait139dc7b2014-02-12 14:53:18 +0530152 }
153 },
154 callback: function(r) {
155 if(!r.exc) {
156 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
157 }
158 }
159 });
160 }
161 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530162 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530163
164 serial_no: function(doc, cdt, cdn) {
165 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530166 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530167
168 if (item.serial_no) {
169 if (!item.item_code) {
170 this.frm.script_manager.trigger("item_code", cdt, cdn);
171 }
172 else {
173 var sr_no = [];
174
175 // Replacing all occurences of comma with carriage return
176 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
177
178 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530179
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530180 // Trim each string and push unique string to new list
181 for (var x=0; x<=serial_nos.length - 1; x++) {
182 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
183 sr_no.push(serial_nos[x].trim());
184 }
185 }
186
187 // Add the new list to the serial no. field in grid with each in new line
188 item.serial_no = "";
189 for (var x=0; x<=sr_no.length - 1; x++)
190 item.serial_no += sr_no[x] + '\n';
191
192 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530193 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530194 }
195 }
196 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530197
Anand Doshi923d41d2013-05-28 17:23:36 +0530198 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530199 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530200 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530201
Anand Doshi3543f302013-05-24 19:25:01 +0530202 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530203 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530204 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530205 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530206 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530207 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530208
Akhilesh Darjee78378272013-10-11 19:06:30 +0530209 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530210 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530211 }
212 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530213 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530214 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530215
Nabin Hait48a0bd32013-07-23 15:31:39 +0530216 this.frm.script_manager.trigger("currency");
Nabin Hait444f9562014-06-20 15:59:49 +0530217 this.apply_pricing_rule();
Anand Doshi3543f302013-05-24 19:25:01 +0530218 }
219 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530220
Anand Doshi3543f302013-05-24 19:25:01 +0530221 get_company_currency: function() {
222 return erpnext.get_currency(this.frm.doc.company);
223 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530224
Anand Doshi3543f302013-05-24 19:25:01 +0530225 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530226 var me = this;
227 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530228
Anand Doshi61a2f682013-06-21 17:55:31 +0530229 var company_currency = this.get_company_currency();
230 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530231 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530232 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530233 me.frm.set_value("conversion_rate", exchange_rate);
234 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530235 });
236 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530237 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530238 }
239 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530240
Anand Doshi61a2f682013-06-21 17:55:31 +0530241 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530242 if(this.frm.doc.currency === this.get_company_currency()) {
243 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530244 }
245 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530246 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
247 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
248 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530249 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530250 if(this.frm.doc.ignore_pricing_rule) {
251 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530252 } else if (!this.in_apply_price_list){
253 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530254 }
255
Nabin Haita3dd72a2014-05-28 12:49:20 +0530256 }
Anand Doshi3543f302013-05-24 19:25:01 +0530257 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530258
Anand Doshi61a2f682013-06-21 17:55:31 +0530259 get_exchange_rate: function(from_currency, to_currency, callback) {
260 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530261 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530262 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530263 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530264 });
265 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530266
Anand Doshi3543f302013-05-24 19:25:01 +0530267 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530268 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530269 this.set_dynamic_labels();
Anand Doshi51f722d2014-07-04 15:44:26 +0530270 this.set_plc_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530271 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530272
Anand Doshi3543f302013-05-24 19:25:01 +0530273 plc_conversion_rate: function() {
Anand Doshi51f722d2014-07-04 15:44:26 +0530274 this.set_plc_conversion_rate();
275 if(!this.in_apply_price_list) {
276 this.apply_price_list();
277 }
278 },
279
280 set_plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530281 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
282 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530283 }
284 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530285 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530286 }
Anand Doshi3543f302013-05-24 19:25:01 +0530287 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530288
Anand Doshi3543f302013-05-24 19:25:01 +0530289 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530290 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530291 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530292
Anand Doshi652bc072014-04-16 15:21:46 +0530293 // tax rate
294 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530295 this.calculate_taxes_and_totals();
296 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530297
Anand Doshi923d41d2013-05-28 17:23:36 +0530298 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530299 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530300 try {
301 this.validate_on_previous_row(tax);
302 this.calculate_taxes_and_totals();
303 } catch(e) {
304 tax.row_id = null;
305 refresh_field("row_id", tax.name, tax.parentfield);
306 throw e;
307 }
308 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530309
Anand Doshi61a2f682013-06-21 17:55:31 +0530310 set_dynamic_labels: function() {
311 // What TODO? should we make price list system non-mandatory?
312 this.frm.toggle_reqd("plc_conversion_rate",
313 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530314
Anand Doshi61a2f682013-06-21 17:55:31 +0530315 var company_currency = this.get_company_currency();
316 this.change_form_labels(company_currency);
317 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530318 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530319 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530320
Anand Doshi923d41d2013-05-28 17:23:36 +0530321 recalculate: function() {
322 this.calculate_taxes_and_totals();
323 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530324
Anand Doshi923d41d2013-05-28 17:23:36 +0530325 recalculate_values: function() {
326 this.calculate_taxes_and_totals();
327 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530328
Anand Doshid0b00722013-05-28 18:54:48 +0530329 calculate_charges: function() {
330 this.calculate_taxes_and_totals();
331 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530332
Nabin Hait444f9562014-06-20 15:59:49 +0530333 ignore_pricing_rule: function() {
334 this.apply_pricing_rule();
335 },
336
337 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530338 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530339 return this.frm.call({
340 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
341 args: { args: this._get_args(item) },
342 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530343 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530344 me._set_values_for_item_list(r.message);
345 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
346 }
347 }
348 });
349 },
350
351 _get_args: function(item) {
352 var me = this;
353 return {
354 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530355 "customer": me.frm.doc.customer,
356 "customer_group": me.frm.doc.customer_group,
357 "territory": me.frm.doc.territory,
358 "supplier": me.frm.doc.supplier,
359 "supplier_type": me.frm.doc.supplier_type,
360 "currency": me.frm.doc.currency,
361 "conversion_rate": me.frm.doc.conversion_rate,
362 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
363 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
364 "company": me.frm.doc.company,
365 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
366 "campaign": me.frm.doc.campaign,
367 "sales_partner": me.frm.doc.sales_partner,
368 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
369 "parenttype": me.frm.doc.doctype,
370 "parent": me.frm.doc.name
371 };
Nabin Hait444f9562014-06-20 15:59:49 +0530372 },
373
374 _get_item_list: function(item) {
375 var item_list = [];
376 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530377 if (d.item_code) {
378 item_list.push({
379 "doctype": d.doctype,
380 "name": d.name,
381 "item_code": d.item_code,
382 "item_group": d.item_group,
383 "brand": d.brand,
384 "qty": d.qty
385 });
386 }
Nabin Hait444f9562014-06-20 15:59:49 +0530387 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530388
Nabin Hait444f9562014-06-20 15:59:49 +0530389 if (item) {
390 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530391 } else {
Nabin Hait444f9562014-06-20 15:59:49 +0530392 $.each(this.get_item_doclist(), function(i, d) {
393 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530394 });
395 }
Nabin Hait444f9562014-06-20 15:59:49 +0530396 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530397 },
398
Anand Doshidffec8f2014-07-01 17:45:15 +0530399 _set_values_for_item_list: function(children) {
400 $.each(children, function(i, d) {
401 $.each(d, function(k, v) {
402 if (["doctype", "name"].indexOf(k)===-1) {
403 frappe.model.set_value(d.doctype, d.name, k, v);
404 }
405 });
406 });
407 },
408
409 apply_price_list: function() {
410 var me = this;
411 return this.frm.call({
412 method: "erpnext.stock.get_item_details.apply_price_list",
413 args: { args: this._get_args() },
414 callback: function(r) {
415 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530416 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530417 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
418 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530419 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +0530420 me._set_values_for_item_list(r.message.children);
421 }
422 }
423 });
424 },
425
Anand Doshi3543f302013-05-24 19:25:01 +0530426 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530427 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530428 try {
429 this.validate_on_previous_row(tax);
430 this.validate_inclusive_tax(tax);
431 this.calculate_taxes_and_totals();
432 } catch(e) {
433 tax.included_in_print_rate = 0;
434 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
435 throw e;
436 }
437 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530438
Anand Doshi3543f302013-05-24 19:25:01 +0530439 validate_on_previous_row: function(tax) {
440 // validate if a valid row id is mentioned in case of
441 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530442 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530443 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530444 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 +0530445 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530446 }
447 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530448
Anand Doshi3543f302013-05-24 19:25:01 +0530449 validate_inclusive_tax: function(tax) {
450 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530451
Anand Doshi3543f302013-05-24 19:25:01 +0530452 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530453 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530454 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530455 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530456
Anand Doshi3543f302013-05-24 19:25:01 +0530457 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530458 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
459 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530460 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530461 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530462
Anand Doshi3543f302013-05-24 19:25:01 +0530463 if(cint(tax.included_in_print_rate)) {
464 if(tax.charge_type == "Actual") {
465 // inclusive tax cannot be of type Actual
466 actual_type_error();
467 } else if(tax.charge_type == "On Previous Row Amount" &&
468 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
469 // referred row should also be an inclusive tax
470 on_previous_row_error(tax.row_id);
471 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530472 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530473 function(t) { return cint(t.included_in_print_rate) ? null : t; });
474 if(taxes_not_included.length > 0) {
475 // all rows above this tax should be inclusive
476 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
477 }
478 }
479 }
480 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530481
Anand Doshi3543f302013-05-24 19:25:01 +0530482 _load_item_tax_rate: function(item_tax_rate) {
483 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
484 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530485
Anand Doshi3543f302013-05-24 19:25:01 +0530486 _get_tax_rate: function(tax, item_tax_map) {
487 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
488 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
489 tax.rate;
490 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530491
Anand Doshi3543f302013-05-24 19:25:01 +0530492 get_item_wise_taxes_html: function() {
493 var item_tax = {};
494 var tax_accounts = [];
495 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530496
Anand Doshi3543f302013-05-24 19:25:01 +0530497 $.each(this.get_tax_doclist(), function(i, tax) {
498 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530499 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530500 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530501 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530502 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530503 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530504 var tax_rate = "";
505 if(tax_data[0] != null) {
506 tax_rate = (tax.charge_type === "Actual") ?
507 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
508 (flt(tax_data[0], tax_rate_precision) + "%");
509 }
510 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
511 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530512
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530513 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530514 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530515 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530516 }
Anand Doshi3543f302013-05-24 19:25:01 +0530517 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530518 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530519 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530520
521 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530522 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530523
Anand Doshi179e3772013-09-05 16:53:57 +0530524 var distinct_item_names = [];
525 var distinct_items = [];
526 $.each(this.get_item_doclist(), function(i, item) {
527 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
528 distinct_item_names.push(item.item_code || item.item_name);
529 distinct_items.push(item);
530 }
531 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530532
Anand Doshi179e3772013-09-05 16:53:57 +0530533 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530534 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530535 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530536 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
537 item_name: item.item_name,
538 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530539 return item_tax_record[head[0]] ?
540 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530541 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530542 }).join("\n")
543 });
544 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530545
Anand Doshi53b73422013-05-31 11:50:01 +0530546 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530547 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
548 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530549 <thead><tr>' + headings + '</tr></thead> \
550 <tbody>' + rows + '</tbody> \
551 </table></div>';
552 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530553
Nabin Hait139dc7b2014-02-12 14:53:18 +0530554 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530555 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530556 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530557
Nabin Hait0f231062014-02-20 11:27:47 +0530558 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530559 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530560 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530561 msgprint(__("Please specify") + ": " +
562 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530563 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530564 valid = false;
565 }
Anand Doshi3543f302013-05-24 19:25:01 +0530566 }
567 });
568 return valid;
569 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530570
Anand Doshi3543f302013-05-24 19:25:01 +0530571 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530572 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530573 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530574
Anand Doshi3543f302013-05-24 19:25:01 +0530575 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530576 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530577 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530578
Anand Doshi3543f302013-05-24 19:25:01 +0530579 validate_conversion_rate: function() {
580 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530581 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530582 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530583 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530584
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530585 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530586 frappe.throw(repl('%(conversion_rate_label)s' +
587 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
588 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530589 {
590 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530591 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530592 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530593 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530594 }
Anand Doshi3543f302013-05-24 19:25:01 +0530595 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530596
Anand Doshi3543f302013-05-24 19:25:01 +0530597 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530598 this.discount_amount_applied = false;
599 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530600 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530601 this.apply_discount_amount();
602 },
603
604 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530605 this.validate_conversion_rate();
606 this.frm.item_doclist = this.get_item_doclist();
607 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530608
Anand Doshi3543f302013-05-24 19:25:01 +0530609 this.calculate_item_values();
610 this.initialize_taxes();
611 this.determine_exclusive_rate && this.determine_exclusive_rate();
612 this.calculate_net_total();
613 this.calculate_taxes();
614 this.calculate_totals();
615 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530616
Anand Doshi3543f302013-05-24 19:25:01 +0530617 this.show_item_wise_taxes();
618 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530619
Anand Doshi3543f302013-05-24 19:25:01 +0530620 initialize_taxes: function() {
621 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530622
Anand Doshi3543f302013-05-24 19:25:01 +0530623 $.each(this.frm.tax_doclist, function(i, tax) {
624 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530625 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530626 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530627 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
628
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530629 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530630 tax_fields.push("tax_amount");
631
632 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530633
Anand Doshi3543f302013-05-24 19:25:01 +0530634 me.validate_on_previous_row(tax);
635 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530636 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530637 });
638 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530639
Anand Doshi3543f302013-05-24 19:25:01 +0530640 calculate_taxes: function() {
641 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530642 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530643
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530644 // maintain actual tax rate based on idx
645 $.each(this.frm.tax_doclist, function(i, tax) {
646 if (tax.charge_type == "Actual") {
647 actual_tax_dict[tax.idx] = flt(tax.rate);
648 }
649 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530650
Anand Doshi3543f302013-05-24 19:25:01 +0530651 $.each(this.frm.item_doclist, function(n, item) {
652 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530653
Anand Doshi3543f302013-05-24 19:25:01 +0530654 $.each(me.frm.tax_doclist, function(i, tax) {
655 // tax_amount represents the amount of tax for the current step
656 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530657
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530658 // Adjust divisional loss to the last item
659 if (tax.charge_type == "Actual") {
660 actual_tax_dict[tax.idx] -= current_tax_amount;
661 if (n == me.frm.item_doclist.length - 1) {
662 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530663 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530664 }
665
Anand Doshi3543f302013-05-24 19:25:01 +0530666 // store tax_amount for current item as it will be used for
667 // charge type = 'On Previous Row Amount'
668 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530669
Anand Doshi3543f302013-05-24 19:25:01 +0530670 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530671 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530672 tax.tax_amount += current_tax_amount;
673
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530674 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530675
Anand Doshi3543f302013-05-24 19:25:01 +0530676 // for buying
677 if(tax.category) {
678 // if just for valuation, do not add the tax amount in total
679 // hence, setting it as 0 for further steps
680 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530681
Anand Doshi3543f302013-05-24 19:25:01 +0530682 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
683 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530684
Anand Doshi3543f302013-05-24 19:25:01 +0530685 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530686 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530687 // item's amount, previously applied tax and the current tax on that item
688 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530689 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530690 precision("total", tax));
691 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530692 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530693 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
694 precision("total", tax));
695 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530696
Anand Doshi3543f302013-05-24 19:25:01 +0530697 // in tax.total, accumulate grand total for each item
698 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530699
700 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530701 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530702 me.round_off_totals(tax);
703
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530704 // adjust Discount Amount loss in last tax iteration
705 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
706 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530707 }
Anand Doshi3543f302013-05-24 19:25:01 +0530708 });
709 });
710 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530711
712 round_off_totals: function(tax) {
713 tax.total = flt(tax.total, precision("total", tax));
714 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530715 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530716 precision("tax_amount", tax));
717 },
718
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530719 adjust_discount_amount_loss: function(tax) {
720 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530721 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530722 discount_amount_loss, precision("tax_amount", tax));
723 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530724 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530725
Anand Doshi3543f302013-05-24 19:25:01 +0530726 get_current_tax_amount: function(item, tax, item_tax_map) {
727 var tax_rate = this._get_tax_rate(tax, item_tax_map);
728 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530729
Anand Doshi3543f302013-05-24 19:25:01 +0530730 if(tax.charge_type == "Actual") {
731 // distribute the tax amount proportionally to each item row
732 var actual = flt(tax.rate, precision("tax_amount", tax));
733 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530734 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530735 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530736
Anand Doshi3543f302013-05-24 19:25:01 +0530737 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530738 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530739
Anand Doshi3543f302013-05-24 19:25:01 +0530740 } else if(tax.charge_type == "On Previous Row Amount") {
741 current_tax_amount = (tax_rate / 100.0) *
742 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530743
Anand Doshi3543f302013-05-24 19:25:01 +0530744 } else if(tax.charge_type == "On Previous Row Total") {
745 current_tax_amount = (tax_rate / 100.0) *
746 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530747 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530748
Anand Doshi53b73422013-05-31 11:50:01 +0530749 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530750
Anand Doshi53b73422013-05-31 11:50:01 +0530751 // store tax breakup for each item
752 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530753
Anand Doshi53b73422013-05-31 11:50:01 +0530754 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530755 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530756
Anand Doshi3543f302013-05-24 19:25:01 +0530757 _cleanup: function() {
758 $.each(this.frm.tax_doclist, function(i, tax) {
759 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530760 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530761 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530762
Anand Doshi3543f302013-05-24 19:25:01 +0530763 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
764 });
765 },
Anand Doshifc777182013-05-27 19:29:07 +0530766
Anand Doshi652bc072014-04-16 15:21:46 +0530767 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530768 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530769 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530770 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530771 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
772 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530773
Anand Doshi652bc072014-04-16 15:21:46 +0530774 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530775 }
776 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530777
Anand Doshi3543f302013-05-24 19:25:01 +0530778 _set_in_company_currency: function(item, print_field, base_field) {
779 // set values in base currency
780 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
781 precision(base_field, item));
782 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530783
Anand Doshibf3e54a2013-06-05 14:11:32 +0530784 get_terms: function() {
785 var me = this;
786 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530787 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530788 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530789 args: {
790 doctype: "Terms and Conditions",
791 fieldname: "terms",
792 filters: { name: this.frm.doc.tc_name },
793 },
794 });
795 }
796 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530797
798 taxes_and_charges: function() {
799 var me = this;
800 if(this.frm.doc.taxes_and_charges) {
801 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530802 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
803 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530804 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
805 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530806 "master_name": this.frm.doc.taxes_and_charges,
807 "tax_parentfield": this.other_fname
808 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530809 callback: function(r) {
810 if(!r.exc) {
Nabin Hait6b039142014-05-02 15:45:10 +0530811 me.frm.set_value(me.other_fname, r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530812 me.calculate_taxes_and_totals();
813 }
814 }
815 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530816 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530817 },
818
819 show_item_wise_taxes: function() {
820 if(this.frm.fields_dict.other_charges_calculation) {
821 $(this.get_item_wise_taxes_html())
822 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
823 }
824 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530825});