blob: 1790a47252486abef16359c3ba795c5cf2971de7 [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 Doshifaefeaa2014-06-24 18:53:04 +053010 this._super();
11
Anand Doshi3543f302013-05-24 19:25:01 +053012 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053013 var today = get_today(),
Nabin Hait73083dc2014-03-14 15:10:42 +053014 currency = frappe.defaults.get_user_default("currency");
Rushabh Mehtaff938022014-04-15 18:40:00 +053015
Anand Doshi3543f302013-05-24 19:25:01 +053016 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053017 posting_date: today,
18 due_date: today,
19 transaction_date: today,
20 currency: currency,
21 price_list_currency: currency,
22 status: "Draft",
Nabin Hait73083dc2014-03-14 15:10:42 +053023 fiscal_year: frappe.defaults.get_user_default("fiscal_year"),
Anand Doshifc777182013-05-27 19:29:07 +053024 is_subcontracted: "No",
Anand Doshi3543f302013-05-24 19:25:01 +053025 }, function(fieldname, value) {
26 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
27 me.frm.set_value(fieldname, value);
Rushabh Mehtaff938022014-04-15 18:40:00 +053028 });
nabinhaitbc408d82014-07-07 16:51:52 +053029
30 if(!this.frm.doc.company) {
31 this.frm.set_value("company", frappe.defaults.get_user_default("company"));
32 } else {
33 cur_frm.script_manager.trigger("company");
34 }
Anand Doshi3543f302013-05-24 19:25:01 +053035 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +053036
Anand Doshi1e4fb682013-08-23 12:56:33 +053037 if(this.other_fname) {
Anand Doshia91c95f2013-08-23 13:00:47 +053038 this[this.other_fname + "_remove"] = this.calculate_taxes_and_totals;
39 }
Rushabh Mehtaff938022014-04-15 18:40:00 +053040
Anand Doshia91c95f2013-08-23 13:00:47 +053041 if(this.fname) {
42 this[this.fname + "_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +053043 }
Anand Doshi3543f302013-05-24 19:25:01 +053044 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053045
Anand Doshi5e4e5d72013-08-06 17:23:44 +053046 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053047 var me = this;
Anand Doshi221dec82014-06-03 14:12:49 +053048 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 +053049 this.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +053050 }
Rushabh Mehtab6843f22014-06-04 13:10:41 +053051 if(frappe.meta.get_docfield(this.tname, "item_code")) {
Rushabh Mehta880b3ef2014-06-06 11:45:11 +053052 cur_frm.get_field(this.fname).grid.set_multiple_add("item_code", "qty");
Rushabh Mehtab6843f22014-06-04 13:10:41 +053053 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053054 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053055
Anand Doshi3543f302013-05-24 19:25:01 +053056 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +053057 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053058 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053059 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053060 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053061
62 // Show POS button only if it is enabled from features setup
63 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
Rushabh Mehtae34090a2013-11-15 11:30:23 +053064 this.make_pos_btn();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053065 },
66
Rushabh Mehtae34090a2013-11-15 11:30:23 +053067 make_pos_btn: function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053068 if(!this.pos_active) {
Pratik Vyasb52618c2014-04-14 16:25:30 +053069 var btn_label = __("POS View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053070 icon = "icon-desktop";
71 } else {
Pratik Vyasb52618c2014-04-14 16:25:30 +053072 var btn_label = __(this.frm.doctype) + __(" View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053073 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053074 }
75 var me = this;
Rushabh Mehtaff938022014-04-15 18:40:00 +053076
Rushabh Mehtae34090a2013-11-15 11:30:23 +053077 this.$pos_btn = this.frm.appframe.add_button(btn_label, function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053078 me.toggle_pos();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053079 }, icon);
80 },
81
82 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053083 // Check whether it is Selling or Buying cycle
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053084 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053085 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
Rushabh Mehtaff938022014-04-15 18:40:00 +053086
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053087 if (!price_list)
Pratik Vyasb52618c2014-04-14 16:25:30 +053088 msgprint(__("Please select Price List"))
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053089 else {
90 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
91
92 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053093 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053094 this.frm.layout.add_view("pos");
95 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
96 }
97
98 // toggle view
99 this.frm.layout.set_view(this.pos_active ? "" : "pos");
100 this.pos_active = !this.pos_active;
101
102 // refresh
103 if(this.pos_active)
104 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530105 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530106 }
Anand Doshi3543f302013-05-24 19:25:01 +0530107 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530108
109
Nabin Hait139dc7b2014-02-12 14:53:18 +0530110 item_code: function(doc, cdt, cdn) {
111 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530112 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530113 if(item.item_code || item.barcode || item.serial_no) {
114 if(!this.validate_company_and_party()) {
115 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
116 } else {
117 return this.frm.call({
118 method: "erpnext.stock.get_item_details.get_item_details",
119 child: item,
120 args: {
121 args: {
122 item_code: item.item_code,
123 barcode: item.barcode,
124 serial_no: item.serial_no,
125 warehouse: item.warehouse,
Nabin Hait444f9562014-06-20 15:59:49 +0530126 parenttype: me.frm.doc.doctype,
127 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530128 customer: me.frm.doc.customer,
129 supplier: me.frm.doc.supplier,
130 currency: me.frm.doc.currency,
131 conversion_rate: me.frm.doc.conversion_rate,
132 price_list: me.frm.doc.selling_price_list ||
133 me.frm.doc.buying_price_list,
134 price_list_currency: me.frm.doc.price_list_currency,
135 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
136 company: me.frm.doc.company,
137 order_type: me.frm.doc.order_type,
138 is_pos: cint(me.frm.doc.is_pos),
139 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Hait444f9562014-06-20 15:59:49 +0530140 transaction_date: me.frm.doc.transaction_date,
141 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
142 doctype: item.doctype,
143 name: item.name
Nabin Hait139dc7b2014-02-12 14:53:18 +0530144 }
145 },
146 callback: function(r) {
147 if(!r.exc) {
148 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
149 }
150 }
151 });
152 }
153 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530154 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530155
156 serial_no: function(doc, cdt, cdn) {
157 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530158 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530159
160 if (item.serial_no) {
161 if (!item.item_code) {
162 this.frm.script_manager.trigger("item_code", cdt, cdn);
163 }
164 else {
165 var sr_no = [];
166
167 // Replacing all occurences of comma with carriage return
168 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
169
170 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530171
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530172 // Trim each string and push unique string to new list
173 for (var x=0; x<=serial_nos.length - 1; x++) {
174 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
175 sr_no.push(serial_nos[x].trim());
176 }
177 }
178
179 // Add the new list to the serial no. field in grid with each in new line
180 item.serial_no = "";
181 for (var x=0; x<=sr_no.length - 1; x++)
182 item.serial_no += sr_no[x] + '\n';
183
184 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530185 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530186 }
187 }
188 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530189
Anand Doshi923d41d2013-05-28 17:23:36 +0530190 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530191 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530192 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530193
Anand Doshi3543f302013-05-24 19:25:01 +0530194 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530195 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530196 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530197 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530198 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530199 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530200
Akhilesh Darjee78378272013-10-11 19:06:30 +0530201 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530202 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530203 }
204 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530205 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530206 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530207
Nabin Hait48a0bd32013-07-23 15:31:39 +0530208 this.frm.script_manager.trigger("currency");
Nabin Hait444f9562014-06-20 15:59:49 +0530209 this.apply_pricing_rule();
Anand Doshi3543f302013-05-24 19:25:01 +0530210 }
211 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530212
Anand Doshi3543f302013-05-24 19:25:01 +0530213 get_company_currency: function() {
214 return erpnext.get_currency(this.frm.doc.company);
215 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530216
Anand Doshi3543f302013-05-24 19:25:01 +0530217 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530218 var me = this;
219 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530220
Anand Doshi61a2f682013-06-21 17:55:31 +0530221 var company_currency = this.get_company_currency();
222 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530223 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530224 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530225 me.frm.set_value("conversion_rate", exchange_rate);
226 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530227 });
228 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530229 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530230 }
231 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530232
Anand Doshi61a2f682013-06-21 17:55:31 +0530233 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530234 if(this.frm.doc.currency === this.get_company_currency()) {
235 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530236 }
237 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530238 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
239 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
240 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530241 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530242 if(this.frm.doc.ignore_pricing_rule) {
243 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530244 } else if (!this.in_apply_price_list){
245 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530246 }
247
Nabin Haita3dd72a2014-05-28 12:49:20 +0530248 }
Anand Doshi3543f302013-05-24 19:25:01 +0530249 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530250
Anand Doshi61a2f682013-06-21 17:55:31 +0530251 get_exchange_rate: function(from_currency, to_currency, callback) {
252 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530253 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530254 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530255 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530256 });
257 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530258
Anand Doshi3543f302013-05-24 19:25:01 +0530259 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530260 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530261 this.set_dynamic_labels();
Anand Doshi51f722d2014-07-04 15:44:26 +0530262 this.set_plc_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530263 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530264
Anand Doshi3543f302013-05-24 19:25:01 +0530265 plc_conversion_rate: function() {
Anand Doshi51f722d2014-07-04 15:44:26 +0530266 this.set_plc_conversion_rate();
267 if(!this.in_apply_price_list) {
268 this.apply_price_list();
269 }
270 },
271
272 set_plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530273 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
274 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530275 }
276 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530277 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530278 }
Anand Doshi3543f302013-05-24 19:25:01 +0530279 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530280
Anand Doshi3543f302013-05-24 19:25:01 +0530281 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530282 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530283 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530284
Anand Doshi652bc072014-04-16 15:21:46 +0530285 // tax rate
286 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530287 this.calculate_taxes_and_totals();
288 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530289
Anand Doshi923d41d2013-05-28 17:23:36 +0530290 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530291 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530292 try {
293 this.validate_on_previous_row(tax);
294 this.calculate_taxes_and_totals();
295 } catch(e) {
296 tax.row_id = null;
297 refresh_field("row_id", tax.name, tax.parentfield);
298 throw e;
299 }
300 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530301
Anand Doshi61a2f682013-06-21 17:55:31 +0530302 set_dynamic_labels: function() {
303 // What TODO? should we make price list system non-mandatory?
304 this.frm.toggle_reqd("plc_conversion_rate",
305 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530306
Anand Doshi61a2f682013-06-21 17:55:31 +0530307 var company_currency = this.get_company_currency();
308 this.change_form_labels(company_currency);
309 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530310 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530311 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530312
Anand Doshi923d41d2013-05-28 17:23:36 +0530313 recalculate: function() {
314 this.calculate_taxes_and_totals();
315 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530316
Anand Doshi923d41d2013-05-28 17:23:36 +0530317 recalculate_values: function() {
318 this.calculate_taxes_and_totals();
319 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530320
Anand Doshid0b00722013-05-28 18:54:48 +0530321 calculate_charges: function() {
322 this.calculate_taxes_and_totals();
323 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530324
Nabin Hait444f9562014-06-20 15:59:49 +0530325 ignore_pricing_rule: function() {
326 this.apply_pricing_rule();
327 },
328
329 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530330 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530331 return this.frm.call({
332 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
333 args: { args: this._get_args(item) },
334 callback: function(r) {
335 if (!r.exc) {
336 me._set_values_for_item_list(r.message);
337 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
338 }
339 }
340 });
341 },
342
343 _get_args: function(item) {
344 var me = this;
345 return {
346 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530347 "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 || me.frm.doc.buying_price_list,
355 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
356 "company": me.frm.doc.company,
357 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
358 "campaign": me.frm.doc.campaign,
359 "sales_partner": me.frm.doc.sales_partner,
360 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
361 "parenttype": me.frm.doc.doctype,
362 "parent": me.frm.doc.name
363 };
Nabin Hait444f9562014-06-20 15:59:49 +0530364 },
365
366 _get_item_list: function(item) {
367 var item_list = [];
368 var append_item = function(d) {
369 item_list.push({
370 "doctype": d.doctype,
371 "name": d.name,
372 "item_code": d.item_code,
373 "item_group": d.item_group,
374 "brand": d.brand,
375 "qty": d.qty
Nabin Haita3dd72a2014-05-28 12:49:20 +0530376 });
Nabin Hait444f9562014-06-20 15:59:49 +0530377 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530378
Nabin Hait444f9562014-06-20 15:59:49 +0530379 if (item) {
380 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530381 } else {
Nabin Hait444f9562014-06-20 15:59:49 +0530382 $.each(this.get_item_doclist(), function(i, d) {
383 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530384 });
385 }
Nabin Hait444f9562014-06-20 15:59:49 +0530386 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530387 },
388
Anand Doshidffec8f2014-07-01 17:45:15 +0530389 _set_values_for_item_list: function(children) {
390 $.each(children, function(i, d) {
391 $.each(d, function(k, v) {
392 if (["doctype", "name"].indexOf(k)===-1) {
393 frappe.model.set_value(d.doctype, d.name, k, v);
394 }
395 });
396 });
397 },
398
399 apply_price_list: function() {
400 var me = this;
401 return this.frm.call({
402 method: "erpnext.stock.get_item_details.apply_price_list",
403 args: { args: this._get_args() },
404 callback: function(r) {
405 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530406 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530407 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
408 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530409 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +0530410 me._set_values_for_item_list(r.message.children);
411 }
412 }
413 });
414 },
415
Anand Doshi3543f302013-05-24 19:25:01 +0530416 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530417 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530418 try {
419 this.validate_on_previous_row(tax);
420 this.validate_inclusive_tax(tax);
421 this.calculate_taxes_and_totals();
422 } catch(e) {
423 tax.included_in_print_rate = 0;
424 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
425 throw e;
426 }
427 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530428
Anand Doshi3543f302013-05-24 19:25:01 +0530429 validate_on_previous_row: function(tax) {
430 // validate if a valid row id is mentioned in case of
431 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530432 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530433 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530434 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 +0530435 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530436 }
437 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530438
Anand Doshi3543f302013-05-24 19:25:01 +0530439 validate_inclusive_tax: function(tax) {
440 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530441
Anand Doshi3543f302013-05-24 19:25:01 +0530442 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530443 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530444 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530445 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530446
Anand Doshi3543f302013-05-24 19:25:01 +0530447 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530448 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
449 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530450 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530451 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530452
Anand Doshi3543f302013-05-24 19:25:01 +0530453 if(cint(tax.included_in_print_rate)) {
454 if(tax.charge_type == "Actual") {
455 // inclusive tax cannot be of type Actual
456 actual_type_error();
457 } else if(tax.charge_type == "On Previous Row Amount" &&
458 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
459 // referred row should also be an inclusive tax
460 on_previous_row_error(tax.row_id);
461 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530462 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530463 function(t) { return cint(t.included_in_print_rate) ? null : t; });
464 if(taxes_not_included.length > 0) {
465 // all rows above this tax should be inclusive
466 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
467 }
468 }
469 }
470 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530471
Anand Doshi3543f302013-05-24 19:25:01 +0530472 _load_item_tax_rate: function(item_tax_rate) {
473 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
474 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530475
Anand Doshi3543f302013-05-24 19:25:01 +0530476 _get_tax_rate: function(tax, item_tax_map) {
477 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
478 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
479 tax.rate;
480 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530481
Anand Doshi3543f302013-05-24 19:25:01 +0530482 get_item_wise_taxes_html: function() {
483 var item_tax = {};
484 var tax_accounts = [];
485 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530486
Anand Doshi3543f302013-05-24 19:25:01 +0530487 $.each(this.get_tax_doclist(), function(i, tax) {
488 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530489 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530490 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530491 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530492 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530493 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530494 var tax_rate = "";
495 if(tax_data[0] != null) {
496 tax_rate = (tax.charge_type === "Actual") ?
497 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
498 (flt(tax_data[0], tax_rate_precision) + "%");
499 }
500 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
501 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530502
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530503 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530504 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530505 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530506 }
Anand Doshi3543f302013-05-24 19:25:01 +0530507 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530508 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530509 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530510
511 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530512 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530513
Anand Doshi179e3772013-09-05 16:53:57 +0530514 var distinct_item_names = [];
515 var distinct_items = [];
516 $.each(this.get_item_doclist(), function(i, item) {
517 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
518 distinct_item_names.push(item.item_code || item.item_name);
519 distinct_items.push(item);
520 }
521 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530522
Anand Doshi179e3772013-09-05 16:53:57 +0530523 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530524 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530525 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530526 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
527 item_name: item.item_name,
528 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530529 return item_tax_record[head[0]] ?
530 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530531 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530532 }).join("\n")
533 });
534 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530535
Anand Doshi53b73422013-05-31 11:50:01 +0530536 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530537 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
538 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530539 <thead><tr>' + headings + '</tr></thead> \
540 <tbody>' + rows + '</tbody> \
541 </table></div>';
542 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530543
Nabin Hait139dc7b2014-02-12 14:53:18 +0530544 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530545 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530546 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530547
Nabin Hait0f231062014-02-20 11:27:47 +0530548 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530549 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530550 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530551 msgprint(__("Please specify") + ": " +
552 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530553 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530554 valid = false;
555 }
Anand Doshi3543f302013-05-24 19:25:01 +0530556 }
557 });
558 return valid;
559 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530560
Anand Doshi3543f302013-05-24 19:25:01 +0530561 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530562 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530563 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530564
Anand Doshi3543f302013-05-24 19:25:01 +0530565 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530566 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530567 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530568
Anand Doshi3543f302013-05-24 19:25:01 +0530569 validate_conversion_rate: function() {
570 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530571 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530572 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530573 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530574
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530575 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530576 frappe.throw(repl('%(conversion_rate_label)s' +
577 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
578 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530579 {
580 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530581 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530582 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530583 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530584 }
Anand Doshi3543f302013-05-24 19:25:01 +0530585 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530586
Anand Doshi3543f302013-05-24 19:25:01 +0530587 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530588 this.discount_amount_applied = false;
589 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530590 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530591 this.apply_discount_amount();
592 },
593
594 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530595 this.validate_conversion_rate();
596 this.frm.item_doclist = this.get_item_doclist();
597 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530598
Anand Doshi3543f302013-05-24 19:25:01 +0530599 this.calculate_item_values();
600 this.initialize_taxes();
601 this.determine_exclusive_rate && this.determine_exclusive_rate();
602 this.calculate_net_total();
603 this.calculate_taxes();
604 this.calculate_totals();
605 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530606
Anand Doshi3543f302013-05-24 19:25:01 +0530607 this.show_item_wise_taxes();
608 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530609
Anand Doshi3543f302013-05-24 19:25:01 +0530610 initialize_taxes: function() {
611 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530612
Anand Doshi3543f302013-05-24 19:25:01 +0530613 $.each(this.frm.tax_doclist, function(i, tax) {
614 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530615 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530616 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530617 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
618
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530619 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530620 tax_fields.push("tax_amount");
621
622 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530623
Anand Doshi3543f302013-05-24 19:25:01 +0530624 me.validate_on_previous_row(tax);
625 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530626 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530627 });
628 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530629
Anand Doshi3543f302013-05-24 19:25:01 +0530630 calculate_taxes: function() {
631 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530632 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530633
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530634 // maintain actual tax rate based on idx
635 $.each(this.frm.tax_doclist, function(i, tax) {
636 if (tax.charge_type == "Actual") {
637 actual_tax_dict[tax.idx] = flt(tax.rate);
638 }
639 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530640
Anand Doshi3543f302013-05-24 19:25:01 +0530641 $.each(this.frm.item_doclist, function(n, item) {
642 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530643
Anand Doshi3543f302013-05-24 19:25:01 +0530644 $.each(me.frm.tax_doclist, function(i, tax) {
645 // tax_amount represents the amount of tax for the current step
646 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530647
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530648 // Adjust divisional loss to the last item
649 if (tax.charge_type == "Actual") {
650 actual_tax_dict[tax.idx] -= current_tax_amount;
651 if (n == me.frm.item_doclist.length - 1) {
652 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530653 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530654 }
655
Anand Doshi3543f302013-05-24 19:25:01 +0530656 // store tax_amount for current item as it will be used for
657 // charge type = 'On Previous Row Amount'
658 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530659
Anand Doshi3543f302013-05-24 19:25:01 +0530660 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530661 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530662 tax.tax_amount += current_tax_amount;
663
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530664 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530665
Anand Doshi3543f302013-05-24 19:25:01 +0530666 // for buying
667 if(tax.category) {
668 // if just for valuation, do not add the tax amount in total
669 // hence, setting it as 0 for further steps
670 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530671
Anand Doshi3543f302013-05-24 19:25:01 +0530672 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
673 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530674
Anand Doshi3543f302013-05-24 19:25:01 +0530675 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530676 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530677 // item's amount, previously applied tax and the current tax on that item
678 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530679 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530680 precision("total", tax));
681 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530682 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530683 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
684 precision("total", tax));
685 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530686
Anand Doshi3543f302013-05-24 19:25:01 +0530687 // in tax.total, accumulate grand total for each item
688 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530689
690 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530691 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530692 me.round_off_totals(tax);
693
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530694 // adjust Discount Amount loss in last tax iteration
695 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
696 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530697 }
Anand Doshi3543f302013-05-24 19:25:01 +0530698 });
699 });
700 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530701
702 round_off_totals: function(tax) {
703 tax.total = flt(tax.total, precision("total", tax));
704 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530705 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530706 precision("tax_amount", tax));
707 },
708
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530709 adjust_discount_amount_loss: function(tax) {
710 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530711 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530712 discount_amount_loss, precision("tax_amount", tax));
713 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530714 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530715
Anand Doshi3543f302013-05-24 19:25:01 +0530716 get_current_tax_amount: function(item, tax, item_tax_map) {
717 var tax_rate = this._get_tax_rate(tax, item_tax_map);
718 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530719
Anand Doshi3543f302013-05-24 19:25:01 +0530720 if(tax.charge_type == "Actual") {
721 // distribute the tax amount proportionally to each item row
722 var actual = flt(tax.rate, precision("tax_amount", tax));
723 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530724 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530725 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530726
Anand Doshi3543f302013-05-24 19:25:01 +0530727 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530728 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530729
Anand Doshi3543f302013-05-24 19:25:01 +0530730 } else if(tax.charge_type == "On Previous Row Amount") {
731 current_tax_amount = (tax_rate / 100.0) *
732 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530733
Anand Doshi3543f302013-05-24 19:25:01 +0530734 } else if(tax.charge_type == "On Previous Row Total") {
735 current_tax_amount = (tax_rate / 100.0) *
736 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530737 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530738
Anand Doshi53b73422013-05-31 11:50:01 +0530739 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530740
Anand Doshi53b73422013-05-31 11:50:01 +0530741 // store tax breakup for each item
742 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530743
Anand Doshi53b73422013-05-31 11:50:01 +0530744 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530745 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530746
Anand Doshi3543f302013-05-24 19:25:01 +0530747 _cleanup: function() {
748 $.each(this.frm.tax_doclist, function(i, tax) {
749 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530750 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530751 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530752
Anand Doshi3543f302013-05-24 19:25:01 +0530753 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
754 });
755 },
Anand Doshifc777182013-05-27 19:29:07 +0530756
Anand Doshi652bc072014-04-16 15:21:46 +0530757 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530758 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530759 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530760 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530761 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
762 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530763
Anand Doshi652bc072014-04-16 15:21:46 +0530764 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530765 }
766 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530767
Anand Doshi3543f302013-05-24 19:25:01 +0530768 _set_in_company_currency: function(item, print_field, base_field) {
769 // set values in base currency
770 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
771 precision(base_field, item));
772 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530773
Anand Doshibf3e54a2013-06-05 14:11:32 +0530774 get_terms: function() {
775 var me = this;
776 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530777 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530778 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530779 args: {
780 doctype: "Terms and Conditions",
781 fieldname: "terms",
782 filters: { name: this.frm.doc.tc_name },
783 },
784 });
785 }
786 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530787
788 taxes_and_charges: function() {
789 var me = this;
790 if(this.frm.doc.taxes_and_charges) {
791 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530792 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
793 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530794 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
795 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530796 "master_name": this.frm.doc.taxes_and_charges,
797 "tax_parentfield": this.other_fname
798 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530799 callback: function(r) {
800 if(!r.exc) {
Nabin Hait6b039142014-05-02 15:45:10 +0530801 me.frm.set_value(me.other_fname, r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530802 me.calculate_taxes_and_totals();
803 }
804 }
805 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530806 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530807 },
808
809 show_item_wise_taxes: function() {
810 if(this.frm.fields_dict.other_charges_calculation) {
811 $(this.get_item_wise_taxes_html())
812 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
813 }
814 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530815});