blob: ad6901c8c399dc603d1612ebae1a4d93297827bf [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() {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +05306 this._super();
Nabin Hait28a05282016-06-27 17:41:39 +05307
8 if(in_list(["Sales Invoice", "Purchase Invoice"], this.frm.doc.doctype)) {
9 this.frm.get_field('advances').grid.editable_fields = [
10 {fieldname: 'reference_name', columns: 2},
11 {fieldname: 'remarks', columns: 3},
12 {fieldname: 'advance_amount', columns: 3},
13 {fieldname: 'allocated_amount', columns: 3}
14 ];
15 }
16
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053017 frappe.ui.form.on(this.frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
18 var item = frappe.get_doc(cdt, cdn);
19 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
20
21 if(item.price_list_rate) {
22 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item));
23 } else {
24 item.discount_percentage = 0.0;
25 }
26
27 cur_frm.cscript.set_gross_profit(item);
28 cur_frm.cscript.calculate_taxes_and_totals();
29 })
30
31 frappe.ui.form.on(this.frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
32 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053033 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053034
35 frappe.ui.form.on(this.frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
36 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053037 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053038
39 frappe.ui.form.on(this.frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
40 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053041 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053042
43 frappe.ui.form.on(this.frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
44 cur_frm.cscript.set_dynamic_labels();
45 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053046 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053047
48 frappe.ui.form.on(this.frm.doctype, "apply_discount_on", function(frm) {
49 if(frm.doc.additional_discount_percentage) {
50 frm.trigger("additional_discount_percentage");
51 } else {
52 cur_frm.cscript.calculate_taxes_and_totals();
53 }
Rushabh Mehta764aa922016-05-02 13:28:46 +053054 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053055
56 frappe.ui.form.on(this.frm.doctype, "additional_discount_percentage", function(frm) {
57 if (frm.via_discount_amount) {
58 return;
59 }
60
61 if(!frm.doc.apply_discount_on) {
62 frappe.msgprint(__("Please set 'Apply Additional Discount On'"));
63 return
64 }
65
66 frm.via_discount_percentage = true;
67
68 if(frm.doc.additional_discount_percentage && frm.doc.discount_amount) {
69 // Reset discount amount and net / grand total
70 frm.set_value("discount_amount", 0);
71 }
72
73 var total = flt(frm.doc[frappe.model.scrub(frm.doc.apply_discount_on)]);
74 var discount_amount = flt(total*flt(frm.doc.additional_discount_percentage) / 100,
75 precision("discount_amount"));
76
77 frm.set_value("discount_amount", discount_amount);
78 delete frm.via_discount_percentage;
79 });
80
81 frappe.ui.form.on(this.frm.doctype, "discount_amount", function(frm) {
82 frm.cscript.set_dynamic_labels();
83
84 if (!frm.via_discount_percentage) {
85 frm.via_discount_amount = true;
86 frm.set_value("additional_discount_percentage", 0);
87 delete frm.via_discount_amount;
88 }
89
90 frm.cscript.calculate_taxes_and_totals();
91 });
92
93 },
Anand Doshi3543f302013-05-24 19:25:01 +053094 onload: function() {
Anand Doshi1e4fb682013-08-23 12:56:33 +053095 var me = this;
Rushabh Mehta8c9c57c2016-04-13 18:22:06 +053096 //this.frm.show_print_first = true;
Anand Doshi3543f302013-05-24 19:25:01 +053097 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053098 var today = get_today(),
Nabin Hait73083dc2014-03-14 15:10:42 +053099 currency = frappe.defaults.get_user_default("currency");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530100
Anand Doshi3543f302013-05-24 19:25:01 +0530101 $.each({
Anand Doshifc777182013-05-27 19:29:07 +0530102 currency: currency,
103 price_list_currency: currency,
104 status: "Draft",
Anand Doshifc777182013-05-27 19:29:07 +0530105 is_subcontracted: "No",
Anand Doshi3543f302013-05-24 19:25:01 +0530106 }, function(fieldname, value) {
107 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
108 me.frm.set_value(fieldname, value);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530109 });
nabinhaitbc408d82014-07-07 16:51:52 +0530110
nabinhaitb7392642014-07-28 14:55:08 +0530111 if(this.frm.doc.company && !this.frm.doc.amended_from) {
Rushabh Mehta78b7a532016-04-06 15:29:38 +0530112 this.frm.script_manager.trigger("company");
nabinhaitbc408d82014-07-07 16:51:52 +0530113 }
Anand Doshi3543f302013-05-24 19:25:01 +0530114 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530115
Nabin Haitdd38a262014-12-26 13:15:21 +0530116 if(this.frm.fields_dict["taxes"]) {
117 this["taxes_remove"] = this.calculate_taxes_and_totals;
Anand Doshia91c95f2013-08-23 13:00:47 +0530118 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530119
Nabin Haitdd38a262014-12-26 13:15:21 +0530120 if(this.frm.fields_dict["items"]) {
121 this["items_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +0530122 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530123
Neil Trini Lasradoe2d8dd02015-06-22 19:06:15 +0530124 if(this.frm.fields_dict["recurring_print_format"]) {
125 this.frm.set_query("recurring_print_format", function(doc) {
126 return{
127 filters: [
128 ['Print Format', 'doc_type', '=', cur_frm.doctype],
129 ]
130 }
131 });
132 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530133
Nabin Hait1d218422015-07-17 15:19:02 +0530134 if(this.frm.fields_dict["return_against"]) {
135 this.frm.set_query("return_against", function(doc) {
136 var filters = {
137 "docstatus": 1,
138 "is_return": 0,
139 "company": doc.company
140 };
141 if (me.frm.fields_dict["customer"] && doc.customer) filters["customer"] = doc.customer;
142 if (me.frm.fields_dict["supplier"] && doc.supplier) filters["supplier"] = doc.supplier;
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530143
Nabin Hait1d218422015-07-17 15:19:02 +0530144 return {
145 filters: filters
146 }
147 });
148 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530149
Anand Doshi3543f302013-05-24 19:25:01 +0530150 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530151
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530152 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +0530153 var me = this;
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530154 if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length
155 && !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) {
156 this.apply_default_taxes();
Anand Doshi70f57eb2015-11-27 14:37:40 +0530157 } else if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"]
Nabin Haitbdfb0702015-10-28 15:38:08 +0530158 && !this.frm.doc.is_pos) {
159 me.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530160 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530161 if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) {
Rushabh Mehta203cc962016-04-07 15:25:43 +0530162 this.setup_item_selector();
Rushabh Mehtab6843f22014-06-04 13:10:41 +0530163 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530164 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530165
Anand Doshi3543f302013-05-24 19:25:01 +0530166 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +0530167 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +0530168 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +0530169 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +0530170 this.set_dynamic_labels();
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530171 this.setup_sms();
Anand Doshife13bfe2015-08-25 12:49:40 +0530172 this.make_show_payments_btn();
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530173 },
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530174
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530175 apply_default_taxes: function() {
176 var me = this;
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530177 var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
178 me.frm.doc.name);
179
180 if(taxes_and_charges_field) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530181 return frappe.call({
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530182 method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges",
183 args: {
184 "master_doctype": taxes_and_charges_field.options
185 },
186 callback: function(r) {
187 if(!r.exc) {
188 me.frm.set_value("taxes", r.message);
Nabin Haitbdfb0702015-10-28 15:38:08 +0530189 me.calculate_taxes_and_totals();
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530190 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530191 }
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530192 });
193 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530194 },
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530195
196 setup_sms: function() {
197 var me = this;
Nabin Hait7132bd32016-04-13 15:19:37 +0530198 if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped", "Closed"], this.frm.doc.status)
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530199 && this.frm.doctype != "Purchase Invoice") {
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530200 this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); });
201 }
202 },
203
204 send_sms: function() {
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530205 var sms_man = new SMSManager(this.frm.doc);
Nabin Hait239c2c32015-01-15 14:21:41 +0530206 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530207
Anand Doshife13bfe2015-08-25 12:49:40 +0530208 make_show_payments_btn: function() {
209 var me = this;
210 if (in_list(["Purchase Invoice", "Sales Invoice"], this.frm.doctype)) {
211 if(this.frm.doc.outstanding_amount !== this.frm.doc.base_grand_total) {
Saurabhe23e99f2016-01-06 15:34:56 +0530212 this.frm.add_custom_button(__("Payments"), function() {
Anand Doshife13bfe2015-08-25 12:49:40 +0530213 frappe.route_options = {
214 "Journal Entry Account.reference_type": me.frm.doc.doctype,
215 "Journal Entry Account.reference_name": me.frm.doc.name
216 };
217
218 frappe.set_route("List", "Journal Entry");
Saurabhe23e99f2016-01-06 15:34:56 +0530219 }, __("View"));
Anand Doshife13bfe2015-08-25 12:49:40 +0530220 }
Nabin Hait239c2c32015-01-15 14:21:41 +0530221 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530222 },
223
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530224 barcode: function(doc, cdt, cdn) {
225 var d = locals[cdt][cdn];
226 if(d.barcode=="" || d.barcode==null) {
227 // barcode cleared, remove item
228 d.item_code = "";
229 }
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530230 this.item_code(doc, cdt, cdn, true);
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530231 },
232
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530233 item_code: function(doc, cdt, cdn, from_barcode) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530234 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530235 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530236
237 // clear barcode if setting item (else barcode will take priority)
238 if(!from_barcode) {
239 item.barcode = null;
240 }
Nabin Hait139dc7b2014-02-12 14:53:18 +0530241 if(item.item_code || item.barcode || item.serial_no) {
242 if(!this.validate_company_and_party()) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530243 cur_frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +0530244 } else {
245 return this.frm.call({
246 method: "erpnext.stock.get_item_details.get_item_details",
247 child: item,
248 args: {
249 args: {
250 item_code: item.item_code,
251 barcode: item.barcode,
252 serial_no: item.serial_no,
253 warehouse: item.warehouse,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530254 customer: me.frm.doc.customer,
255 supplier: me.frm.doc.supplier,
256 currency: me.frm.doc.currency,
257 conversion_rate: me.frm.doc.conversion_rate,
258 price_list: me.frm.doc.selling_price_list ||
259 me.frm.doc.buying_price_list,
260 price_list_currency: me.frm.doc.price_list_currency,
261 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
262 company: me.frm.doc.company,
263 order_type: me.frm.doc.order_type,
264 is_pos: cint(me.frm.doc.is_pos),
265 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Haita1bf43b2015-03-17 10:50:47 +0530266 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
Nabin Hait444f9562014-06-20 15:59:49 +0530267 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
Anand Doshiafe85bd2016-01-21 21:04:16 +0530268 doctype: me.frm.doc.doctype,
269 name: me.frm.doc.name,
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530270 project: item.project || me.frm.doc.project,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530271 qty: item.qty
Nabin Hait139dc7b2014-02-12 14:53:18 +0530272 }
273 },
Anand Doshi13945092014-09-21 19:45:49 +0530274
Nabin Hait139dc7b2014-02-12 14:53:18 +0530275 callback: function(r) {
276 if(!r.exc) {
277 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
278 }
279 }
280 });
281 }
282 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530283 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530284
285 serial_no: function(doc, cdt, cdn) {
286 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530287 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530288
289 if (item.serial_no) {
290 if (!item.item_code) {
291 this.frm.script_manager.trigger("item_code", cdt, cdn);
292 }
293 else {
294 var sr_no = [];
295
296 // Replacing all occurences of comma with carriage return
297 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
298
299 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530300
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530301 // Trim each string and push unique string to new list
302 for (var x=0; x<=serial_nos.length - 1; x++) {
303 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
304 sr_no.push(serial_nos[x].trim());
305 }
306 }
307
308 // Add the new list to the serial no. field in grid with each in new line
309 item.serial_no = "";
310 for (var x=0; x<=sr_no.length - 1; x++)
311 item.serial_no += sr_no[x] + '\n';
312
313 refresh_field("serial_no", item.name, item.parentfield);
Nabin Haitb7c0c552015-08-04 12:18:12 +0530314 if(!doc.is_return) {
315 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
316 }
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530317 }
318 }
319 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530320
Anand Doshi923d41d2013-05-28 17:23:36 +0530321 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530322 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530323 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530324
Anand Doshi3543f302013-05-24 19:25:01 +0530325 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530326 var me = this;
Nabin Haita731ad42016-02-04 17:21:28 +0530327 var set_pricing = function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530328 if(me.frm.doc.company && me.frm.fields_dict.currency) {
329 var company_currency = me.get_company_currency();
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530330 var company_doc = frappe.get_doc(":Company", me.frm.doc.company);
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530331 if (!me.frm.doc.currency) {
332 me.frm.set_value("currency", company_currency);
333 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530334
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530335 if (me.frm.doc.currency == company_currency) {
336 me.frm.set_value("conversion_rate", 1.0);
337 }
338 if (me.frm.doc.price_list_currency == company_currency) {
339 me.frm.set_value('plc_conversion_rate', 1.0);
340 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530341 if (company_doc.default_letter_head) {
Rushabh Mehtac1857342015-05-27 12:29:57 +0530342 if(me.frm.fields_dict.letter_head) {
343 me.frm.set_value("letter_head", company_doc.default_letter_head);
344 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530345 }
Rushabh Mehtac1857342015-05-27 12:29:57 +0530346 if (company_doc.default_terms && me.frm.doc.doctype != "Purchase Invoice") {
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530347 me.frm.set_value("tc_name", company_doc.default_terms);
348 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530349
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530350 me.frm.script_manager.trigger("currency");
351 me.apply_pricing_rule();
Nabin Haitdd38a262014-12-26 13:15:21 +0530352 }
Anand Doshi3543f302013-05-24 19:25:01 +0530353 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530354
Nabin Haita731ad42016-02-04 17:21:28 +0530355 var set_party_account = function(set_pricing) {
356 if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) {
357 if(me.frm.doc.doctype=="Sales Invoice") {
358 var party_type = "Customer";
359 var party_account_field = 'debit_to';
360 } else {
361 var party_type = "Supplier";
362 var party_account_field = 'credit_to';
363 }
Anand Doshi39a28912016-02-15 11:42:18 +0530364
Nabin Haitef864402016-02-10 14:47:25 +0530365 var party = me.frm.doc[frappe.model.scrub(party_type)];
366 if(party) {
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530367 return frappe.call({
368 method: "erpnext.accounts.party.get_party_account",
369 args: {
370 company: me.frm.doc.company,
371 party_type: party_type,
Nabin Haitef864402016-02-10 14:47:25 +0530372 party: party
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530373 },
374 callback: function(r) {
375 if(!r.exc && r.message) {
376 me.frm.set_value(party_account_field, r.message);
377 set_pricing();
378 }
Nabin Haita731ad42016-02-04 17:21:28 +0530379 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530380 });
Nabin Haitef864402016-02-10 14:47:25 +0530381 } else {
382 set_pricing();
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530383 }
Nabin Haita731ad42016-02-04 17:21:28 +0530384 } else {
385 set_pricing();
386 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530387
Nabin Haita731ad42016-02-04 17:21:28 +0530388 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530389
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530390 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
391 else var date = this.frm.doc.transaction_date;
Rushabh Mehta5dfe20c2016-04-26 12:59:08 +0530392
393 if (frappe.meta.get_docfield(this.frm.doctype, "shipping_address") &&
Rohit Waghchaureb2e7a1f2016-04-11 12:10:20 +0530394 in_list(['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'], this.frm.doctype)){
395 erpnext.utils.get_shipping_address(this.frm, function(){
396 set_party_account(set_pricing);
397 })
398 }else{
399 set_party_account(set_pricing);
400 }
Rushabh Mehta45ca8a42015-04-28 16:02:09 +0530401
402 if(this.frm.doc.company) {
403 erpnext.last_selected_company = this.frm.doc.company;
404 }
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530405 },
406
407 transaction_date: function() {
Anand Doshi87da6622015-10-19 14:59:32 +0530408 if (this.frm.doc.transaction_date) {
409 this.frm.transaction_date = this.frm.doc.transaction_date;
410 }
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530411 },
412
413 posting_date: function() {
Nabin Hait40e92b62015-07-09 17:12:23 +0530414 var me = this;
415 if (this.frm.doc.posting_date) {
Anand Doshi87da6622015-10-19 14:59:32 +0530416 this.frm.posting_date = this.frm.doc.posting_date;
417
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530418 if ((this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer) ||
Nabin Hait40e92b62015-07-09 17:12:23 +0530419 (this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier)) {
420 return frappe.call({
421 method: "erpnext.accounts.party.get_due_date",
422 args: {
423 "posting_date": me.frm.doc.posting_date,
424 "party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier",
425 "party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier,
426 "company": me.frm.doc.company
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530427 },
Nabin Hait40e92b62015-07-09 17:12:23 +0530428 callback: function(r, rt) {
429 if(r.message) {
430 me.frm.set_value("due_date", r.message);
431 }
Nabin Hait40e92b62015-07-09 17:12:23 +0530432 }
433 })
Nabin Hait40e92b62015-07-09 17:12:23 +0530434 }
435 }
Anand Doshi3543f302013-05-24 19:25:01 +0530436 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530437
Anand Doshi3543f302013-05-24 19:25:01 +0530438 get_company_currency: function() {
439 return erpnext.get_currency(this.frm.doc.company);
440 },
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530441
Neil Trini Lasrado09a66c42015-07-07 16:41:37 +0530442 contact_person: function() {
443 erpnext.utils.get_contact_details(this.frm);
444 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530445
Anand Doshi3543f302013-05-24 19:25:01 +0530446 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530447 var me = this;
448 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530449
Anand Doshi61a2f682013-06-21 17:55:31 +0530450 var company_currency = this.get_company_currency();
Nabin Hait69cdf592015-05-02 18:46:10 +0530451 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
Pratik Vyas1a84d8e2015-05-19 12:23:52 +0530452 if(this.frm.doc.currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530453 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530454 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530455 me.frm.set_value("conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530456 });
457 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530458 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530459 }
460 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530461
Anand Doshi61a2f682013-06-21 17:55:31 +0530462 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530463 if(this.frm.doc.currency === this.get_company_currency()) {
464 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530465 }
466 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530467 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
468 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
469 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530470
Nabin Haita3dd72a2014-05-28 12:49:20 +0530471 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530472 if(this.frm.doc.ignore_pricing_rule) {
473 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530474 } else if (!this.in_apply_price_list){
475 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530476 }
477
Nabin Haita3dd72a2014-05-28 12:49:20 +0530478 }
Anand Doshi3543f302013-05-24 19:25:01 +0530479 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530480
Anand Doshi61a2f682013-06-21 17:55:31 +0530481 get_exchange_rate: function(from_currency, to_currency, callback) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530482 return frappe.call({
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530483 method: "erpnext.setup.utils.get_exchange_rate",
484 args: {
485 from_currency: from_currency,
486 to_currency: to_currency
487 },
488 callback: function(r) {
489 callback(flt(r.message));
490 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530491 });
492 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530493
Anand Doshi3543f302013-05-24 19:25:01 +0530494 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530495 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530496 this.set_dynamic_labels();
Nabin Haitec361ea2015-05-11 18:14:45 +0530497
498 var company_currency = this.get_company_currency();
499 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
500 if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
501 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
502 function(exchange_rate) {
503 me.frm.set_value("plc_conversion_rate", exchange_rate);
504 });
505 } else {
506 this.plc_conversion_rate();
507 }
Anand Doshi3543f302013-05-24 19:25:01 +0530508 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530509
Anand Doshi3543f302013-05-24 19:25:01 +0530510 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530511 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
512 this.frm.set_value("plc_conversion_rate", 1.0);
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530513 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency
Nabin Hait1d218422015-07-17 15:19:02 +0530514 && this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 &&
Nabin Haitec361ea2015-05-11 18:14:45 +0530515 cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
516 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530517 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530518
Nabin Haitec361ea2015-05-11 18:14:45 +0530519 if(!this.in_apply_price_list) {
520 this.apply_price_list();
Anand Doshi61a2f682013-06-21 17:55:31 +0530521 }
Anand Doshi3543f302013-05-24 19:25:01 +0530522 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530523
Anand Doshi3543f302013-05-24 19:25:01 +0530524 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530525 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530526 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530527
Anand Doshi61a2f682013-06-21 17:55:31 +0530528 set_dynamic_labels: function() {
529 // What TODO? should we make price list system non-mandatory?
530 this.frm.toggle_reqd("plc_conversion_rate",
531 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530532
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530533 if(this.frm.doc_currency!==this.frm.doc.currency) {
534 // reset names only when the currency is different
535
536 var company_currency = this.get_company_currency();
537 this.change_form_labels(company_currency);
538 this.change_grid_labels(company_currency);
539 this.frm.refresh_fields();
540 this.frm.doc_currency = this.frm.doc.currency;
541 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530542 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530543
Nabin Hait613d0812015-02-23 11:58:15 +0530544 change_form_labels: function(company_currency) {
545 var me = this;
546 var field_label_map = {};
547
548 var setup_field_label_map = function(fields_list, currency) {
549 $.each(fields_list, function(i, fname) {
550 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
551 if(docfield) {
552 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
553 field_label_map[fname] = label.trim() + " (" + currency + ")";
554 }
555 });
556 };
557 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
558 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
559 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530560 "base_paid_amount", "base_write_off_amount", "base_change_amount"
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530561 ], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530562
563 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
564 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530565 "rounded_total", "in_words", "paid_amount", "write_off_amount"], this.frm.doc.currency);
Anand Doshi87da6622015-10-19 14:59:32 +0530566
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530567 setup_field_label_map(["outstanding_amount", "total_advance"], this.frm.doc.party_account_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530568
569 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
570 + " = [?] " + company_currency)
571
572 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
573 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
574 + " = [?] " + company_currency)
575 }
576
577 // toggle fields
578 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
579 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530580 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount",
581 "base_paid_amount", "base_write_off_amount"],
Nabin Hait613d0812015-02-23 11:58:15 +0530582 this.frm.doc.currency != company_currency);
583
584 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
585 this.frm.doc.price_list_currency != company_currency);
586
587 // set labels
588 $.each(field_label_map, function(fname, label) {
589 me.frm.fields_dict[fname].set_label(label);
590 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530591
Nabin Hait903c42a2015-02-24 15:24:49 +0530592 var show =cint(cur_frm.doc.discount_amount) ||
593 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
594
595 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
596 cur_frm.toggle_display("net_total", show);
597
598 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
599 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
600
Nabin Hait613d0812015-02-23 11:58:15 +0530601 },
602
603 change_grid_labels: function(company_currency) {
604 var me = this;
605 var field_label_map = {};
606
607 var setup_field_label_map = function(fields_list, currency, parentfield) {
608 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
609 $.each(fields_list, function(i, fname) {
610 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
611 if(docfield) {
612 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
613 field_label_map[grid_doctype + "-" + fname] =
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530614 label.trim() + " (" + __(currency) + ")";
Nabin Hait613d0812015-02-23 11:58:15 +0530615 }
616 });
617 }
618
619 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
620 company_currency, "items");
621
622 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
623 this.frm.doc.currency, "items");
624
625 if(this.frm.fields_dict["taxes"]) {
626 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
627
628 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
629 }
630
631 if(this.frm.fields_dict["advances"]) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530632 setup_field_label_map(["advance_amount", "allocated_amount"],
Nabin Haitaa015902015-10-16 13:08:33 +0530633 this.frm.doc.party_account_currency, "advances");
Nabin Hait613d0812015-02-23 11:58:15 +0530634 }
635
636 // toggle columns
637 var item_grid = this.frm.fields_dict["items"].grid;
638 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
639 if(frappe.meta.get_docfield(item_grid.doctype, fname))
640 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
641 });
642
643 var show = (cint(cur_frm.doc.discount_amount)) ||
644 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
645
646 $.each(["net_rate", "net_amount"], function(i, fname) {
647 if(frappe.meta.get_docfield(item_grid.doctype, fname))
648 item_grid.set_column_disp(fname, show);
649 });
650
651 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
652 if(frappe.meta.get_docfield(item_grid.doctype, fname))
653 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
654 });
655
656 // set labels
657 var $wrapper = $(this.frm.wrapper);
658 $.each(field_label_map, function(fname, label) {
659 fname = fname.split("-");
660 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
661 if(df) df.label = label;
662 });
663 },
664
Anand Doshi923d41d2013-05-28 17:23:36 +0530665 recalculate: function() {
666 this.calculate_taxes_and_totals();
667 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530668
Anand Doshi923d41d2013-05-28 17:23:36 +0530669 recalculate_values: function() {
670 this.calculate_taxes_and_totals();
671 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530672
Anand Doshid0b00722013-05-28 18:54:48 +0530673 calculate_charges: function() {
674 this.calculate_taxes_and_totals();
675 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530676
Nabin Hait444f9562014-06-20 15:59:49 +0530677 ignore_pricing_rule: function() {
678 this.apply_pricing_rule();
679 },
680
681 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530682 var me = this;
Anand Doshi70f57eb2015-11-27 14:37:40 +0530683 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530684 if (!(args.items && args.items.length)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530685 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
686 return;
687 }
688
Anand Doshidffec8f2014-07-01 17:45:15 +0530689 return this.frm.call({
690 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
Anand Doshi70f57eb2015-11-27 14:37:40 +0530691 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530692 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530693 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530694 me._set_values_for_item_list(r.message);
Saurabh2d7af632016-02-26 11:09:20 +0530695 if(item) me.set_gross_profit(item);
Anand Doshidffec8f2014-07-01 17:45:15 +0530696 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
697 }
698 }
699 });
700 },
701
702 _get_args: function(item) {
703 var me = this;
704 return {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530705 "items": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530706 "customer": me.frm.doc.customer,
707 "customer_group": me.frm.doc.customer_group,
708 "territory": me.frm.doc.territory,
709 "supplier": me.frm.doc.supplier,
710 "supplier_type": me.frm.doc.supplier_type,
711 "currency": me.frm.doc.currency,
712 "conversion_rate": me.frm.doc.conversion_rate,
713 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Haitdd82bf22015-05-11 16:49:17 +0530714 "price_list_currency": me.frm.doc.price_list_currency,
Nabin Hait444f9562014-06-20 15:59:49 +0530715 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
716 "company": me.frm.doc.company,
717 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
718 "campaign": me.frm.doc.campaign,
719 "sales_partner": me.frm.doc.sales_partner,
720 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530721 "doctype": me.frm.doc.doctype,
722 "name": me.frm.doc.name
Nabin Hait444f9562014-06-20 15:59:49 +0530723 };
Nabin Hait444f9562014-06-20 15:59:49 +0530724 },
725
726 _get_item_list: function(item) {
727 var item_list = [];
728 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530729 if (d.item_code) {
730 item_list.push({
731 "doctype": d.doctype,
732 "name": d.name,
733 "item_code": d.item_code,
734 "item_group": d.item_group,
735 "brand": d.brand,
Anand Doshi39a28912016-02-15 11:42:18 +0530736 "qty": d.qty,
737 "parenttype": d.parenttype,
738 "parent": d.parent
Anand Doshi39edcbc2014-07-28 16:00:19 +0530739 });
mbauskarc79415e2016-01-18 16:32:24 +0530740
741 // if doctype is Quotation Item / Sales Order Iten then add Margin Type and rate in item_list
mbauskara52472c2016-03-05 15:10:25 +0530742 if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype){
743 item_list[0]["margin_type"] = d.margin_type
744 item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530745 }
Anand Doshi39edcbc2014-07-28 16:00:19 +0530746 }
Nabin Hait444f9562014-06-20 15:59:49 +0530747 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530748
Nabin Hait444f9562014-06-20 15:59:49 +0530749 if (item) {
750 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530751 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530752 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530753 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530754 });
755 }
Nabin Hait444f9562014-06-20 15:59:49 +0530756 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530757 },
758
Anand Doshidffec8f2014-07-01 17:45:15 +0530759 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530760 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530761 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530762 for(var i=0, l=children.length; i<l; i++) {
763 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530764 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530765
766 for(var k in d) {
767 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530768 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530769 if(k=="price_list_rate") {
770 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
771 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530772 frappe.model.set_value(d.doctype, d.name, k, v);
773 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530774 }
775
Nabin Hait49a27292014-12-17 11:21:32 +0530776 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530777 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530778 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
779 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530780 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530781
Anand Doshidea3fb02015-02-05 15:05:58 +0530782 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530783 },
784
Nabin Hait49a27292014-12-17 11:21:32 +0530785 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530786 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530787 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530788 if (!((args.items && args.items.length) || args.price_list)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530789 return;
790 }
Nabin Hait37b047d2015-02-23 16:01:33 +0530791
Anand Doshidffec8f2014-07-01 17:45:15 +0530792 return this.frm.call({
793 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530794 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530795 callback: function(r) {
796 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530797 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530798 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
799 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530800 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530801
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530802 if(args.items.length) {
Nabin Hait37b047d2015-02-23 16:01:33 +0530803 me._set_values_for_item_list(r.message.children);
804 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530805 }
806 }
807 });
808 },
809
Anand Doshi3543f302013-05-24 19:25:01 +0530810 get_item_wise_taxes_html: function() {
811 var item_tax = {};
812 var tax_accounts = [];
813 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530814
Nabin Hait96339342015-01-21 17:22:11 +0530815 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530816 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530817 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530818 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530819 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530820 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530821 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530822 var tax_rate = "";
823 if(tax_data[0] != null) {
824 tax_rate = (tax.charge_type === "Actual") ?
825 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
826 (flt(tax_data[0], tax_rate_precision) + "%");
827 }
828 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
829 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530830
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530831 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530832 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530833 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530834 }
Anand Doshi3543f302013-05-24 19:25:01 +0530835 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530836 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530837 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530838
839 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530840 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530841
Anand Doshi179e3772013-09-05 16:53:57 +0530842 var distinct_item_names = [];
843 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530844 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530845 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
846 distinct_item_names.push(item.item_code || item.item_name);
847 distinct_items.push(item);
848 }
849 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530850
Anand Doshi179e3772013-09-05 16:53:57 +0530851 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530852 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530853 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530854 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
855 item_name: item.item_name,
856 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530857 return item_tax_record[head[0]] ?
858 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530859 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530860 }).join("\n")
861 });
862 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530863
Anand Doshi53b73422013-05-31 11:50:01 +0530864 if(!rows) return "";
Rushabh Mehtaf24f6042015-08-20 18:01:40 +0530865 return '<p><a class="h6 text-muted" href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">'
866 + __("Show tax break-up") + '</a><br><br></p>\
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530867 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530868 <thead><tr>' + headings + '</tr></thead> \
869 <tbody>' + rows + '</tbody> \
870 </table></div>';
871 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530872
Nabin Hait139dc7b2014-02-12 14:53:18 +0530873 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530874 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530875 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530876
Nabin Hait0f231062014-02-20 11:27:47 +0530877 $.each(["company", "customer"], function(i, fieldname) {
Saurabh8a8ef852015-10-21 19:28:53 +0530878 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname && me.frm.doc.doctype != "Purchase Order")) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530879 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530880 msgprint(__("Please specify") + ": " +
881 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530882 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530883 valid = false;
884 }
Anand Doshi3543f302013-05-24 19:25:01 +0530885 }
886 });
887 return valid;
888 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530889
Anand Doshibf3e54a2013-06-05 14:11:32 +0530890 get_terms: function() {
891 var me = this;
892 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530893 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530894 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530895 args: {
896 doctype: "Terms and Conditions",
897 fieldname: "terms",
898 filters: { name: this.frm.doc.tc_name },
899 },
900 });
901 }
902 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530903
904 taxes_and_charges: function() {
905 var me = this;
906 if(this.frm.doc.taxes_and_charges) {
907 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530908 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
909 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530910 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
911 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530912 "master_name": this.frm.doc.taxes_and_charges,
Nabin Hait6b039142014-05-02 15:45:10 +0530913 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530914 callback: function(r) {
915 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530916 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530917 me.calculate_taxes_and_totals();
918 }
919 }
920 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530921 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530922 },
923
924 show_item_wise_taxes: function() {
925 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530926 var html = this.get_item_wise_taxes_html();
927 if (html) {
928 this.frm.toggle_display("other_charges_calculation", true);
929 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
930 } else {
931 this.frm.toggle_display("other_charges_calculation", false);
932 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530933 }
934 },
Anand Doshi13945092014-09-21 19:45:49 +0530935
936 is_recurring: function() {
937 // set default values for recurring documents
Rushabh Mehta756e5182016-03-03 13:00:17 +0530938 if(this.frm.doc.is_recurring && this.frm.doc.__islocal) {
939 frappe.msgprint(__("Please set recurring after saving"));
940 this.frm.set_value('is_recurring', 0);
941 return;
942 }
943
Anand Doshi13945092014-09-21 19:45:49 +0530944 if(this.frm.doc.is_recurring) {
Nabin Hait68a628e2016-03-21 12:03:16 +0530945 if(!this.frm.doc.recurring_id) {
946 this.frm.set_value('recurring_id', this.frm.doc.name);
947 }
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530948
Anand Doshi13945092014-09-21 19:45:49 +0530949 var owner_email = this.frm.doc.owner=="Administrator"
950 ? frappe.user_info("Administrator").email
951 : this.frm.doc.owner;
952
953 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
954 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
955 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
956 }
957
958 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
959 },
960
961 from_date: function() {
962 // set to_date
963 if(this.frm.doc.from_date) {
964 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
965 'Yearly': 12};
966
967 var months = recurring_type_map[this.frm.doc.recurring_type];
968 if(months) {
969 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
970 months);
971 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
972 refresh_field('to_date');
973 }
974 }
Saurabhd3135532016-02-25 18:59:20 +0530975 },
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530976
Saurabh2d7af632016-02-26 11:09:20 +0530977 set_gross_profit: function(item) {
Saurabh5ada14b2016-02-26 18:02:55 +0530978 if (this.frm.doc.doctype == "Sales Order" && item.valuation_rate) {
Saurabh3fbf3ce2016-03-09 15:31:04 +0530979 rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1);
980 item.gross_profit = flt(((rate - item.valuation_rate) * item.qty), precision("amount", item));
Saurabh2d7af632016-02-26 11:09:20 +0530981 }
Rushabh Mehta203cc962016-04-07 15:25:43 +0530982 },
983
984 setup_item_selector: function() {
Rushabh Mehtabc928242016-05-29 23:08:22 +0530985 // TODO: remove item selector
986
987 return;
Rushabh Mehta203cc962016-04-07 15:25:43 +0530988 if(!this.item_selector) {
989 this.item_selector = new erpnext.ItemSelector({frm: this.frm});
990 }
Nabin Hait28a05282016-06-27 17:41:39 +0530991 },
992
993 get_advances: function() {
994 if(!this.frm.is_return) {
995 return this.frm.call({
996 method: "set_advances",
997 doc: this.frm.doc,
998 callback: function(r, rt) {
999 refresh_field("advances");
1000 }
1001 })
1002 }
Anand Doshi13945092014-09-21 19:45:49 +05301003 }
Rushabh Mehtabe2ee182016-04-29 17:22:42 +05301004});