1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/*
* Copyright (C) 2007-2008, M2X
*
* Authors: Jean-Paul Saman
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
// use the dPFramework to have easy database operations (store, delete etc.) by using its ObjectOrientedDesign
// therefore we have to create a child class for the module timesheet
// a class named (like this) in the form: module/module.class.php is automatically loaded by the dPFramework
/**
* @package dotProject
* @subpackage modules
* @version $Revision: 1.0 $
*/
// include the powerful parent class that we want to extend for timesheet
// use the dPFramework for easy inclusion of this class here
require_once( $AppUI->getSystemClass('dp') );
/**
* The Timesheet Class
*/
class CTimesheet extends CDpObject {
// link variables to the timesheet object (according to the existing columns in the database table timesheet)
//use null for a new object, so the database automatically assigns an unique id by 'not null'-functionality
var $timesheet_id = null;
var $timesheet_period = null;
var $timesheet_status = null;
var $timesheet_date = null;
var $timesheet_creator = null;
var $timesheet_worked = null;
// the constructor of the CTimesheet class, always combined with the table name and the unique key of the table
function CTimesheet() {
$this->CDpObject( 'timesheet', 'timesheet_id' );
$this->timesheet_id=$_POST["timesheet_id"];
}
function check() {
if (!intval($this->timesheet_status))
$this->timesheet_status=0;
if ( $this->timesheet_id == "0" ) {
$this->init();
}
return NULL;
}
function init() {
$q = new DBQuery();
$q->addTable('timesheet');
$q->addInsert('timesheet_period,timesheet_status,timesheet_date,timesheet_creator,timesheet_worked',
$this->timesheet_period.','.$_POST['timesheet_status'].','.
$_POST['timesheet_date'].','.$_POST['timesheet_creator'].','.
$_POST['timesheet_worked'], true);
$q->exec();
$this->timesheet_id = db_insert_id();
}
function project_purge()
{
$q = new DBQuery();
$q->setDelete('timesheet_project');
$q->addWhere('timesheet_id = '. $this->timesheet_id );
if (!$q->exec())
return db_error();
}
function project_store()
{
// Get the current timesheet period
if (intval($_POST['ts_period'])) {
$period = new CDate();
$period->setMonth($_POST['ts_period']);
}
else
$period = new CDate();
$q = new DBQuery();
$q->addQuery('project_id');
$q->addQuery('project_name');
$q->addQuery('project_status');
$q->addTable('projects');
$q->addOrder('project_name ASC');
$projects = $q->LoadList();
foreach ($projects as $row) {
$t = new DBQuery;
$t->addQuery('task_id');
$t->addQuery('task_name');
$t->addQuery('task_owner');
$t->addQuery('task_start_date');
$t->addQuery('task_hours_worked');
$t->addTable('tasks');
$t->addWhere('task_project = '. $row["project_id"]);
$t->addOrder('task_start_date DESC');
$tasks = $t->LoadList();
$t->Clear();
$amount = 0;
foreach ($tasks as $item) {
// Query the task_log table for actual start dates.
$t->addQuery('task_log_id');
$t->addQuery('task_log_name');
$t->addQuery('task_log_creator');
$t->addQuery('task_log_date');
$t->addQuery('task_log_hours');
$t->addTable('task_log');
$t->addWhere('task_log_task = '. $item["task_id"]);
// What if this is different then current user?
$t->addWhere('task_log_creator = ' . $this->timesheet_creator);
$t->addOrder('task_log_date DESC');
// TODO: select on user and date
$log = $t->LoadList();
foreach ($log as $logitem) {
if (intval($logitem['task_log_date']))
{
$taskDate = new CDate($logitem['task_log_date']);
if ($period->GetMonth() == $taskDate->GetMonth()) {
$amount = $amount + $logitem["task_log_hours"];
}
// else do not account the worked hours to the project for this month
}
}
}
$sheet = new DBQuery();
$sheet->addTable('timesheet_project');
$sheet->addInsert('timesheet_id,timesheet_project,timesheet_project_amount',
$this->timesheet_id.','.$row["project_id"].','.$amount, true);
if (!$sheet->exec()) {
echo db_error();
}
}
}
function store() {
if ($this->timesheet_id != 0) {
$this->_action='updated';
$q = new DBQuery();
$q->addTable('timesheet');
$q->addUpdate('timesheet_worked', $this->timesheet_worked);
$q->addWhere('timesheet_id = ' . $this->timesheet_id);
if (!$q->exec()) {
echo db_error();
}
}
else {
if (!intval($this->timesheet_status))
$this->timesheet_status=0;
$q = new DBQuery;
$q->addTable('timesheet');
$q->addInsert('timesheet_period,timesheet_status,timesheet_date,timesheet_creator,timesheet_worked',
$this->timesheet_period.','.$this->timesheet_status.','.
$this->timesheet_date.','.$this->timesheet_creator.',' .
$this->timesheet_worked, true);
if (!$q->exec())
echo db_error();
}
}
// overload the delete method of the parent class for adaptation for timesheet's needs
function delete() {
$q = new DBQuery();
$q->setDelete('timesheet_project');
$q->addWhere('timesheet_id = '. $this->timesheet_id );
if (!$q->exec()) {
return db_error();
}
$q->clear();
$q->setDelete('timesheet');
$q->addWhere('timesheet_id = '. $this->timesheet_id );
if (!$q->exec()) {
return db_error();
} else {
return NULL;
}
}
function change_status($stat)
{
$msg = $this->check();
$this->_action='updated';
$q = new DBQuery();
$q->addTable('timesheet');
$q->addUpdate('timesheet_status', $this->timesheet_status);
$q->addWhere('timesheet_id = ' . $this->timesheet_id);
if (!$q->exec()) {
return db_error();
}
// Why is this needed ??
if ($stat>0) {
$this->project_purge();
$this->project_store();
}
}
function change_period($period)
{
$msg = $this->check();
$this->_action='updated';
$q = new DBQuery();
$q->addTable('timesheet');
$q->addUpdate('timesheet_period', $period);
$q->addWhere('timesheet_id = ' . $this->timesheet_id);
if (!$q->exec()) {
return db_error();
}
}
}
?>