blob: a4b1abbb6a61012db0477bef3272fa3cdd65edc1 [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 company: frappe.defaults.get_user_default("company"),
24 fiscal_year: frappe.defaults.get_user_default("fiscal_year"),
Anand Doshifc777182013-05-27 19:29:07 +053025 is_subcontracted: "No",
Anand Doshi3543f302013-05-24 19:25:01 +053026 }, function(fieldname, value) {
27 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
28 me.frm.set_value(fieldname, value);
Rushabh Mehtaff938022014-04-15 18:40:00 +053029 });
Anand Doshi3543f302013-05-24 19:25:01 +053030 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +053031
Anand Doshi1e4fb682013-08-23 12:56:33 +053032 if(this.other_fname) {
Anand Doshia91c95f2013-08-23 13:00:47 +053033 this[this.other_fname + "_remove"] = this.calculate_taxes_and_totals;
34 }
Rushabh Mehtaff938022014-04-15 18:40:00 +053035
Anand Doshia91c95f2013-08-23 13:00:47 +053036 if(this.fname) {
37 this[this.fname + "_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +053038 }
Anand Doshi3543f302013-05-24 19:25:01 +053039 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053040
Anand Doshi5e4e5d72013-08-06 17:23:44 +053041 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053042 var me = this;
Anand Doshi221dec82014-06-03 14:12:49 +053043 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 +053044 this.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +053045 }
Rushabh Mehtab6843f22014-06-04 13:10:41 +053046 if(frappe.meta.get_docfield(this.tname, "item_code")) {
Rushabh Mehta880b3ef2014-06-06 11:45:11 +053047 cur_frm.get_field(this.fname).grid.set_multiple_add("item_code", "qty");
Rushabh Mehtab6843f22014-06-04 13:10:41 +053048 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053049 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053050
Anand Doshi3543f302013-05-24 19:25:01 +053051 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +053052 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053053 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053054 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053055 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053056
57 // Show POS button only if it is enabled from features setup
58 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
Rushabh Mehtae34090a2013-11-15 11:30:23 +053059 this.make_pos_btn();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053060 },
61
Rushabh Mehtae34090a2013-11-15 11:30:23 +053062 make_pos_btn: function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053063 if(!this.pos_active) {
Pratik Vyasb52618c2014-04-14 16:25:30 +053064 var btn_label = __("POS View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053065 icon = "icon-desktop";
66 } else {
Pratik Vyasb52618c2014-04-14 16:25:30 +053067 var btn_label = __(this.frm.doctype) + __(" View"),
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053068 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053069 }
70 var me = this;
Rushabh Mehtaff938022014-04-15 18:40:00 +053071
Rushabh Mehtae34090a2013-11-15 11:30:23 +053072 this.$pos_btn = this.frm.appframe.add_button(btn_label, function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053073 me.toggle_pos();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053074 }, icon);
75 },
76
77 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053078 // Check whether it is Selling or Buying cycle
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053079 var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053080 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
Rushabh Mehtaff938022014-04-15 18:40:00 +053081
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053082 if (!price_list)
Pratik Vyasb52618c2014-04-14 16:25:30 +053083 msgprint(__("Please select Price List"))
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053084 else {
85 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
86
87 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053088 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053089 this.frm.layout.add_view("pos");
90 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
91 }
92
93 // toggle view
94 this.frm.layout.set_view(this.pos_active ? "" : "pos");
95 this.pos_active = !this.pos_active;
96
97 // refresh
98 if(this.pos_active)
99 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530100 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530101 }
Anand Doshi3543f302013-05-24 19:25:01 +0530102 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530103
104
Nabin Hait139dc7b2014-02-12 14:53:18 +0530105 item_code: function(doc, cdt, cdn) {
106 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530107 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530108 if(item.item_code || item.barcode || item.serial_no) {
109 if(!this.validate_company_and_party()) {
110 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
111 } else {
112 return this.frm.call({
113 method: "erpnext.stock.get_item_details.get_item_details",
114 child: item,
115 args: {
116 args: {
117 item_code: item.item_code,
118 barcode: item.barcode,
119 serial_no: item.serial_no,
120 warehouse: item.warehouse,
Nabin Hait444f9562014-06-20 15:59:49 +0530121 parenttype: me.frm.doc.doctype,
122 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530123 customer: me.frm.doc.customer,
124 supplier: me.frm.doc.supplier,
125 currency: me.frm.doc.currency,
126 conversion_rate: me.frm.doc.conversion_rate,
127 price_list: me.frm.doc.selling_price_list ||
128 me.frm.doc.buying_price_list,
129 price_list_currency: me.frm.doc.price_list_currency,
130 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
131 company: me.frm.doc.company,
132 order_type: me.frm.doc.order_type,
133 is_pos: cint(me.frm.doc.is_pos),
134 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Hait444f9562014-06-20 15:59:49 +0530135 transaction_date: me.frm.doc.transaction_date,
136 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
137 doctype: item.doctype,
138 name: item.name
Nabin Hait139dc7b2014-02-12 14:53:18 +0530139 }
140 },
141 callback: function(r) {
142 if(!r.exc) {
143 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
144 }
145 }
146 });
147 }
148 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530149 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530150
151 serial_no: function(doc, cdt, cdn) {
152 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530153 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530154
155 if (item.serial_no) {
156 if (!item.item_code) {
157 this.frm.script_manager.trigger("item_code", cdt, cdn);
158 }
159 else {
160 var sr_no = [];
161
162 // Replacing all occurences of comma with carriage return
163 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
164
165 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530166
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530167 // Trim each string and push unique string to new list
168 for (var x=0; x<=serial_nos.length - 1; x++) {
169 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
170 sr_no.push(serial_nos[x].trim());
171 }
172 }
173
174 // Add the new list to the serial no. field in grid with each in new line
175 item.serial_no = "";
176 for (var x=0; x<=sr_no.length - 1; x++)
177 item.serial_no += sr_no[x] + '\n';
178
179 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530180 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530181 }
182 }
183 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530184
Anand Doshi923d41d2013-05-28 17:23:36 +0530185 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530186 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530187 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530188
Anand Doshi3543f302013-05-24 19:25:01 +0530189 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530190 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530191 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530192 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530193 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530194 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530195
Akhilesh Darjee78378272013-10-11 19:06:30 +0530196 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530197 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530198 }
199 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530200 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530201 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530202
Nabin Hait48a0bd32013-07-23 15:31:39 +0530203 this.frm.script_manager.trigger("currency");
Nabin Hait444f9562014-06-20 15:59:49 +0530204 this.apply_pricing_rule();
Anand Doshi3543f302013-05-24 19:25:01 +0530205 }
206 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530207
Anand Doshi3543f302013-05-24 19:25:01 +0530208 get_company_currency: function() {
209 return erpnext.get_currency(this.frm.doc.company);
210 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530211
Anand Doshi3543f302013-05-24 19:25:01 +0530212 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530213 var me = this;
214 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530215
Anand Doshi61a2f682013-06-21 17:55:31 +0530216 var company_currency = this.get_company_currency();
217 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530218 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530219 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530220 me.frm.set_value("conversion_rate", exchange_rate);
221 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530222 });
223 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530224 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530225 }
226 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530227
Anand Doshi61a2f682013-06-21 17:55:31 +0530228 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530229 if(this.frm.doc.currency === this.get_company_currency()) {
230 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530231 }
232 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530233 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
234 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
235 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530236 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530237 if(this.frm.doc.ignore_pricing_rule) {
238 this.calculate_taxes_and_totals();
239 } else {
240 this.apply_pricing_rule();
241 }
242
Nabin Haita3dd72a2014-05-28 12:49:20 +0530243 }
Anand Doshi3543f302013-05-24 19:25:01 +0530244 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530245
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530246 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530247 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530248 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
249 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530250 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530251 method: "erpnext.setup.utils.get_price_list_currency",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530252 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530253 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530254 },
Anand Doshi3543f302013-05-24 19:25:01 +0530255 callback: function(r) {
256 if(!r.exc) {
257 me.price_list_currency();
258 }
259 }
260 });
261 }
262 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530263
Anand Doshi61a2f682013-06-21 17:55:31 +0530264 get_exchange_rate: function(from_currency, to_currency, callback) {
265 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530266 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530267 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530268 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530269 });
270 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530271
Anand Doshi3543f302013-05-24 19:25:01 +0530272 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530273 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530274 this.set_dynamic_labels();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530275
Anand Doshi61a2f682013-06-21 17:55:31 +0530276 var company_currency = this.get_company_currency();
277 if(this.frm.doc.price_list_currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530278 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530279 function(exchange_rate) {
280 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530281 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530282 me.plc_conversion_rate();
283 }
284 });
285 } else {
286 this.plc_conversion_rate();
287 }
Anand Doshi3543f302013-05-24 19:25:01 +0530288 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530289
Anand Doshi3543f302013-05-24 19:25:01 +0530290 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530291 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
292 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530293 }
294 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530295 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530296 }
Anand Doshi3543f302013-05-24 19:25:01 +0530297 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530298
Anand Doshi3543f302013-05-24 19:25:01 +0530299 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530300 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530301 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530302
Anand Doshi652bc072014-04-16 15:21:46 +0530303 // tax rate
304 rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530305 this.calculate_taxes_and_totals();
306 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530307
Anand Doshi923d41d2013-05-28 17:23:36 +0530308 row_id: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530309 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530310 try {
311 this.validate_on_previous_row(tax);
312 this.calculate_taxes_and_totals();
313 } catch(e) {
314 tax.row_id = null;
315 refresh_field("row_id", tax.name, tax.parentfield);
316 throw e;
317 }
318 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530319
Anand Doshi61a2f682013-06-21 17:55:31 +0530320 set_dynamic_labels: function() {
321 // What TODO? should we make price list system non-mandatory?
322 this.frm.toggle_reqd("plc_conversion_rate",
323 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530324
Anand Doshi61a2f682013-06-21 17:55:31 +0530325 var company_currency = this.get_company_currency();
326 this.change_form_labels(company_currency);
327 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530328 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530329 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530330
Anand Doshi923d41d2013-05-28 17:23:36 +0530331 recalculate: function() {
332 this.calculate_taxes_and_totals();
333 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530334
Anand Doshi923d41d2013-05-28 17:23:36 +0530335 recalculate_values: function() {
336 this.calculate_taxes_and_totals();
337 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530338
Anand Doshid0b00722013-05-28 18:54:48 +0530339 calculate_charges: function() {
340 this.calculate_taxes_and_totals();
341 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530342
Nabin Hait444f9562014-06-20 15:59:49 +0530343 ignore_pricing_rule: function() {
344 this.apply_pricing_rule();
345 },
346
347 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530348 var me = this;
Nabin Hait444f9562014-06-20 15:59:49 +0530349 var item_list = this._get_item_list(item);
350 var args = {
351 "item_list": item_list,
352 "customer": me.frm.doc.customer,
353 "customer_group": me.frm.doc.customer_group,
354 "territory": me.frm.doc.territory,
355 "supplier": me.frm.doc.supplier,
356 "supplier_type": me.frm.doc.supplier_type,
357 "currency": me.frm.doc.currency,
358 "conversion_rate": me.frm.doc.conversion_rate,
359 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
360 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
361 "company": me.frm.doc.company,
362 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
363 "campaign": me.frm.doc.campaign,
364 "sales_partner": me.frm.doc.sales_partner,
365 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
366 "parenttype": me.frm.doc.doctype,
367 "parent": me.frm.doc.name
368 };
369 return this.frm.call({
370 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
371 args: { args: args },
372 callback: function(r) {
373 if (!r.exc) {
374 $.each(r.message, function(i, d) {
375 $.each(d, function(k, v) {
376 if (["doctype", "name"].indexOf(k)===-1) {
377 frappe.model.set_value(d.doctype, d.name, k, v);
378 }
379 });
380 });
381 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
Nabin Haita3dd72a2014-05-28 12:49:20 +0530382 }
Nabin Hait444f9562014-06-20 15:59:49 +0530383 }
384 });
385 },
386
387 _get_item_list: function(item) {
388 var item_list = [];
389 var append_item = function(d) {
390 item_list.push({
391 "doctype": d.doctype,
392 "name": d.name,
393 "item_code": d.item_code,
394 "item_group": d.item_group,
395 "brand": d.brand,
396 "qty": d.qty
Nabin Haita3dd72a2014-05-28 12:49:20 +0530397 });
Nabin Hait444f9562014-06-20 15:59:49 +0530398 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530399
Nabin Hait444f9562014-06-20 15:59:49 +0530400 if (item) {
401 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530402 } else {
Nabin Hait444f9562014-06-20 15:59:49 +0530403 $.each(this.get_item_doclist(), function(i, d) {
404 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530405 });
406 }
Nabin Hait444f9562014-06-20 15:59:49 +0530407 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530408 },
409
Anand Doshi3543f302013-05-24 19:25:01 +0530410 included_in_print_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530411 var tax = frappe.get_doc(cdt, cdn);
Anand Doshi3543f302013-05-24 19:25:01 +0530412 try {
413 this.validate_on_previous_row(tax);
414 this.validate_inclusive_tax(tax);
415 this.calculate_taxes_and_totals();
416 } catch(e) {
417 tax.included_in_print_rate = 0;
418 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
419 throw e;
420 }
421 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530422
Anand Doshi3543f302013-05-24 19:25:01 +0530423 validate_on_previous_row: function(tax) {
424 // validate if a valid row id is mentioned in case of
425 // On Previous Row Amount and On Previous Row Total
Rushabh Mehtaff938022014-04-15 18:40:00 +0530426 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530427 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530428 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 +0530429 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530430 }
431 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530432
Anand Doshi3543f302013-05-24 19:25:01 +0530433 validate_inclusive_tax: function(tax) {
434 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530435
Anand Doshi3543f302013-05-24 19:25:01 +0530436 var actual_type_error = function() {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530437 var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530438 frappe.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530439 };
Rushabh Mehtaff938022014-04-15 18:40:00 +0530440
Anand Doshi3543f302013-05-24 19:25:01 +0530441 var on_previous_row_error = function(row_range) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530442 var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
443 [tax.idx, __(tax.doctype), tax.charge_type, row_range])
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 if(cint(tax.included_in_print_rate)) {
448 if(tax.charge_type == "Actual") {
449 // inclusive tax cannot be of type Actual
450 actual_type_error();
451 } else if(tax.charge_type == "On Previous Row Amount" &&
452 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
453 // referred row should also be an inclusive tax
454 on_previous_row_error(tax.row_id);
455 } else if(tax.charge_type == "On Previous Row Total") {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530456 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
Anand Doshi3543f302013-05-24 19:25:01 +0530457 function(t) { return cint(t.included_in_print_rate) ? null : t; });
458 if(taxes_not_included.length > 0) {
459 // all rows above this tax should be inclusive
460 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
461 }
462 }
463 }
464 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530465
Anand Doshi3543f302013-05-24 19:25:01 +0530466 _load_item_tax_rate: function(item_tax_rate) {
467 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
468 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530469
Anand Doshi3543f302013-05-24 19:25:01 +0530470 _get_tax_rate: function(tax, item_tax_map) {
471 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
472 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
473 tax.rate;
474 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530475
Anand Doshi3543f302013-05-24 19:25:01 +0530476 get_item_wise_taxes_html: function() {
477 var item_tax = {};
478 var tax_accounts = [];
479 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530480
Anand Doshi3543f302013-05-24 19:25:01 +0530481 $.each(this.get_tax_doclist(), function(i, tax) {
482 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530483 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530484 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530485 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530486 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530487 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530488 var tax_rate = "";
489 if(tax_data[0] != null) {
490 tax_rate = (tax.charge_type === "Actual") ?
491 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
492 (flt(tax_data[0], tax_rate_precision) + "%");
493 }
494 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
495 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530496
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530497 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530498 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530499 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530500 }
Anand Doshi3543f302013-05-24 19:25:01 +0530501 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530502 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530503 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530504
505 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530506 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530507
Anand Doshi179e3772013-09-05 16:53:57 +0530508 var distinct_item_names = [];
509 var distinct_items = [];
510 $.each(this.get_item_doclist(), function(i, item) {
511 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
512 distinct_item_names.push(item.item_code || item.item_name);
513 distinct_items.push(item);
514 }
515 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530516
Anand Doshi179e3772013-09-05 16:53:57 +0530517 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530518 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530519 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530520 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
521 item_name: item.item_name,
522 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530523 return item_tax_record[head[0]] ?
524 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530525 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530526 }).join("\n")
527 });
528 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530529
Anand Doshi53b73422013-05-31 11:50:01 +0530530 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530531 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
532 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530533 <thead><tr>' + headings + '</tr></thead> \
534 <tbody>' + rows + '</tbody> \
535 </table></div>';
536 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530537
Nabin Hait139dc7b2014-02-12 14:53:18 +0530538 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530539 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530540 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530541
Nabin Hait0f231062014-02-20 11:27:47 +0530542 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530543 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530544 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530545 msgprint(__("Please specify") + ": " +
546 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530547 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530548 valid = false;
549 }
Anand Doshi3543f302013-05-24 19:25:01 +0530550 }
551 });
552 return valid;
553 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530554
Anand Doshi3543f302013-05-24 19:25:01 +0530555 get_item_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530556 return this.frm.doc[this.fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530557 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530558
Anand Doshi3543f302013-05-24 19:25:01 +0530559 get_tax_doclist: function() {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530560 return this.frm.doc[this.other_fname] || [];
Anand Doshi3543f302013-05-24 19:25:01 +0530561 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530562
Anand Doshi3543f302013-05-24 19:25:01 +0530563 validate_conversion_rate: function() {
564 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530565 var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
Anand Doshi3543f302013-05-24 19:25:01 +0530566 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530567 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530568
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530569 if(!this.frm.doc.conversion_rate) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530570 frappe.throw(repl('%(conversion_rate_label)s' +
571 __(' is mandatory. Maybe Currency Exchange record is not created for ') +
572 '%(from_currency)s' + __(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530573 {
574 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530575 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530576 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530577 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530578 }
Anand Doshi3543f302013-05-24 19:25:01 +0530579 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530580
Anand Doshi3543f302013-05-24 19:25:01 +0530581 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530582 this.discount_amount_applied = false;
583 this._calculate_taxes_and_totals();
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530584 if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530585 this.apply_discount_amount();
586 },
587
588 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530589 this.validate_conversion_rate();
590 this.frm.item_doclist = this.get_item_doclist();
591 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530592
Anand Doshi3543f302013-05-24 19:25:01 +0530593 this.calculate_item_values();
594 this.initialize_taxes();
595 this.determine_exclusive_rate && this.determine_exclusive_rate();
596 this.calculate_net_total();
597 this.calculate_taxes();
598 this.calculate_totals();
599 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530600
Anand Doshi3543f302013-05-24 19:25:01 +0530601 this.show_item_wise_taxes();
602 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530603
Anand Doshi3543f302013-05-24 19:25:01 +0530604 initialize_taxes: function() {
605 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530606
Anand Doshi3543f302013-05-24 19:25:01 +0530607 $.each(this.frm.tax_doclist, function(i, tax) {
608 tax.item_wise_tax_detail = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530609 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530610 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530611 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
612
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530613 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530614 tax_fields.push("tax_amount");
615
616 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530617
Anand Doshi3543f302013-05-24 19:25:01 +0530618 me.validate_on_previous_row(tax);
619 me.validate_inclusive_tax(tax);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530620 frappe.model.round_floats_in(tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530621 });
622 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530623
Anand Doshi3543f302013-05-24 19:25:01 +0530624 calculate_taxes: function() {
625 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530626 var actual_tax_dict = {};
Rushabh Mehtaff938022014-04-15 18:40:00 +0530627
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530628 // maintain actual tax rate based on idx
629 $.each(this.frm.tax_doclist, function(i, tax) {
630 if (tax.charge_type == "Actual") {
631 actual_tax_dict[tax.idx] = flt(tax.rate);
632 }
633 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530634
Anand Doshi3543f302013-05-24 19:25:01 +0530635 $.each(this.frm.item_doclist, function(n, item) {
636 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530637
Anand Doshi3543f302013-05-24 19:25:01 +0530638 $.each(me.frm.tax_doclist, function(i, tax) {
639 // tax_amount represents the amount of tax for the current step
640 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530641
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530642 // Adjust divisional loss to the last item
643 if (tax.charge_type == "Actual") {
644 actual_tax_dict[tax.idx] -= current_tax_amount;
645 if (n == me.frm.item_doclist.length - 1) {
646 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530647 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530648 }
649
Anand Doshi3543f302013-05-24 19:25:01 +0530650 // store tax_amount for current item as it will be used for
651 // charge type = 'On Previous Row Amount'
652 tax.tax_amount_for_current_item = current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530653
Anand Doshi3543f302013-05-24 19:25:01 +0530654 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530655 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530656 tax.tax_amount += current_tax_amount;
657
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530658 tax.tax_amount_after_discount_amount += current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530659
Anand Doshi3543f302013-05-24 19:25:01 +0530660 // for buying
661 if(tax.category) {
662 // if just for valuation, do not add the tax amount in total
663 // hence, setting it as 0 for further steps
664 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530665
Anand Doshi3543f302013-05-24 19:25:01 +0530666 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
667 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530668
Anand Doshi3543f302013-05-24 19:25:01 +0530669 // Calculate tax.total viz. grand total till that step
Rushabh Mehtaff938022014-04-15 18:40:00 +0530670 // note: grand_total_for_current_item contains the contribution of
Anand Doshi3543f302013-05-24 19:25:01 +0530671 // item's amount, previously applied tax and the current tax on that item
672 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530673 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530674 precision("total", tax));
675 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530676 tax.grand_total_for_current_item =
Anand Doshi3543f302013-05-24 19:25:01 +0530677 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
678 precision("total", tax));
679 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530680
Anand Doshi3543f302013-05-24 19:25:01 +0530681 // in tax.total, accumulate grand total for each item
682 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530683
684 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530685 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530686 me.round_off_totals(tax);
687
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530688 // adjust Discount Amount loss in last tax iteration
689 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
690 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530691 }
Anand Doshi3543f302013-05-24 19:25:01 +0530692 });
693 });
694 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530695
696 round_off_totals: function(tax) {
697 tax.total = flt(tax.total, precision("total", tax));
698 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530699 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530700 precision("tax_amount", tax));
701 },
702
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530703 adjust_discount_amount_loss: function(tax) {
704 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530705 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530706 discount_amount_loss, precision("tax_amount", tax));
707 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530708 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530709
Anand Doshi3543f302013-05-24 19:25:01 +0530710 get_current_tax_amount: function(item, tax, item_tax_map) {
711 var tax_rate = this._get_tax_rate(tax, item_tax_map);
712 var current_tax_amount = 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530713
Anand Doshi3543f302013-05-24 19:25:01 +0530714 if(tax.charge_type == "Actual") {
715 // distribute the tax amount proportionally to each item row
716 var actual = flt(tax.rate, precision("tax_amount", tax));
717 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530718 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530719 0.0;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530720
Anand Doshi3543f302013-05-24 19:25:01 +0530721 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530722 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530723
Anand Doshi3543f302013-05-24 19:25:01 +0530724 } else if(tax.charge_type == "On Previous Row Amount") {
725 current_tax_amount = (tax_rate / 100.0) *
726 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530727
Anand Doshi3543f302013-05-24 19:25:01 +0530728 } else if(tax.charge_type == "On Previous Row Total") {
729 current_tax_amount = (tax_rate / 100.0) *
730 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530731 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530732
Anand Doshi53b73422013-05-31 11:50:01 +0530733 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530734
Anand Doshi53b73422013-05-31 11:50:01 +0530735 // store tax breakup for each item
736 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
Rushabh Mehtaff938022014-04-15 18:40:00 +0530737
Anand Doshi53b73422013-05-31 11:50:01 +0530738 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530739 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530740
Anand Doshi3543f302013-05-24 19:25:01 +0530741 _cleanup: function() {
742 $.each(this.frm.tax_doclist, function(i, tax) {
743 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
Rushabh Mehtaff938022014-04-15 18:40:00 +0530744 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
Anand Doshi3543f302013-05-24 19:25:01 +0530745 function(i, fieldname) { delete tax[fieldname]; });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530746
Anand Doshi3543f302013-05-24 19:25:01 +0530747 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
748 });
749 },
Anand Doshifc777182013-05-27 19:29:07 +0530750
Anand Doshi652bc072014-04-16 15:21:46 +0530751 calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
Anand Doshifc777182013-05-27 19:29:07 +0530752 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530753 var advance_doclist = this.frm.doc[advance_parentfield] || [];
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530754 this.frm.doc.total_advance = flt(frappe.utils.sum(
Anand Doshifc777182013-05-27 19:29:07 +0530755 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
756 ), precision("total_advance"));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530757
Anand Doshi652bc072014-04-16 15:21:46 +0530758 this.calculate_outstanding_amount(update_paid_amount);
Anand Doshifc777182013-05-27 19:29:07 +0530759 }
760 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530761
Anand Doshi3543f302013-05-24 19:25:01 +0530762 _set_in_company_currency: function(item, print_field, base_field) {
763 // set values in base currency
764 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
765 precision(base_field, item));
766 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530767
Anand Doshibf3e54a2013-06-05 14:11:32 +0530768 get_terms: function() {
769 var me = this;
770 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530771 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530772 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530773 args: {
774 doctype: "Terms and Conditions",
775 fieldname: "terms",
776 filters: { name: this.frm.doc.tc_name },
777 },
778 });
779 }
780 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530781
782 taxes_and_charges: function() {
783 var me = this;
784 if(this.frm.doc.taxes_and_charges) {
785 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530786 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
787 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530788 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
789 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530790 "master_name": this.frm.doc.taxes_and_charges,
791 "tax_parentfield": this.other_fname
792 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530793 callback: function(r) {
794 if(!r.exc) {
Nabin Hait6b039142014-05-02 15:45:10 +0530795 me.frm.set_value(me.other_fname, r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530796 me.calculate_taxes_and_totals();
797 }
798 }
799 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530800 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530801 },
802
803 show_item_wise_taxes: function() {
804 if(this.frm.fields_dict.other_charges_calculation) {
805 $(this.get_item_wise_taxes_html())
806 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
807 }
808 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530809});