blob: 8fce8e89a7ea392c615aaaa02f0c5ebb057a59c6 [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");
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +05306frappe.require("assets/erpnext/js/utils.js");
7
Anand Doshi3543f302013-05-24 19:25:01 +05308
Anand Doshia648f462013-07-30 14:42:15 +05309erpnext.TransactionController = erpnext.stock.StockController.extend({
Anand Doshi3543f302013-05-24 19:25:01 +053010 onload: function() {
Anand Doshi1e4fb682013-08-23 12:56:33 +053011 var me = this;
Anand Doshifaefeaa2014-06-24 18:53:04 +053012 this._super();
13
Anand Doshi3543f302013-05-24 19:25:01 +053014 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053015 var today = get_today(),
Nabin Hait73083dc2014-03-14 15:10:42 +053016 currency = frappe.defaults.get_user_default("currency");
Rushabh Mehtaff938022014-04-15 18:40:00 +053017
Anand Doshi3543f302013-05-24 19:25:01 +053018 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053019 posting_date: today,
20 due_date: today,
21 transaction_date: today,
22 currency: currency,
23 price_list_currency: currency,
24 status: "Draft",
Anand Doshifc777182013-05-27 19:29:07 +053025 is_subcontracted: "No",
Anand Doshi3543f302013-05-24 19:25:01 +053026 }, function(fieldname, value) {
27 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
28 me.frm.set_value(fieldname, value);
Rushabh Mehtaff938022014-04-15 18:40:00 +053029 });
nabinhaitbc408d82014-07-07 16:51:52 +053030
nabinhaitb7392642014-07-28 14:55:08 +053031 if(this.frm.doc.company && !this.frm.doc.amended_from) {
nabinhaitbc408d82014-07-07 16:51:52 +053032 cur_frm.script_manager.trigger("company");
33 }
Anand Doshi3543f302013-05-24 19:25:01 +053034 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +053035
Anand Doshi1e4fb682013-08-23 12:56:33 +053036 if(this.other_fname) {
Anand Doshia91c95f2013-08-23 13:00:47 +053037 this[this.other_fname + "_remove"] = this.calculate_taxes_and_totals;
38 }
Rushabh Mehtaff938022014-04-15 18:40:00 +053039
Anand Doshia91c95f2013-08-23 13:00:47 +053040 if(this.fname) {
41 this[this.fname + "_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +053042 }
Anand Doshi3543f302013-05-24 19:25:01 +053043 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053044
Anand Doshi5e4e5d72013-08-06 17:23:44 +053045 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053046 var me = this;
Anand Doshi221dec82014-06-03 14:12:49 +053047 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 +053048 this.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +053049 }
Rushabh Mehtab6843f22014-06-04 13:10:41 +053050 if(frappe.meta.get_docfield(this.tname, "item_code")) {
Rushabh Mehta880b3ef2014-06-06 11:45:11 +053051 cur_frm.get_field(this.fname).grid.set_multiple_add("item_code", "qty");
Rushabh Mehtab6843f22014-06-04 13:10:41 +053052 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053053 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053054
Anand Doshi3543f302013-05-24 19:25:01 +053055 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +053056 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053057 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053058 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053059 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053060
61 // Show POS button only if it is enabled from features setup
Rushabh Mehtaab8bde02014-08-12 15:15:39 +053062 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request") {
Rushabh Mehtae34090a2013-11-15 11:30:23 +053063 this.make_pos_btn();
Rushabh Mehtaab8bde02014-08-12 15:15:39 +053064 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053065 },
66
Rushabh Mehtae34090a2013-11-15 11:30:23 +053067 make_pos_btn: function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053068 var me = this;
Rushabh Mehta23607f92014-08-08 17:23:40 +053069 if(this.frm.doc.docstatus===0) {
70 if(!this.pos_active) {
71 var btn_label = __("POS View"),
72 icon = "icon-th";
73 } else {
74 var btn_label = __("Form View"),
75 icon = "icon-file-text";
76 }
Rushabh Mehtaff938022014-04-15 18:40:00 +053077
Rushabh Mehta23607f92014-08-08 17:23:40 +053078 if(erpnext.open_as_pos) {
79 me.toggle_pos(true);
80 erpnext.open_as_pos = false;
81 }
Rushabh Mehta72e17192014-08-08 15:30:49 +053082
Rushabh Mehtaab8bde02014-08-12 15:15:39 +053083 this.$pos_btn && this.$pos_btn.remove();
84
Rushabh Mehta23607f92014-08-08 17:23:40 +053085 this.$pos_btn = this.frm.appframe.add_primary_action(btn_label, function() {
86 me.toggle_pos();
87 }, icon, "btn-default");
88 } else {
89 // hack: will avoid calling refresh from refresh
90 setTimeout(function() { me.toggle_pos(false); }, 100);
Rushabh Mehta72e17192014-08-08 15:30:49 +053091 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053092 },
93
94 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053095 // Check whether it is Selling or Buying cycle
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053096 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053097 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
Rushabh Mehtaff938022014-04-15 18:40:00 +053098
Rushabh Mehta23607f92014-08-08 17:23:40 +053099 if((show===true && this.pos_active) || (show===false && !this.pos_active))
100 return;
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530101
Rushabh Mehta23607f92014-08-08 17:23:40 +0530102 if(show && !price_list) {
103 frappe.throw(__("Please select Price List"));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530104 }
Rushabh Mehta23607f92014-08-08 17:23:40 +0530105
106 // make pos
107 if(!this.frm.pos) {
108 this.frm.layout.add_view("pos");
109 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
110 }
111
112 // toggle view
113 this.frm.layout.set_view(this.pos_active ? "" : "pos");
114 this.pos_active = !this.pos_active;
115
116 // refresh
117 if(this.pos_active)
118 this.frm.pos.refresh();
119 this.frm.refresh();
Anand Doshi3543f302013-05-24 19:25:01 +0530120 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530121
122
Nabin Hait139dc7b2014-02-12 14:53:18 +0530123 item_code: function(doc, cdt, cdn) {
124 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530125 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530126 if(item.item_code || item.barcode || item.serial_no) {
127 if(!this.validate_company_and_party()) {
128 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
129 } else {
130 return this.frm.call({
131 method: "erpnext.stock.get_item_details.get_item_details",
132 child: item,
133 args: {
134 args: {
135 item_code: item.item_code,
136 barcode: item.barcode,
137 serial_no: item.serial_no,
138 warehouse: item.warehouse,
Nabin Hait444f9562014-06-20 15:59:49 +0530139 parenttype: me.frm.doc.doctype,
140 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530141 customer: me.frm.doc.customer,
142 supplier: me.frm.doc.supplier,
143 currency: me.frm.doc.currency,
144 conversion_rate: me.frm.doc.conversion_rate,
145 price_list: me.frm.doc.selling_price_list ||
146 me.frm.doc.buying_price_list,
147 price_list_currency: me.frm.doc.price_list_currency,
148 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
149 company: me.frm.doc.company,
150 order_type: me.frm.doc.order_type,
151 is_pos: cint(me.frm.doc.is_pos),
152 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Hait444f9562014-06-20 15:59:49 +0530153 transaction_date: me.frm.doc.transaction_date,
154 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
155 doctype: item.doctype,
Anand Doshi43f087c2014-08-26 14:25:53 +0530156 name: item.name,
157 project_name: item.project_name || me.frm.doc.project_name
Nabin Hait139dc7b2014-02-12 14:53:18 +0530158 }
159 },
Anand Doshi13945092014-09-21 19:45:49 +0530160
Nabin Hait139dc7b2014-02-12 14:53:18 +0530161 callback: function(r) {
162 if(!r.exc) {
163 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
164 }
165 }
166 });
167 }
168 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530169 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530170
171 serial_no: function(doc, cdt, cdn) {
172 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530173 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530174
175 if (item.serial_no) {
176 if (!item.item_code) {
177 this.frm.script_manager.trigger("item_code", cdt, cdn);
178 }
179 else {
180 var sr_no = [];
181
182 // Replacing all occurences of comma with carriage return
183 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
184
185 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530186
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530187 // Trim each string and push unique string to new list
188 for (var x=0; x<=serial_nos.length - 1; x++) {
189 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
190 sr_no.push(serial_nos[x].trim());
191 }
192 }
193
194 // Add the new list to the serial no. field in grid with each in new line
195 item.serial_no = "";
196 for (var x=0; x<=sr_no.length - 1; x++)
197 item.serial_no += sr_no[x] + '\n';
198
199 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530200 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530201 }
202 }
203 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530204
Anand Doshi923d41d2013-05-28 17:23:36 +0530205 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530206 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530207 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530208
Anand Doshi3543f302013-05-24 19:25:01 +0530209 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530210 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530211 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530212 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530213 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530214 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530215
Akhilesh Darjee78378272013-10-11 19:06:30 +0530216 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530217 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530218 }
219 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530220 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530221 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530222
Nabin Hait48a0bd32013-07-23 15:31:39 +0530223 this.frm.script_manager.trigger("currency");
Nabin Hait444f9562014-06-20 15:59:49 +0530224 this.apply_pricing_rule();
Anand Doshi3543f302013-05-24 19:25:01 +0530225 }
Neil Trini Lasrado565d9862014-10-07 18:26:39 +0530226 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.posting_date);
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530227 },
228
229 posting_date: function() {
Neil Trini Lasrado565d9862014-10-07 18:26:39 +0530230 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.posting_date);
Anand Doshi3543f302013-05-24 19:25:01 +0530231 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530232
Anand Doshi3543f302013-05-24 19:25:01 +0530233 get_company_currency: function() {
234 return erpnext.get_currency(this.frm.doc.company);
235 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530236
Anand Doshi3543f302013-05-24 19:25:01 +0530237 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530238 var me = this;
239 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530240
Anand Doshi61a2f682013-06-21 17:55:31 +0530241 var company_currency = this.get_company_currency();
242 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530243 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530244 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530245 me.frm.set_value("conversion_rate", exchange_rate);
246 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530247 });
248 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530249 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530250 }
251 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530252
Anand Doshi61a2f682013-06-21 17:55:31 +0530253 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530254 if(this.frm.doc.currency === this.get_company_currency()) {
255 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530256 }
257 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530258 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
259 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
260 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530261 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530262 if(this.frm.doc.ignore_pricing_rule) {
263 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530264 } else if (!this.in_apply_price_list){
265 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530266 }
267
Nabin Haita3dd72a2014-05-28 12:49:20 +0530268 }
Anand Doshi3543f302013-05-24 19:25:01 +0530269 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530270
Anand Doshi61a2f682013-06-21 17:55:31 +0530271 get_exchange_rate: function(from_currency, to_currency, callback) {
272 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530273 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530274 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530275 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530276 });
277 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530278
Anand Doshi3543f302013-05-24 19:25:01 +0530279 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530280 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530281 this.set_dynamic_labels();
Anand Doshi51f722d2014-07-04 15:44:26 +0530282 this.set_plc_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530283 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530284
Anand Doshi3543f302013-05-24 19:25:01 +0530285 plc_conversion_rate: function() {
Anand Doshi51f722d2014-07-04 15:44:26 +0530286 this.set_plc_conversion_rate();
287 if(!this.in_apply_price_list) {
288 this.apply_price_list();
289 }
290 },
291
292 set_plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530293 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
294 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530295 }
296 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530297 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530298 }
Anand Doshi3543f302013-05-24 19:25:01 +0530299 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530300
Anand Doshi3543f302013-05-24 19:25:01 +0530301 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530302 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530303 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530304
Anand Doshi652bc072014-04-16 15:21:46 +0530305 // tax rate
306 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530307 this.calculate_taxes_and_totals();
308 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530309
Anand Doshi923d41d2013-05-28 17:23:36 +0530310 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530311 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530312 try {
313 this.validate_on_previous_row(tax);
314 this.calculate_taxes_and_totals();
315 } catch(e) {
316 tax.row_id = null;
317 refresh_field("row_id", tax.name, tax.parentfield);
318 throw e;
319 }
320 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530321
Anand Doshi61a2f682013-06-21 17:55:31 +0530322 set_dynamic_labels: function() {
323 // What TODO? should we make price list system non-mandatory?
324 this.frm.toggle_reqd("plc_conversion_rate",
325 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530326
Anand Doshi61a2f682013-06-21 17:55:31 +0530327 var company_currency = this.get_company_currency();
328 this.change_form_labels(company_currency);
329 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530330 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530331 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530332
Anand Doshi923d41d2013-05-28 17:23:36 +0530333 recalculate: function() {
334 this.calculate_taxes_and_totals();
335 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530336
Anand Doshi923d41d2013-05-28 17:23:36 +0530337 recalculate_values: function() {
338 this.calculate_taxes_and_totals();
339 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530340
Anand Doshid0b00722013-05-28 18:54:48 +0530341 calculate_charges: function() {
342 this.calculate_taxes_and_totals();
343 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530344
Nabin Hait444f9562014-06-20 15:59:49 +0530345 ignore_pricing_rule: function() {
346 this.apply_pricing_rule();
347 },
348
349 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530350 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530351 return this.frm.call({
352 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
353 args: { args: this._get_args(item) },
354 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530355 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530356 me._set_values_for_item_list(r.message);
357 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
358 }
359 }
360 });
361 },
362
363 _get_args: function(item) {
364 var me = this;
365 return {
366 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530367 "customer": me.frm.doc.customer,
368 "customer_group": me.frm.doc.customer_group,
369 "territory": me.frm.doc.territory,
370 "supplier": me.frm.doc.supplier,
371 "supplier_type": me.frm.doc.supplier_type,
372 "currency": me.frm.doc.currency,
373 "conversion_rate": me.frm.doc.conversion_rate,
374 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
375 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
376 "company": me.frm.doc.company,
377 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
378 "campaign": me.frm.doc.campaign,
379 "sales_partner": me.frm.doc.sales_partner,
380 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
381 "parenttype": me.frm.doc.doctype,
382 "parent": me.frm.doc.name
383 };
Nabin Hait444f9562014-06-20 15:59:49 +0530384 },
385
386 _get_item_list: function(item) {
387 var item_list = [];
388 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530389 if (d.item_code) {
390 item_list.push({
391 "doctype": d.doctype,
392 "name": d.name,
393 "item_code": d.item_code,
394 "item_group": d.item_group,
395 "brand": d.brand,
396 "qty": d.qty
397 });
398 }
Nabin Hait444f9562014-06-20 15:59:49 +0530399 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530400
Nabin Hait444f9562014-06-20 15:59:49 +0530401 if (item) {
402 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530403 } else {
Nabin Hait444f9562014-06-20 15:59:49 +0530404 $.each(this.get_item_doclist(), function(i, d) {
405 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530406 });
407 }
Nabin Hait444f9562014-06-20 15:59:49 +0530408 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530409 },
410
Anand Doshidffec8f2014-07-01 17:45:15 +0530411 _set_values_for_item_list: function(children) {
412 $.each(children, function(i, d) {
413 $.each(d, function(k, v) {
414 if (["doctype", "name"].indexOf(k)===-1) {
415 frappe.model.set_value(d.doctype, d.name, k, v);
416 }
417 });
418 });
419 },
420
421 apply_price_list: function() {
422 var me = this;
423 return this.frm.call({
424 method: "erpnext.stock.get_item_details.apply_price_list",
425 args: { args: this._get_args() },
426 callback: function(r) {
427 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530428 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530429 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
430 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530431 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +0530432 me._set_values_for_item_list(r.message.children);
433 }
434 }
435 });
436 },
437
Anand Doshi3543f302013-05-24 19:25:01 +0530438 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530439 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530440 try {
441 this.validate_on_previous_row(tax);
442 this.validate_inclusive_tax(tax);
443 this.calculate_taxes_and_totals();
444 } catch(e) {
445 tax.included_in_print_rate = 0;
446 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
447 throw e;
448 }
449 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530450
Anand Doshi3543f302013-05-24 19:25:01 +0530451 validate_on_previous_row: function(tax) {
452 // validate if a valid row id is mentioned in case of
453 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530454 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530455 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530456 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 +0530457 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530458 }
459 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530460
Anand Doshi3543f302013-05-24 19:25:01 +0530461 validate_inclusive_tax: function(tax) {
462 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530463
Anand Doshi3543f302013-05-24 19:25:01 +0530464 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530465 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530466 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530467 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530468
Anand Doshi3543f302013-05-24 19:25:01 +0530469 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530470 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
471 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530472 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530473 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530474
Anand Doshi3543f302013-05-24 19:25:01 +0530475 if(cint(tax.included_in_print_rate)) {
476 if(tax.charge_type == "Actual") {
477 // inclusive tax cannot be of type Actual
478 actual_type_error();
479 } else if(tax.charge_type == "On Previous Row Amount" &&
480 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
481 // referred row should also be an inclusive tax
482 on_previous_row_error(tax.row_id);
483 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530484 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530485 function(t) { return cint(t.included_in_print_rate) ? null : t; });
486 if(taxes_not_included.length > 0) {
487 // all rows above this tax should be inclusive
488 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
489 }
490 }
491 }
492 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530493
Anand Doshi3543f302013-05-24 19:25:01 +0530494 _load_item_tax_rate: function(item_tax_rate) {
495 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
496 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530497
Anand Doshi3543f302013-05-24 19:25:01 +0530498 _get_tax_rate: function(tax, item_tax_map) {
499 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
500 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
501 tax.rate;
502 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530503
Anand Doshi3543f302013-05-24 19:25:01 +0530504 get_item_wise_taxes_html: function() {
505 var item_tax = {};
506 var tax_accounts = [];
507 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530508
Anand Doshi3543f302013-05-24 19:25:01 +0530509 $.each(this.get_tax_doclist(), function(i, tax) {
510 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530511 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530512 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530513 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530514 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530515 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530516 var tax_rate = "";
517 if(tax_data[0] != null) {
518 tax_rate = (tax.charge_type === "Actual") ?
519 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
520 (flt(tax_data[0], tax_rate_precision) + "%");
521 }
522 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
523 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530524
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530525 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530526 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530527 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530528 }
Anand Doshi3543f302013-05-24 19:25:01 +0530529 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530530 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530531 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530532
533 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530534 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530535
Anand Doshi179e3772013-09-05 16:53:57 +0530536 var distinct_item_names = [];
537 var distinct_items = [];
538 $.each(this.get_item_doclist(), function(i, item) {
539 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
540 distinct_item_names.push(item.item_code || item.item_name);
541 distinct_items.push(item);
542 }
543 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530544
Anand Doshi179e3772013-09-05 16:53:57 +0530545 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530546 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530547 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530548 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
549 item_name: item.item_name,
550 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530551 return item_tax_record[head[0]] ?
552 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530553 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530554 }).join("\n")
555 });
556 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530557
Anand Doshi53b73422013-05-31 11:50:01 +0530558 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530559 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
560 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530561 <thead><tr>' + headings + '</tr></thead> \
562 <tbody>' + rows + '</tbody> \
563 </table></div>';
564 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530565
Nabin Hait139dc7b2014-02-12 14:53:18 +0530566 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530567 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530568 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530569
Nabin Hait0f231062014-02-20 11:27:47 +0530570 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530571 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530572 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530573 msgprint(__("Please specify") + ": " +
574 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530575 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530576 valid = false;
577 }
Anand Doshi3543f302013-05-24 19:25:01 +0530578 }
579 });
580 return valid;
581 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530582
Anand Doshi3543f302013-05-24 19:25:01 +0530583 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530584 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530585 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530586
Anand Doshi3543f302013-05-24 19:25:01 +0530587 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530588 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530589 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530590
Anand Doshi3543f302013-05-24 19:25:01 +0530591 validate_conversion_rate: function() {
592 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530593 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530594 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530595 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530596
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530597 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530598 frappe.throw(repl('%(conversion_rate_label)s' +
599 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
600 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530601 {
602 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530603 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530604 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530605 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530606 }
Anand Doshi3543f302013-05-24 19:25:01 +0530607 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530608
Anand Doshi3543f302013-05-24 19:25:01 +0530609 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530610 this.discount_amount_applied = false;
611 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530612 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530613 this.apply_discount_amount();
614 },
615
616 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530617 this.validate_conversion_rate();
618 this.frm.item_doclist = this.get_item_doclist();
619 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530620
Anand Doshi3543f302013-05-24 19:25:01 +0530621 this.calculate_item_values();
622 this.initialize_taxes();
623 this.determine_exclusive_rate && this.determine_exclusive_rate();
624 this.calculate_net_total();
625 this.calculate_taxes();
626 this.calculate_totals();
627 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530628
Anand Doshi3543f302013-05-24 19:25:01 +0530629 this.show_item_wise_taxes();
630 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530631
Anand Doshi3543f302013-05-24 19:25:01 +0530632 initialize_taxes: function() {
633 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530634
Anand Doshi3543f302013-05-24 19:25:01 +0530635 $.each(this.frm.tax_doclist, function(i, tax) {
636 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530637 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530638 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530639 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
640
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530641 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530642 tax_fields.push("tax_amount");
643
644 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530645
Anand Doshi3543f302013-05-24 19:25:01 +0530646 me.validate_on_previous_row(tax);
647 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530648 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530649 });
650 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530651
Anand Doshi3543f302013-05-24 19:25:01 +0530652 calculate_taxes: function() {
653 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530654 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530655
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530656 // maintain actual tax rate based on idx
657 $.each(this.frm.tax_doclist, function(i, tax) {
658 if (tax.charge_type == "Actual") {
Nabin Hait0a35eff2014-08-19 12:39:40 +0530659 actual_tax_dict[tax.idx] = flt(tax.rate, precision("tax_amount", tax));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530660 }
661 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530662
Anand Doshi3543f302013-05-24 19:25:01 +0530663 $.each(this.frm.item_doclist, function(n, item) {
664 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530665
Anand Doshi3543f302013-05-24 19:25:01 +0530666 $.each(me.frm.tax_doclist, function(i, tax) {
667 // tax_amount represents the amount of tax for the current step
668 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530669
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530670 // Adjust divisional loss to the last item
671 if (tax.charge_type == "Actual") {
672 actual_tax_dict[tax.idx] -= current_tax_amount;
673 if (n == me.frm.item_doclist.length - 1) {
674 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530675 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530676 }
677
Anand Doshi3543f302013-05-24 19:25:01 +0530678 // store tax_amount for current item as it will be used for
679 // charge type = 'On Previous Row Amount'
680 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530681
Anand Doshi3543f302013-05-24 19:25:01 +0530682 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530683 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530684 tax.tax_amount += current_tax_amount;
685
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530686 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530687
Anand Doshi3543f302013-05-24 19:25:01 +0530688 // for buying
689 if(tax.category) {
690 // if just for valuation, do not add the tax amount in total
691 // hence, setting it as 0 for further steps
692 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530693
Anand Doshi3543f302013-05-24 19:25:01 +0530694 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
695 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530696
Anand Doshi3543f302013-05-24 19:25:01 +0530697 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530698 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530699 // item's amount, previously applied tax and the current tax on that item
700 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530701 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530702 precision("total", tax));
703 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530704 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530705 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
706 precision("total", tax));
707 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530708
Anand Doshi3543f302013-05-24 19:25:01 +0530709 // in tax.total, accumulate grand total for each item
710 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530711
712 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530713 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530714 me.round_off_totals(tax);
715
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530716 // adjust Discount Amount loss in last tax iteration
717 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
718 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530719 }
Anand Doshi3543f302013-05-24 19:25:01 +0530720 });
721 });
722 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530723
724 round_off_totals: function(tax) {
725 tax.total = flt(tax.total, precision("total", tax));
726 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530727 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530728 precision("tax_amount", tax));
729 },
730
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530731 adjust_discount_amount_loss: function(tax) {
732 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530733 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530734 discount_amount_loss, precision("tax_amount", tax));
735 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530736 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530737
Anand Doshi3543f302013-05-24 19:25:01 +0530738 get_current_tax_amount: function(item, tax, item_tax_map) {
739 var tax_rate = this._get_tax_rate(tax, item_tax_map);
740 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530741
Anand Doshi3543f302013-05-24 19:25:01 +0530742 if(tax.charge_type == "Actual") {
743 // distribute the tax amount proportionally to each item row
744 var actual = flt(tax.rate, precision("tax_amount", tax));
745 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530746 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530747 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530748
Anand Doshi3543f302013-05-24 19:25:01 +0530749 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530750 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530751
Anand Doshi3543f302013-05-24 19:25:01 +0530752 } else if(tax.charge_type == "On Previous Row Amount") {
753 current_tax_amount = (tax_rate / 100.0) *
754 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530755
Anand Doshi3543f302013-05-24 19:25:01 +0530756 } else if(tax.charge_type == "On Previous Row Total") {
757 current_tax_amount = (tax_rate / 100.0) *
758 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530759 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530760
Anand Doshi53b73422013-05-31 11:50:01 +0530761 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530762
Anand Doshi53b73422013-05-31 11:50:01 +0530763 // store tax breakup for each item
764 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530765
Anand Doshi53b73422013-05-31 11:50:01 +0530766 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530767 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530768
Anand Doshi3543f302013-05-24 19:25:01 +0530769 _cleanup: function() {
770 $.each(this.frm.tax_doclist, function(i, tax) {
771 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530772 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530773 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530774
Anand Doshi3543f302013-05-24 19:25:01 +0530775 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
776 });
777 },
Anand Doshifc777182013-05-27 19:29:07 +0530778
Anand Doshi652bc072014-04-16 15:21:46 +0530779 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530780 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530781 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530782 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530783 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
784 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530785
Anand Doshi652bc072014-04-16 15:21:46 +0530786 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530787 }
788 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530789
Anand Doshi3543f302013-05-24 19:25:01 +0530790 _set_in_company_currency: function(item, print_field, base_field) {
791 // set values in base currency
792 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
793 precision(base_field, item));
794 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530795
Anand Doshibf3e54a2013-06-05 14:11:32 +0530796 get_terms: function() {
797 var me = this;
798 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530799 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530800 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530801 args: {
802 doctype: "Terms and Conditions",
803 fieldname: "terms",
804 filters: { name: this.frm.doc.tc_name },
805 },
806 });
807 }
808 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530809
810 taxes_and_charges: function() {
811 var me = this;
812 if(this.frm.doc.taxes_and_charges) {
813 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530814 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
815 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530816 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
817 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530818 "master_name": this.frm.doc.taxes_and_charges,
819 "tax_parentfield": this.other_fname
820 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530821 callback: function(r) {
822 if(!r.exc) {
Nabin Hait6b039142014-05-02 15:45:10 +0530823 me.frm.set_value(me.other_fname, r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530824 me.calculate_taxes_and_totals();
825 }
826 }
827 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530828 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530829 },
830
831 show_item_wise_taxes: function() {
832 if(this.frm.fields_dict.other_charges_calculation) {
833 $(this.get_item_wise_taxes_html())
834 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
835 }
836 },
Anand Doshi13945092014-09-21 19:45:49 +0530837
838 is_recurring: function() {
839 // set default values for recurring documents
840 if(this.frm.doc.is_recurring) {
841 var owner_email = this.frm.doc.owner=="Administrator"
842 ? frappe.user_info("Administrator").email
843 : this.frm.doc.owner;
844
845 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
846 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
847 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
848 }
849
850 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
851 },
852
853 from_date: function() {
854 // set to_date
855 if(this.frm.doc.from_date) {
856 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
857 'Yearly': 12};
858
859 var months = recurring_type_map[this.frm.doc.recurring_type];
860 if(months) {
861 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
862 months);
863 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
864 refresh_field('to_date');
865 }
866 }
867 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530868});