Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | // License: GNU General Public License v3. See license.txt |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 3 | frappe.provide("erpnext"); |
Nabin Hait | b072c74 | 2014-12-16 12:49:13 +0530 | [diff] [blame] | 4 | frappe.provide("erpnext.utils"); |
Anand Doshi | e65e621 | 2012-11-19 15:43:18 +0530 | [diff] [blame] | 5 | |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 6 | $.extend(erpnext, { |
| 7 | get_currency: function(company) { |
| 8 | if(!company && cur_frm) |
| 9 | company = cur_frm.doc.company; |
| 10 | if(company) |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 11 | return frappe.get_doc(":Company", company).default_currency || frappe.boot.sysdefaults.currency; |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 12 | else |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 13 | return frappe.boot.sysdefaults.currency; |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 14 | }, |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 15 | |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 16 | get_fiscal_year: function(company, date, fn) { |
Neil Trini Lasrado | 78fa695 | 2014-10-03 17:43:02 +0530 | [diff] [blame] | 17 | frappe.call({ |
| 18 | type:"GET", |
| 19 | method: "erpnext.accounts.utils.get_fiscal_year", |
| 20 | args: { |
Neil Trini Lasrado | e6b14a4 | 2014-10-07 12:50:17 +0530 | [diff] [blame] | 21 | "company": company, |
| 22 | "date": date, |
Neil Trini Lasrado | 0866272 | 2015-01-12 16:41:46 +0530 | [diff] [blame] | 23 | "verbose": 0 |
Neil Trini Lasrado | 78fa695 | 2014-10-03 17:43:02 +0530 | [diff] [blame] | 24 | }, |
| 25 | callback: function(r) { |
Neil Trini Lasrado | 565d986 | 2014-10-07 18:26:39 +0530 | [diff] [blame] | 26 | if (r.message) cur_frm.set_value("fiscal_year", r.message[0]); |
Neil Trini Lasrado | eaae58f | 2014-10-09 15:00:10 +0530 | [diff] [blame] | 27 | if (fn) fn(); |
Neil Trini Lasrado | 78fa695 | 2014-10-03 17:43:02 +0530 | [diff] [blame] | 28 | } |
| 29 | }); |
| 30 | }, |
| 31 | |
Anand Doshi | 8bde7f9 | 2014-04-24 18:11:49 +0530 | [diff] [blame] | 32 | toggle_naming_series: function() { |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 33 | if(cur_frm.fields_dict.naming_series) { |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 34 | cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false); |
| 35 | } |
| 36 | }, |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 37 | |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 38 | hide_company: function() { |
| 39 | if(cur_frm.fields_dict.company) { |
Rushabh Mehta | 8941841 | 2013-10-07 18:22:29 +0530 | [diff] [blame] | 40 | var companies = Object.keys(locals[":Company"] || {}); |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 41 | if(companies.length === 1) { |
| 42 | if(!cur_frm.doc.company) cur_frm.set_value("company", companies[0]); |
| 43 | cur_frm.toggle_display("company", false); |
| 44 | } |
| 45 | } |
Anand Doshi | 1ffc5b5 | 2013-07-15 18:09:43 +0530 | [diff] [blame] | 46 | }, |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 47 | |
Akhilesh Darjee | e158cea | 2013-10-31 19:50:51 +0530 | [diff] [blame] | 48 | add_applicable_territory: function() { |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 49 | if(cur_frm.doc.__islocal && (cur_frm.doc.territories || []).length===0) { |
Nabin Hait | 73083dc | 2014-03-14 15:10:42 +0530 | [diff] [blame] | 50 | var default_territory = frappe.defaults.get_user_default("territory"); |
Nabin Hait | 05b28eb | 2014-02-19 11:04:32 +0530 | [diff] [blame] | 51 | if(default_territory) { |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 52 | var territory = frappe.model.add_child(cur_frm.doc, "Applicable Territory", |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 53 | "territories"); |
Nabin Hait | 05b28eb | 2014-02-19 11:04:32 +0530 | [diff] [blame] | 54 | territory.territory = default_territory; |
| 55 | } |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 56 | |
Anand Doshi | 1ffc5b5 | 2013-07-15 18:09:43 +0530 | [diff] [blame] | 57 | } |
| 58 | }, |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 59 | |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 60 | setup_serial_no: function(grid_row) { |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 61 | if(!grid_row.fields_dict.serial_no || |
Anand Doshi | f83cf58 | 2013-08-20 15:36:10 +0530 | [diff] [blame] | 62 | grid_row.fields_dict.serial_no.get_status()!=="Write") return; |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 63 | |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 64 | var $btn = $('<button class="btn btn-sm btn-default">'+__("Add Serial No")+'</button>') |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 65 | .appendTo($("<div>") |
Rushabh Mehta | 7d8b189 | 2013-11-08 16:27:18 +0530 | [diff] [blame] | 66 | .css({"margin-bottom": "10px", "margin-left": "15px"}) |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 67 | .appendTo(grid_row.fields_dict.serial_no.$wrapper)); |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 68 | |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 69 | $btn.on("click", function() { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 70 | var d = new frappe.ui.Dialog({ |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 71 | title: __("Add Serial No"), |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 72 | fields: [ |
| 73 | { |
| 74 | "fieldtype": "Link", |
| 75 | "options": "Serial No", |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 76 | "label": __("Serial No"), |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 77 | "get_query": { |
| 78 | item_code: grid_row.doc.item_code, |
| 79 | warehouse: grid_row.doc.warehouse |
| 80 | } |
| 81 | }, |
| 82 | { |
| 83 | "fieldtype": "Button", |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 84 | "label": __("Add") |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 85 | } |
| 86 | ] |
| 87 | }); |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 88 | |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 89 | d.get_input("add").on("click", function() { |
| 90 | var serial_no = d.get_value("serial_no"); |
| 91 | if(serial_no) { |
| 92 | var val = (grid_row.doc.serial_no || "").split("\n").concat([serial_no]).join("\n"); |
| 93 | grid_row.fields_dict.serial_no.set_model_value(val.trim()); |
| 94 | } |
| 95 | d.hide(); |
| 96 | return false; |
| 97 | }); |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 98 | |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 99 | d.show(); |
| 100 | }); |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 101 | }, |
Neil Trini Lasrado | 31a92bd | 2014-12-15 15:49:40 +0530 | [diff] [blame] | 102 | |
| 103 | get_letter_head: function(company) { |
| 104 | frappe.call({ |
| 105 | type:"GET", |
| 106 | method: "erpnext.accounts.utils.get_letter_head", |
| 107 | args: { |
| 108 | "company": company |
| 109 | }, |
| 110 | callback: function(r) { |
| 111 | if (!r.exe) cur_frm.set_value("letter_head", r.message); |
| 112 | } |
| 113 | }); |
| 114 | }, |
| 115 | |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 116 | }); |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 117 | |
Nabin Hait | b072c74 | 2014-12-16 12:49:13 +0530 | [diff] [blame] | 118 | |
| 119 | $.extend(erpnext.utils, { |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 120 | render_address_and_contact: function(frm) { |
| 121 | // render address |
| 122 | $(frm.fields_dict['address_html'].wrapper) |
Rushabh Mehta | afbfdcf | 2014-12-30 18:09:40 +0530 | [diff] [blame] | 123 | .html(frappe.render_template("address_list", |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 124 | cur_frm.doc.__onload)) |
| 125 | .find(".btn-address").on("click", function() { |
| 126 | new_doc("Address"); |
| 127 | }); |
| 128 | |
| 129 | // render contact |
| 130 | if(frm.fields_dict['contact_html']) { |
| 131 | $(frm.fields_dict['contact_html'].wrapper) |
Rushabh Mehta | afbfdcf | 2014-12-30 18:09:40 +0530 | [diff] [blame] | 132 | .html(frappe.render_template("contact_list", |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 133 | cur_frm.doc.__onload)) |
| 134 | .find(".btn-contact").on("click", function() { |
| 135 | new_doc("Contact"); |
| 136 | } |
| 137 | ); |
| 138 | } |
| 139 | } |
Nabin Hait | b072c74 | 2014-12-16 12:49:13 +0530 | [diff] [blame] | 140 | }) |