blob: 2c372042eab3ee361979e6043794003c8a6a1755 [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;
Anand Doshi221dec82014-06-03 14:12:49 +053041 if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc[this.fname] && !this.frm.doc.is_pos) {
Anand Doshib054eb72014-04-22 20:35:22 +053042 this.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +053043 }
Rushabh Mehtab6843f22014-06-04 13:10:41 +053044 if(frappe.meta.get_docfield(this.tname, "item_code")) {
Rushabh Mehta880b3ef2014-06-06 11:45:11 +053045 cur_frm.get_field(this.fname).grid.set_multiple_add("item_code", "qty");
Rushabh Mehtab6843f22014-06-04 13:10:41 +053046 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053047 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053048
Anand Doshi3543f302013-05-24 19:25:01 +053049 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +053050 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053051 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053052 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053053 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053054
55 // Show POS button only if it is enabled from features setup
56 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
Rushabh Mehtae34090a2013-11-15 11:30:23 +053057 this.make_pos_btn();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053058 },
59
Rushabh Mehtae34090a2013-11-15 11:30:23 +053060 make_pos_btn: function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053061 if(!this.pos_active) {
Pratik Vyasb52618c2014-04-14 16:25:30 +053062 var btn_label = __("POS View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053063 icon = "icon-desktop";
64 } else {
Pratik Vyasb52618c2014-04-14 16:25:30 +053065 var btn_label = __(this.frm.doctype) + __(" View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053066 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053067 }
68 var me = this;
Rushabh Mehtaff938022014-04-15 18:40:00 +053069
Rushabh Mehtae34090a2013-11-15 11:30:23 +053070 this.$pos_btn = this.frm.appframe.add_button(btn_label, function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053071 me.toggle_pos();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053072 }, icon);
73 },
74
75 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053076 // Check whether it is Selling or Buying cycle
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053077 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053078 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
Rushabh Mehtaff938022014-04-15 18:40:00 +053079
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053080 if (!price_list)
Pratik Vyasb52618c2014-04-14 16:25:30 +053081 msgprint(__("Please select Price List"))
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053082 else {
83 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
84
85 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053086 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053087 this.frm.layout.add_view("pos");
88 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
89 }
90
91 // toggle view
92 this.frm.layout.set_view(this.pos_active ? "" : "pos");
93 this.pos_active = !this.pos_active;
94
95 // refresh
96 if(this.pos_active)
97 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053098 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053099 }
Anand Doshi3543f302013-05-24 19:25:01 +0530100 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530101
102
Nabin Hait139dc7b2014-02-12 14:53:18 +0530103 item_code: function(doc, cdt, cdn) {
104 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530105 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530106 if(item.item_code || item.barcode || item.serial_no) {
107 if(!this.validate_company_and_party()) {
108 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
109 } else {
110 return this.frm.call({
111 method: "erpnext.stock.get_item_details.get_item_details",
112 child: item,
113 args: {
114 args: {
115 item_code: item.item_code,
116 barcode: item.barcode,
117 serial_no: item.serial_no,
118 warehouse: item.warehouse,
119 doctype: me.frm.doc.doctype,
120 docname: me.frm.doc.name,
121 customer: me.frm.doc.customer,
122 supplier: me.frm.doc.supplier,
123 currency: me.frm.doc.currency,
124 conversion_rate: me.frm.doc.conversion_rate,
125 price_list: me.frm.doc.selling_price_list ||
126 me.frm.doc.buying_price_list,
127 price_list_currency: me.frm.doc.price_list_currency,
128 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
129 company: me.frm.doc.company,
130 order_type: me.frm.doc.order_type,
131 is_pos: cint(me.frm.doc.is_pos),
132 is_subcontracted: me.frm.doc.is_subcontracted,
133 transaction_date: me.frm.doc.transaction_date
134 }
135 },
136 callback: function(r) {
137 if(!r.exc) {
138 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
139 }
140 }
141 });
142 }
143 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530144 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530145
146 serial_no: function(doc, cdt, cdn) {
147 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530148 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530149
150 if (item.serial_no) {
151 if (!item.item_code) {
152 this.frm.script_manager.trigger("item_code", cdt, cdn);
153 }
154 else {
155 var sr_no = [];
156
157 // Replacing all occurences of comma with carriage return
158 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
159
160 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530161
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530162 // Trim each string and push unique string to new list
163 for (var x=0; x<=serial_nos.length - 1; x++) {
164 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
165 sr_no.push(serial_nos[x].trim());
166 }
167 }
168
169 // Add the new list to the serial no. field in grid with each in new line
170 item.serial_no = "";
171 for (var x=0; x<=sr_no.length - 1; x++)
172 item.serial_no += sr_no[x] + '\n';
173
174 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530175 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530176 }
177 }
178 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530179
Anand Doshi923d41d2013-05-28 17:23:36 +0530180 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530181 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530182 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530183
Anand Doshi3543f302013-05-24 19:25:01 +0530184 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530185 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530186 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530187 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530188 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530189 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530190
Akhilesh Darjee78378272013-10-11 19:06:30 +0530191 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530192 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530193 }
194 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530195 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530196 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530197
Nabin Hait48a0bd32013-07-23 15:31:39 +0530198 this.frm.script_manager.trigger("currency");
Nabin Haita3dd72a2014-05-28 12:49:20 +0530199 this.apply_pricing_rule()
Anand Doshi3543f302013-05-24 19:25:01 +0530200 }
201 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530202
Anand Doshi3543f302013-05-24 19:25:01 +0530203 get_company_currency: function() {
204 return erpnext.get_currency(this.frm.doc.company);
205 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530206
Anand Doshi3543f302013-05-24 19:25:01 +0530207 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530208 var me = this;
209 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530210
Anand Doshi61a2f682013-06-21 17:55:31 +0530211 var company_currency = this.get_company_currency();
212 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530213 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530214 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530215 me.frm.set_value("conversion_rate", exchange_rate);
216 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530217 });
218 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530219 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530220 }
221 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530222
Anand Doshi61a2f682013-06-21 17:55:31 +0530223 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530224 if(this.frm.doc.currency === this.get_company_currency()) {
225 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530226 }
227 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530228 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
229 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
230 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530231 if(flt(this.frm.doc.conversion_rate)>0.0) {
232 this.apply_pricing_rule();
233 }
Anand Doshi3543f302013-05-24 19:25:01 +0530234 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530235
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530236 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530237 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530238 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
239 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530240 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530241 method: "erpnext.setup.utils.get_price_list_currency",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530242 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530243 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530244 },
Anand Doshi3543f302013-05-24 19:25:01 +0530245 callback: function(r) {
246 if(!r.exc) {
247 me.price_list_currency();
248 }
249 }
250 });
251 }
252 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530253
Anand Doshi61a2f682013-06-21 17:55:31 +0530254 get_exchange_rate: function(from_currency, to_currency, callback) {
255 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530256 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530257 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530258 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530259 });
260 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530261
Anand Doshi3543f302013-05-24 19:25:01 +0530262 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530263 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530264 this.set_dynamic_labels();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530265
Anand Doshi61a2f682013-06-21 17:55:31 +0530266 var company_currency = this.get_company_currency();
267 if(this.frm.doc.price_list_currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530268 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530269 function(exchange_rate) {
270 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530271 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530272 me.plc_conversion_rate();
273 }
274 });
275 } else {
276 this.plc_conversion_rate();
277 }
Anand Doshi3543f302013-05-24 19:25:01 +0530278 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530279
Anand Doshi3543f302013-05-24 19:25:01 +0530280 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530281 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
282 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530283 }
284 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530285 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530286 this.apply_pricing_rule();
Anand Doshi61a2f682013-06-21 17:55:31 +0530287 }
Anand Doshi3543f302013-05-24 19:25:01 +0530288 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530289
Anand Doshi3543f302013-05-24 19:25:01 +0530290 qty: function(doc, cdt, cdn) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530291 this.apply_pricing_rule(frappe.get_doc(cdt, cdn));
Anand Doshi3543f302013-05-24 19:25:01 +0530292 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530293
Anand Doshi652bc072014-04-16 15:21:46 +0530294 // tax rate
295 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530296 this.calculate_taxes_and_totals();
297 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530298
Anand Doshi923d41d2013-05-28 17:23:36 +0530299 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530300 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530301 try {
302 this.validate_on_previous_row(tax);
303 this.calculate_taxes_and_totals();
304 } catch(e) {
305 tax.row_id = null;
306 refresh_field("row_id", tax.name, tax.parentfield);
307 throw e;
308 }
309 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530310
Anand Doshi61a2f682013-06-21 17:55:31 +0530311 set_dynamic_labels: function() {
312 // What TODO? should we make price list system non-mandatory?
313 this.frm.toggle_reqd("plc_conversion_rate",
314 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530315
Anand Doshi61a2f682013-06-21 17:55:31 +0530316 var company_currency = this.get_company_currency();
317 this.change_form_labels(company_currency);
318 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530319 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530320 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530321
Anand Doshi923d41d2013-05-28 17:23:36 +0530322 recalculate: function() {
323 this.calculate_taxes_and_totals();
324 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530325
Anand Doshi923d41d2013-05-28 17:23:36 +0530326 recalculate_values: function() {
327 this.calculate_taxes_and_totals();
328 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530329
Anand Doshid0b00722013-05-28 18:54:48 +0530330 calculate_charges: function() {
331 this.calculate_taxes_and_totals();
332 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530333
Nabin Haita3dd72a2014-05-28 12:49:20 +0530334 apply_pricing_rule: function(item) {
335 var me = this;
336
337 var _apply_pricing_rule = function(item) {
338 return me.frm.call({
339 method: "erpnext.stock.get_item_details.apply_pricing_rule",
340 child: item,
341 args: {
342 args: {
343 item_code: item.item_code,
344 item_group: item.item_group,
345 brand: item.brand,
346 qty: item.qty,
347 customer: me.frm.doc.customer,
348 customer_group: me.frm.doc.customer_group,
349 territory: me.frm.doc.territory,
350 supplier: me.frm.doc.supplier,
351 supplier_type: me.frm.doc.supplier_type,
352 currency: me.frm.doc.currency,
353 conversion_rate: me.frm.doc.conversion_rate,
354 price_list: me.frm.doc.selling_price_list ||
355 me.frm.doc.buying_price_list,
356 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
357 company: me.frm.doc.company,
358 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
359 campaign: me.frm.doc.campaign,
360 sales_partner: me.frm.doc.sales_partner
361 }
362 },
363 callback: function(r) {
364 if(!r.exc) {
365 me.frm.script_manager.trigger("price_list_rate", item.doctype, item.name);
366 }
367 }
368 });
369 }
370
371
Nabin Haitc1367d92014-05-30 11:43:00 +0530372 if(item) {
373 _apply_pricing_rule(item);
374 } else {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530375 $.each(this.get_item_doclist(), function(n, item) {
376 _apply_pricing_rule(item);
377 });
378 }
379 },
380
Anand Doshi3543f302013-05-24 19:25:01 +0530381 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530382 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530383 try {
384 this.validate_on_previous_row(tax);
385 this.validate_inclusive_tax(tax);
386 this.calculate_taxes_and_totals();
387 } catch(e) {
388 tax.included_in_print_rate = 0;
389 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
390 throw e;
391 }
392 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530393
Anand Doshi3543f302013-05-24 19:25:01 +0530394 validate_on_previous_row: function(tax) {
395 // validate if a valid row id is mentioned in case of
396 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530397 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530398 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530399 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 +0530400 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530401 }
402 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530403
Anand Doshi3543f302013-05-24 19:25:01 +0530404 validate_inclusive_tax: function(tax) {
405 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530406
Anand Doshi3543f302013-05-24 19:25:01 +0530407 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530408 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530409 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530410 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530411
Anand Doshi3543f302013-05-24 19:25:01 +0530412 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530413 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
414 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530415 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530416 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530417
Anand Doshi3543f302013-05-24 19:25:01 +0530418 if(cint(tax.included_in_print_rate)) {
419 if(tax.charge_type == "Actual") {
420 // inclusive tax cannot be of type Actual
421 actual_type_error();
422 } else if(tax.charge_type == "On Previous Row Amount" &&
423 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
424 // referred row should also be an inclusive tax
425 on_previous_row_error(tax.row_id);
426 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530427 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530428 function(t) { return cint(t.included_in_print_rate) ? null : t; });
429 if(taxes_not_included.length > 0) {
430 // all rows above this tax should be inclusive
431 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
432 }
433 }
434 }
435 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530436
Anand Doshi3543f302013-05-24 19:25:01 +0530437 _load_item_tax_rate: function(item_tax_rate) {
438 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
439 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530440
Anand Doshi3543f302013-05-24 19:25:01 +0530441 _get_tax_rate: function(tax, item_tax_map) {
442 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
443 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
444 tax.rate;
445 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530446
Anand Doshi3543f302013-05-24 19:25:01 +0530447 get_item_wise_taxes_html: function() {
448 var item_tax = {};
449 var tax_accounts = [];
450 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530451
Anand Doshi3543f302013-05-24 19:25:01 +0530452 $.each(this.get_tax_doclist(), function(i, tax) {
453 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530454 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530455 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530456 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530457 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530458 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530459 var tax_rate = "";
460 if(tax_data[0] != null) {
461 tax_rate = (tax.charge_type === "Actual") ?
462 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
463 (flt(tax_data[0], tax_rate_precision) + "%");
464 }
465 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
466 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530467
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530468 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530469 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530470 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530471 }
Anand Doshi3543f302013-05-24 19:25:01 +0530472 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530473 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530474 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530475
476 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530477 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530478
Anand Doshi179e3772013-09-05 16:53:57 +0530479 var distinct_item_names = [];
480 var distinct_items = [];
481 $.each(this.get_item_doclist(), function(i, item) {
482 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
483 distinct_item_names.push(item.item_code || item.item_name);
484 distinct_items.push(item);
485 }
486 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530487
Anand Doshi179e3772013-09-05 16:53:57 +0530488 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530489 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530490 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530491 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
492 item_name: item.item_name,
493 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530494 return item_tax_record[head[0]] ?
495 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530496 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530497 }).join("\n")
498 });
499 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530500
Anand Doshi53b73422013-05-31 11:50:01 +0530501 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530502 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
503 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530504 <thead><tr>' + headings + '</tr></thead> \
505 <tbody>' + rows + '</tbody> \
506 </table></div>';
507 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530508
Nabin Hait139dc7b2014-02-12 14:53:18 +0530509 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530510 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530511 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530512
Nabin Hait0f231062014-02-20 11:27:47 +0530513 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530514 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530515 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530516 msgprint(__("Please specify") + ": " +
517 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530518 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530519 valid = false;
520 }
Anand Doshi3543f302013-05-24 19:25:01 +0530521 }
522 });
523 return valid;
524 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530525
Anand Doshi3543f302013-05-24 19:25:01 +0530526 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530527 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530528 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530529
Anand Doshi3543f302013-05-24 19:25:01 +0530530 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530531 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530532 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530533
Anand Doshi3543f302013-05-24 19:25:01 +0530534 validate_conversion_rate: function() {
535 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530536 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530537 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530538 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530539
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530540 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530541 frappe.throw(repl('%(conversion_rate_label)s' +
542 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
543 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530544 {
545 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530546 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530547 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530548 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530549 }
Anand Doshi3543f302013-05-24 19:25:01 +0530550 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530551
Anand Doshi3543f302013-05-24 19:25:01 +0530552 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530553 this.discount_amount_applied = false;
554 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530555 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530556 this.apply_discount_amount();
557 },
558
559 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530560 this.validate_conversion_rate();
561 this.frm.item_doclist = this.get_item_doclist();
562 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530563
Anand Doshi3543f302013-05-24 19:25:01 +0530564 this.calculate_item_values();
565 this.initialize_taxes();
566 this.determine_exclusive_rate && this.determine_exclusive_rate();
567 this.calculate_net_total();
568 this.calculate_taxes();
569 this.calculate_totals();
570 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530571
Anand Doshi3543f302013-05-24 19:25:01 +0530572 this.show_item_wise_taxes();
573 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530574
Anand Doshi3543f302013-05-24 19:25:01 +0530575 initialize_taxes: function() {
576 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530577
Anand Doshi3543f302013-05-24 19:25:01 +0530578 $.each(this.frm.tax_doclist, function(i, tax) {
579 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530580 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530581 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530582 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
583
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530584 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530585 tax_fields.push("tax_amount");
586
587 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530588
Anand Doshi3543f302013-05-24 19:25:01 +0530589 me.validate_on_previous_row(tax);
590 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530591 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530592 });
593 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530594
Anand Doshi3543f302013-05-24 19:25:01 +0530595 calculate_taxes: function() {
596 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530597 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530598
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530599 // maintain actual tax rate based on idx
600 $.each(this.frm.tax_doclist, function(i, tax) {
601 if (tax.charge_type == "Actual") {
602 actual_tax_dict[tax.idx] = flt(tax.rate);
603 }
604 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530605
Anand Doshi3543f302013-05-24 19:25:01 +0530606 $.each(this.frm.item_doclist, function(n, item) {
607 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530608
Anand Doshi3543f302013-05-24 19:25:01 +0530609 $.each(me.frm.tax_doclist, function(i, tax) {
610 // tax_amount represents the amount of tax for the current step
611 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530612
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530613 // Adjust divisional loss to the last item
614 if (tax.charge_type == "Actual") {
615 actual_tax_dict[tax.idx] -= current_tax_amount;
616 if (n == me.frm.item_doclist.length - 1) {
617 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530618 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530619 }
620
Anand Doshi3543f302013-05-24 19:25:01 +0530621 // store tax_amount for current item as it will be used for
622 // charge type = 'On Previous Row Amount'
623 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530624
Anand Doshi3543f302013-05-24 19:25:01 +0530625 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530626 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530627 tax.tax_amount += current_tax_amount;
628
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530629 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530630
Anand Doshi3543f302013-05-24 19:25:01 +0530631 // for buying
632 if(tax.category) {
633 // if just for valuation, do not add the tax amount in total
634 // hence, setting it as 0 for further steps
635 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530636
Anand Doshi3543f302013-05-24 19:25:01 +0530637 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
638 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530639
Anand Doshi3543f302013-05-24 19:25:01 +0530640 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530641 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530642 // item's amount, previously applied tax and the current tax on that item
643 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530644 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530645 precision("total", tax));
646 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530647 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530648 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
649 precision("total", tax));
650 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530651
Anand Doshi3543f302013-05-24 19:25:01 +0530652 // in tax.total, accumulate grand total for each item
653 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530654
655 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530656 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530657 me.round_off_totals(tax);
658
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530659 // adjust Discount Amount loss in last tax iteration
660 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
661 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530662 }
Anand Doshi3543f302013-05-24 19:25:01 +0530663 });
664 });
665 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530666
667 round_off_totals: function(tax) {
668 tax.total = flt(tax.total, precision("total", tax));
669 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530670 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530671 precision("tax_amount", tax));
672 },
673
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530674 adjust_discount_amount_loss: function(tax) {
675 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530676 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530677 discount_amount_loss, precision("tax_amount", tax));
678 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530679 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530680
Anand Doshi3543f302013-05-24 19:25:01 +0530681 get_current_tax_amount: function(item, tax, item_tax_map) {
682 var tax_rate = this._get_tax_rate(tax, item_tax_map);
683 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530684
Anand Doshi3543f302013-05-24 19:25:01 +0530685 if(tax.charge_type == "Actual") {
686 // distribute the tax amount proportionally to each item row
687 var actual = flt(tax.rate, precision("tax_amount", tax));
688 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530689 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530690 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530691
Anand Doshi3543f302013-05-24 19:25:01 +0530692 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530693 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530694
Anand Doshi3543f302013-05-24 19:25:01 +0530695 } else if(tax.charge_type == "On Previous Row Amount") {
696 current_tax_amount = (tax_rate / 100.0) *
697 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530698
Anand Doshi3543f302013-05-24 19:25:01 +0530699 } else if(tax.charge_type == "On Previous Row Total") {
700 current_tax_amount = (tax_rate / 100.0) *
701 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530702 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530703
Anand Doshi53b73422013-05-31 11:50:01 +0530704 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530705
Anand Doshi53b73422013-05-31 11:50:01 +0530706 // store tax breakup for each item
707 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530708
Anand Doshi53b73422013-05-31 11:50:01 +0530709 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530710 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530711
Anand Doshi3543f302013-05-24 19:25:01 +0530712 _cleanup: function() {
713 $.each(this.frm.tax_doclist, function(i, tax) {
714 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530715 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530716 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530717
Anand Doshi3543f302013-05-24 19:25:01 +0530718 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
719 });
720 },
Anand Doshifc777182013-05-27 19:29:07 +0530721
Anand Doshi652bc072014-04-16 15:21:46 +0530722 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530723 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530724 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530725 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530726 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
727 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530728
Anand Doshi652bc072014-04-16 15:21:46 +0530729 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530730 }
731 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530732
Anand Doshi3543f302013-05-24 19:25:01 +0530733 _set_in_company_currency: function(item, print_field, base_field) {
734 // set values in base currency
735 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
736 precision(base_field, item));
737 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530738
Anand Doshibf3e54a2013-06-05 14:11:32 +0530739 get_terms: function() {
740 var me = this;
741 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530742 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530743 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530744 args: {
745 doctype: "Terms and Conditions",
746 fieldname: "terms",
747 filters: { name: this.frm.doc.tc_name },
748 },
749 });
750 }
751 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530752
753 taxes_and_charges: function() {
754 var me = this;
755 if(this.frm.doc.taxes_and_charges) {
756 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530757 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
758 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530759 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
760 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530761 "master_name": this.frm.doc.taxes_and_charges,
762 "tax_parentfield": this.other_fname
763 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530764 callback: function(r) {
765 if(!r.exc) {
Nabin Hait6b039142014-05-02 15:45:10 +0530766 me.frm.set_value(me.other_fname, r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530767 me.calculate_taxes_and_totals();
768 }
769 }
770 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530771 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530772 },
773
774 show_item_wise_taxes: function() {
775 if(this.frm.fields_dict.other_charges_calculation) {
776 $(this.get_item_wise_taxes_html())
777 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
778 }
779 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530780});