blob: ebdb136dd4caf466e80df6dd5aee8bcca776c139 [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 Haite2f054c2015-03-09 14:54:37 +0530151 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
Nabin Hait444f9562014-06-20 15:59:49 +0530152 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 },
Anand Doshi13945092014-09-21 19:45:49 +0530158
Nabin Hait139dc7b2014-02-12 14:53:18 +0530159 callback: function(r) {
160 if(!r.exc) {
161 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
162 }
163 }
164 });
165 }
166 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530167 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530168
169 serial_no: function(doc, cdt, cdn) {
170 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530171 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530172
173 if (item.serial_no) {
174 if (!item.item_code) {
175 this.frm.script_manager.trigger("item_code", cdt, cdn);
176 }
177 else {
178 var sr_no = [];
179
180 // Replacing all occurences of comma with carriage return
181 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
182
183 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530184
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530185 // Trim each string and push unique string to new list
186 for (var x=0; x<=serial_nos.length - 1; x++) {
187 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
188 sr_no.push(serial_nos[x].trim());
189 }
190 }
191
192 // Add the new list to the serial no. field in grid with each in new line
193 item.serial_no = "";
194 for (var x=0; x<=sr_no.length - 1; x++)
195 item.serial_no += sr_no[x] + '\n';
196
197 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530198 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530199 }
200 }
201 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530202
Anand Doshi923d41d2013-05-28 17:23:36 +0530203 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530204 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530205 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530206
Anand Doshi3543f302013-05-24 19:25:01 +0530207 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530208 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530209 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530210 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530211 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530212 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530213
Akhilesh Darjee78378272013-10-11 19:06:30 +0530214 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530215 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530216 }
217 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530218 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530219 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530220
Nabin Hait48a0bd32013-07-23 15:31:39 +0530221 this.frm.script_manager.trigger("currency");
Nabin Hait444f9562014-06-20 15:59:49 +0530222 this.apply_pricing_rule();
Anand Doshi3543f302013-05-24 19:25:01 +0530223 }
224 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530225
Anand Doshi3543f302013-05-24 19:25:01 +0530226 get_company_currency: function() {
227 return erpnext.get_currency(this.frm.doc.company);
228 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530229
Anand Doshi3543f302013-05-24 19:25:01 +0530230 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530231 var me = this;
232 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530233
Anand Doshi61a2f682013-06-21 17:55:31 +0530234 var company_currency = this.get_company_currency();
235 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530236 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530237 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530238 me.frm.set_value("conversion_rate", exchange_rate);
239 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530240 });
241 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530242 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530243 }
244 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530245
Anand Doshi61a2f682013-06-21 17:55:31 +0530246 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530247 if(this.frm.doc.currency === this.get_company_currency()) {
248 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530249 }
250 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530251 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
252 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
253 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530254 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530255 if(this.frm.doc.ignore_pricing_rule) {
256 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530257 } else if (!this.in_apply_price_list){
258 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530259 }
260
Nabin Haita3dd72a2014-05-28 12:49:20 +0530261 }
Anand Doshi3543f302013-05-24 19:25:01 +0530262 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530263
Anand Doshi61a2f682013-06-21 17:55:31 +0530264 get_exchange_rate: function(from_currency, to_currency, callback) {
265 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530266 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530267 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530268 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530269 });
270 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530271
Anand Doshi3543f302013-05-24 19:25:01 +0530272 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530273 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530274 this.set_dynamic_labels();
Anand Doshi51f722d2014-07-04 15:44:26 +0530275 this.set_plc_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530276 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530277
Anand Doshi3543f302013-05-24 19:25:01 +0530278 plc_conversion_rate: function() {
Anand Doshi51f722d2014-07-04 15:44:26 +0530279 this.set_plc_conversion_rate();
280 if(!this.in_apply_price_list) {
281 this.apply_price_list();
282 }
283 },
284
285 set_plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530286 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
287 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530288 }
289 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530290 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530291 }
Anand Doshi3543f302013-05-24 19:25:01 +0530292 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530293
Anand Doshi3543f302013-05-24 19:25:01 +0530294 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530295 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530296 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530297
Anand Doshi652bc072014-04-16 15:21:46 +0530298 // tax rate
299 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530300 this.calculate_taxes_and_totals();
301 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530302
Anand Doshi923d41d2013-05-28 17:23:36 +0530303 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530304 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530305 try {
306 this.validate_on_previous_row(tax);
307 this.calculate_taxes_and_totals();
308 } catch(e) {
309 tax.row_id = null;
310 refresh_field("row_id", tax.name, tax.parentfield);
311 throw e;
312 }
313 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530314
Anand Doshi61a2f682013-06-21 17:55:31 +0530315 set_dynamic_labels: function() {
316 // What TODO? should we make price list system non-mandatory?
317 this.frm.toggle_reqd("plc_conversion_rate",
318 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530319
Anand Doshi61a2f682013-06-21 17:55:31 +0530320 var company_currency = this.get_company_currency();
321 this.change_form_labels(company_currency);
322 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530323 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530324 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530325
Anand Doshi923d41d2013-05-28 17:23:36 +0530326 recalculate: function() {
327 this.calculate_taxes_and_totals();
328 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530329
Anand Doshi923d41d2013-05-28 17:23:36 +0530330 recalculate_values: function() {
331 this.calculate_taxes_and_totals();
332 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530333
Anand Doshid0b00722013-05-28 18:54:48 +0530334 calculate_charges: function() {
335 this.calculate_taxes_and_totals();
336 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530337
Nabin Hait444f9562014-06-20 15:59:49 +0530338 ignore_pricing_rule: function() {
339 this.apply_pricing_rule();
340 },
341
342 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530343 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530344 return this.frm.call({
345 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
346 args: { args: this._get_args(item) },
347 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530348 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530349 me._set_values_for_item_list(r.message);
350 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
351 }
352 }
353 });
354 },
355
356 _get_args: function(item) {
357 var me = this;
358 return {
359 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530360 "customer": me.frm.doc.customer,
361 "customer_group": me.frm.doc.customer_group,
362 "territory": me.frm.doc.territory,
363 "supplier": me.frm.doc.supplier,
364 "supplier_type": me.frm.doc.supplier_type,
365 "currency": me.frm.doc.currency,
366 "conversion_rate": me.frm.doc.conversion_rate,
367 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
368 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
369 "company": me.frm.doc.company,
370 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
371 "campaign": me.frm.doc.campaign,
372 "sales_partner": me.frm.doc.sales_partner,
373 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
374 "parenttype": me.frm.doc.doctype,
375 "parent": me.frm.doc.name
376 };
Nabin Hait444f9562014-06-20 15:59:49 +0530377 },
378
379 _get_item_list: function(item) {
380 var item_list = [];
381 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530382 if (d.item_code) {
383 item_list.push({
384 "doctype": d.doctype,
385 "name": d.name,
386 "item_code": d.item_code,
387 "item_group": d.item_group,
388 "brand": d.brand,
389 "qty": d.qty
390 });
391 }
Nabin Hait444f9562014-06-20 15:59:49 +0530392 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530393
Nabin Hait444f9562014-06-20 15:59:49 +0530394 if (item) {
395 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530396 } else {
Nabin Hait444f9562014-06-20 15:59:49 +0530397 $.each(this.get_item_doclist(), function(i, d) {
398 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530399 });
400 }
Nabin Hait444f9562014-06-20 15:59:49 +0530401 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530402 },
403
Anand Doshidffec8f2014-07-01 17:45:15 +0530404 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530405 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530406 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530407 for(var i=0, l=children.length; i<l; i++) {
408 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530409 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530410
411 for(var k in d) {
412 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530413 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530414 if(k=="price_list_rate") {
415 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
416 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530417 frappe.model.set_value(d.doctype, d.name, k, v);
418 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530419 }
420
Nabin Hait49a27292014-12-17 11:21:32 +0530421 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530422 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530423 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
424 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530425 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530426
Anand Doshidea3fb02015-02-05 15:05:58 +0530427 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530428 },
429
Nabin Hait49a27292014-12-17 11:21:32 +0530430 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530431 var me = this;
432 return this.frm.call({
433 method: "erpnext.stock.get_item_details.apply_price_list",
Nabin Hait49a27292014-12-17 11:21:32 +0530434 args: { args: this._get_args(item) },
Anand Doshidffec8f2014-07-01 17:45:15 +0530435 callback: function(r) {
436 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530437 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530438 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
439 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530440 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +0530441 me._set_values_for_item_list(r.message.children);
442 }
443 }
444 });
445 },
446
Anand Doshi3543f302013-05-24 19:25:01 +0530447 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530448 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530449 try {
450 this.validate_on_previous_row(tax);
451 this.validate_inclusive_tax(tax);
452 this.calculate_taxes_and_totals();
453 } catch(e) {
454 tax.included_in_print_rate = 0;
455 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
456 throw e;
457 }
458 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530459
Anand Doshi3543f302013-05-24 19:25:01 +0530460 validate_on_previous_row: function(tax) {
461 // validate if a valid row id is mentioned in case of
462 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530463 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530464 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530465 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 +0530466 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530467 }
468 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530469
Anand Doshi3543f302013-05-24 19:25:01 +0530470 validate_inclusive_tax: function(tax) {
471 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530472
Anand Doshi3543f302013-05-24 19:25:01 +0530473 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530474 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530475 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530476 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530477
Anand Doshi3543f302013-05-24 19:25:01 +0530478 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530479 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
480 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530481 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530482 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530483
Anand Doshi3543f302013-05-24 19:25:01 +0530484 if(cint(tax.included_in_print_rate)) {
485 if(tax.charge_type == "Actual") {
486 // inclusive tax cannot be of type Actual
487 actual_type_error();
488 } else if(tax.charge_type == "On Previous Row Amount" &&
489 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
490 // referred row should also be an inclusive tax
491 on_previous_row_error(tax.row_id);
492 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530493 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530494 function(t) { return cint(t.included_in_print_rate) ? null : t; });
495 if(taxes_not_included.length > 0) {
496 // all rows above this tax should be inclusive
497 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
498 }
499 }
500 }
501 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530502
Anand Doshi3543f302013-05-24 19:25:01 +0530503 _load_item_tax_rate: function(item_tax_rate) {
504 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
505 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530506
Anand Doshi3543f302013-05-24 19:25:01 +0530507 _get_tax_rate: function(tax, item_tax_map) {
508 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
509 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
510 tax.rate;
511 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530512
Anand Doshi3543f302013-05-24 19:25:01 +0530513 get_item_wise_taxes_html: function() {
514 var item_tax = {};
515 var tax_accounts = [];
516 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530517
Anand Doshi3543f302013-05-24 19:25:01 +0530518 $.each(this.get_tax_doclist(), function(i, tax) {
519 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530520 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530521 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530522 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530523 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530524 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530525 var tax_rate = "";
526 if(tax_data[0] != null) {
527 tax_rate = (tax.charge_type === "Actual") ?
528 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
529 (flt(tax_data[0], tax_rate_precision) + "%");
530 }
531 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
532 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530533
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530534 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530535 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530536 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530537 }
Anand Doshi3543f302013-05-24 19:25:01 +0530538 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530539 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530540 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530541
542 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530543 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530544
Anand Doshi179e3772013-09-05 16:53:57 +0530545 var distinct_item_names = [];
546 var distinct_items = [];
547 $.each(this.get_item_doclist(), function(i, item) {
548 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
549 distinct_item_names.push(item.item_code || item.item_name);
550 distinct_items.push(item);
551 }
552 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530553
Anand Doshi179e3772013-09-05 16:53:57 +0530554 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530555 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530556 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530557 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
558 item_name: item.item_name,
559 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530560 return item_tax_record[head[0]] ?
561 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530562 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530563 }).join("\n")
564 });
565 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530566
Anand Doshi53b73422013-05-31 11:50:01 +0530567 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530568 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
569 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530570 <thead><tr>' + headings + '</tr></thead> \
571 <tbody>' + rows + '</tbody> \
572 </table></div>';
573 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530574
Nabin Hait139dc7b2014-02-12 14:53:18 +0530575 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530576 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530577 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530578
Nabin Hait0f231062014-02-20 11:27:47 +0530579 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530580 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530581 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530582 msgprint(__("Please specify") + ": " +
583 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530584 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530585 valid = false;
586 }
Anand Doshi3543f302013-05-24 19:25:01 +0530587 }
588 });
589 return valid;
590 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530591
Anand Doshi3543f302013-05-24 19:25:01 +0530592 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530593 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530594 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530595
Anand Doshi3543f302013-05-24 19:25:01 +0530596 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530597 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530598 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530599
Anand Doshi3543f302013-05-24 19:25:01 +0530600 validate_conversion_rate: function() {
601 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530602 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530603 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530604 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530605
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530606 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530607 frappe.throw(repl('%(conversion_rate_label)s' +
608 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
609 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530610 {
611 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530612 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530613 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530614 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530615 }
Anand Doshi3543f302013-05-24 19:25:01 +0530616 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530617
Anand Doshi3543f302013-05-24 19:25:01 +0530618 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530619 this.discount_amount_applied = false;
620 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530621 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530622 this.apply_discount_amount();
623 },
624
625 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530626 this.validate_conversion_rate();
627 this.frm.item_doclist = this.get_item_doclist();
628 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530629
Anand Doshi3543f302013-05-24 19:25:01 +0530630 this.calculate_item_values();
631 this.initialize_taxes();
632 this.determine_exclusive_rate && this.determine_exclusive_rate();
633 this.calculate_net_total();
634 this.calculate_taxes();
635 this.calculate_totals();
636 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530637
Anand Doshi3543f302013-05-24 19:25:01 +0530638 this.show_item_wise_taxes();
639 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530640
Anand Doshi3543f302013-05-24 19:25:01 +0530641 initialize_taxes: function() {
642 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530643
Anand Doshi3543f302013-05-24 19:25:01 +0530644 $.each(this.frm.tax_doclist, function(i, tax) {
645 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530646 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530647 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530648 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
649
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530650 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530651 tax_fields.push("tax_amount");
652
653 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530654
Anand Doshi3543f302013-05-24 19:25:01 +0530655 me.validate_on_previous_row(tax);
656 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530657 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530658 });
659 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530660
Anand Doshi3543f302013-05-24 19:25:01 +0530661 calculate_taxes: function() {
662 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530663 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530664
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530665 // maintain actual tax rate based on idx
666 $.each(this.frm.tax_doclist, function(i, tax) {
667 if (tax.charge_type == "Actual") {
Nabin Hait0a35eff2014-08-19 12:39:40 +0530668 actual_tax_dict[tax.idx] = flt(tax.rate, precision("tax_amount", tax));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530669 }
670 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530671
Anand Doshi3543f302013-05-24 19:25:01 +0530672 $.each(this.frm.item_doclist, function(n, item) {
673 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530674
Anand Doshi3543f302013-05-24 19:25:01 +0530675 $.each(me.frm.tax_doclist, function(i, tax) {
676 // tax_amount represents the amount of tax for the current step
677 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530678
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530679 // Adjust divisional loss to the last item
680 if (tax.charge_type == "Actual") {
681 actual_tax_dict[tax.idx] -= current_tax_amount;
682 if (n == me.frm.item_doclist.length - 1) {
683 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530684 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530685 }
686
Anand Doshi3543f302013-05-24 19:25:01 +0530687 // store tax_amount for current item as it will be used for
688 // charge type = 'On Previous Row Amount'
689 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530690
Anand Doshi3543f302013-05-24 19:25:01 +0530691 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530692 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530693 tax.tax_amount += current_tax_amount;
694
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530695 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530696
Anand Doshi3543f302013-05-24 19:25:01 +0530697 // for buying
698 if(tax.category) {
699 // if just for valuation, do not add the tax amount in total
700 // hence, setting it as 0 for further steps
701 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530702
Anand Doshi3543f302013-05-24 19:25:01 +0530703 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
704 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530705
Anand Doshi3543f302013-05-24 19:25:01 +0530706 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530707 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530708 // item's amount, previously applied tax and the current tax on that item
709 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530710 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530711 precision("total", tax));
712 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530713 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530714 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
715 precision("total", tax));
716 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530717
Anand Doshi3543f302013-05-24 19:25:01 +0530718 // in tax.total, accumulate grand total for each item
719 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530720
721 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530722 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530723 me.round_off_totals(tax);
724
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530725 // adjust Discount Amount loss in last tax iteration
726 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
727 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530728 }
Anand Doshi3543f302013-05-24 19:25:01 +0530729 });
730 });
731 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530732
733 round_off_totals: function(tax) {
734 tax.total = flt(tax.total, precision("total", tax));
735 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530736 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530737 precision("tax_amount", tax));
738 },
739
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530740 adjust_discount_amount_loss: function(tax) {
Nabin Hait2244ac42015-01-12 17:35:14 +0530741 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.base_discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530742 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530743 discount_amount_loss, precision("tax_amount", tax));
744 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530745 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530746
Anand Doshi3543f302013-05-24 19:25:01 +0530747 get_current_tax_amount: function(item, tax, item_tax_map) {
748 var tax_rate = this._get_tax_rate(tax, item_tax_map);
749 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530750
Anand Doshi3543f302013-05-24 19:25:01 +0530751 if(tax.charge_type == "Actual") {
752 // distribute the tax amount proportionally to each item row
753 var actual = flt(tax.rate, precision("tax_amount", tax));
754 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait2244ac42015-01-12 17:35:14 +0530755 ((item.base_amount / this.frm.doc.net_total) * actual) : 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530756
Anand Doshi3543f302013-05-24 19:25:01 +0530757 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530758 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530759
Anand Doshi3543f302013-05-24 19:25:01 +0530760 } else if(tax.charge_type == "On Previous Row Amount") {
761 current_tax_amount = (tax_rate / 100.0) *
762 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530763
Anand Doshi3543f302013-05-24 19:25:01 +0530764 } else if(tax.charge_type == "On Previous Row Total") {
765 current_tax_amount = (tax_rate / 100.0) *
766 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530767 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530768
Anand Doshi53b73422013-05-31 11:50:01 +0530769 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530770
Anand Doshi53b73422013-05-31 11:50:01 +0530771 // store tax breakup for each item
772 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530773
Anand Doshi53b73422013-05-31 11:50:01 +0530774 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530775 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530776
Anand Doshi3543f302013-05-24 19:25:01 +0530777 _cleanup: function() {
778 $.each(this.frm.tax_doclist, function(i, tax) {
779 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530780 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530781 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530782
Anand Doshi3543f302013-05-24 19:25:01 +0530783 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
784 });
785 },
Anand Doshifc777182013-05-27 19:29:07 +0530786
Anand Doshi652bc072014-04-16 15:21:46 +0530787 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530788 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530789 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530790 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530791 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
792 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530793
Anand Doshi652bc072014-04-16 15:21:46 +0530794 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530795 }
796 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530797
Anand Doshi3543f302013-05-24 19:25:01 +0530798 _set_in_company_currency: function(item, print_field, base_field) {
799 // set values in base currency
800 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
801 precision(base_field, item));
802 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530803
Anand Doshibf3e54a2013-06-05 14:11:32 +0530804 get_terms: function() {
805 var me = this;
806 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530807 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530808 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530809 args: {
810 doctype: "Terms and Conditions",
811 fieldname: "terms",
812 filters: { name: this.frm.doc.tc_name },
813 },
814 });
815 }
816 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530817
818 taxes_and_charges: function() {
819 var me = this;
820 if(this.frm.doc.taxes_and_charges) {
821 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530822 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
823 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530824 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
825 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530826 "master_name": this.frm.doc.taxes_and_charges,
827 "tax_parentfield": this.other_fname
828 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530829 callback: function(r) {
830 if(!r.exc) {
Nabin Hait6b039142014-05-02 15:45:10 +0530831 me.frm.set_value(me.other_fname, r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530832 me.calculate_taxes_and_totals();
833 }
834 }
835 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530836 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530837 },
838
839 show_item_wise_taxes: function() {
840 if(this.frm.fields_dict.other_charges_calculation) {
841 $(this.get_item_wise_taxes_html())
842 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
843 }
844 },
Anand Doshi13945092014-09-21 19:45:49 +0530845
846 is_recurring: function() {
847 // set default values for recurring documents
848 if(this.frm.doc.is_recurring) {
849 var owner_email = this.frm.doc.owner=="Administrator"
850 ? frappe.user_info("Administrator").email
851 : this.frm.doc.owner;
852
853 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
854 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
855 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
856 }
857
858 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
859 },
860
861 from_date: function() {
862 // set to_date
863 if(this.frm.doc.from_date) {
864 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
865 'Yearly': 12};
866
867 var months = recurring_type_map[this.frm.doc.recurring_type];
868 if(months) {
869 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
870 months);
871 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
872 refresh_field('to_date');
873 }
874 }
875 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530876});