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