blob: cf2347fa6747d66302133b7f2cc1d3bfca968b48 [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
Akhilesh Darjee4f721562014-01-29 16:31:38 +053039 if(this.frm.fields_dict.taxes_and_charges) {
40 this.frm.set_query("taxes_and_charges", 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 {
Akhilesh Darjee4f721562014-01-29 16:31:38 +053084 query : "erpnext.controllers.queries.get_batch_no",
Nabin Haitd1fd1e22013-10-18 12:29:11 +053085 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({
Nabin Hait436f5262014-02-10 14:47:54 +0530130 method: "erpnext.stock.get_item_details.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,
Nabin Hait436f5262014-02-10 14:47:54 +0530143 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),
Nabin Hait436f5262014-02-10 14:47:54 +0530149 "transaction_type": "selling"
Anand Doshi1dde46a2013-05-15 21:15:57 +0530150 }
151 },
152 callback: function(r) {
Anand Doshif3096132013-05-21 19:35:06 +0530153 if(!r.exc) {
Nabin Haita7f757a2014-02-10 17:54:04 +0530154 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
Anand Doshif3096132013-05-21 19:35:06 +0530155 }
Anand Doshi1dde46a2013-05-15 21:15:57 +0530156 }
157 });
158 }
159 }
160 },
161
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530162 selling_price_list: function() {
163 this.get_price_list_currency("Selling");
Anand Doshif3096132013-05-21 19:35:06 +0530164 },
165
Nabin Haita7f757a2014-02-10 17:54:04 +0530166 price_list_rate: function(doc, cdt, cdn) {
Anand Doshif3096132013-05-21 19:35:06 +0530167 var item = wn.model.get_doc(cdt, cdn);
Nabin Haita7f757a2014-02-10 17:54:04 +0530168 wn.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
Anand Doshif3096132013-05-21 19:35:06 +0530169
Nabin Hait7979f7e2014-02-10 18:26:49 +0530170 item.rate = flt(item.price_list_rate * (1 - item.discount_percentage / 100.0),
171 precision("rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530172
173 this.calculate_taxes_and_totals();
174 },
175
Nabin Haita7f757a2014-02-10 17:54:04 +0530176 discount_percentage: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530177 var item = wn.model.get_doc(cdt, cdn);
Nabin Haita7f757a2014-02-10 17:54:04 +0530178 if(!item.price_list_rate) {
179 item.discount_percentage = 0.0;
Anand Doshi923d41d2013-05-28 17:23:36 +0530180 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530181 this.price_list_rate(doc, cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530182 }
Anand Doshif3096132013-05-21 19:35:06 +0530183 },
184
Nabin Hait7979f7e2014-02-10 18:26:49 +0530185 rate: function(doc, cdt, cdn) {
Anand Doshif3096132013-05-21 19:35:06 +0530186 var item = wn.model.get_doc(cdt, cdn);
Nabin Hait7979f7e2014-02-10 18:26:49 +0530187 wn.model.round_floats_in(item, ["rate", "price_list_rate"]);
Anand Doshif3096132013-05-21 19:35:06 +0530188
Nabin Haita7f757a2014-02-10 17:54:04 +0530189 if(item.price_list_rate) {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530190 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0,
Nabin Haita7f757a2014-02-10 17:54:04 +0530191 precision("discount_percentage", item));
Anand Doshif3096132013-05-21 19:35:06 +0530192 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530193 item.discount_percentage = 0.0;
Anand Doshif3096132013-05-21 19:35:06 +0530194 }
195
196 this.calculate_taxes_and_totals();
197 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530198
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530199 discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530200 this.calculate_taxes_and_totals();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530201 },
Anand Doshif3096132013-05-21 19:35:06 +0530202
Anand Doshif3096132013-05-21 19:35:06 +0530203 commission_rate: function() {
204 this.calculate_commission();
205 refresh_field("total_commission");
206 },
207
208 total_commission: function() {
209 if(this.frm.doc.net_total) {
210 wn.model.round_floats_in(this.frm.doc, ["net_total", "total_commission"]);
211
212 if(this.frm.doc.net_total < this.frm.doc.total_commission) {
213 var msg = (wn._("[Error]") + " " +
214 wn._(wn.meta.get_label(this.frm.doc.doctype, "total_commission",
215 this.frm.doc.name)) + " > " +
216 wn._(wn.meta.get_label(this.frm.doc.doctype, "net_total", this.frm.doc.name)));
217 msgprint(msg);
218 throw msg;
219 }
220
221 this.frm.set_value("commission_rate",
222 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.net_total));
223 }
224 },
225
226 allocated_percentage: function(doc, cdt, cdn) {
227 var sales_person = wn.model.get_doc(cdt, cdn);
228
229 if(sales_person.allocated_percentage) {
230 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
231 precision("allocated_percentage", sales_person));
232 sales_person.allocated_amount = flt(this.frm.doc.net_total *
233 sales_person.allocated_percentage / 100.0,
234 precision("allocated_amount", sales_person));
235
236 refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name,
237 sales_person.parentfield);
238 }
239 },
240
Anand Doshifc777182013-05-27 19:29:07 +0530241 warehouse: function(doc, cdt, cdn) {
Anand Doshi7dd50342013-07-22 16:36:11 +0530242 var item = wn.model.get_doc(cdt, cdn);
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530243 if(item.item_code && item.warehouse) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530244 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530245 method: "erpnext.selling.utils.get_available_qty",
Anand Doshifc777182013-05-27 19:29:07 +0530246 child: item,
247 args: {
248 item_code: item.item_code,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530249 warehouse: item.warehouse,
Anand Doshifc777182013-05-27 19:29:07 +0530250 },
251 });
252 }
253 },
254
Anand Doshif3096132013-05-21 19:35:06 +0530255 toggle_rounded_total: function() {
256 var me = this;
257 if(cint(wn.defaults.get_global_default("disable_rounded_total"))) {
258 $.each(["rounded_total", "rounded_total_export"], function(i, fieldname) {
259 me.frm.set_df_property(fieldname, "print_hide", 1);
260 me.frm.toggle_display(fieldname, false);
261 });
262 }
263 },
264
Anand Doshi33fe8672013-08-02 12:39:10 +0530265 toggle_editable_price_list_rate: function() {
Nabin Haita7f757a2014-02-10 17:54:04 +0530266 var df = wn.meta.get_docfield(this.tname, "price_list_rate", this.frm.doc.name);
Anand Doshi33fe8672013-08-02 12:39:10 +0530267 var editable_price_list_rate = cint(wn.defaults.get_default("editable_price_list_rate"));
268
269 if(df && editable_price_list_rate) {
270 df.read_only = 0;
271 }
272 },
273
Anand Doshif3096132013-05-21 19:35:06 +0530274 calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530275 this._super();
Anand Doshi923d41d2013-05-28 17:23:36 +0530276 this.calculate_total_advance("Sales Invoice", "advance_adjustment_details");
Anand Doshif3096132013-05-21 19:35:06 +0530277 this.calculate_commission();
278 this.calculate_contribution();
Anand Doshi923d41d2013-05-28 17:23:36 +0530279
280 // TODO check for custom_recalc in custom scripts of server
Anand Doshif3096132013-05-21 19:35:06 +0530281
Anand Doshi2168e392013-05-23 19:25:08 +0530282 this.frm.refresh_fields();
Anand Doshif3096132013-05-21 19:35:06 +0530283 },
284
285 calculate_item_values: function() {
286 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530287
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530288 if (!this.discount_amount_applied) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530289 $.each(this.frm.item_doclist, function(i, item) {
290 wn.model.round_floats_in(item);
Nabin Hait1eb56012014-02-10 19:20:15 +0530291 item.amount = flt(item.rate * item.qty, precision("amount", item));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530292
Nabin Haita7f757a2014-02-10 17:54:04 +0530293 me._set_in_company_currency(item, "price_list_rate", "base_price_list_rate");
Nabin Hait7979f7e2014-02-10 18:26:49 +0530294 me._set_in_company_currency(item, "rate", "base_rate");
Nabin Hait1eb56012014-02-10 19:20:15 +0530295 me._set_in_company_currency(item, "amount", "base_amount");
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530296 });
297 }
Anand Doshif3096132013-05-21 19:35:06 +0530298 },
299
Anand Doshif3096132013-05-21 19:35:06 +0530300 determine_exclusive_rate: function() {
301 var me = this;
302 $.each(me.frm.item_doclist, function(n, item) {
303 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
304 var cumulated_tax_fraction = 0.0;
305
306 $.each(me.frm.tax_doclist, function(i, tax) {
307 tax.tax_fraction_for_current_item = me.get_current_tax_fraction(tax, item_tax_map);
308
309 if(i==0) {
Anand Doshi2168e392013-05-23 19:25:08 +0530310 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530311 } else {
Anand Doshi2168e392013-05-23 19:25:08 +0530312 tax.grand_total_fraction_for_current_item =
313 me.frm.tax_doclist[i-1].grand_total_fraction_for_current_item +
Anand Doshif3096132013-05-21 19:35:06 +0530314 tax.tax_fraction_for_current_item;
315 }
316
317 cumulated_tax_fraction += tax.tax_fraction_for_current_item;
318 });
319
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530320 if(cumulated_tax_fraction && !me.discount_amount_applied) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530321 item.base_amount = flt(
322 (item.amount * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction),
323 precision("base_amount", item));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530324
Nabin Hait1eb56012014-02-10 19:20:15 +0530325 item.base_rate = flt(item.base_amount / item.qty, precision("base_rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530326
Nabin Haita7f757a2014-02-10 17:54:04 +0530327 if(item.discount_percentage == 100) {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530328 item.base_price_list_rate = item.base_rate;
329 item.base_rate = 0.0;
Anand Doshif3096132013-05-21 19:35:06 +0530330 } else {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530331 item.base_price_list_rate = flt(item.base_rate / (1 - item.discount_percentage / 100.0),
Nabin Haita7f757a2014-02-10 17:54:04 +0530332 precision("base_price_list_rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530333 }
334 }
335 });
336 },
337
338 get_current_tax_fraction: function(tax, item_tax_map) {
339 // Get tax fraction for calculating tax exclusive amount
340 // from tax inclusive amount
341 var current_tax_fraction = 0.0;
342
343 if(cint(tax.included_in_print_rate)) {
Anand Doshi2168e392013-05-23 19:25:08 +0530344 var tax_rate = this._get_tax_rate(tax, item_tax_map);
Anand Doshif3096132013-05-21 19:35:06 +0530345
346 if(tax.charge_type == "On Net Total") {
347 current_tax_fraction = (tax_rate / 100.0);
348
349 } else if(tax.charge_type == "On Previous Row Amount") {
350 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530351 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530352
353 } else if(tax.charge_type == "On Previous Row Total") {
354 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530355 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530356 }
357 }
358
359 return current_tax_fraction;
360 },
361
362 calculate_net_total: function() {
363 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530364 this.frm.doc.net_total = this.frm.doc.net_total_export = 0.0;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530365
Anand Doshif3096132013-05-21 19:35:06 +0530366 $.each(this.frm.item_doclist, function(i, item) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530367 me.frm.doc.net_total += item.base_amount;
368 me.frm.doc.net_total_export += item.amount;
Anand Doshif3096132013-05-21 19:35:06 +0530369 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530370
Anand Doshif3096132013-05-21 19:35:06 +0530371 wn.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]);
372 },
373
Anand Doshif3096132013-05-21 19:35:06 +0530374 calculate_totals: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530375 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530376 var tax_count = this.frm.tax_doclist.length;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530377
Anand Doshif3096132013-05-21 19:35:06 +0530378 this.frm.doc.grand_total = flt(
379 tax_count ? this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total,
380 precision("grand_total"));
381 this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate,
382 precision("grand_total_export"));
383
384 this.frm.doc.other_charges_total = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
385 precision("other_charges_total"));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530386 this.frm.doc.other_charges_total_export = flt(this.frm.doc.grand_total_export -
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530387 this.frm.doc.net_total_export + flt(this.frm.doc.discount_amount),
Anand Doshif3096132013-05-21 19:35:06 +0530388 precision("other_charges_total_export"));
389
390 this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
391 this.frm.doc.rounded_total_export = Math.round(this.frm.doc.grand_total_export);
392 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530393
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530394 apply_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530395 var me = this;
396 var distributed_amount = 0.0;
397
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530398 if (this.frm.doc.discount_amount) {
399 var grand_total_for_discount_amount = this.get_grand_total_for_discount_amount();
400 // calculate item amount after Discount Amount
401 if (grand_total_for_discount_amount) {
402 $.each(this.frm.item_doclist, function(i, item) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530403 distributed_amount = flt(me.frm.doc.discount_amount) * item.base_amount / grand_total_for_discount_amount;
404 item.base_amount = flt(item.base_amount - distributed_amount, precision("base_amount", item));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530405 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530406
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530407 this.discount_amount_applied = true;
408 this._calculate_taxes_and_totals();
409 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530410 }
411 },
412
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530413 get_grand_total_for_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530414 var me = this;
415 var total_actual_tax = 0.0;
416 var actual_taxes_dict = {};
417
418 $.each(this.frm.tax_doclist, function(i, tax) {
419 if (tax.charge_type == "Actual")
420 actual_taxes_dict[tax.idx] = tax.tax_amount;
421 else if (actual_taxes_dict[tax.row_id] !== null) {
422 actual_tax_amount = flt(actual_taxes_dict[tax.row_id]) * flt(tax.rate) / 100;
423 actual_taxes_dict[tax.idx] = actual_tax_amount;
424 }
425 });
426
427 $.each(actual_taxes_dict, function(key, value) {
428 if (value)
429 total_actual_tax += value;
430 });
431
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530432 grand_total_for_discount_amount = flt(this.frm.doc.grand_total - total_actual_tax,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530433 precision("grand_total"));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530434 return grand_total_for_discount_amount;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530435 },
Anand Doshif3096132013-05-21 19:35:06 +0530436
Anand Doshifc777182013-05-27 19:29:07 +0530437 calculate_outstanding_amount: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530438 // NOTE:
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530439 // paid_amount and write_off_amount is only for POS Invoice
Anand Doshi923d41d2013-05-28 17:23:36 +0530440 // total_advance is only for non POS Invoice
Anand Doshi29ea5d02013-07-05 17:23:14 +0530441 if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.docstatus==0) {
Anand Doshifc777182013-05-27 19:29:07 +0530442 wn.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount",
443 "paid_amount"]);
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530444 var total_amount_to_pay = this.frm.doc.grand_total - this.frm.doc.write_off_amount - this.frm.doc.total_advance;
445 this.frm.doc.paid_amount = this.frm.doc.is_pos? flt(total_amount_to_pay): 0.0;
446
447 this.frm.doc.outstanding_amount = flt(total_amount_to_pay - this.frm.doc.paid_amount,
448 precision("outstanding_amount"));
Anand Doshifc777182013-05-27 19:29:07 +0530449 }
450 },
451
Anand Doshif3096132013-05-21 19:35:06 +0530452 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530453 if(this.frm.fields_dict.commission_rate) {
454 if(this.frm.doc.commission_rate > 100) {
455 var msg = wn._(wn.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
456 " " + wn._("cannot be greater than 100");
457 msgprint(msg);
458 throw msg;
459 }
Anand Doshif3096132013-05-21 19:35:06 +0530460
Anand Doshi923d41d2013-05-28 17:23:36 +0530461 this.frm.doc.total_commission = flt(this.frm.doc.net_total * this.frm.doc.commission_rate / 100.0,
462 precision("total_commission"));
463 }
Anand Doshif3096132013-05-21 19:35:06 +0530464 },
465
466 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530467 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530468 $.each(wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
469 {parentfield: "sales_team"}), function(i, sales_person) {
470 wn.model.round_floats_in(sales_person);
471 if(sales_person.allocated_percentage) {
472 sales_person.allocated_amount = flt(
473 me.frm.doc.net_total * sales_person.allocated_percentage / 100.0,
474 precision("allocated_amount", sales_person));
475 }
476 });
477 },
478
479 _cleanup: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530480 this._super();
481 this.frm.doc.in_words = this.frm.doc.in_words_export = "";
Anand Doshif3096132013-05-21 19:35:06 +0530482 },
Anand Doshi2168e392013-05-23 19:25:08 +0530483
Anand Doshicefccb92013-07-15 18:28:14 +0530484 shipping_rule: function() {
485 var me = this;
486 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530487 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530488 doc: this.frm.doc,
489 method: "apply_shipping_rule",
490 callback: function(r) {
491 if(!r.exc) {
492 me.calculate_taxes_and_totals();
493 }
494 }
495 })
496 }
497 },
498
Anand Doshi2168e392013-05-23 19:25:08 +0530499 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530500 this._super();
Anand Doshi9d049242013-06-05 20:46:56 +0530501 set_sales_bom_help(this.frm.doc);
Anand Doshi2168e392013-05-23 19:25:08 +0530502 },
503
504 change_form_labels: function(company_currency) {
505 var me = this;
506 var field_label_map = {};
507
508 var setup_field_label_map = function(fields_list, currency) {
509 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530510 var docfield = wn.meta.docfield_map[me.frm.doc.doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530511 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530512 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530513 field_label_map[fname] = label.trim() + " (" + currency + ")";
514 }
515 });
516 };
Anand Doshi2168e392013-05-23 19:25:08 +0530517 setup_field_label_map(["net_total", "other_charges_total", "grand_total",
518 "rounded_total", "in_words",
519 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"],
520 company_currency);
521
522 setup_field_label_map(["net_total_export", "other_charges_total_export", "grand_total_export",
523 "rounded_total_export", "in_words_export"], this.frm.doc.currency);
524
Rushabh Mehtaef584552013-11-02 14:47:11 +0530525 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
526 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530527
528 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Rushabh Mehtaef584552013-11-02 14:47:11 +0530529 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
530 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530531 }
532
533 // toggle fields
534 this.frm.toggle_display(["conversion_rate", "net_total", "other_charges_total",
535 "grand_total", "rounded_total", "in_words"],
536 this.frm.doc.currency != company_currency);
537
Rushabh Mehtaa063b4d2013-11-06 11:29:47 +0530538 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
Anand Doshi2168e392013-05-23 19:25:08 +0530539 this.frm.doc.price_list_currency != company_currency);
540
541 // set labels
542 $.each(field_label_map, function(fname, label) {
543 me.frm.fields_dict[fname].set_label(label);
544 });
545 },
546
547 change_grid_labels: function(company_currency) {
548 var me = this;
549 var field_label_map = {};
550
551 var setup_field_label_map = function(fields_list, currency, parentfield) {
552 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
553 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530554 var docfield = wn.meta.docfield_map[grid_doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530555 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530556 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530557 field_label_map[grid_doctype + "-" + fname] =
558 label.trim() + " (" + currency + ")";
559 }
560 });
561 }
562
Nabin Hait1eb56012014-02-10 19:20:15 +0530563 setup_field_label_map(["base_rate", "base_price_list_rate", "base_amount"],
Anand Doshi2168e392013-05-23 19:25:08 +0530564 company_currency, this.fname);
565
Nabin Hait1eb56012014-02-10 19:20:15 +0530566 setup_field_label_map(["rate", "price_list_rate", "amount"],
Anand Doshi2168e392013-05-23 19:25:08 +0530567 this.frm.doc.currency, this.fname);
568
569 setup_field_label_map(["tax_amount", "total"], company_currency, "other_charges");
570
571 if(this.frm.fields_dict["advance_allocation_details"]) {
572 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency,
573 "advance_allocation_details");
574 }
575
576 // toggle columns
577 var item_grid = this.frm.fields_dict[this.fname].grid;
Anand Doshiedfba962013-08-05 11:56:54 +0530578 var show = (this.frm.doc.currency != company_currency) ||
579 (wn.model.get_doclist(cur_frm.doctype, cur_frm.docname,
580 {parentfield: "other_charges", included_in_print_rate: 1}).length);
581
Nabin Hait1eb56012014-02-10 19:20:15 +0530582 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
Anand Doshi2168e392013-05-23 19:25:08 +0530583 if(wn.meta.get_docfield(item_grid.doctype, fname))
584 item_grid.set_column_disp(fname, show);
585 });
586
587 // set labels
588 var $wrapper = $(this.frm.wrapper);
589 $.each(field_label_map, function(fname, label) {
Anand Doshi5013dcb2013-08-05 12:16:04 +0530590 fname = fname.split("-");
591 var df = wn.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
592 if(df) df.label = label;
Anand Doshi2168e392013-05-23 19:25:08 +0530593 });
594 },
595
Anand Doshi8f9f8a42013-06-28 19:18:33 +0530596 shipping_address_name: function () {
597 var me = this;
598 if(this.frm.doc.shipping_address_name) {
599 wn.model.with_doc("Address", this.frm.doc.shipping_address_name, function(name) {
600 var address = wn.model.get_doc("Address", name);
601
602 var out = $.map(["address_line1", "address_line2", "city"],
603 function(f) { return address[f]; });
604
605 var state_pincode = $.map(["state", "pincode"], function(f) { return address[f]; }).join(" ");
606 if(state_pincode) out.push(state_pincode);
607
608 if(address["country"]) out.push(address["country"]);
609
610 out.concat($.map([["Phone:", address["phone"]], ["Fax:", address["fax"]]],
611 function(val) { return val[1] ? val.join(" ") : null; }));
612
613 me.frm.set_value("shipping_address", out.join("\n"));
614 });
615 }
616 }
Anand Doshi1dde46a2013-05-15 21:15:57 +0530617});
618
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530619// Help for Sales BOM items
620var set_sales_bom_help = function(doc) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530621 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530622 if (getchildren('Packed Item', doc.name, 'packing_details').length) {
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530623 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
624
625 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Bárbara Perretti4098c262013-09-27 17:05:17 -0300626 help_msg = "<div class='alert alert-warning'>" +
627 wn._("For 'Sales BOM' items, warehouse, serial no and batch no \
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530628 will be considered from the 'Packing List' table. \
629 If warehouse and batch no are same for all packing items for any 'Sales BOM' item, \
Bárbara Perretti4098c262013-09-27 17:05:17 -0300630 those values can be entered in the main item table, values will be copied to 'Packing List' table.")+
631 "</div>";
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530632 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = help_msg;
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530633 }
634 } else {
635 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
636 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530637 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = '';
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530638 }
639 }
640 refresh_field('sales_bom_help');
641}