Commit 3f6e6356 authored by Jean-Paul Saman's avatar Jean-Paul Saman

Update and cleanup timesheet.class.php

parent 78011656
<?php <?php
/* /*
* Copyright (C) 2007-2008, M2X * Copyright (C) 2007-2014, M2X
* *
* Authors: Jean-Paul Saman * Authors: Jean-Paul Saman
* *
...@@ -19,76 +19,82 @@ ...@@ -19,76 +19,82 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. * 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 * @package web2project
* @subpackage modules * @subpackage modules
* @version $Revision: 1.0 $ * @version $Revision: 2.0 $
*/ */
// include the powerful parent class that we want to extend for timesheet if (!defined('W2P_BASE_DIR')) {
// use the dPFramework for easy inclusion of this class here die('You should not access this file directly.');
require_once( $AppUI->getSystemClass('dp') ); }
/** /**
* The Timesheet Class * The Timesheet Class
*/ */
class CTimesheet extends CDpObject { class CTimesheet extends w2p_Core_BaseObject {
// link variables to the timesheet object (according to the existing columns in the database table timesheet) // 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 //use null for a new object, so the database automatically assigns an unique id by 'not null'-functionality
var $timesheet_id = null; public $timesheet_id = null;
var $timesheet_period = null; public $timesheet_period = null;
var $timesheet_status = 0; public $timesheet_status = 0;
var $timesheet_date = null; public $timesheet_date = null;
var $timesheet_creator = null; public $timesheet_creator = null;
var $timesheet_worked = null; public $timesheet_worked = null;
// the constructor of the CTimesheet class, always combined with the table name and the unique key of the table // the constructor of the CTimesheet class, always combined with the table name and the unique key of the table
function CTimesheet() { public function __construct() {
$this->CDpObject( 'timesheet', 'timesheet_id' ); parent::__construct('timesheet', 'timesheet_id');
$this->timesheet_id=$_POST["timesheet_id"]; $this->timesheet_id=$_POST["timesheet_id"];
} }
function check() { public function isValid() {
if ( $this->timesheet_id == "0" ) { $baseErrorMsg = get_class($this) . '::store-check failed -';
$this->init(); if ($this->timesheet_id == 0) {
$this->_error['timesheet_id'] = $baseErrorMsg . 'timesheet id not set';
} }
return NULL; return (count($this->_error)) ? false : true;
}
public function loadFull() {
// loadFull information for this timesheet
} }
function init() { protected function init() {
$q = new DBQuery(); $q = $this->_getQuery();
$q->addTable('timesheet'); $q->addTable('timesheet');
$q->addInsert('timesheet_period,timesheet_status,timesheet_date,timesheet_creator,timesheet_worked', $q->addInsert('timesheet_period,timesheet_status,timesheet_date,timesheet_creator,timesheet_worked',
$_POST['timesheet_period'].','.$_POST['timesheet_status'].','. $_POST['timesheet_period'].','.$_POST['timesheet_status'].','.
$_POST['timesheet_date'].','.$_POST['timesheet_creator'].','. $_POST['timesheet_date'].','.$_POST['timesheet_creator'].','.
$_POST['timesheet_worked'], true); $_POST['timesheet_worked'], true);
if (!$q->exec()) { if (!($q->exec())) {
return dberror(); $this->_error['init-timesheet'] = db_error();
return false;
} }
// TODO: move this to a postCreate hook
$this->timesheet_id = db_insert_id(); $this->timesheet_id = db_insert_id();
return true;
} }
function project_purge() protected function project_purge()
{ {
$q = new DBQuery(); $q = $this->_getQuery();
$q->setDelete('timesheet_project'); $q->setDelete('timesheet_project');
$q->addWhere('timesheet_id = '. $this->timesheet_id ); $q->addWhere('timesheet_id = '. $this->timesheet_id);
if (!$q->exec()) if (!($q->exec())) {
return db_error(); $this->_error['project-purge-timesheet'] = db_error();
return false;
}
return true;
} }
function project_store() protected function project_store()
{ {
// Get the current timesheet period // Get the current timesheet period
$period = new CDate(); $period = new w2p_UtilitiesDate($_POST['timesheet_period']);
$period->setDate($_POST['timesheet_period'],DATE_FORMAT_UNIXTIME); //$period->setDate($_POST['timesheet_period'],DATE_FORMAT_UNIXTIME);
$q = new DBQuery(); $q = $this->_getQuery();
$q->addQuery('project_id'); $q->addQuery('project_id');
$q->addQuery('project_name'); $q->addQuery('project_name');
$q->addQuery('project_status'); $q->addQuery('project_status');
...@@ -96,7 +102,7 @@ class CTimesheet extends CDpObject { ...@@ -96,7 +102,7 @@ class CTimesheet extends CDpObject {
$q->addOrder('project_name ASC'); $q->addOrder('project_name ASC');
$projects = $q->LoadList(); $projects = $q->LoadList();
foreach ($projects as $row) { foreach ($projects as $row) {
$t = new DBQuery; $t = $this->_getQuery();
$t->addQuery('task_id'); $t->addQuery('task_id');
$t->addQuery('task_name'); $t->addQuery('task_name');
$t->addQuery('task_owner'); $t->addQuery('task_owner');
...@@ -126,7 +132,7 @@ class CTimesheet extends CDpObject { ...@@ -126,7 +132,7 @@ class CTimesheet extends CDpObject {
foreach ($log as $logitem) { foreach ($log as $logitem) {
if (intval($logitem['task_log_date'])) if (intval($logitem['task_log_date']))
{ {
$taskDate = new CDate($logitem['task_log_date']); $taskDate = new w2p_Utilities_Date($logitem['task_log_date']);
if ($period->GetMonth() == $taskDate->GetMonth()) { if ($period->GetMonth() == $taskDate->GetMonth()) {
$amount = $amount + $logitem["task_log_hours"]; $amount = $amount + $logitem["task_log_hours"];
} }
...@@ -134,83 +140,126 @@ class CTimesheet extends CDpObject { ...@@ -134,83 +140,126 @@ class CTimesheet extends CDpObject {
} }
} }
} }
$sheet = new DBQuery(); $sheet = $this->_getQuery();
$sheet->addTable('timesheet_project'); $sheet->addTable('timesheet_project');
$sheet->addInsert('timesheet_id,timesheet_project,timesheet_project_amount', $sheet->addInsert('timesheet_id,timesheet_project,timesheet_project_amount',
$this->timesheet_id.','.$row["project_id"].','.$amount, true); $this->timesheet_id.','.$row["project_id"].','.$amount, true);
if (!$sheet->exec()) { if (!($q->exec())) {
return db_error(); $this->_error['project-purge-timesheet'] = db_error();
return false;
} }
} }
return true;
} }
function store() { public function store($unused = null) {
if ($this->timesheet_id != 0) { $this->clearErrors();
$this->_action='updated';
$q = new DBQuery(); if ($this->timesheet_id != 0 && $this->canEdit()) {
$this->_action='updated'; // FIXME: still needed??
$q = $this->_getQuery();
$q->addTable('timesheet'); $q->addTable('timesheet');
$q->addUpdate('timesheet_worked', $this->timesheet_worked); $q->addUpdate('timesheet_worked', $this->timesheet_worked);
$q->addWhere('timesheet_id = ' . $this->timesheet_id); $q->addWhere('timesheet_id = ' . $this->timesheet_id);
if (!$q->exec()) { if (!($q->exec())) {
return db_error(); $this->_error['update-timesheet'] = db_error();
return false;
} }
return true;
} }
else { else if ($this->canCreate()) {
$q = new DBQuery; $q = $this->_getQuery();
$q->addTable('timesheet'); $q->addTable('timesheet');
$q->addInsert('timesheet_period,timesheet_status,timesheet_date,timesheet_creator,timesheet_worked', $q->addInsert('timesheet_period,timesheet_status,timesheet_date,timesheet_creator,timesheet_worked',
$this->timesheet_period.','.$this->timesheet_status.','. $this->timesheet_period.','.$this->timesheet_status.','.
$this->timesheet_date.','.$this->timesheet_creator.',' . $this->timesheet_date.','.$this->timesheet_creator.',' .
$this->timesheet_worked, true); $this->timesheet_worked, true);
if (!$q->exec()) if (!($q->exec())) {
return db_error(); $this->_error['store-timesheet'] = db_error();
return false;
}
return true;
} }
return false;
} }
// overload the delete method of the parent class for adaptation for timesheet's needs // overload the delete method of the parent class for adaptation for timesheet's needs
function delete() { public function delete($unused = null) {
$q = new DBQuery(); $this->clearErrors();
$q->setDelete('timesheet_project'); if ($this->canDelete()) {
$q->addWhere('timesheet_id = '. $this->timesheet_id ); $q = $this->_getQuery();
if (!$q->exec()) { $q->setDelete('timesheet_project');
return db_error(); $q->addWhere('timesheet_id = '. $this->timesheet_id );
if (!($q->exec())) {
$this->_error['delete-timesheet-project'] = db_error();
return false;
}
$q->clear();
$q->setDelete('timesheet');
$q->addWhere('timesheet_id = '. $this->timesheet_id );
if (!($q->exec())) {
$this->_error['delete-timesheet'] = db_error();
return false;
}
return true;
} }
$q->clear(); return false;
$q->setDelete('timesheet'); }
$q->addWhere('timesheet_id = '. $this->timesheet_id );
if (!$q->exec()) { public function change_status() {
return db_error(); $this->clearErrors();
} else { if ($this->canEdit()) {
return NULL; $this->_action='updated';
$q = $this->_getQuery();
$q->addTable('timesheet');
$q->addUpdate('timesheet_status', $this->timesheet_status);
$q->addWhere('timesheet_id = ' . $this->timesheet_id);
if (!($q->exec())) {
$this->_error['change-status-timesheet'] = db_error();
return false;
}
return true;
} }
return false;
} }
function change_status() public function change_period($period) {
{ $result = false;
$msg = $this->check(); $this->clearErrors();
$this->_action='updated'; if ($this->canEdit()) {
$q = new DBQuery(); $this->_action='updated';
$q->addTable('timesheet'); $q = $this->_getQuery();
$q->addUpdate('timesheet_status', $this->timesheet_status); $q->addTable('timesheet');
$q->addWhere('timesheet_id = ' . $this->timesheet_id); $q->addUpdate('timesheet_period', $period);
if (!$q->exec()) { $q->addWhere('timesheet_id = ' . $this->timesheet_id);
return db_error(); if (!($q->exec())) {
$this->_error['change-period-timesheet'] = db_error();
return $result;
}
$result = true;
$purge_result = $this->project_purge();
$store_result = $this->project_store();
$result = $result && $purge_result && $store_result;
} }
return $result;
} }
function change_period($period) public function get_config() {
{ // Read values from table
$msg = $this->check(); $config = null;
$this->_action='updated'; $q = $this->_getQuery();
$q = new DBQuery(); $q->addQuery('*');
$q->addTable('timesheet'); $q->addTable('timesheet_config');
$q->addUpdate('timesheet_period', $period);
$q->addWhere('timesheet_id = ' . $this->timesheet_id);
if (!$q->exec()) { if (!$q->exec()) {
return db_error(); $AppUI->setMsg(db_error(), UI_MSG_ERROR);
return $config;
} }
$this->project_purge(); while ($row = $q->fetchRow()) {
$this->project_store(); $config[$row['timesheet_config']] = $row['timesheet_value'];
}
$q->clear();
return $config;
} }
} }
?> ?>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment