blob: 37cc75ea643c6d848e1b2e237226c4c2e84a5f00 [file] [log] [blame]
Rushabh Mehta2fa2f712012-09-24 19:13:42 +05301// ERPNext - web based ERP (http://erpnext.com)
2// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17var current_module;
18var is_system_manager = 0;
19
20wn.provide('erpnext.startup');
21
Rushabh Mehta2fa2f712012-09-24 19:13:42 +053022erpnext.startup.set_globals = function() {
23 if(inList(user_roles,'System Manager')) is_system_manager = 1;
24}
25
26erpnext.startup.start = function() {
27 console.log('Starting up...');
28 $('#startup_div').html('Starting up...').toggle(true);
29
30
31 erpnext.startup.set_globals();
32
33 if(user != 'Guest'){
34 if(wn.boot.user_background) {
35 erpnext.set_user_background(wn.boot.user_background);
36 }
Rushabh Mehtab0d32b72012-09-28 16:11:57 +053037 if(wn.boot.profile.defaults.theme) {
Rushabh Mehta3c041932012-09-28 18:47:47 +053038 erpnext.set_theme(wn.boot.profile.defaults.theme[0]);
Rushabh Mehtab0d32b72012-09-28 16:11:57 +053039 }
Rushabh Mehta2fa2f712012-09-24 19:13:42 +053040
Rushabh Mehta079df382012-10-02 16:54:42 +053041 erpnext.setup_mousetrap();
42
Rushabh Mehta2fa2f712012-09-24 19:13:42 +053043 // always allow apps
44 wn.boot.profile.allow_modules = wn.boot.profile.allow_modules.concat(
45 ['To Do', 'Knowledge Base', 'Calendar', 'Activity', 'Messages'])
46
47 // setup toolbar
48 erpnext.toolbar.setup();
49
50 // set interval for updates
51 erpnext.startup.set_periodic_updates();
52
53 // border to the body
54 // ------------------
55 $('footer').html('<div class="web-footer erpnext-footer">\
Rushabh Mehta724bb472012-11-13 12:32:11 +053056 <a href="#attributions">Attributions and License</a> | \
57 <a href="#latest-updates"><b>Latest Updates</b></a></div>');
Rushabh Mehta2fa2f712012-09-24 19:13:42 +053058
59 // complete registration
60 if(in_list(user_roles,'System Manager') && (wn.boot.setup_complete=='No')) {
61 wn.require("app/js/complete_setup.js");
62 erpnext.complete_setup.show();
63 }
64 if(wn.boot.expires_on && in_list(user_roles, 'System Manager')) {
Rushabh Mehtad1784cc2012-11-12 12:57:42 +053065 var today = dateutil.str_to_obj(wn.boot.server_date);
Rushabh Mehta2fa2f712012-09-24 19:13:42 +053066 var expires_on = dateutil.str_to_obj(wn.boot.expires_on);
67 var diff = dateutil.get_diff(expires_on, today);
68 if (0 <= diff && diff <= 15) {
69 var expiry_string = diff==0 ? "today" : repl("in %(diff)s day(s)", { diff: diff });
70 $('header').append(repl('<div class="expiry-info"> \
71 Your ERPNext subscription will <b>expire %(expiry_string)s</b>. \
72 Please renew your subscription to continue using ERPNext \
73 (and remove this annoying banner). \
74 </div>', { expiry_string: expiry_string }));
75 } else if (diff < 0) {
76 $('header').append(repl('<div class="expiry-info"> \
77 This ERPNext subscription <b>has expired</b>. \
78 </div>', { expiry_string: expiry_string }));
79 }
80 }
81 erpnext.set_about();
82 if(wn.control_panel.custom_startup_code)
83 eval(wn.control_panel.custom_startup_code);
84 }
85}
86
87
88// ========== Update Messages ============
89erpnext.update_messages = function(reset) {
90 // Updates Team Messages
91
92 if(inList(['Guest'], user) || !wn.session_alive) { return; }
93
94 if(!reset) {
95 var set_messages = function(r) {
96 if(!r.exc) {
97 // This function is defined in toolbar.js
98 erpnext.toolbar.set_new_comments(r.message.unread_messages);
99
100 var show_in_circle = function(parent_id, msg) {
101 var parent = $('#'+parent_id);
102 if(parent) {
103 if(msg) {
104 parent.find('span:first').text(msg);
105 parent.toggle(true);
106 } else {
107 parent.toggle(false);
108 }
109 }
110 }
111
Rushabh Mehtaec406862012-11-13 10:59:11 +0530112 show_in_circle('unread_messages', r.message.unread_messages);
Rushabh Mehta2fa2f712012-09-24 19:13:42 +0530113 show_in_circle('open_support_tickets', r.message.open_support_tickets);
114 show_in_circle('things_todo', r.message.things_todo);
115 show_in_circle('todays_events', r.message.todays_events);
116 show_in_circle('open_tasks', r.message.open_tasks);
117 show_in_circle('unanswered_questions', r.message.unanswered_questions);
118
119 } else {
120 clearInterval(wn.updates.id);
121 }
122 }
123
124 wn.call({
125 method: 'startup.startup.get_global_status_messages',
126 callback: set_messages
127 });
128
129 } else {
130 erpnext.toolbar.set_new_comments(0);
131 $('#unread_messages').toggle(false);
132 }
133}
134
135erpnext.startup.set_periodic_updates = function() {
136 // Set interval for periodic updates of team messages
137 wn.updates = {};
138
139 if(wn.updates.id) {
140 clearInterval(wn.updates.id);
141 }
142
143 wn.updates.id = setInterval(erpnext.update_messages, 60000);
144}
145
146erpnext.set_user_background = function(src) {
Anand Doshia4cc3832012-10-02 19:13:04 +0530147 set_style(repl('#body_div { background: url("files/%(src)s") repeat fixed;}',
148 {src:src}))
Rushabh Mehta2fa2f712012-09-24 19:13:42 +0530149}
150
Rushabh Mehta2fa2f712012-09-24 19:13:42 +0530151// subject, sender, description
152erpnext.send_message = function(opts) {
153 if(opts.btn) {
154 $(opts.btn).start_working();
155 }
156 wn.call({
157 method: 'website.send_message',
158 args: opts,
159 callback: function(r) {
160 if(opts.btn) {
161 $(opts.btn).done_working();
162 }
163 if(opts.callback)opts.callback(r)
164 }
165 });
166}
167
168erpnext.hide_naming_series = function() {
169 if(cur_frm.fields_dict.naming_series) {
170 hide_field('naming_series');
171 if(cur_frm.doc.__islocal) {
172 unhide_field('naming_series');
173 }
174 }
175}
Rushabh Mehtacebb0272012-09-28 12:18:43 +0530176
Rushabh Mehta079df382012-10-02 16:54:42 +0530177erpnext.setup_mousetrap = function() {
178 Mousetrap.bind(["command+g", "ctrl+g"], function() {
179 wn.ui.toolbar.search.show();
180 return false;
181 });
Rushabh Mehtacebb0272012-09-28 12:18:43 +0530182
Rushabh Mehta079df382012-10-02 16:54:42 +0530183 Mousetrap.bind(["command+s", "ctrl+s"], function() {
184 if(cur_frm)
185 cur_frm.save();
Rushabh Mehtaac7baf02012-10-03 12:22:13 +0530186 else if(wn.container.page.save_action)
187 wn.container.page.save_action();
Rushabh Mehta079df382012-10-02 16:54:42 +0530188 return false;
189 });
190}
Rushabh Mehtab0d32b72012-09-28 16:11:57 +0530191
Rushabh Mehta9d1faea2012-10-02 14:56:41 +0530192// start
193$(document).bind('startup', function() {
194 erpnext.startup.start();
Anand Doshie65e6212012-11-19 15:43:18 +0530195});