blob: ea576d5ae07c7a124f841cb94b52220cdb1e1e2c [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,
Nabin Hait9daca102014-06-20 15:59:49 +0530119 parenttype: me.frm.doc.doctype,
120 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530121 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,
Nabin Hait9daca102014-06-20 15:59:49 +0530133 transaction_date: me.frm.doc.transaction_date,
134 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
135 doctype: item.doctype,
136 name: item.name
Nabin Hait139dc7b2014-02-12 14:53:18 +0530137 }
138 },
139 callback: function(r) {
140 if(!r.exc) {
141 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
142 }
143 }
144 });
145 }
146 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530147 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530148
149 serial_no: function(doc, cdt, cdn) {
150 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530151 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530152
153 if (item.serial_no) {
154 if (!item.item_code) {
155 this.frm.script_manager.trigger("item_code", cdt, cdn);
156 }
157 else {
158 var sr_no = [];
159
160 // Replacing all occurences of comma with carriage return
161 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
162
163 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530164
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530165 // Trim each string and push unique string to new list
166 for (var x=0; x<=serial_nos.length - 1; x++) {
167 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
168 sr_no.push(serial_nos[x].trim());
169 }
170 }
171
172 // Add the new list to the serial no. field in grid with each in new line
173 item.serial_no = "";
174 for (var x=0; x<=sr_no.length - 1; x++)
175 item.serial_no += sr_no[x] + '\n';
176
177 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530178 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530179 }
180 }
181 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530182
Anand Doshi923d41d2013-05-28 17:23:36 +0530183 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530184 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530185 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530186
Anand Doshi3543f302013-05-24 19:25:01 +0530187 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530188 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530189 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530190 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530191 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530192 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530193
Akhilesh Darjee78378272013-10-11 19:06:30 +0530194 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530195 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530196 }
197 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530198 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530199 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530200
Nabin Hait48a0bd32013-07-23 15:31:39 +0530201 this.frm.script_manager.trigger("currency");
Nabin Hait9daca102014-06-20 15:59:49 +0530202 this.apply_pricing_rule();
Anand Doshi3543f302013-05-24 19:25:01 +0530203 }
204 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530205
Anand Doshi3543f302013-05-24 19:25:01 +0530206 get_company_currency: function() {
207 return erpnext.get_currency(this.frm.doc.company);
208 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530209
Anand Doshi3543f302013-05-24 19:25:01 +0530210 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530211 var me = this;
212 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530213
Anand Doshi61a2f682013-06-21 17:55:31 +0530214 var company_currency = this.get_company_currency();
215 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530216 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530217 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530218 me.frm.set_value("conversion_rate", exchange_rate);
219 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530220 });
221 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530222 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530223 }
224 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530225
Anand Doshi61a2f682013-06-21 17:55:31 +0530226 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530227 if(this.frm.doc.currency === this.get_company_currency()) {
228 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530229 }
230 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530231 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
232 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
233 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530234 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait9daca102014-06-20 15:59:49 +0530235 if(this.frm.doc.ignore_pricing_rule) {
236 this.calculate_taxes_and_totals();
237 } else {
238 this.apply_pricing_rule();
239 }
240
Nabin Haita3dd72a2014-05-28 12:49:20 +0530241 }
Anand Doshi3543f302013-05-24 19:25:01 +0530242 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530243
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530244 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530245 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530246 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
247 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530248 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530249 method: "erpnext.setup.utils.get_price_list_currency",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530250 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530251 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530252 },
Anand Doshi3543f302013-05-24 19:25:01 +0530253 callback: function(r) {
254 if(!r.exc) {
255 me.price_list_currency();
256 }
257 }
258 });
259 }
260 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530261
Anand Doshi61a2f682013-06-21 17:55:31 +0530262 get_exchange_rate: function(from_currency, to_currency, callback) {
263 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530264 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530265 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530266 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530267 });
268 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530269
Anand Doshi3543f302013-05-24 19:25:01 +0530270 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530271 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530272 this.set_dynamic_labels();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530273
Anand Doshi61a2f682013-06-21 17:55:31 +0530274 var company_currency = this.get_company_currency();
275 if(this.frm.doc.price_list_currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530276 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530277 function(exchange_rate) {
278 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530279 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530280 me.plc_conversion_rate();
281 }
282 });
283 } else {
284 this.plc_conversion_rate();
285 }
Anand Doshi3543f302013-05-24 19:25:01 +0530286 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530287
Anand Doshi3543f302013-05-24 19:25:01 +0530288 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530289 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
290 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530291 }
292 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530293 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530294 }
Anand Doshi3543f302013-05-24 19:25:01 +0530295 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530296
Anand Doshi3543f302013-05-24 19:25:01 +0530297 qty: function(doc, cdt, cdn) {
Nabin Hait9daca102014-06-20 15:59:49 +0530298 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530299 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530300
Anand Doshi652bc072014-04-16 15:21:46 +0530301 // tax rate
302 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530303 this.calculate_taxes_and_totals();
304 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530305
Anand Doshi923d41d2013-05-28 17:23:36 +0530306 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530307 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530308 try {
309 this.validate_on_previous_row(tax);
310 this.calculate_taxes_and_totals();
311 } catch(e) {
312 tax.row_id = null;
313 refresh_field("row_id", tax.name, tax.parentfield);
314 throw e;
315 }
316 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530317
Anand Doshi61a2f682013-06-21 17:55:31 +0530318 set_dynamic_labels: function() {
319 // What TODO? should we make price list system non-mandatory?
320 this.frm.toggle_reqd("plc_conversion_rate",
321 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530322
Anand Doshi61a2f682013-06-21 17:55:31 +0530323 var company_currency = this.get_company_currency();
324 this.change_form_labels(company_currency);
325 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530326 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530327 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530328
Anand Doshi923d41d2013-05-28 17:23:36 +0530329 recalculate: function() {
330 this.calculate_taxes_and_totals();
331 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530332
Anand Doshi923d41d2013-05-28 17:23:36 +0530333 recalculate_values: function() {
334 this.calculate_taxes_and_totals();
335 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530336
Anand Doshid0b00722013-05-28 18:54:48 +0530337 calculate_charges: function() {
338 this.calculate_taxes_and_totals();
339 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530340
Nabin Hait9daca102014-06-20 15:59:49 +0530341 ignore_pricing_rule: function() {
342 this.apply_pricing_rule();
343 },
344
345 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530346 var me = this;
Nabin Hait9daca102014-06-20 15:59:49 +0530347 var item_list = this._get_item_list(item);
348 var args = {
349 "item_list": item_list,
350 "customer": me.frm.doc.customer,
351 "customer_group": me.frm.doc.customer_group,
352 "territory": me.frm.doc.territory,
353 "supplier": me.frm.doc.supplier,
354 "supplier_type": me.frm.doc.supplier_type,
355 "currency": me.frm.doc.currency,
356 "conversion_rate": me.frm.doc.conversion_rate,
357 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
358 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
359 "company": me.frm.doc.company,
360 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
361 "campaign": me.frm.doc.campaign,
362 "sales_partner": me.frm.doc.sales_partner,
363 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
364 "parenttype": me.frm.doc.doctype,
365 "parent": me.frm.doc.name
366 };
367 return this.frm.call({
368 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
369 args: { args: args },
370 callback: function(r) {
371 if (!r.exc) {
372 $.each(r.message, function(i, d) {
373 $.each(d, function(k, v) {
374 if (["doctype", "name"].indexOf(k)===-1) {
375 frappe.model.set_value(d.doctype, d.name, k, v);
376 }
377 });
378 });
379 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
Nabin Haita3dd72a2014-05-28 12:49:20 +0530380 }
Nabin Hait9daca102014-06-20 15:59:49 +0530381 }
382 });
383 },
384
385 _get_item_list: function(item) {
386 var item_list = [];
387 var append_item = function(d) {
388 item_list.push({
389 "doctype": d.doctype,
390 "name": d.name,
391 "item_code": d.item_code,
392 "item_group": d.item_group,
393 "brand": d.brand,
394 "qty": d.qty
Nabin Haita3dd72a2014-05-28 12:49:20 +0530395 });
Nabin Hait9daca102014-06-20 15:59:49 +0530396 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530397
Nabin Hait9daca102014-06-20 15:59:49 +0530398 if (item) {
399 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530400 } else {
Nabin Hait9daca102014-06-20 15:59:49 +0530401 $.each(this.get_item_doclist(), function(i, d) {
402 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530403 });
404 }
Nabin Hait9daca102014-06-20 15:59:49 +0530405 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530406 },
407
Anand Doshi3543f302013-05-24 19:25:01 +0530408 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530409 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530410 try {
411 this.validate_on_previous_row(tax);
412 this.validate_inclusive_tax(tax);
413 this.calculate_taxes_and_totals();
414 } catch(e) {
415 tax.included_in_print_rate = 0;
416 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
417 throw e;
418 }
419 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530420
Anand Doshi3543f302013-05-24 19:25:01 +0530421 validate_on_previous_row: function(tax) {
422 // validate if a valid row id is mentioned in case of
423 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530424 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530425 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530426 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 +0530427 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530428 }
429 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530430
Anand Doshi3543f302013-05-24 19:25:01 +0530431 validate_inclusive_tax: function(tax) {
432 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530433
Anand Doshi3543f302013-05-24 19:25:01 +0530434 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530435 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530436 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530437 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530438
Anand Doshi3543f302013-05-24 19:25:01 +0530439 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530440 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
441 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530442 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530443 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530444
Anand Doshi3543f302013-05-24 19:25:01 +0530445 if(cint(tax.included_in_print_rate)) {
446 if(tax.charge_type == "Actual") {
447 // inclusive tax cannot be of type Actual
448 actual_type_error();
449 } else if(tax.charge_type == "On Previous Row Amount" &&
450 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
451 // referred row should also be an inclusive tax
452 on_previous_row_error(tax.row_id);
453 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530454 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530455 function(t) { return cint(t.included_in_print_rate) ? null : t; });
456 if(taxes_not_included.length > 0) {
457 // all rows above this tax should be inclusive
458 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
459 }
460 }
461 }
462 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530463
Anand Doshi3543f302013-05-24 19:25:01 +0530464 _load_item_tax_rate: function(item_tax_rate) {
465 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
466 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530467
Anand Doshi3543f302013-05-24 19:25:01 +0530468 _get_tax_rate: function(tax, item_tax_map) {
469 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
470 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
471 tax.rate;
472 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530473
Anand Doshi3543f302013-05-24 19:25:01 +0530474 get_item_wise_taxes_html: function() {
475 var item_tax = {};
476 var tax_accounts = [];
477 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530478
Anand Doshi3543f302013-05-24 19:25:01 +0530479 $.each(this.get_tax_doclist(), function(i, tax) {
480 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530481 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530482 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530483 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530484 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530485 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530486 var tax_rate = "";
487 if(tax_data[0] != null) {
488 tax_rate = (tax.charge_type === "Actual") ?
489 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
490 (flt(tax_data[0], tax_rate_precision) + "%");
491 }
492 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
493 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530494
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530495 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530496 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530497 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530498 }
Anand Doshi3543f302013-05-24 19:25:01 +0530499 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530500 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530501 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530502
503 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530504 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530505
Anand Doshi179e3772013-09-05 16:53:57 +0530506 var distinct_item_names = [];
507 var distinct_items = [];
508 $.each(this.get_item_doclist(), function(i, item) {
509 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
510 distinct_item_names.push(item.item_code || item.item_name);
511 distinct_items.push(item);
512 }
513 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530514
Anand Doshi179e3772013-09-05 16:53:57 +0530515 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530516 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530517 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530518 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
519 item_name: item.item_name,
520 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530521 return item_tax_record[head[0]] ?
522 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530523 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530524 }).join("\n")
525 });
526 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530527
Anand Doshi53b73422013-05-31 11:50:01 +0530528 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530529 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
530 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530531 <thead><tr>' + headings + '</tr></thead> \
532 <tbody>' + rows + '</tbody> \
533 </table></div>';
534 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530535
Nabin Hait139dc7b2014-02-12 14:53:18 +0530536 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530537 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530538 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530539
Nabin Hait0f231062014-02-20 11:27:47 +0530540 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530541 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530542 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530543 msgprint(__("Please specify") + ": " +
544 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530545 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530546 valid = false;
547 }
Anand Doshi3543f302013-05-24 19:25:01 +0530548 }
549 });
550 return valid;
551 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530552
Anand Doshi3543f302013-05-24 19:25:01 +0530553 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530554 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530555 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530556
Anand Doshi3543f302013-05-24 19:25:01 +0530557 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530558 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530559 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530560
Anand Doshi3543f302013-05-24 19:25:01 +0530561 validate_conversion_rate: function() {
562 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530563 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530564 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530565 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530566
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530567 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530568 frappe.throw(repl('%(conversion_rate_label)s' +
569 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
570 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530571 {
572 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530573 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530574 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530575 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530576 }
Anand Doshi3543f302013-05-24 19:25:01 +0530577 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530578
Anand Doshi3543f302013-05-24 19:25:01 +0530579 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530580 this.discount_amount_applied = false;
581 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530582 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530583 this.apply_discount_amount();
584 },
585
586 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530587 this.validate_conversion_rate();
588 this.frm.item_doclist = this.get_item_doclist();
589 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530590
Anand Doshi3543f302013-05-24 19:25:01 +0530591 this.calculate_item_values();
592 this.initialize_taxes();
593 this.determine_exclusive_rate && this.determine_exclusive_rate();
594 this.calculate_net_total();
595 this.calculate_taxes();
596 this.calculate_totals();
597 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530598
Anand Doshi3543f302013-05-24 19:25:01 +0530599 this.show_item_wise_taxes();
600 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530601
Anand Doshi3543f302013-05-24 19:25:01 +0530602 initialize_taxes: function() {
603 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530604
Anand Doshi3543f302013-05-24 19:25:01 +0530605 $.each(this.frm.tax_doclist, function(i, tax) {
606 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530607 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530608 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530609 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
610
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530611 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530612 tax_fields.push("tax_amount");
613
614 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530615
Anand Doshi3543f302013-05-24 19:25:01 +0530616 me.validate_on_previous_row(tax);
617 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530618 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530619 });
620 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530621
Anand Doshi3543f302013-05-24 19:25:01 +0530622 calculate_taxes: function() {
623 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530624 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530625
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530626 // maintain actual tax rate based on idx
627 $.each(this.frm.tax_doclist, function(i, tax) {
628 if (tax.charge_type == "Actual") {
629 actual_tax_dict[tax.idx] = flt(tax.rate);
630 }
631 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530632
Anand Doshi3543f302013-05-24 19:25:01 +0530633 $.each(this.frm.item_doclist, function(n, item) {
634 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530635
Anand Doshi3543f302013-05-24 19:25:01 +0530636 $.each(me.frm.tax_doclist, function(i, tax) {
637 // tax_amount represents the amount of tax for the current step
638 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530639
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530640 // Adjust divisional loss to the last item
641 if (tax.charge_type == "Actual") {
642 actual_tax_dict[tax.idx] -= current_tax_amount;
643 if (n == me.frm.item_doclist.length - 1) {
644 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530645 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530646 }
647
Anand Doshi3543f302013-05-24 19:25:01 +0530648 // store tax_amount for current item as it will be used for
649 // charge type = 'On Previous Row Amount'
650 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530651
Anand Doshi3543f302013-05-24 19:25:01 +0530652 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530653 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530654 tax.tax_amount += current_tax_amount;
655
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530656 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530657
Anand Doshi3543f302013-05-24 19:25:01 +0530658 // for buying
659 if(tax.category) {
660 // if just for valuation, do not add the tax amount in total
661 // hence, setting it as 0 for further steps
662 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530663
Anand Doshi3543f302013-05-24 19:25:01 +0530664 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
665 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530666
Anand Doshi3543f302013-05-24 19:25:01 +0530667 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530668 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530669 // item's amount, previously applied tax and the current tax on that item
670 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530671 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530672 precision("total", tax));
673 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530674 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530675 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
676 precision("total", tax));
677 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530678
Anand Doshi3543f302013-05-24 19:25:01 +0530679 // in tax.total, accumulate grand total for each item
680 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530681
682 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530683 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530684 me.round_off_totals(tax);
685
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530686 // adjust Discount Amount loss in last tax iteration
687 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
688 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530689 }
Anand Doshi3543f302013-05-24 19:25:01 +0530690 });
691 });
692 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530693
694 round_off_totals: function(tax) {
695 tax.total = flt(tax.total, precision("total", tax));
696 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530697 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530698 precision("tax_amount", tax));
699 },
700
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530701 adjust_discount_amount_loss: function(tax) {
702 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530703 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530704 discount_amount_loss, precision("tax_amount", tax));
705 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530706 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530707
Anand Doshi3543f302013-05-24 19:25:01 +0530708 get_current_tax_amount: function(item, tax, item_tax_map) {
709 var tax_rate = this._get_tax_rate(tax, item_tax_map);
710 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530711
Anand Doshi3543f302013-05-24 19:25:01 +0530712 if(tax.charge_type == "Actual") {
713 // distribute the tax amount proportionally to each item row
714 var actual = flt(tax.rate, precision("tax_amount", tax));
715 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530716 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530717 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530718
Anand Doshi3543f302013-05-24 19:25:01 +0530719 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530720 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530721
Anand Doshi3543f302013-05-24 19:25:01 +0530722 } else if(tax.charge_type == "On Previous Row Amount") {
723 current_tax_amount = (tax_rate / 100.0) *
724 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530725
Anand Doshi3543f302013-05-24 19:25:01 +0530726 } else if(tax.charge_type == "On Previous Row Total") {
727 current_tax_amount = (tax_rate / 100.0) *
728 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530729 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530730
Anand Doshi53b73422013-05-31 11:50:01 +0530731 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530732
Anand Doshi53b73422013-05-31 11:50:01 +0530733 // store tax breakup for each item
734 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530735
Anand Doshi53b73422013-05-31 11:50:01 +0530736 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530737 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530738
Anand Doshi3543f302013-05-24 19:25:01 +0530739 _cleanup: function() {
740 $.each(this.frm.tax_doclist, function(i, tax) {
741 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530742 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530743 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530744
Anand Doshi3543f302013-05-24 19:25:01 +0530745 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
746 });
747 },
Anand Doshifc777182013-05-27 19:29:07 +0530748
Anand Doshi652bc072014-04-16 15:21:46 +0530749 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530750 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530751 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530752 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530753 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
754 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530755
Anand Doshi652bc072014-04-16 15:21:46 +0530756 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530757 }
758 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530759
Anand Doshi3543f302013-05-24 19:25:01 +0530760 _set_in_company_currency: function(item, print_field, base_field) {
761 // set values in base currency
762 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
763 precision(base_field, item));
764 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530765
Anand Doshibf3e54a2013-06-05 14:11:32 +0530766 get_terms: function() {
767 var me = this;
768 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530769 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530770 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530771 args: {
772 doctype: "Terms and Conditions",
773 fieldname: "terms",
774 filters: { name: this.frm.doc.tc_name },
775 },
776 });
777 }
778 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530779
780 taxes_and_charges: function() {
781 var me = this;
782 if(this.frm.doc.taxes_and_charges) {
783 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530784 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
785 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530786 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
787 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530788 "master_name": this.frm.doc.taxes_and_charges,
789 "tax_parentfield": this.other_fname
790 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530791 callback: function(r) {
792 if(!r.exc) {
Nabin Hait6b039142014-05-02 15:45:10 +0530793 me.frm.set_value(me.other_fname, r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530794 me.calculate_taxes_and_totals();
795 }
796 }
797 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530798 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530799 },
800
801 show_item_wise_taxes: function() {
802 if(this.frm.fields_dict.other_charges_calculation) {
803 $(this.get_item_wise_taxes_html())
804 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
805 }
806 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530807});