blob: 001da28c9bdfbcb8238ccf997b489dbc3dfd92f8 [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
Nabin Haitdd38a262014-12-26 13:15:21 +053036 if(this.frm.fields_dict["taxes"]) {
37 this["taxes_remove"] = this.calculate_taxes_and_totals;
Anand Doshia91c95f2013-08-23 13:00:47 +053038 }
Rushabh Mehtaff938022014-04-15 18:40:00 +053039
Nabin Haitdd38a262014-12-26 13:15:21 +053040 if(this.frm.fields_dict["items"]) {
41 this["items_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;
Nabin Haitdd38a262014-12-26 13:15:21 +053047 if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"] && !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 }
Nabin Haitdd38a262014-12-26 13:15:21 +053050 if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) {
51 cur_frm.get_field("items").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();
Nabin Hait239c2c32015-01-15 14:21:41 +053058 this.hide_currency_and_price_list()
Anand Doshi3543f302013-05-24 19:25:01 +053059 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053060 this.set_dynamic_labels();
Anand Doshi50d7e8c2015-01-06 17:14:13 +053061 erpnext.pos.make_pos_btn(this.frm);
Nabin Hait239c2c32015-01-15 14:21:41 +053062 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +053063
Nabin Hait239c2c32015-01-15 14:21:41 +053064 hide_currency_and_price_list: function() {
65 if(this.frm.doc.docstatus > 0) {
66 hide_field("currency_and_price_list");
67 } else {
68 unhide_field("currency_and_price_list");
69 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053070 },
71
Nabin Hait139dc7b2014-02-12 14:53:18 +053072 item_code: function(doc, cdt, cdn) {
73 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +053074 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +053075 if(item.item_code || item.barcode || item.serial_no) {
76 if(!this.validate_company_and_party()) {
Nabin Haitdd38a262014-12-26 13:15:21 +053077 cur_frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +053078 } else {
79 return this.frm.call({
80 method: "erpnext.stock.get_item_details.get_item_details",
81 child: item,
82 args: {
83 args: {
84 item_code: item.item_code,
85 barcode: item.barcode,
86 serial_no: item.serial_no,
87 warehouse: item.warehouse,
Nabin Hait444f9562014-06-20 15:59:49 +053088 parenttype: me.frm.doc.doctype,
89 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +053090 customer: me.frm.doc.customer,
91 supplier: me.frm.doc.supplier,
92 currency: me.frm.doc.currency,
93 conversion_rate: me.frm.doc.conversion_rate,
94 price_list: me.frm.doc.selling_price_list ||
95 me.frm.doc.buying_price_list,
96 price_list_currency: me.frm.doc.price_list_currency,
97 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
98 company: me.frm.doc.company,
99 order_type: me.frm.doc.order_type,
100 is_pos: cint(me.frm.doc.is_pos),
101 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Hait444f9562014-06-20 15:59:49 +0530102 transaction_date: me.frm.doc.transaction_date,
103 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
104 doctype: item.doctype,
Anand Doshi43f087c2014-08-26 14:25:53 +0530105 name: item.name,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530106 project_name: item.project_name || me.frm.doc.project_name,
107 qty: item.qty
Nabin Hait139dc7b2014-02-12 14:53:18 +0530108 }
109 },
Anand Doshi13945092014-09-21 19:45:49 +0530110
Nabin Hait139dc7b2014-02-12 14:53:18 +0530111 callback: function(r) {
112 if(!r.exc) {
113 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
114 }
115 }
116 });
117 }
118 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530119 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530120
121 serial_no: function(doc, cdt, cdn) {
122 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530123 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530124
125 if (item.serial_no) {
126 if (!item.item_code) {
127 this.frm.script_manager.trigger("item_code", cdt, cdn);
128 }
129 else {
130 var sr_no = [];
131
132 // Replacing all occurences of comma with carriage return
133 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
134
135 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530136
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530137 // Trim each string and push unique string to new list
138 for (var x=0; x<=serial_nos.length - 1; x++) {
139 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
140 sr_no.push(serial_nos[x].trim());
141 }
142 }
143
144 // Add the new list to the serial no. field in grid with each in new line
145 item.serial_no = "";
146 for (var x=0; x<=sr_no.length - 1; x++)
147 item.serial_no += sr_no[x] + '\n';
148
149 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530150 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530151 }
152 }
153 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530154
Anand Doshi923d41d2013-05-28 17:23:36 +0530155 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530156 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530157 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530158
Anand Doshi3543f302013-05-24 19:25:01 +0530159 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530160 var me = this;
161 var fn = function() {
162 if(me.frm.doc.company && me.frm.fields_dict.currency) {
163 var company_currency = me.get_company_currency();
164 if (!me.frm.doc.currency) {
165 me.frm.set_value("currency", company_currency);
166 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530167
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530168 if (me.frm.doc.currency == company_currency) {
169 me.frm.set_value("conversion_rate", 1.0);
170 }
171 if (me.frm.doc.price_list_currency == company_currency) {
172 me.frm.set_value('plc_conversion_rate', 1.0);
173 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530174
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530175 me.frm.script_manager.trigger("currency");
176 me.apply_pricing_rule();
Nabin Haitdd38a262014-12-26 13:15:21 +0530177 }
Anand Doshi3543f302013-05-24 19:25:01 +0530178 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530179
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530180 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
181 else var date = this.frm.doc.transaction_date;
182 erpnext.get_fiscal_year(this.frm.doc.company, date, fn);
Nabin Haitdd38a262014-12-26 13:15:21 +0530183
Neil Trini Lasrado31a92bd2014-12-15 15:49:40 +0530184 erpnext.get_letter_head(this.frm.doc.company);
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530185 },
186
187 transaction_date: function() {
188 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.transaction_date);
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530189 },
190
191 posting_date: function() {
Neil Trini Lasrado565d9862014-10-07 18:26:39 +0530192 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.posting_date);
Anand Doshi3543f302013-05-24 19:25:01 +0530193 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530194
Anand Doshi3543f302013-05-24 19:25:01 +0530195 get_company_currency: function() {
196 return erpnext.get_currency(this.frm.doc.company);
197 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530198
Anand Doshi3543f302013-05-24 19:25:01 +0530199 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530200 var me = this;
201 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530202
Anand Doshi61a2f682013-06-21 17:55:31 +0530203 var company_currency = this.get_company_currency();
204 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530205 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530206 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530207 me.frm.set_value("conversion_rate", exchange_rate);
208 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530209 });
210 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530211 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530212 }
213 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530214
Anand Doshi61a2f682013-06-21 17:55:31 +0530215 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530216 if(this.frm.doc.currency === this.get_company_currency()) {
217 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530218 }
219 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530220 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
221 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
222 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530223 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530224 if(this.frm.doc.ignore_pricing_rule) {
225 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530226 } else if (!this.in_apply_price_list){
227 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530228 }
229
Nabin Haita3dd72a2014-05-28 12:49:20 +0530230 }
Anand Doshi3543f302013-05-24 19:25:01 +0530231 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530232
Anand Doshi61a2f682013-06-21 17:55:31 +0530233 get_exchange_rate: function(from_currency, to_currency, callback) {
234 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530235 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530236 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530237 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530238 });
239 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530240
Anand Doshi3543f302013-05-24 19:25:01 +0530241 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530242 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530243 this.set_dynamic_labels();
Anand Doshi51f722d2014-07-04 15:44:26 +0530244 this.set_plc_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530245 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530246
Anand Doshi3543f302013-05-24 19:25:01 +0530247 plc_conversion_rate: function() {
Anand Doshi51f722d2014-07-04 15:44:26 +0530248 this.set_plc_conversion_rate();
249 if(!this.in_apply_price_list) {
250 this.apply_price_list();
251 }
252 },
253
254 set_plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530255 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
256 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530257 }
258 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530259 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530260 }
Anand Doshi3543f302013-05-24 19:25:01 +0530261 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530262
Anand Doshi3543f302013-05-24 19:25:01 +0530263 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530264 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530265 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530266
Anand Doshi652bc072014-04-16 15:21:46 +0530267 // tax rate
268 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530269 this.calculate_taxes_and_totals();
270 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530271
Anand Doshi923d41d2013-05-28 17:23:36 +0530272 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530273 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530274 try {
275 this.validate_on_previous_row(tax);
276 this.calculate_taxes_and_totals();
277 } catch(e) {
278 tax.row_id = null;
279 refresh_field("row_id", tax.name, tax.parentfield);
280 throw e;
281 }
282 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530283
Anand Doshi61a2f682013-06-21 17:55:31 +0530284 set_dynamic_labels: function() {
285 // What TODO? should we make price list system non-mandatory?
286 this.frm.toggle_reqd("plc_conversion_rate",
287 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530288
Anand Doshi61a2f682013-06-21 17:55:31 +0530289 var company_currency = this.get_company_currency();
290 this.change_form_labels(company_currency);
291 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530292 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530293 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530294
Anand Doshi923d41d2013-05-28 17:23:36 +0530295 recalculate: function() {
296 this.calculate_taxes_and_totals();
297 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530298
Anand Doshi923d41d2013-05-28 17:23:36 +0530299 recalculate_values: function() {
300 this.calculate_taxes_and_totals();
301 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530302
Anand Doshid0b00722013-05-28 18:54:48 +0530303 calculate_charges: function() {
304 this.calculate_taxes_and_totals();
305 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530306
Nabin Hait444f9562014-06-20 15:59:49 +0530307 ignore_pricing_rule: function() {
308 this.apply_pricing_rule();
309 },
310
311 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530312 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530313 return this.frm.call({
314 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
315 args: { args: this._get_args(item) },
316 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530317 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530318 me._set_values_for_item_list(r.message);
319 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
320 }
321 }
322 });
323 },
324
325 _get_args: function(item) {
326 var me = this;
327 return {
328 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530329 "customer": me.frm.doc.customer,
330 "customer_group": me.frm.doc.customer_group,
331 "territory": me.frm.doc.territory,
332 "supplier": me.frm.doc.supplier,
333 "supplier_type": me.frm.doc.supplier_type,
334 "currency": me.frm.doc.currency,
335 "conversion_rate": me.frm.doc.conversion_rate,
336 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
337 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
338 "company": me.frm.doc.company,
339 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
340 "campaign": me.frm.doc.campaign,
341 "sales_partner": me.frm.doc.sales_partner,
342 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
343 "parenttype": me.frm.doc.doctype,
344 "parent": me.frm.doc.name
345 };
Nabin Hait444f9562014-06-20 15:59:49 +0530346 },
347
348 _get_item_list: function(item) {
349 var item_list = [];
350 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530351 if (d.item_code) {
352 item_list.push({
353 "doctype": d.doctype,
354 "name": d.name,
355 "item_code": d.item_code,
356 "item_group": d.item_group,
357 "brand": d.brand,
358 "qty": d.qty
359 });
360 }
Nabin Hait444f9562014-06-20 15:59:49 +0530361 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530362
Nabin Hait444f9562014-06-20 15:59:49 +0530363 if (item) {
364 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530365 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530366 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530367 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530368 });
369 }
Nabin Hait444f9562014-06-20 15:59:49 +0530370 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530371 },
372
Anand Doshidffec8f2014-07-01 17:45:15 +0530373 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530374 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530375 var price_list_rate_changed = false;
Anand Doshidffec8f2014-07-01 17:45:15 +0530376 $.each(children, function(i, d) {
Nabin Hait49a27292014-12-17 11:21:32 +0530377 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidffec8f2014-07-01 17:45:15 +0530378 $.each(d, function(k, v) {
379 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530380 if(k=="price_list_rate") {
381 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
382 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530383 frappe.model.set_value(d.doctype, d.name, k, v);
384 }
385 });
Nabin Hait49a27292014-12-17 11:21:32 +0530386 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530387 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530388 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
389 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530390
391 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530392 });
393 },
394
Nabin Hait49a27292014-12-17 11:21:32 +0530395 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530396 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530397 var args = this._get_args(item);
398 if(!args.item_list.length) {
399 return;
400 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530401 return this.frm.call({
402 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530403 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530404 callback: function(r) {
405 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530406 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530407 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
408 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530409 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +0530410 me._set_values_for_item_list(r.message.children);
411 }
412 }
413 });
414 },
415
Anand Doshi3543f302013-05-24 19:25:01 +0530416 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530417 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530418 try {
419 this.validate_on_previous_row(tax);
420 this.validate_inclusive_tax(tax);
421 this.calculate_taxes_and_totals();
422 } catch(e) {
423 tax.included_in_print_rate = 0;
424 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
425 throw e;
426 }
427 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530428
Anand Doshi3543f302013-05-24 19:25:01 +0530429 validate_on_previous_row: function(tax) {
430 // validate if a valid row id is mentioned in case of
431 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530432 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530433 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530434 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 +0530435 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530436 }
437 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530438
Anand Doshi3543f302013-05-24 19:25:01 +0530439 validate_inclusive_tax: function(tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530440 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530441 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530442 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530443 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530444
Anand Doshi3543f302013-05-24 19:25:01 +0530445 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530446 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
447 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530448 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530449 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530450
Anand Doshi3543f302013-05-24 19:25:01 +0530451 if(cint(tax.included_in_print_rate)) {
452 if(tax.charge_type == "Actual") {
453 // inclusive tax cannot be of type Actual
454 actual_type_error();
455 } else if(tax.charge_type == "On Previous Row Amount" &&
Nabin Hait239c2c32015-01-15 14:21:41 +0530456 !cint(this.frm.doc["taxes"][tax.row_id - 1].included_in_print_rate)) {
Anand Doshi3543f302013-05-24 19:25:01 +0530457 // referred row should also be an inclusive tax
458 on_previous_row_error(tax.row_id);
459 } else if(tax.charge_type == "On Previous Row Total") {
Nabin Hait239c2c32015-01-15 14:21:41 +0530460 var taxes_not_included = $.map(this.frm.doc["taxes"].slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530461 function(t) { return cint(t.included_in_print_rate) ? null : t; });
462 if(taxes_not_included.length > 0) {
463 // all rows above this tax should be inclusive
464 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
465 }
466 }
467 }
468 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530469
Anand Doshi3543f302013-05-24 19:25:01 +0530470 _load_item_tax_rate: function(item_tax_rate) {
471 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
472 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530473
Anand Doshi3543f302013-05-24 19:25:01 +0530474 _get_tax_rate: function(tax, item_tax_map) {
475 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
476 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
477 tax.rate;
478 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530479
Anand Doshi3543f302013-05-24 19:25:01 +0530480 get_item_wise_taxes_html: function() {
481 var item_tax = {};
482 var tax_accounts = [];
483 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530484
Nabin Hait96339342015-01-21 17:22:11 +0530485 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530486 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530487 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530488 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530489 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530490 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530491 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530492 var tax_rate = "";
493 if(tax_data[0] != null) {
494 tax_rate = (tax.charge_type === "Actual") ?
495 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
496 (flt(tax_data[0], tax_rate_precision) + "%");
497 }
498 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
499 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530500
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530501 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530502 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530503 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530504 }
Anand Doshi3543f302013-05-24 19:25:01 +0530505 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530506 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530507 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530508
509 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530510 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530511
Anand Doshi179e3772013-09-05 16:53:57 +0530512 var distinct_item_names = [];
513 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530514 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530515 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
516 distinct_item_names.push(item.item_code || item.item_name);
517 distinct_items.push(item);
518 }
519 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530520
Anand Doshi179e3772013-09-05 16:53:57 +0530521 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530522 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530523 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530524 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
525 item_name: item.item_name,
526 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530527 return item_tax_record[head[0]] ?
528 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530529 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530530 }).join("\n")
531 });
532 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530533
Anand Doshi53b73422013-05-31 11:50:01 +0530534 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530535 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
536 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530537 <thead><tr>' + headings + '</tr></thead> \
538 <tbody>' + rows + '</tbody> \
539 </table></div>';
540 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530541
Nabin Hait139dc7b2014-02-12 14:53:18 +0530542 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530543 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530544 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530545
Nabin Hait0f231062014-02-20 11:27:47 +0530546 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530547 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530548 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530549 msgprint(__("Please specify") + ": " +
550 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530551 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530552 valid = false;
553 }
Anand Doshi3543f302013-05-24 19:25:01 +0530554 }
555 });
556 return valid;
557 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530558
Anand Doshi3543f302013-05-24 19:25:01 +0530559 validate_conversion_rate: function() {
560 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530561 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530562 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530563 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530564
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530565 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530566 frappe.throw(repl('%(conversion_rate_label)s' +
567 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
568 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530569 {
570 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530571 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530572 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530573 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530574 }
Anand Doshi3543f302013-05-24 19:25:01 +0530575 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530576
Anand Doshi3543f302013-05-24 19:25:01 +0530577 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530578 this.discount_amount_applied = false;
579 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530580 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530581 this.apply_discount_amount();
582 },
583
584 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530585 this.validate_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530586 this.calculate_item_values();
587 this.initialize_taxes();
588 this.determine_exclusive_rate && this.determine_exclusive_rate();
589 this.calculate_net_total();
590 this.calculate_taxes();
591 this.calculate_totals();
592 this._cleanup();
593 this.show_item_wise_taxes();
594 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530595
Anand Doshi3543f302013-05-24 19:25:01 +0530596 initialize_taxes: function() {
597 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530598
Nabin Hait96339342015-01-21 17:22:11 +0530599 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530600 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530601 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530602 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530603 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
604
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530605 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530606 tax_fields.push("tax_amount");
607
608 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530609
Anand Doshi3543f302013-05-24 19:25:01 +0530610 me.validate_on_previous_row(tax);
611 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530612 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530613 });
614 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530615
Anand Doshi3543f302013-05-24 19:25:01 +0530616 calculate_taxes: function() {
617 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530618 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530619
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530620 // maintain actual tax rate based on idx
Nabin Hait96339342015-01-21 17:22:11 +0530621 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530622 if (tax.charge_type == "Actual") {
Nabin Hait0a35eff2014-08-19 12:39:40 +0530623 actual_tax_dict[tax.idx] = flt(tax.rate, precision("tax_amount", tax));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530624 }
625 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530626
Nabin Hait96339342015-01-21 17:22:11 +0530627 $.each(this.frm.doc["items"] || [], function(n, item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530628 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530629
Nabin Hait96339342015-01-21 17:22:11 +0530630 $.each(me.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530631 // tax_amount represents the amount of tax for the current step
632 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530633
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530634 // Adjust divisional loss to the last item
635 if (tax.charge_type == "Actual") {
636 actual_tax_dict[tax.idx] -= current_tax_amount;
Nabin Hait239c2c32015-01-15 14:21:41 +0530637 if (n == me.frm.doc["items"].length - 1) {
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530638 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530639 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530640 }
641
Anand Doshi3543f302013-05-24 19:25:01 +0530642 // store tax_amount for current item as it will be used for
643 // charge type = 'On Previous Row Amount'
644 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530645
Anand Doshi3543f302013-05-24 19:25:01 +0530646 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530647 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530648 tax.tax_amount += current_tax_amount;
649
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530650 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530651
Anand Doshi3543f302013-05-24 19:25:01 +0530652 // for buying
653 if(tax.category) {
654 // if just for valuation, do not add the tax amount in total
655 // hence, setting it as 0 for further steps
656 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530657
Anand Doshi3543f302013-05-24 19:25:01 +0530658 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
659 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530660
Anand Doshi3543f302013-05-24 19:25:01 +0530661 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530662 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530663 // item's amount, previously applied tax and the current tax on that item
664 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530665 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530666 precision("total", tax));
667 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530668 tax.grand_total_for_current_item =
Nabin Hait239c2c32015-01-15 14:21:41 +0530669 flt(me.frm.doc["taxes"][i-1].grand_total_for_current_item + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530670 precision("total", tax));
671 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530672
Anand Doshi3543f302013-05-24 19:25:01 +0530673 // in tax.total, accumulate grand total for each item
674 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530675
676 // set precision in the last item iteration
Nabin Hait239c2c32015-01-15 14:21:41 +0530677 if (n == me.frm.doc["items"].length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530678 me.round_off_totals(tax);
679
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530680 // adjust Discount Amount loss in last tax iteration
Nabin Hait239c2c32015-01-15 14:21:41 +0530681 if ((i == me.frm.doc["taxes"].length - 1) && me.discount_amount_applied)
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530682 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530683 }
Anand Doshi3543f302013-05-24 19:25:01 +0530684 });
685 });
686 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530687
688 round_off_totals: function(tax) {
689 tax.total = flt(tax.total, precision("total", tax));
690 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530691 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530692 precision("tax_amount", tax));
693 },
694
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530695 adjust_discount_amount_loss: function(tax) {
Nabin Hait2244ac42015-01-12 17:35:14 +0530696 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 +0530697 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530698 discount_amount_loss, precision("tax_amount", tax));
699 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530700 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530701
Anand Doshi3543f302013-05-24 19:25:01 +0530702 get_current_tax_amount: function(item, tax, item_tax_map) {
703 var tax_rate = this._get_tax_rate(tax, item_tax_map);
704 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530705
Anand Doshi3543f302013-05-24 19:25:01 +0530706 if(tax.charge_type == "Actual") {
707 // distribute the tax amount proportionally to each item row
708 var actual = flt(tax.rate, precision("tax_amount", tax));
709 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait2244ac42015-01-12 17:35:14 +0530710 ((item.base_amount / this.frm.doc.net_total) * actual) : 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530711
Anand Doshi3543f302013-05-24 19:25:01 +0530712 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530713 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530714
Anand Doshi3543f302013-05-24 19:25:01 +0530715 } else if(tax.charge_type == "On Previous Row Amount") {
716 current_tax_amount = (tax_rate / 100.0) *
Nabin Hait239c2c32015-01-15 14:21:41 +0530717 this.frm.doc["taxes"][cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530718
Anand Doshi3543f302013-05-24 19:25:01 +0530719 } else if(tax.charge_type == "On Previous Row Total") {
720 current_tax_amount = (tax_rate / 100.0) *
Nabin Hait239c2c32015-01-15 14:21:41 +0530721 this.frm.doc["taxes"][cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530722 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530723
Anand Doshi53b73422013-05-31 11:50:01 +0530724 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530725
Anand Doshi53b73422013-05-31 11:50:01 +0530726 // store tax breakup for each item
727 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530728
Anand Doshi53b73422013-05-31 11:50:01 +0530729 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530730 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530731
Anand Doshi3543f302013-05-24 19:25:01 +0530732 _cleanup: function() {
Nabin Hait96339342015-01-21 17:22:11 +0530733 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530734 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530735 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530736 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530737
Anand Doshi3543f302013-05-24 19:25:01 +0530738 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
739 });
740 },
Anand Doshifc777182013-05-27 19:29:07 +0530741
Anand Doshi652bc072014-04-16 15:21:46 +0530742 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530743 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530744 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530745 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530746 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
747 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530748
Anand Doshi652bc072014-04-16 15:21:46 +0530749 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530750 }
751 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530752
Anand Doshi3543f302013-05-24 19:25:01 +0530753 _set_in_company_currency: function(item, print_field, base_field) {
754 // set values in base currency
755 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
756 precision(base_field, item));
757 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530758
Anand Doshibf3e54a2013-06-05 14:11:32 +0530759 get_terms: function() {
760 var me = this;
761 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530762 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530763 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530764 args: {
765 doctype: "Terms and Conditions",
766 fieldname: "terms",
767 filters: { name: this.frm.doc.tc_name },
768 },
769 });
770 }
771 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530772
773 taxes_and_charges: function() {
774 var me = this;
775 if(this.frm.doc.taxes_and_charges) {
776 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530777 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
778 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530779 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
780 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530781 "master_name": this.frm.doc.taxes_and_charges,
Nabin Haitdd38a262014-12-26 13:15:21 +0530782 "tax_parentfield": "taxes"
Nabin Hait6b039142014-05-02 15:45:10 +0530783 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530784 callback: function(r) {
785 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530786 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530787 me.calculate_taxes_and_totals();
788 }
789 }
790 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530791 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530792 },
793
794 show_item_wise_taxes: function() {
795 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530796 var html = this.get_item_wise_taxes_html();
797 if (html) {
798 this.frm.toggle_display("other_charges_calculation", true);
799 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
800 } else {
801 this.frm.toggle_display("other_charges_calculation", false);
802 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530803 }
804 },
Anand Doshi13945092014-09-21 19:45:49 +0530805
806 is_recurring: function() {
807 // set default values for recurring documents
808 if(this.frm.doc.is_recurring) {
809 var owner_email = this.frm.doc.owner=="Administrator"
810 ? frappe.user_info("Administrator").email
811 : this.frm.doc.owner;
812
813 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
814 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
815 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
816 }
817
818 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
819 },
820
821 from_date: function() {
822 // set to_date
823 if(this.frm.doc.from_date) {
824 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
825 'Yearly': 12};
826
827 var months = recurring_type_map[this.frm.doc.recurring_type];
828 if(months) {
829 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
830 months);
831 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
832 refresh_field('to_date');
833 }
834 }
835 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530836});