blob: 27ee1521c894e20d06c09ae3c42c9d62d0159ef0 [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() {
50 this.frm.clear_custom_buttons();
Anand Doshi8bde7f92014-04-24 18:11:49 +053051 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053052 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053053 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053054 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053055
56 // Show POS button only if it is enabled from features setup
57 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
Rushabh Mehtae34090a2013-11-15 11:30:23 +053058 this.make_pos_btn();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053059 },
60
Rushabh Mehtae34090a2013-11-15 11:30:23 +053061 make_pos_btn: function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053062 if(!this.pos_active) {
Pratik Vyasb52618c2014-04-14 16:25:30 +053063 var btn_label = __("POS View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053064 icon = "icon-desktop";
65 } else {
Pratik Vyasb52618c2014-04-14 16:25:30 +053066 var btn_label = __(this.frm.doctype) + __(" View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053067 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053068 }
69 var me = this;
Rushabh Mehtaff938022014-04-15 18:40:00 +053070
Rushabh Mehtae34090a2013-11-15 11:30:23 +053071 this.$pos_btn = this.frm.appframe.add_button(btn_label, function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053072 me.toggle_pos();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053073 }, icon);
74 },
75
76 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053077 // Check whether it is Selling or Buying cycle
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053078 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053079 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
Rushabh Mehtaff938022014-04-15 18:40:00 +053080
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053081 if (!price_list)
Pratik Vyasb52618c2014-04-14 16:25:30 +053082 msgprint(__("Please select Price List"))
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053083 else {
84 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
85
86 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053087 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053088 this.frm.layout.add_view("pos");
89 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
90 }
91
92 // toggle view
93 this.frm.layout.set_view(this.pos_active ? "" : "pos");
94 this.pos_active = !this.pos_active;
95
96 // refresh
97 if(this.pos_active)
98 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053099 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530100 }
Anand Doshi3543f302013-05-24 19:25:01 +0530101 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530102
103
Nabin Hait139dc7b2014-02-12 14:53:18 +0530104 item_code: function(doc, cdt, cdn) {
105 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530106 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530107 if(item.item_code || item.barcode || item.serial_no) {
108 if(!this.validate_company_and_party()) {
109 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
110 } else {
111 return this.frm.call({
112 method: "erpnext.stock.get_item_details.get_item_details",
113 child: item,
114 args: {
115 args: {
116 item_code: item.item_code,
117 barcode: item.barcode,
118 serial_no: item.serial_no,
119 warehouse: item.warehouse,
120 doctype: me.frm.doc.doctype,
121 docname: me.frm.doc.name,
122 customer: me.frm.doc.customer,
123 supplier: me.frm.doc.supplier,
124 currency: me.frm.doc.currency,
125 conversion_rate: me.frm.doc.conversion_rate,
126 price_list: me.frm.doc.selling_price_list ||
127 me.frm.doc.buying_price_list,
128 price_list_currency: me.frm.doc.price_list_currency,
129 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
130 company: me.frm.doc.company,
131 order_type: me.frm.doc.order_type,
132 is_pos: cint(me.frm.doc.is_pos),
133 is_subcontracted: me.frm.doc.is_subcontracted,
134 transaction_date: me.frm.doc.transaction_date
135 }
136 },
137 callback: function(r) {
138 if(!r.exc) {
139 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
140 }
141 }
142 });
143 }
144 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530145 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530146
147 serial_no: function(doc, cdt, cdn) {
148 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530149 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530150
151 if (item.serial_no) {
152 if (!item.item_code) {
153 this.frm.script_manager.trigger("item_code", cdt, cdn);
154 }
155 else {
156 var sr_no = [];
157
158 // Replacing all occurences of comma with carriage return
159 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
160
161 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530162
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530163 // Trim each string and push unique string to new list
164 for (var x=0; x<=serial_nos.length - 1; x++) {
165 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
166 sr_no.push(serial_nos[x].trim());
167 }
168 }
169
170 // Add the new list to the serial no. field in grid with each in new line
171 item.serial_no = "";
172 for (var x=0; x<=sr_no.length - 1; x++)
173 item.serial_no += sr_no[x] + '\n';
174
175 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530176 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530177 }
178 }
179 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530180
Anand Doshi923d41d2013-05-28 17:23:36 +0530181 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530182 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530183 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530184
Anand Doshi3543f302013-05-24 19:25:01 +0530185 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530186 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530187 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530188 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530189 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530190 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530191
Akhilesh Darjee78378272013-10-11 19:06:30 +0530192 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530193 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530194 }
195 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530196 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530197 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530198
Nabin Hait48a0bd32013-07-23 15:31:39 +0530199 this.frm.script_manager.trigger("currency");
Nabin Haita3dd72a2014-05-28 12:49:20 +0530200 this.apply_pricing_rule()
Anand Doshi3543f302013-05-24 19:25:01 +0530201 }
202 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530203
Anand Doshi3543f302013-05-24 19:25:01 +0530204 get_company_currency: function() {
205 return erpnext.get_currency(this.frm.doc.company);
206 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530207
Anand Doshi3543f302013-05-24 19:25:01 +0530208 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530209 var me = this;
210 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530211
Anand Doshi61a2f682013-06-21 17:55:31 +0530212 var company_currency = this.get_company_currency();
213 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530214 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530215 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530216 me.frm.set_value("conversion_rate", exchange_rate);
217 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530218 });
219 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530220 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530221 }
222 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530223
Anand Doshi61a2f682013-06-21 17:55:31 +0530224 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530225 if(this.frm.doc.currency === this.get_company_currency()) {
226 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530227 }
228 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530229 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
230 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
231 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530232 if(flt(this.frm.doc.conversion_rate)>0.0) {
233 this.apply_pricing_rule();
234 }
Anand Doshi3543f302013-05-24 19:25:01 +0530235 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530236
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530237 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530238 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530239 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
240 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530241 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530242 method: "erpnext.setup.utils.get_price_list_currency",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530243 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530244 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530245 },
Anand Doshi3543f302013-05-24 19:25:01 +0530246 callback: function(r) {
247 if(!r.exc) {
248 me.price_list_currency();
249 }
250 }
251 });
252 }
253 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530254
Anand Doshi61a2f682013-06-21 17:55:31 +0530255 get_exchange_rate: function(from_currency, to_currency, callback) {
256 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530257 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530258 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530259 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530260 });
261 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530262
Anand Doshi3543f302013-05-24 19:25:01 +0530263 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530264 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530265 this.set_dynamic_labels();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530266
Anand Doshi61a2f682013-06-21 17:55:31 +0530267 var company_currency = this.get_company_currency();
268 if(this.frm.doc.price_list_currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530269 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530270 function(exchange_rate) {
271 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530272 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530273 me.plc_conversion_rate();
274 }
275 });
276 } else {
277 this.plc_conversion_rate();
278 }
Anand Doshi3543f302013-05-24 19:25:01 +0530279 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530280
Anand Doshi3543f302013-05-24 19:25:01 +0530281 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530282 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
283 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530284 }
285 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530286 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530287 this.apply_pricing_rule();
Anand Doshi61a2f682013-06-21 17:55:31 +0530288 }
Anand Doshi3543f302013-05-24 19:25:01 +0530289 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530290
Anand Doshi3543f302013-05-24 19:25:01 +0530291 qty: function(doc, cdt, cdn) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530292 this.apply_pricing_rule(frappe.get_doc(cdt, cdn));
Anand Doshi3543f302013-05-24 19:25:01 +0530293 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530294
Anand Doshi652bc072014-04-16 15:21:46 +0530295 // tax rate
296 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530297 this.calculate_taxes_and_totals();
298 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530299
Anand Doshi923d41d2013-05-28 17:23:36 +0530300 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530301 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530302 try {
303 this.validate_on_previous_row(tax);
304 this.calculate_taxes_and_totals();
305 } catch(e) {
306 tax.row_id = null;
307 refresh_field("row_id", tax.name, tax.parentfield);
308 throw e;
309 }
310 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530311
Anand Doshi61a2f682013-06-21 17:55:31 +0530312 set_dynamic_labels: function() {
313 // What TODO? should we make price list system non-mandatory?
314 this.frm.toggle_reqd("plc_conversion_rate",
315 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530316
Anand Doshi61a2f682013-06-21 17:55:31 +0530317 var company_currency = this.get_company_currency();
318 this.change_form_labels(company_currency);
319 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530320 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530321 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530322
Anand Doshi923d41d2013-05-28 17:23:36 +0530323 recalculate: function() {
324 this.calculate_taxes_and_totals();
325 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530326
Anand Doshi923d41d2013-05-28 17:23:36 +0530327 recalculate_values: function() {
328 this.calculate_taxes_and_totals();
329 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530330
Anand Doshid0b00722013-05-28 18:54:48 +0530331 calculate_charges: function() {
332 this.calculate_taxes_and_totals();
333 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530334
Nabin Haita3dd72a2014-05-28 12:49:20 +0530335 apply_pricing_rule: function(item) {
336 var me = this;
337
338 var _apply_pricing_rule = function(item) {
339 return me.frm.call({
340 method: "erpnext.stock.get_item_details.apply_pricing_rule",
341 child: item,
342 args: {
343 args: {
344 item_code: item.item_code,
345 item_group: item.item_group,
346 brand: item.brand,
347 qty: item.qty,
348 customer: me.frm.doc.customer,
349 customer_group: me.frm.doc.customer_group,
350 territory: me.frm.doc.territory,
351 supplier: me.frm.doc.supplier,
352 supplier_type: me.frm.doc.supplier_type,
353 currency: me.frm.doc.currency,
354 conversion_rate: me.frm.doc.conversion_rate,
355 price_list: me.frm.doc.selling_price_list ||
356 me.frm.doc.buying_price_list,
357 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
358 company: me.frm.doc.company,
359 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
360 campaign: me.frm.doc.campaign,
361 sales_partner: me.frm.doc.sales_partner
362 }
363 },
364 callback: function(r) {
365 if(!r.exc) {
366 me.frm.script_manager.trigger("price_list_rate", item.doctype, item.name);
367 }
368 }
369 });
370 }
371
372
Nabin Haitc1367d92014-05-30 11:43:00 +0530373 if(item) {
374 _apply_pricing_rule(item);
375 } else {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530376 $.each(this.get_item_doclist(), function(n, item) {
377 _apply_pricing_rule(item);
378 });
379 }
380 },
381
Anand Doshi3543f302013-05-24 19:25:01 +0530382 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530383 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530384 try {
385 this.validate_on_previous_row(tax);
386 this.validate_inclusive_tax(tax);
387 this.calculate_taxes_and_totals();
388 } catch(e) {
389 tax.included_in_print_rate = 0;
390 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
391 throw e;
392 }
393 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530394
Anand Doshi3543f302013-05-24 19:25:01 +0530395 validate_on_previous_row: function(tax) {
396 // validate if a valid row id is mentioned in case of
397 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530398 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530399 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530400 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 +0530401 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530402 }
403 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530404
Anand Doshi3543f302013-05-24 19:25:01 +0530405 validate_inclusive_tax: function(tax) {
406 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530407
Anand Doshi3543f302013-05-24 19:25:01 +0530408 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530409 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530410 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530411 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530412
Anand Doshi3543f302013-05-24 19:25:01 +0530413 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530414 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
415 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530416 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530417 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530418
Anand Doshi3543f302013-05-24 19:25:01 +0530419 if(cint(tax.included_in_print_rate)) {
420 if(tax.charge_type == "Actual") {
421 // inclusive tax cannot be of type Actual
422 actual_type_error();
423 } else if(tax.charge_type == "On Previous Row Amount" &&
424 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
425 // referred row should also be an inclusive tax
426 on_previous_row_error(tax.row_id);
427 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530428 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530429 function(t) { return cint(t.included_in_print_rate) ? null : t; });
430 if(taxes_not_included.length > 0) {
431 // all rows above this tax should be inclusive
432 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
433 }
434 }
435 }
436 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530437
Anand Doshi3543f302013-05-24 19:25:01 +0530438 _load_item_tax_rate: function(item_tax_rate) {
439 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
440 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530441
Anand Doshi3543f302013-05-24 19:25:01 +0530442 _get_tax_rate: function(tax, item_tax_map) {
443 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
444 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
445 tax.rate;
446 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530447
Anand Doshi3543f302013-05-24 19:25:01 +0530448 get_item_wise_taxes_html: function() {
449 var item_tax = {};
450 var tax_accounts = [];
451 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530452
Anand Doshi3543f302013-05-24 19:25:01 +0530453 $.each(this.get_tax_doclist(), function(i, tax) {
454 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530455 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530456 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530457 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530458 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530459 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530460 var tax_rate = "";
461 if(tax_data[0] != null) {
462 tax_rate = (tax.charge_type === "Actual") ?
463 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
464 (flt(tax_data[0], tax_rate_precision) + "%");
465 }
466 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
467 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530468
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530469 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530470 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530471 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530472 }
Anand Doshi3543f302013-05-24 19:25:01 +0530473 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530474 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530475 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530476
477 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530478 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530479
Anand Doshi179e3772013-09-05 16:53:57 +0530480 var distinct_item_names = [];
481 var distinct_items = [];
482 $.each(this.get_item_doclist(), function(i, item) {
483 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
484 distinct_item_names.push(item.item_code || item.item_name);
485 distinct_items.push(item);
486 }
487 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530488
Anand Doshi179e3772013-09-05 16:53:57 +0530489 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530490 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530491 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530492 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
493 item_name: item.item_name,
494 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530495 return item_tax_record[head[0]] ?
496 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530497 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530498 }).join("\n")
499 });
500 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530501
Anand Doshi53b73422013-05-31 11:50:01 +0530502 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530503 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
504 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530505 <thead><tr>' + headings + '</tr></thead> \
506 <tbody>' + rows + '</tbody> \
507 </table></div>';
508 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530509
Nabin Hait139dc7b2014-02-12 14:53:18 +0530510 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530511 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530512 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530513
Nabin Hait0f231062014-02-20 11:27:47 +0530514 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530515 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530516 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530517 msgprint(__("Please specify") + ": " +
518 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530519 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530520 valid = false;
521 }
Anand Doshi3543f302013-05-24 19:25:01 +0530522 }
523 });
524 return valid;
525 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530526
Anand Doshi3543f302013-05-24 19:25:01 +0530527 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530528 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530529 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530530
Anand Doshi3543f302013-05-24 19:25:01 +0530531 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530532 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530533 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530534
Anand Doshi3543f302013-05-24 19:25:01 +0530535 validate_conversion_rate: function() {
536 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530537 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530538 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530539 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530540
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530541 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530542 frappe.throw(repl('%(conversion_rate_label)s' +
543 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
544 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530545 {
546 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530547 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530548 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530549 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530550 }
Anand Doshi3543f302013-05-24 19:25:01 +0530551 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530552
Anand Doshi3543f302013-05-24 19:25:01 +0530553 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530554 this.discount_amount_applied = false;
555 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530556 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530557 this.apply_discount_amount();
558 },
559
560 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530561 this.validate_conversion_rate();
562 this.frm.item_doclist = this.get_item_doclist();
563 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530564
Anand Doshi3543f302013-05-24 19:25:01 +0530565 this.calculate_item_values();
566 this.initialize_taxes();
567 this.determine_exclusive_rate && this.determine_exclusive_rate();
568 this.calculate_net_total();
569 this.calculate_taxes();
570 this.calculate_totals();
571 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530572
Anand Doshi3543f302013-05-24 19:25:01 +0530573 this.show_item_wise_taxes();
574 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530575
Anand Doshi3543f302013-05-24 19:25:01 +0530576 initialize_taxes: function() {
577 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530578
Anand Doshi3543f302013-05-24 19:25:01 +0530579 $.each(this.frm.tax_doclist, function(i, tax) {
580 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530581 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530582 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530583 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
584
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530585 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530586 tax_fields.push("tax_amount");
587
588 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530589
Anand Doshi3543f302013-05-24 19:25:01 +0530590 me.validate_on_previous_row(tax);
591 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530592 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530593 });
594 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530595
Anand Doshi3543f302013-05-24 19:25:01 +0530596 calculate_taxes: function() {
597 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530598 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530599
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530600 // maintain actual tax rate based on idx
601 $.each(this.frm.tax_doclist, function(i, tax) {
602 if (tax.charge_type == "Actual") {
603 actual_tax_dict[tax.idx] = flt(tax.rate);
604 }
605 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530606
Anand Doshi3543f302013-05-24 19:25:01 +0530607 $.each(this.frm.item_doclist, function(n, item) {
608 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530609
Anand Doshi3543f302013-05-24 19:25:01 +0530610 $.each(me.frm.tax_doclist, function(i, tax) {
611 // tax_amount represents the amount of tax for the current step
612 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530613
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530614 // Adjust divisional loss to the last item
615 if (tax.charge_type == "Actual") {
616 actual_tax_dict[tax.idx] -= current_tax_amount;
617 if (n == me.frm.item_doclist.length - 1) {
618 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530619 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530620 }
621
Anand Doshi3543f302013-05-24 19:25:01 +0530622 // store tax_amount for current item as it will be used for
623 // charge type = 'On Previous Row Amount'
624 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530625
Anand Doshi3543f302013-05-24 19:25:01 +0530626 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530627 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530628 tax.tax_amount += current_tax_amount;
629
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530630 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530631
Anand Doshi3543f302013-05-24 19:25:01 +0530632 // for buying
633 if(tax.category) {
634 // if just for valuation, do not add the tax amount in total
635 // hence, setting it as 0 for further steps
636 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530637
Anand Doshi3543f302013-05-24 19:25:01 +0530638 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
639 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530640
Anand Doshi3543f302013-05-24 19:25:01 +0530641 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530642 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530643 // item's amount, previously applied tax and the current tax on that item
644 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530645 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530646 precision("total", tax));
647 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530648 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530649 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
650 precision("total", tax));
651 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530652
Anand Doshi3543f302013-05-24 19:25:01 +0530653 // in tax.total, accumulate grand total for each item
654 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530655
656 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530657 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530658 me.round_off_totals(tax);
659
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530660 // adjust Discount Amount loss in last tax iteration
661 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
662 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530663 }
Anand Doshi3543f302013-05-24 19:25:01 +0530664 });
665 });
666 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530667
668 round_off_totals: function(tax) {
669 tax.total = flt(tax.total, precision("total", tax));
670 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530671 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530672 precision("tax_amount", tax));
673 },
674
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530675 adjust_discount_amount_loss: function(tax) {
676 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530677 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530678 discount_amount_loss, precision("tax_amount", tax));
679 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530680 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530681
Anand Doshi3543f302013-05-24 19:25:01 +0530682 get_current_tax_amount: function(item, tax, item_tax_map) {
683 var tax_rate = this._get_tax_rate(tax, item_tax_map);
684 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530685
Anand Doshi3543f302013-05-24 19:25:01 +0530686 if(tax.charge_type == "Actual") {
687 // distribute the tax amount proportionally to each item row
688 var actual = flt(tax.rate, precision("tax_amount", tax));
689 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530690 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530691 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530692
Anand Doshi3543f302013-05-24 19:25:01 +0530693 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530694 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530695
Anand Doshi3543f302013-05-24 19:25:01 +0530696 } else if(tax.charge_type == "On Previous Row Amount") {
697 current_tax_amount = (tax_rate / 100.0) *
698 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530699
Anand Doshi3543f302013-05-24 19:25:01 +0530700 } else if(tax.charge_type == "On Previous Row Total") {
701 current_tax_amount = (tax_rate / 100.0) *
702 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530703 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530704
Anand Doshi53b73422013-05-31 11:50:01 +0530705 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530706
Anand Doshi53b73422013-05-31 11:50:01 +0530707 // store tax breakup for each item
708 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530709
Anand Doshi53b73422013-05-31 11:50:01 +0530710 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530711 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530712
Anand Doshi3543f302013-05-24 19:25:01 +0530713 _cleanup: function() {
714 $.each(this.frm.tax_doclist, function(i, tax) {
715 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530716 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530717 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530718
Anand Doshi3543f302013-05-24 19:25:01 +0530719 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
720 });
721 },
Anand Doshifc777182013-05-27 19:29:07 +0530722
Anand Doshi652bc072014-04-16 15:21:46 +0530723 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530724 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530725 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530726 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530727 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
728 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530729
Anand Doshi652bc072014-04-16 15:21:46 +0530730 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530731 }
732 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530733
Anand Doshi3543f302013-05-24 19:25:01 +0530734 _set_in_company_currency: function(item, print_field, base_field) {
735 // set values in base currency
736 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
737 precision(base_field, item));
738 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530739
Anand Doshibf3e54a2013-06-05 14:11:32 +0530740 get_terms: function() {
741 var me = this;
742 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530743 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530744 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530745 args: {
746 doctype: "Terms and Conditions",
747 fieldname: "terms",
748 filters: { name: this.frm.doc.tc_name },
749 },
750 });
751 }
752 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530753
754 taxes_and_charges: function() {
755 var me = this;
756 if(this.frm.doc.taxes_and_charges) {
757 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530758 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
759 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530760 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
761 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530762 "master_name": this.frm.doc.taxes_and_charges,
763 "tax_parentfield": this.other_fname
764 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530765 callback: function(r) {
766 if(!r.exc) {
Nabin Hait6b039142014-05-02 15:45:10 +0530767 me.frm.set_value(me.other_fname, r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530768 me.calculate_taxes_and_totals();
769 }
770 }
771 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530772 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530773 },
774
775 show_item_wise_taxes: function() {
776 if(this.frm.fields_dict.other_charges_calculation) {
777 $(this.get_item_wise_taxes_html())
778 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
779 }
780 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530781});