blob: 58c68e8d7fe28eef3713d5a670d30954ad6f7764 [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
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05304frappe.provide("erpnext");
Nabin Haitb4a3dfa2015-01-22 17:13:13 +05305frappe.require("assets/erpnext/js/controllers/taxes_and_totals.js");
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +05306frappe.require("assets/erpnext/js/utils.js");
7
Nabin Haitb4a3dfa2015-01-22 17:13:13 +05308erpnext.TransactionController = erpnext.taxes_and_totals.extend({
Anand Doshi3543f302013-05-24 19:25:01 +05309 onload: function() {
Anand Doshi1e4fb682013-08-23 12:56:33 +053010 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +053011 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053012 var today = get_today(),
Nabin Hait73083dc2014-03-14 15:10:42 +053013 currency = frappe.defaults.get_user_default("currency");
Rushabh Mehtaff938022014-04-15 18:40:00 +053014
Anand Doshi87da6622015-10-19 14:59:32 +053015 $.each(["posting_date", "transaction_date"], function(i, fieldname) {
16 if (me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname] && me.frm[fieldname]) {
17 me.frm.set_value(fieldname, me.frm[fieldname]);
18 }
19 });
20
Anand Doshi3543f302013-05-24 19:25:01 +053021 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053022 posting_date: today,
Anand Doshifc777182013-05-27 19:29:07 +053023 transaction_date: today,
24 currency: currency,
25 price_list_currency: currency,
26 status: "Draft",
Anand Doshifc777182013-05-27 19:29:07 +053027 is_subcontracted: "No",
Anand Doshi3543f302013-05-24 19:25:01 +053028 }, function(fieldname, value) {
29 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
30 me.frm.set_value(fieldname, value);
Rushabh Mehtaff938022014-04-15 18:40:00 +053031 });
nabinhaitbc408d82014-07-07 16:51:52 +053032
nabinhaitb7392642014-07-28 14:55:08 +053033 if(this.frm.doc.company && !this.frm.doc.amended_from) {
nabinhaitbc408d82014-07-07 16:51:52 +053034 cur_frm.script_manager.trigger("company");
35 }
Anand Doshi3543f302013-05-24 19:25:01 +053036 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +053037
Nabin Haitdd38a262014-12-26 13:15:21 +053038 if(this.frm.fields_dict["taxes"]) {
39 this["taxes_remove"] = this.calculate_taxes_and_totals;
Anand Doshia91c95f2013-08-23 13:00:47 +053040 }
Rushabh Mehtaff938022014-04-15 18:40:00 +053041
Nabin Haitdd38a262014-12-26 13:15:21 +053042 if(this.frm.fields_dict["items"]) {
43 this["items_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +053044 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +053045
Neil Trini Lasradoe2d8dd02015-06-22 19:06:15 +053046 if(this.frm.fields_dict["recurring_print_format"]) {
47 this.frm.set_query("recurring_print_format", function(doc) {
48 return{
49 filters: [
50 ['Print Format', 'doc_type', '=', cur_frm.doctype],
51 ]
52 }
53 });
54 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +053055
Nabin Hait1d218422015-07-17 15:19:02 +053056 if(this.frm.fields_dict["return_against"]) {
57 this.frm.set_query("return_against", function(doc) {
58 var filters = {
59 "docstatus": 1,
60 "is_return": 0,
61 "company": doc.company
62 };
63 if (me.frm.fields_dict["customer"] && doc.customer) filters["customer"] = doc.customer;
64 if (me.frm.fields_dict["supplier"] && doc.supplier) filters["supplier"] = doc.supplier;
Rushabh Mehtaa208c562015-08-03 13:18:54 +053065
Nabin Hait1d218422015-07-17 15:19:02 +053066 return {
67 filters: filters
68 }
69 });
70 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +053071
Anand Doshi3543f302013-05-24 19:25:01 +053072 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053073
Anand Doshi5e4e5d72013-08-06 17:23:44 +053074 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053075 var me = this;
Nabin Haitffc7f3f2015-05-12 15:07:02 +053076 if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length
77 && !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) {
78 this.apply_default_taxes();
Anand Doshi70f57eb2015-11-27 14:37:40 +053079 } else if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"]
Nabin Haitbdfb0702015-10-28 15:38:08 +053080 && !this.frm.doc.is_pos) {
81 me.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +053082 }
Nabin Haitdd38a262014-12-26 13:15:21 +053083 if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) {
84 cur_frm.get_field("items").grid.set_multiple_add("item_code", "qty");
Rushabh Mehtab6843f22014-06-04 13:10:41 +053085 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053086 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053087
Anand Doshi3543f302013-05-24 19:25:01 +053088 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +053089 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053090 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053091 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053092 this.set_dynamic_labels();
Anand Doshi50d7e8c2015-01-06 17:14:13 +053093 erpnext.pos.make_pos_btn(this.frm);
Rushabh Mehta614e7ab2015-02-18 19:51:48 +053094 this.setup_sms();
Anand Doshife13bfe2015-08-25 12:49:40 +053095 this.make_show_payments_btn();
Rushabh Mehta614e7ab2015-02-18 19:51:48 +053096 },
Rushabh Mehta4a3c8052015-05-19 11:10:12 +053097
Nabin Haitffc7f3f2015-05-12 15:07:02 +053098 apply_default_taxes: function() {
99 var me = this;
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530100 var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
101 me.frm.doc.name);
102
103 if(taxes_and_charges_field) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530104 return frappe.call({
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530105 method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges",
106 args: {
107 "master_doctype": taxes_and_charges_field.options
108 },
109 callback: function(r) {
110 if(!r.exc) {
111 me.frm.set_value("taxes", r.message);
Nabin Haitbdfb0702015-10-28 15:38:08 +0530112 me.calculate_taxes_and_totals();
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530113 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530114 }
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530115 });
116 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530117 },
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530118
119 setup_sms: function() {
120 var me = this;
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530121 if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped"], this.frm.doc.status)
122 && this.frm.doctype != "Purchase Invoice") {
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530123 this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); });
124 }
125 },
126
127 send_sms: function() {
128 frappe.require("assets/erpnext/js/sms_manager.js");
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530129 var sms_man = new SMSManager(this.frm.doc);
Nabin Hait239c2c32015-01-15 14:21:41 +0530130 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530131
Anand Doshife13bfe2015-08-25 12:49:40 +0530132 make_show_payments_btn: function() {
133 var me = this;
134 if (in_list(["Purchase Invoice", "Sales Invoice"], this.frm.doctype)) {
135 if(this.frm.doc.outstanding_amount !== this.frm.doc.base_grand_total) {
Saurabhe23e99f2016-01-06 15:34:56 +0530136 this.frm.add_custom_button(__("Payments"), function() {
Anand Doshife13bfe2015-08-25 12:49:40 +0530137 frappe.route_options = {
138 "Journal Entry Account.reference_type": me.frm.doc.doctype,
139 "Journal Entry Account.reference_name": me.frm.doc.name
140 };
141
142 frappe.set_route("List", "Journal Entry");
Saurabhe23e99f2016-01-06 15:34:56 +0530143 }, __("View"));
Anand Doshife13bfe2015-08-25 12:49:40 +0530144 }
Nabin Hait239c2c32015-01-15 14:21:41 +0530145 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530146 },
147
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530148 barcode: function(doc, cdt, cdn) {
149 var d = locals[cdt][cdn];
150 if(d.barcode=="" || d.barcode==null) {
151 // barcode cleared, remove item
152 d.item_code = "";
153 }
154 this.item_code(doc, cdt, cdn);
155 },
156
Nabin Hait139dc7b2014-02-12 14:53:18 +0530157 item_code: function(doc, cdt, cdn) {
158 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530159 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530160 if(item.item_code || item.barcode || item.serial_no) {
161 if(!this.validate_company_and_party()) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530162 cur_frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +0530163 } else {
164 return this.frm.call({
165 method: "erpnext.stock.get_item_details.get_item_details",
166 child: item,
167 args: {
168 args: {
169 item_code: item.item_code,
170 barcode: item.barcode,
171 serial_no: item.serial_no,
172 warehouse: item.warehouse,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530173 customer: me.frm.doc.customer,
174 supplier: me.frm.doc.supplier,
175 currency: me.frm.doc.currency,
176 conversion_rate: me.frm.doc.conversion_rate,
177 price_list: me.frm.doc.selling_price_list ||
178 me.frm.doc.buying_price_list,
179 price_list_currency: me.frm.doc.price_list_currency,
180 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
181 company: me.frm.doc.company,
182 order_type: me.frm.doc.order_type,
183 is_pos: cint(me.frm.doc.is_pos),
184 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Haita1bf43b2015-03-17 10:50:47 +0530185 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
Nabin Hait444f9562014-06-20 15:59:49 +0530186 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
Anand Doshiafe85bd2016-01-21 21:04:16 +0530187 doctype: me.frm.doc.doctype,
188 name: me.frm.doc.name,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530189 project_name: item.project_name || me.frm.doc.project_name,
190 qty: item.qty
Nabin Hait139dc7b2014-02-12 14:53:18 +0530191 }
192 },
Anand Doshi13945092014-09-21 19:45:49 +0530193
Nabin Hait139dc7b2014-02-12 14:53:18 +0530194 callback: function(r) {
195 if(!r.exc) {
196 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
197 }
198 }
199 });
200 }
201 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530202 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530203
204 serial_no: function(doc, cdt, cdn) {
205 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530206 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530207
208 if (item.serial_no) {
209 if (!item.item_code) {
210 this.frm.script_manager.trigger("item_code", cdt, cdn);
211 }
212 else {
213 var sr_no = [];
214
215 // Replacing all occurences of comma with carriage return
216 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
217
218 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530219
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530220 // Trim each string and push unique string to new list
221 for (var x=0; x<=serial_nos.length - 1; x++) {
222 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
223 sr_no.push(serial_nos[x].trim());
224 }
225 }
226
227 // Add the new list to the serial no. field in grid with each in new line
228 item.serial_no = "";
229 for (var x=0; x<=sr_no.length - 1; x++)
230 item.serial_no += sr_no[x] + '\n';
231
232 refresh_field("serial_no", item.name, item.parentfield);
Nabin Haitb7c0c552015-08-04 12:18:12 +0530233 if(!doc.is_return) {
234 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
235 }
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530236 }
237 }
238 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530239
Anand Doshi923d41d2013-05-28 17:23:36 +0530240 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530241 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530242 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530243
Anand Doshi3543f302013-05-24 19:25:01 +0530244 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530245 var me = this;
Nabin Haita731ad42016-02-04 17:21:28 +0530246 var set_pricing = function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530247 if(me.frm.doc.company && me.frm.fields_dict.currency) {
248 var company_currency = me.get_company_currency();
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530249 var company_doc = frappe.get_doc(":Company", me.frm.doc.company);
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530250 if (!me.frm.doc.currency) {
251 me.frm.set_value("currency", company_currency);
252 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530253
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530254 if (me.frm.doc.currency == company_currency) {
255 me.frm.set_value("conversion_rate", 1.0);
256 }
257 if (me.frm.doc.price_list_currency == company_currency) {
258 me.frm.set_value('plc_conversion_rate', 1.0);
259 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530260 if (company_doc.default_letter_head) {
Rushabh Mehtac1857342015-05-27 12:29:57 +0530261 if(me.frm.fields_dict.letter_head) {
262 me.frm.set_value("letter_head", company_doc.default_letter_head);
263 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530264 }
Rushabh Mehtac1857342015-05-27 12:29:57 +0530265 if (company_doc.default_terms && me.frm.doc.doctype != "Purchase Invoice") {
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530266 me.frm.set_value("tc_name", company_doc.default_terms);
267 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530268
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530269 me.frm.script_manager.trigger("currency");
270 me.apply_pricing_rule();
Nabin Haitdd38a262014-12-26 13:15:21 +0530271 }
Anand Doshi3543f302013-05-24 19:25:01 +0530272 }
Nabin Haita731ad42016-02-04 17:21:28 +0530273
274 var set_party_account = function(set_pricing) {
275 if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) {
276 if(me.frm.doc.doctype=="Sales Invoice") {
277 var party_type = "Customer";
278 var party_account_field = 'debit_to';
279 } else {
280 var party_type = "Supplier";
281 var party_account_field = 'credit_to';
282 }
283
284 return frappe.call({
285 method: "erpnext.accounts.party.get_party_account",
286 args: {
287 company: me.frm.doc.company,
288 party_type: party_type,
289 party: me.frm.doc[frappe.model.scrub(party_type)]
290 },
291 callback: function(r) {
292 if(!r.exc && r.message) {
293 me.frm.set_value(party_account_field, r.message);
294 set_pricing();
295 }
296 }
297 });
298 } else {
299 set_pricing();
300 }
301
302 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530303
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530304 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
305 else var date = this.frm.doc.transaction_date;
Nabin Haita731ad42016-02-04 17:21:28 +0530306 erpnext.get_fiscal_year(this.frm.doc.company, date, function() { set_party_account(set_pricing); });
Rushabh Mehta45ca8a42015-04-28 16:02:09 +0530307
308 if(this.frm.doc.company) {
309 erpnext.last_selected_company = this.frm.doc.company;
310 }
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530311 },
312
313 transaction_date: function() {
Anand Doshi87da6622015-10-19 14:59:32 +0530314 if (this.frm.doc.transaction_date) {
315 this.frm.transaction_date = this.frm.doc.transaction_date;
316 }
317
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530318 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.transaction_date);
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530319 },
320
321 posting_date: function() {
Nabin Hait40e92b62015-07-09 17:12:23 +0530322 var me = this;
323 if (this.frm.doc.posting_date) {
Anand Doshi87da6622015-10-19 14:59:32 +0530324 this.frm.posting_date = this.frm.doc.posting_date;
325
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530326 if ((this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer) ||
Nabin Hait40e92b62015-07-09 17:12:23 +0530327 (this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier)) {
328 return frappe.call({
329 method: "erpnext.accounts.party.get_due_date",
330 args: {
331 "posting_date": me.frm.doc.posting_date,
332 "party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier",
333 "party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier,
334 "company": me.frm.doc.company
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530335 },
Nabin Hait40e92b62015-07-09 17:12:23 +0530336 callback: function(r, rt) {
337 if(r.message) {
338 me.frm.set_value("due_date", r.message);
339 }
340 erpnext.get_fiscal_year(me.frm.doc.company, me.frm.doc.posting_date);
341 }
342 })
343 } else {
344 erpnext.get_fiscal_year(me.frm.doc.company, me.frm.doc.posting_date);
345 }
346 }
Anand Doshi3543f302013-05-24 19:25:01 +0530347 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530348
Anand Doshi3543f302013-05-24 19:25:01 +0530349 get_company_currency: function() {
350 return erpnext.get_currency(this.frm.doc.company);
351 },
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530352
Neil Trini Lasrado09a66c42015-07-07 16:41:37 +0530353 contact_person: function() {
354 erpnext.utils.get_contact_details(this.frm);
355 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530356
Anand Doshi3543f302013-05-24 19:25:01 +0530357 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530358 var me = this;
359 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530360
Anand Doshi61a2f682013-06-21 17:55:31 +0530361 var company_currency = this.get_company_currency();
Nabin Hait69cdf592015-05-02 18:46:10 +0530362 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
Pratik Vyas1a84d8e2015-05-19 12:23:52 +0530363 if(this.frm.doc.currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530364 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530365 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530366 me.frm.set_value("conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530367 });
368 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530369 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530370 }
371 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530372
Anand Doshi61a2f682013-06-21 17:55:31 +0530373 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530374 if(this.frm.doc.currency === this.get_company_currency()) {
375 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530376 }
377 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530378 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
379 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
380 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530381
Nabin Haita3dd72a2014-05-28 12:49:20 +0530382 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530383 if(this.frm.doc.ignore_pricing_rule) {
384 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530385 } else if (!this.in_apply_price_list){
386 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530387 }
388
Nabin Haita3dd72a2014-05-28 12:49:20 +0530389 }
Anand Doshi3543f302013-05-24 19:25:01 +0530390 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530391
Anand Doshi61a2f682013-06-21 17:55:31 +0530392 get_exchange_rate: function(from_currency, to_currency, callback) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530393 return frappe.call({
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530394 method: "erpnext.setup.utils.get_exchange_rate",
395 args: {
396 from_currency: from_currency,
397 to_currency: to_currency
398 },
399 callback: function(r) {
400 callback(flt(r.message));
401 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530402 });
403 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530404
Anand Doshi3543f302013-05-24 19:25:01 +0530405 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530406 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530407 this.set_dynamic_labels();
Nabin Haitec361ea2015-05-11 18:14:45 +0530408
409 var company_currency = this.get_company_currency();
410 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
411 if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
412 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
413 function(exchange_rate) {
414 me.frm.set_value("plc_conversion_rate", exchange_rate);
415 });
416 } else {
417 this.plc_conversion_rate();
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 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530422 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
423 this.frm.set_value("plc_conversion_rate", 1.0);
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530424 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency
Nabin Hait1d218422015-07-17 15:19:02 +0530425 && this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 &&
Nabin Haitec361ea2015-05-11 18:14:45 +0530426 cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
427 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530428 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530429
Nabin Haitec361ea2015-05-11 18:14:45 +0530430 if(!this.in_apply_price_list) {
431 this.apply_price_list();
Anand Doshi61a2f682013-06-21 17:55:31 +0530432 }
Anand Doshi3543f302013-05-24 19:25:01 +0530433 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530434
Anand Doshi3543f302013-05-24 19:25:01 +0530435 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530436 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530437 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530438
Anand Doshi61a2f682013-06-21 17:55:31 +0530439 set_dynamic_labels: function() {
440 // What TODO? should we make price list system non-mandatory?
441 this.frm.toggle_reqd("plc_conversion_rate",
442 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530443
Anand Doshi61a2f682013-06-21 17:55:31 +0530444 var company_currency = this.get_company_currency();
445 this.change_form_labels(company_currency);
446 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530447 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530448 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530449
Nabin Hait613d0812015-02-23 11:58:15 +0530450 change_form_labels: function(company_currency) {
451 var me = this;
452 var field_label_map = {};
453
454 var setup_field_label_map = function(fields_list, currency) {
455 $.each(fields_list, function(i, fname) {
456 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
457 if(docfield) {
458 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
459 field_label_map[fname] = label.trim() + " (" + currency + ")";
460 }
461 });
462 };
463 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
464 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
465 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530466 "base_paid_amount", "base_write_off_amount"
467 ], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530468
469 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
470 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530471 "rounded_total", "in_words", "paid_amount", "write_off_amount"], this.frm.doc.currency);
Anand Doshi87da6622015-10-19 14:59:32 +0530472
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530473 setup_field_label_map(["outstanding_amount", "total_advance"], this.frm.doc.party_account_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530474
475 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
476 + " = [?] " + company_currency)
477
478 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
479 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
480 + " = [?] " + company_currency)
481 }
482
483 // toggle fields
484 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
485 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530486 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount",
487 "base_paid_amount", "base_write_off_amount"],
Nabin Hait613d0812015-02-23 11:58:15 +0530488 this.frm.doc.currency != company_currency);
489
490 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
491 this.frm.doc.price_list_currency != company_currency);
492
493 // set labels
494 $.each(field_label_map, function(fname, label) {
495 me.frm.fields_dict[fname].set_label(label);
496 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530497
Nabin Hait903c42a2015-02-24 15:24:49 +0530498 var show =cint(cur_frm.doc.discount_amount) ||
499 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
500
501 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
502 cur_frm.toggle_display("net_total", show);
503
504 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
505 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
506
Nabin Hait613d0812015-02-23 11:58:15 +0530507 },
508
509 change_grid_labels: function(company_currency) {
510 var me = this;
511 var field_label_map = {};
512
513 var setup_field_label_map = function(fields_list, currency, parentfield) {
514 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
515 $.each(fields_list, function(i, fname) {
516 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
517 if(docfield) {
518 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
519 field_label_map[grid_doctype + "-" + fname] =
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530520 label.trim() + " (" + __(currency) + ")";
Nabin Hait613d0812015-02-23 11:58:15 +0530521 }
522 });
523 }
524
525 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
526 company_currency, "items");
527
528 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
529 this.frm.doc.currency, "items");
530
531 if(this.frm.fields_dict["taxes"]) {
532 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
533
534 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
535 }
536
537 if(this.frm.fields_dict["advances"]) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530538 setup_field_label_map(["advance_amount", "allocated_amount"],
Nabin Haitaa015902015-10-16 13:08:33 +0530539 this.frm.doc.party_account_currency, "advances");
Nabin Hait613d0812015-02-23 11:58:15 +0530540 }
541
542 // toggle columns
543 var item_grid = this.frm.fields_dict["items"].grid;
544 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
545 if(frappe.meta.get_docfield(item_grid.doctype, fname))
546 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
547 });
548
549 var show = (cint(cur_frm.doc.discount_amount)) ||
550 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
551
552 $.each(["net_rate", "net_amount"], function(i, fname) {
553 if(frappe.meta.get_docfield(item_grid.doctype, fname))
554 item_grid.set_column_disp(fname, show);
555 });
556
557 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
558 if(frappe.meta.get_docfield(item_grid.doctype, fname))
559 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
560 });
561
562 // set labels
563 var $wrapper = $(this.frm.wrapper);
564 $.each(field_label_map, function(fname, label) {
565 fname = fname.split("-");
566 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
567 if(df) df.label = label;
568 });
569 },
570
Anand Doshi923d41d2013-05-28 17:23:36 +0530571 recalculate: function() {
572 this.calculate_taxes_and_totals();
573 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530574
Anand Doshi923d41d2013-05-28 17:23:36 +0530575 recalculate_values: function() {
576 this.calculate_taxes_and_totals();
577 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530578
Anand Doshid0b00722013-05-28 18:54:48 +0530579 calculate_charges: function() {
580 this.calculate_taxes_and_totals();
581 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530582
Nabin Hait444f9562014-06-20 15:59:49 +0530583 ignore_pricing_rule: function() {
584 this.apply_pricing_rule();
585 },
586
587 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530588 var me = this;
Anand Doshi70f57eb2015-11-27 14:37:40 +0530589 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530590 if (!(args.items && args.items.length)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530591 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
592 return;
593 }
594
Anand Doshidffec8f2014-07-01 17:45:15 +0530595 return this.frm.call({
596 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
Anand Doshi70f57eb2015-11-27 14:37:40 +0530597 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530598 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530599 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530600 me._set_values_for_item_list(r.message);
601 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
602 }
603 }
604 });
605 },
606
607 _get_args: function(item) {
608 var me = this;
609 return {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530610 "items": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530611 "customer": me.frm.doc.customer,
612 "customer_group": me.frm.doc.customer_group,
613 "territory": me.frm.doc.territory,
614 "supplier": me.frm.doc.supplier,
615 "supplier_type": me.frm.doc.supplier_type,
616 "currency": me.frm.doc.currency,
617 "conversion_rate": me.frm.doc.conversion_rate,
618 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Haitdd82bf22015-05-11 16:49:17 +0530619 "price_list_currency": me.frm.doc.price_list_currency,
Nabin Hait444f9562014-06-20 15:59:49 +0530620 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
621 "company": me.frm.doc.company,
622 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
623 "campaign": me.frm.doc.campaign,
624 "sales_partner": me.frm.doc.sales_partner,
625 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530626 "doctype": me.frm.doc.doctype,
627 "name": me.frm.doc.name
Nabin Hait444f9562014-06-20 15:59:49 +0530628 };
Nabin Hait444f9562014-06-20 15:59:49 +0530629 },
630
631 _get_item_list: function(item) {
632 var item_list = [];
633 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530634 if (d.item_code) {
635 item_list.push({
636 "doctype": d.doctype,
637 "name": d.name,
638 "item_code": d.item_code,
639 "item_group": d.item_group,
640 "brand": d.brand,
641 "qty": d.qty
642 });
643 }
Nabin Hait444f9562014-06-20 15:59:49 +0530644 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530645
Nabin Hait444f9562014-06-20 15:59:49 +0530646 if (item) {
647 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530648 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530649 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530650 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530651 });
652 }
Nabin Hait444f9562014-06-20 15:59:49 +0530653 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530654 },
655
Anand Doshidffec8f2014-07-01 17:45:15 +0530656 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530657 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530658 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530659 for(var i=0, l=children.length; i<l; i++) {
660 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530661 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530662
663 for(var k in d) {
664 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530665 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530666 if(k=="price_list_rate") {
667 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
668 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530669 frappe.model.set_value(d.doctype, d.name, k, v);
670 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530671 }
672
Nabin Hait49a27292014-12-17 11:21:32 +0530673 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530674 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530675 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
676 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530677 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530678
Anand Doshidea3fb02015-02-05 15:05:58 +0530679 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530680 },
681
Nabin Hait49a27292014-12-17 11:21:32 +0530682 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530683 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530684 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530685 if (!((args.items && args.items.length) || args.price_list)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530686 return;
687 }
Nabin Hait37b047d2015-02-23 16:01:33 +0530688
Anand Doshidffec8f2014-07-01 17:45:15 +0530689 return this.frm.call({
690 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530691 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530692 callback: function(r) {
693 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530694 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530695 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
696 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530697 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530698
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530699 if(args.items.length) {
Nabin Hait37b047d2015-02-23 16:01:33 +0530700 me._set_values_for_item_list(r.message.children);
701 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530702 }
703 }
704 });
705 },
706
Anand Doshi3543f302013-05-24 19:25:01 +0530707 get_item_wise_taxes_html: function() {
708 var item_tax = {};
709 var tax_accounts = [];
710 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530711
Nabin Hait96339342015-01-21 17:22:11 +0530712 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530713 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530714 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530715 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530716 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530717 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530718 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530719 var tax_rate = "";
720 if(tax_data[0] != null) {
721 tax_rate = (tax.charge_type === "Actual") ?
722 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
723 (flt(tax_data[0], tax_rate_precision) + "%");
724 }
725 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
726 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530727
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530728 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530729 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530730 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530731 }
Anand Doshi3543f302013-05-24 19:25:01 +0530732 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530733 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530734 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530735
736 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530737 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530738
Anand Doshi179e3772013-09-05 16:53:57 +0530739 var distinct_item_names = [];
740 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530741 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530742 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
743 distinct_item_names.push(item.item_code || item.item_name);
744 distinct_items.push(item);
745 }
746 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530747
Anand Doshi179e3772013-09-05 16:53:57 +0530748 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530749 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530750 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530751 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
752 item_name: item.item_name,
753 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530754 return item_tax_record[head[0]] ?
755 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530756 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530757 }).join("\n")
758 });
759 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530760
Anand Doshi53b73422013-05-31 11:50:01 +0530761 if(!rows) return "";
Rushabh Mehtaf24f6042015-08-20 18:01:40 +0530762 return '<p><a class="h6 text-muted" href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">'
763 + __("Show tax break-up") + '</a><br><br></p>\
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530764 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530765 <thead><tr>' + headings + '</tr></thead> \
766 <tbody>' + rows + '</tbody> \
767 </table></div>';
768 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530769
Nabin Hait139dc7b2014-02-12 14:53:18 +0530770 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530771 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530772 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530773
Nabin Hait0f231062014-02-20 11:27:47 +0530774 $.each(["company", "customer"], function(i, fieldname) {
Saurabh8a8ef852015-10-21 19:28:53 +0530775 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname && me.frm.doc.doctype != "Purchase Order")) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530776 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530777 msgprint(__("Please specify") + ": " +
778 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530779 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530780 valid = false;
781 }
Anand Doshi3543f302013-05-24 19:25:01 +0530782 }
783 });
784 return valid;
785 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530786
Anand Doshibf3e54a2013-06-05 14:11:32 +0530787 get_terms: function() {
788 var me = this;
789 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530790 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530791 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530792 args: {
793 doctype: "Terms and Conditions",
794 fieldname: "terms",
795 filters: { name: this.frm.doc.tc_name },
796 },
797 });
798 }
799 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530800
801 taxes_and_charges: function() {
802 var me = this;
803 if(this.frm.doc.taxes_and_charges) {
804 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530805 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
806 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530807 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
808 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530809 "master_name": this.frm.doc.taxes_and_charges,
Nabin Hait6b039142014-05-02 15:45:10 +0530810 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530811 callback: function(r) {
812 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530813 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530814 me.calculate_taxes_and_totals();
815 }
816 }
817 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530818 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530819 },
820
821 show_item_wise_taxes: function() {
822 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530823 var html = this.get_item_wise_taxes_html();
824 if (html) {
825 this.frm.toggle_display("other_charges_calculation", true);
826 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
827 } else {
828 this.frm.toggle_display("other_charges_calculation", false);
829 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530830 }
831 },
Anand Doshi13945092014-09-21 19:45:49 +0530832
833 is_recurring: function() {
834 // set default values for recurring documents
835 if(this.frm.doc.is_recurring) {
836 var owner_email = this.frm.doc.owner=="Administrator"
837 ? frappe.user_info("Administrator").email
838 : this.frm.doc.owner;
839
840 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
841 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
842 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
843 }
844
845 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
846 },
847
848 from_date: function() {
849 // set to_date
850 if(this.frm.doc.from_date) {
851 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
852 'Yearly': 12};
853
854 var months = recurring_type_map[this.frm.doc.recurring_type];
855 if(months) {
856 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
857 months);
858 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
859 refresh_field('to_date');
860 }
861 }
862 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530863});
Nabin Haitce245122015-02-22 20:14:49 +0530864
Nabin Hait93b3a372015-02-24 17:08:34 +0530865frappe.ui.form.on(cur_frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530866 var item = frappe.get_doc(cdt, cdn);
867 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
868
869 if(item.price_list_rate) {
870 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item));
871 } else {
872 item.discount_percentage = 0.0;
873 }
874
875 cur_frm.cscript.calculate_taxes_and_totals();
876})
Nabin Hait613d0812015-02-23 11:58:15 +0530877
878frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
879 cur_frm.cscript.calculate_taxes_and_totals();
880})
881
882frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
883 cur_frm.cscript.calculate_taxes_and_totals();
884})
885
886frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
887 cur_frm.cscript.calculate_taxes_and_totals();
888})
889
890frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530891 cur_frm.cscript.set_dynamic_labels();
Nabin Hait613d0812015-02-23 11:58:15 +0530892 cur_frm.cscript.calculate_taxes_and_totals();
893})
Nabin Hait37b047d2015-02-23 16:01:33 +0530894
895frappe.ui.form.on(cur_frm.doctype, "apply_discount_on", function(frm) {
Nabin Hait3769d872015-12-18 13:12:02 +0530896 if(frm.doc.additional_discount_percentage) {
897 frm.trigger("additional_discount_percentage");
898 } else {
899 cur_frm.cscript.calculate_taxes_and_totals();
900 }
Nabin Hait37b047d2015-02-23 16:01:33 +0530901})
902
Nabin Hait3769d872015-12-18 13:12:02 +0530903frappe.ui.form.on(cur_frm.doctype, "additional_discount_percentage", function(frm) {
904 if (frm.via_discount_amount) {
905 return;
906 }
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530907
Nabin Hait3769d872015-12-18 13:12:02 +0530908 if(!frm.doc.apply_discount_on) {
909 frappe.msgprint(__("Please set 'Apply Additional Discount On'"));
910 return
911 }
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530912
Nabin Hait3769d872015-12-18 13:12:02 +0530913 frm.via_discount_percentage = true;
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530914
Nabin Hait3769d872015-12-18 13:12:02 +0530915 if(frm.doc.additional_discount_percentage && frm.doc.discount_amount) {
916 // Reset discount amount and net / grand total
917 frm.set_value("discount_amount", 0);
918 }
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530919
Nabin Hait3769d872015-12-18 13:12:02 +0530920 var total = flt(frm.doc[frappe.model.scrub(frm.doc.apply_discount_on)]);
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530921 var discount_amount = flt(total*flt(frm.doc.additional_discount_percentage) / 100,
Nabin Hait3769d872015-12-18 13:12:02 +0530922 precision("discount_amount"));
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530923
Nabin Hait3769d872015-12-18 13:12:02 +0530924 frm.set_value("discount_amount", discount_amount);
925 delete frm.via_discount_percentage;
926});
927
Nabin Hait37b047d2015-02-23 16:01:33 +0530928frappe.ui.form.on(cur_frm.doctype, "discount_amount", function(frm) {
Nabin Hait3769d872015-12-18 13:12:02 +0530929 frm.cscript.set_dynamic_labels();
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530930
Nabin Hait3769d872015-12-18 13:12:02 +0530931 if (!frm.via_discount_percentage) {
932 frm.via_discount_amount = true;
933 frm.set_value("additional_discount_percentage", 0);
934 delete frm.via_discount_amount;
935 }
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530936
Nabin Hait3769d872015-12-18 13:12:02 +0530937 frm.cscript.calculate_taxes_and_totals();
938});
Neil Trini Lasradoe2d8dd02015-06-22 19:06:15 +0530939
940