blob: d8fdb706b0999f037ddff0f15e196ea86518bb17 [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 },
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530228
229 flat_discount: function() {
230 this.flat_discount_applied = false;
231 this.calculate_taxes_and_totals();
232 this.apply_flat_discount();
233 },
Anand Doshif3096132013-05-21 19:35:06 +0530234
Anand Doshif3096132013-05-21 19:35:06 +0530235 commission_rate: function() {
236 this.calculate_commission();
237 refresh_field("total_commission");
238 },
239
240 total_commission: function() {
241 if(this.frm.doc.net_total) {
242 wn.model.round_floats_in(this.frm.doc, ["net_total", "total_commission"]);
243
244 if(this.frm.doc.net_total < this.frm.doc.total_commission) {
245 var msg = (wn._("[Error]") + " " +
246 wn._(wn.meta.get_label(this.frm.doc.doctype, "total_commission",
247 this.frm.doc.name)) + " > " +
248 wn._(wn.meta.get_label(this.frm.doc.doctype, "net_total", this.frm.doc.name)));
249 msgprint(msg);
250 throw msg;
251 }
252
253 this.frm.set_value("commission_rate",
254 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.net_total));
255 }
256 },
257
258 allocated_percentage: function(doc, cdt, cdn) {
259 var sales_person = wn.model.get_doc(cdt, cdn);
260
261 if(sales_person.allocated_percentage) {
262 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
263 precision("allocated_percentage", sales_person));
264 sales_person.allocated_amount = flt(this.frm.doc.net_total *
265 sales_person.allocated_percentage / 100.0,
266 precision("allocated_amount", sales_person));
267
268 refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name,
269 sales_person.parentfield);
270 }
271 },
272
Anand Doshifc777182013-05-27 19:29:07 +0530273 warehouse: function(doc, cdt, cdn) {
Anand Doshi7dd50342013-07-22 16:36:11 +0530274 var item = wn.model.get_doc(cdt, cdn);
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530275 if(item.item_code && item.warehouse) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530276 return this.frm.call({
Anand Doshifc777182013-05-27 19:29:07 +0530277 method: "selling.utils.get_available_qty",
278 child: item,
279 args: {
280 item_code: item.item_code,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530281 warehouse: item.warehouse,
Anand Doshifc777182013-05-27 19:29:07 +0530282 },
283 });
284 }
285 },
286
Anand Doshif3096132013-05-21 19:35:06 +0530287 toggle_rounded_total: function() {
288 var me = this;
289 if(cint(wn.defaults.get_global_default("disable_rounded_total"))) {
290 $.each(["rounded_total", "rounded_total_export"], function(i, fieldname) {
291 me.frm.set_df_property(fieldname, "print_hide", 1);
292 me.frm.toggle_display(fieldname, false);
293 });
294 }
295 },
296
Anand Doshi33fe8672013-08-02 12:39:10 +0530297 toggle_editable_price_list_rate: function() {
298 var df = wn.meta.get_docfield(this.tname, "ref_rate", this.frm.doc.name);
299 var editable_price_list_rate = cint(wn.defaults.get_default("editable_price_list_rate"));
300
301 if(df && editable_price_list_rate) {
302 df.read_only = 0;
303 }
304 },
305
Anand Doshif3096132013-05-21 19:35:06 +0530306 calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530307 this._super();
Anand Doshi923d41d2013-05-28 17:23:36 +0530308 this.calculate_total_advance("Sales Invoice", "advance_adjustment_details");
Anand Doshif3096132013-05-21 19:35:06 +0530309 this.calculate_commission();
310 this.calculate_contribution();
Anand Doshi923d41d2013-05-28 17:23:36 +0530311
312 // TODO check for custom_recalc in custom scripts of server
Anand Doshif3096132013-05-21 19:35:06 +0530313
Anand Doshi2168e392013-05-23 19:25:08 +0530314 this.frm.refresh_fields();
Anand Doshif3096132013-05-21 19:35:06 +0530315 },
316
317 calculate_item_values: function() {
318 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530319
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530320 if (!this.flat_discount_applied) {
321 $.each(this.frm.item_doclist, function(i, item) {
322 wn.model.round_floats_in(item);
323 item.export_amount = flt(item.export_rate * item.qty, precision("export_amount", item));
324
325 me._set_in_company_currency(item, "ref_rate", "base_ref_rate");
326 me._set_in_company_currency(item, "export_rate", "basic_rate");
327 me._set_in_company_currency(item, "export_amount", "amount");
328 });
329 }
Anand Doshif3096132013-05-21 19:35:06 +0530330 },
331
Anand Doshif3096132013-05-21 19:35:06 +0530332 determine_exclusive_rate: function() {
333 var me = this;
334 $.each(me.frm.item_doclist, function(n, item) {
335 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
336 var cumulated_tax_fraction = 0.0;
337
338 $.each(me.frm.tax_doclist, function(i, tax) {
339 tax.tax_fraction_for_current_item = me.get_current_tax_fraction(tax, item_tax_map);
340
341 if(i==0) {
Anand Doshi2168e392013-05-23 19:25:08 +0530342 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530343 } else {
Anand Doshi2168e392013-05-23 19:25:08 +0530344 tax.grand_total_fraction_for_current_item =
345 me.frm.tax_doclist[i-1].grand_total_fraction_for_current_item +
Anand Doshif3096132013-05-21 19:35:06 +0530346 tax.tax_fraction_for_current_item;
347 }
348
349 cumulated_tax_fraction += tax.tax_fraction_for_current_item;
350 });
351
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530352 if(cumulated_tax_fraction && !me.flat_discount_applied) {
Anand Doshifbe1e162013-08-06 19:38:19 +0530353 item.amount = flt(
354 (item.export_amount * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction),
355 precision("amount", item));
356
357 item.basic_rate = flt(item.amount / item.qty, precision("basic_rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530358
359 if(item.adj_rate == 100) {
360 item.base_ref_rate = item.basic_rate;
361 item.basic_rate = 0.0;
362 } else {
363 item.base_ref_rate = flt(item.basic_rate / (1 - item.adj_rate / 100.0),
364 precision("base_ref_rate", item));
365 }
366 }
367 });
368 },
369
370 get_current_tax_fraction: function(tax, item_tax_map) {
371 // Get tax fraction for calculating tax exclusive amount
372 // from tax inclusive amount
373 var current_tax_fraction = 0.0;
374
375 if(cint(tax.included_in_print_rate)) {
Anand Doshi2168e392013-05-23 19:25:08 +0530376 var tax_rate = this._get_tax_rate(tax, item_tax_map);
Anand Doshif3096132013-05-21 19:35:06 +0530377
378 if(tax.charge_type == "On Net Total") {
379 current_tax_fraction = (tax_rate / 100.0);
380
381 } else if(tax.charge_type == "On Previous Row Amount") {
382 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530383 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530384
385 } else if(tax.charge_type == "On Previous Row Total") {
386 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530387 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530388 }
389 }
390
391 return current_tax_fraction;
392 },
393
394 calculate_net_total: function() {
395 var me = this;
396
397 this.frm.doc.net_total = this.frm.doc.net_total_export = 0.0;
398 $.each(this.frm.item_doclist, function(i, item) {
399 me.frm.doc.net_total += item.amount;
400 me.frm.doc.net_total_export += item.export_amount;
401 });
402
403 wn.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]);
404 },
405
Anand Doshif3096132013-05-21 19:35:06 +0530406 calculate_totals: function() {
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530407 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530408 var tax_count = this.frm.tax_doclist.length;
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530409 this.total_tax_excluding_actual = 0.0;
410
Anand Doshif3096132013-05-21 19:35:06 +0530411 this.frm.doc.grand_total = flt(
412 tax_count ? this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total,
413 precision("grand_total"));
414 this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate,
415 precision("grand_total_export"));
416
417 this.frm.doc.other_charges_total = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
418 precision("other_charges_total"));
419 this.frm.doc.other_charges_total_export = flt(
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530420 this.frm.doc.grand_total_export - this.frm.doc.net_total_export + this.frm.doc.flat_discount,
Anand Doshif3096132013-05-21 19:35:06 +0530421 precision("other_charges_total_export"));
422
423 this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
424 this.frm.doc.rounded_total_export = Math.round(this.frm.doc.grand_total_export);
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530425
426 // calculate total amount for flat discount
427 $.each(this.frm.tax_doclist, function(i, tax) {
428 if (tax.charge_type != "Actual") {
429 me.total_tax_excluding_actual += flt(tax.tax_amount, precision("tax_amount", tax));
430 }
431 });
432
433 this.total_amount_for_flat_discount = flt(this.frm.doc.net_total +
434 this.total_tax_excluding_actual, precision("grand_total"));
435 },
436
437 apply_flat_discount: function() {
438 var me = this;
439 var distributed_amount = 0.0;
440
441 if (this.frm.doc.flat_discount && this.total_amount_for_flat_discount) {
442 // calculate item amount after flat discount
443 $.each(this.frm.item_doclist, function(i, item) {
444 distributed_amount = flt(me.frm.doc.flat_discount * item.amount / me.total_amount_for_flat_discount,
445 precision("amount", item));
446 item.amount -= distributed_amount;
447 });
448
449 this.flat_discount_applied = true;
450 this.calculate_taxes_and_totals();
451 }
Anand Doshif3096132013-05-21 19:35:06 +0530452 },
453
Anand Doshifc777182013-05-27 19:29:07 +0530454 calculate_outstanding_amount: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530455 // NOTE:
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530456 // paid_amount and write_off_amount is only for POS Invoice
Anand Doshi923d41d2013-05-28 17:23:36 +0530457 // total_advance is only for non POS Invoice
Anand Doshi29ea5d02013-07-05 17:23:14 +0530458 if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.docstatus==0) {
Anand Doshifc777182013-05-27 19:29:07 +0530459 wn.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount",
460 "paid_amount"]);
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530461 var total_amount_to_pay = this.frm.doc.grand_total - this.frm.doc.write_off_amount - this.frm.doc.total_advance;
462 this.frm.doc.paid_amount = this.frm.doc.is_pos? flt(total_amount_to_pay): 0.0;
463
464 this.frm.doc.outstanding_amount = flt(total_amount_to_pay - this.frm.doc.paid_amount,
465 precision("outstanding_amount"));
Anand Doshifc777182013-05-27 19:29:07 +0530466 }
467 },
468
Anand Doshif3096132013-05-21 19:35:06 +0530469 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530470 if(this.frm.fields_dict.commission_rate) {
471 if(this.frm.doc.commission_rate > 100) {
472 var msg = wn._(wn.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
473 " " + wn._("cannot be greater than 100");
474 msgprint(msg);
475 throw msg;
476 }
Anand Doshif3096132013-05-21 19:35:06 +0530477
Anand Doshi923d41d2013-05-28 17:23:36 +0530478 this.frm.doc.total_commission = flt(this.frm.doc.net_total * this.frm.doc.commission_rate / 100.0,
479 precision("total_commission"));
480 }
Anand Doshif3096132013-05-21 19:35:06 +0530481 },
482
483 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530484 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530485 $.each(wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
486 {parentfield: "sales_team"}), function(i, sales_person) {
487 wn.model.round_floats_in(sales_person);
488 if(sales_person.allocated_percentage) {
489 sales_person.allocated_amount = flt(
490 me.frm.doc.net_total * sales_person.allocated_percentage / 100.0,
491 precision("allocated_amount", sales_person));
492 }
493 });
494 },
495
496 _cleanup: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530497 this._super();
498 this.frm.doc.in_words = this.frm.doc.in_words_export = "";
Anand Doshif3096132013-05-21 19:35:06 +0530499 },
Anand Doshi2168e392013-05-23 19:25:08 +0530500
501 show_item_wise_taxes: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530502 if(this.frm.fields_dict.other_charges_calculation) {
503 $(this.get_item_wise_taxes_html())
504 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
505 }
Anand Doshi2168e392013-05-23 19:25:08 +0530506 },
507
Anand Doshi77cd10b2013-06-27 12:17:30 +0530508 charge: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530509 var me = this;
510 if(this.frm.doc.charge) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530511 return this.frm.call({
Anand Doshi2168e392013-05-23 19:25:08 +0530512 doc: this.frm.doc,
513 method: "get_other_charges",
514 callback: function(r) {
515 if(!r.exc) {
516 me.calculate_taxes_and_totals();
517 }
518 }
519 });
520 }
521 },
522
Anand Doshicefccb92013-07-15 18:28:14 +0530523 shipping_rule: function() {
524 var me = this;
525 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530526 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530527 doc: this.frm.doc,
528 method: "apply_shipping_rule",
529 callback: function(r) {
530 if(!r.exc) {
531 me.calculate_taxes_and_totals();
532 }
533 }
534 })
535 }
536 },
537
Anand Doshi2168e392013-05-23 19:25:08 +0530538 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530539 this._super();
Anand Doshi9d049242013-06-05 20:46:56 +0530540 set_sales_bom_help(this.frm.doc);
Anand Doshi2168e392013-05-23 19:25:08 +0530541 },
542
543 change_form_labels: function(company_currency) {
544 var me = this;
545 var field_label_map = {};
546
547 var setup_field_label_map = function(fields_list, currency) {
548 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530549 var docfield = wn.meta.docfield_map[me.frm.doc.doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530550 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530551 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530552 field_label_map[fname] = label.trim() + " (" + currency + ")";
553 }
554 });
555 };
Anand Doshi2168e392013-05-23 19:25:08 +0530556 setup_field_label_map(["net_total", "other_charges_total", "grand_total",
557 "rounded_total", "in_words",
558 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"],
559 company_currency);
560
561 setup_field_label_map(["net_total_export", "other_charges_total_export", "grand_total_export",
562 "rounded_total_export", "in_words_export"], this.frm.doc.currency);
563
Rushabh Mehtaef584552013-11-02 14:47:11 +0530564 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
565 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530566
567 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Rushabh Mehtaef584552013-11-02 14:47:11 +0530568 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
569 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530570 }
571
572 // toggle fields
573 this.frm.toggle_display(["conversion_rate", "net_total", "other_charges_total",
574 "grand_total", "rounded_total", "in_words"],
575 this.frm.doc.currency != company_currency);
576
Rushabh Mehtaa063b4d2013-11-06 11:29:47 +0530577 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
Anand Doshi2168e392013-05-23 19:25:08 +0530578 this.frm.doc.price_list_currency != company_currency);
579
580 // set labels
581 $.each(field_label_map, function(fname, label) {
582 me.frm.fields_dict[fname].set_label(label);
583 });
584 },
585
586 change_grid_labels: function(company_currency) {
587 var me = this;
588 var field_label_map = {};
589
590 var setup_field_label_map = function(fields_list, currency, parentfield) {
591 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
592 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530593 var docfield = wn.meta.docfield_map[grid_doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530594 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530595 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530596 field_label_map[grid_doctype + "-" + fname] =
597 label.trim() + " (" + currency + ")";
598 }
599 });
600 }
601
602 setup_field_label_map(["basic_rate", "base_ref_rate", "amount"],
603 company_currency, this.fname);
604
605 setup_field_label_map(["export_rate", "ref_rate", "export_amount"],
606 this.frm.doc.currency, this.fname);
607
608 setup_field_label_map(["tax_amount", "total"], company_currency, "other_charges");
609
610 if(this.frm.fields_dict["advance_allocation_details"]) {
611 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency,
612 "advance_allocation_details");
613 }
614
615 // toggle columns
616 var item_grid = this.frm.fields_dict[this.fname].grid;
Anand Doshiedfba962013-08-05 11:56:54 +0530617 var show = (this.frm.doc.currency != company_currency) ||
618 (wn.model.get_doclist(cur_frm.doctype, cur_frm.docname,
619 {parentfield: "other_charges", included_in_print_rate: 1}).length);
620
Anand Doshi2168e392013-05-23 19:25:08 +0530621 $.each(["basic_rate", "base_ref_rate", "amount"], function(i, fname) {
622 if(wn.meta.get_docfield(item_grid.doctype, fname))
623 item_grid.set_column_disp(fname, show);
624 });
625
626 // set labels
627 var $wrapper = $(this.frm.wrapper);
628 $.each(field_label_map, function(fname, label) {
Anand Doshi5013dcb2013-08-05 12:16:04 +0530629 fname = fname.split("-");
630 var df = wn.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
631 if(df) df.label = label;
Anand Doshi2168e392013-05-23 19:25:08 +0530632 });
633 },
634
Anand Doshi8f9f8a42013-06-28 19:18:33 +0530635 shipping_address_name: function () {
636 var me = this;
637 if(this.frm.doc.shipping_address_name) {
638 wn.model.with_doc("Address", this.frm.doc.shipping_address_name, function(name) {
639 var address = wn.model.get_doc("Address", name);
640
641 var out = $.map(["address_line1", "address_line2", "city"],
642 function(f) { return address[f]; });
643
644 var state_pincode = $.map(["state", "pincode"], function(f) { return address[f]; }).join(" ");
645 if(state_pincode) out.push(state_pincode);
646
647 if(address["country"]) out.push(address["country"]);
648
649 out.concat($.map([["Phone:", address["phone"]], ["Fax:", address["fax"]]],
650 function(val) { return val[1] ? val.join(" ") : null; }));
651
652 me.frm.set_value("shipping_address", out.join("\n"));
653 });
654 }
655 }
Anand Doshi1dde46a2013-05-15 21:15:57 +0530656});
657
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530658// Help for Sales BOM items
659var set_sales_bom_help = function(doc) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530660 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530661 if (getchildren('Packed Item', doc.name, 'packing_details').length) {
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530662 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
663
664 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Bárbara Perretti4098c262013-09-27 17:05:17 -0300665 help_msg = "<div class='alert alert-warning'>" +
666 wn._("For 'Sales BOM' items, warehouse, serial no and batch no \
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530667 will be considered from the 'Packing List' table. \
668 If warehouse and batch no are same for all packing items for any 'Sales BOM' item, \
Bárbara Perretti4098c262013-09-27 17:05:17 -0300669 those values can be entered in the main item table, values will be copied to 'Packing List' table.")+
670 "</div>";
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530671 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = help_msg;
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530672 }
673 } else {
674 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
675 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530676 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = '';
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530677 }
678 }
679 refresh_field('sales_bom_help');
680}