blob: c0549ba41dddc05b997255fcb9523ce7b3b12a42 [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
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05303frappe.provide("erpnext");
Nabin Haitb072c742014-12-16 12:49:13 +05304frappe.provide("erpnext.utils");
Anand Doshie65e6212012-11-19 15:43:18 +05305
Anand Doshibdee6e02013-07-09 13:03:39 +05306$.extend(erpnext, {
7 get_currency: function(company) {
8 if(!company && cur_frm)
9 company = cur_frm.doc.company;
10 if(company)
Rushabh Mehta66d52b52014-03-27 14:17:33 +053011 return frappe.get_doc(":Company", company).default_currency || frappe.boot.sysdefaults.currency;
Anand Doshibdee6e02013-07-09 13:03:39 +053012 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053013 return frappe.boot.sysdefaults.currency;
Anand Doshibdee6e02013-07-09 13:03:39 +053014 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053015
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +053016 get_fiscal_year: function(company, date, fn) {
Nabin Haiteb00cae2015-05-27 13:18:09 +053017 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 Lasrado78fa6952014-10-03 17:43:02 +053032 },
33
Anand Doshi8bde7f92014-04-24 18:11:49 +053034 toggle_naming_series: function() {
Rushabh Mehtac38fc712014-04-16 17:21:25 +053035 if(cur_frm.fields_dict.naming_series) {
Anand Doshibdee6e02013-07-09 13:03:39 +053036 cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false);
37 }
38 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053039
Anand Doshibdee6e02013-07-09 13:03:39 +053040 hide_company: function() {
41 if(cur_frm.fields_dict.company) {
Rushabh Mehta89418412013-10-07 18:22:29 +053042 var companies = Object.keys(locals[":Company"] || {});
Anand Doshibdee6e02013-07-09 13:03:39 +053043 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 Mehta45ca8a42015-04-28 16:02:09 +053046 } else if(erpnext.last_selected_company) {
47 if(!cur_frm.doc.company) cur_frm.set_value("company", erpnext.last_selected_company);
Anand Doshibdee6e02013-07-09 13:03:39 +053048 }
49 }
Anand Doshi1ffc5b52013-07-15 18:09:43 +053050 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053051
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053052 setup_serial_no: function() {
53 var grid_row = cur_frm.open_grid_row();
Rushabh Mehtac38fc712014-04-16 17:21:25 +053054 if(!grid_row.fields_dict.serial_no ||
Anand Doshif83cf582013-08-20 15:36:10 +053055 grid_row.fields_dict.serial_no.get_status()!=="Write") return;
Rushabh Mehtac38fc712014-04-16 17:21:25 +053056
Pratik Vyasb52618c2014-04-14 16:25:30 +053057 var $btn = $('<button class="btn btn-sm btn-default">'+__("Add Serial No")+'</button>')
Rushabh Mehta62030e02013-08-14 18:37:28 +053058 .appendTo($("<div>")
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053059 .css({"margin-bottom": "10px", "margin-top": "10px"})
Rushabh Mehta62030e02013-08-14 18:37:28 +053060 .appendTo(grid_row.fields_dict.serial_no.$wrapper));
Rushabh Mehtac38fc712014-04-16 17:21:25 +053061
Rushabh Mehta62030e02013-08-14 18:37:28 +053062 $btn.on("click", function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053063 var d = new frappe.ui.Dialog({
Pratik Vyasb52618c2014-04-14 16:25:30 +053064 title: __("Add Serial No"),
Rushabh Mehta62030e02013-08-14 18:37:28 +053065 fields: [
66 {
67 "fieldtype": "Link",
68 "options": "Serial No",
Pratik Vyasb52618c2014-04-14 16:25:30 +053069 "label": __("Serial No"),
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053070 "get_query": function () {
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053071 return {
72 filters: {
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053073 item_code:grid_row.doc.item_code ,
Nabin Haitb7c0c552015-08-04 12:18:12 +053074 warehouse:cur_frm.doc.is_return ? null : grid_row.doc.warehouse
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053075 }
76 }
Rushabh Mehta62030e02013-08-14 18:37:28 +053077 }
78 },
79 {
80 "fieldtype": "Button",
Pratik Vyasb52618c2014-04-14 16:25:30 +053081 "label": __("Add")
Rushabh Mehta62030e02013-08-14 18:37:28 +053082 }
83 ]
84 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053085
Rushabh Mehta62030e02013-08-14 18:37:28 +053086 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 Mehtac38fc712014-04-16 17:21:25 +053095
Rushabh Mehta62030e02013-08-14 18:37:28 +053096 d.show();
97 });
Rushabh Mehta5495bc52015-05-19 12:00:34 +053098 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053099});
Rushabh Mehta7d368752014-11-24 14:16:47 +0530100
Nabin Haitb072c742014-12-16 12:49:13 +0530101
102$.extend(erpnext.utils, {
Nabin Hait915778f2015-10-30 14:43:13 +0530103 clear_address_and_contact: function(frm) {
104 $(frm.fields_dict['address_html'].wrapper).html("");
105 frm.fields_dict['contact_html'] && $(frm.fields_dict['contact_html'].wrapper).html("");
106 },
107
Rushabh Mehta7d368752014-11-24 14:16:47 +0530108 render_address_and_contact: function(frm) {
109 // render address
110 $(frm.fields_dict['address_html'].wrapper)
Rushabh Mehtaafbfdcf2014-12-30 18:09:40 +0530111 .html(frappe.render_template("address_list",
Rushabh Mehta7d368752014-11-24 14:16:47 +0530112 cur_frm.doc.__onload))
113 .find(".btn-address").on("click", function() {
114 new_doc("Address");
115 });
116
117 // render contact
118 if(frm.fields_dict['contact_html']) {
119 $(frm.fields_dict['contact_html'].wrapper)
Rushabh Mehtaafbfdcf2014-12-30 18:09:40 +0530120 .html(frappe.render_template("contact_list",
Rushabh Mehta7d368752014-11-24 14:16:47 +0530121 cur_frm.doc.__onload))
122 .find(".btn-contact").on("click", function() {
123 new_doc("Contact");
124 }
125 );
126 }
Anand Doshi06ad3082015-09-23 12:46:59 +0530127 },
128
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530129 copy_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname) {
130 var d = locals[dt][dn];
131 if(d[fieldname]){
132 var cl = doc[table_fieldname] || [];
133 for(var i = 0; i < cl.length; i++) {
134 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
135 }
136 }
137 refresh_field(table_fieldname);
Rushabh Mehta7d368752014-11-24 14:16:47 +0530138 }
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530139});
140
141// add description on posting time
142$(document).on('app_ready', function() {
143 if(!frappe.datetime.is_timezone_same()) {
144 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
145 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
146 frappe.ui.form.on(d, "onload", function(frm) {
147 cur_frm.set_df_property("posting_time", "description",
148 sys_defaults.time_zone);
149 });
150 });
151 }
152});