Source for file company.class.php

Documentation is available at company.class.php


1 <?php
2 /**
3 * @version $Id: company.class.php,v 1.6 2003/09/17 10:42:18 cybot_tm Exp $
4 * @category phpTimeSheet
5 * @package phpTimeSheet
6 */
7
8 /**
9 * Class Company reflects Table company
10 *
11 * @package phpTimeSheet
12 * @version $Id: company.class.php,v 1.6 2003/09/17 10:42:18 cybot_tm Exp $
13 */
14 class Company
15 {
16 /**
17 * @var int Company ID
18 * @access protected
19 * @since 2003-08-31
20 */
21 var $id = 0; // Database id
22
23 /**
24 * @var string Company Name
25 * @access protected
26 * @since 2003-08-31
27 */
28 var $name = ''; // Company name
29
30 /**
31 * @var int Number of Workers in this Company
32 * @access protected
33 * @since 2003-08-31
34 */
35 var $numworkers = 0; // Number of workers employed by company
36
37 /**
38 * @var array Worker-ID => Worker-Name
39 * @access protected
40 * @since 2003-08-31
41 */
42 var $workers; // Array of worker names, indexed by ddb indices.
43
44 /**
45 * @var int Number of Projects
46 * @access protected
47 * @since 2003-08-31
48 */
49 var $numpids = 0; // Number of PIDs this company has hours on
50
51 /**
52 * @var array Project-ID => Project-Name
53 * @access protected
54 * @since 2003-08-31
55 */
56 var $pids; // Array of PIDs indexed by pid id
57
58 /**
59 * @var array Project-ID => Project-Hours
60 * @access protected
61 * @since 2003-08-31
62 */
63 var $pidhours; // Array of hours indexed by PID id
64
65 /**
66 * @return bool success
67 * @param int $company_id Company ID
68 */
69 function Company($company_id = 0)
70 {
71 $this->SetId($company_id);
72
73 if ( $this->GetId() === 0 )
74 {
75 return true;
76 }
77
78 $sql = '
79 SELECT `coname`
80 FROM `' . PTS_TBL_COMPANY . '`
81 WHERE `coindex` = ' . $this->GetId();
82
83 $result = mysql_query($sql) or die('<pre>' . __FILE__ . ': ' . __LINE__ . "\n" . __CLASS__ . '::' . __FUNCTION__ . "\n" . $sql . "\n" . mysql_error());
84
85 $this->SetName(mysql_result($result, 0, 0));
86
87 return true;
88 }
89
90 function GetId() { return $this->id; }
91 function GetName() { return $this->name; }
92 function GetNumPids() { return $this->numpids; }
93 function GetNumWorkers() { return $this->numworkers; }
94
95
96 function SetId($company_id)
97 {
98 $this->id = (int) $company_id;
99 return true;
100 }
101
102 function SetName($company_name)
103 {
104 $this->name = trim($company_name);
105 return true;
106 }
107
108 function SetNumPids($numpids)
109 {
110 $this->numpids = (int) $numpids;
111 return true;
112 }
113
114 function SetNumWorkers($numworkers)
115 {
116 $this->numworkers = (int) $numworkers;
117 return true;
118 }
119
120 /**
121 * @return bool success
122 * @desc saves Object in DB
123 */
124 function update_db()
125 {
126 $sql = '
127 UPDATE `' . PTS_TBL_COMPANY . '`
128 SET `coname` = "' . $this->GetName() . '"
129 WHERE `coindex` = ' . $this->GetId();
130 $result = mysql_query($sql) or die('<pre>' . __FILE__ . ': ' . __LINE__ . "\n" . __CLASS__ . '::' . __FUNCTION__ . "\n" . $sql . "\n" . mysql_error());
131
132 return true;
133 }
134
135 /* legt Firma in DB an */
136 function db_insert()
137 {
138 $sql = 'INSERT INTO `' . PTS_TBL_COMPANY . '` SET `pname` = "' . $this->GetName() . '"';
139 $result = mysql_query($sql) or die('<pre>' . __FILE__ . ': ' . __LINE__ . "\n" . __CLASS__ . '::' . __FUNCTION__ . "\n" . $sql . "\n" . mysql_error());
140
141 $this->SetId(mysql_insert_id());
142 return (bool) $this->GetId();
143 }
144
145 /* gibt Summe der Stunden des Projekts mit PID $pid der Firma zurück */
146 function get_hours_for_pid($project_id)
147 {
148 $sql = '
149 SELECT SUBSTRING_INDEX(SEC_TO_TIME(SUM(TIME_TO_SEC(`time`))), ":", 2)
150 FROM `' . PTS_TBL_PROJECT_TIME . '`
151 WHERE `company` = ' . $this->GetID() . '
152 AND `pid` = ' . $project_id;
153 $result = mysql_query($sql) or die('<pre>' . __FILE__ . ': ' . __LINE__ . "\n" . __CLASS__ . '::' . __FUNCTION__ . "\n" . $sql . "\n" . mysql_error());
154
155 return mysql_result($result, 0, 0);
156 }
157
158 /* Returns an array of all the jobs this company has job name indexed by job id */
159 function get_pids()
160 {
161 $sql = '
162 SELECT DISTINCT `pid`, `pname`
163 FROM `' . PTS_TBL_PROJECT_TIME . '`
164 LEFT JOIN `' . PTS_TBL_PROJECT . '`
165 ON `' . PTS_TBL_PROJECT_TIME . '`.`pid` = `' . PTS_TBL_PROJECT . '`.`pindex`
166 WHERE `' . PTS_TBL_PROJECT_TIME . '`.`company` = ' . $this->GetId();
167 $result = mysql_query($sql) or die('<pre>' . __FILE__ . ': ' . __LINE__ . "\n" . __CLASS__ . '::' . __FUNCTION__ . "\n" . $sql . "\n" . mysql_error());
168
169 while ( $row = mysql_fetch_assoc($result) )
170 {
171 $this->pids[$row['pid']] = $row['pname'];
172 }
173 $this->SetNumPids(count($this->pids));
174
175 return $this->pids;
176 }
177
178 /* gibt Array mit allen Angestellten zurück, de bei dieser Firma arbeiten,
179 * sortiert nach Name */
180 function get_workers()
181 {
182 $sql = '
183 SELECT *
184 FROM `' . PTS_TBL_WORKER . '`
185 WHERE `company` = ' . $§this->GetId() . '
186 ORDER BY `wname`';
187 $result = mysql_query($sql) or die('<pre>' . __FILE__ . ': ' . __LINE__ . "\n" . __CLASS__ . '::' . __FUNCTION__ . "\n" . $sql . "\n" . mysql_error());
188
189 if ( mysql_num_rows($result) < 1 )
190 {
191 $this->numworkers = 0;
192 return(0);
193 }
194
195 while ( $row = mysql_fetch_assoc($result) )
196 {
197 $this->workers[$row['windex']] = $row['wname'];
198 }
199 $this->SetNumWorkers(count($this->workers));
200
201 return $this->workers;
202 }
203
204 function report_form($template)
205 {
206 $comp_target = $this->GetId();
207 $comp_name = $this->GetName();
208
209 //$comp_workers = $this->get_workers();
210 //$numworkers = $this->numworkers;
211
212 $comp_pids = $this->get_pids();
213 $numpids = $this->numpids;
214 $this->get_workers();
215 $numworkers = $this->numworkers;
216 $total_hours = 0;
217 if ( $numpids > 0 )
218 {
219 while ( list($key, $val) = each($comp_pids) )
220 {
221 $thispidhours = $this->get_hours_for_pid($key);
222 $this->pidhours[$key] = $thispidhours;
223 $pidarray[$key] = sprintf("%.2f:%s", $thispidhours, $val);
224 $total_hours += $thispidhours;
225 }
226 }
227 $pidhours = $this->pidhours;
228
229 if ( isset($comp_pids) )
230 {
231 reset($comp_pids);
232 }
233 include($template);
234 }
235
236 function modify_form($template)
237 {
238 global $password
239
240 $comp_target = $this->GetId();
241 $comp_name = $this->GetName();
242
243 $comp_workers = $this->get_workers();
244 $numworkers = $this->GetNumWorkers();
245
246 $comp_pids = $this->get_pids();
247 $numpids = $this->GetNumPids();
248
249 if ( $numpids > 0 )
250 {
251 foreach ( $comp_pids as $project_id => $project_name )
252 {
253 $thispidhours = $this->get_hours_for_pid($project_id);
254 $this->pidhours[$project_id] = $thispidhours;
255 }
256 }
257 $pidhours = $this->pidhours;
258
259 include $template;
260
261 return true;
262 }
263
264 /**
265 * @static
266 */
267 function GetSelectBox($only_active = true, $selected_company_id = 0)
268 {
269 $sql = 'SELECT `coindex`, `coname` FROM `' . PTS_TBL_COMPANY . '` ORDER BY `coname`';
270 $result = mysql_query($sql) or die('<pre>' . __FILE__ . ': ' . __LINE__ . "\n" . __CLASS__ . '::' . __FUNCTION__ . "\n" . $sql . "\n" . mysql_error());
271
272 $selectbox = '<select name="company_id">';
273 while ( $row = mysql_fetch_assoc($result) )
274 {
275 if ( $selected_company_id == $row['coindex'] )
276 {
277 $selected = ' selected="selected"';
278 }
279 else
280 {
281 $selected = '';
282 }
283 $selectbox .= '<option value="' . $row['coindex'] . '"' . $selected . '>' . $row['coname'] . '</option>';
284 }
285 $selectbox .= '</select>';
286
287 return $selectbox;
288 }
289 }
290 ?>

Documentation generated on Fri, 26 Sep 2003 15:39:35 +0200 by phpDocumentor 1.2.2