blob: 330e725ed8ef8fbe6aac08b2c75d45f4570d8dcc [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");
13{% include "public/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 {
Rushabh Mehta6de403f2013-12-13 14:10:14 +053062 query: "erpnext.controllers.queries.item_query",
Anand Doshi9b496142013-07-11 19:13:58 +053063 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({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530161 method: "erpnext.selling.utils.get_item_details",
Anand Doshi1dde46a2013-05-15 21:15:57 +0530162 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 Darjeed203aea2013-12-27 17:49:57 +0530228
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530229 discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530230 this.calculate_taxes_and_totals();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530231 },
Anand Doshif3096132013-05-21 19:35:06 +0530232
Anand Doshif3096132013-05-21 19:35:06 +0530233 commission_rate: function() {
234 this.calculate_commission();
235 refresh_field("total_commission");
236 },
237
238 total_commission: function() {
239 if(this.frm.doc.net_total) {
240 wn.model.round_floats_in(this.frm.doc, ["net_total", "total_commission"]);
241
242 if(this.frm.doc.net_total < this.frm.doc.total_commission) {
243 var msg = (wn._("[Error]") + " " +
244 wn._(wn.meta.get_label(this.frm.doc.doctype, "total_commission",
245 this.frm.doc.name)) + " > " +
246 wn._(wn.meta.get_label(this.frm.doc.doctype, "net_total", this.frm.doc.name)));
247 msgprint(msg);
248 throw msg;
249 }
250
251 this.frm.set_value("commission_rate",
252 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.net_total));
253 }
254 },
255
256 allocated_percentage: function(doc, cdt, cdn) {
257 var sales_person = wn.model.get_doc(cdt, cdn);
258
259 if(sales_person.allocated_percentage) {
260 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
261 precision("allocated_percentage", sales_person));
262 sales_person.allocated_amount = flt(this.frm.doc.net_total *
263 sales_person.allocated_percentage / 100.0,
264 precision("allocated_amount", sales_person));
265
266 refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name,
267 sales_person.parentfield);
268 }
269 },
270
Anand Doshifc777182013-05-27 19:29:07 +0530271 warehouse: function(doc, cdt, cdn) {
Anand Doshi7dd50342013-07-22 16:36:11 +0530272 var item = wn.model.get_doc(cdt, cdn);
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530273 if(item.item_code && item.warehouse) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530274 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530275 method: "erpnext.selling.utils.get_available_qty",
Anand Doshifc777182013-05-27 19:29:07 +0530276 child: item,
277 args: {
278 item_code: item.item_code,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530279 warehouse: item.warehouse,
Anand Doshifc777182013-05-27 19:29:07 +0530280 },
281 });
282 }
283 },
284
Anand Doshif3096132013-05-21 19:35:06 +0530285 toggle_rounded_total: function() {
286 var me = this;
287 if(cint(wn.defaults.get_global_default("disable_rounded_total"))) {
288 $.each(["rounded_total", "rounded_total_export"], function(i, fieldname) {
289 me.frm.set_df_property(fieldname, "print_hide", 1);
290 me.frm.toggle_display(fieldname, false);
291 });
292 }
293 },
294
Anand Doshi33fe8672013-08-02 12:39:10 +0530295 toggle_editable_price_list_rate: function() {
296 var df = wn.meta.get_docfield(this.tname, "ref_rate", this.frm.doc.name);
297 var editable_price_list_rate = cint(wn.defaults.get_default("editable_price_list_rate"));
298
299 if(df && editable_price_list_rate) {
300 df.read_only = 0;
301 }
302 },
303
Anand Doshif3096132013-05-21 19:35:06 +0530304 calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530305 this._super();
Anand Doshi923d41d2013-05-28 17:23:36 +0530306 this.calculate_total_advance("Sales Invoice", "advance_adjustment_details");
Anand Doshif3096132013-05-21 19:35:06 +0530307 this.calculate_commission();
308 this.calculate_contribution();
Anand Doshi923d41d2013-05-28 17:23:36 +0530309
310 // TODO check for custom_recalc in custom scripts of server
Anand Doshif3096132013-05-21 19:35:06 +0530311
Anand Doshi2168e392013-05-23 19:25:08 +0530312 this.frm.refresh_fields();
Anand Doshif3096132013-05-21 19:35:06 +0530313 },
314
315 calculate_item_values: function() {
316 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530317
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530318 if (!this.discount_amount_applied) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530319 $.each(this.frm.item_doclist, function(i, item) {
320 wn.model.round_floats_in(item);
321 item.export_amount = flt(item.export_rate * item.qty, precision("export_amount", item));
322
323 me._set_in_company_currency(item, "ref_rate", "base_ref_rate");
324 me._set_in_company_currency(item, "export_rate", "basic_rate");
325 me._set_in_company_currency(item, "export_amount", "amount");
326 });
327 }
Anand Doshif3096132013-05-21 19:35:06 +0530328 },
329
Anand Doshif3096132013-05-21 19:35:06 +0530330 determine_exclusive_rate: function() {
331 var me = this;
332 $.each(me.frm.item_doclist, function(n, item) {
333 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
334 var cumulated_tax_fraction = 0.0;
335
336 $.each(me.frm.tax_doclist, function(i, tax) {
337 tax.tax_fraction_for_current_item = me.get_current_tax_fraction(tax, item_tax_map);
338
339 if(i==0) {
Anand Doshi2168e392013-05-23 19:25:08 +0530340 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530341 } else {
Anand Doshi2168e392013-05-23 19:25:08 +0530342 tax.grand_total_fraction_for_current_item =
343 me.frm.tax_doclist[i-1].grand_total_fraction_for_current_item +
Anand Doshif3096132013-05-21 19:35:06 +0530344 tax.tax_fraction_for_current_item;
345 }
346
347 cumulated_tax_fraction += tax.tax_fraction_for_current_item;
348 });
349
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530350 if(cumulated_tax_fraction && !me.discount_amount_applied) {
Anand Doshifbe1e162013-08-06 19:38:19 +0530351 item.amount = flt(
352 (item.export_amount * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction),
353 precision("amount", item));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530354
Anand Doshifbe1e162013-08-06 19:38:19 +0530355 item.basic_rate = flt(item.amount / item.qty, precision("basic_rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530356
357 if(item.adj_rate == 100) {
358 item.base_ref_rate = item.basic_rate;
359 item.basic_rate = 0.0;
360 } else {
361 item.base_ref_rate = flt(item.basic_rate / (1 - item.adj_rate / 100.0),
362 precision("base_ref_rate", item));
363 }
364 }
365 });
366 },
367
368 get_current_tax_fraction: function(tax, item_tax_map) {
369 // Get tax fraction for calculating tax exclusive amount
370 // from tax inclusive amount
371 var current_tax_fraction = 0.0;
372
373 if(cint(tax.included_in_print_rate)) {
Anand Doshi2168e392013-05-23 19:25:08 +0530374 var tax_rate = this._get_tax_rate(tax, item_tax_map);
Anand Doshif3096132013-05-21 19:35:06 +0530375
376 if(tax.charge_type == "On Net Total") {
377 current_tax_fraction = (tax_rate / 100.0);
378
379 } else if(tax.charge_type == "On Previous Row Amount") {
380 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530381 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530382
383 } else if(tax.charge_type == "On Previous Row Total") {
384 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530385 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530386 }
387 }
388
389 return current_tax_fraction;
390 },
391
392 calculate_net_total: function() {
393 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530394 this.frm.doc.net_total = this.frm.doc.net_total_export = 0.0;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530395
Anand Doshif3096132013-05-21 19:35:06 +0530396 $.each(this.frm.item_doclist, function(i, item) {
397 me.frm.doc.net_total += item.amount;
398 me.frm.doc.net_total_export += item.export_amount;
399 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530400
Anand Doshif3096132013-05-21 19:35:06 +0530401 wn.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]);
402 },
403
Anand Doshif3096132013-05-21 19:35:06 +0530404 calculate_totals: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530405 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530406 var tax_count = this.frm.tax_doclist.length;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530407
Anand Doshif3096132013-05-21 19:35:06 +0530408 this.frm.doc.grand_total = flt(
409 tax_count ? this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total,
410 precision("grand_total"));
411 this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate,
412 precision("grand_total_export"));
413
414 this.frm.doc.other_charges_total = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
415 precision("other_charges_total"));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530416 this.frm.doc.other_charges_total_export = flt(this.frm.doc.grand_total_export -
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530417 this.frm.doc.net_total_export + flt(this.frm.doc.discount_amount),
Anand Doshif3096132013-05-21 19:35:06 +0530418 precision("other_charges_total_export"));
419
420 this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
421 this.frm.doc.rounded_total_export = Math.round(this.frm.doc.grand_total_export);
422 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530423
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530424 apply_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530425 var me = this;
426 var distributed_amount = 0.0;
427
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530428 if (this.frm.doc.discount_amount) {
429 var grand_total_for_discount_amount = this.get_grand_total_for_discount_amount();
430 // calculate item amount after Discount Amount
431 if (grand_total_for_discount_amount) {
432 $.each(this.frm.item_doclist, function(i, item) {
433 distributed_amount = flt(me.frm.doc.discount_amount) * item.amount / grand_total_for_discount_amount;
434 item.amount = flt(item.amount - distributed_amount, precision("amount", item));
435 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530436
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530437 this.discount_amount_applied = true;
438 this._calculate_taxes_and_totals();
439 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530440 }
441 },
442
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530443 get_grand_total_for_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530444 var me = this;
445 var total_actual_tax = 0.0;
446 var actual_taxes_dict = {};
447
448 $.each(this.frm.tax_doclist, function(i, tax) {
449 if (tax.charge_type == "Actual")
450 actual_taxes_dict[tax.idx] = tax.tax_amount;
451 else if (actual_taxes_dict[tax.row_id] !== null) {
452 actual_tax_amount = flt(actual_taxes_dict[tax.row_id]) * flt(tax.rate) / 100;
453 actual_taxes_dict[tax.idx] = actual_tax_amount;
454 }
455 });
456
457 $.each(actual_taxes_dict, function(key, value) {
458 if (value)
459 total_actual_tax += value;
460 });
461
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530462 grand_total_for_discount_amount = flt(this.frm.doc.grand_total - total_actual_tax,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530463 precision("grand_total"));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530464 return grand_total_for_discount_amount;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530465 },
Anand Doshif3096132013-05-21 19:35:06 +0530466
Anand Doshifc777182013-05-27 19:29:07 +0530467 calculate_outstanding_amount: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530468 // NOTE:
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530469 // paid_amount and write_off_amount is only for POS Invoice
Anand Doshi923d41d2013-05-28 17:23:36 +0530470 // total_advance is only for non POS Invoice
Anand Doshi29ea5d02013-07-05 17:23:14 +0530471 if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.docstatus==0) {
Anand Doshifc777182013-05-27 19:29:07 +0530472 wn.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount",
473 "paid_amount"]);
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530474 var total_amount_to_pay = this.frm.doc.grand_total - this.frm.doc.write_off_amount - this.frm.doc.total_advance;
475 this.frm.doc.paid_amount = this.frm.doc.is_pos? flt(total_amount_to_pay): 0.0;
476
477 this.frm.doc.outstanding_amount = flt(total_amount_to_pay - this.frm.doc.paid_amount,
478 precision("outstanding_amount"));
Anand Doshifc777182013-05-27 19:29:07 +0530479 }
480 },
481
Anand Doshif3096132013-05-21 19:35:06 +0530482 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530483 if(this.frm.fields_dict.commission_rate) {
484 if(this.frm.doc.commission_rate > 100) {
485 var msg = wn._(wn.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
486 " " + wn._("cannot be greater than 100");
487 msgprint(msg);
488 throw msg;
489 }
Anand Doshif3096132013-05-21 19:35:06 +0530490
Anand Doshi923d41d2013-05-28 17:23:36 +0530491 this.frm.doc.total_commission = flt(this.frm.doc.net_total * this.frm.doc.commission_rate / 100.0,
492 precision("total_commission"));
493 }
Anand Doshif3096132013-05-21 19:35:06 +0530494 },
495
496 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530497 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530498 $.each(wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
499 {parentfield: "sales_team"}), function(i, sales_person) {
500 wn.model.round_floats_in(sales_person);
501 if(sales_person.allocated_percentage) {
502 sales_person.allocated_amount = flt(
503 me.frm.doc.net_total * sales_person.allocated_percentage / 100.0,
504 precision("allocated_amount", sales_person));
505 }
506 });
507 },
508
509 _cleanup: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530510 this._super();
511 this.frm.doc.in_words = this.frm.doc.in_words_export = "";
Anand Doshif3096132013-05-21 19:35:06 +0530512 },
Anand Doshi2168e392013-05-23 19:25:08 +0530513
514 show_item_wise_taxes: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530515 if(this.frm.fields_dict.other_charges_calculation) {
516 $(this.get_item_wise_taxes_html())
517 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
518 }
Anand Doshi2168e392013-05-23 19:25:08 +0530519 },
520
Anand Doshi77cd10b2013-06-27 12:17:30 +0530521 charge: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530522 var me = this;
523 if(this.frm.doc.charge) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530524 return this.frm.call({
Anand Doshi2168e392013-05-23 19:25:08 +0530525 doc: this.frm.doc,
526 method: "get_other_charges",
527 callback: function(r) {
528 if(!r.exc) {
529 me.calculate_taxes_and_totals();
530 }
531 }
532 });
533 }
534 },
535
Anand Doshicefccb92013-07-15 18:28:14 +0530536 shipping_rule: function() {
537 var me = this;
538 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530539 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530540 doc: this.frm.doc,
541 method: "apply_shipping_rule",
542 callback: function(r) {
543 if(!r.exc) {
544 me.calculate_taxes_and_totals();
545 }
546 }
547 })
548 }
549 },
550
Anand Doshi2168e392013-05-23 19:25:08 +0530551 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530552 this._super();
Anand Doshi9d049242013-06-05 20:46:56 +0530553 set_sales_bom_help(this.frm.doc);
Anand Doshi2168e392013-05-23 19:25:08 +0530554 },
555
556 change_form_labels: function(company_currency) {
557 var me = this;
558 var field_label_map = {};
559
560 var setup_field_label_map = function(fields_list, currency) {
561 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530562 var docfield = wn.meta.docfield_map[me.frm.doc.doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530563 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530564 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530565 field_label_map[fname] = label.trim() + " (" + currency + ")";
566 }
567 });
568 };
Anand Doshi2168e392013-05-23 19:25:08 +0530569 setup_field_label_map(["net_total", "other_charges_total", "grand_total",
570 "rounded_total", "in_words",
571 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"],
572 company_currency);
573
574 setup_field_label_map(["net_total_export", "other_charges_total_export", "grand_total_export",
575 "rounded_total_export", "in_words_export"], this.frm.doc.currency);
576
Rushabh Mehtaef584552013-11-02 14:47:11 +0530577 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
578 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530579
580 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Rushabh Mehtaef584552013-11-02 14:47:11 +0530581 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
582 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530583 }
584
585 // toggle fields
586 this.frm.toggle_display(["conversion_rate", "net_total", "other_charges_total",
587 "grand_total", "rounded_total", "in_words"],
588 this.frm.doc.currency != company_currency);
589
Rushabh Mehtaa063b4d2013-11-06 11:29:47 +0530590 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
Anand Doshi2168e392013-05-23 19:25:08 +0530591 this.frm.doc.price_list_currency != company_currency);
592
593 // set labels
594 $.each(field_label_map, function(fname, label) {
595 me.frm.fields_dict[fname].set_label(label);
596 });
597 },
598
599 change_grid_labels: function(company_currency) {
600 var me = this;
601 var field_label_map = {};
602
603 var setup_field_label_map = function(fields_list, currency, parentfield) {
604 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
605 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530606 var docfield = wn.meta.docfield_map[grid_doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530607 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530608 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530609 field_label_map[grid_doctype + "-" + fname] =
610 label.trim() + " (" + currency + ")";
611 }
612 });
613 }
614
615 setup_field_label_map(["basic_rate", "base_ref_rate", "amount"],
616 company_currency, this.fname);
617
618 setup_field_label_map(["export_rate", "ref_rate", "export_amount"],
619 this.frm.doc.currency, this.fname);
620
621 setup_field_label_map(["tax_amount", "total"], company_currency, "other_charges");
622
623 if(this.frm.fields_dict["advance_allocation_details"]) {
624 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency,
625 "advance_allocation_details");
626 }
627
628 // toggle columns
629 var item_grid = this.frm.fields_dict[this.fname].grid;
Anand Doshiedfba962013-08-05 11:56:54 +0530630 var show = (this.frm.doc.currency != company_currency) ||
631 (wn.model.get_doclist(cur_frm.doctype, cur_frm.docname,
632 {parentfield: "other_charges", included_in_print_rate: 1}).length);
633
Anand Doshi2168e392013-05-23 19:25:08 +0530634 $.each(["basic_rate", "base_ref_rate", "amount"], function(i, fname) {
635 if(wn.meta.get_docfield(item_grid.doctype, fname))
636 item_grid.set_column_disp(fname, show);
637 });
638
639 // set labels
640 var $wrapper = $(this.frm.wrapper);
641 $.each(field_label_map, function(fname, label) {
Anand Doshi5013dcb2013-08-05 12:16:04 +0530642 fname = fname.split("-");
643 var df = wn.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
644 if(df) df.label = label;
Anand Doshi2168e392013-05-23 19:25:08 +0530645 });
646 },
647
Anand Doshi8f9f8a42013-06-28 19:18:33 +0530648 shipping_address_name: function () {
649 var me = this;
650 if(this.frm.doc.shipping_address_name) {
651 wn.model.with_doc("Address", this.frm.doc.shipping_address_name, function(name) {
652 var address = wn.model.get_doc("Address", name);
653
654 var out = $.map(["address_line1", "address_line2", "city"],
655 function(f) { return address[f]; });
656
657 var state_pincode = $.map(["state", "pincode"], function(f) { return address[f]; }).join(" ");
658 if(state_pincode) out.push(state_pincode);
659
660 if(address["country"]) out.push(address["country"]);
661
662 out.concat($.map([["Phone:", address["phone"]], ["Fax:", address["fax"]]],
663 function(val) { return val[1] ? val.join(" ") : null; }));
664
665 me.frm.set_value("shipping_address", out.join("\n"));
666 });
667 }
668 }
Anand Doshi1dde46a2013-05-15 21:15:57 +0530669});
670
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530671// Help for Sales BOM items
672var set_sales_bom_help = function(doc) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530673 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530674 if (getchildren('Packed Item', doc.name, 'packing_details').length) {
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530675 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
676
677 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Bárbara Perretti4098c262013-09-27 17:05:17 -0300678 help_msg = "<div class='alert alert-warning'>" +
679 wn._("For 'Sales BOM' items, warehouse, serial no and batch no \
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530680 will be considered from the 'Packing List' table. \
681 If warehouse and batch no are same for all packing items for any 'Sales BOM' item, \
Bárbara Perretti4098c262013-09-27 17:05:17 -0300682 those values can be entered in the main item table, values will be copied to 'Packing List' table.")+
683 "</div>";
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530684 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = help_msg;
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530685 }
686 } else {
687 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
688 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530689 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = '';
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530690 }
691 }
692 refresh_field('sales_bom_help');
693}