2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 require_once('WEB-INF/config.php');
30 require_once('WEB-INF/lib/common.lib.php');
31 if (!require_once('MDB2.php')) {
32 die ("Unable to require MDB2 module. Please check it<br>\n");
34 require_once('initialize.php');
35 import('ttUserHelper');
36 import('ttTaskHelper');
38 function setChange($sql) {
39 print "<pre>".$sql."</pre>";
40 $mdb2 = getConnection();
41 $affected = $mdb2->exec($sql);
42 if (is_a($affected, 'PEAR_Error')) {
43 print "error: ".$affected->getMessage()."<br>";
45 print "successful update<br>\n";
51 print "Processing...<br>\n";
53 if ($_POST["crstructure"]) {
54 $sqlQuery = join("\n", file("mysql.sql"));
55 $sqlQuery = str_replace("TYPE=MyISAM","",$sqlQuery);
56 $queries = explode(";",$sqlQuery);
57 if (is_array($queries)) {
58 foreach ($queries as $query) {
59 $query = trim($query);
60 if (strlen($query)>0) {
67 if ($_POST["convert5to7"]) {
68 setChange("alter table `activity_log` CHANGE al_comment al_comment BLOB");
69 setChange("CREATE TABLE `sysconfig` (`sysc_id` int(11) unsigned NOT NULL auto_increment,`sysc_name` varchar(32) NOT NULL default '',`sysc_value` varchar(70) default NULL, PRIMARY KEY (`sysc_id`), UNIQUE KEY `sysc_id` (`sysc_id`), UNIQUE KEY `sysc_name` (`sysc_name`))");
70 setChange("alter table `companies` add c_locktime int(4) default -1");
71 setChange("alter table `activity_log` add al_billable tinyint(4) default 0");
72 setChange("alter table `sysconfig` drop INDEX `sysc_name`");
73 setChange("alter table `sysconfig` add sysc_id_u int(4)");
74 setChange("alter table `report_filter_set` add rfs_billable VARCHAR(10)");
75 setChange("ALTER TABLE clients MODIFY clnt_id int(11) NOT NULL AUTO_INCREMENT");
76 setChange("ALTER TABLE `users` ADD `u_show_pie` smallint(2) DEFAULT '1'");
77 setChange("alter table `users` ADD `u_pie_mode` smallint(2) DEFAULT '1'");
78 setChange("alter table users drop `u_aprojects`");
81 if ($_POST["convert7to133"]) {
82 setChange("ALTER TABLE users ADD COLUMN u_lang VARCHAR(20) DEFAULT NULL");
83 setChange("ALTER TABLE users ADD COLUMN u_email VARCHAR(100) DEFAULT NULL");
84 setChange("ALTER TABLE `activity_log` drop `al_proof`");
85 setChange("ALTER TABLE `activity_log` drop `al_charge`");
86 setChange("ALTER TABLE `activities` drop `a_project_id`");
87 setChange("DROP TABLE `activity_status_list`");
88 setChange("DROP TABLE `project_status_list`");
89 setChange("DROP TABLE `user_status_list`");
90 setChange("DROP TABLE `companies_c_id_seq`");
91 setChange("ALTER TABLE projects ADD COLUMN p_activities TEXT");
94 // The update_projects function updates p_activities field in the projects table so that we could
95 // improve performance of the application by using this field instead of activity_bind table.
96 if ($_POST["update_projects"]) {
97 $mdb2 = getConnection();
98 // $sql = "select p_id from projects where p_status = 1 and p_activities is NULL";
99 $sql = "select p_id from projects where p_status = 1";
100 $res = $mdb2->query($sql);
101 if (is_a($res, 'PEAR_Error')) {
102 die($res->getMessage());
104 // Iterate through projects.
105 while ($val = $res->fetchRow()) {
106 $project_id = $val['p_id'];
108 // Get activity binds for project (old way).
109 // $sql = "select ab_id_a from activity_bind where ab_id_p = $project_id";
110 $sql = "select ab_id_a, a_id, a_name from activity_bind
111 inner join activities on (ab_id_a = a_id)
112 where ab_id_p = $project_id
115 $result = $mdb2->query($sql);
116 if (is_a($result, 'PEAR_Error')) {
117 die($result->getMessage());
119 $activity_arr = array();
120 while ($value = $result->fetchRow()) {
121 $activity_arr[] = $value['ab_id_a'];
123 $a_comma_separated = implode(",", $activity_arr); // This is a comma-separated list of associated activity ids.
125 // Re-bind the project to activities (new way).
126 $sql = "update projects set p_activities = ".$mdb2->quote($a_comma_separated)." where p_id = $project_id";
127 $affected = $mdb2->exec($sql);
128 if (is_a($affected, 'PEAR_Error')) {
129 die($affected->getMessage());
134 if ($_POST["convert133to1340"]) {
135 setChange("ALTER TABLE companies ADD COLUMN c_show_pie smallint(2) DEFAULT 1");
136 setChange("ALTER TABLE companies ADD COLUMN c_pie_mode smallint(2) DEFAULT 1");
137 setChange("ALTER TABLE companies ADD COLUMN c_lang varchar(20) default NULL");
140 // The update_companies function sets up c_show_pie, c_pie_mode, and c_lang
141 // fields in the companies table from the corresponding manager fields.
142 if ($_POST["update_companies"]) {
143 $mdb2 = getConnection();
144 // Get all active managers.
145 $sql = "select u_company_id, u_show_pie, u_pie_mode, u_lang from users
146 where u_manager_id is NULL and u_login <> 'admin' and u_company_id is not NULL and u_active = 1";
147 $res = $mdb2->query($sql);
148 if (is_a($res, 'PEAR_Error')) {
149 die($res->getMessage());
151 // Iterate through managers and set fields in the companies table.
152 while ($val = $res->fetchRow()) {
153 $company_id = $val['u_company_id'];
154 $show_pie = $val['u_show_pie'];
155 $pie_mode = $val['u_pie_mode'];
156 $lang = $val['u_lang'];
158 $sql = "update companies set
159 c_show_pie = $show_pie, c_pie_mode = $pie_mode, c_lang = ".$mdb2->quote($lang).
160 " where c_id = $company_id";
162 $result = $mdb2->query($sql);
163 if (is_a($result, 'PEAR_Error')) {
164 die($result->getMessage());
169 if ($_POST["convert1340to1485"]) {
170 setChange("ALTER TABLE users DROP u_show_pie");
171 setChange("ALTER TABLE users DROP u_pie_mode");
172 setChange("ALTER TABLE users DROP u_lang");
173 setChange("ALTER TABLE `users` modify u_login varchar(100) NOT NULL");
174 setChange("ALTER TABLE `users` modify u_active smallint(6) default '1'");
175 setChange("drop index u_login_idx on users");
176 setChange("create unique index u_login_idx on users(u_login, u_active)");
177 setChange("ALTER TABLE companies MODIFY `c_lang` varchar(20) NOT NULL default 'en'");
178 setChange("ALTER TABLE companies ADD COLUMN `c_date_format` varchar(20) NOT NULL default '%Y-%m-%d'");
179 setChange("ALTER TABLE companies ADD COLUMN `c_time_format` varchar(20) NOT NULL default '%H:%M'");
180 setChange("ALTER TABLE companies ADD COLUMN `c_week_start` smallint(2) NOT NULL DEFAULT '0'");
181 setChange("ALTER TABLE clients MODIFY `clnt_status` smallint(6) default '1'");
182 setChange("create unique index clnt_name_idx on clients(clnt_id_um, clnt_name, clnt_status)");
183 setChange("ALTER TABLE projects modify p_status smallint(6) default '1'");
184 setChange("update projects set p_status = NULL where p_status = 1000");
185 setChange("drop index p_manager_idx on projects");
186 setChange("create unique index p_name_idx on projects(p_manager_id, p_name, p_status)");
187 setChange("ALTER TABLE activities modify a_status smallint(6) default '1'");
188 setChange("update activities set a_status = NULL where a_status = 1000");
189 setChange("drop index a_manager_idx on activities");
190 setChange("create unique index a_name_idx on activities(a_manager_id, a_name, a_status)");
191 setChange("RENAME TABLE companies TO teams");
192 setChange("RENAME TABLE teams TO att_teams");
193 setChange("ALTER TABLE att_teams CHANGE c_id id int(11) NOT NULL auto_increment");
194 setChange("RENAME TABLE users TO att_users");
195 setChange("update att_users set u_company_id = 0 where u_company_id is NULL");
196 setChange("ALTER TABLE att_users CHANGE u_company_id team_id int(11) NOT NULL");
197 setChange("RENAME TABLE att_teams TO tt_teams");
198 setChange("RENAME TABLE att_users TO tt_users");
199 setChange("ALTER TABLE tt_teams CHANGE c_name name varchar(80) NOT NULL");
200 setChange("ALTER TABLE `tt_teams` drop `c_www`");
201 setChange("ALTER TABLE `tt_teams` MODIFY `name` varchar(80) default NULL");
202 setChange("ALTER TABLE clients ADD COLUMN `your_name` varchar(255) default NULL");
203 setChange("ALTER TABLE tt_teams ADD COLUMN `address` varchar(255) default NULL");
204 setChange("ALTER TABLE invoice_header ADD COLUMN `client_name` varchar(255) default NULL");
205 setChange("ALTER TABLE invoice_header ADD COLUMN `client_addr` varchar(255) default NULL");
206 setChange("ALTER TABLE report_filter_set ADD COLUMN `rfs_cb_cost` tinyint(4) default '0'");
207 setChange("ALTER TABLE activity_log DROP primary key");
208 setChange("ALTER TABLE activity_log ADD COLUMN `id` bigint NOT NULL auto_increment primary key");
209 setChange("CREATE TABLE `tt_custom_fields` (`id` int(11) NOT NULL auto_increment, `team_id` int(11) NOT NULL, `type` tinyint(4) NOT NULL default '0', `label` varchar(32) NOT NULL default '', PRIMARY KEY (`id`))");
210 setChange("CREATE TABLE `tt_custom_field_options` (`id` int(11) NOT NULL auto_increment, `field_id` int(11) NOT NULL, `value` varchar(32) NOT NULL default '', PRIMARY KEY (`id`))");
211 setChange("CREATE TABLE `tt_custom_field_log` (`id` bigint NOT NULL auto_increment, `al_id` bigint NOT NULL, `field_id` int(11) NOT NULL, `value` varchar(255) default NULL, PRIMARY KEY (`id`))");
212 setChange("ALTER TABLE tt_users DROP u_level");
213 setChange("ALTER TABLE tt_custom_fields ADD COLUMN `status` tinyint(4) default '1'");
214 setChange("ALTER TABLE report_filter_set ADD COLUMN `rfs_cb_cf_1` tinyint(4) default '0'");
215 setChange("ALTER TABLE tt_teams ADD COLUMN `plugins` varchar(255) default NULL");
216 setChange("ALTER TABLE tt_teams MODIFY c_locktime int(4) default '0'");
217 setChange("ALTER TABLE clients DROP your_name");
218 setChange("ALTER TABLE clients DROP clnt_addr_your");
219 setChange("ALTER TABLE `tt_custom_fields` ADD COLUMN `required` tinyint(4) default '0'");
220 setChange("ALTER TABLE tt_teams DROP c_pie_mode");
221 setChange("RENAME TABLE report_filter_set TO tt_fav_reports");
222 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_id id int(11) unsigned NOT NULL auto_increment");
223 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_name name varchar(200) NOT NULL");
224 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_id_u user_id int(11) NOT NULL");
225 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_id_p project_id int(11) default NULL");
226 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_id_a task_id int(11) default NULL");
227 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_users users text default NULL");
228 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_period period tinyint(4) default NULL");
229 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_period_start period_start date default NULL");
230 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_period_finish period_end date default NULL");
231 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_project show_project tinyint(4) NOT NULL default '0'");
232 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_activity show_task tinyint(4) NOT NULL default '0'");
233 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_note show_note tinyint(4) NOT NULL default '0'");
234 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_start show_start tinyint(4) NOT NULL default '0'");
235 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_finish show_end tinyint(4) NOT NULL default '0'");
236 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_duration show_duration tinyint(4) NOT NULL default '0'");
237 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_cost show_cost tinyint(4) NOT NULL default '0'");
238 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_cf_1 show_custom_field_1 tinyint(4) NOT NULL default '0'");
239 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_idle show_empty_days tinyint(4) NOT NULL default '0'");
240 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_totals_only show_totals_only tinyint(4) NOT NULL default '0'");
241 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_groupby group_by varchar(20) default NULL");
242 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_billable billable tinyint(4) default NULL");
243 setChange("ALTER TABLE projects CHANGE p_activities tasks text default NULL");
244 setChange("ALTER TABLE tt_teams CHANGE c_currency currency varchar(7) default NULL");
245 setChange("ALTER TABLE tt_teams CHANGE c_locktime locktime int(4) default '0'");
246 setChange("ALTER TABLE tt_teams CHANGE c_show_pie show_pie smallint(2) DEFAULT '1'");
247 setChange("ALTER TABLE tt_teams CHANGE c_lang lang varchar(10) NOT NULL default 'en'");
248 setChange("ALTER TABLE tt_teams CHANGE c_date_format date_format varchar(20) NOT NULL default '%Y-%m-%d'");
249 setChange("ALTER TABLE tt_teams CHANGE c_time_format time_format varchar(20) NOT NULL default '%H:%M'");
250 setChange("ALTER TABLE tt_teams CHANGE c_week_start week_start smallint(2) NOT NULL DEFAULT '0'");
251 setChange("ALTER TABLE tt_users CHANGE u_id id int(11) NOT NULL auto_increment");
252 setChange("ALTER TABLE tt_users CHANGE u_timestamp timestamp timestamp NOT NULL");
253 setChange("ALTER TABLE tt_users CHANGE u_login login varchar(50) NOT NULL");
254 setChange("drop index u_login_idx on tt_users");
255 setChange("create unique index login_idx on tt_users(login, u_active)");
256 setChange("ALTER TABLE tt_users CHANGE u_password password varchar(50) default NULL");
257 setChange("ALTER TABLE tt_users CHANGE u_name name varchar(100) default NULL");
258 setChange("ALTER TABLE tt_users CHANGE u_email email varchar(100) default NULL");
259 setChange("ALTER TABLE tt_users CHANGE u_rate rate float(6,2) NOT NULL default '0.00'");
260 setChange("update tt_users set u_active = NULL where u_active = 1000");
261 setChange("ALTER TABLE tt_users CHANGE u_active status tinyint(4) default '1'");
262 setChange("ALTER TABLE tt_teams ADD COLUMN status tinyint(4) default '1'");
263 setChange("ALTER TABLE tt_users ADD COLUMN role int(11) default '4'");
264 setChange("update tt_users set role = 1024 where login = 'admin'");
265 setChange("update tt_users set role = 68 where u_comanager = 1");
266 setChange("update tt_users set role = 324 where u_manager_id is null and login != 'admin'");
267 setChange("ALTER TABLE user_bind CHANGE ub_checked status tinyint(4) default '1'");
268 setChange("ALTER TABLE activities ADD COLUMN team_id int(11) NOT NULL");
269 setChange("ALTER TABLE clients ADD COLUMN team_id int(11) NOT NULL");
270 setChange("ALTER TABLE projects ADD COLUMN team_id int(11) NOT NULL");
273 // The update_to_team_id function sets team_id field projects, activities, and clients tables.
274 if ($_POST["update_to_team_id"]) {
275 $mdb2 = getConnection();
278 $sql = "select p_id, p_manager_id from projects where team_id = 0 limit 1000";
279 $res = $mdb2->query($sql);
280 if (is_a($res, 'PEAR_Error')) {
281 die($res->getMessage());
283 // Iterate through projects.
284 $projects_updated = 0;
285 while ($val = $res->fetchRow()) {
286 $project_id = $val['p_id'];
287 $manager_id = $val['p_manager_id'];
289 $sql = "select team_id from tt_users where id = $manager_id";
290 $res2 = $mdb2->query($sql);
291 if (is_a($res2, 'PEAR_Error')) {
292 die($res2->getMessage());
294 $val2 = $res2->fetchRow();
295 $team_id = $val2['team_id'];
298 $sql = "update projects set team_id = $team_id where p_id = $project_id";
299 $affected = $mdb2->exec($sql);
300 if (is_a($affected, 'PEAR_Error')) {
301 die($affected->getMessage());
303 $projects_updated += $affected;
306 print "Updated $projects_updated projects...<br>\n";
309 $sql = "select a_id, a_manager_id from activities where team_id = 0 limit 1000";
310 $res = $mdb2->query($sql);
311 if (is_a($res, 'PEAR_Error')) {
312 die($res->getMessage());
314 // Iterate through tasks.
316 while ($val = $res->fetchRow()) {
317 $task_id = $val['a_id'];
318 $manager_id = $val['a_manager_id'];
320 $sql = "select team_id from tt_users where id = $manager_id";
321 $res2 = $mdb2->query($sql);
322 if (is_a($res2, 'PEAR_Error')) {
323 die($res2->getMessage());
325 $val2 = $res2->fetchRow();
326 $team_id = $val2['team_id'];
329 $sql = "update activities set team_id = $team_id where a_id = $task_id";
330 $affected = $mdb2->exec($sql);
331 if (is_a($affected, 'PEAR_Error')) {
332 die($affected->getMessage());
334 $tasks_updated += $affected;
337 print "Updated $tasks_updated tasks...<br>\n";
340 $sql = "select clnt_id, clnt_id_um from clients where team_id = 0 limit 1000";
341 $res = $mdb2->query($sql);
342 if (is_a($res, 'PEAR_Error')) {
343 die($res->getMessage());
345 // Iterate through clients.
346 $clients_updated = 0;
347 while ($val = $res->fetchRow()) {
348 $client_id = $val['clnt_id'];
349 $manager_id = $val['clnt_id_um'];
351 $sql = "select team_id from tt_users where id = $manager_id";
352 $res2 = $mdb2->query($sql);
353 if (is_a($res2, 'PEAR_Error')) {
354 die($res2->getMessage());
356 $val2 = $res2->fetchRow();
357 $team_id = $val2['team_id'];
360 $sql = "update clients set team_id = $team_id where clnt_id = $client_id";
361 $affected = $mdb2->exec($sql);
362 if (is_a($affected, 'PEAR_Error')) {
363 die($affected->getMessage());
365 $clients_updated += $affected;
368 print "Updated $clients_updated clients...<br>\n";
371 if ($_POST["convert1485to1579"]) {
372 setChange("ALTER TABLE tt_fav_reports MODIFY id int(11) NOT NULL auto_increment");
373 setChange("RENAME TABLE clients TO tt_clients");
374 setChange("ALTER TABLE tt_clients CHANGE clnt_id id int(11) NOT NULL AUTO_INCREMENT");
375 setChange("ALTER TABLE tt_clients CHANGE clnt_status status tinyint(4) default '1'");
376 setChange("ALTER TABLE tt_clients DROP clnt_id_um");
377 setChange("ALTER TABLE tt_clients CHANGE clnt_name name varchar(80) NOT NULL");
378 setChange("drop index clnt_name_idx on tt_clients");
379 setChange("drop index client_name_idx on tt_clients");
380 setChange("create unique index client_name_idx on tt_clients(team_id, name, status)");
381 setChange("ALTER TABLE tt_teams ADD COLUMN `timestamp` timestamp NOT NULL");
382 setChange("ALTER TABLE tt_clients CHANGE clnt_addr_cust address varchar(255) default NULL");
383 setChange("ALTER TABLE tt_clients DROP clnt_discount");
384 setChange("ALTER TABLE tt_clients DROP clnt_comment");
385 setChange("ALTER TABLE tt_clients DROP clnt_fsubtotals");
386 setChange("ALTER TABLE tt_clients CHANGE clnt_tax tax float(6,2) NOT NULL default '0.00'");
387 setChange("ALTER TABLE activity_log ADD COLUMN client_id int(11) default NULL");
388 setChange("ALTER TABLE tt_teams DROP show_pie");
389 setChange("ALTER TABLE tt_fav_reports CHANGE group_by sort_by varchar(20) default 'date'");
390 setChange("RENAME TABLE tmp_refs TO tt_tmp_refs");
391 setChange("ALTER TABLE tt_tmp_refs CHANGE tr_created timestamp timestamp NOT NULL");
392 setChange("ALTER TABLE tt_tmp_refs CHANGE tr_code ref char(32) NOT NULL default ''");
393 setChange("ALTER TABLE tt_tmp_refs CHANGE tr_userid user_id int(11) NOT NULL");
394 setChange("RENAME TABLE projects TO tt_projects");
395 setChange("ALTER TABLE tt_projects CHANGE p_id id int(11) NOT NULL auto_increment");
396 setChange("ALTER TABLE tt_projects DROP p_timestamp");
397 setChange("ALTER TABLE tt_projects CHANGE p_name name varchar(80) NOT NULL");
398 setChange("ALTER TABLE tt_projects CHANGE p_status status tinyint(4) default '1'");
399 setChange("drop index p_name_idx on tt_projects");
400 setChange("create unique index project_idx on tt_projects(team_id, name, status)");
401 setChange("RENAME TABLE activities TO tt_tasks");
402 setChange("ALTER TABLE tt_tasks CHANGE a_id id int(11) NOT NULL auto_increment");
403 setChange("ALTER TABLE tt_tasks DROP a_timestamp");
404 setChange("ALTER TABLE tt_tasks CHANGE a_name name varchar(80) NOT NULL");
405 setChange("ALTER TABLE tt_tasks CHANGE a_status status tinyint(4) default '1'");
406 setChange("drop index a_name_idx on tt_tasks");
407 setChange("create unique index task_idx on tt_tasks(team_id, name, status)");
408 setChange("RENAME TABLE invoice_header TO tt_invoice_headers");
409 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_user_id user_id int(11) NOT NULL");
410 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_number number varchar(20) default NULL");
411 setChange("ALTER TABLE tt_invoice_headers DROP ih_addr_your");
412 setChange("ALTER TABLE tt_invoice_headers DROP ih_addr_cust");
413 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_comment comment varchar(255) default NULL");
414 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_tax tax float(6,2) default '0.00'");
415 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_discount discount float(6,2) default '0.00'");
416 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_fsubtotals subtotals tinyint(4) NOT NULL default '0'");
417 setChange("ALTER TABLE tt_users DROP u_comanager");
418 setChange("ALTER TABLE tt_tasks DROP a_manager_id");
419 setChange("ALTER TABLE tt_projects DROP p_manager_id");
420 setChange("ALTER TABLE tt_users DROP u_manager_id");
421 setChange("ALTER TABLE activity_bind DROP ab_id");
422 setChange("RENAME TABLE activity_bind TO tt_project_task_binds");
423 setChange("ALTER TABLE tt_project_task_binds CHANGE ab_id_p project_id int(11) NOT NULL");
424 setChange("ALTER TABLE tt_project_task_binds CHANGE ab_id_a task_id int(11) NOT NULL");
425 setChange("RENAME TABLE user_bind TO tt_user_project_binds");
426 setChange("ALTER TABLE tt_user_project_binds CHANGE ub_rate rate float(6,2) NOT NULL default '0.00'");
427 setChange("ALTER TABLE tt_user_project_binds CHANGE ub_id_p project_id int(11) NOT NULL");
428 setChange("ALTER TABLE tt_user_project_binds CHANGE ub_id_u user_id int(11) NOT NULL");
429 setChange("ALTER TABLE tt_user_project_binds CHANGE ub_id id int(11) NOT NULL auto_increment");
430 setChange("CREATE TABLE `tt_client_project_binds` (`client_id` int(11) NOT NULL, `project_id` int(11) NOT NULL)");
431 setChange("ALTER TABLE tt_user_project_binds MODIFY rate float(6,2) default '0.00'");
432 setChange("ALTER TABLE tt_clients MODIFY tax float(6,2) default '0.00'");
433 setChange("RENAME TABLE activity_log TO tt_log");
434 setChange("ALTER TABLE tt_log CHANGE al_timestamp timestamp timestamp NOT NULL");
435 setChange("ALTER TABLE tt_log CHANGE al_user_id user_id int(11) NOT NULL");
436 setChange("ALTER TABLE tt_log CHANGE al_date date date NOT NULL");
437 setChange("drop index al_date_idx on tt_log");
438 setChange("create index date_idx on tt_log(date)");
439 setChange("ALTER TABLE tt_log CHANGE al_from start time default NULL");
440 setChange("ALTER TABLE tt_log CHANGE al_duration duration time default NULL");
441 setChange("ALTER TABLE tt_log CHANGE al_project_id project_id int(11) NOT NULL");
442 setChange("ALTER TABLE tt_log MODIFY project_id int(11) default NULL");
443 setChange("ALTER TABLE tt_log CHANGE al_activity_id task_id int(11) default NULL");
444 setChange("ALTER TABLE tt_log CHANGE al_comment comment blob");
445 setChange("ALTER TABLE tt_log CHANGE al_billable billable tinyint(4) default '0'");
446 setChange("drop index al_user_id_idx on tt_log");
447 setChange("drop index al_project_id_idx on tt_log");
448 setChange("drop index al_activity_id_idx on tt_log");
449 setChange("create index user_idx on tt_log(user_id)");
450 setChange("create index project_idx on tt_log(project_id)");
451 setChange("create index task_idx on tt_log(task_id)");
452 setChange("ALTER TABLE tt_custom_field_log CHANGE al_id log_id bigint NOT NULL");
453 setChange("RENAME TABLE sysconfig TO tt_config");
454 setChange("ALTER TABLE tt_config DROP sysc_id");
455 setChange("ALTER TABLE tt_config CHANGE sysc_id_u user_id int(11) NOT NULL");
456 setChange("ALTER TABLE tt_config CHANGE sysc_name param_name varchar(32) NOT NULL");
457 setChange("ALTER TABLE tt_config CHANGE sysc_value param_value varchar(80) default NULL");
458 setChange("create unique index param_idx on tt_config(user_id, param_name)");
459 setChange("ALTER TABLE tt_log ADD COLUMN invoice_id int(11) default NULL");
460 setChange("ALTER TABLE tt_projects ADD COLUMN description varchar(255) default NULL");
461 setChange("CREATE TABLE `tt_invoices` (`id` int(11) NOT NULL auto_increment, `team_id` int(11) NOT NULL, `number` varchar(20) default NULL, `client_name` varchar(255) default NULL, `client_addr` varchar(255) default NULL, `comment` varchar(255) default NULL, `tax` float(6,2) default '0.00', `discount` float(6,2) default '0.00', PRIMARY KEY (`id`))");
462 setChange("ALTER TABLE tt_invoices drop number");
463 setChange("ALTER TABLE tt_invoices drop client_name");
464 setChange("ALTER TABLE tt_invoices drop client_addr");
465 setChange("ALTER TABLE tt_invoices drop comment");
466 setChange("ALTER TABLE tt_invoices drop tax");
467 setChange("ALTER TABLE tt_invoices ADD COLUMN name varchar(80) NOT NULL");
468 setChange("ALTER TABLE tt_invoices ADD COLUMN client_id int(11) NOT NULL");
469 setChange("ALTER TABLE tt_invoices ADD COLUMN start_date date NOT NULL");
470 setChange("ALTER TABLE tt_invoices ADD COLUMN end_date date NOT NULL");
471 setChange("create unique index name_idx on tt_invoices(team_id, name)");
472 setChange("drop index ub_id_u on tt_user_project_binds");
473 setChange("create unique index bind_idx on tt_user_project_binds(user_id, project_id)");
474 setChange("create index client_idx on tt_log(client_id)");
475 setChange("create index invoice_idx on tt_log(invoice_id)");
478 if ($_POST["convert1579to1600"]) {
479 setChange("ALTER TABLE tt_invoices ADD COLUMN date date NOT NULL");
480 setChange("ALTER TABLE tt_teams ADD COLUMN custom_logo tinyint(4) default '0'");
481 setChange("ALTER TABLE tt_tasks ADD COLUMN description varchar(255) default NULL");
482 setChange("ALTER TABLE tt_projects MODIFY name varchar(80) COLLATE utf8_bin NOT NULL");
483 setChange("ALTER TABLE tt_users MODIFY login varchar(50) COLLATE utf8_bin NOT NULL");
484 setChange("ALTER TABLE tt_tasks MODIFY name varchar(80) COLLATE utf8_bin NOT NULL");
485 setChange("ALTER TABLE tt_invoices MODIFY name varchar(80) COLLATE utf8_bin NOT NULL");
486 setChange("ALTER TABLE tt_clients MODIFY name varchar(80) COLLATE utf8_bin NOT NULL");
487 setChange("ALTER TABLE tt_clients ADD COLUMN projects text default NULL");
488 setChange("ALTER TABLE tt_custom_field_log ADD COLUMN option_id int(11) default NULL");
489 setChange("ALTER TABLE tt_teams ADD COLUMN tracking_mode smallint(2) NOT NULL DEFAULT '2'");
490 setChange("ALTER TABLE tt_teams ADD COLUMN record_type smallint(2) NOT NULL DEFAULT '0'");
491 setChange("ALTER TABLE tt_invoices DROP start_date");
492 setChange("ALTER TABLE tt_invoices DROP end_date");
495 if ($_POST["convert1600to1900"]) {
496 setChange("DROP TABLE IF EXISTS tt_invoice_headers");
497 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `client_id` int(11) default NULL");
498 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `cf_1_option_id` int(11) default NULL");
499 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `show_client` tinyint(4) NOT NULL default '0'");
500 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `show_invoice` tinyint(4) NOT NULL default '0'");
501 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `group_by` varchar(20) default NULL");
502 setChange("CREATE TABLE `tt_expense_items` (`id` bigint NOT NULL auto_increment, `date` date NOT NULL, `user_id` int(11) NOT NULL, `client_id` int(11) default NULL, `project_id` int(11) default NULL, `name` varchar(255) NOT NULL, `cost` decimal(10,2) default '0.00', `invoice_id` int(11) default NULL, PRIMARY KEY (`id`))");
503 setChange("create index date_idx on tt_expense_items(date)");
504 setChange("create index user_idx on tt_expense_items(user_id)");
505 setChange("create index client_idx on tt_expense_items(client_id)");
506 setChange("create index project_idx on tt_expense_items(project_id)");
507 setChange("create index invoice_idx on tt_expense_items(invoice_id)");
508 setChange("ALTER TABLE tt_fav_reports DROP sort_by");
509 setChange("ALTER TABLE tt_fav_reports DROP show_empty_days");
510 setChange("ALTER TABLE tt_invoices DROP discount");
511 setChange("ALTER TABLE tt_users ADD COLUMN `client_id` int(11) default NULL");
512 setChange("ALTER TABLE tt_teams ADD COLUMN `decimal_mark` char(1) NOT NULL default '.'");
513 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `invoice` tinyint(4) default NULL");
514 setChange("CREATE TABLE `tt_cron` (`id` int(11) NOT NULL auto_increment, `cron_spec` varchar(255) NOT NULL, `last` int(11) default NULL, `next` int(11) default NULL, `report_id` int(11) default NULL, `email` varchar(100) default NULL, `status` tinyint(4) default '1', PRIMARY KEY (`id`))");
515 setChange("ALTER TABLE tt_cron ADD COLUMN `team_id` int(11) NOT NULL");
516 setChange("create index client_idx on tt_client_project_binds(client_id)");
517 setChange("create index project_idx on tt_client_project_binds(project_id)");
518 setChange("ALTER TABLE tt_log ADD COLUMN status tinyint(4) default '1'");
519 setChange("ALTER TABLE tt_custom_field_log ADD COLUMN status tinyint(4) default '1'");
520 setChange("ALTER TABLE tt_expense_items ADD COLUMN status tinyint(4) default '1'");
521 setChange("ALTER TABLE tt_invoices ADD COLUMN status tinyint(4) default '1'");
522 setChange("DROP INDEX name_idx on tt_invoices");
523 setChange("create unique index name_idx on tt_invoices(team_id, name, status)");
526 // The update_clients function updates projects field in tt_clients table.
527 if ($_POST["update_clients"]) {
528 $mdb2 = getConnection();
529 $sql = "select id from tt_clients where status = 1 or status = 0";
530 $res = $mdb2->query($sql);
531 if (is_a($res, 'PEAR_Error')) {
532 die($res->getMessage());
535 $clients_updated = 0;
536 // Iterate through clients.
537 while ($val = $res->fetchRow()) {
538 $client_id = $val['id'];
540 // Get projects binds for client.
541 $sql = "select cpb.project_id from tt_client_project_binds cpb
542 left join tt_projects p on (p.id = cpb.project_id)
543 where cpb.client_id = $client_id order by p.name";
545 $result = $mdb2->query($sql);
546 if (is_a($result, 'PEAR_Error'))
547 die($result->getMessage());
549 $project_arr = array();
550 while ($value = $result->fetchRow()) {
551 $project_arr[] = $value['project_id'];
553 $comma_separated = implode(',', $project_arr); // This is a comma-separated list of associated project ids.
555 // Update the projects field.
556 $sql = "update tt_clients set projects = ".$mdb2->quote($comma_separated)." where id = $client_id";
557 $affected = $mdb2->exec($sql);
558 if (is_a($affected, 'PEAR_Error'))
559 die($affected->getMessage());
560 $clients_updated += $affected;
562 print "Updated $clients_updated clients...<br>\n";
565 // The update_custom_fields function updates option_id field field in tt_custom_field_log table.
566 if ($_POST['update_custom_fields']) {
567 $mdb2 = getConnection();
568 $sql = "update tt_custom_field_log set option_id = value where field_id in (select id from tt_custom_fields where type = 2)";
569 $affected = $mdb2->exec($sql);
570 if (is_a($affected, 'PEAR_Error'))
571 die($affected->getMessage());
573 print "Updated $affected custom fields...<br>\n";
576 // The update_tracking_mode function sets the tracking_mode field in tt_teams table to 2 (== MODE_PROJECTS_AND_TASKS).
577 if ($_POST['update_tracking_mode']) {
578 $mdb2 = getConnection();
579 $sql = "update tt_teams set tracking_mode = 2 where tracking_mode = 0";
580 $affected = $mdb2->exec($sql);
581 if (is_a($affected, 'PEAR_Error'))
582 die($affected->getMessage());
584 print "Updated $affected teams...<br>\n";
587 if ($_POST["cleanup"]) {
589 $mdb2 = getConnection();
590 $inactive_teams = ttTeamHelper::getInactiveTeams();
592 $count = count($inactive_teams);
593 print "$count inactive teams found...<br>\n";
594 for ($i = 0; $i < $count; $i++) {
595 print " deleting team ".$inactive_teams[$i]."<br>\n";
596 $res = ttTeamHelper::delete($inactive_teams[$i]);
599 setChange("OPTIMIZE TABLE tt_client_project_binds");
600 setChange("OPTIMIZE TABLE tt_clients");
601 setChange("OPTIMIZE TABLE tt_config");
602 setChange("OPTIMIZE TABLE tt_custom_field_log");
603 setChange("OPTIMIZE TABLE tt_custom_field_options");
604 setChange("OPTIMIZE TABLE tt_custom_fields");
605 setChange("OPTIMIZE TABLE tt_expense_items");
606 setChange("OPTIMIZE TABLE tt_fav_reports");
607 setChange("OPTIMIZE TABLE tt_invoices");
608 setChange("OPTIMIZE TABLE tt_log");
609 setChange("OPTIMIZE TABLE tt_project_task_binds");
610 setChange("OPTIMIZE TABLE tt_projects");
611 setChange("OPTIMIZE TABLE tt_tasks");
612 setChange("OPTIMIZE TABLE tt_teams");
613 setChange("OPTIMIZE TABLE tt_tmp_refs");
614 setChange("OPTIMIZE TABLE tt_user_project_binds");
615 setChange("OPTIMIZE TABLE tt_users");
626 <table width="80%" border="1" cellpadding="10" cellspacing="0">
628 <td width="80%"><b>Create database structure (v1.9)</b>
629 <br>(applies only to new installations, do not execute when updating)</br></td><td><input type="submit" name="crstructure" value="Create"></td>
635 <table width="80%" border="1" cellpadding="10" cellspacing="0">
637 <td>Update database structure (v0.5 to v0.7)</td><td><input type="submit" name="convert5to7" value="Update"></td>
640 <td>Update database structure (v0.7 to v1.3.3)</td>
641 <td><input type="submit" name="convert7to133" value="Update"><br><input type="submit" name="update_projects" value="Update projects"></td>
644 <td>Update database structure (v1.3.3 to v1.3.40)</td>
645 <td><input type="submit" name="convert133to1340" value="Update"><br><input type="submit" name="update_companies" value="Update companies"></td>
648 <td>Update database structure (v1.3.40 to v1.4.85)</td>
649 <td><input type="submit" name="convert1340to1485" value="Update"><br><input type="submit" name="update_to_team_id" value="Update team_id"></td>
652 <td>Update database structure (v1.4.85 to v1.5.79)</td>
653 <td><input type="submit" name="convert1485to1579" value="Update"></td>
656 <td>Update database structure (v1.5.79 to v1.6)</td>
657 <td><input type="submit" name="convert1579to1600" value="Update"><br><input type="submit" name="update_clients" value="Update clients"><br><input type="submit" name="update_custom_fields" value="Update custom fields"><br><input type="submit" name="update_tracking_mode" value="Update tracking mode"></td>
660 <td>Update database structure (v1.6 to v1.9)</td>
661 <td><input type="submit" name="convert1600to1900" value="Update"><br></td>
665 <h2>DB Maintenance</h2>
666 <table width="80%" border="1" cellpadding="10" cellspacing="0">
668 <td width="80%">Clean up DB from inactive teams</td><td><input type="submit" name="cleanup" value="Clean up"></td>