<?php
/* * *******************************************************************************
* TES is a Time and Expense Management program developed by
* Initechs, LLC. Copyright (C) 2009 - 2010 Initechs LLC.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY INITECHS, INITECHS DISCLAIMS THE WARRANTY
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* 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, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* You can contact Initechs headquarters at 1841 Piedmont Road, Suite 301,
* Marietta, GA, USA. or at email address hide@address.com
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display od the "Initechs" logo.
* If the display of the logo is not reasonably feasible for technical reasons,
* the Appropriate Legal Notices must display the words "Powered by Initechs".
* ****************************************************************************** */
$basedir = dirname(__FILE__) . '/..';
require_once("$basedir/baseclass/DBCommonFunctions.php");
require_once("$basedir/expensedetails/config.php");
require_once("$basedir/invoices/DbObj.php");
require_once("$basedir/baseclass/GetAuthorization.php");
class ExpenseDetailData extends DBCommonFunctions {
public function createRow($usid, $pjid, $wedt, $esdt, $catg, $desc, $cmnt, $amnt, $mile) {
if ($usid == ''
or $pjid == ''
or $wedt == ''
or $esdt == ''
or $catg == ''
or $desc == ''
)
throw new iInvalidArgumentException();
if (!isValidDate($wedt, 'ymd'))
throw new iInvalidDataException();
if (!isValidDate($esdt, 'ymd'))
throw new iInvalidDataException();
$susid = $this->escapeString($usid);
$spjid = $this->escapeString($pjid);
$swedt = $this->escapeString($wedt);
$sesdt = $this->escapeString($esdt);
$scatg = $this->escapeString($catg);
$sdesc = $this->escapeString($desc);
$samnt = $this->escapeString($amnt);
if ($samnt == '')
$samnt = '0';
$smile = $this->escapeString($mile);
if ($smile == '')
$smile = '0';
$scmnt = $this->escapeString($cmnt);
$loggedinUser = loggedUserID();
$wsdt = tesAddDate($swedt, -6);
if (($sesdt < $wsdt)
or ($sesdt > $swedt))
throw new iBLError('expensedate', 'er0045');
$DbObj = new dbObj();
$RowData = $DbObj->fetchRow('expensecategories', 'expensecategories_id', $scatg);
if ($RowData['status'] <> '10')
throw new iBLError('expensecategories_id', 'er0033');
if (($RowData['ismileage'] == '1')
and ($smile == '0')) {
throw new iBLError('mile', 'er0031');
} else if (($RowData['ismileage'] == '0')
and ($samnt == '0')) {
throw new iBLError('amount', 'er0032');
}
if ($RowData['ismileage'] == '1')
$samnt = $smile * $RowData['mileagerate'];
$RowData = $DbObj->fetchRow('projects', 'projects_id', $spjid);
if ($RowData['status'] <> '10')
throw new iBLError('projects_id', 'er0043');
$billToAccountId = $RowData['billtoaccount_id'];
$RowData = $DbObj->fetchRow('accounts', 'accounts_id', $RowData['accounts_id']);
if ($RowData['status'] <> '10')
throw new iBLError('projects_id', 'er0044');
$where = "users_id = '$susid' and projects_id = '$spjid' and weekenddate = '$swedt' ";
$RowData = $DbObj->fetchRowbyWhereClause('expenseheaders', $where);
if ($RowData['status'] >= '30')
throw new iBLError('nocategory', 'er0036');
$this->beginTransaction();
$Invoicehdr = new InvoiceData();
$invoiceid = $Invoicehdr->createOpenInvoice($billToAccountId);
try {
$query = "INSERT INTO expensedetails
(users_id, projects_id, weekenddate, expensedate, expensecategories_id, description,
amount, mile, comment, invoices_id, createby, changeby, createat, changeat)
VALUES('$susid', '$spjid', '$swedt', '$sesdt', '$scatg', '$sdesc',
'$samnt', '$smile', '$scmnt', '$invoiceid', '$loggedinUser', '$loggedinUser', now(), now())";
$conn = $this->getConnection();
$conn->query($query);
$recid = $conn->insert_id;
$this->chkQueryError($conn, $query);
$this->commitTransaction();
return $recid;
} catch (Exception $e) {
$this->rollbackTransaction();
throw $e;
}
}
public function updateRow($uid, $esdt, $catg, $desc, $cmnt, $amnt, $mile) {
if ($uid == ''
or $esdt == ''
or $catg == ''
or $desc == ''
)
throw new iInvalidArgumentException();
$suid = $this->escapeString($uid);
$sesdt = $this->escapeString($esdt);
$scatg = $this->escapeString($catg);
$sdesc = $this->escapeString($desc);
$scmnt = $this->escapeString($cmnt);
$samnt = $this->escapeString($amnt);
if ($samnt == '')
$samnt = '0';
$smile = $this->escapeString($mile);
if ($smile == '')
$smile = '0';
$loggedinUser = loggedUserID();
$DbObj = new dbObj();
$RowData = $DbObj->fetchRow('expensedetails', 'uid', $suid);
$wsdt = tesAddDate($RowData['weekenddate'], -6);
if (($sesdt < $wsdt)
or ($sesdt > $RowData['weekenddate']))
throw new iBLError('expensedate', 'er0045');
$DbObj = new dbObj();
$RowData = $DbObj->fetchRow('expensecategories', 'expensecategories_id', $scatg);
if ($RowData['status'] <> '10')
throw new iBLError('expensecategories_id', 'er0033');
if (($RowData['ismileage'] == '1')
and ($smile == '0')) {
throw new iBLError('mile', 'er0031');
} else if (($RowData['ismileage'] == '0')
and ($samnt == '0')) {
throw new iBLError('amount', 'er0032');
}
if ($RowData['ismileage'] == '1')
$samnt = $smile * $RowData['mileagerate'];
$where = "uid = '$suid' ";
$ED = $DbObj->fetchRowbyWhereClause('expensedetails', $where);
$where = "users_id = '{$ED['users_id']}' and projects_id = '{$ED['projects_id']}' and weekenddate = '{$ED['weekenddate']}' ";
$EH = $DbObj->fetchRowbyWhereClause('expenseheaders', $where);
if ($EH['status'] >= '30')
throw new iBLError('nocategory', 'er0036');
$auth = new userauthorization();
$authority = $auth->chkauthorityLevel('expensedetails', 'Edit');
if (($authority == 'none')
or (($authority == 'own')
and ($this->getRecordCreator('expensedetails', $uid) <> $loggedinUser)
and ($this->getRecordUserid('expensedetails', $uid) <> $loggedinUser)))
throw new iBLError('nocategory', 'er0041');
$this->beginTransaction();
try {
$query = "UPDATE expensedetails
SET
expensedate = '$sesdt',
expensecategories_id = '$scatg',
description = '$sdesc',
comment = '$scmnt',
amount = '$samnt',
mile = '$smile',
changeby ='$loggedinUser',
changeat= now()
WHERE uid = '$uid'";
$conn = $this->getConnection();
$conn->query($query);
$this->chkQueryError($conn, $query);
$this->commitTransaction();
} catch (Exception $e) {
$this->rollbackTransaction();
throw $e;
}
}
public function deleteRow($table, $uid) {
if ($table == ''
or $uid == '')
throw new iInvalidArgumentException();
$suid = $this->escapeString($uid);
$DbObj = new dbObj();
$where = "uid = '$suid' ";
$ED = $DbObj->fetchRowbyWhereClause('expensedetails', $where);
$where = "users_id = '{$ED['users_id']}' and projects_id = '{$ED['projects_id']}' and weekenddate = '{$ED['weekenddate']}' ";
$EH = $DbObj->fetchRowbyWhereClause('expenseheaders', $where);
if ($EH['status'] >= '30')
throw new iBLError('nocategory', 'er0036');
parent::deleteRow($table, $uid);
}
public function GetTotalExpense($users_id, $projects_id, $weekenddate) {
$query = "select sum(ed.amount) as totalamount
from expensedetails ed
where ed.users_id = '$users_id'
and ed.projects_id = '$projects_id'
and ed.weekenddate = '$weekenddate'";
$Data = new dbObj();
$RowData = $Data->getDatabyQuery($query);
return ($RowData == null) ? 0 : $RowData[0]['totalamount'];
}
public function GetStatus($users_id, $projects_id, $weekenddate) {
$query = "select status
from expenseheaders
where users_id = '$users_id'
and projects_id = '$projects_id'
and weekenddate = '$weekenddate'";
$Data = new dbObj();
$RowData = $Data->getDatabyQuery($query);
return ($RowData == null) ? 0 : $RowData[0]['status'];
}
}
?>