blob: 4c0b341d489cbed58f3ca226c29610eaa91befa6 [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 Mehtaab8bde02014-08-12 15:15:39 +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();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +053062 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053063 },
64
Rushabh Mehtae34090a2013-11-15 11:30:23 +053065 make_pos_btn: function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053066 var me = this;
Rushabh Mehta23607f92014-08-08 17:23:40 +053067 if(this.frm.doc.docstatus===0) {
68 if(!this.pos_active) {
69 var btn_label = __("POS View"),
70 icon = "icon-th";
71 } else {
72 var btn_label = __("Form View"),
73 icon = "icon-file-text";
74 }
Rushabh Mehtaff938022014-04-15 18:40:00 +053075
Rushabh Mehta23607f92014-08-08 17:23:40 +053076 if(erpnext.open_as_pos) {
77 me.toggle_pos(true);
78 erpnext.open_as_pos = false;
79 }
Rushabh Mehta72e17192014-08-08 15:30:49 +053080
Rushabh Mehtaab8bde02014-08-12 15:15:39 +053081 this.$pos_btn && this.$pos_btn.remove();
82
Rushabh Mehta23607f92014-08-08 17:23:40 +053083 this.$pos_btn = this.frm.appframe.add_primary_action(btn_label, function() {
84 me.toggle_pos();
85 }, icon, "btn-default");
86 } else {
87 // hack: will avoid calling refresh from refresh
88 setTimeout(function() { me.toggle_pos(false); }, 100);
Rushabh Mehta72e17192014-08-08 15:30:49 +053089 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053090 },
91
92 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053093 // Check whether it is Selling or Buying cycle
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053094 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053095 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
Rushabh Mehtaff938022014-04-15 18:40:00 +053096
Rushabh Mehta23607f92014-08-08 17:23:40 +053097 if((show===true && this.pos_active) || (show===false && !this.pos_active))
98 return;
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053099
Rushabh Mehta23607f92014-08-08 17:23:40 +0530100 if(show && !price_list) {
101 frappe.throw(__("Please select Price List"));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530102 }
Rushabh Mehta23607f92014-08-08 17:23:40 +0530103
104 // make pos
105 if(!this.frm.pos) {
106 this.frm.layout.add_view("pos");
107 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
108 }
109
110 // toggle view
111 this.frm.layout.set_view(this.pos_active ? "" : "pos");
112 this.pos_active = !this.pos_active;
113
114 // refresh
115 if(this.pos_active)
116 this.frm.pos.refresh();
117 this.frm.refresh();
Anand Doshi3543f302013-05-24 19:25:01 +0530118 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530119
120
Nabin Hait139dc7b2014-02-12 14:53:18 +0530121 item_code: function(doc, cdt, cdn) {
122 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530123 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530124 if(item.item_code || item.barcode || item.serial_no) {
125 if(!this.validate_company_and_party()) {
126 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
127 } else {
128 return this.frm.call({
129 method: "erpnext.stock.get_item_details.get_item_details",
130 child: item,
131 args: {
132 args: {
133 item_code: item.item_code,
134 barcode: item.barcode,
135 serial_no: item.serial_no,
136 warehouse: item.warehouse,
Nabin Hait444f9562014-06-20 15:59:49 +0530137 parenttype: me.frm.doc.doctype,
138 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530139 customer: me.frm.doc.customer,
140 supplier: me.frm.doc.supplier,
141 currency: me.frm.doc.currency,
142 conversion_rate: me.frm.doc.conversion_rate,
143 price_list: me.frm.doc.selling_price_list ||
144 me.frm.doc.buying_price_list,
145 price_list_currency: me.frm.doc.price_list_currency,
146 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
147 company: me.frm.doc.company,
148 order_type: me.frm.doc.order_type,
149 is_pos: cint(me.frm.doc.is_pos),
150 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Hait444f9562014-06-20 15:59:49 +0530151 transaction_date: me.frm.doc.transaction_date,
152 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
153 doctype: item.doctype,
Anand Doshi43f087c2014-08-26 14:25:53 +0530154 name: item.name,
155 project_name: item.project_name || me.frm.doc.project_name
Nabin Hait139dc7b2014-02-12 14:53:18 +0530156 }
157 },
158 callback: function(r) {
159 if(!r.exc) {
160 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
161 }
162 }
163 });
164 }
165 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530166 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530167
168 serial_no: function(doc, cdt, cdn) {
169 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530170 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530171
172 if (item.serial_no) {
173 if (!item.item_code) {
174 this.frm.script_manager.trigger("item_code", cdt, cdn);
175 }
176 else {
177 var sr_no = [];
178
179 // Replacing all occurences of comma with carriage return
180 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
181
182 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530183
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530184 // Trim each string and push unique string to new list
185 for (var x=0; x<=serial_nos.length - 1; x++) {
186 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
187 sr_no.push(serial_nos[x].trim());
188 }
189 }
190
191 // Add the new list to the serial no. field in grid with each in new line
192 item.serial_no = "";
193 for (var x=0; x<=sr_no.length - 1; x++)
194 item.serial_no += sr_no[x] + '\n';
195
196 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530197 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530198 }
199 }
200 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530201
Anand Doshi923d41d2013-05-28 17:23:36 +0530202 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530203 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530204 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530205
Anand Doshi3543f302013-05-24 19:25:01 +0530206 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530207 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530208 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530209 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530210 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530211 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530212
Akhilesh Darjee78378272013-10-11 19:06:30 +0530213 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530214 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530215 }
216 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530217 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530218 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530219
Nabin Hait48a0bd32013-07-23 15:31:39 +0530220 this.frm.script_manager.trigger("currency");
Nabin Hait444f9562014-06-20 15:59:49 +0530221 this.apply_pricing_rule();
Anand Doshi3543f302013-05-24 19:25:01 +0530222 }
223 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530224
Anand Doshi3543f302013-05-24 19:25:01 +0530225 get_company_currency: function() {
226 return erpnext.get_currency(this.frm.doc.company);
227 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530228
Anand Doshi3543f302013-05-24 19:25:01 +0530229 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530230 var me = this;
231 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530232
Anand Doshi61a2f682013-06-21 17:55:31 +0530233 var company_currency = this.get_company_currency();
234 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530235 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530236 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530237 me.frm.set_value("conversion_rate", exchange_rate);
238 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530239 });
240 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530241 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530242 }
243 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530244
Anand Doshi61a2f682013-06-21 17:55:31 +0530245 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530246 if(this.frm.doc.currency === this.get_company_currency()) {
247 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530248 }
249 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530250 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
251 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
252 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530253 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530254 if(this.frm.doc.ignore_pricing_rule) {
255 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530256 } else if (!this.in_apply_price_list){
257 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530258 }
259
Nabin Haita3dd72a2014-05-28 12:49:20 +0530260 }
Anand Doshi3543f302013-05-24 19:25:01 +0530261 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530262
Anand Doshi61a2f682013-06-21 17:55:31 +0530263 get_exchange_rate: function(from_currency, to_currency, callback) {
264 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530265 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530266 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530267 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530268 });
269 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530270
Anand Doshi3543f302013-05-24 19:25:01 +0530271 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530272 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530273 this.set_dynamic_labels();
Anand Doshi51f722d2014-07-04 15:44:26 +0530274 this.set_plc_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530275 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530276
Anand Doshi3543f302013-05-24 19:25:01 +0530277 plc_conversion_rate: function() {
Anand Doshi51f722d2014-07-04 15:44:26 +0530278 this.set_plc_conversion_rate();
279 if(!this.in_apply_price_list) {
280 this.apply_price_list();
281 }
282 },
283
284 set_plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530285 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
286 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530287 }
288 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530289 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530290 }
Anand Doshi3543f302013-05-24 19:25:01 +0530291 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530292
Anand Doshi3543f302013-05-24 19:25:01 +0530293 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530294 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530295 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530296
Anand Doshi652bc072014-04-16 15:21:46 +0530297 // tax rate
298 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530299 this.calculate_taxes_and_totals();
300 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530301
Anand Doshi923d41d2013-05-28 17:23:36 +0530302 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530303 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530304 try {
305 this.validate_on_previous_row(tax);
306 this.calculate_taxes_and_totals();
307 } catch(e) {
308 tax.row_id = null;
309 refresh_field("row_id", tax.name, tax.parentfield);
310 throw e;
311 }
312 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530313
Anand Doshi61a2f682013-06-21 17:55:31 +0530314 set_dynamic_labels: function() {
315 // What TODO? should we make price list system non-mandatory?
316 this.frm.toggle_reqd("plc_conversion_rate",
317 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530318
Anand Doshi61a2f682013-06-21 17:55:31 +0530319 var company_currency = this.get_company_currency();
320 this.change_form_labels(company_currency);
321 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530322 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530323 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530324
Anand Doshi923d41d2013-05-28 17:23:36 +0530325 recalculate: function() {
326 this.calculate_taxes_and_totals();
327 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530328
Anand Doshi923d41d2013-05-28 17:23:36 +0530329 recalculate_values: function() {
330 this.calculate_taxes_and_totals();
331 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530332
Anand Doshid0b00722013-05-28 18:54:48 +0530333 calculate_charges: function() {
334 this.calculate_taxes_and_totals();
335 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530336
Nabin Hait444f9562014-06-20 15:59:49 +0530337 ignore_pricing_rule: function() {
338 this.apply_pricing_rule();
339 },
340
341 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530342 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530343 return this.frm.call({
344 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
345 args: { args: this._get_args(item) },
346 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530347 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530348 me._set_values_for_item_list(r.message);
349 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
350 }
351 }
352 });
353 },
354
355 _get_args: function(item) {
356 var me = this;
357 return {
358 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530359 "customer": me.frm.doc.customer,
360 "customer_group": me.frm.doc.customer_group,
361 "territory": me.frm.doc.territory,
362 "supplier": me.frm.doc.supplier,
363 "supplier_type": me.frm.doc.supplier_type,
364 "currency": me.frm.doc.currency,
365 "conversion_rate": me.frm.doc.conversion_rate,
366 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
367 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
368 "company": me.frm.doc.company,
369 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
370 "campaign": me.frm.doc.campaign,
371 "sales_partner": me.frm.doc.sales_partner,
372 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
373 "parenttype": me.frm.doc.doctype,
374 "parent": me.frm.doc.name
375 };
Nabin Hait444f9562014-06-20 15:59:49 +0530376 },
377
378 _get_item_list: function(item) {
379 var item_list = [];
380 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530381 if (d.item_code) {
382 item_list.push({
383 "doctype": d.doctype,
384 "name": d.name,
385 "item_code": d.item_code,
386 "item_group": d.item_group,
387 "brand": d.brand,
388 "qty": d.qty
389 });
390 }
Nabin Hait444f9562014-06-20 15:59:49 +0530391 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530392
Nabin Hait444f9562014-06-20 15:59:49 +0530393 if (item) {
394 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530395 } else {
Nabin Hait444f9562014-06-20 15:59:49 +0530396 $.each(this.get_item_doclist(), function(i, d) {
397 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530398 });
399 }
Nabin Hait444f9562014-06-20 15:59:49 +0530400 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530401 },
402
Anand Doshidffec8f2014-07-01 17:45:15 +0530403 _set_values_for_item_list: function(children) {
404 $.each(children, function(i, d) {
405 $.each(d, function(k, v) {
406 if (["doctype", "name"].indexOf(k)===-1) {
407 frappe.model.set_value(d.doctype, d.name, k, v);
408 }
409 });
410 });
411 },
412
413 apply_price_list: function() {
414 var me = this;
415 return this.frm.call({
416 method: "erpnext.stock.get_item_details.apply_price_list",
417 args: { args: this._get_args() },
418 callback: function(r) {
419 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530420 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530421 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
422 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530423 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +0530424 me._set_values_for_item_list(r.message.children);
425 }
426 }
427 });
428 },
429
Anand Doshi3543f302013-05-24 19:25:01 +0530430 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530431 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530432 try {
433 this.validate_on_previous_row(tax);
434 this.validate_inclusive_tax(tax);
435 this.calculate_taxes_and_totals();
436 } catch(e) {
437 tax.included_in_print_rate = 0;
438 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
439 throw e;
440 }
441 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530442
Anand Doshi3543f302013-05-24 19:25:01 +0530443 validate_on_previous_row: function(tax) {
444 // validate if a valid row id is mentioned in case of
445 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530446 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530447 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530448 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 +0530449 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530450 }
451 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530452
Anand Doshi3543f302013-05-24 19:25:01 +0530453 validate_inclusive_tax: function(tax) {
454 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530455
Anand Doshi3543f302013-05-24 19:25:01 +0530456 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530457 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530458 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530459 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530460
Anand Doshi3543f302013-05-24 19:25:01 +0530461 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530462 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
463 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530464 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530465 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530466
Anand Doshi3543f302013-05-24 19:25:01 +0530467 if(cint(tax.included_in_print_rate)) {
468 if(tax.charge_type == "Actual") {
469 // inclusive tax cannot be of type Actual
470 actual_type_error();
471 } else if(tax.charge_type == "On Previous Row Amount" &&
472 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
473 // referred row should also be an inclusive tax
474 on_previous_row_error(tax.row_id);
475 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530476 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530477 function(t) { return cint(t.included_in_print_rate) ? null : t; });
478 if(taxes_not_included.length > 0) {
479 // all rows above this tax should be inclusive
480 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
481 }
482 }
483 }
484 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530485
Anand Doshi3543f302013-05-24 19:25:01 +0530486 _load_item_tax_rate: function(item_tax_rate) {
487 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
488 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530489
Anand Doshi3543f302013-05-24 19:25:01 +0530490 _get_tax_rate: function(tax, item_tax_map) {
491 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
492 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
493 tax.rate;
494 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530495
Anand Doshi3543f302013-05-24 19:25:01 +0530496 get_item_wise_taxes_html: function() {
497 var item_tax = {};
498 var tax_accounts = [];
499 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530500
Anand Doshi3543f302013-05-24 19:25:01 +0530501 $.each(this.get_tax_doclist(), function(i, tax) {
502 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530503 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530504 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530505 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530506 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530507 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530508 var tax_rate = "";
509 if(tax_data[0] != null) {
510 tax_rate = (tax.charge_type === "Actual") ?
511 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
512 (flt(tax_data[0], tax_rate_precision) + "%");
513 }
514 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
515 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530516
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530517 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530518 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530519 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530520 }
Anand Doshi3543f302013-05-24 19:25:01 +0530521 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530522 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530523 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530524
525 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530526 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530527
Anand Doshi179e3772013-09-05 16:53:57 +0530528 var distinct_item_names = [];
529 var distinct_items = [];
530 $.each(this.get_item_doclist(), function(i, item) {
531 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
532 distinct_item_names.push(item.item_code || item.item_name);
533 distinct_items.push(item);
534 }
535 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530536
Anand Doshi179e3772013-09-05 16:53:57 +0530537 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530538 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530539 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530540 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
541 item_name: item.item_name,
542 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530543 return item_tax_record[head[0]] ?
544 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530545 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530546 }).join("\n")
547 });
548 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530549
Anand Doshi53b73422013-05-31 11:50:01 +0530550 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530551 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
552 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530553 <thead><tr>' + headings + '</tr></thead> \
554 <tbody>' + rows + '</tbody> \
555 </table></div>';
556 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530557
Nabin Hait139dc7b2014-02-12 14:53:18 +0530558 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530559 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530560 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530561
Nabin Hait0f231062014-02-20 11:27:47 +0530562 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530563 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530564 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530565 msgprint(__("Please specify") + ": " +
566 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530567 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530568 valid = false;
569 }
Anand Doshi3543f302013-05-24 19:25:01 +0530570 }
571 });
572 return valid;
573 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530574
Anand Doshi3543f302013-05-24 19:25:01 +0530575 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530576 return this.frm.doc[this.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 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530580 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530581 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530582
Anand Doshi3543f302013-05-24 19:25:01 +0530583 validate_conversion_rate: function() {
584 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530585 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530586 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530587 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530588
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530589 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530590 frappe.throw(repl('%(conversion_rate_label)s' +
591 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
592 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530593 {
594 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530595 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530596 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530597 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530598 }
Anand Doshi3543f302013-05-24 19:25:01 +0530599 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530600
Anand Doshi3543f302013-05-24 19:25:01 +0530601 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530602 this.discount_amount_applied = false;
603 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530604 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530605 this.apply_discount_amount();
606 },
607
608 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530609 this.validate_conversion_rate();
610 this.frm.item_doclist = this.get_item_doclist();
611 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530612
Anand Doshi3543f302013-05-24 19:25:01 +0530613 this.calculate_item_values();
614 this.initialize_taxes();
615 this.determine_exclusive_rate && this.determine_exclusive_rate();
616 this.calculate_net_total();
617 this.calculate_taxes();
618 this.calculate_totals();
619 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530620
Anand Doshi3543f302013-05-24 19:25:01 +0530621 this.show_item_wise_taxes();
622 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530623
Anand Doshi3543f302013-05-24 19:25:01 +0530624 initialize_taxes: function() {
625 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530626
Anand Doshi3543f302013-05-24 19:25:01 +0530627 $.each(this.frm.tax_doclist, function(i, tax) {
628 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530629 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530630 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530631 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
632
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530633 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530634 tax_fields.push("tax_amount");
635
636 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530637
Anand Doshi3543f302013-05-24 19:25:01 +0530638 me.validate_on_previous_row(tax);
639 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530640 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530641 });
642 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530643
Anand Doshi3543f302013-05-24 19:25:01 +0530644 calculate_taxes: function() {
645 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530646 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530647
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530648 // maintain actual tax rate based on idx
649 $.each(this.frm.tax_doclist, function(i, tax) {
650 if (tax.charge_type == "Actual") {
Nabin Hait0a35eff2014-08-19 12:39:40 +0530651 actual_tax_dict[tax.idx] = flt(tax.rate, precision("tax_amount", tax));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530652 }
653 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530654
Anand Doshi3543f302013-05-24 19:25:01 +0530655 $.each(this.frm.item_doclist, function(n, item) {
656 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530657
Anand Doshi3543f302013-05-24 19:25:01 +0530658 $.each(me.frm.tax_doclist, function(i, tax) {
659 // tax_amount represents the amount of tax for the current step
660 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530661
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530662 // Adjust divisional loss to the last item
663 if (tax.charge_type == "Actual") {
664 actual_tax_dict[tax.idx] -= current_tax_amount;
665 if (n == me.frm.item_doclist.length - 1) {
666 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530667 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530668 }
669
Anand Doshi3543f302013-05-24 19:25:01 +0530670 // store tax_amount for current item as it will be used for
671 // charge type = 'On Previous Row Amount'
672 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530673
Anand Doshi3543f302013-05-24 19:25:01 +0530674 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530675 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530676 tax.tax_amount += current_tax_amount;
677
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530678 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530679
Anand Doshi3543f302013-05-24 19:25:01 +0530680 // for buying
681 if(tax.category) {
682 // if just for valuation, do not add the tax amount in total
683 // hence, setting it as 0 for further steps
684 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530685
Anand Doshi3543f302013-05-24 19:25:01 +0530686 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
687 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530688
Anand Doshi3543f302013-05-24 19:25:01 +0530689 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530690 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530691 // item's amount, previously applied tax and the current tax on that item
692 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530693 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530694 precision("total", tax));
695 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530696 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530697 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
698 precision("total", tax));
699 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530700
Anand Doshi3543f302013-05-24 19:25:01 +0530701 // in tax.total, accumulate grand total for each item
702 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530703
704 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530705 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530706 me.round_off_totals(tax);
707
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530708 // adjust Discount Amount loss in last tax iteration
709 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
710 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530711 }
Anand Doshi3543f302013-05-24 19:25:01 +0530712 });
713 });
714 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530715
716 round_off_totals: function(tax) {
717 tax.total = flt(tax.total, precision("total", tax));
718 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530719 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530720 precision("tax_amount", tax));
721 },
722
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530723 adjust_discount_amount_loss: function(tax) {
724 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530725 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530726 discount_amount_loss, precision("tax_amount", tax));
727 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530728 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530729
Anand Doshi3543f302013-05-24 19:25:01 +0530730 get_current_tax_amount: function(item, tax, item_tax_map) {
731 var tax_rate = this._get_tax_rate(tax, item_tax_map);
732 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530733
Anand Doshi3543f302013-05-24 19:25:01 +0530734 if(tax.charge_type == "Actual") {
735 // distribute the tax amount proportionally to each item row
736 var actual = flt(tax.rate, precision("tax_amount", tax));
737 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530738 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530739 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530740
Anand Doshi3543f302013-05-24 19:25:01 +0530741 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530742 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530743
Anand Doshi3543f302013-05-24 19:25:01 +0530744 } else if(tax.charge_type == "On Previous Row Amount") {
745 current_tax_amount = (tax_rate / 100.0) *
746 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530747
Anand Doshi3543f302013-05-24 19:25:01 +0530748 } else if(tax.charge_type == "On Previous Row Total") {
749 current_tax_amount = (tax_rate / 100.0) *
750 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530751 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530752
Anand Doshi53b73422013-05-31 11:50:01 +0530753 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530754
Anand Doshi53b73422013-05-31 11:50:01 +0530755 // store tax breakup for each item
756 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530757
Anand Doshi53b73422013-05-31 11:50:01 +0530758 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530759 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530760
Anand Doshi3543f302013-05-24 19:25:01 +0530761 _cleanup: function() {
762 $.each(this.frm.tax_doclist, function(i, tax) {
763 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530764 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530765 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530766
Anand Doshi3543f302013-05-24 19:25:01 +0530767 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
768 });
769 },
Anand Doshifc777182013-05-27 19:29:07 +0530770
Anand Doshi652bc072014-04-16 15:21:46 +0530771 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530772 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530773 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530774 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530775 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
776 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530777
Anand Doshi652bc072014-04-16 15:21:46 +0530778 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530779 }
780 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530781
Anand Doshi3543f302013-05-24 19:25:01 +0530782 _set_in_company_currency: function(item, print_field, base_field) {
783 // set values in base currency
784 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
785 precision(base_field, item));
786 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530787
Anand Doshibf3e54a2013-06-05 14:11:32 +0530788 get_terms: function() {
789 var me = this;
790 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530791 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530792 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530793 args: {
794 doctype: "Terms and Conditions",
795 fieldname: "terms",
796 filters: { name: this.frm.doc.tc_name },
797 },
798 });
799 }
800 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530801
802 taxes_and_charges: function() {
803 var me = this;
804 if(this.frm.doc.taxes_and_charges) {
805 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530806 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
807 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530808 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
809 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530810 "master_name": this.frm.doc.taxes_and_charges,
811 "tax_parentfield": this.other_fname
812 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530813 callback: function(r) {
814 if(!r.exc) {
Nabin Hait6b039142014-05-02 15:45:10 +0530815 me.frm.set_value(me.other_fname, r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530816 me.calculate_taxes_and_totals();
817 }
818 }
819 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530820 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530821 },
822
823 show_item_wise_taxes: function() {
824 if(this.frm.fields_dict.other_charges_calculation) {
825 $(this.get_item_wise_taxes_html())
826 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
827 }
828 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530829});