Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe 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) { |
Nabin Hait | eb00cae | 2015-05-27 13:18:09 +0530 | [diff] [blame] | 17 | if(frappe.meta.get_docfield(cur_frm.doctype, "fiscal_year")) { |
| 18 | frappe.call({ |
| 19 | type:"GET", |
| 20 | method: "erpnext.accounts.utils.get_fiscal_year", |
| 21 | args: { |
| 22 | "company": company, |
| 23 | "date": date, |
| 24 | "verbose": 0 |
| 25 | }, |
| 26 | callback: function(r) { |
| 27 | if (r.message) cur_frm.set_value("fiscal_year", r.message[0]); |
| 28 | if (fn) fn(); |
| 29 | } |
| 30 | }); |
| 31 | } |
Neil Trini Lasrado | 78fa695 | 2014-10-03 17:43:02 +0530 | [diff] [blame] | 32 | }, |
| 33 | |
Anand Doshi | 8bde7f9 | 2014-04-24 18:11:49 +0530 | [diff] [blame] | 34 | toggle_naming_series: function() { |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 35 | if(cur_frm.fields_dict.naming_series) { |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 36 | cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false); |
| 37 | } |
| 38 | }, |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 39 | |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 40 | hide_company: function() { |
| 41 | if(cur_frm.fields_dict.company) { |
Rushabh Mehta | 8941841 | 2013-10-07 18:22:29 +0530 | [diff] [blame] | 42 | var companies = Object.keys(locals[":Company"] || {}); |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 43 | if(companies.length === 1) { |
| 44 | if(!cur_frm.doc.company) cur_frm.set_value("company", companies[0]); |
| 45 | cur_frm.toggle_display("company", false); |
Rushabh Mehta | 45ca8a4 | 2015-04-28 16:02:09 +0530 | [diff] [blame] | 46 | } else if(erpnext.last_selected_company) { |
| 47 | if(!cur_frm.doc.company) cur_frm.set_value("company", erpnext.last_selected_company); |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 48 | } |
| 49 | } |
Anand Doshi | 1ffc5b5 | 2013-07-15 18:09:43 +0530 | [diff] [blame] | 50 | }, |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 51 | |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 52 | setup_serial_no: function() { |
| 53 | var grid_row = cur_frm.open_grid_row(); |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 54 | if(!grid_row.fields_dict.serial_no || |
Anand Doshi | f83cf58 | 2013-08-20 15:36:10 +0530 | [diff] [blame] | 55 | grid_row.fields_dict.serial_no.get_status()!=="Write") return; |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 56 | |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 57 | 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] | 58 | .appendTo($("<div>") |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 59 | .css({"margin-bottom": "10px", "margin-top": "10px"}) |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 60 | .appendTo(grid_row.fields_dict.serial_no.$wrapper)); |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 61 | |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 62 | $btn.on("click", function() { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 63 | var d = new frappe.ui.Dialog({ |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 64 | title: __("Add Serial No"), |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 65 | fields: [ |
| 66 | { |
| 67 | "fieldtype": "Link", |
| 68 | "options": "Serial No", |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 69 | "label": __("Serial No"), |
Neil Trini Lasrado | 3d61bbb | 2015-03-12 12:51:50 +0530 | [diff] [blame] | 70 | "get_query": function () { |
Rushabh Mehta | 45ca8a4 | 2015-04-28 16:02:09 +0530 | [diff] [blame] | 71 | return { |
| 72 | filters: { |
Neil Trini Lasrado | 3d61bbb | 2015-03-12 12:51:50 +0530 | [diff] [blame] | 73 | item_code:grid_row.doc.item_code , |
Nabin Hait | b7c0c55 | 2015-08-04 12:18:12 +0530 | [diff] [blame] | 74 | warehouse:cur_frm.doc.is_return ? null : grid_row.doc.warehouse |
Neil Trini Lasrado | 3d61bbb | 2015-03-12 12:51:50 +0530 | [diff] [blame] | 75 | } |
| 76 | } |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 77 | } |
| 78 | }, |
| 79 | { |
| 80 | "fieldtype": "Button", |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 81 | "label": __("Add") |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 82 | } |
| 83 | ] |
| 84 | }); |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 85 | |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 86 | d.get_input("add").on("click", function() { |
| 87 | var serial_no = d.get_value("serial_no"); |
| 88 | if(serial_no) { |
| 89 | var val = (grid_row.doc.serial_no || "").split("\n").concat([serial_no]).join("\n"); |
| 90 | grid_row.fields_dict.serial_no.set_model_value(val.trim()); |
| 91 | } |
| 92 | d.hide(); |
| 93 | return false; |
| 94 | }); |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 95 | |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 96 | d.show(); |
| 97 | }); |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 98 | } |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 99 | }); |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 100 | |
Nabin Hait | b072c74 | 2014-12-16 12:49:13 +0530 | [diff] [blame] | 101 | |
| 102 | $.extend(erpnext.utils, { |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 103 | render_address_and_contact: function(frm) { |
| 104 | // render address |
| 105 | $(frm.fields_dict['address_html'].wrapper) |
Rushabh Mehta | afbfdcf | 2014-12-30 18:09:40 +0530 | [diff] [blame] | 106 | .html(frappe.render_template("address_list", |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 107 | cur_frm.doc.__onload)) |
| 108 | .find(".btn-address").on("click", function() { |
| 109 | new_doc("Address"); |
| 110 | }); |
| 111 | |
| 112 | // render contact |
| 113 | if(frm.fields_dict['contact_html']) { |
| 114 | $(frm.fields_dict['contact_html'].wrapper) |
Rushabh Mehta | afbfdcf | 2014-12-30 18:09:40 +0530 | [diff] [blame] | 115 | .html(frappe.render_template("contact_list", |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 116 | cur_frm.doc.__onload)) |
| 117 | .find(".btn-contact").on("click", function() { |
| 118 | new_doc("Contact"); |
| 119 | } |
| 120 | ); |
| 121 | } |
Anand Doshi | 06ad308 | 2015-09-23 12:46:59 +0530 | [diff] [blame] | 122 | }, |
| 123 | |
Neil Trini Lasrado | fe2ffae | 2015-07-08 18:16:51 +0530 | [diff] [blame] | 124 | copy_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname) { |
| 125 | var d = locals[dt][dn]; |
| 126 | if(d[fieldname]){ |
| 127 | var cl = doc[table_fieldname] || []; |
| 128 | for(var i = 0; i < cl.length; i++) { |
| 129 | if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname]; |
| 130 | } |
| 131 | } |
| 132 | refresh_field(table_fieldname); |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 133 | } |
Rushabh Mehta | 95b995b | 2015-05-28 12:10:55 +0530 | [diff] [blame] | 134 | }); |
| 135 | |
| 136 | // add description on posting time |
| 137 | $(document).on('app_ready', function() { |
| 138 | if(!frappe.datetime.is_timezone_same()) { |
| 139 | $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry", |
| 140 | "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) { |
| 141 | frappe.ui.form.on(d, "onload", function(frm) { |
| 142 | cur_frm.set_df_property("posting_time", "description", |
| 143 | sys_defaults.time_zone); |
| 144 | }); |
| 145 | }); |
| 146 | } |
| 147 | }); |