blob: 107114ec64214894d59624c47045d69d09b37ba2 [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();
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
Anand Doshi50d7e8c2015-01-06 17:14:13 +053061 erpnext.pos.make_pos_btn(this.frm);
62
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053063 },
64
Nabin Hait139dc7b2014-02-12 14:53:18 +053065 item_code: function(doc, cdt, cdn) {
66 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +053067 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +053068 if(item.item_code || item.barcode || item.serial_no) {
69 if(!this.validate_company_and_party()) {
Nabin Haitdd38a262014-12-26 13:15:21 +053070 cur_frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +053071 } else {
72 return this.frm.call({
73 method: "erpnext.stock.get_item_details.get_item_details",
74 child: item,
75 args: {
76 args: {
77 item_code: item.item_code,
78 barcode: item.barcode,
79 serial_no: item.serial_no,
80 warehouse: item.warehouse,
Nabin Hait444f9562014-06-20 15:59:49 +053081 parenttype: me.frm.doc.doctype,
82 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +053083 customer: me.frm.doc.customer,
84 supplier: me.frm.doc.supplier,
85 currency: me.frm.doc.currency,
86 conversion_rate: me.frm.doc.conversion_rate,
87 price_list: me.frm.doc.selling_price_list ||
88 me.frm.doc.buying_price_list,
89 price_list_currency: me.frm.doc.price_list_currency,
90 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
91 company: me.frm.doc.company,
92 order_type: me.frm.doc.order_type,
93 is_pos: cint(me.frm.doc.is_pos),
94 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Hait444f9562014-06-20 15:59:49 +053095 transaction_date: me.frm.doc.transaction_date,
96 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
97 doctype: item.doctype,
Anand Doshi43f087c2014-08-26 14:25:53 +053098 name: item.name,
Anand Doshi9b955fe2015-01-05 16:19:12 +053099 project_name: item.project_name || me.frm.doc.project_name,
100 qty: item.qty
Nabin Hait139dc7b2014-02-12 14:53:18 +0530101 }
102 },
Anand Doshi13945092014-09-21 19:45:49 +0530103
Nabin Hait139dc7b2014-02-12 14:53:18 +0530104 callback: function(r) {
105 if(!r.exc) {
106 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
107 }
108 }
109 });
110 }
111 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530112 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530113
114 serial_no: function(doc, cdt, cdn) {
115 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530116 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530117
118 if (item.serial_no) {
119 if (!item.item_code) {
120 this.frm.script_manager.trigger("item_code", cdt, cdn);
121 }
122 else {
123 var sr_no = [];
124
125 // Replacing all occurences of comma with carriage return
126 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
127
128 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530129
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530130 // Trim each string and push unique string to new list
131 for (var x=0; x<=serial_nos.length - 1; x++) {
132 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
133 sr_no.push(serial_nos[x].trim());
134 }
135 }
136
137 // Add the new list to the serial no. field in grid with each in new line
138 item.serial_no = "";
139 for (var x=0; x<=sr_no.length - 1; x++)
140 item.serial_no += sr_no[x] + '\n';
141
142 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530143 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530144 }
145 }
146 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530147
Anand Doshi923d41d2013-05-28 17:23:36 +0530148 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530149 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530150 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530151
Anand Doshi3543f302013-05-24 19:25:01 +0530152 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530153 var me = this;
154 var fn = function() {
155 if(me.frm.doc.company && me.frm.fields_dict.currency) {
156 var company_currency = me.get_company_currency();
157 if (!me.frm.doc.currency) {
158 me.frm.set_value("currency", company_currency);
159 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530160
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530161 if (me.frm.doc.currency == company_currency) {
162 me.frm.set_value("conversion_rate", 1.0);
163 }
164 if (me.frm.doc.price_list_currency == company_currency) {
165 me.frm.set_value('plc_conversion_rate', 1.0);
166 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530167
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530168 me.frm.script_manager.trigger("currency");
169 me.apply_pricing_rule();
Nabin Haitdd38a262014-12-26 13:15:21 +0530170 }
Anand Doshi3543f302013-05-24 19:25:01 +0530171 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530172
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530173 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
174 else var date = this.frm.doc.transaction_date;
175 erpnext.get_fiscal_year(this.frm.doc.company, date, fn);
Nabin Haitdd38a262014-12-26 13:15:21 +0530176
Neil Trini Lasrado31a92bd2014-12-15 15:49:40 +0530177 erpnext.get_letter_head(this.frm.doc.company);
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530178 },
179
180 transaction_date: function() {
181 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.transaction_date);
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530182 },
183
184 posting_date: function() {
Neil Trini Lasrado565d9862014-10-07 18:26:39 +0530185 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.posting_date);
Anand Doshi3543f302013-05-24 19:25:01 +0530186 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530187
Anand Doshi3543f302013-05-24 19:25:01 +0530188 get_company_currency: function() {
189 return erpnext.get_currency(this.frm.doc.company);
190 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530191
Anand Doshi3543f302013-05-24 19:25:01 +0530192 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530193 var me = this;
194 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530195
Anand Doshi61a2f682013-06-21 17:55:31 +0530196 var company_currency = this.get_company_currency();
197 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530198 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530199 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530200 me.frm.set_value("conversion_rate", exchange_rate);
201 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530202 });
203 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530204 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530205 }
206 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530207
Anand Doshi61a2f682013-06-21 17:55:31 +0530208 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530209 if(this.frm.doc.currency === this.get_company_currency()) {
210 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530211 }
212 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530213 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
214 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
215 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530216 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530217 if(this.frm.doc.ignore_pricing_rule) {
218 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530219 } else if (!this.in_apply_price_list){
220 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530221 }
222
Nabin Haita3dd72a2014-05-28 12:49:20 +0530223 }
Anand Doshi3543f302013-05-24 19:25:01 +0530224 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530225
Anand Doshi61a2f682013-06-21 17:55:31 +0530226 get_exchange_rate: function(from_currency, to_currency, callback) {
227 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530228 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530229 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530230 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530231 });
232 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530233
Anand Doshi3543f302013-05-24 19:25:01 +0530234 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530235 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530236 this.set_dynamic_labels();
Anand Doshi51f722d2014-07-04 15:44:26 +0530237 this.set_plc_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530238 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530239
Anand Doshi3543f302013-05-24 19:25:01 +0530240 plc_conversion_rate: function() {
Anand Doshi51f722d2014-07-04 15:44:26 +0530241 this.set_plc_conversion_rate();
242 if(!this.in_apply_price_list) {
243 this.apply_price_list();
244 }
245 },
246
247 set_plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530248 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
249 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530250 }
251 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530252 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530253 }
Anand Doshi3543f302013-05-24 19:25:01 +0530254 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530255
Anand Doshi3543f302013-05-24 19:25:01 +0530256 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530257 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530258 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530259
Anand Doshi652bc072014-04-16 15:21:46 +0530260 // tax rate
261 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530262 this.calculate_taxes_and_totals();
263 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530264
Anand Doshi923d41d2013-05-28 17:23:36 +0530265 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530266 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530267 try {
268 this.validate_on_previous_row(tax);
269 this.calculate_taxes_and_totals();
270 } catch(e) {
271 tax.row_id = null;
272 refresh_field("row_id", tax.name, tax.parentfield);
273 throw e;
274 }
275 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530276
Anand Doshi61a2f682013-06-21 17:55:31 +0530277 set_dynamic_labels: function() {
278 // What TODO? should we make price list system non-mandatory?
279 this.frm.toggle_reqd("plc_conversion_rate",
280 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530281
Anand Doshi61a2f682013-06-21 17:55:31 +0530282 var company_currency = this.get_company_currency();
283 this.change_form_labels(company_currency);
284 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530285 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530286 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530287
Anand Doshi923d41d2013-05-28 17:23:36 +0530288 recalculate: function() {
289 this.calculate_taxes_and_totals();
290 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530291
Anand Doshi923d41d2013-05-28 17:23:36 +0530292 recalculate_values: function() {
293 this.calculate_taxes_and_totals();
294 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530295
Anand Doshid0b00722013-05-28 18:54:48 +0530296 calculate_charges: function() {
297 this.calculate_taxes_and_totals();
298 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530299
Nabin Hait444f9562014-06-20 15:59:49 +0530300 ignore_pricing_rule: function() {
301 this.apply_pricing_rule();
302 },
303
304 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530305 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530306 return this.frm.call({
307 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
308 args: { args: this._get_args(item) },
309 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530310 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530311 me._set_values_for_item_list(r.message);
312 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
313 }
314 }
315 });
316 },
317
318 _get_args: function(item) {
319 var me = this;
320 return {
321 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530322 "customer": me.frm.doc.customer,
323 "customer_group": me.frm.doc.customer_group,
324 "territory": me.frm.doc.territory,
325 "supplier": me.frm.doc.supplier,
326 "supplier_type": me.frm.doc.supplier_type,
327 "currency": me.frm.doc.currency,
328 "conversion_rate": me.frm.doc.conversion_rate,
329 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
330 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
331 "company": me.frm.doc.company,
332 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
333 "campaign": me.frm.doc.campaign,
334 "sales_partner": me.frm.doc.sales_partner,
335 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
336 "parenttype": me.frm.doc.doctype,
337 "parent": me.frm.doc.name
338 };
Nabin Hait444f9562014-06-20 15:59:49 +0530339 },
340
341 _get_item_list: function(item) {
342 var item_list = [];
343 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530344 if (d.item_code) {
345 item_list.push({
346 "doctype": d.doctype,
347 "name": d.name,
348 "item_code": d.item_code,
349 "item_group": d.item_group,
350 "brand": d.brand,
351 "qty": d.qty
352 });
353 }
Nabin Hait444f9562014-06-20 15:59:49 +0530354 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530355
Nabin Hait444f9562014-06-20 15:59:49 +0530356 if (item) {
357 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530358 } else {
Nabin Hait444f9562014-06-20 15:59:49 +0530359 $.each(this.get_item_doclist(), function(i, d) {
360 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530361 });
362 }
Nabin Hait444f9562014-06-20 15:59:49 +0530363 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530364 },
365
Anand Doshidffec8f2014-07-01 17:45:15 +0530366 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530367 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530368 var price_list_rate_changed = false;
Anand Doshidffec8f2014-07-01 17:45:15 +0530369 $.each(children, function(i, d) {
Nabin Hait49a27292014-12-17 11:21:32 +0530370 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidffec8f2014-07-01 17:45:15 +0530371 $.each(d, function(k, v) {
372 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530373 if(k=="price_list_rate") {
374 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
375 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530376 frappe.model.set_value(d.doctype, d.name, k, v);
377 }
378 });
Nabin Hait49a27292014-12-17 11:21:32 +0530379 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530380 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530381 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
382 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530383
384 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530385 });
386 },
387
Nabin Hait49a27292014-12-17 11:21:32 +0530388 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530389 var me = this;
390 return this.frm.call({
391 method: "erpnext.stock.get_item_details.apply_price_list",
Nabin Hait49a27292014-12-17 11:21:32 +0530392 args: { args: this._get_args(item) },
Anand Doshidffec8f2014-07-01 17:45:15 +0530393 callback: function(r) {
394 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530395 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530396 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
397 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530398 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +0530399 me._set_values_for_item_list(r.message.children);
400 }
401 }
402 });
403 },
404
Anand Doshi3543f302013-05-24 19:25:01 +0530405 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530406 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530407 try {
408 this.validate_on_previous_row(tax);
409 this.validate_inclusive_tax(tax);
410 this.calculate_taxes_and_totals();
411 } catch(e) {
412 tax.included_in_print_rate = 0;
413 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
414 throw e;
415 }
416 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530417
Anand Doshi3543f302013-05-24 19:25:01 +0530418 validate_on_previous_row: function(tax) {
419 // validate if a valid row id is mentioned in case of
420 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530421 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530422 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530423 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 +0530424 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530425 }
426 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530427
Anand Doshi3543f302013-05-24 19:25:01 +0530428 validate_inclusive_tax: function(tax) {
429 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530430
Anand Doshi3543f302013-05-24 19:25:01 +0530431 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530432 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530433 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530434 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530435
Anand Doshi3543f302013-05-24 19:25:01 +0530436 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530437 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
438 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530439 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530440 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530441
Anand Doshi3543f302013-05-24 19:25:01 +0530442 if(cint(tax.included_in_print_rate)) {
443 if(tax.charge_type == "Actual") {
444 // inclusive tax cannot be of type Actual
445 actual_type_error();
446 } else if(tax.charge_type == "On Previous Row Amount" &&
447 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
448 // referred row should also be an inclusive tax
449 on_previous_row_error(tax.row_id);
450 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530451 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530452 function(t) { return cint(t.included_in_print_rate) ? null : t; });
453 if(taxes_not_included.length > 0) {
454 // all rows above this tax should be inclusive
455 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
456 }
457 }
458 }
459 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530460
Anand Doshi3543f302013-05-24 19:25:01 +0530461 _load_item_tax_rate: function(item_tax_rate) {
462 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
463 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530464
Anand Doshi3543f302013-05-24 19:25:01 +0530465 _get_tax_rate: function(tax, item_tax_map) {
466 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
467 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
468 tax.rate;
469 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530470
Anand Doshi3543f302013-05-24 19:25:01 +0530471 get_item_wise_taxes_html: function() {
472 var item_tax = {};
473 var tax_accounts = [];
474 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530475
Anand Doshi3543f302013-05-24 19:25:01 +0530476 $.each(this.get_tax_doclist(), function(i, tax) {
477 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530478 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530479 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530480 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530481 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530482 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530483 var tax_rate = "";
484 if(tax_data[0] != null) {
485 tax_rate = (tax.charge_type === "Actual") ?
486 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
487 (flt(tax_data[0], tax_rate_precision) + "%");
488 }
489 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
490 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530491
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530492 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530493 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530494 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530495 }
Anand Doshi3543f302013-05-24 19:25:01 +0530496 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530497 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530498 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530499
500 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530501 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530502
Anand Doshi179e3772013-09-05 16:53:57 +0530503 var distinct_item_names = [];
504 var distinct_items = [];
505 $.each(this.get_item_doclist(), function(i, item) {
506 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
507 distinct_item_names.push(item.item_code || item.item_name);
508 distinct_items.push(item);
509 }
510 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530511
Anand Doshi179e3772013-09-05 16:53:57 +0530512 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530513 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530514 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530515 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
516 item_name: item.item_name,
517 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530518 return item_tax_record[head[0]] ?
519 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530520 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530521 }).join("\n")
522 });
523 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530524
Anand Doshi53b73422013-05-31 11:50:01 +0530525 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530526 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
527 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530528 <thead><tr>' + headings + '</tr></thead> \
529 <tbody>' + rows + '</tbody> \
530 </table></div>';
531 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530532
Nabin Hait139dc7b2014-02-12 14:53:18 +0530533 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530534 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530535 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530536
Nabin Hait0f231062014-02-20 11:27:47 +0530537 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530538 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530539 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530540 msgprint(__("Please specify") + ": " +
541 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530542 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530543 valid = false;
544 }
Anand Doshi3543f302013-05-24 19:25:01 +0530545 }
546 });
547 return valid;
548 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530549
Anand Doshi3543f302013-05-24 19:25:01 +0530550 get_item_doclist: function() {
Nabin Haitdd38a262014-12-26 13:15:21 +0530551 return this.frm.doc["items"] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530552 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530553
Anand Doshi3543f302013-05-24 19:25:01 +0530554 get_tax_doclist: function() {
Nabin Haitdd38a262014-12-26 13:15:21 +0530555 return this.frm.doc["taxes"] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530556 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530557
Anand Doshi3543f302013-05-24 19:25:01 +0530558 validate_conversion_rate: function() {
559 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530560 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530561 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530562 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530563
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530564 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530565 frappe.throw(repl('%(conversion_rate_label)s' +
566 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
567 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530568 {
569 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530570 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530571 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530572 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530573 }
Anand Doshi3543f302013-05-24 19:25:01 +0530574 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530575
Anand Doshi3543f302013-05-24 19:25:01 +0530576 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530577 this.discount_amount_applied = false;
578 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530579 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530580 this.apply_discount_amount();
581 },
582
583 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530584 this.validate_conversion_rate();
585 this.frm.item_doclist = this.get_item_doclist();
586 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530587
Anand Doshi3543f302013-05-24 19:25:01 +0530588 this.calculate_item_values();
589 this.initialize_taxes();
590 this.determine_exclusive_rate && this.determine_exclusive_rate();
591 this.calculate_net_total();
592 this.calculate_taxes();
593 this.calculate_totals();
594 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530595
Anand Doshi3543f302013-05-24 19:25:01 +0530596 this.show_item_wise_taxes();
597 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530598
Anand Doshi3543f302013-05-24 19:25:01 +0530599 initialize_taxes: function() {
600 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530601
Anand Doshi3543f302013-05-24 19:25:01 +0530602 $.each(this.frm.tax_doclist, function(i, tax) {
603 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530604 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530605 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530606 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
607
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530608 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530609 tax_fields.push("tax_amount");
610
611 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530612
Anand Doshi3543f302013-05-24 19:25:01 +0530613 me.validate_on_previous_row(tax);
614 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530615 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530616 });
617 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530618
Anand Doshi3543f302013-05-24 19:25:01 +0530619 calculate_taxes: function() {
620 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530621 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530622
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530623 // maintain actual tax rate based on idx
624 $.each(this.frm.tax_doclist, function(i, tax) {
625 if (tax.charge_type == "Actual") {
Nabin Hait0a35eff2014-08-19 12:39:40 +0530626 actual_tax_dict[tax.idx] = flt(tax.rate, precision("tax_amount", tax));
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530627 }
628 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530629
Anand Doshi3543f302013-05-24 19:25:01 +0530630 $.each(this.frm.item_doclist, function(n, item) {
631 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530632
Anand Doshi3543f302013-05-24 19:25:01 +0530633 $.each(me.frm.tax_doclist, function(i, tax) {
634 // tax_amount represents the amount of tax for the current step
635 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530636
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530637 // Adjust divisional loss to the last item
638 if (tax.charge_type == "Actual") {
639 actual_tax_dict[tax.idx] -= current_tax_amount;
640 if (n == me.frm.item_doclist.length - 1) {
641 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530642 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530643 }
644
Anand Doshi3543f302013-05-24 19:25:01 +0530645 // store tax_amount for current item as it will be used for
646 // charge type = 'On Previous Row Amount'
647 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530648
Anand Doshi3543f302013-05-24 19:25:01 +0530649 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530650 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530651 tax.tax_amount += current_tax_amount;
652
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530653 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530654
Anand Doshi3543f302013-05-24 19:25:01 +0530655 // for buying
656 if(tax.category) {
657 // if just for valuation, do not add the tax amount in total
658 // hence, setting it as 0 for further steps
659 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530660
Anand Doshi3543f302013-05-24 19:25:01 +0530661 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
662 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530663
Anand Doshi3543f302013-05-24 19:25:01 +0530664 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530665 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530666 // item's amount, previously applied tax and the current tax on that item
667 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530668 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530669 precision("total", tax));
670 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530671 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530672 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
673 precision("total", tax));
674 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530675
Anand Doshi3543f302013-05-24 19:25:01 +0530676 // in tax.total, accumulate grand total for each item
677 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530678
679 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530680 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530681 me.round_off_totals(tax);
682
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530683 // adjust Discount Amount loss in last tax iteration
684 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
685 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530686 }
Anand Doshi3543f302013-05-24 19:25:01 +0530687 });
688 });
689 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530690
691 round_off_totals: function(tax) {
692 tax.total = flt(tax.total, precision("total", tax));
693 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530694 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530695 precision("tax_amount", tax));
696 },
697
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530698 adjust_discount_amount_loss: function(tax) {
699 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530700 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530701 discount_amount_loss, precision("tax_amount", tax));
702 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530703 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530704
Anand Doshi3543f302013-05-24 19:25:01 +0530705 get_current_tax_amount: function(item, tax, item_tax_map) {
706 var tax_rate = this._get_tax_rate(tax, item_tax_map);
707 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530708
Anand Doshi3543f302013-05-24 19:25:01 +0530709 if(tax.charge_type == "Actual") {
710 // distribute the tax amount proportionally to each item row
711 var actual = flt(tax.rate, precision("tax_amount", tax));
712 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530713 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530714 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530715
Anand Doshi3543f302013-05-24 19:25:01 +0530716 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530717 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530718
Anand Doshi3543f302013-05-24 19:25:01 +0530719 } else if(tax.charge_type == "On Previous Row Amount") {
720 current_tax_amount = (tax_rate / 100.0) *
721 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530722
Anand Doshi3543f302013-05-24 19:25:01 +0530723 } else if(tax.charge_type == "On Previous Row Total") {
724 current_tax_amount = (tax_rate / 100.0) *
725 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530726 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530727
Anand Doshi53b73422013-05-31 11:50:01 +0530728 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530729
Anand Doshi53b73422013-05-31 11:50:01 +0530730 // store tax breakup for each item
731 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530732
Anand Doshi53b73422013-05-31 11:50:01 +0530733 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530734 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530735
Anand Doshi3543f302013-05-24 19:25:01 +0530736 _cleanup: function() {
737 $.each(this.frm.tax_doclist, function(i, tax) {
738 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530739 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530740 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530741
Anand Doshi3543f302013-05-24 19:25:01 +0530742 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
743 });
744 },
Anand Doshifc777182013-05-27 19:29:07 +0530745
Anand Doshi652bc072014-04-16 15:21:46 +0530746 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530747 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530748 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530749 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530750 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
751 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530752
Anand Doshi652bc072014-04-16 15:21:46 +0530753 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530754 }
755 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530756
Anand Doshi3543f302013-05-24 19:25:01 +0530757 _set_in_company_currency: function(item, print_field, base_field) {
758 // set values in base currency
759 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
760 precision(base_field, item));
761 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530762
Anand Doshibf3e54a2013-06-05 14:11:32 +0530763 get_terms: function() {
764 var me = this;
765 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530766 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530767 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530768 args: {
769 doctype: "Terms and Conditions",
770 fieldname: "terms",
771 filters: { name: this.frm.doc.tc_name },
772 },
773 });
774 }
775 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530776
777 taxes_and_charges: function() {
778 var me = this;
779 if(this.frm.doc.taxes_and_charges) {
780 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530781 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
782 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530783 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
784 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530785 "master_name": this.frm.doc.taxes_and_charges,
Nabin Haitdd38a262014-12-26 13:15:21 +0530786 "tax_parentfield": "taxes"
Nabin Hait6b039142014-05-02 15:45:10 +0530787 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530788 callback: function(r) {
789 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530790 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530791 me.calculate_taxes_and_totals();
792 }
793 }
794 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530795 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530796 },
797
798 show_item_wise_taxes: function() {
799 if(this.frm.fields_dict.other_charges_calculation) {
800 $(this.get_item_wise_taxes_html())
801 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
802 }
803 },
Anand Doshi13945092014-09-21 19:45:49 +0530804
805 is_recurring: function() {
806 // set default values for recurring documents
807 if(this.frm.doc.is_recurring) {
808 var owner_email = this.frm.doc.owner=="Administrator"
809 ? frappe.user_info("Administrator").email
810 : this.frm.doc.owner;
811
812 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
813 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
814 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
815 }
816
817 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
818 },
819
820 from_date: function() {
821 // set to_date
822 if(this.frm.doc.from_date) {
823 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
824 'Yearly': 12};
825
826 var months = recurring_type_map[this.frm.doc.recurring_type];
827 if(months) {
828 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
829 months);
830 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
831 refresh_field('to_date');
832 }
833 }
834 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530835});