blob: 2f25deca83a95fa8e63d45f2ac1840bda21758c7 [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
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05303
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05304// Preset
5// ------
6// cur_frm.cscript.tname - Details table name
7// cur_frm.cscript.fname - Details fieldname
Rushabh Mehta6de403f2013-12-13 14:10:14 +05308// cur_frm.cscript.other_fname - fieldname
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05309// cur_frm.cscript.sales_team_fname - Sales Team fieldname
10
Anand Doshi1dde46a2013-05-15 21:15:57 +053011wn.provide("erpnext.selling");
Rushabh Mehta6de403f2013-12-13 14:10:14 +053012wn.require("assets/erpnext/js/transaction.js");
Rushabh Mehtab09d9da2014-01-02 11:47:23 +053013
Rushabh Mehta6de403f2013-12-13 14:10:14 +053014{% include "public/js/controllers/accounts.js" %}
Anand Doshi1dde46a2013-05-15 21:15:57 +053015
Anand Doshi3543f302013-05-24 19:25:01 +053016erpnext.selling.SellingController = erpnext.TransactionController.extend({
Anand Doshi9b496142013-07-11 19:13:58 +053017 onload: function() {
18 this._super();
19 this.toggle_rounded_total();
20 this.setup_queries();
Anand Doshi33fe8672013-08-02 12:39:10 +053021 this.toggle_editable_price_list_rate();
Anand Doshi9b496142013-07-11 19:13:58 +053022 },
23
24 setup_queries: function() {
Anand Doshi99100a42013-07-04 17:13:53 +053025 var me = this;
26
Anand Doshif3096132013-05-21 19:35:06 +053027 this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate");
Anand Doshi8f9f8a42013-06-28 19:18:33 +053028
Anand Doshid5d39ac2013-07-29 13:28:37 +053029 $.each([["customer_address", "customer_filter"],
30 ["shipping_address_name", "customer_filter"],
31 ["contact_person", "customer_filter"],
32 ["customer", "customer"],
33 ["lead", "lead"]],
34 function(i, opts) {
35 if(me.frm.fields_dict[opts[0]])
36 me.frm.set_query(opts[0], erpnext.queries[opts[1]]);
37 });
Anand Doshi99100a42013-07-04 17:13:53 +053038
39 if(this.frm.fields_dict.charge) {
40 this.frm.set_query("charge", function() {
Saurabhf52dc072013-07-10 13:07:49 +053041 return {
42 filters: [
43 ['Sales Taxes and Charges Master', 'company', '=', me.frm.doc.company],
Saurabhf52dc072013-07-10 13:07:49 +053044 ['Sales Taxes and Charges Master', 'docstatus', '!=', 2]
45 ]
46 }
Anand Doshi99100a42013-07-04 17:13:53 +053047 });
48 }
Anand Doshi99100a42013-07-04 17:13:53 +053049
Rushabh Mehta4a404e92013-08-09 18:11:35 +053050 if(this.frm.fields_dict.selling_price_list) {
51 this.frm.set_query("selling_price_list", function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +053052 return { filters: { selling: 1 } };
Anand Doshi720a01a2013-07-26 11:32:02 +053053 });
Anand Doshi720a01a2013-07-26 11:32:02 +053054 }
55
Anand Doshi99100a42013-07-04 17:13:53 +053056 if(!this.fname) {
57 return;
58 }
59
60 if(this.frm.fields_dict[this.fname].grid.get_field('item_code')) {
61 this.frm.set_query("item_code", this.fname, function() {
Anand Doshi9b496142013-07-11 19:13:58 +053062 return {
Rushabh Mehta6de403f2013-12-13 14:10:14 +053063 query: "erpnext.controllers.queries.item_query",
Anand Doshi9b496142013-07-11 19:13:58 +053064 filters: (me.frm.doc.order_type === "Maintenance" ?
65 {'is_service_item': 'Yes'}:
66 {'is_sales_item': 'Yes' })
67 }
Anand Doshi99100a42013-07-04 17:13:53 +053068 });
69 }
70
71 if(this.frm.fields_dict[this.fname].grid.get_field('batch_no')) {
72 this.frm.set_query("batch_no", this.fname, function(doc, cdt, cdn) {
73 var item = wn.model.get_doc(cdt, cdn);
74 if(!item.item_code) {
Bárbara Perretti4098c262013-09-27 17:05:17 -030075 wn.throw(wn._("Please enter Item Code to get batch no"));
Anand Doshi99100a42013-07-04 17:13:53 +053076 } else {
Nabin Haitd1fd1e22013-10-18 12:29:11 +053077 filters = {
78 'item_code': item.item_code,
79 'posting_date': me.frm.doc.posting_date,
80 }
81 if(item.warehouse) filters["warehouse"] = item.warehouse
82
83 return {
84 query : "controllers.queries.get_batch_no",
85 filters: filters
Anand Doshi99100a42013-07-04 17:13:53 +053086 }
87 }
88 });
89 }
Anand Doshied698922013-07-23 15:16:50 +053090
91 if(this.frm.fields_dict.sales_team && this.frm.fields_dict.sales_team.grid.get_field("sales_person")) {
Anand Doshid5d39ac2013-07-29 13:28:37 +053092 this.frm.set_query("sales_person", "sales_team", erpnext.queries.not_a_group_filter);
Anand Doshied698922013-07-23 15:16:50 +053093 }
Anand Doshi1dde46a2013-05-15 21:15:57 +053094 },
95
Anand Doshic4a54fe2013-08-01 18:19:51 +053096 refresh: function() {
97 this._super();
Rushabh Mehta8aded132013-07-04 12:50:52 +053098 this.frm.toggle_display("customer_name",
Akhilesh Darjee5ce1b8b2013-12-09 16:29:04 +053099 (this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer));
Anand Doshic4a54fe2013-08-01 18:19:51 +0530100 if(this.frm.fields_dict.packing_details) {
101 var packing_list_exists = this.frm.get_doclist({parentfield: "packing_details"}).length;
102 this.frm.toggle_display("packing_list", packing_list_exists ? true : false);
103 }
Rushabh Mehta8aded132013-07-04 12:50:52 +0530104 },
105
Anand Doshi3543f302013-05-24 19:25:01 +0530106 customer: function() {
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530107 erpnext.utils.get_party_details(this.frm);
Anand Doshi3543f302013-05-24 19:25:01 +0530108 },
109
Nabin Haita279d782013-07-15 13:04:33 +0530110 customer_address: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530111 erpnext.utils.get_address_display(this.frm, "customer_address");
Nabin Haita279d782013-07-15 13:04:33 +0530112 },
113
114 contact_person: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530115 erpnext.utils.get_contact_details(this.frm);
Nabin Haita279d782013-07-15 13:04:33 +0530116 },
117
Anand Doshif3096132013-05-21 19:35:06 +0530118 barcode: function(doc, cdt, cdn) {
119 this.item_code(doc, cdt, cdn);
Anand Doshi1dde46a2013-05-15 21:15:57 +0530120 },
121
122 item_code: function(doc, cdt, cdn) {
123 var me = this;
124 var item = wn.model.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530125 if(item.item_code || item.barcode || item.serial_no) {
Anand Doshi3543f302013-05-24 19:25:01 +0530126 if(!this.validate_company_and_party("customer")) {
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530127 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
Anand Doshi1dde46a2013-05-15 21:15:57 +0530128 } else {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530129 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530130 method: "erpnext.selling.utils.get_item_details",
Anand Doshi1dde46a2013-05-15 21:15:57 +0530131 child: item,
132 args: {
133 args: {
134 item_code: item.item_code,
Anand Doshif3096132013-05-21 19:35:06 +0530135 barcode: item.barcode,
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530136 serial_no: item.serial_no,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530137 warehouse: item.warehouse,
138 doctype: me.frm.doc.doctype,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530139 parentfield: item.parentfield,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530140 customer: me.frm.doc.customer,
141 currency: me.frm.doc.currency,
142 conversion_rate: me.frm.doc.conversion_rate,
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530143 selling_price_list: me.frm.doc.selling_price_list,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530144 price_list_currency: me.frm.doc.price_list_currency,
145 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
146 company: me.frm.doc.company,
Anand Doshif3096132013-05-21 19:35:06 +0530147 order_type: me.frm.doc.order_type,
148 is_pos: cint(me.frm.doc.is_pos),
Anand Doshi1dde46a2013-05-15 21:15:57 +0530149 }
150 },
151 callback: function(r) {
Anand Doshif3096132013-05-21 19:35:06 +0530152 if(!r.exc) {
Anand Doshi80166e22013-07-17 14:49:13 +0530153 me.frm.script_manager.trigger("ref_rate", cdt, cdn);
Anand Doshif3096132013-05-21 19:35:06 +0530154 }
Anand Doshi1dde46a2013-05-15 21:15:57 +0530155 }
156 });
157 }
158 }
159 },
160
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530161 selling_price_list: function() {
162 this.get_price_list_currency("Selling");
Anand Doshif3096132013-05-21 19:35:06 +0530163 },
164
165 ref_rate: function(doc, cdt, cdn) {
166 var item = wn.model.get_doc(cdt, cdn);
167 wn.model.round_floats_in(item, ["ref_rate", "adj_rate"]);
168
169 item.export_rate = flt(item.ref_rate * (1 - item.adj_rate / 100.0),
170 precision("export_rate", item));
171
172 this.calculate_taxes_and_totals();
173 },
174
Anand Doshif3096132013-05-21 19:35:06 +0530175 adj_rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530176 var item = wn.model.get_doc(cdt, cdn);
177 if(!item.ref_rate) {
178 item.adj_rate = 0.0;
179 } else {
180 this.ref_rate(doc, cdt, cdn);
181 }
Anand Doshif3096132013-05-21 19:35:06 +0530182 },
183
184 export_rate: function(doc, cdt, cdn) {
185 var item = wn.model.get_doc(cdt, cdn);
186 wn.model.round_floats_in(item, ["export_rate", "ref_rate"]);
187
188 if(item.ref_rate) {
189 item.adj_rate = flt((1 - item.export_rate / item.ref_rate) * 100.0,
190 precision("adj_rate", item));
191 } else {
192 item.adj_rate = 0.0;
193 }
194
195 this.calculate_taxes_and_totals();
196 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530197
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530198 discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530199 this.calculate_taxes_and_totals();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530200 },
Anand Doshif3096132013-05-21 19:35:06 +0530201
Anand Doshif3096132013-05-21 19:35:06 +0530202 commission_rate: function() {
203 this.calculate_commission();
204 refresh_field("total_commission");
205 },
206
207 total_commission: function() {
208 if(this.frm.doc.net_total) {
209 wn.model.round_floats_in(this.frm.doc, ["net_total", "total_commission"]);
210
211 if(this.frm.doc.net_total < this.frm.doc.total_commission) {
212 var msg = (wn._("[Error]") + " " +
213 wn._(wn.meta.get_label(this.frm.doc.doctype, "total_commission",
214 this.frm.doc.name)) + " > " +
215 wn._(wn.meta.get_label(this.frm.doc.doctype, "net_total", this.frm.doc.name)));
216 msgprint(msg);
217 throw msg;
218 }
219
220 this.frm.set_value("commission_rate",
221 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.net_total));
222 }
223 },
224
225 allocated_percentage: function(doc, cdt, cdn) {
226 var sales_person = wn.model.get_doc(cdt, cdn);
227
228 if(sales_person.allocated_percentage) {
229 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
230 precision("allocated_percentage", sales_person));
231 sales_person.allocated_amount = flt(this.frm.doc.net_total *
232 sales_person.allocated_percentage / 100.0,
233 precision("allocated_amount", sales_person));
234
235 refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name,
236 sales_person.parentfield);
237 }
238 },
239
Anand Doshifc777182013-05-27 19:29:07 +0530240 warehouse: function(doc, cdt, cdn) {
Anand Doshi7dd50342013-07-22 16:36:11 +0530241 var item = wn.model.get_doc(cdt, cdn);
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530242 if(item.item_code && item.warehouse) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530243 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530244 method: "erpnext.selling.utils.get_available_qty",
Anand Doshifc777182013-05-27 19:29:07 +0530245 child: item,
246 args: {
247 item_code: item.item_code,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530248 warehouse: item.warehouse,
Anand Doshifc777182013-05-27 19:29:07 +0530249 },
250 });
251 }
252 },
253
Anand Doshif3096132013-05-21 19:35:06 +0530254 toggle_rounded_total: function() {
255 var me = this;
256 if(cint(wn.defaults.get_global_default("disable_rounded_total"))) {
257 $.each(["rounded_total", "rounded_total_export"], function(i, fieldname) {
258 me.frm.set_df_property(fieldname, "print_hide", 1);
259 me.frm.toggle_display(fieldname, false);
260 });
261 }
262 },
263
Anand Doshi33fe8672013-08-02 12:39:10 +0530264 toggle_editable_price_list_rate: function() {
265 var df = wn.meta.get_docfield(this.tname, "ref_rate", this.frm.doc.name);
266 var editable_price_list_rate = cint(wn.defaults.get_default("editable_price_list_rate"));
267
268 if(df && editable_price_list_rate) {
269 df.read_only = 0;
270 }
271 },
272
Anand Doshif3096132013-05-21 19:35:06 +0530273 calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530274 this._super();
Anand Doshi923d41d2013-05-28 17:23:36 +0530275 this.calculate_total_advance("Sales Invoice", "advance_adjustment_details");
Anand Doshif3096132013-05-21 19:35:06 +0530276 this.calculate_commission();
277 this.calculate_contribution();
Anand Doshi923d41d2013-05-28 17:23:36 +0530278
279 // TODO check for custom_recalc in custom scripts of server
Anand Doshif3096132013-05-21 19:35:06 +0530280
Anand Doshi2168e392013-05-23 19:25:08 +0530281 this.frm.refresh_fields();
Anand Doshif3096132013-05-21 19:35:06 +0530282 },
283
284 calculate_item_values: function() {
285 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530286
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530287 if (!this.discount_amount_applied) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530288 $.each(this.frm.item_doclist, function(i, item) {
289 wn.model.round_floats_in(item);
290 item.export_amount = flt(item.export_rate * item.qty, precision("export_amount", item));
291
292 me._set_in_company_currency(item, "ref_rate", "base_ref_rate");
293 me._set_in_company_currency(item, "export_rate", "basic_rate");
294 me._set_in_company_currency(item, "export_amount", "amount");
295 });
296 }
Anand Doshif3096132013-05-21 19:35:06 +0530297 },
298
Anand Doshif3096132013-05-21 19:35:06 +0530299 determine_exclusive_rate: function() {
300 var me = this;
301 $.each(me.frm.item_doclist, function(n, item) {
302 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
303 var cumulated_tax_fraction = 0.0;
304
305 $.each(me.frm.tax_doclist, function(i, tax) {
306 tax.tax_fraction_for_current_item = me.get_current_tax_fraction(tax, item_tax_map);
307
308 if(i==0) {
Anand Doshi2168e392013-05-23 19:25:08 +0530309 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530310 } else {
Anand Doshi2168e392013-05-23 19:25:08 +0530311 tax.grand_total_fraction_for_current_item =
312 me.frm.tax_doclist[i-1].grand_total_fraction_for_current_item +
Anand Doshif3096132013-05-21 19:35:06 +0530313 tax.tax_fraction_for_current_item;
314 }
315
316 cumulated_tax_fraction += tax.tax_fraction_for_current_item;
317 });
318
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530319 if(cumulated_tax_fraction && !me.discount_amount_applied) {
Anand Doshifbe1e162013-08-06 19:38:19 +0530320 item.amount = flt(
321 (item.export_amount * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction),
322 precision("amount", item));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530323
Anand Doshifbe1e162013-08-06 19:38:19 +0530324 item.basic_rate = flt(item.amount / item.qty, precision("basic_rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530325
326 if(item.adj_rate == 100) {
327 item.base_ref_rate = item.basic_rate;
328 item.basic_rate = 0.0;
329 } else {
330 item.base_ref_rate = flt(item.basic_rate / (1 - item.adj_rate / 100.0),
331 precision("base_ref_rate", item));
332 }
333 }
334 });
335 },
336
337 get_current_tax_fraction: function(tax, item_tax_map) {
338 // Get tax fraction for calculating tax exclusive amount
339 // from tax inclusive amount
340 var current_tax_fraction = 0.0;
341
342 if(cint(tax.included_in_print_rate)) {
Anand Doshi2168e392013-05-23 19:25:08 +0530343 var tax_rate = this._get_tax_rate(tax, item_tax_map);
Anand Doshif3096132013-05-21 19:35:06 +0530344
345 if(tax.charge_type == "On Net Total") {
346 current_tax_fraction = (tax_rate / 100.0);
347
348 } else if(tax.charge_type == "On Previous Row Amount") {
349 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530350 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530351
352 } else if(tax.charge_type == "On Previous Row Total") {
353 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530354 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530355 }
356 }
357
358 return current_tax_fraction;
359 },
360
361 calculate_net_total: function() {
362 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530363 this.frm.doc.net_total = this.frm.doc.net_total_export = 0.0;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530364
Anand Doshif3096132013-05-21 19:35:06 +0530365 $.each(this.frm.item_doclist, function(i, item) {
366 me.frm.doc.net_total += item.amount;
367 me.frm.doc.net_total_export += item.export_amount;
368 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530369
Anand Doshif3096132013-05-21 19:35:06 +0530370 wn.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]);
371 },
372
Anand Doshif3096132013-05-21 19:35:06 +0530373 calculate_totals: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530374 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530375 var tax_count = this.frm.tax_doclist.length;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530376
Anand Doshif3096132013-05-21 19:35:06 +0530377 this.frm.doc.grand_total = flt(
378 tax_count ? this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total,
379 precision("grand_total"));
380 this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate,
381 precision("grand_total_export"));
382
383 this.frm.doc.other_charges_total = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
384 precision("other_charges_total"));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530385 this.frm.doc.other_charges_total_export = flt(this.frm.doc.grand_total_export -
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530386 this.frm.doc.net_total_export + flt(this.frm.doc.discount_amount),
Anand Doshif3096132013-05-21 19:35:06 +0530387 precision("other_charges_total_export"));
388
389 this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
390 this.frm.doc.rounded_total_export = Math.round(this.frm.doc.grand_total_export);
391 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530392
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530393 apply_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530394 var me = this;
395 var distributed_amount = 0.0;
396
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530397 if (this.frm.doc.discount_amount) {
398 var grand_total_for_discount_amount = this.get_grand_total_for_discount_amount();
399 // calculate item amount after Discount Amount
400 if (grand_total_for_discount_amount) {
401 $.each(this.frm.item_doclist, function(i, item) {
402 distributed_amount = flt(me.frm.doc.discount_amount) * item.amount / grand_total_for_discount_amount;
403 item.amount = flt(item.amount - distributed_amount, precision("amount", item));
404 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530405
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530406 this.discount_amount_applied = true;
407 this._calculate_taxes_and_totals();
408 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530409 }
410 },
411
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530412 get_grand_total_for_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530413 var me = this;
414 var total_actual_tax = 0.0;
415 var actual_taxes_dict = {};
416
417 $.each(this.frm.tax_doclist, function(i, tax) {
418 if (tax.charge_type == "Actual")
419 actual_taxes_dict[tax.idx] = tax.tax_amount;
420 else if (actual_taxes_dict[tax.row_id] !== null) {
421 actual_tax_amount = flt(actual_taxes_dict[tax.row_id]) * flt(tax.rate) / 100;
422 actual_taxes_dict[tax.idx] = actual_tax_amount;
423 }
424 });
425
426 $.each(actual_taxes_dict, function(key, value) {
427 if (value)
428 total_actual_tax += value;
429 });
430
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530431 grand_total_for_discount_amount = flt(this.frm.doc.grand_total - total_actual_tax,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530432 precision("grand_total"));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530433 return grand_total_for_discount_amount;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530434 },
Anand Doshif3096132013-05-21 19:35:06 +0530435
Anand Doshifc777182013-05-27 19:29:07 +0530436 calculate_outstanding_amount: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530437 // NOTE:
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530438 // paid_amount and write_off_amount is only for POS Invoice
Anand Doshi923d41d2013-05-28 17:23:36 +0530439 // total_advance is only for non POS Invoice
Anand Doshi29ea5d02013-07-05 17:23:14 +0530440 if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.docstatus==0) {
Anand Doshifc777182013-05-27 19:29:07 +0530441 wn.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount",
442 "paid_amount"]);
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530443 var total_amount_to_pay = this.frm.doc.grand_total - this.frm.doc.write_off_amount - this.frm.doc.total_advance;
444 this.frm.doc.paid_amount = this.frm.doc.is_pos? flt(total_amount_to_pay): 0.0;
445
446 this.frm.doc.outstanding_amount = flt(total_amount_to_pay - this.frm.doc.paid_amount,
447 precision("outstanding_amount"));
Anand Doshifc777182013-05-27 19:29:07 +0530448 }
449 },
450
Anand Doshif3096132013-05-21 19:35:06 +0530451 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530452 if(this.frm.fields_dict.commission_rate) {
453 if(this.frm.doc.commission_rate > 100) {
454 var msg = wn._(wn.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
455 " " + wn._("cannot be greater than 100");
456 msgprint(msg);
457 throw msg;
458 }
Anand Doshif3096132013-05-21 19:35:06 +0530459
Anand Doshi923d41d2013-05-28 17:23:36 +0530460 this.frm.doc.total_commission = flt(this.frm.doc.net_total * this.frm.doc.commission_rate / 100.0,
461 precision("total_commission"));
462 }
Anand Doshif3096132013-05-21 19:35:06 +0530463 },
464
465 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530466 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530467 $.each(wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
468 {parentfield: "sales_team"}), function(i, sales_person) {
469 wn.model.round_floats_in(sales_person);
470 if(sales_person.allocated_percentage) {
471 sales_person.allocated_amount = flt(
472 me.frm.doc.net_total * sales_person.allocated_percentage / 100.0,
473 precision("allocated_amount", sales_person));
474 }
475 });
476 },
477
478 _cleanup: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530479 this._super();
480 this.frm.doc.in_words = this.frm.doc.in_words_export = "";
Anand Doshif3096132013-05-21 19:35:06 +0530481 },
Anand Doshi2168e392013-05-23 19:25:08 +0530482
483 show_item_wise_taxes: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530484 if(this.frm.fields_dict.other_charges_calculation) {
485 $(this.get_item_wise_taxes_html())
486 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
487 }
Anand Doshi2168e392013-05-23 19:25:08 +0530488 },
489
Anand Doshi77cd10b2013-06-27 12:17:30 +0530490 charge: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530491 var me = this;
492 if(this.frm.doc.charge) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530493 return this.frm.call({
Anand Doshi2168e392013-05-23 19:25:08 +0530494 doc: this.frm.doc,
495 method: "get_other_charges",
496 callback: function(r) {
497 if(!r.exc) {
498 me.calculate_taxes_and_totals();
499 }
500 }
501 });
502 }
503 },
504
Anand Doshicefccb92013-07-15 18:28:14 +0530505 shipping_rule: function() {
506 var me = this;
507 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530508 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530509 doc: this.frm.doc,
510 method: "apply_shipping_rule",
511 callback: function(r) {
512 if(!r.exc) {
513 me.calculate_taxes_and_totals();
514 }
515 }
516 })
517 }
518 },
519
Anand Doshi2168e392013-05-23 19:25:08 +0530520 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530521 this._super();
Anand Doshi9d049242013-06-05 20:46:56 +0530522 set_sales_bom_help(this.frm.doc);
Anand Doshi2168e392013-05-23 19:25:08 +0530523 },
524
525 change_form_labels: function(company_currency) {
526 var me = this;
527 var field_label_map = {};
528
529 var setup_field_label_map = function(fields_list, currency) {
530 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530531 var docfield = wn.meta.docfield_map[me.frm.doc.doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530532 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530533 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530534 field_label_map[fname] = label.trim() + " (" + currency + ")";
535 }
536 });
537 };
Anand Doshi2168e392013-05-23 19:25:08 +0530538 setup_field_label_map(["net_total", "other_charges_total", "grand_total",
539 "rounded_total", "in_words",
540 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"],
541 company_currency);
542
543 setup_field_label_map(["net_total_export", "other_charges_total_export", "grand_total_export",
544 "rounded_total_export", "in_words_export"], this.frm.doc.currency);
545
Rushabh Mehtaef584552013-11-02 14:47:11 +0530546 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
547 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530548
549 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Rushabh Mehtaef584552013-11-02 14:47:11 +0530550 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
551 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530552 }
553
554 // toggle fields
555 this.frm.toggle_display(["conversion_rate", "net_total", "other_charges_total",
556 "grand_total", "rounded_total", "in_words"],
557 this.frm.doc.currency != company_currency);
558
Rushabh Mehtaa063b4d2013-11-06 11:29:47 +0530559 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
Anand Doshi2168e392013-05-23 19:25:08 +0530560 this.frm.doc.price_list_currency != company_currency);
561
562 // set labels
563 $.each(field_label_map, function(fname, label) {
564 me.frm.fields_dict[fname].set_label(label);
565 });
566 },
567
568 change_grid_labels: function(company_currency) {
569 var me = this;
570 var field_label_map = {};
571
572 var setup_field_label_map = function(fields_list, currency, parentfield) {
573 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
574 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530575 var docfield = wn.meta.docfield_map[grid_doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530576 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530577 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530578 field_label_map[grid_doctype + "-" + fname] =
579 label.trim() + " (" + currency + ")";
580 }
581 });
582 }
583
584 setup_field_label_map(["basic_rate", "base_ref_rate", "amount"],
585 company_currency, this.fname);
586
587 setup_field_label_map(["export_rate", "ref_rate", "export_amount"],
588 this.frm.doc.currency, this.fname);
589
590 setup_field_label_map(["tax_amount", "total"], company_currency, "other_charges");
591
592 if(this.frm.fields_dict["advance_allocation_details"]) {
593 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency,
594 "advance_allocation_details");
595 }
596
597 // toggle columns
598 var item_grid = this.frm.fields_dict[this.fname].grid;
Anand Doshiedfba962013-08-05 11:56:54 +0530599 var show = (this.frm.doc.currency != company_currency) ||
600 (wn.model.get_doclist(cur_frm.doctype, cur_frm.docname,
601 {parentfield: "other_charges", included_in_print_rate: 1}).length);
602
Anand Doshi2168e392013-05-23 19:25:08 +0530603 $.each(["basic_rate", "base_ref_rate", "amount"], function(i, fname) {
604 if(wn.meta.get_docfield(item_grid.doctype, fname))
605 item_grid.set_column_disp(fname, show);
606 });
607
608 // set labels
609 var $wrapper = $(this.frm.wrapper);
610 $.each(field_label_map, function(fname, label) {
Anand Doshi5013dcb2013-08-05 12:16:04 +0530611 fname = fname.split("-");
612 var df = wn.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
613 if(df) df.label = label;
Anand Doshi2168e392013-05-23 19:25:08 +0530614 });
615 },
616
Anand Doshi8f9f8a42013-06-28 19:18:33 +0530617 shipping_address_name: function () {
618 var me = this;
619 if(this.frm.doc.shipping_address_name) {
620 wn.model.with_doc("Address", this.frm.doc.shipping_address_name, function(name) {
621 var address = wn.model.get_doc("Address", name);
622
623 var out = $.map(["address_line1", "address_line2", "city"],
624 function(f) { return address[f]; });
625
626 var state_pincode = $.map(["state", "pincode"], function(f) { return address[f]; }).join(" ");
627 if(state_pincode) out.push(state_pincode);
628
629 if(address["country"]) out.push(address["country"]);
630
631 out.concat($.map([["Phone:", address["phone"]], ["Fax:", address["fax"]]],
632 function(val) { return val[1] ? val.join(" ") : null; }));
633
634 me.frm.set_value("shipping_address", out.join("\n"));
635 });
636 }
637 }
Anand Doshi1dde46a2013-05-15 21:15:57 +0530638});
639
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530640// Help for Sales BOM items
641var set_sales_bom_help = function(doc) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530642 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530643 if (getchildren('Packed Item', doc.name, 'packing_details').length) {
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530644 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
645
646 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Bárbara Perretti4098c262013-09-27 17:05:17 -0300647 help_msg = "<div class='alert alert-warning'>" +
648 wn._("For 'Sales BOM' items, warehouse, serial no and batch no \
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530649 will be considered from the 'Packing List' table. \
650 If warehouse and batch no are same for all packing items for any 'Sales BOM' item, \
Bárbara Perretti4098c262013-09-27 17:05:17 -0300651 those values can be entered in the main item table, values will be copied to 'Packing List' table.")+
652 "</div>";
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530653 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = help_msg;
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530654 }
655 } else {
656 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
657 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530658 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = '';
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530659 }
660 }
661 refresh_field('sales_bom_help');
662}