blob: 067302eba22c80c8ef4a4848b41abd9a38510f77 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Anand Doshi3543f302013-05-24 19:25:01 +05303
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05304frappe.provide("erpnext");
5frappe.require("assets/erpnext/js/controllers/stock_controller.js");
Anand Doshi3543f302013-05-24 19:25:01 +05306
Anand Doshia648f462013-07-30 14:42:15 +05307erpnext.TransactionController = erpnext.stock.StockController.extend({
Anand Doshi3543f302013-05-24 19:25:01 +05308 onload: function() {
Anand Doshi1e4fb682013-08-23 12:56:33 +05309 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +053010 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053011 var today = get_today(),
Nabin Hait73083dc2014-03-14 15:10:42 +053012 currency = frappe.defaults.get_user_default("currency");
Rushabh Mehtaff938022014-04-15 18:40:00 +053013
Anand Doshi3543f302013-05-24 19:25:01 +053014 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053015 posting_date: today,
16 due_date: today,
17 transaction_date: today,
18 currency: currency,
19 price_list_currency: currency,
20 status: "Draft",
Nabin Hait73083dc2014-03-14 15:10:42 +053021 company: frappe.defaults.get_user_default("company"),
22 fiscal_year: frappe.defaults.get_user_default("fiscal_year"),
Anand Doshifc777182013-05-27 19:29:07 +053023 is_subcontracted: "No",
Anand Doshi3543f302013-05-24 19:25:01 +053024 }, function(fieldname, value) {
25 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
26 me.frm.set_value(fieldname, value);
Rushabh Mehtaff938022014-04-15 18:40:00 +053027 });
Anand Doshi3543f302013-05-24 19:25:01 +053028 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +053029
Anand Doshi1e4fb682013-08-23 12:56:33 +053030 if(this.other_fname) {
Anand Doshia91c95f2013-08-23 13:00:47 +053031 this[this.other_fname + "_remove"] = this.calculate_taxes_and_totals;
32 }
Rushabh Mehtaff938022014-04-15 18:40:00 +053033
Anand Doshia91c95f2013-08-23 13:00:47 +053034 if(this.fname) {
35 this[this.fname + "_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +053036 }
Anand Doshi3543f302013-05-24 19:25:01 +053037 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053038
Anand Doshi5e4e5d72013-08-06 17:23:44 +053039 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053040 var me = this;
41 if(this.frm.doc.__islocal && this.frm.doc.company && !this.frm.doc.is_pos) {
Akhilesh Darjee04638a52013-10-17 13:19:46 +053042 if(!this.frm.doc.customer || !this.frm.doc.supplier) {
Akhilesh Darjee78378272013-10-11 19:06:30 +053043 return this.frm.call({
44 doc: this.frm.doc,
45 method: "onload_post_render",
46 freeze: true,
47 callback: function(r) {
48 // remove this call when using client side mapper
Akhilesh Darjee78378272013-10-11 19:06:30 +053049 me.set_dynamic_labels();
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +053050 me.calculate_taxes_and_totals();
Akhilesh Darjee78378272013-10-11 19:06:30 +053051 }
52 });
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053053 } else {
54 this.calculate_taxes_and_totals();
55 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053056 }
57 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053058
Anand Doshi3543f302013-05-24 19:25:01 +053059 refresh: function() {
60 this.frm.clear_custom_buttons();
61 erpnext.hide_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053062 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053063 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053064 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053065
66 // Show POS button only if it is enabled from features setup
67 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
Rushabh Mehtae34090a2013-11-15 11:30:23 +053068 this.make_pos_btn();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053069 },
70
Rushabh Mehtae34090a2013-11-15 11:30:23 +053071 make_pos_btn: function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053072 if(!this.pos_active) {
Pratik Vyasb52618c2014-04-14 16:25:30 +053073 var btn_label = __("POS View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053074 icon = "icon-desktop";
75 } else {
Pratik Vyasb52618c2014-04-14 16:25:30 +053076 var btn_label = __(this.frm.doctype) + __(" View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053077 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053078 }
79 var me = this;
Rushabh Mehtaff938022014-04-15 18:40:00 +053080
Rushabh Mehtae34090a2013-11-15 11:30:23 +053081 this.$pos_btn = this.frm.appframe.add_button(btn_label, function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053082 me.toggle_pos();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053083 }, icon);
84 },
85
86 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053087 // Check whether it is Selling or Buying cycle
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053088 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053089 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
Rushabh Mehtaff938022014-04-15 18:40:00 +053090
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053091 if (!price_list)
Pratik Vyasb52618c2014-04-14 16:25:30 +053092 msgprint(__("Please select Price List"))
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053093 else {
94 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
95
96 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053097 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053098 this.frm.layout.add_view("pos");
99 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
100 }
101
102 // toggle view
103 this.frm.layout.set_view(this.pos_active ? "" : "pos");
104 this.pos_active = !this.pos_active;
105
106 // refresh
107 if(this.pos_active)
108 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530109 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530110 }
Anand Doshi3543f302013-05-24 19:25:01 +0530111 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530112
113
Nabin Hait139dc7b2014-02-12 14:53:18 +0530114 item_code: function(doc, cdt, cdn) {
115 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530116 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530117 if(item.item_code || item.barcode || item.serial_no) {
118 if(!this.validate_company_and_party()) {
119 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
120 } else {
121 return this.frm.call({
122 method: "erpnext.stock.get_item_details.get_item_details",
123 child: item,
124 args: {
125 args: {
126 item_code: item.item_code,
127 barcode: item.barcode,
128 serial_no: item.serial_no,
129 warehouse: item.warehouse,
130 doctype: me.frm.doc.doctype,
131 docname: me.frm.doc.name,
132 customer: me.frm.doc.customer,
133 supplier: me.frm.doc.supplier,
134 currency: me.frm.doc.currency,
135 conversion_rate: me.frm.doc.conversion_rate,
136 price_list: me.frm.doc.selling_price_list ||
137 me.frm.doc.buying_price_list,
138 price_list_currency: me.frm.doc.price_list_currency,
139 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
140 company: me.frm.doc.company,
141 order_type: me.frm.doc.order_type,
142 is_pos: cint(me.frm.doc.is_pos),
143 is_subcontracted: me.frm.doc.is_subcontracted,
144 transaction_date: me.frm.doc.transaction_date
145 }
146 },
147 callback: function(r) {
148 if(!r.exc) {
149 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
150 }
151 }
152 });
153 }
154 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530155 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530156
157 serial_no: function(doc, cdt, cdn) {
158 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530159 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530160
161 if (item.serial_no) {
162 if (!item.item_code) {
163 this.frm.script_manager.trigger("item_code", cdt, cdn);
164 }
165 else {
166 var sr_no = [];
167
168 // Replacing all occurences of comma with carriage return
169 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
170
171 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530172
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530173 // Trim each string and push unique string to new list
174 for (var x=0; x<=serial_nos.length - 1; x++) {
175 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
176 sr_no.push(serial_nos[x].trim());
177 }
178 }
179
180 // Add the new list to the serial no. field in grid with each in new line
181 item.serial_no = "";
182 for (var x=0; x<=sr_no.length - 1; x++)
183 item.serial_no += sr_no[x] + '\n';
184
185 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530186 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530187 }
188 }
189 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530190
Anand Doshi923d41d2013-05-28 17:23:36 +0530191 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530192 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530193 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530194
Anand Doshi3543f302013-05-24 19:25:01 +0530195 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530196 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530197 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530198 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530199 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530200 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530201
Akhilesh Darjee78378272013-10-11 19:06:30 +0530202 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530203 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530204 }
205 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530206 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530207 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530208
Nabin Hait48a0bd32013-07-23 15:31:39 +0530209 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +0530210 }
211 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530212
Anand Doshi3543f302013-05-24 19:25:01 +0530213 get_company_currency: function() {
214 return erpnext.get_currency(this.frm.doc.company);
215 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530216
Anand Doshi3543f302013-05-24 19:25:01 +0530217 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530218 var me = this;
219 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530220
Anand Doshi61a2f682013-06-21 17:55:31 +0530221 var company_currency = this.get_company_currency();
222 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530223 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530224 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530225 me.frm.set_value("conversion_rate", exchange_rate);
226 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530227 });
228 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530229 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530230 }
231 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530232
Anand Doshi61a2f682013-06-21 17:55:31 +0530233 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530234 if(this.frm.doc.currency === this.get_company_currency()) {
235 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530236 }
237 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530238 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
239 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
240 }
Nabin Haitb338b6e2013-10-15 16:55:24 +0530241 if(flt(this.frm.doc.conversion_rate)>0.0) this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530242 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530243
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530244 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530245 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530246 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
247 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530248 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530249 method: "erpnext.setup.utils.get_price_list_currency",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530250 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530251 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530252 },
Anand Doshi3543f302013-05-24 19:25:01 +0530253 callback: function(r) {
254 if(!r.exc) {
255 me.price_list_currency();
256 }
257 }
258 });
259 }
260 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530261
Anand Doshi61a2f682013-06-21 17:55:31 +0530262 get_exchange_rate: function(from_currency, to_currency, callback) {
263 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530264 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530265 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530266 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530267 });
268 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530269
Anand Doshi3543f302013-05-24 19:25:01 +0530270 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530271 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530272 this.set_dynamic_labels();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530273
Anand Doshi61a2f682013-06-21 17:55:31 +0530274 var company_currency = this.get_company_currency();
275 if(this.frm.doc.price_list_currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530276 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530277 function(exchange_rate) {
278 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530279 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530280 me.plc_conversion_rate();
281 }
282 });
283 } else {
284 this.plc_conversion_rate();
285 }
Anand Doshi3543f302013-05-24 19:25:01 +0530286 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530287
Anand Doshi3543f302013-05-24 19:25:01 +0530288 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530289 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
290 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530291 }
292 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530293 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
294 this.calculate_taxes_and_totals();
295 }
Anand Doshi3543f302013-05-24 19:25:01 +0530296 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530297
Anand Doshi3543f302013-05-24 19:25:01 +0530298 qty: function(doc, cdt, cdn) {
299 this.calculate_taxes_and_totals();
300 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530301
Anand Doshi652bc072014-04-16 15:21:46 +0530302 // tax rate
303 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530304 this.calculate_taxes_and_totals();
305 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530306
Anand Doshi923d41d2013-05-28 17:23:36 +0530307 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530308 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530309 try {
310 this.validate_on_previous_row(tax);
311 this.calculate_taxes_and_totals();
312 } catch(e) {
313 tax.row_id = null;
314 refresh_field("row_id", tax.name, tax.parentfield);
315 throw e;
316 }
317 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530318
Anand Doshi61a2f682013-06-21 17:55:31 +0530319 set_dynamic_labels: function() {
320 // What TODO? should we make price list system non-mandatory?
321 this.frm.toggle_reqd("plc_conversion_rate",
322 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530323
Anand Doshi61a2f682013-06-21 17:55:31 +0530324 var company_currency = this.get_company_currency();
325 this.change_form_labels(company_currency);
326 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530327 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530328 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530329
Anand Doshi923d41d2013-05-28 17:23:36 +0530330 recalculate: function() {
331 this.calculate_taxes_and_totals();
332 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530333
Anand Doshi923d41d2013-05-28 17:23:36 +0530334 recalculate_values: function() {
335 this.calculate_taxes_and_totals();
336 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530337
Anand Doshid0b00722013-05-28 18:54:48 +0530338 calculate_charges: function() {
339 this.calculate_taxes_and_totals();
340 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530341
Anand Doshi3543f302013-05-24 19:25:01 +0530342 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530343 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530344 try {
345 this.validate_on_previous_row(tax);
346 this.validate_inclusive_tax(tax);
347 this.calculate_taxes_and_totals();
348 } catch(e) {
349 tax.included_in_print_rate = 0;
350 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
351 throw e;
352 }
353 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530354
Anand Doshi3543f302013-05-24 19:25:01 +0530355 validate_on_previous_row: function(tax) {
356 // validate if a valid row id is mentioned in case of
357 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530358 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530359 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530360 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 +0530361 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530362 }
363 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530364
Anand Doshi3543f302013-05-24 19:25:01 +0530365 validate_inclusive_tax: function(tax) {
366 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530367
Anand Doshi3543f302013-05-24 19:25:01 +0530368 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530369 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530370 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530371 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530372
Anand Doshi3543f302013-05-24 19:25:01 +0530373 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530374 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
375 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530376 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530377 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530378
Anand Doshi3543f302013-05-24 19:25:01 +0530379 if(cint(tax.included_in_print_rate)) {
380 if(tax.charge_type == "Actual") {
381 // inclusive tax cannot be of type Actual
382 actual_type_error();
383 } else if(tax.charge_type == "On Previous Row Amount" &&
384 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
385 // referred row should also be an inclusive tax
386 on_previous_row_error(tax.row_id);
387 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530388 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530389 function(t) { return cint(t.included_in_print_rate) ? null : t; });
390 if(taxes_not_included.length > 0) {
391 // all rows above this tax should be inclusive
392 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
393 }
394 }
395 }
396 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530397
Anand Doshi3543f302013-05-24 19:25:01 +0530398 _load_item_tax_rate: function(item_tax_rate) {
399 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
400 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530401
Anand Doshi3543f302013-05-24 19:25:01 +0530402 _get_tax_rate: function(tax, item_tax_map) {
403 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
404 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
405 tax.rate;
406 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530407
Anand Doshi3543f302013-05-24 19:25:01 +0530408 get_item_wise_taxes_html: function() {
409 var item_tax = {};
410 var tax_accounts = [];
411 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530412
Anand Doshi3543f302013-05-24 19:25:01 +0530413 $.each(this.get_tax_doclist(), function(i, tax) {
414 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530415 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530416 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530417 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530418 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530419 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530420 var tax_rate = "";
421 if(tax_data[0] != null) {
422 tax_rate = (tax.charge_type === "Actual") ?
423 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
424 (flt(tax_data[0], tax_rate_precision) + "%");
425 }
426 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
427 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530428
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530429 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530430 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530431 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530432 }
Anand Doshi3543f302013-05-24 19:25:01 +0530433 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530434 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530435 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530436
437 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530438 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530439
Anand Doshi179e3772013-09-05 16:53:57 +0530440 var distinct_item_names = [];
441 var distinct_items = [];
442 $.each(this.get_item_doclist(), function(i, item) {
443 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
444 distinct_item_names.push(item.item_code || item.item_name);
445 distinct_items.push(item);
446 }
447 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530448
Anand Doshi179e3772013-09-05 16:53:57 +0530449 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530450 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530451 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530452 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
453 item_name: item.item_name,
454 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530455 return item_tax_record[head[0]] ?
456 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530457 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530458 }).join("\n")
459 });
460 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530461
Anand Doshi53b73422013-05-31 11:50:01 +0530462 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530463 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
464 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530465 <thead><tr>' + headings + '</tr></thead> \
466 <tbody>' + rows + '</tbody> \
467 </table></div>';
468 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530469
Nabin Hait139dc7b2014-02-12 14:53:18 +0530470 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530471 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530472 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530473
Nabin Hait0f231062014-02-20 11:27:47 +0530474 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530475 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530476 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530477 msgprint(__("Please specify") + ": " +
478 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530479 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530480 valid = false;
481 }
Anand Doshi3543f302013-05-24 19:25:01 +0530482 }
483 });
484 return valid;
485 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530486
Anand Doshi3543f302013-05-24 19:25:01 +0530487 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530488 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530489 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530490
Anand Doshi3543f302013-05-24 19:25:01 +0530491 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530492 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530493 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530494
Anand Doshi3543f302013-05-24 19:25:01 +0530495 validate_conversion_rate: function() {
496 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530497 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530498 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530499 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530500
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530501 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530502 frappe.throw(repl('%(conversion_rate_label)s' +
503 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
504 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530505 {
506 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530507 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530508 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530509 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530510 }
Anand Doshi3543f302013-05-24 19:25:01 +0530511 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530512
Anand Doshi3543f302013-05-24 19:25:01 +0530513 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530514 this.discount_amount_applied = false;
515 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530516 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530517 this.apply_discount_amount();
518 },
519
520 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530521 this.validate_conversion_rate();
522 this.frm.item_doclist = this.get_item_doclist();
523 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530524
Anand Doshi3543f302013-05-24 19:25:01 +0530525 this.calculate_item_values();
526 this.initialize_taxes();
527 this.determine_exclusive_rate && this.determine_exclusive_rate();
528 this.calculate_net_total();
529 this.calculate_taxes();
530 this.calculate_totals();
531 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530532
Anand Doshi3543f302013-05-24 19:25:01 +0530533 this.show_item_wise_taxes();
534 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530535
Anand Doshi3543f302013-05-24 19:25:01 +0530536 initialize_taxes: function() {
537 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530538
Anand Doshi3543f302013-05-24 19:25:01 +0530539 $.each(this.frm.tax_doclist, function(i, tax) {
540 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530541 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530542 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530543 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
544
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530545 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530546 tax_fields.push("tax_amount");
547
548 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530549
Anand Doshi3543f302013-05-24 19:25:01 +0530550 me.validate_on_previous_row(tax);
551 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530552 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530553 });
554 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530555
Anand Doshi3543f302013-05-24 19:25:01 +0530556 calculate_taxes: function() {
557 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530558 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530559
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530560 // maintain actual tax rate based on idx
561 $.each(this.frm.tax_doclist, function(i, tax) {
562 if (tax.charge_type == "Actual") {
563 actual_tax_dict[tax.idx] = flt(tax.rate);
564 }
565 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530566
Anand Doshi3543f302013-05-24 19:25:01 +0530567 $.each(this.frm.item_doclist, function(n, item) {
568 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530569
Anand Doshi3543f302013-05-24 19:25:01 +0530570 $.each(me.frm.tax_doclist, function(i, tax) {
571 // tax_amount represents the amount of tax for the current step
572 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530573
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530574 // Adjust divisional loss to the last item
575 if (tax.charge_type == "Actual") {
576 actual_tax_dict[tax.idx] -= current_tax_amount;
577 if (n == me.frm.item_doclist.length - 1) {
578 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530579 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530580 }
581
Anand Doshi3543f302013-05-24 19:25:01 +0530582 // store tax_amount for current item as it will be used for
583 // charge type = 'On Previous Row Amount'
584 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530585
Anand Doshi3543f302013-05-24 19:25:01 +0530586 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530587 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530588 tax.tax_amount += current_tax_amount;
589
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530590 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530591
Anand Doshi3543f302013-05-24 19:25:01 +0530592 // for buying
593 if(tax.category) {
594 // if just for valuation, do not add the tax amount in total
595 // hence, setting it as 0 for further steps
596 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530597
Anand Doshi3543f302013-05-24 19:25:01 +0530598 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
599 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530600
Anand Doshi3543f302013-05-24 19:25:01 +0530601 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530602 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530603 // item's amount, previously applied tax and the current tax on that item
604 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530605 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530606 precision("total", tax));
607 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530608 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530609 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
610 precision("total", tax));
611 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530612
Anand Doshi3543f302013-05-24 19:25:01 +0530613 // in tax.total, accumulate grand total for each item
614 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530615
616 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530617 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530618 me.round_off_totals(tax);
619
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530620 // adjust Discount Amount loss in last tax iteration
621 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
622 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530623 }
Anand Doshi3543f302013-05-24 19:25:01 +0530624 });
625 });
626 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530627
628 round_off_totals: function(tax) {
629 tax.total = flt(tax.total, precision("total", tax));
630 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530631 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530632 precision("tax_amount", tax));
633 },
634
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530635 adjust_discount_amount_loss: function(tax) {
636 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530637 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530638 discount_amount_loss, precision("tax_amount", tax));
639 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530640 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530641
Anand Doshi3543f302013-05-24 19:25:01 +0530642 get_current_tax_amount: function(item, tax, item_tax_map) {
643 var tax_rate = this._get_tax_rate(tax, item_tax_map);
644 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530645
Anand Doshi3543f302013-05-24 19:25:01 +0530646 if(tax.charge_type == "Actual") {
647 // distribute the tax amount proportionally to each item row
648 var actual = flt(tax.rate, precision("tax_amount", tax));
649 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530650 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530651 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530652
Anand Doshi3543f302013-05-24 19:25:01 +0530653 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530654 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530655
Anand Doshi3543f302013-05-24 19:25:01 +0530656 } else if(tax.charge_type == "On Previous Row Amount") {
657 current_tax_amount = (tax_rate / 100.0) *
658 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530659
Anand Doshi3543f302013-05-24 19:25:01 +0530660 } else if(tax.charge_type == "On Previous Row Total") {
661 current_tax_amount = (tax_rate / 100.0) *
662 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530663 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530664
Anand Doshi53b73422013-05-31 11:50:01 +0530665 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530666
Anand Doshi53b73422013-05-31 11:50:01 +0530667 // store tax breakup for each item
668 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530669
Anand Doshi53b73422013-05-31 11:50:01 +0530670 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530671 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530672
Anand Doshi3543f302013-05-24 19:25:01 +0530673 _cleanup: function() {
674 $.each(this.frm.tax_doclist, function(i, tax) {
675 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530676 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530677 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530678
Anand Doshi3543f302013-05-24 19:25:01 +0530679 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
680 });
681 },
Anand Doshifc777182013-05-27 19:29:07 +0530682
Anand Doshi652bc072014-04-16 15:21:46 +0530683 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530684 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530685 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530686 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530687 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
688 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530689
Anand Doshi652bc072014-04-16 15:21:46 +0530690 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530691 }
692 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530693
Anand Doshi3543f302013-05-24 19:25:01 +0530694 _set_in_company_currency: function(item, print_field, base_field) {
695 // set values in base currency
696 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
697 precision(base_field, item));
698 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530699
Anand Doshibf3e54a2013-06-05 14:11:32 +0530700 get_terms: function() {
701 var me = this;
702 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530703 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530704 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530705 args: {
706 doctype: "Terms and Conditions",
707 fieldname: "terms",
708 filters: { name: this.frm.doc.tc_name },
709 },
710 });
711 }
712 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530713
714 taxes_and_charges: function() {
715 var me = this;
716 if(this.frm.doc.taxes_and_charges) {
717 return this.frm.call({
718 doc: this.frm.doc,
Anand Doshiac32bad2014-04-18 01:30:14 +0530719 method: "set_other_charges",
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530720 callback: function(r) {
721 if(!r.exc) {
722 me.calculate_taxes_and_totals();
723 }
724 }
725 });
726 }
727 },
728
729 show_item_wise_taxes: function() {
730 if(this.frm.fields_dict.other_charges_calculation) {
731 $(this.get_item_wise_taxes_html())
732 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
733 }
734 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530735});