blob: ac0287d05a564f33016cedccbb066ed6c9468b50 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Anand Doshi3543f302013-05-24 19:25:01 +05303
Nabin Haitb4a3dfa2015-01-22 17:13:13 +05304erpnext.TransactionController = erpnext.taxes_and_totals.extend({
Rushabh Mehtabe2ee182016-04-29 17:22:42 +05305 setup: function() {
6 frappe.ui.form.on(this.frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
7 var item = frappe.get_doc(cdt, cdn);
8 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
9
10 if(item.price_list_rate) {
11 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item));
12 } else {
13 item.discount_percentage = 0.0;
14 }
15
16 cur_frm.cscript.set_gross_profit(item);
17 cur_frm.cscript.calculate_taxes_and_totals();
18 })
19
20 frappe.ui.form.on(this.frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
21 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053022 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053023
24 frappe.ui.form.on(this.frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
25 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053026 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053027
28 frappe.ui.form.on(this.frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
29 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053030 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053031
32 frappe.ui.form.on(this.frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
33 cur_frm.cscript.set_dynamic_labels();
34 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053035 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053036
37 frappe.ui.form.on(this.frm.doctype, "apply_discount_on", function(frm) {
38 if(frm.doc.additional_discount_percentage) {
39 frm.trigger("additional_discount_percentage");
40 } else {
41 cur_frm.cscript.calculate_taxes_and_totals();
42 }
Rushabh Mehta764aa922016-05-02 13:28:46 +053043 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053044
45 frappe.ui.form.on(this.frm.doctype, "additional_discount_percentage", function(frm) {
46 if (frm.via_discount_amount) {
47 return;
48 }
49
50 if(!frm.doc.apply_discount_on) {
51 frappe.msgprint(__("Please set 'Apply Additional Discount On'"));
52 return
53 }
54
55 frm.via_discount_percentage = true;
56
57 if(frm.doc.additional_discount_percentage && frm.doc.discount_amount) {
58 // Reset discount amount and net / grand total
59 frm.set_value("discount_amount", 0);
60 }
61
62 var total = flt(frm.doc[frappe.model.scrub(frm.doc.apply_discount_on)]);
63 var discount_amount = flt(total*flt(frm.doc.additional_discount_percentage) / 100,
64 precision("discount_amount"));
65
66 frm.set_value("discount_amount", discount_amount);
67 delete frm.via_discount_percentage;
68 });
69
70 frappe.ui.form.on(this.frm.doctype, "discount_amount", function(frm) {
71 frm.cscript.set_dynamic_labels();
72
73 if (!frm.via_discount_percentage) {
74 frm.via_discount_amount = true;
75 frm.set_value("additional_discount_percentage", 0);
76 delete frm.via_discount_amount;
77 }
78
79 frm.cscript.calculate_taxes_and_totals();
80 });
81
82 },
Anand Doshi3543f302013-05-24 19:25:01 +053083 onload: function() {
Anand Doshi1e4fb682013-08-23 12:56:33 +053084 var me = this;
Rushabh Mehta8c9c57c2016-04-13 18:22:06 +053085 //this.frm.show_print_first = true;
Anand Doshi3543f302013-05-24 19:25:01 +053086 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053087 var today = get_today(),
Nabin Hait73083dc2014-03-14 15:10:42 +053088 currency = frappe.defaults.get_user_default("currency");
Rushabh Mehtaff938022014-04-15 18:40:00 +053089
Anand Doshi3543f302013-05-24 19:25:01 +053090 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053091 currency: currency,
92 price_list_currency: currency,
93 status: "Draft",
Anand Doshifc777182013-05-27 19:29:07 +053094 is_subcontracted: "No",
Anand Doshi3543f302013-05-24 19:25:01 +053095 }, function(fieldname, value) {
96 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
97 me.frm.set_value(fieldname, value);
Rushabh Mehtaff938022014-04-15 18:40:00 +053098 });
nabinhaitbc408d82014-07-07 16:51:52 +053099
nabinhaitb7392642014-07-28 14:55:08 +0530100 if(this.frm.doc.company && !this.frm.doc.amended_from) {
Rushabh Mehta78b7a532016-04-06 15:29:38 +0530101 this.frm.script_manager.trigger("company");
nabinhaitbc408d82014-07-07 16:51:52 +0530102 }
Anand Doshi3543f302013-05-24 19:25:01 +0530103 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530104
Nabin Haitdd38a262014-12-26 13:15:21 +0530105 if(this.frm.fields_dict["taxes"]) {
106 this["taxes_remove"] = this.calculate_taxes_and_totals;
Anand Doshia91c95f2013-08-23 13:00:47 +0530107 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530108
Nabin Haitdd38a262014-12-26 13:15:21 +0530109 if(this.frm.fields_dict["items"]) {
110 this["items_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +0530111 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530112
Neil Trini Lasradoe2d8dd02015-06-22 19:06:15 +0530113 if(this.frm.fields_dict["recurring_print_format"]) {
114 this.frm.set_query("recurring_print_format", function(doc) {
115 return{
116 filters: [
117 ['Print Format', 'doc_type', '=', cur_frm.doctype],
118 ]
119 }
120 });
121 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530122
Nabin Hait1d218422015-07-17 15:19:02 +0530123 if(this.frm.fields_dict["return_against"]) {
124 this.frm.set_query("return_against", function(doc) {
125 var filters = {
126 "docstatus": 1,
127 "is_return": 0,
128 "company": doc.company
129 };
130 if (me.frm.fields_dict["customer"] && doc.customer) filters["customer"] = doc.customer;
131 if (me.frm.fields_dict["supplier"] && doc.supplier) filters["supplier"] = doc.supplier;
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530132
Nabin Hait1d218422015-07-17 15:19:02 +0530133 return {
134 filters: filters
135 }
136 });
137 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530138
Anand Doshi3543f302013-05-24 19:25:01 +0530139 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530140
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530141 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +0530142 var me = this;
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530143 if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length
144 && !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) {
145 this.apply_default_taxes();
Anand Doshi70f57eb2015-11-27 14:37:40 +0530146 } else if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"]
Nabin Haitbdfb0702015-10-28 15:38:08 +0530147 && !this.frm.doc.is_pos) {
148 me.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530149 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530150 if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) {
Rushabh Mehta203cc962016-04-07 15:25:43 +0530151 this.setup_item_selector();
Rushabh Mehtab6843f22014-06-04 13:10:41 +0530152 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530153 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530154
Anand Doshi3543f302013-05-24 19:25:01 +0530155 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +0530156 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +0530157 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +0530158 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +0530159 this.set_dynamic_labels();
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530160 this.setup_sms();
Anand Doshife13bfe2015-08-25 12:49:40 +0530161 this.make_show_payments_btn();
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530162 },
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530163
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530164 apply_default_taxes: function() {
165 var me = this;
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530166 var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
167 me.frm.doc.name);
168
169 if(taxes_and_charges_field) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530170 return frappe.call({
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530171 method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges",
172 args: {
173 "master_doctype": taxes_and_charges_field.options
174 },
175 callback: function(r) {
176 if(!r.exc) {
177 me.frm.set_value("taxes", r.message);
Nabin Haitbdfb0702015-10-28 15:38:08 +0530178 me.calculate_taxes_and_totals();
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530179 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530180 }
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530181 });
182 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530183 },
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530184
185 setup_sms: function() {
186 var me = this;
Nabin Hait7132bd32016-04-13 15:19:37 +0530187 if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped", "Closed"], this.frm.doc.status)
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530188 && this.frm.doctype != "Purchase Invoice") {
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530189 this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); });
190 }
191 },
192
193 send_sms: function() {
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530194 var sms_man = new SMSManager(this.frm.doc);
Nabin Hait239c2c32015-01-15 14:21:41 +0530195 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530196
Anand Doshife13bfe2015-08-25 12:49:40 +0530197 make_show_payments_btn: function() {
198 var me = this;
199 if (in_list(["Purchase Invoice", "Sales Invoice"], this.frm.doctype)) {
200 if(this.frm.doc.outstanding_amount !== this.frm.doc.base_grand_total) {
Saurabhe23e99f2016-01-06 15:34:56 +0530201 this.frm.add_custom_button(__("Payments"), function() {
Anand Doshife13bfe2015-08-25 12:49:40 +0530202 frappe.route_options = {
203 "Journal Entry Account.reference_type": me.frm.doc.doctype,
204 "Journal Entry Account.reference_name": me.frm.doc.name
205 };
206
207 frappe.set_route("List", "Journal Entry");
Saurabhe23e99f2016-01-06 15:34:56 +0530208 }, __("View"));
Anand Doshife13bfe2015-08-25 12:49:40 +0530209 }
Nabin Hait239c2c32015-01-15 14:21:41 +0530210 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530211 },
212
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530213 barcode: function(doc, cdt, cdn) {
214 var d = locals[cdt][cdn];
215 if(d.barcode=="" || d.barcode==null) {
216 // barcode cleared, remove item
217 d.item_code = "";
218 }
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530219 this.item_code(doc, cdt, cdn, true);
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530220 },
221
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530222 item_code: function(doc, cdt, cdn, from_barcode) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530223 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530224 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530225
226 // clear barcode if setting item (else barcode will take priority)
227 if(!from_barcode) {
228 item.barcode = null;
229 }
Nabin Hait139dc7b2014-02-12 14:53:18 +0530230 if(item.item_code || item.barcode || item.serial_no) {
231 if(!this.validate_company_and_party()) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530232 cur_frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +0530233 } else {
234 return this.frm.call({
235 method: "erpnext.stock.get_item_details.get_item_details",
236 child: item,
237 args: {
238 args: {
239 item_code: item.item_code,
240 barcode: item.barcode,
241 serial_no: item.serial_no,
242 warehouse: item.warehouse,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530243 customer: me.frm.doc.customer,
244 supplier: me.frm.doc.supplier,
245 currency: me.frm.doc.currency,
246 conversion_rate: me.frm.doc.conversion_rate,
247 price_list: me.frm.doc.selling_price_list ||
248 me.frm.doc.buying_price_list,
249 price_list_currency: me.frm.doc.price_list_currency,
250 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
251 company: me.frm.doc.company,
252 order_type: me.frm.doc.order_type,
253 is_pos: cint(me.frm.doc.is_pos),
254 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Haita1bf43b2015-03-17 10:50:47 +0530255 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
Nabin Hait444f9562014-06-20 15:59:49 +0530256 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
Anand Doshiafe85bd2016-01-21 21:04:16 +0530257 doctype: me.frm.doc.doctype,
258 name: me.frm.doc.name,
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530259 project: item.project || me.frm.doc.project,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530260 qty: item.qty
Nabin Hait139dc7b2014-02-12 14:53:18 +0530261 }
262 },
Anand Doshi13945092014-09-21 19:45:49 +0530263
Nabin Hait139dc7b2014-02-12 14:53:18 +0530264 callback: function(r) {
265 if(!r.exc) {
266 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
267 }
268 }
269 });
270 }
271 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530272 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530273
274 serial_no: function(doc, cdt, cdn) {
275 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530276 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530277
278 if (item.serial_no) {
279 if (!item.item_code) {
280 this.frm.script_manager.trigger("item_code", cdt, cdn);
281 }
282 else {
283 var sr_no = [];
284
285 // Replacing all occurences of comma with carriage return
286 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
287
288 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530289
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530290 // Trim each string and push unique string to new list
291 for (var x=0; x<=serial_nos.length - 1; x++) {
292 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
293 sr_no.push(serial_nos[x].trim());
294 }
295 }
296
297 // Add the new list to the serial no. field in grid with each in new line
298 item.serial_no = "";
299 for (var x=0; x<=sr_no.length - 1; x++)
300 item.serial_no += sr_no[x] + '\n';
301
302 refresh_field("serial_no", item.name, item.parentfield);
Nabin Haitb7c0c552015-08-04 12:18:12 +0530303 if(!doc.is_return) {
304 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
305 }
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530306 }
307 }
308 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530309
Anand Doshi923d41d2013-05-28 17:23:36 +0530310 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530311 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530312 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530313
Anand Doshi3543f302013-05-24 19:25:01 +0530314 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530315 var me = this;
Nabin Haita731ad42016-02-04 17:21:28 +0530316 var set_pricing = function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530317 if(me.frm.doc.company && me.frm.fields_dict.currency) {
318 var company_currency = me.get_company_currency();
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530319 var company_doc = frappe.get_doc(":Company", me.frm.doc.company);
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530320 if (!me.frm.doc.currency) {
321 me.frm.set_value("currency", company_currency);
322 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530323
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530324 if (me.frm.doc.currency == company_currency) {
325 me.frm.set_value("conversion_rate", 1.0);
326 }
327 if (me.frm.doc.price_list_currency == company_currency) {
328 me.frm.set_value('plc_conversion_rate', 1.0);
329 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530330 if (company_doc.default_letter_head) {
Rushabh Mehtac1857342015-05-27 12:29:57 +0530331 if(me.frm.fields_dict.letter_head) {
332 me.frm.set_value("letter_head", company_doc.default_letter_head);
333 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530334 }
Rushabh Mehtac1857342015-05-27 12:29:57 +0530335 if (company_doc.default_terms && me.frm.doc.doctype != "Purchase Invoice") {
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530336 me.frm.set_value("tc_name", company_doc.default_terms);
337 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530338
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530339 me.frm.script_manager.trigger("currency");
340 me.apply_pricing_rule();
Nabin Haitdd38a262014-12-26 13:15:21 +0530341 }
Anand Doshi3543f302013-05-24 19:25:01 +0530342 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530343
Nabin Haita731ad42016-02-04 17:21:28 +0530344 var set_party_account = function(set_pricing) {
345 if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) {
346 if(me.frm.doc.doctype=="Sales Invoice") {
347 var party_type = "Customer";
348 var party_account_field = 'debit_to';
349 } else {
350 var party_type = "Supplier";
351 var party_account_field = 'credit_to';
352 }
Anand Doshi39a28912016-02-15 11:42:18 +0530353
Nabin Haitef864402016-02-10 14:47:25 +0530354 var party = me.frm.doc[frappe.model.scrub(party_type)];
355 if(party) {
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530356 return frappe.call({
357 method: "erpnext.accounts.party.get_party_account",
358 args: {
359 company: me.frm.doc.company,
360 party_type: party_type,
Nabin Haitef864402016-02-10 14:47:25 +0530361 party: party
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530362 },
363 callback: function(r) {
364 if(!r.exc && r.message) {
365 me.frm.set_value(party_account_field, r.message);
366 set_pricing();
367 }
Nabin Haita731ad42016-02-04 17:21:28 +0530368 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530369 });
Nabin Haitef864402016-02-10 14:47:25 +0530370 } else {
371 set_pricing();
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530372 }
Nabin Haita731ad42016-02-04 17:21:28 +0530373 } else {
374 set_pricing();
375 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530376
Nabin Haita731ad42016-02-04 17:21:28 +0530377 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530378
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530379 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
380 else var date = this.frm.doc.transaction_date;
Rushabh Mehta5dfe20c2016-04-26 12:59:08 +0530381
382 if (frappe.meta.get_docfield(this.frm.doctype, "shipping_address") &&
Rohit Waghchaureb2e7a1f2016-04-11 12:10:20 +0530383 in_list(['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'], this.frm.doctype)){
384 erpnext.utils.get_shipping_address(this.frm, function(){
385 set_party_account(set_pricing);
386 })
387 }else{
388 set_party_account(set_pricing);
389 }
Rushabh Mehta45ca8a42015-04-28 16:02:09 +0530390
391 if(this.frm.doc.company) {
392 erpnext.last_selected_company = this.frm.doc.company;
393 }
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530394 },
395
396 transaction_date: function() {
Anand Doshi87da6622015-10-19 14:59:32 +0530397 if (this.frm.doc.transaction_date) {
398 this.frm.transaction_date = this.frm.doc.transaction_date;
399 }
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530400 },
401
402 posting_date: function() {
Nabin Hait40e92b62015-07-09 17:12:23 +0530403 var me = this;
404 if (this.frm.doc.posting_date) {
Anand Doshi87da6622015-10-19 14:59:32 +0530405 this.frm.posting_date = this.frm.doc.posting_date;
406
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530407 if ((this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer) ||
Nabin Hait40e92b62015-07-09 17:12:23 +0530408 (this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier)) {
409 return frappe.call({
410 method: "erpnext.accounts.party.get_due_date",
411 args: {
412 "posting_date": me.frm.doc.posting_date,
413 "party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier",
414 "party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier,
415 "company": me.frm.doc.company
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530416 },
Nabin Hait40e92b62015-07-09 17:12:23 +0530417 callback: function(r, rt) {
418 if(r.message) {
419 me.frm.set_value("due_date", r.message);
420 }
Nabin Hait40e92b62015-07-09 17:12:23 +0530421 }
422 })
Nabin Hait40e92b62015-07-09 17:12:23 +0530423 }
424 }
Anand Doshi3543f302013-05-24 19:25:01 +0530425 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530426
Anand Doshi3543f302013-05-24 19:25:01 +0530427 get_company_currency: function() {
428 return erpnext.get_currency(this.frm.doc.company);
429 },
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530430
Neil Trini Lasrado09a66c42015-07-07 16:41:37 +0530431 contact_person: function() {
432 erpnext.utils.get_contact_details(this.frm);
433 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530434
Anand Doshi3543f302013-05-24 19:25:01 +0530435 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530436 var me = this;
437 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530438
Anand Doshi61a2f682013-06-21 17:55:31 +0530439 var company_currency = this.get_company_currency();
Nabin Hait69cdf592015-05-02 18:46:10 +0530440 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
Pratik Vyas1a84d8e2015-05-19 12:23:52 +0530441 if(this.frm.doc.currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530442 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530443 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530444 me.frm.set_value("conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530445 });
446 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530447 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530448 }
449 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530450
Anand Doshi61a2f682013-06-21 17:55:31 +0530451 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530452 if(this.frm.doc.currency === this.get_company_currency()) {
453 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530454 }
455 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530456 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
457 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
458 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530459
Nabin Haita3dd72a2014-05-28 12:49:20 +0530460 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530461 if(this.frm.doc.ignore_pricing_rule) {
462 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530463 } else if (!this.in_apply_price_list){
464 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530465 }
466
Nabin Haita3dd72a2014-05-28 12:49:20 +0530467 }
Anand Doshi3543f302013-05-24 19:25:01 +0530468 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530469
Anand Doshi61a2f682013-06-21 17:55:31 +0530470 get_exchange_rate: function(from_currency, to_currency, callback) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530471 return frappe.call({
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530472 method: "erpnext.setup.utils.get_exchange_rate",
473 args: {
474 from_currency: from_currency,
475 to_currency: to_currency
476 },
477 callback: function(r) {
478 callback(flt(r.message));
479 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530480 });
481 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530482
Anand Doshi3543f302013-05-24 19:25:01 +0530483 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530484 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530485 this.set_dynamic_labels();
Nabin Haitec361ea2015-05-11 18:14:45 +0530486
487 var company_currency = this.get_company_currency();
488 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
489 if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
490 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
491 function(exchange_rate) {
492 me.frm.set_value("plc_conversion_rate", exchange_rate);
493 });
494 } else {
495 this.plc_conversion_rate();
496 }
Anand Doshi3543f302013-05-24 19:25:01 +0530497 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530498
Anand Doshi3543f302013-05-24 19:25:01 +0530499 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530500 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
501 this.frm.set_value("plc_conversion_rate", 1.0);
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530502 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency
Nabin Hait1d218422015-07-17 15:19:02 +0530503 && this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 &&
Nabin Haitec361ea2015-05-11 18:14:45 +0530504 cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
505 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530506 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530507
Nabin Haitec361ea2015-05-11 18:14:45 +0530508 if(!this.in_apply_price_list) {
509 this.apply_price_list();
Anand Doshi61a2f682013-06-21 17:55:31 +0530510 }
Anand Doshi3543f302013-05-24 19:25:01 +0530511 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530512
Anand Doshi3543f302013-05-24 19:25:01 +0530513 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530514 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530515 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530516
Anand Doshi61a2f682013-06-21 17:55:31 +0530517 set_dynamic_labels: function() {
518 // What TODO? should we make price list system non-mandatory?
519 this.frm.toggle_reqd("plc_conversion_rate",
520 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530521
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530522 if(this.frm.doc_currency!==this.frm.doc.currency) {
523 // reset names only when the currency is different
524
525 var company_currency = this.get_company_currency();
526 this.change_form_labels(company_currency);
527 this.change_grid_labels(company_currency);
528 this.frm.refresh_fields();
529 this.frm.doc_currency = this.frm.doc.currency;
530 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530531 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530532
Nabin Hait613d0812015-02-23 11:58:15 +0530533 change_form_labels: function(company_currency) {
534 var me = this;
535 var field_label_map = {};
536
537 var setup_field_label_map = function(fields_list, currency) {
538 $.each(fields_list, function(i, fname) {
539 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
540 if(docfield) {
541 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
542 field_label_map[fname] = label.trim() + " (" + currency + ")";
543 }
544 });
545 };
546 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
547 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
548 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530549 "base_paid_amount", "base_write_off_amount", "base_change_amount"
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530550 ], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530551
552 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
553 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530554 "rounded_total", "in_words", "paid_amount", "write_off_amount"], this.frm.doc.currency);
Anand Doshi87da6622015-10-19 14:59:32 +0530555
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530556 setup_field_label_map(["outstanding_amount", "total_advance"], this.frm.doc.party_account_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530557
558 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
559 + " = [?] " + company_currency)
560
561 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
562 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
563 + " = [?] " + company_currency)
564 }
565
566 // toggle fields
567 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
568 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530569 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount",
570 "base_paid_amount", "base_write_off_amount"],
Nabin Hait613d0812015-02-23 11:58:15 +0530571 this.frm.doc.currency != company_currency);
572
573 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
574 this.frm.doc.price_list_currency != company_currency);
575
576 // set labels
577 $.each(field_label_map, function(fname, label) {
578 me.frm.fields_dict[fname].set_label(label);
579 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530580
Nabin Hait903c42a2015-02-24 15:24:49 +0530581 var show =cint(cur_frm.doc.discount_amount) ||
582 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
583
584 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
585 cur_frm.toggle_display("net_total", show);
586
587 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
588 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
589
Nabin Hait613d0812015-02-23 11:58:15 +0530590 },
591
592 change_grid_labels: function(company_currency) {
593 var me = this;
594 var field_label_map = {};
595
596 var setup_field_label_map = function(fields_list, currency, parentfield) {
597 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
598 $.each(fields_list, function(i, fname) {
599 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
600 if(docfield) {
601 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
602 field_label_map[grid_doctype + "-" + fname] =
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530603 label.trim() + " (" + __(currency) + ")";
Nabin Hait613d0812015-02-23 11:58:15 +0530604 }
605 });
606 }
607
608 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
609 company_currency, "items");
610
611 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
612 this.frm.doc.currency, "items");
613
614 if(this.frm.fields_dict["taxes"]) {
615 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
616
617 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
618 }
619
620 if(this.frm.fields_dict["advances"]) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530621 setup_field_label_map(["advance_amount", "allocated_amount"],
Nabin Haitaa015902015-10-16 13:08:33 +0530622 this.frm.doc.party_account_currency, "advances");
Nabin Hait613d0812015-02-23 11:58:15 +0530623 }
624
625 // toggle columns
626 var item_grid = this.frm.fields_dict["items"].grid;
627 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
628 if(frappe.meta.get_docfield(item_grid.doctype, fname))
629 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
630 });
631
632 var show = (cint(cur_frm.doc.discount_amount)) ||
633 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
634
635 $.each(["net_rate", "net_amount"], function(i, fname) {
636 if(frappe.meta.get_docfield(item_grid.doctype, fname))
637 item_grid.set_column_disp(fname, show);
638 });
639
640 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
641 if(frappe.meta.get_docfield(item_grid.doctype, fname))
642 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
643 });
644
645 // set labels
646 var $wrapper = $(this.frm.wrapper);
647 $.each(field_label_map, function(fname, label) {
648 fname = fname.split("-");
649 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
650 if(df) df.label = label;
651 });
652 },
653
Anand Doshi923d41d2013-05-28 17:23:36 +0530654 recalculate: function() {
655 this.calculate_taxes_and_totals();
656 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530657
Anand Doshi923d41d2013-05-28 17:23:36 +0530658 recalculate_values: function() {
659 this.calculate_taxes_and_totals();
660 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530661
Anand Doshid0b00722013-05-28 18:54:48 +0530662 calculate_charges: function() {
663 this.calculate_taxes_and_totals();
664 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530665
Nabin Hait444f9562014-06-20 15:59:49 +0530666 ignore_pricing_rule: function() {
667 this.apply_pricing_rule();
668 },
669
670 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530671 var me = this;
Anand Doshi70f57eb2015-11-27 14:37:40 +0530672 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530673 if (!(args.items && args.items.length)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530674 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
675 return;
676 }
677
Anand Doshidffec8f2014-07-01 17:45:15 +0530678 return this.frm.call({
679 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
Anand Doshi70f57eb2015-11-27 14:37:40 +0530680 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530681 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530682 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530683 me._set_values_for_item_list(r.message);
Saurabh2d7af632016-02-26 11:09:20 +0530684 if(item) me.set_gross_profit(item);
Anand Doshidffec8f2014-07-01 17:45:15 +0530685 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
686 }
687 }
688 });
689 },
690
691 _get_args: function(item) {
692 var me = this;
693 return {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530694 "items": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530695 "customer": me.frm.doc.customer,
696 "customer_group": me.frm.doc.customer_group,
697 "territory": me.frm.doc.territory,
698 "supplier": me.frm.doc.supplier,
699 "supplier_type": me.frm.doc.supplier_type,
700 "currency": me.frm.doc.currency,
701 "conversion_rate": me.frm.doc.conversion_rate,
702 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Haitdd82bf22015-05-11 16:49:17 +0530703 "price_list_currency": me.frm.doc.price_list_currency,
Nabin Hait444f9562014-06-20 15:59:49 +0530704 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
705 "company": me.frm.doc.company,
706 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
707 "campaign": me.frm.doc.campaign,
708 "sales_partner": me.frm.doc.sales_partner,
709 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530710 "doctype": me.frm.doc.doctype,
711 "name": me.frm.doc.name
Nabin Hait444f9562014-06-20 15:59:49 +0530712 };
Nabin Hait444f9562014-06-20 15:59:49 +0530713 },
714
715 _get_item_list: function(item) {
716 var item_list = [];
717 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530718 if (d.item_code) {
719 item_list.push({
720 "doctype": d.doctype,
721 "name": d.name,
722 "item_code": d.item_code,
723 "item_group": d.item_group,
724 "brand": d.brand,
Anand Doshi39a28912016-02-15 11:42:18 +0530725 "qty": d.qty,
726 "parenttype": d.parenttype,
727 "parent": d.parent
Anand Doshi39edcbc2014-07-28 16:00:19 +0530728 });
mbauskarc79415e2016-01-18 16:32:24 +0530729
730 // if doctype is Quotation Item / Sales Order Iten then add Margin Type and rate in item_list
mbauskara52472c2016-03-05 15:10:25 +0530731 if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype){
732 item_list[0]["margin_type"] = d.margin_type
733 item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530734 }
Anand Doshi39edcbc2014-07-28 16:00:19 +0530735 }
Nabin Hait444f9562014-06-20 15:59:49 +0530736 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530737
Nabin Hait444f9562014-06-20 15:59:49 +0530738 if (item) {
739 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530740 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530741 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530742 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530743 });
744 }
Nabin Hait444f9562014-06-20 15:59:49 +0530745 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530746 },
747
Anand Doshidffec8f2014-07-01 17:45:15 +0530748 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530749 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530750 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530751 for(var i=0, l=children.length; i<l; i++) {
752 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530753 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530754
755 for(var k in d) {
756 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530757 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530758 if(k=="price_list_rate") {
759 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
760 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530761 frappe.model.set_value(d.doctype, d.name, k, v);
762 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530763 }
764
Nabin Hait49a27292014-12-17 11:21:32 +0530765 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530766 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530767 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
768 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530769 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530770
Anand Doshidea3fb02015-02-05 15:05:58 +0530771 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530772 },
773
Nabin Hait49a27292014-12-17 11:21:32 +0530774 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530775 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530776 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530777 if (!((args.items && args.items.length) || args.price_list)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530778 return;
779 }
Nabin Hait37b047d2015-02-23 16:01:33 +0530780
Anand Doshidffec8f2014-07-01 17:45:15 +0530781 return this.frm.call({
782 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530783 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530784 callback: function(r) {
785 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530786 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530787 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
788 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530789 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530790
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530791 if(args.items.length) {
Nabin Hait37b047d2015-02-23 16:01:33 +0530792 me._set_values_for_item_list(r.message.children);
793 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530794 }
795 }
796 });
797 },
798
Anand Doshi3543f302013-05-24 19:25:01 +0530799 get_item_wise_taxes_html: function() {
800 var item_tax = {};
801 var tax_accounts = [];
802 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530803
Nabin Hait96339342015-01-21 17:22:11 +0530804 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530805 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530806 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530807 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530808 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530809 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530810 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530811 var tax_rate = "";
812 if(tax_data[0] != null) {
813 tax_rate = (tax.charge_type === "Actual") ?
814 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
815 (flt(tax_data[0], tax_rate_precision) + "%");
816 }
817 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
818 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530819
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530820 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530821 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530822 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530823 }
Anand Doshi3543f302013-05-24 19:25:01 +0530824 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530825 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530826 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530827
828 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530829 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530830
Anand Doshi179e3772013-09-05 16:53:57 +0530831 var distinct_item_names = [];
832 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530833 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530834 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
835 distinct_item_names.push(item.item_code || item.item_name);
836 distinct_items.push(item);
837 }
838 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530839
Anand Doshi179e3772013-09-05 16:53:57 +0530840 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530841 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530842 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530843 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
844 item_name: item.item_name,
845 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530846 return item_tax_record[head[0]] ?
847 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530848 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530849 }).join("\n")
850 });
851 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530852
Anand Doshi53b73422013-05-31 11:50:01 +0530853 if(!rows) return "";
Rushabh Mehtaf24f6042015-08-20 18:01:40 +0530854 return '<p><a class="h6 text-muted" href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">'
855 + __("Show tax break-up") + '</a><br><br></p>\
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530856 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530857 <thead><tr>' + headings + '</tr></thead> \
858 <tbody>' + rows + '</tbody> \
859 </table></div>';
860 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530861
Nabin Hait139dc7b2014-02-12 14:53:18 +0530862 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530863 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530864 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530865
Nabin Hait0f231062014-02-20 11:27:47 +0530866 $.each(["company", "customer"], function(i, fieldname) {
Saurabh8a8ef852015-10-21 19:28:53 +0530867 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname && me.frm.doc.doctype != "Purchase Order")) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530868 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530869 msgprint(__("Please specify") + ": " +
870 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530871 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530872 valid = false;
873 }
Anand Doshi3543f302013-05-24 19:25:01 +0530874 }
875 });
876 return valid;
877 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530878
Anand Doshibf3e54a2013-06-05 14:11:32 +0530879 get_terms: function() {
880 var me = this;
881 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530882 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530883 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530884 args: {
885 doctype: "Terms and Conditions",
886 fieldname: "terms",
887 filters: { name: this.frm.doc.tc_name },
888 },
889 });
890 }
891 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530892
893 taxes_and_charges: function() {
894 var me = this;
895 if(this.frm.doc.taxes_and_charges) {
896 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530897 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
898 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530899 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
900 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530901 "master_name": this.frm.doc.taxes_and_charges,
Nabin Hait6b039142014-05-02 15:45:10 +0530902 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530903 callback: function(r) {
904 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530905 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530906 me.calculate_taxes_and_totals();
907 }
908 }
909 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530910 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530911 },
912
913 show_item_wise_taxes: function() {
914 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530915 var html = this.get_item_wise_taxes_html();
916 if (html) {
917 this.frm.toggle_display("other_charges_calculation", true);
918 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
919 } else {
920 this.frm.toggle_display("other_charges_calculation", false);
921 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530922 }
923 },
Anand Doshi13945092014-09-21 19:45:49 +0530924
925 is_recurring: function() {
926 // set default values for recurring documents
Rushabh Mehta756e5182016-03-03 13:00:17 +0530927 if(this.frm.doc.is_recurring && this.frm.doc.__islocal) {
928 frappe.msgprint(__("Please set recurring after saving"));
929 this.frm.set_value('is_recurring', 0);
930 return;
931 }
932
Anand Doshi13945092014-09-21 19:45:49 +0530933 if(this.frm.doc.is_recurring) {
Nabin Hait68a628e2016-03-21 12:03:16 +0530934 if(!this.frm.doc.recurring_id) {
935 this.frm.set_value('recurring_id', this.frm.doc.name);
936 }
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530937
Anand Doshi13945092014-09-21 19:45:49 +0530938 var owner_email = this.frm.doc.owner=="Administrator"
939 ? frappe.user_info("Administrator").email
940 : this.frm.doc.owner;
941
942 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
943 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
944 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
945 }
946
947 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
948 },
949
950 from_date: function() {
951 // set to_date
952 if(this.frm.doc.from_date) {
953 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
954 'Yearly': 12};
955
956 var months = recurring_type_map[this.frm.doc.recurring_type];
957 if(months) {
958 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
959 months);
960 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
961 refresh_field('to_date');
962 }
963 }
Saurabhd3135532016-02-25 18:59:20 +0530964 },
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530965
Saurabh2d7af632016-02-26 11:09:20 +0530966 set_gross_profit: function(item) {
Saurabh5ada14b2016-02-26 18:02:55 +0530967 if (this.frm.doc.doctype == "Sales Order" && item.valuation_rate) {
Saurabh3fbf3ce2016-03-09 15:31:04 +0530968 rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1);
969 item.gross_profit = flt(((rate - item.valuation_rate) * item.qty), precision("amount", item));
Saurabh2d7af632016-02-26 11:09:20 +0530970 }
Rushabh Mehta203cc962016-04-07 15:25:43 +0530971 },
972
973 setup_item_selector: function() {
Rushabh Mehtabc928242016-05-29 23:08:22 +0530974 // TODO: remove item selector
975
976 return;
Rushabh Mehta203cc962016-04-07 15:25:43 +0530977 if(!this.item_selector) {
978 this.item_selector = new erpnext.ItemSelector({frm: this.frm});
979 }
Anand Doshi13945092014-09-21 19:45:49 +0530980 }
Rushabh Mehta203cc962016-04-07 15:25:43 +0530981
982
983
Rushabh Mehtabe2ee182016-04-29 17:22:42 +0530984});