blob: f49b050d43133cbfb16be9d56b19034c44cfc999 [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();
172 },
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530173
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530174 apply_default_taxes: function() {
175 var me = this;
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530176 var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
177 me.frm.doc.name);
178
179 if(taxes_and_charges_field) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530180 return frappe.call({
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530181 method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges",
182 args: {
183 "master_doctype": taxes_and_charges_field.options
184 },
185 callback: function(r) {
186 if(!r.exc) {
187 me.frm.set_value("taxes", r.message);
Nabin Haitbdfb0702015-10-28 15:38:08 +0530188 me.calculate_taxes_and_totals();
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530189 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530190 }
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530191 });
192 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530193 },
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530194
195 setup_sms: function() {
196 var me = this;
Nabin Hait7132bd32016-04-13 15:19:37 +0530197 if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped", "Closed"], this.frm.doc.status)
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530198 && this.frm.doctype != "Purchase Invoice") {
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530199 this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); });
200 }
201 },
202
203 send_sms: function() {
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530204 var sms_man = new SMSManager(this.frm.doc);
Nabin Hait239c2c32015-01-15 14:21:41 +0530205 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530206
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530207 barcode: function(doc, cdt, cdn) {
208 var d = locals[cdt][cdn];
209 if(d.barcode=="" || d.barcode==null) {
210 // barcode cleared, remove item
211 d.item_code = "";
212 }
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530213 this.item_code(doc, cdt, cdn, true);
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530214 },
215
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530216 item_code: function(doc, cdt, cdn, from_barcode) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530217 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530218 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530219
220 // clear barcode if setting item (else barcode will take priority)
221 if(!from_barcode) {
222 item.barcode = null;
223 }
Nabin Hait139dc7b2014-02-12 14:53:18 +0530224 if(item.item_code || item.barcode || item.serial_no) {
225 if(!this.validate_company_and_party()) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530226 cur_frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +0530227 } else {
228 return this.frm.call({
229 method: "erpnext.stock.get_item_details.get_item_details",
230 child: item,
231 args: {
232 args: {
233 item_code: item.item_code,
234 barcode: item.barcode,
235 serial_no: item.serial_no,
236 warehouse: item.warehouse,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530237 customer: me.frm.doc.customer,
238 supplier: me.frm.doc.supplier,
239 currency: me.frm.doc.currency,
240 conversion_rate: me.frm.doc.conversion_rate,
241 price_list: me.frm.doc.selling_price_list ||
242 me.frm.doc.buying_price_list,
243 price_list_currency: me.frm.doc.price_list_currency,
244 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
245 company: me.frm.doc.company,
246 order_type: me.frm.doc.order_type,
247 is_pos: cint(me.frm.doc.is_pos),
248 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Haita1bf43b2015-03-17 10:50:47 +0530249 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
Nabin Hait444f9562014-06-20 15:59:49 +0530250 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
Anand Doshiafe85bd2016-01-21 21:04:16 +0530251 doctype: me.frm.doc.doctype,
252 name: me.frm.doc.name,
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530253 project: item.project || me.frm.doc.project,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530254 qty: item.qty
Nabin Hait139dc7b2014-02-12 14:53:18 +0530255 }
256 },
Anand Doshi13945092014-09-21 19:45:49 +0530257
Nabin Hait139dc7b2014-02-12 14:53:18 +0530258 callback: function(r) {
259 if(!r.exc) {
260 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
261 }
262 }
263 });
264 }
265 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530266 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530267
268 serial_no: function(doc, cdt, cdn) {
269 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530270 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530271
272 if (item.serial_no) {
273 if (!item.item_code) {
274 this.frm.script_manager.trigger("item_code", cdt, cdn);
275 }
276 else {
277 var sr_no = [];
278
279 // Replacing all occurences of comma with carriage return
280 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
281
282 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530283
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530284 // Trim each string and push unique string to new list
285 for (var x=0; x<=serial_nos.length - 1; x++) {
286 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
287 sr_no.push(serial_nos[x].trim());
288 }
289 }
290
291 // Add the new list to the serial no. field in grid with each in new line
292 item.serial_no = "";
293 for (var x=0; x<=sr_no.length - 1; x++)
294 item.serial_no += sr_no[x] + '\n';
295
296 refresh_field("serial_no", item.name, item.parentfield);
Nabin Haitb7c0c552015-08-04 12:18:12 +0530297 if(!doc.is_return) {
298 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
299 }
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530300 }
301 }
302 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530303
Anand Doshi923d41d2013-05-28 17:23:36 +0530304 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530305 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530306 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530307
Anand Doshi3543f302013-05-24 19:25:01 +0530308 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530309 var me = this;
Nabin Haita731ad42016-02-04 17:21:28 +0530310 var set_pricing = function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530311 if(me.frm.doc.company && me.frm.fields_dict.currency) {
312 var company_currency = me.get_company_currency();
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530313 var company_doc = frappe.get_doc(":Company", me.frm.doc.company);
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530314 if (!me.frm.doc.currency) {
315 me.frm.set_value("currency", company_currency);
316 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530317
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530318 if (me.frm.doc.currency == company_currency) {
319 me.frm.set_value("conversion_rate", 1.0);
320 }
321 if (me.frm.doc.price_list_currency == company_currency) {
322 me.frm.set_value('plc_conversion_rate', 1.0);
323 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530324 if (company_doc.default_letter_head) {
Rushabh Mehtac1857342015-05-27 12:29:57 +0530325 if(me.frm.fields_dict.letter_head) {
326 me.frm.set_value("letter_head", company_doc.default_letter_head);
327 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530328 }
Rushabh Mehtac1857342015-05-27 12:29:57 +0530329 if (company_doc.default_terms && me.frm.doc.doctype != "Purchase Invoice") {
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530330 me.frm.set_value("tc_name", company_doc.default_terms);
331 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530332
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530333 me.frm.script_manager.trigger("currency");
334 me.apply_pricing_rule();
Nabin Haitdd38a262014-12-26 13:15:21 +0530335 }
Anand Doshi3543f302013-05-24 19:25:01 +0530336 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530337
Nabin Haita731ad42016-02-04 17:21:28 +0530338 var set_party_account = function(set_pricing) {
339 if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) {
340 if(me.frm.doc.doctype=="Sales Invoice") {
341 var party_type = "Customer";
342 var party_account_field = 'debit_to';
343 } else {
344 var party_type = "Supplier";
345 var party_account_field = 'credit_to';
346 }
Anand Doshi39a28912016-02-15 11:42:18 +0530347
Nabin Haitef864402016-02-10 14:47:25 +0530348 var party = me.frm.doc[frappe.model.scrub(party_type)];
349 if(party) {
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530350 return frappe.call({
351 method: "erpnext.accounts.party.get_party_account",
352 args: {
353 company: me.frm.doc.company,
354 party_type: party_type,
Nabin Haitef864402016-02-10 14:47:25 +0530355 party: party
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530356 },
357 callback: function(r) {
358 if(!r.exc && r.message) {
359 me.frm.set_value(party_account_field, r.message);
360 set_pricing();
361 }
Nabin Haita731ad42016-02-04 17:21:28 +0530362 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530363 });
Nabin Haitef864402016-02-10 14:47:25 +0530364 } else {
365 set_pricing();
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530366 }
Nabin Haita731ad42016-02-04 17:21:28 +0530367 } else {
368 set_pricing();
369 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530370
Nabin Haita731ad42016-02-04 17:21:28 +0530371 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530372
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530373 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
374 else var date = this.frm.doc.transaction_date;
Rushabh Mehta5dfe20c2016-04-26 12:59:08 +0530375
376 if (frappe.meta.get_docfield(this.frm.doctype, "shipping_address") &&
Rohit Waghchaureb2e7a1f2016-04-11 12:10:20 +0530377 in_list(['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'], this.frm.doctype)){
378 erpnext.utils.get_shipping_address(this.frm, function(){
379 set_party_account(set_pricing);
380 })
381 }else{
382 set_party_account(set_pricing);
383 }
Rushabh Mehta45ca8a42015-04-28 16:02:09 +0530384
385 if(this.frm.doc.company) {
386 erpnext.last_selected_company = this.frm.doc.company;
387 }
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530388 },
389
390 transaction_date: function() {
Anand Doshi87da6622015-10-19 14:59:32 +0530391 if (this.frm.doc.transaction_date) {
392 this.frm.transaction_date = this.frm.doc.transaction_date;
393 }
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530394 },
395
396 posting_date: function() {
Nabin Hait40e92b62015-07-09 17:12:23 +0530397 var me = this;
398 if (this.frm.doc.posting_date) {
Anand Doshi87da6622015-10-19 14:59:32 +0530399 this.frm.posting_date = this.frm.doc.posting_date;
400
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530401 if ((this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer) ||
Nabin Hait40e92b62015-07-09 17:12:23 +0530402 (this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier)) {
403 return frappe.call({
404 method: "erpnext.accounts.party.get_due_date",
405 args: {
406 "posting_date": me.frm.doc.posting_date,
407 "party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier",
408 "party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier,
409 "company": me.frm.doc.company
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530410 },
Nabin Hait40e92b62015-07-09 17:12:23 +0530411 callback: function(r, rt) {
412 if(r.message) {
413 me.frm.set_value("due_date", r.message);
414 }
Nabin Hait40e92b62015-07-09 17:12:23 +0530415 }
416 })
Nabin Hait40e92b62015-07-09 17:12:23 +0530417 }
418 }
Anand Doshi3543f302013-05-24 19:25:01 +0530419 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530420
Anand Doshi3543f302013-05-24 19:25:01 +0530421 get_company_currency: function() {
422 return erpnext.get_currency(this.frm.doc.company);
423 },
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530424
Neil Trini Lasrado09a66c42015-07-07 16:41:37 +0530425 contact_person: function() {
426 erpnext.utils.get_contact_details(this.frm);
427 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530428
Anand Doshi3543f302013-05-24 19:25:01 +0530429 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530430 var me = this;
431 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530432
Anand Doshi61a2f682013-06-21 17:55:31 +0530433 var company_currency = this.get_company_currency();
Nabin Hait69cdf592015-05-02 18:46:10 +0530434 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
Pratik Vyas1a84d8e2015-05-19 12:23:52 +0530435 if(this.frm.doc.currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530436 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530437 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530438 me.frm.set_value("conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530439 });
440 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530441 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530442 }
443 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530444
Anand Doshi61a2f682013-06-21 17:55:31 +0530445 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530446 if(this.frm.doc.currency === this.get_company_currency()) {
447 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530448 }
449 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530450 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
451 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
452 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530453
Nabin Haita3dd72a2014-05-28 12:49:20 +0530454 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530455 if(this.frm.doc.ignore_pricing_rule) {
456 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530457 } else if (!this.in_apply_price_list){
458 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530459 }
460
Nabin Haita3dd72a2014-05-28 12:49:20 +0530461 }
Anand Doshi3543f302013-05-24 19:25:01 +0530462 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530463
Anand Doshi61a2f682013-06-21 17:55:31 +0530464 get_exchange_rate: function(from_currency, to_currency, callback) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530465 return frappe.call({
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530466 method: "erpnext.setup.utils.get_exchange_rate",
467 args: {
468 from_currency: from_currency,
469 to_currency: to_currency
470 },
471 callback: function(r) {
472 callback(flt(r.message));
473 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530474 });
475 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530476
Anand Doshi3543f302013-05-24 19:25:01 +0530477 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530478 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530479 this.set_dynamic_labels();
Nabin Haitec361ea2015-05-11 18:14:45 +0530480
481 var company_currency = this.get_company_currency();
482 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
483 if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
484 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
485 function(exchange_rate) {
486 me.frm.set_value("plc_conversion_rate", exchange_rate);
487 });
488 } else {
489 this.plc_conversion_rate();
490 }
Anand Doshi3543f302013-05-24 19:25:01 +0530491 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530492
Anand Doshi3543f302013-05-24 19:25:01 +0530493 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530494 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
495 this.frm.set_value("plc_conversion_rate", 1.0);
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530496 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency
Nabin Hait1d218422015-07-17 15:19:02 +0530497 && this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 &&
Nabin Haitec361ea2015-05-11 18:14:45 +0530498 cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
499 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530500 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530501
Nabin Haitec361ea2015-05-11 18:14:45 +0530502 if(!this.in_apply_price_list) {
503 this.apply_price_list();
Anand Doshi61a2f682013-06-21 17:55:31 +0530504 }
Anand Doshi3543f302013-05-24 19:25:01 +0530505 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530506
Anand Doshi3543f302013-05-24 19:25:01 +0530507 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530508 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530509 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530510
Anand Doshi61a2f682013-06-21 17:55:31 +0530511 set_dynamic_labels: function() {
512 // What TODO? should we make price list system non-mandatory?
513 this.frm.toggle_reqd("plc_conversion_rate",
514 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530515
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530516 if(this.frm.doc_currency!==this.frm.doc.currency) {
517 // reset names only when the currency is different
518
519 var company_currency = this.get_company_currency();
520 this.change_form_labels(company_currency);
521 this.change_grid_labels(company_currency);
522 this.frm.refresh_fields();
523 this.frm.doc_currency = this.frm.doc.currency;
524 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530525 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530526
Nabin Hait613d0812015-02-23 11:58:15 +0530527 change_form_labels: function(company_currency) {
528 var me = this;
529 var field_label_map = {};
530
531 var setup_field_label_map = function(fields_list, currency) {
532 $.each(fields_list, function(i, fname) {
533 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
534 if(docfield) {
535 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
536 field_label_map[fname] = label.trim() + " (" + currency + ")";
537 }
538 });
539 };
540 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
541 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
542 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530543 "base_paid_amount", "base_write_off_amount", "base_change_amount"
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530544 ], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530545
546 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
547 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530548 "rounded_total", "in_words", "paid_amount", "write_off_amount"], this.frm.doc.currency);
Anand Doshi87da6622015-10-19 14:59:32 +0530549
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530550 setup_field_label_map(["outstanding_amount", "total_advance"], this.frm.doc.party_account_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530551
552 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
553 + " = [?] " + company_currency)
554
555 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
556 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
557 + " = [?] " + company_currency)
558 }
559
560 // toggle fields
561 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
562 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530563 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount",
564 "base_paid_amount", "base_write_off_amount"],
Nabin Hait613d0812015-02-23 11:58:15 +0530565 this.frm.doc.currency != company_currency);
566
567 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
568 this.frm.doc.price_list_currency != company_currency);
569
570 // set labels
571 $.each(field_label_map, function(fname, label) {
572 me.frm.fields_dict[fname].set_label(label);
573 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530574
Nabin Hait903c42a2015-02-24 15:24:49 +0530575 var show =cint(cur_frm.doc.discount_amount) ||
576 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
577
578 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
579 cur_frm.toggle_display("net_total", show);
580
581 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
582 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
583
Nabin Hait613d0812015-02-23 11:58:15 +0530584 },
585
586 change_grid_labels: function(company_currency) {
587 var me = this;
588 var field_label_map = {};
589
590 var setup_field_label_map = function(fields_list, currency, parentfield) {
591 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
592 $.each(fields_list, function(i, fname) {
593 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
594 if(docfield) {
595 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
596 field_label_map[grid_doctype + "-" + fname] =
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530597 label.trim() + " (" + __(currency) + ")";
Nabin Hait613d0812015-02-23 11:58:15 +0530598 }
599 });
600 }
601
602 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
603 company_currency, "items");
604
605 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
606 this.frm.doc.currency, "items");
607
608 if(this.frm.fields_dict["taxes"]) {
609 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
610
611 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
612 }
613
614 if(this.frm.fields_dict["advances"]) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530615 setup_field_label_map(["advance_amount", "allocated_amount"],
Nabin Haitaa015902015-10-16 13:08:33 +0530616 this.frm.doc.party_account_currency, "advances");
Nabin Hait613d0812015-02-23 11:58:15 +0530617 }
618
619 // toggle columns
620 var item_grid = this.frm.fields_dict["items"].grid;
621 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
622 if(frappe.meta.get_docfield(item_grid.doctype, fname))
623 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
624 });
625
626 var show = (cint(cur_frm.doc.discount_amount)) ||
627 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
628
629 $.each(["net_rate", "net_amount"], function(i, fname) {
630 if(frappe.meta.get_docfield(item_grid.doctype, fname))
631 item_grid.set_column_disp(fname, show);
632 });
633
634 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
635 if(frappe.meta.get_docfield(item_grid.doctype, fname))
636 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
637 });
638
639 // set labels
640 var $wrapper = $(this.frm.wrapper);
641 $.each(field_label_map, function(fname, label) {
642 fname = fname.split("-");
643 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
644 if(df) df.label = label;
645 });
646 },
647
Anand Doshi923d41d2013-05-28 17:23:36 +0530648 recalculate: function() {
649 this.calculate_taxes_and_totals();
650 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530651
Anand Doshi923d41d2013-05-28 17:23:36 +0530652 recalculate_values: function() {
653 this.calculate_taxes_and_totals();
654 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530655
Anand Doshid0b00722013-05-28 18:54:48 +0530656 calculate_charges: function() {
657 this.calculate_taxes_and_totals();
658 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530659
Nabin Hait444f9562014-06-20 15:59:49 +0530660 ignore_pricing_rule: function() {
661 this.apply_pricing_rule();
662 },
663
664 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530665 var me = this;
Anand Doshi70f57eb2015-11-27 14:37:40 +0530666 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530667 if (!(args.items && args.items.length)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530668 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
669 return;
670 }
671
Anand Doshidffec8f2014-07-01 17:45:15 +0530672 return this.frm.call({
673 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
Anand Doshi70f57eb2015-11-27 14:37:40 +0530674 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530675 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530676 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530677 me._set_values_for_item_list(r.message);
Saurabh2d7af632016-02-26 11:09:20 +0530678 if(item) me.set_gross_profit(item);
Anand Doshidffec8f2014-07-01 17:45:15 +0530679 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
680 }
681 }
682 });
683 },
684
685 _get_args: function(item) {
686 var me = this;
687 return {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530688 "items": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530689 "customer": me.frm.doc.customer,
690 "customer_group": me.frm.doc.customer_group,
691 "territory": me.frm.doc.territory,
692 "supplier": me.frm.doc.supplier,
693 "supplier_type": me.frm.doc.supplier_type,
694 "currency": me.frm.doc.currency,
695 "conversion_rate": me.frm.doc.conversion_rate,
696 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Haitdd82bf22015-05-11 16:49:17 +0530697 "price_list_currency": me.frm.doc.price_list_currency,
Nabin Hait444f9562014-06-20 15:59:49 +0530698 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
699 "company": me.frm.doc.company,
700 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
701 "campaign": me.frm.doc.campaign,
702 "sales_partner": me.frm.doc.sales_partner,
703 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530704 "doctype": me.frm.doc.doctype,
705 "name": me.frm.doc.name
Nabin Hait444f9562014-06-20 15:59:49 +0530706 };
Nabin Hait444f9562014-06-20 15:59:49 +0530707 },
708
709 _get_item_list: function(item) {
710 var item_list = [];
711 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530712 if (d.item_code) {
713 item_list.push({
714 "doctype": d.doctype,
715 "name": d.name,
716 "item_code": d.item_code,
717 "item_group": d.item_group,
718 "brand": d.brand,
Anand Doshi39a28912016-02-15 11:42:18 +0530719 "qty": d.qty,
720 "parenttype": d.parenttype,
721 "parent": d.parent
Anand Doshi39edcbc2014-07-28 16:00:19 +0530722 });
mbauskarc79415e2016-01-18 16:32:24 +0530723
724 // if doctype is Quotation Item / Sales Order Iten then add Margin Type and rate in item_list
mbauskara52472c2016-03-05 15:10:25 +0530725 if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype){
726 item_list[0]["margin_type"] = d.margin_type
727 item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530728 }
Anand Doshi39edcbc2014-07-28 16:00:19 +0530729 }
Nabin Hait444f9562014-06-20 15:59:49 +0530730 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530731
Nabin Hait444f9562014-06-20 15:59:49 +0530732 if (item) {
733 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530734 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530735 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530736 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530737 });
738 }
Nabin Hait444f9562014-06-20 15:59:49 +0530739 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530740 },
741
Anand Doshidffec8f2014-07-01 17:45:15 +0530742 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530743 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530744 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530745 for(var i=0, l=children.length; i<l; i++) {
746 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530747 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530748
749 for(var k in d) {
750 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530751 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530752 if(k=="price_list_rate") {
753 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
754 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530755 frappe.model.set_value(d.doctype, d.name, k, v);
756 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530757 }
758
Nabin Hait49a27292014-12-17 11:21:32 +0530759 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530760 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530761 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
762 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530763 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530764
Anand Doshidea3fb02015-02-05 15:05:58 +0530765 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530766 },
767
Nabin Hait49a27292014-12-17 11:21:32 +0530768 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530769 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530770 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530771 if (!((args.items && args.items.length) || args.price_list)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530772 return;
773 }
Nabin Hait37b047d2015-02-23 16:01:33 +0530774
Anand Doshidffec8f2014-07-01 17:45:15 +0530775 return this.frm.call({
776 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530777 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530778 callback: function(r) {
779 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530780 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530781 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
782 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530783 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530784
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530785 if(args.items.length) {
Nabin Hait37b047d2015-02-23 16:01:33 +0530786 me._set_values_for_item_list(r.message.children);
787 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530788 }
789 }
790 });
791 },
792
Anand Doshi3543f302013-05-24 19:25:01 +0530793 get_item_wise_taxes_html: function() {
794 var item_tax = {};
795 var tax_accounts = [];
796 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530797
Nabin Hait96339342015-01-21 17:22:11 +0530798 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530799 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530800 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530801 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530802 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530803 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530804 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530805 var tax_rate = "";
806 if(tax_data[0] != null) {
807 tax_rate = (tax.charge_type === "Actual") ?
808 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
809 (flt(tax_data[0], tax_rate_precision) + "%");
810 }
811 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
812 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530813
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530814 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530815 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530816 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530817 }
Anand Doshi3543f302013-05-24 19:25:01 +0530818 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530819 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530820 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530821
822 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530823 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530824
Anand Doshi179e3772013-09-05 16:53:57 +0530825 var distinct_item_names = [];
826 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530827 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530828 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
829 distinct_item_names.push(item.item_code || item.item_name);
830 distinct_items.push(item);
831 }
832 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530833
Anand Doshi179e3772013-09-05 16:53:57 +0530834 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530835 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530836 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530837 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
838 item_name: item.item_name,
839 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530840 return item_tax_record[head[0]] ?
841 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530842 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530843 }).join("\n")
844 });
845 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530846
Anand Doshi53b73422013-05-31 11:50:01 +0530847 if(!rows) return "";
Rushabh Mehtaf24f6042015-08-20 18:01:40 +0530848 return '<p><a class="h6 text-muted" href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">'
849 + __("Show tax break-up") + '</a><br><br></p>\
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530850 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530851 <thead><tr>' + headings + '</tr></thead> \
852 <tbody>' + rows + '</tbody> \
853 </table></div>';
854 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530855
Nabin Hait139dc7b2014-02-12 14:53:18 +0530856 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530857 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530858 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530859
Nabin Hait0f231062014-02-20 11:27:47 +0530860 $.each(["company", "customer"], function(i, fieldname) {
Saurabh8a8ef852015-10-21 19:28:53 +0530861 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname && me.frm.doc.doctype != "Purchase Order")) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530862 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530863 msgprint(__("Please specify") + ": " +
864 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530865 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530866 valid = false;
867 }
Anand Doshi3543f302013-05-24 19:25:01 +0530868 }
869 });
870 return valid;
871 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530872
Anand Doshibf3e54a2013-06-05 14:11:32 +0530873 get_terms: function() {
874 var me = this;
875 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530876 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530877 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530878 args: {
879 doctype: "Terms and Conditions",
880 fieldname: "terms",
881 filters: { name: this.frm.doc.tc_name },
882 },
883 });
884 }
885 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530886
887 taxes_and_charges: function() {
888 var me = this;
889 if(this.frm.doc.taxes_and_charges) {
890 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530891 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
892 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530893 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
894 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530895 "master_name": this.frm.doc.taxes_and_charges,
Nabin Hait6b039142014-05-02 15:45:10 +0530896 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530897 callback: function(r) {
898 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530899 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530900 me.calculate_taxes_and_totals();
901 }
902 }
903 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530904 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530905 },
906
907 show_item_wise_taxes: function() {
908 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530909 var html = this.get_item_wise_taxes_html();
910 if (html) {
911 this.frm.toggle_display("other_charges_calculation", true);
912 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
913 } else {
914 this.frm.toggle_display("other_charges_calculation", false);
915 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530916 }
917 },
Anand Doshi13945092014-09-21 19:45:49 +0530918
919 is_recurring: function() {
920 // set default values for recurring documents
Rushabh Mehta756e5182016-03-03 13:00:17 +0530921 if(this.frm.doc.is_recurring && this.frm.doc.__islocal) {
922 frappe.msgprint(__("Please set recurring after saving"));
923 this.frm.set_value('is_recurring', 0);
924 return;
925 }
926
Anand Doshi13945092014-09-21 19:45:49 +0530927 if(this.frm.doc.is_recurring) {
Nabin Hait68a628e2016-03-21 12:03:16 +0530928 if(!this.frm.doc.recurring_id) {
929 this.frm.set_value('recurring_id', this.frm.doc.name);
930 }
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530931
Anand Doshi13945092014-09-21 19:45:49 +0530932 var owner_email = this.frm.doc.owner=="Administrator"
933 ? frappe.user_info("Administrator").email
934 : this.frm.doc.owner;
935
936 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
937 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
938 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
939 }
940
941 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
942 },
943
944 from_date: function() {
945 // set to_date
946 if(this.frm.doc.from_date) {
947 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
948 'Yearly': 12};
949
950 var months = recurring_type_map[this.frm.doc.recurring_type];
951 if(months) {
952 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
953 months);
954 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
955 refresh_field('to_date');
956 }
957 }
Saurabhd3135532016-02-25 18:59:20 +0530958 },
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530959
Saurabh2d7af632016-02-26 11:09:20 +0530960 set_gross_profit: function(item) {
Saurabh5ada14b2016-02-26 18:02:55 +0530961 if (this.frm.doc.doctype == "Sales Order" && item.valuation_rate) {
Saurabh3fbf3ce2016-03-09 15:31:04 +0530962 rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1);
963 item.gross_profit = flt(((rate - item.valuation_rate) * item.qty), precision("amount", item));
Saurabh2d7af632016-02-26 11:09:20 +0530964 }
Rushabh Mehta203cc962016-04-07 15:25:43 +0530965 },
966
967 setup_item_selector: function() {
Rushabh Mehtabc928242016-05-29 23:08:22 +0530968 // TODO: remove item selector
969
970 return;
Rushabh Mehta203cc962016-04-07 15:25:43 +0530971 if(!this.item_selector) {
972 this.item_selector = new erpnext.ItemSelector({frm: this.frm});
973 }
Nabin Hait28a05282016-06-27 17:41:39 +0530974 },
975
976 get_advances: function() {
977 if(!this.frm.is_return) {
978 return this.frm.call({
979 method: "set_advances",
980 doc: this.frm.doc,
981 callback: function(r, rt) {
982 refresh_field("advances");
983 }
984 })
985 }
Nabin Hait05aefbb2016-06-28 19:42:19 +0530986 },
987
988 make_payment_entry: function() {
989 return frappe.call({
Nabin Hait13093b42016-06-29 18:04:37 +0530990 method: "erpnext.accounts.doctype.payment_entry.payment_entry.get_payment_entry",
Nabin Hait05aefbb2016-06-28 19:42:19 +0530991 args: {
992 "dt": cur_frm.doc.doctype,
993 "dn": cur_frm.doc.name
994 },
995 callback: function(r) {
996 var doclist = frappe.model.sync(r.message);
997 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
998 }
999 });
Anand Doshi13945092014-09-21 19:45:49 +05301000 }
Rushabh Mehtabe2ee182016-04-29 17:22:42 +05301001});