blob: 6eb21368949288021ca519415eaca963443177c4 [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
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053011frappe.provide("erpnext.selling");
12frappe.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) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053073 var item = frappe.model.get_doc(cdt, cdn);
Anand Doshi99100a42013-07-04 17:13:53 +053074 if(!item.item_code) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053075 frappe.throw(frappe._("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
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530122 selling_price_list: function() {
123 this.get_price_list_currency("Selling");
Anand Doshif3096132013-05-21 19:35:06 +0530124 },
125
Nabin Haita7f757a2014-02-10 17:54:04 +0530126 price_list_rate: function(doc, cdt, cdn) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530127 var item = frappe.model.get_doc(cdt, cdn);
128 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
Anand Doshif3096132013-05-21 19:35:06 +0530129
Nabin Hait7979f7e2014-02-10 18:26:49 +0530130 item.rate = flt(item.price_list_rate * (1 - item.discount_percentage / 100.0),
131 precision("rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530132
133 this.calculate_taxes_and_totals();
134 },
135
Nabin Haita7f757a2014-02-10 17:54:04 +0530136 discount_percentage: function(doc, cdt, cdn) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530137 var item = frappe.model.get_doc(cdt, cdn);
Nabin Haita7f757a2014-02-10 17:54:04 +0530138 if(!item.price_list_rate) {
139 item.discount_percentage = 0.0;
Anand Doshi923d41d2013-05-28 17:23:36 +0530140 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530141 this.price_list_rate(doc, cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530142 }
Anand Doshif3096132013-05-21 19:35:06 +0530143 },
144
Nabin Hait7979f7e2014-02-10 18:26:49 +0530145 rate: function(doc, cdt, cdn) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530146 var item = frappe.model.get_doc(cdt, cdn);
147 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
Anand Doshif3096132013-05-21 19:35:06 +0530148
Nabin Haita7f757a2014-02-10 17:54:04 +0530149 if(item.price_list_rate) {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530150 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0,
Nabin Haita7f757a2014-02-10 17:54:04 +0530151 precision("discount_percentage", item));
Anand Doshif3096132013-05-21 19:35:06 +0530152 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530153 item.discount_percentage = 0.0;
Anand Doshif3096132013-05-21 19:35:06 +0530154 }
155
156 this.calculate_taxes_and_totals();
157 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530158
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530159 discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530160 this.calculate_taxes_and_totals();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530161 },
Anand Doshif3096132013-05-21 19:35:06 +0530162
Anand Doshif3096132013-05-21 19:35:06 +0530163 commission_rate: function() {
164 this.calculate_commission();
165 refresh_field("total_commission");
166 },
167
168 total_commission: function() {
169 if(this.frm.doc.net_total) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530170 frappe.model.round_floats_in(this.frm.doc, ["net_total", "total_commission"]);
Anand Doshif3096132013-05-21 19:35:06 +0530171
172 if(this.frm.doc.net_total < this.frm.doc.total_commission) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530173 var msg = (frappe._("[Error]") + " " +
174 frappe._(frappe.meta.get_label(this.frm.doc.doctype, "total_commission",
Anand Doshif3096132013-05-21 19:35:06 +0530175 this.frm.doc.name)) + " > " +
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530176 frappe._(frappe.meta.get_label(this.frm.doc.doctype, "net_total", this.frm.doc.name)));
Anand Doshif3096132013-05-21 19:35:06 +0530177 msgprint(msg);
178 throw msg;
179 }
180
181 this.frm.set_value("commission_rate",
182 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.net_total));
183 }
184 },
185
186 allocated_percentage: function(doc, cdt, cdn) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530187 var sales_person = frappe.model.get_doc(cdt, cdn);
Anand Doshif3096132013-05-21 19:35:06 +0530188
189 if(sales_person.allocated_percentage) {
190 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
191 precision("allocated_percentage", sales_person));
192 sales_person.allocated_amount = flt(this.frm.doc.net_total *
193 sales_person.allocated_percentage / 100.0,
194 precision("allocated_amount", sales_person));
195
196 refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name,
197 sales_person.parentfield);
198 }
199 },
200
Anand Doshifc777182013-05-27 19:29:07 +0530201 warehouse: function(doc, cdt, cdn) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530202 var item = frappe.model.get_doc(cdt, cdn);
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530203 if(item.item_code && item.warehouse) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530204 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530205 method: "erpnext.selling.utils.get_available_qty",
Anand Doshifc777182013-05-27 19:29:07 +0530206 child: item,
207 args: {
208 item_code: item.item_code,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530209 warehouse: item.warehouse,
Anand Doshifc777182013-05-27 19:29:07 +0530210 },
211 });
212 }
213 },
214
Anand Doshif3096132013-05-21 19:35:06 +0530215 toggle_rounded_total: function() {
216 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530217 if(cint(frappe.defaults.get_global_default("disable_rounded_total"))) {
Anand Doshif3096132013-05-21 19:35:06 +0530218 $.each(["rounded_total", "rounded_total_export"], function(i, fieldname) {
219 me.frm.set_df_property(fieldname, "print_hide", 1);
220 me.frm.toggle_display(fieldname, false);
221 });
222 }
223 },
224
Anand Doshi33fe8672013-08-02 12:39:10 +0530225 toggle_editable_price_list_rate: function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530226 var df = frappe.meta.get_docfield(this.tname, "price_list_rate", this.frm.doc.name);
227 var editable_price_list_rate = cint(frappe.defaults.get_default("editable_price_list_rate"));
Anand Doshi33fe8672013-08-02 12:39:10 +0530228
229 if(df && editable_price_list_rate) {
230 df.read_only = 0;
231 }
232 },
233
Anand Doshif3096132013-05-21 19:35:06 +0530234 calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530235 this._super();
Anand Doshi923d41d2013-05-28 17:23:36 +0530236 this.calculate_total_advance("Sales Invoice", "advance_adjustment_details");
Anand Doshif3096132013-05-21 19:35:06 +0530237 this.calculate_commission();
238 this.calculate_contribution();
Anand Doshi923d41d2013-05-28 17:23:36 +0530239
240 // TODO check for custom_recalc in custom scripts of server
Anand Doshif3096132013-05-21 19:35:06 +0530241
Anand Doshi2168e392013-05-23 19:25:08 +0530242 this.frm.refresh_fields();
Anand Doshif3096132013-05-21 19:35:06 +0530243 },
244
245 calculate_item_values: function() {
246 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530247
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530248 if (!this.discount_amount_applied) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530249 $.each(this.frm.item_doclist, function(i, item) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530250 frappe.model.round_floats_in(item);
Nabin Hait1eb56012014-02-10 19:20:15 +0530251 item.amount = flt(item.rate * item.qty, precision("amount", item));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530252
Nabin Haita7f757a2014-02-10 17:54:04 +0530253 me._set_in_company_currency(item, "price_list_rate", "base_price_list_rate");
Nabin Hait7979f7e2014-02-10 18:26:49 +0530254 me._set_in_company_currency(item, "rate", "base_rate");
Nabin Hait1eb56012014-02-10 19:20:15 +0530255 me._set_in_company_currency(item, "amount", "base_amount");
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530256 });
257 }
Anand Doshif3096132013-05-21 19:35:06 +0530258 },
259
Anand Doshif3096132013-05-21 19:35:06 +0530260 determine_exclusive_rate: function() {
261 var me = this;
262 $.each(me.frm.item_doclist, function(n, item) {
263 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
264 var cumulated_tax_fraction = 0.0;
265
266 $.each(me.frm.tax_doclist, function(i, tax) {
267 tax.tax_fraction_for_current_item = me.get_current_tax_fraction(tax, item_tax_map);
268
269 if(i==0) {
Anand Doshi2168e392013-05-23 19:25:08 +0530270 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530271 } else {
Anand Doshi2168e392013-05-23 19:25:08 +0530272 tax.grand_total_fraction_for_current_item =
273 me.frm.tax_doclist[i-1].grand_total_fraction_for_current_item +
Anand Doshif3096132013-05-21 19:35:06 +0530274 tax.tax_fraction_for_current_item;
275 }
276
277 cumulated_tax_fraction += tax.tax_fraction_for_current_item;
278 });
279
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530280 if(cumulated_tax_fraction && !me.discount_amount_applied) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530281 item.base_amount = flt(
282 (item.amount * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction),
283 precision("base_amount", item));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530284
Nabin Hait1eb56012014-02-10 19:20:15 +0530285 item.base_rate = flt(item.base_amount / item.qty, precision("base_rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530286
Nabin Haita7f757a2014-02-10 17:54:04 +0530287 if(item.discount_percentage == 100) {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530288 item.base_price_list_rate = item.base_rate;
289 item.base_rate = 0.0;
Anand Doshif3096132013-05-21 19:35:06 +0530290 } else {
Nabin Hait7979f7e2014-02-10 18:26:49 +0530291 item.base_price_list_rate = flt(item.base_rate / (1 - item.discount_percentage / 100.0),
Nabin Haita7f757a2014-02-10 17:54:04 +0530292 precision("base_price_list_rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530293 }
294 }
295 });
296 },
297
298 get_current_tax_fraction: function(tax, item_tax_map) {
299 // Get tax fraction for calculating tax exclusive amount
300 // from tax inclusive amount
301 var current_tax_fraction = 0.0;
302
303 if(cint(tax.included_in_print_rate)) {
Anand Doshi2168e392013-05-23 19:25:08 +0530304 var tax_rate = this._get_tax_rate(tax, item_tax_map);
Anand Doshif3096132013-05-21 19:35:06 +0530305
306 if(tax.charge_type == "On Net Total") {
307 current_tax_fraction = (tax_rate / 100.0);
308
309 } else if(tax.charge_type == "On Previous Row Amount") {
310 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530311 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530312
313 } else if(tax.charge_type == "On Previous Row Total") {
314 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530315 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530316 }
317 }
318
319 return current_tax_fraction;
320 },
321
322 calculate_net_total: function() {
323 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530324 this.frm.doc.net_total = this.frm.doc.net_total_export = 0.0;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530325
Anand Doshif3096132013-05-21 19:35:06 +0530326 $.each(this.frm.item_doclist, function(i, item) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530327 me.frm.doc.net_total += item.base_amount;
328 me.frm.doc.net_total_export += item.amount;
Anand Doshif3096132013-05-21 19:35:06 +0530329 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530330
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530331 frappe.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]);
Anand Doshif3096132013-05-21 19:35:06 +0530332 },
333
Anand Doshif3096132013-05-21 19:35:06 +0530334 calculate_totals: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530335 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530336 var tax_count = this.frm.tax_doclist.length;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530337
Anand Doshif3096132013-05-21 19:35:06 +0530338 this.frm.doc.grand_total = flt(
339 tax_count ? this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total,
340 precision("grand_total"));
341 this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate,
342 precision("grand_total_export"));
343
344 this.frm.doc.other_charges_total = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
345 precision("other_charges_total"));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530346 this.frm.doc.other_charges_total_export = flt(this.frm.doc.grand_total_export -
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530347 this.frm.doc.net_total_export + flt(this.frm.doc.discount_amount),
Anand Doshif3096132013-05-21 19:35:06 +0530348 precision("other_charges_total_export"));
349
350 this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
351 this.frm.doc.rounded_total_export = Math.round(this.frm.doc.grand_total_export);
352 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530353
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530354 apply_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530355 var me = this;
356 var distributed_amount = 0.0;
357
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530358 if (this.frm.doc.discount_amount) {
359 var grand_total_for_discount_amount = this.get_grand_total_for_discount_amount();
360 // calculate item amount after Discount Amount
361 if (grand_total_for_discount_amount) {
362 $.each(this.frm.item_doclist, function(i, item) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530363 distributed_amount = flt(me.frm.doc.discount_amount) * item.base_amount / grand_total_for_discount_amount;
364 item.base_amount = flt(item.base_amount - distributed_amount, precision("base_amount", item));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530365 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530366
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530367 this.discount_amount_applied = true;
368 this._calculate_taxes_and_totals();
369 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530370 }
371 },
372
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530373 get_grand_total_for_discount_amount: function() {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530374 var me = this;
375 var total_actual_tax = 0.0;
376 var actual_taxes_dict = {};
377
378 $.each(this.frm.tax_doclist, function(i, tax) {
379 if (tax.charge_type == "Actual")
380 actual_taxes_dict[tax.idx] = tax.tax_amount;
381 else if (actual_taxes_dict[tax.row_id] !== null) {
382 actual_tax_amount = flt(actual_taxes_dict[tax.row_id]) * flt(tax.rate) / 100;
383 actual_taxes_dict[tax.idx] = actual_tax_amount;
384 }
385 });
386
387 $.each(actual_taxes_dict, function(key, value) {
388 if (value)
389 total_actual_tax += value;
390 });
391
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530392 grand_total_for_discount_amount = flt(this.frm.doc.grand_total - total_actual_tax,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530393 precision("grand_total"));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530394 return grand_total_for_discount_amount;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530395 },
Anand Doshif3096132013-05-21 19:35:06 +0530396
Anand Doshifc777182013-05-27 19:29:07 +0530397 calculate_outstanding_amount: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530398 // NOTE:
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530399 // paid_amount and write_off_amount is only for POS Invoice
Anand Doshi923d41d2013-05-28 17:23:36 +0530400 // total_advance is only for non POS Invoice
Anand Doshi29ea5d02013-07-05 17:23:14 +0530401 if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.docstatus==0) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530402 frappe.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount",
Anand Doshifc777182013-05-27 19:29:07 +0530403 "paid_amount"]);
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530404 var total_amount_to_pay = this.frm.doc.grand_total - this.frm.doc.write_off_amount - this.frm.doc.total_advance;
405 this.frm.doc.paid_amount = this.frm.doc.is_pos? flt(total_amount_to_pay): 0.0;
406
407 this.frm.doc.outstanding_amount = flt(total_amount_to_pay - this.frm.doc.paid_amount,
408 precision("outstanding_amount"));
Anand Doshifc777182013-05-27 19:29:07 +0530409 }
410 },
411
Anand Doshif3096132013-05-21 19:35:06 +0530412 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530413 if(this.frm.fields_dict.commission_rate) {
414 if(this.frm.doc.commission_rate > 100) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530415 var msg = frappe._(frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
416 " " + frappe._("cannot be greater than 100");
Anand Doshi923d41d2013-05-28 17:23:36 +0530417 msgprint(msg);
418 throw msg;
419 }
Anand Doshif3096132013-05-21 19:35:06 +0530420
Anand Doshi923d41d2013-05-28 17:23:36 +0530421 this.frm.doc.total_commission = flt(this.frm.doc.net_total * this.frm.doc.commission_rate / 100.0,
422 precision("total_commission"));
423 }
Anand Doshif3096132013-05-21 19:35:06 +0530424 },
425
426 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530427 var me = this;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530428 $.each(frappe.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
Anand Doshif3096132013-05-21 19:35:06 +0530429 {parentfield: "sales_team"}), function(i, sales_person) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530430 frappe.model.round_floats_in(sales_person);
Anand Doshif3096132013-05-21 19:35:06 +0530431 if(sales_person.allocated_percentage) {
432 sales_person.allocated_amount = flt(
433 me.frm.doc.net_total * sales_person.allocated_percentage / 100.0,
434 precision("allocated_amount", sales_person));
435 }
436 });
437 },
438
439 _cleanup: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530440 this._super();
441 this.frm.doc.in_words = this.frm.doc.in_words_export = "";
Anand Doshif3096132013-05-21 19:35:06 +0530442 },
Anand Doshi2168e392013-05-23 19:25:08 +0530443
Anand Doshicefccb92013-07-15 18:28:14 +0530444 shipping_rule: function() {
445 var me = this;
446 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530447 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530448 doc: this.frm.doc,
449 method: "apply_shipping_rule",
450 callback: function(r) {
451 if(!r.exc) {
452 me.calculate_taxes_and_totals();
453 }
454 }
455 })
456 }
457 },
458
Anand Doshi2168e392013-05-23 19:25:08 +0530459 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530460 this._super();
Anand Doshi9d049242013-06-05 20:46:56 +0530461 set_sales_bom_help(this.frm.doc);
Anand Doshi2168e392013-05-23 19:25:08 +0530462 },
463
464 change_form_labels: function(company_currency) {
465 var me = this;
466 var field_label_map = {};
467
468 var setup_field_label_map = function(fields_list, currency) {
469 $.each(fields_list, function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530470 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530471 if(docfield) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530472 var label = frappe._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530473 field_label_map[fname] = label.trim() + " (" + currency + ")";
474 }
475 });
476 };
Anand Doshi2168e392013-05-23 19:25:08 +0530477 setup_field_label_map(["net_total", "other_charges_total", "grand_total",
478 "rounded_total", "in_words",
479 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"],
480 company_currency);
481
482 setup_field_label_map(["net_total_export", "other_charges_total_export", "grand_total_export",
483 "rounded_total_export", "in_words_export"], this.frm.doc.currency);
484
Rushabh Mehtaef584552013-11-02 14:47:11 +0530485 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
486 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530487
488 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Rushabh Mehtaef584552013-11-02 14:47:11 +0530489 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
490 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530491 }
492
493 // toggle fields
494 this.frm.toggle_display(["conversion_rate", "net_total", "other_charges_total",
495 "grand_total", "rounded_total", "in_words"],
496 this.frm.doc.currency != company_currency);
497
Rushabh Mehtaa063b4d2013-11-06 11:29:47 +0530498 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
Anand Doshi2168e392013-05-23 19:25:08 +0530499 this.frm.doc.price_list_currency != company_currency);
500
501 // set labels
502 $.each(field_label_map, function(fname, label) {
503 me.frm.fields_dict[fname].set_label(label);
504 });
505 },
506
507 change_grid_labels: function(company_currency) {
508 var me = this;
509 var field_label_map = {};
510
511 var setup_field_label_map = function(fields_list, currency, parentfield) {
512 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
513 $.each(fields_list, function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530514 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530515 if(docfield) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530516 var label = frappe._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530517 field_label_map[grid_doctype + "-" + fname] =
518 label.trim() + " (" + currency + ")";
519 }
520 });
521 }
522
Nabin Hait1eb56012014-02-10 19:20:15 +0530523 setup_field_label_map(["base_rate", "base_price_list_rate", "base_amount"],
Anand Doshi2168e392013-05-23 19:25:08 +0530524 company_currency, this.fname);
525
Nabin Hait1eb56012014-02-10 19:20:15 +0530526 setup_field_label_map(["rate", "price_list_rate", "amount"],
Anand Doshi2168e392013-05-23 19:25:08 +0530527 this.frm.doc.currency, this.fname);
528
529 setup_field_label_map(["tax_amount", "total"], company_currency, "other_charges");
530
531 if(this.frm.fields_dict["advance_allocation_details"]) {
532 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency,
533 "advance_allocation_details");
534 }
535
536 // toggle columns
537 var item_grid = this.frm.fields_dict[this.fname].grid;
Anand Doshiedfba962013-08-05 11:56:54 +0530538 var show = (this.frm.doc.currency != company_currency) ||
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530539 (frappe.model.get_doclist(cur_frm.doctype, cur_frm.docname,
Anand Doshiedfba962013-08-05 11:56:54 +0530540 {parentfield: "other_charges", included_in_print_rate: 1}).length);
541
Nabin Hait1eb56012014-02-10 19:20:15 +0530542 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530543 if(frappe.meta.get_docfield(item_grid.doctype, fname))
Anand Doshi2168e392013-05-23 19:25:08 +0530544 item_grid.set_column_disp(fname, show);
545 });
546
547 // set labels
548 var $wrapper = $(this.frm.wrapper);
549 $.each(field_label_map, function(fname, label) {
Anand Doshi5013dcb2013-08-05 12:16:04 +0530550 fname = fname.split("-");
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530551 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530552 if(df) df.label = label;
Anand Doshi2168e392013-05-23 19:25:08 +0530553 });
554 },
555
Anand Doshi8f9f8a42013-06-28 19:18:33 +0530556 shipping_address_name: function () {
557 var me = this;
558 if(this.frm.doc.shipping_address_name) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530559 frappe.model.with_doc("Address", this.frm.doc.shipping_address_name, function(name) {
560 var address = frappe.model.get_doc("Address", name);
Anand Doshi8f9f8a42013-06-28 19:18:33 +0530561
562 var out = $.map(["address_line1", "address_line2", "city"],
563 function(f) { return address[f]; });
564
565 var state_pincode = $.map(["state", "pincode"], function(f) { return address[f]; }).join(" ");
566 if(state_pincode) out.push(state_pincode);
567
568 if(address["country"]) out.push(address["country"]);
569
570 out.concat($.map([["Phone:", address["phone"]], ["Fax:", address["fax"]]],
571 function(val) { return val[1] ? val.join(" ") : null; }));
572
573 me.frm.set_value("shipping_address", out.join("\n"));
574 });
575 }
576 }
Anand Doshi1dde46a2013-05-15 21:15:57 +0530577});
578
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530579// Help for Sales BOM items
580var set_sales_bom_help = function(doc) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530581 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530582 if (getchildren('Packed Item', doc.name, 'packing_details').length) {
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530583 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
584
585 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Bárbara Perretti4098c262013-09-27 17:05:17 -0300586 help_msg = "<div class='alert alert-warning'>" +
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530587 frappe._("For 'Sales BOM' items, warehouse, serial no and batch no \
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530588 will be considered from the 'Packing List' table. \
589 If warehouse and batch no are same for all packing items for any 'Sales BOM' item, \
Bárbara Perretti4098c262013-09-27 17:05:17 -0300590 those values can be entered in the main item table, values will be copied to 'Packing List' table.")+
591 "</div>";
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530592 frappe.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = help_msg;
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530593 }
594 } else {
595 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
596 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530597 frappe.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = '';
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530598 }
599 }
600 refresh_field('sales_bom_help');
601}