blob: d6c8fdd0e40df1d13659e895821561a075ec358c [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 Mehta707932d2012-09-25 10:42:00 +05308// cur_frm.cscript.other_fname - wn.require('app/accounts/doctype/sales_taxes_and_charges_master/sales_taxes_and_charges_master.js'); 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");
Anand Doshi3543f302013-05-24 19:25:01 +053012wn.require("app/js/transaction.js");
Rushabh Mehtaf56d73c2013-10-10 16:35:09 +053013wn.require("app/js/controllers/accounts.js");
Anand Doshi1dde46a2013-05-15 21:15:57 +053014
Anand Doshi3543f302013-05-24 19:25:01 +053015erpnext.selling.SellingController = erpnext.TransactionController.extend({
Anand Doshi9b496142013-07-11 19:13:58 +053016 onload: function() {
17 this._super();
18 this.toggle_rounded_total();
19 this.setup_queries();
Anand Doshi33fe8672013-08-02 12:39:10 +053020 this.toggle_editable_price_list_rate();
Anand Doshi9b496142013-07-11 19:13:58 +053021 },
22
23 setup_queries: function() {
Anand Doshi99100a42013-07-04 17:13:53 +053024 var me = this;
25
Anand Doshif3096132013-05-21 19:35:06 +053026 this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate");
Anand Doshi8f9f8a42013-06-28 19:18:33 +053027
Anand Doshid5d39ac2013-07-29 13:28:37 +053028 $.each([["customer_address", "customer_filter"],
29 ["shipping_address_name", "customer_filter"],
30 ["contact_person", "customer_filter"],
31 ["customer", "customer"],
32 ["lead", "lead"]],
33 function(i, opts) {
34 if(me.frm.fields_dict[opts[0]])
35 me.frm.set_query(opts[0], erpnext.queries[opts[1]]);
36 });
Anand Doshi99100a42013-07-04 17:13:53 +053037
38 if(this.frm.fields_dict.charge) {
39 this.frm.set_query("charge", function() {
Saurabhf52dc072013-07-10 13:07:49 +053040 return {
41 filters: [
42 ['Sales Taxes and Charges Master', 'company', '=', me.frm.doc.company],
Saurabhf52dc072013-07-10 13:07:49 +053043 ['Sales Taxes and Charges Master', 'docstatus', '!=', 2]
44 ]
45 }
Anand Doshi99100a42013-07-04 17:13:53 +053046 });
47 }
Anand Doshi99100a42013-07-04 17:13:53 +053048
Rushabh Mehta4a404e92013-08-09 18:11:35 +053049 if(this.frm.fields_dict.selling_price_list) {
50 this.frm.set_query("selling_price_list", function() {
Anand Doshi720a01a2013-07-26 11:32:02 +053051 return { filters: { buying_or_selling: "Selling" } };
52 });
Anand Doshi720a01a2013-07-26 11:32:02 +053053 }
54
Anand Doshi99100a42013-07-04 17:13:53 +053055 if(!this.fname) {
56 return;
57 }
58
59 if(this.frm.fields_dict[this.fname].grid.get_field('item_code')) {
60 this.frm.set_query("item_code", this.fname, function() {
Anand Doshi9b496142013-07-11 19:13:58 +053061 return {
62 query: "controllers.queries.item_query",
63 filters: (me.frm.doc.order_type === "Maintenance" ?
64 {'is_service_item': 'Yes'}:
65 {'is_sales_item': 'Yes' })
66 }
Anand Doshi99100a42013-07-04 17:13:53 +053067 });
68 }
69
70 if(this.frm.fields_dict[this.fname].grid.get_field('batch_no')) {
71 this.frm.set_query("batch_no", this.fname, function(doc, cdt, cdn) {
72 var item = wn.model.get_doc(cdt, cdn);
73 if(!item.item_code) {
Bárbara Perretti4098c262013-09-27 17:05:17 -030074 wn.throw(wn._("Please enter Item Code to get batch no"));
Anand Doshi99100a42013-07-04 17:13:53 +053075 } else {
Nabin Haitd1fd1e22013-10-18 12:29:11 +053076 filters = {
77 'item_code': item.item_code,
78 'posting_date': me.frm.doc.posting_date,
79 }
80 if(item.warehouse) filters["warehouse"] = item.warehouse
81
82 return {
83 query : "controllers.queries.get_batch_no",
84 filters: filters
Anand Doshi99100a42013-07-04 17:13:53 +053085 }
86 }
87 });
88 }
Anand Doshied698922013-07-23 15:16:50 +053089
90 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 +053091 this.frm.set_query("sales_person", "sales_team", erpnext.queries.not_a_group_filter);
Anand Doshied698922013-07-23 15:16:50 +053092 }
Anand Doshi1dde46a2013-05-15 21:15:57 +053093 },
94
Anand Doshic4a54fe2013-08-01 18:19:51 +053095 refresh: function() {
96 this._super();
Rushabh Mehta8aded132013-07-04 12:50:52 +053097 this.frm.toggle_display("customer_name",
Akhilesh Darjee5ce1b8b2013-12-09 16:29:04 +053098 (this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer));
Anand Doshic4a54fe2013-08-01 18:19:51 +053099 if(this.frm.fields_dict.packing_details) {
100 var packing_list_exists = this.frm.get_doclist({parentfield: "packing_details"}).length;
101 this.frm.toggle_display("packing_list", packing_list_exists ? true : false);
102 }
Rushabh Mehta8aded132013-07-04 12:50:52 +0530103 },
104
Anand Doshi3543f302013-05-24 19:25:01 +0530105 customer: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530106 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530107 if(this.frm.doc.customer || this.frm.doc.debit_to) {
108 if(!this.frm.doc.company) {
109 this.frm.set_value("customer", null);
110 msgprint(wn._("Please specify Company"));
111 } else {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530112 var selling_price_list = this.frm.doc.selling_price_list;
Anand Doshi1fac2a92013-07-29 19:30:39 +0530113 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530114 doc: this.frm.doc,
115 method: "set_customer_defaults",
116 freeze: true,
117 callback: function(r) {
118 if(!r.exc) {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530119 (me.frm.doc.selling_price_list !== selling_price_list) ?
120 me.selling_price_list() :
Anand Doshi923d41d2013-05-28 17:23:36 +0530121 me.price_list_currency();
Anand Doshi3543f302013-05-24 19:25:01 +0530122 }
123 }
124 });
125 }
126 }
Anand Doshi3543f302013-05-24 19:25:01 +0530127 },
128
Nabin Haita279d782013-07-15 13:04:33 +0530129 customer_address: function() {
130 var me = this;
131 if(this.frm.doc.customer) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530132 return this.frm.call({
Nabin Haita279d782013-07-15 13:04:33 +0530133 doc: this.frm.doc,
134 args: {
135 customer: this.frm.doc.customer,
136 address: this.frm.doc.customer_address,
137 contact: this.frm.doc.contact_person
138 },
Nabin Haitdeff5782013-07-24 11:30:35 +0530139 method: "set_customer_address",
Nabin Haita279d782013-07-15 13:04:33 +0530140 freeze: true,
Nabin Haita279d782013-07-15 13:04:33 +0530141 });
142 }
143 },
144
145 contact_person: function() {
146 this.customer_address();
147 },
148
Anand Doshif3096132013-05-21 19:35:06 +0530149 barcode: function(doc, cdt, cdn) {
150 this.item_code(doc, cdt, cdn);
Anand Doshi1dde46a2013-05-15 21:15:57 +0530151 },
152
153 item_code: function(doc, cdt, cdn) {
154 var me = this;
155 var item = wn.model.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530156 if(item.item_code || item.barcode || item.serial_no) {
Anand Doshi3543f302013-05-24 19:25:01 +0530157 if(!this.validate_company_and_party("customer")) {
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530158 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
Anand Doshi1dde46a2013-05-15 21:15:57 +0530159 } else {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530160 return this.frm.call({
Anand Doshi1dde46a2013-05-15 21:15:57 +0530161 method: "selling.utils.get_item_details",
162 child: item,
163 args: {
164 args: {
165 item_code: item.item_code,
Anand Doshif3096132013-05-21 19:35:06 +0530166 barcode: item.barcode,
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530167 serial_no: item.serial_no,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530168 warehouse: item.warehouse,
169 doctype: me.frm.doc.doctype,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530170 parentfield: item.parentfield,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530171 customer: me.frm.doc.customer,
172 currency: me.frm.doc.currency,
173 conversion_rate: me.frm.doc.conversion_rate,
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530174 selling_price_list: me.frm.doc.selling_price_list,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530175 price_list_currency: me.frm.doc.price_list_currency,
176 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
177 company: me.frm.doc.company,
Anand Doshif3096132013-05-21 19:35:06 +0530178 order_type: me.frm.doc.order_type,
179 is_pos: cint(me.frm.doc.is_pos),
Anand Doshi1dde46a2013-05-15 21:15:57 +0530180 }
181 },
182 callback: function(r) {
Anand Doshif3096132013-05-21 19:35:06 +0530183 if(!r.exc) {
Anand Doshi80166e22013-07-17 14:49:13 +0530184 me.frm.script_manager.trigger("ref_rate", cdt, cdn);
Anand Doshif3096132013-05-21 19:35:06 +0530185 }
Anand Doshi1dde46a2013-05-15 21:15:57 +0530186 }
187 });
188 }
189 }
190 },
191
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530192 selling_price_list: function() {
193 this.get_price_list_currency("Selling");
Anand Doshif3096132013-05-21 19:35:06 +0530194 },
195
196 ref_rate: function(doc, cdt, cdn) {
197 var item = wn.model.get_doc(cdt, cdn);
198 wn.model.round_floats_in(item, ["ref_rate", "adj_rate"]);
199
200 item.export_rate = flt(item.ref_rate * (1 - item.adj_rate / 100.0),
201 precision("export_rate", item));
202
203 this.calculate_taxes_and_totals();
204 },
205
Anand Doshif3096132013-05-21 19:35:06 +0530206 adj_rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530207 var item = wn.model.get_doc(cdt, cdn);
208 if(!item.ref_rate) {
209 item.adj_rate = 0.0;
210 } else {
211 this.ref_rate(doc, cdt, cdn);
212 }
Anand Doshif3096132013-05-21 19:35:06 +0530213 },
214
215 export_rate: function(doc, cdt, cdn) {
216 var item = wn.model.get_doc(cdt, cdn);
217 wn.model.round_floats_in(item, ["export_rate", "ref_rate"]);
218
219 if(item.ref_rate) {
220 item.adj_rate = flt((1 - item.export_rate / item.ref_rate) * 100.0,
221 precision("adj_rate", item));
222 } else {
223 item.adj_rate = 0.0;
224 }
225
226 this.calculate_taxes_and_totals();
227 },
228
Anand Doshif3096132013-05-21 19:35:06 +0530229 commission_rate: function() {
230 this.calculate_commission();
231 refresh_field("total_commission");
232 },
233
234 total_commission: function() {
235 if(this.frm.doc.net_total) {
236 wn.model.round_floats_in(this.frm.doc, ["net_total", "total_commission"]);
237
238 if(this.frm.doc.net_total < this.frm.doc.total_commission) {
239 var msg = (wn._("[Error]") + " " +
240 wn._(wn.meta.get_label(this.frm.doc.doctype, "total_commission",
241 this.frm.doc.name)) + " > " +
242 wn._(wn.meta.get_label(this.frm.doc.doctype, "net_total", this.frm.doc.name)));
243 msgprint(msg);
244 throw msg;
245 }
246
247 this.frm.set_value("commission_rate",
248 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.net_total));
249 }
250 },
251
252 allocated_percentage: function(doc, cdt, cdn) {
253 var sales_person = wn.model.get_doc(cdt, cdn);
254
255 if(sales_person.allocated_percentage) {
256 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
257 precision("allocated_percentage", sales_person));
258 sales_person.allocated_amount = flt(this.frm.doc.net_total *
259 sales_person.allocated_percentage / 100.0,
260 precision("allocated_amount", sales_person));
261
262 refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name,
263 sales_person.parentfield);
264 }
265 },
266
Anand Doshifc777182013-05-27 19:29:07 +0530267 warehouse: function(doc, cdt, cdn) {
Anand Doshi7dd50342013-07-22 16:36:11 +0530268 var item = wn.model.get_doc(cdt, cdn);
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530269 if(item.item_code && item.warehouse) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530270 return this.frm.call({
Anand Doshifc777182013-05-27 19:29:07 +0530271 method: "selling.utils.get_available_qty",
272 child: item,
273 args: {
274 item_code: item.item_code,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530275 warehouse: item.warehouse,
Anand Doshifc777182013-05-27 19:29:07 +0530276 },
277 });
278 }
279 },
280
Anand Doshif3096132013-05-21 19:35:06 +0530281 toggle_rounded_total: function() {
282 var me = this;
283 if(cint(wn.defaults.get_global_default("disable_rounded_total"))) {
284 $.each(["rounded_total", "rounded_total_export"], function(i, fieldname) {
285 me.frm.set_df_property(fieldname, "print_hide", 1);
286 me.frm.toggle_display(fieldname, false);
287 });
288 }
289 },
290
Anand Doshi33fe8672013-08-02 12:39:10 +0530291 toggle_editable_price_list_rate: function() {
292 var df = wn.meta.get_docfield(this.tname, "ref_rate", this.frm.doc.name);
293 var editable_price_list_rate = cint(wn.defaults.get_default("editable_price_list_rate"));
294
295 if(df && editable_price_list_rate) {
296 df.read_only = 0;
297 }
298 },
299
Anand Doshif3096132013-05-21 19:35:06 +0530300 calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530301 this._super();
Anand Doshi923d41d2013-05-28 17:23:36 +0530302 this.calculate_total_advance("Sales Invoice", "advance_adjustment_details");
Anand Doshif3096132013-05-21 19:35:06 +0530303 this.calculate_commission();
304 this.calculate_contribution();
Anand Doshi923d41d2013-05-28 17:23:36 +0530305
306 // TODO check for custom_recalc in custom scripts of server
Anand Doshif3096132013-05-21 19:35:06 +0530307
Anand Doshi2168e392013-05-23 19:25:08 +0530308 this.frm.refresh_fields();
Anand Doshif3096132013-05-21 19:35:06 +0530309 },
310
311 calculate_item_values: function() {
312 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530313 $.each(this.frm.item_doclist, function(i, item) {
314 wn.model.round_floats_in(item);
315 item.export_amount = flt(item.export_rate * item.qty, precision("export_amount", item));
316
Anand Doshi3543f302013-05-24 19:25:01 +0530317 me._set_in_company_currency(item, "ref_rate", "base_ref_rate");
318 me._set_in_company_currency(item, "export_rate", "basic_rate");
319 me._set_in_company_currency(item, "export_amount", "amount");
Anand Doshif3096132013-05-21 19:35:06 +0530320 });
321
322 },
323
Anand Doshif3096132013-05-21 19:35:06 +0530324 determine_exclusive_rate: function() {
325 var me = this;
326 $.each(me.frm.item_doclist, function(n, item) {
327 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
328 var cumulated_tax_fraction = 0.0;
329
330 $.each(me.frm.tax_doclist, function(i, tax) {
331 tax.tax_fraction_for_current_item = me.get_current_tax_fraction(tax, item_tax_map);
332
333 if(i==0) {
Anand Doshi2168e392013-05-23 19:25:08 +0530334 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530335 } else {
Anand Doshi2168e392013-05-23 19:25:08 +0530336 tax.grand_total_fraction_for_current_item =
337 me.frm.tax_doclist[i-1].grand_total_fraction_for_current_item +
Anand Doshif3096132013-05-21 19:35:06 +0530338 tax.tax_fraction_for_current_item;
339 }
340
341 cumulated_tax_fraction += tax.tax_fraction_for_current_item;
342 });
343
344 if(cumulated_tax_fraction) {
Anand Doshifbe1e162013-08-06 19:38:19 +0530345 item.amount = flt(
346 (item.export_amount * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction),
347 precision("amount", item));
348
349 item.basic_rate = flt(item.amount / item.qty, precision("basic_rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530350
351 if(item.adj_rate == 100) {
352 item.base_ref_rate = item.basic_rate;
353 item.basic_rate = 0.0;
354 } else {
355 item.base_ref_rate = flt(item.basic_rate / (1 - item.adj_rate / 100.0),
356 precision("base_ref_rate", item));
357 }
358 }
359 });
360 },
361
362 get_current_tax_fraction: function(tax, item_tax_map) {
363 // Get tax fraction for calculating tax exclusive amount
364 // from tax inclusive amount
365 var current_tax_fraction = 0.0;
366
367 if(cint(tax.included_in_print_rate)) {
Anand Doshi2168e392013-05-23 19:25:08 +0530368 var tax_rate = this._get_tax_rate(tax, item_tax_map);
Anand Doshif3096132013-05-21 19:35:06 +0530369
370 if(tax.charge_type == "On Net Total") {
371 current_tax_fraction = (tax_rate / 100.0);
372
373 } else if(tax.charge_type == "On Previous Row Amount") {
374 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530375 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530376
377 } else if(tax.charge_type == "On Previous Row Total") {
378 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530379 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530380 }
381 }
382
383 return current_tax_fraction;
384 },
385
386 calculate_net_total: function() {
387 var me = this;
388
389 this.frm.doc.net_total = this.frm.doc.net_total_export = 0.0;
390 $.each(this.frm.item_doclist, function(i, item) {
391 me.frm.doc.net_total += item.amount;
392 me.frm.doc.net_total_export += item.export_amount;
393 });
394
395 wn.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]);
396 },
397
Anand Doshif3096132013-05-21 19:35:06 +0530398 calculate_totals: function() {
399 var tax_count = this.frm.tax_doclist.length;
400 this.frm.doc.grand_total = flt(
401 tax_count ? this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total,
402 precision("grand_total"));
403 this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate,
404 precision("grand_total_export"));
405
406 this.frm.doc.other_charges_total = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
407 precision("other_charges_total"));
408 this.frm.doc.other_charges_total_export = flt(
409 this.frm.doc.grand_total_export - this.frm.doc.net_total_export,
410 precision("other_charges_total_export"));
411
412 this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
413 this.frm.doc.rounded_total_export = Math.round(this.frm.doc.grand_total_export);
414 },
415
Anand Doshifc777182013-05-27 19:29:07 +0530416 calculate_outstanding_amount: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530417 // NOTE:
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530418 // paid_amount and write_off_amount is only for POS Invoice
Anand Doshi923d41d2013-05-28 17:23:36 +0530419 // total_advance is only for non POS Invoice
Anand Doshi29ea5d02013-07-05 17:23:14 +0530420 if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.docstatus==0) {
Anand Doshifc777182013-05-27 19:29:07 +0530421 wn.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount",
422 "paid_amount"]);
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530423 var total_amount_to_pay = this.frm.doc.grand_total - this.frm.doc.write_off_amount - this.frm.doc.total_advance;
424 this.frm.doc.paid_amount = this.frm.doc.is_pos? flt(total_amount_to_pay): 0.0;
425
426 this.frm.doc.outstanding_amount = flt(total_amount_to_pay - this.frm.doc.paid_amount,
427 precision("outstanding_amount"));
Anand Doshifc777182013-05-27 19:29:07 +0530428 }
429 },
430
Anand Doshif3096132013-05-21 19:35:06 +0530431 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530432 if(this.frm.fields_dict.commission_rate) {
433 if(this.frm.doc.commission_rate > 100) {
434 var msg = wn._(wn.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
435 " " + wn._("cannot be greater than 100");
436 msgprint(msg);
437 throw msg;
438 }
Anand Doshif3096132013-05-21 19:35:06 +0530439
Anand Doshi923d41d2013-05-28 17:23:36 +0530440 this.frm.doc.total_commission = flt(this.frm.doc.net_total * this.frm.doc.commission_rate / 100.0,
441 precision("total_commission"));
442 }
Anand Doshif3096132013-05-21 19:35:06 +0530443 },
444
445 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530446 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530447 $.each(wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
448 {parentfield: "sales_team"}), function(i, sales_person) {
449 wn.model.round_floats_in(sales_person);
450 if(sales_person.allocated_percentage) {
451 sales_person.allocated_amount = flt(
452 me.frm.doc.net_total * sales_person.allocated_percentage / 100.0,
453 precision("allocated_amount", sales_person));
454 }
455 });
456 },
457
458 _cleanup: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530459 this._super();
460 this.frm.doc.in_words = this.frm.doc.in_words_export = "";
Anand Doshif3096132013-05-21 19:35:06 +0530461 },
Anand Doshi2168e392013-05-23 19:25:08 +0530462
463 show_item_wise_taxes: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530464 if(this.frm.fields_dict.other_charges_calculation) {
465 $(this.get_item_wise_taxes_html())
466 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
467 }
Anand Doshi2168e392013-05-23 19:25:08 +0530468 },
469
Anand Doshi77cd10b2013-06-27 12:17:30 +0530470 charge: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530471 var me = this;
472 if(this.frm.doc.charge) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530473 return this.frm.call({
Anand Doshi2168e392013-05-23 19:25:08 +0530474 doc: this.frm.doc,
475 method: "get_other_charges",
476 callback: function(r) {
477 if(!r.exc) {
478 me.calculate_taxes_and_totals();
479 }
480 }
481 });
482 }
483 },
484
Anand Doshicefccb92013-07-15 18:28:14 +0530485 shipping_rule: function() {
486 var me = this;
487 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530488 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530489 doc: this.frm.doc,
490 method: "apply_shipping_rule",
491 callback: function(r) {
492 if(!r.exc) {
493 me.calculate_taxes_and_totals();
494 }
495 }
496 })
497 }
498 },
499
Anand Doshi2168e392013-05-23 19:25:08 +0530500 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530501 this._super();
Anand Doshi9d049242013-06-05 20:46:56 +0530502 set_sales_bom_help(this.frm.doc);
Anand Doshi2168e392013-05-23 19:25:08 +0530503 },
504
505 change_form_labels: function(company_currency) {
506 var me = this;
507 var field_label_map = {};
508
509 var setup_field_label_map = function(fields_list, currency) {
510 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530511 var docfield = wn.meta.docfield_map[me.frm.doc.doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530512 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530513 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530514 field_label_map[fname] = label.trim() + " (" + currency + ")";
515 }
516 });
517 };
518
519 setup_field_label_map(["net_total", "other_charges_total", "grand_total",
520 "rounded_total", "in_words",
521 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"],
522 company_currency);
523
524 setup_field_label_map(["net_total_export", "other_charges_total_export", "grand_total_export",
525 "rounded_total_export", "in_words_export"], this.frm.doc.currency);
526
Rushabh Mehtaef584552013-11-02 14:47:11 +0530527 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
528 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530529
530 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Rushabh Mehtaef584552013-11-02 14:47:11 +0530531 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
532 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530533 }
534
535 // toggle fields
536 this.frm.toggle_display(["conversion_rate", "net_total", "other_charges_total",
537 "grand_total", "rounded_total", "in_words"],
538 this.frm.doc.currency != company_currency);
539
Rushabh Mehtaa063b4d2013-11-06 11:29:47 +0530540 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
Anand Doshi2168e392013-05-23 19:25:08 +0530541 this.frm.doc.price_list_currency != company_currency);
542
543 // set labels
544 $.each(field_label_map, function(fname, label) {
545 me.frm.fields_dict[fname].set_label(label);
546 });
547 },
548
549 change_grid_labels: function(company_currency) {
550 var me = this;
551 var field_label_map = {};
552
553 var setup_field_label_map = function(fields_list, currency, parentfield) {
554 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
555 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530556 var docfield = wn.meta.docfield_map[grid_doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530557 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530558 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530559 field_label_map[grid_doctype + "-" + fname] =
560 label.trim() + " (" + currency + ")";
561 }
562 });
563 }
564
565 setup_field_label_map(["basic_rate", "base_ref_rate", "amount"],
566 company_currency, this.fname);
567
568 setup_field_label_map(["export_rate", "ref_rate", "export_amount"],
569 this.frm.doc.currency, this.fname);
570
571 setup_field_label_map(["tax_amount", "total"], company_currency, "other_charges");
572
573 if(this.frm.fields_dict["advance_allocation_details"]) {
574 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency,
575 "advance_allocation_details");
576 }
577
578 // toggle columns
579 var item_grid = this.frm.fields_dict[this.fname].grid;
Anand Doshiedfba962013-08-05 11:56:54 +0530580 var show = (this.frm.doc.currency != company_currency) ||
581 (wn.model.get_doclist(cur_frm.doctype, cur_frm.docname,
582 {parentfield: "other_charges", included_in_print_rate: 1}).length);
583
Anand Doshi2168e392013-05-23 19:25:08 +0530584 $.each(["basic_rate", "base_ref_rate", "amount"], function(i, fname) {
585 if(wn.meta.get_docfield(item_grid.doctype, fname))
586 item_grid.set_column_disp(fname, show);
587 });
588
589 // set labels
590 var $wrapper = $(this.frm.wrapper);
591 $.each(field_label_map, function(fname, label) {
Anand Doshi5013dcb2013-08-05 12:16:04 +0530592 fname = fname.split("-");
593 var df = wn.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
594 if(df) df.label = label;
Anand Doshi2168e392013-05-23 19:25:08 +0530595 });
596 },
597
Anand Doshi8f9f8a42013-06-28 19:18:33 +0530598 shipping_address_name: function () {
599 var me = this;
600 if(this.frm.doc.shipping_address_name) {
601 wn.model.with_doc("Address", this.frm.doc.shipping_address_name, function(name) {
602 var address = wn.model.get_doc("Address", name);
603
604 var out = $.map(["address_line1", "address_line2", "city"],
605 function(f) { return address[f]; });
606
607 var state_pincode = $.map(["state", "pincode"], function(f) { return address[f]; }).join(" ");
608 if(state_pincode) out.push(state_pincode);
609
610 if(address["country"]) out.push(address["country"]);
611
612 out.concat($.map([["Phone:", address["phone"]], ["Fax:", address["fax"]]],
613 function(val) { return val[1] ? val.join(" ") : null; }));
614
615 me.frm.set_value("shipping_address", out.join("\n"));
616 });
617 }
618 }
Anand Doshi1dde46a2013-05-15 21:15:57 +0530619});
620
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530621// Help for Sales BOM items
622var set_sales_bom_help = function(doc) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530623 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530624 if (getchildren('Packed Item', doc.name, 'packing_details').length) {
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530625 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
626
627 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Bárbara Perretti4098c262013-09-27 17:05:17 -0300628 help_msg = "<div class='alert alert-warning'>" +
629 wn._("For 'Sales BOM' items, warehouse, serial no and batch no \
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530630 will be considered from the 'Packing List' table. \
631 If warehouse and batch no are same for all packing items for any 'Sales BOM' item, \
Bárbara Perretti4098c262013-09-27 17:05:17 -0300632 those values can be entered in the main item table, values will be copied to 'Packing List' table.")+
633 "</div>";
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530634 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = help_msg;
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530635 }
636 } else {
637 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
638 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530639 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = '';
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530640 }
641 }
642 refresh_field('sales_bom_help');
643}