blob: 6d30ef09998104d23d1f02fdbdb50ff6cada01f4 [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) {
Anand Doshib054eb72014-04-22 20:35:22 +053042 this.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +053043 }
44 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053045
Anand Doshi3543f302013-05-24 19:25:01 +053046 refresh: function() {
47 this.frm.clear_custom_buttons();
Anand Doshi8bde7f92014-04-24 18:11:49 +053048 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053049 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053050 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053051 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053052
53 // Show POS button only if it is enabled from features setup
54 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
Rushabh Mehtae34090a2013-11-15 11:30:23 +053055 this.make_pos_btn();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053056 },
57
Rushabh Mehtae34090a2013-11-15 11:30:23 +053058 make_pos_btn: function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053059 if(!this.pos_active) {
Pratik Vyasb52618c2014-04-14 16:25:30 +053060 var btn_label = __("POS View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053061 icon = "icon-desktop";
62 } else {
Pratik Vyasb52618c2014-04-14 16:25:30 +053063 var btn_label = __(this.frm.doctype) + __(" View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053064 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053065 }
66 var me = this;
Rushabh Mehtaff938022014-04-15 18:40:00 +053067
Rushabh Mehtae34090a2013-11-15 11:30:23 +053068 this.$pos_btn = this.frm.appframe.add_button(btn_label, function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053069 me.toggle_pos();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053070 }, icon);
71 },
72
73 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053074 // Check whether it is Selling or Buying cycle
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053075 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053076 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
Rushabh Mehtaff938022014-04-15 18:40:00 +053077
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053078 if (!price_list)
Pratik Vyasb52618c2014-04-14 16:25:30 +053079 msgprint(__("Please select Price List"))
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053080 else {
81 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
82
83 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053084 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053085 this.frm.layout.add_view("pos");
86 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
87 }
88
89 // toggle view
90 this.frm.layout.set_view(this.pos_active ? "" : "pos");
91 this.pos_active = !this.pos_active;
92
93 // refresh
94 if(this.pos_active)
95 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053096 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053097 }
Anand Doshi3543f302013-05-24 19:25:01 +053098 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053099
100
Nabin Hait139dc7b2014-02-12 14:53:18 +0530101 item_code: function(doc, cdt, cdn) {
102 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530103 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530104 if(item.item_code || item.barcode || item.serial_no) {
105 if(!this.validate_company_and_party()) {
106 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
107 } else {
108 return this.frm.call({
109 method: "erpnext.stock.get_item_details.get_item_details",
110 child: item,
111 args: {
112 args: {
113 item_code: item.item_code,
114 barcode: item.barcode,
115 serial_no: item.serial_no,
116 warehouse: item.warehouse,
117 doctype: me.frm.doc.doctype,
118 docname: me.frm.doc.name,
119 customer: me.frm.doc.customer,
120 supplier: me.frm.doc.supplier,
121 currency: me.frm.doc.currency,
122 conversion_rate: me.frm.doc.conversion_rate,
123 price_list: me.frm.doc.selling_price_list ||
124 me.frm.doc.buying_price_list,
125 price_list_currency: me.frm.doc.price_list_currency,
126 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
127 company: me.frm.doc.company,
128 order_type: me.frm.doc.order_type,
129 is_pos: cint(me.frm.doc.is_pos),
130 is_subcontracted: me.frm.doc.is_subcontracted,
131 transaction_date: me.frm.doc.transaction_date
132 }
133 },
134 callback: function(r) {
135 if(!r.exc) {
136 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
137 }
138 }
139 });
140 }
141 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530142 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530143
144 serial_no: function(doc, cdt, cdn) {
145 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530146 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530147
148 if (item.serial_no) {
149 if (!item.item_code) {
150 this.frm.script_manager.trigger("item_code", cdt, cdn);
151 }
152 else {
153 var sr_no = [];
154
155 // Replacing all occurences of comma with carriage return
156 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
157
158 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530159
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530160 // Trim each string and push unique string to new list
161 for (var x=0; x<=serial_nos.length - 1; x++) {
162 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
163 sr_no.push(serial_nos[x].trim());
164 }
165 }
166
167 // Add the new list to the serial no. field in grid with each in new line
168 item.serial_no = "";
169 for (var x=0; x<=sr_no.length - 1; x++)
170 item.serial_no += sr_no[x] + '\n';
171
172 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530173 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530174 }
175 }
176 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530177
Anand Doshi923d41d2013-05-28 17:23:36 +0530178 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530179 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530180 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530181
Anand Doshi3543f302013-05-24 19:25:01 +0530182 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530183 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530184 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530185 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530186 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530187 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530188
Akhilesh Darjee78378272013-10-11 19:06:30 +0530189 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530190 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530191 }
192 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530193 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530194 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530195
Nabin Hait48a0bd32013-07-23 15:31:39 +0530196 this.frm.script_manager.trigger("currency");
Nabin Haita3dd72a2014-05-28 12:49:20 +0530197 this.apply_pricing_rule()
Anand Doshi3543f302013-05-24 19:25:01 +0530198 }
199 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530200
Anand Doshi3543f302013-05-24 19:25:01 +0530201 get_company_currency: function() {
202 return erpnext.get_currency(this.frm.doc.company);
203 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530204
Anand Doshi3543f302013-05-24 19:25:01 +0530205 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530206 var me = this;
207 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530208
Anand Doshi61a2f682013-06-21 17:55:31 +0530209 var company_currency = this.get_company_currency();
210 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530211 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530212 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530213 me.frm.set_value("conversion_rate", exchange_rate);
214 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530215 });
216 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530217 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530218 }
219 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530220
Anand Doshi61a2f682013-06-21 17:55:31 +0530221 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530222 if(this.frm.doc.currency === this.get_company_currency()) {
223 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530224 }
225 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530226 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
227 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
228 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530229 if(flt(this.frm.doc.conversion_rate)>0.0) {
230 this.apply_pricing_rule();
231 }
Anand Doshi3543f302013-05-24 19:25:01 +0530232 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530233
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530234 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530235 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530236 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
237 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530238 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530239 method: "erpnext.setup.utils.get_price_list_currency",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530240 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530241 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530242 },
Anand Doshi3543f302013-05-24 19:25:01 +0530243 callback: function(r) {
244 if(!r.exc) {
245 me.price_list_currency();
246 }
247 }
248 });
249 }
250 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530251
Anand Doshi61a2f682013-06-21 17:55:31 +0530252 get_exchange_rate: function(from_currency, to_currency, callback) {
253 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530254 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530255 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530256 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530257 });
258 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530259
Anand Doshi3543f302013-05-24 19:25:01 +0530260 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530261 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530262 this.set_dynamic_labels();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530263
Anand Doshi61a2f682013-06-21 17:55:31 +0530264 var company_currency = this.get_company_currency();
265 if(this.frm.doc.price_list_currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530266 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530267 function(exchange_rate) {
268 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530269 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530270 me.plc_conversion_rate();
271 }
272 });
273 } else {
274 this.plc_conversion_rate();
275 }
Anand Doshi3543f302013-05-24 19:25:01 +0530276 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530277
Anand Doshi3543f302013-05-24 19:25:01 +0530278 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530279 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
280 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530281 }
282 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530283 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530284 this.apply_pricing_rule();
Anand Doshi61a2f682013-06-21 17:55:31 +0530285 }
Anand Doshi3543f302013-05-24 19:25:01 +0530286 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530287
Anand Doshi3543f302013-05-24 19:25:01 +0530288 qty: function(doc, cdt, cdn) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530289 this.apply_pricing_rule(frappe.get_doc(cdt, cdn));
Anand Doshi3543f302013-05-24 19:25:01 +0530290 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530291
Anand Doshi652bc072014-04-16 15:21:46 +0530292 // tax rate
293 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530294 this.calculate_taxes_and_totals();
295 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530296
Anand Doshi923d41d2013-05-28 17:23:36 +0530297 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530298 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530299 try {
300 this.validate_on_previous_row(tax);
301 this.calculate_taxes_and_totals();
302 } catch(e) {
303 tax.row_id = null;
304 refresh_field("row_id", tax.name, tax.parentfield);
305 throw e;
306 }
307 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530308
Anand Doshi61a2f682013-06-21 17:55:31 +0530309 set_dynamic_labels: function() {
310 // What TODO? should we make price list system non-mandatory?
311 this.frm.toggle_reqd("plc_conversion_rate",
312 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530313
Anand Doshi61a2f682013-06-21 17:55:31 +0530314 var company_currency = this.get_company_currency();
315 this.change_form_labels(company_currency);
316 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530317 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530318 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530319
Anand Doshi923d41d2013-05-28 17:23:36 +0530320 recalculate: function() {
321 this.calculate_taxes_and_totals();
322 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530323
Anand Doshi923d41d2013-05-28 17:23:36 +0530324 recalculate_values: function() {
325 this.calculate_taxes_and_totals();
326 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530327
Anand Doshid0b00722013-05-28 18:54:48 +0530328 calculate_charges: function() {
329 this.calculate_taxes_and_totals();
330 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530331
Nabin Haita3dd72a2014-05-28 12:49:20 +0530332 apply_pricing_rule: function(item) {
333 var me = this;
334
335 var _apply_pricing_rule = function(item) {
336 return me.frm.call({
337 method: "erpnext.stock.get_item_details.apply_pricing_rule",
338 child: item,
339 args: {
340 args: {
341 item_code: item.item_code,
342 item_group: item.item_group,
343 brand: item.brand,
344 qty: item.qty,
345 customer: me.frm.doc.customer,
346 customer_group: me.frm.doc.customer_group,
347 territory: me.frm.doc.territory,
348 supplier: me.frm.doc.supplier,
349 supplier_type: me.frm.doc.supplier_type,
350 currency: me.frm.doc.currency,
351 conversion_rate: me.frm.doc.conversion_rate,
352 price_list: me.frm.doc.selling_price_list ||
353 me.frm.doc.buying_price_list,
354 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
355 company: me.frm.doc.company,
356 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
357 campaign: me.frm.doc.campaign,
358 sales_partner: me.frm.doc.sales_partner
359 }
360 },
361 callback: function(r) {
362 if(!r.exc) {
363 me.frm.script_manager.trigger("price_list_rate", item.doctype, item.name);
364 }
365 }
366 });
367 }
368
369
Nabin Haitc1367d92014-05-30 11:43:00 +0530370 if(item) {
371 _apply_pricing_rule(item);
372 } else {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530373 $.each(this.get_item_doclist(), function(n, item) {
374 _apply_pricing_rule(item);
375 });
376 }
377 },
378
Anand Doshi3543f302013-05-24 19:25:01 +0530379 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530380 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530381 try {
382 this.validate_on_previous_row(tax);
383 this.validate_inclusive_tax(tax);
384 this.calculate_taxes_and_totals();
385 } catch(e) {
386 tax.included_in_print_rate = 0;
387 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
388 throw e;
389 }
390 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530391
Anand Doshi3543f302013-05-24 19:25:01 +0530392 validate_on_previous_row: function(tax) {
393 // validate if a valid row id is mentioned in case of
394 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530395 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530396 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530397 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 +0530398 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530399 }
400 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530401
Anand Doshi3543f302013-05-24 19:25:01 +0530402 validate_inclusive_tax: function(tax) {
403 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530404
Anand Doshi3543f302013-05-24 19:25:01 +0530405 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530406 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530407 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530408 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530409
Anand Doshi3543f302013-05-24 19:25:01 +0530410 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530411 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
412 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530413 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530414 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530415
Anand Doshi3543f302013-05-24 19:25:01 +0530416 if(cint(tax.included_in_print_rate)) {
417 if(tax.charge_type == "Actual") {
418 // inclusive tax cannot be of type Actual
419 actual_type_error();
420 } else if(tax.charge_type == "On Previous Row Amount" &&
421 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
422 // referred row should also be an inclusive tax
423 on_previous_row_error(tax.row_id);
424 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530425 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530426 function(t) { return cint(t.included_in_print_rate) ? null : t; });
427 if(taxes_not_included.length > 0) {
428 // all rows above this tax should be inclusive
429 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
430 }
431 }
432 }
433 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530434
Anand Doshi3543f302013-05-24 19:25:01 +0530435 _load_item_tax_rate: function(item_tax_rate) {
436 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
437 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530438
Anand Doshi3543f302013-05-24 19:25:01 +0530439 _get_tax_rate: function(tax, item_tax_map) {
440 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
441 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
442 tax.rate;
443 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530444
Anand Doshi3543f302013-05-24 19:25:01 +0530445 get_item_wise_taxes_html: function() {
446 var item_tax = {};
447 var tax_accounts = [];
448 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530449
Anand Doshi3543f302013-05-24 19:25:01 +0530450 $.each(this.get_tax_doclist(), function(i, tax) {
451 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530452 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530453 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530454 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530455 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530456 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530457 var tax_rate = "";
458 if(tax_data[0] != null) {
459 tax_rate = (tax.charge_type === "Actual") ?
460 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
461 (flt(tax_data[0], tax_rate_precision) + "%");
462 }
463 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
464 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530465
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530466 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530467 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530468 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530469 }
Anand Doshi3543f302013-05-24 19:25:01 +0530470 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530471 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530472 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530473
474 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530475 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530476
Anand Doshi179e3772013-09-05 16:53:57 +0530477 var distinct_item_names = [];
478 var distinct_items = [];
479 $.each(this.get_item_doclist(), function(i, item) {
480 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
481 distinct_item_names.push(item.item_code || item.item_name);
482 distinct_items.push(item);
483 }
484 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530485
Anand Doshi179e3772013-09-05 16:53:57 +0530486 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530487 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530488 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530489 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
490 item_name: item.item_name,
491 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530492 return item_tax_record[head[0]] ?
493 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530494 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530495 }).join("\n")
496 });
497 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530498
Anand Doshi53b73422013-05-31 11:50:01 +0530499 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530500 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
501 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530502 <thead><tr>' + headings + '</tr></thead> \
503 <tbody>' + rows + '</tbody> \
504 </table></div>';
505 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530506
Nabin Hait139dc7b2014-02-12 14:53:18 +0530507 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530508 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530509 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530510
Nabin Hait0f231062014-02-20 11:27:47 +0530511 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530512 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530513 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530514 msgprint(__("Please specify") + ": " +
515 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530516 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530517 valid = false;
518 }
Anand Doshi3543f302013-05-24 19:25:01 +0530519 }
520 });
521 return valid;
522 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530523
Anand Doshi3543f302013-05-24 19:25:01 +0530524 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530525 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530526 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530527
Anand Doshi3543f302013-05-24 19:25:01 +0530528 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530529 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530530 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530531
Anand Doshi3543f302013-05-24 19:25:01 +0530532 validate_conversion_rate: function() {
533 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530534 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530535 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530536 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530537
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530538 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530539 frappe.throw(repl('%(conversion_rate_label)s' +
540 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
541 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530542 {
543 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530544 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530545 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530546 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530547 }
Anand Doshi3543f302013-05-24 19:25:01 +0530548 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530549
Anand Doshi3543f302013-05-24 19:25:01 +0530550 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530551 this.discount_amount_applied = false;
552 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530553 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530554 this.apply_discount_amount();
555 },
556
557 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530558 this.validate_conversion_rate();
559 this.frm.item_doclist = this.get_item_doclist();
560 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530561
Anand Doshi3543f302013-05-24 19:25:01 +0530562 this.calculate_item_values();
563 this.initialize_taxes();
564 this.determine_exclusive_rate && this.determine_exclusive_rate();
565 this.calculate_net_total();
566 this.calculate_taxes();
567 this.calculate_totals();
568 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530569
Anand Doshi3543f302013-05-24 19:25:01 +0530570 this.show_item_wise_taxes();
571 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530572
Anand Doshi3543f302013-05-24 19:25:01 +0530573 initialize_taxes: function() {
574 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530575
Anand Doshi3543f302013-05-24 19:25:01 +0530576 $.each(this.frm.tax_doclist, function(i, tax) {
577 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530578 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530579 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530580 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
581
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530582 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530583 tax_fields.push("tax_amount");
584
585 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530586
Anand Doshi3543f302013-05-24 19:25:01 +0530587 me.validate_on_previous_row(tax);
588 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530589 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530590 });
591 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530592
Anand Doshi3543f302013-05-24 19:25:01 +0530593 calculate_taxes: function() {
594 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530595 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530596
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530597 // maintain actual tax rate based on idx
598 $.each(this.frm.tax_doclist, function(i, tax) {
599 if (tax.charge_type == "Actual") {
600 actual_tax_dict[tax.idx] = flt(tax.rate);
601 }
602 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530603
Anand Doshi3543f302013-05-24 19:25:01 +0530604 $.each(this.frm.item_doclist, function(n, item) {
605 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530606
Anand Doshi3543f302013-05-24 19:25:01 +0530607 $.each(me.frm.tax_doclist, function(i, tax) {
608 // tax_amount represents the amount of tax for the current step
609 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530610
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530611 // Adjust divisional loss to the last item
612 if (tax.charge_type == "Actual") {
613 actual_tax_dict[tax.idx] -= current_tax_amount;
614 if (n == me.frm.item_doclist.length - 1) {
615 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530616 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530617 }
618
Anand Doshi3543f302013-05-24 19:25:01 +0530619 // store tax_amount for current item as it will be used for
620 // charge type = 'On Previous Row Amount'
621 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530622
Anand Doshi3543f302013-05-24 19:25:01 +0530623 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530624 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530625 tax.tax_amount += current_tax_amount;
626
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530627 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530628
Anand Doshi3543f302013-05-24 19:25:01 +0530629 // for buying
630 if(tax.category) {
631 // if just for valuation, do not add the tax amount in total
632 // hence, setting it as 0 for further steps
633 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530634
Anand Doshi3543f302013-05-24 19:25:01 +0530635 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
636 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530637
Anand Doshi3543f302013-05-24 19:25:01 +0530638 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530639 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530640 // item's amount, previously applied tax and the current tax on that item
641 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530642 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530643 precision("total", tax));
644 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530645 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530646 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
647 precision("total", tax));
648 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530649
Anand Doshi3543f302013-05-24 19:25:01 +0530650 // in tax.total, accumulate grand total for each item
651 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530652
653 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530654 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530655 me.round_off_totals(tax);
656
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530657 // adjust Discount Amount loss in last tax iteration
658 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
659 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530660 }
Anand Doshi3543f302013-05-24 19:25:01 +0530661 });
662 });
663 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530664
665 round_off_totals: function(tax) {
666 tax.total = flt(tax.total, precision("total", tax));
667 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530668 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530669 precision("tax_amount", tax));
670 },
671
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530672 adjust_discount_amount_loss: function(tax) {
673 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530674 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530675 discount_amount_loss, precision("tax_amount", tax));
676 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530677 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530678
Anand Doshi3543f302013-05-24 19:25:01 +0530679 get_current_tax_amount: function(item, tax, item_tax_map) {
680 var tax_rate = this._get_tax_rate(tax, item_tax_map);
681 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530682
Anand Doshi3543f302013-05-24 19:25:01 +0530683 if(tax.charge_type == "Actual") {
684 // distribute the tax amount proportionally to each item row
685 var actual = flt(tax.rate, precision("tax_amount", tax));
686 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530687 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530688 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530689
Anand Doshi3543f302013-05-24 19:25:01 +0530690 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530691 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530692
Anand Doshi3543f302013-05-24 19:25:01 +0530693 } else if(tax.charge_type == "On Previous Row Amount") {
694 current_tax_amount = (tax_rate / 100.0) *
695 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530696
Anand Doshi3543f302013-05-24 19:25:01 +0530697 } else if(tax.charge_type == "On Previous Row Total") {
698 current_tax_amount = (tax_rate / 100.0) *
699 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530700 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530701
Anand Doshi53b73422013-05-31 11:50:01 +0530702 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530703
Anand Doshi53b73422013-05-31 11:50:01 +0530704 // store tax breakup for each item
705 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530706
Anand Doshi53b73422013-05-31 11:50:01 +0530707 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530708 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530709
Anand Doshi3543f302013-05-24 19:25:01 +0530710 _cleanup: function() {
711 $.each(this.frm.tax_doclist, function(i, tax) {
712 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530713 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530714 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530715
Anand Doshi3543f302013-05-24 19:25:01 +0530716 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
717 });
718 },
Anand Doshifc777182013-05-27 19:29:07 +0530719
Anand Doshi652bc072014-04-16 15:21:46 +0530720 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530721 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530722 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530723 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530724 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
725 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530726
Anand Doshi652bc072014-04-16 15:21:46 +0530727 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530728 }
729 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530730
Anand Doshi3543f302013-05-24 19:25:01 +0530731 _set_in_company_currency: function(item, print_field, base_field) {
732 // set values in base currency
733 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
734 precision(base_field, item));
735 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530736
Anand Doshibf3e54a2013-06-05 14:11:32 +0530737 get_terms: function() {
738 var me = this;
739 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530740 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530741 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530742 args: {
743 doctype: "Terms and Conditions",
744 fieldname: "terms",
745 filters: { name: this.frm.doc.tc_name },
746 },
747 });
748 }
749 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530750
751 taxes_and_charges: function() {
752 var me = this;
753 if(this.frm.doc.taxes_and_charges) {
754 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530755 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
756 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530757 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
758 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530759 "master_name": this.frm.doc.taxes_and_charges,
760 "tax_parentfield": this.other_fname
761 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530762 callback: function(r) {
763 if(!r.exc) {
Nabin Hait6b039142014-05-02 15:45:10 +0530764 me.frm.set_value(me.other_fname, r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530765 me.calculate_taxes_and_totals();
766 }
767 }
768 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530769 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530770 },
771
772 show_item_wise_taxes: function() {
773 if(this.frm.fields_dict.other_charges_calculation) {
774 $(this.get_item_wise_taxes_html())
775 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
776 }
777 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530778});