Location: PHPKode > projects > TEMS -Time and Expense Management System > tems/expenseheaders/DbObj.php
<?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/expenseheaders/config.php");
require_once("$basedir/invoices/DbObj.php");
require_once("$basedir/baseclass/GetAuthorization.php");

class ExpenseHeaderData extends DBCommonFunctions {

    public function createRow($usid, $pjid, $wedt, $desc, $cmnt, $locn) {

        global $expenseheaders_status_array;
        $weekendday = $_SESSION['company']['weekendday'];

        if ($usid == ''
           or $pjid == ''
           or $wedt == ''
           or $desc == ''
           or $locn == '')
            throw new iInvalidArgumentException();

        if (!chkWeekendDateIso($wedt, $weekendday))
            throw new iInvalidDataException();

        $susid = $this->escapeString($usid);
        $spjid = $this->escapeString($pjid);
        $swedt = $this->escapeString($wedt);
        $sdesc = $this->escapeString($desc);
        $scmnt = $this->escapeString($cmnt);
        $slocn = $this->escapeString($locn);
        $loggedinUser = loggedUserID();

        $DbObj = new dbObj();
        $where = "users_id = '$susid' and projects_id = '$spjid' ";
        $RowData = $DbObj->fetchRowbyWhereClause('users_projects_roles', $where);
        if ($RowData['status'] <> '10')
            throw new iBLError('users_id', 'er0034');

        $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');


        $this->beginTransaction();

        $Invoicehdr = new InvoiceData();
        $invoiceid = $Invoicehdr->createOpenInvoice($billToAccountId);
        try {
            $query = "INSERT INTO expenseheaders
                                        (users_id, projects_id, weekenddate, description, comment, location,
                                        status, invoices_id, createby, changeby, createat, changeat)
                                VALUES('$susid', '$spjid', '$swedt', '$sdesc', '$scmnt', '$slocn',
                                        '10', '$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, $desc, $cmnt, $locn) {

        global $expenseheaderss_status_array;

        if ($uid == ''
           or $desc == ''
           or $locn == '')
            throw new iInvalidArgumentException();

        $suid = $this->escapeString($uid);
        $sdesc = $this->escapeString($desc);
        $scmnt = $this->escapeString($cmnt);
        $slocn = $this->escapeString($locn);

        $DbObj = new dbObj();
        $where = "uid = '$suid' ";
        $RowData = $DbObj->fetchRowbyWhereClause('expenseheaders', $where);
        if ($RowData['status'] >= '30')
            throw new iBLError('nocategory', 'er0066');

        $loggedinUser = loggedUserID();

        $auth = new userauthorization();
        $authority = $auth->chkauthorityLevel('expenseheaders', 'Edit');

        if (($authority == 'none')
           or (($authority == 'own')
               and ($this->getRecordCreator('expenseheaders', $uid) <> $loggedinUser)
               and ($this->getRecordUserid('expenseheaders', $uid) <> $loggedinUser)))
                    throw new iBLError('nocategory', 'er0041');

        $this->beginTransaction();
        try {
            $query = "UPDATE expenseheaders
                         SET
                            description = '$sdesc',
                            comment = '$scmnt',
                            location = '$slocn',
                            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 submitExpense($uid) {
        global $expenseheaderss_status_array;

        if ($uid == '')
            throw new iInvalidArgumentException();

        $suid = $this->escapeString($uid);
        $DbObj = new dbObj();
        $where = "uid = '$suid' ";
        $RowData = $DbObj->fetchRowbyWhereClause('expenseheaders', $where);
        if ($RowData['status'] >= '20')
            throw new iBLError('nocategory', 'er0035');

        $where = "users_id = '{$RowData['users_id']}' and projects_id = '{$RowData['projects_id']}' and weekenddate = '{$RowData['weekenddate']}'";
        $detailCount = $DbObj->countRows('expensedetails', $where);
        if ($detailCount == 0)
            throw new iBLError('nocategory', 'er0064');

        $RowData = $DbObj->fetchRow('projects', 'projects_id', $RowData['projects_id']);
        if ($RowData['status'] <> '10')
            throw new iBLError('projects_id', 'er0043');

        $RowData = $DbObj->fetchRow('accounts', 'accounts_id', $RowData['accounts_id']);
        if ($RowData['status'] <> '10')
            throw new iBLError('projects_id', 'er0044');

        $loggedinUser = loggedUserID();

        $this->beginTransaction();
        try {
            $query = "UPDATE expenseheaders
			 SET
                             status = '20',
                             submitdate= now(),
                             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 verifyExpense($uid) {
        global $expenseheaderss_status_array;

        if ($uid == '')
            throw new iInvalidArgumentException();

        $suid = $this->escapeString($uid);
        $DbObj = new dbObj();
        $where = "uid = '$suid' ";
        $RowData = $DbObj->fetchRowbyWhereClause('expenseheaders', $where);
        if ($RowData['status'] >= '30')
            throw new iBLError('nocategory', 'er0040');

        if ($RowData['status'] <> '20')
            throw new iBLError('nocategory', 'er0037');

        $where = "users_id = '{$RowData['users_id']}' and projects_id = '{$RowData['projects_id']}' and weekenddate = '{$RowData['weekenddate']}'";
        $detailCount = $DbObj->countRows('expensedetails', $where);
        if ($detailCount == 0)
            throw new iBLError('nocategory', 'er0065');

        $RowData = $DbObj->fetchRow('projects', 'projects_id', $RowData['projects_id']);
        if ($RowData['status'] <> '10')
            throw new iBLError('projects_id', 'er0043');

        $RowData = $DbObj->fetchRow('accounts', 'accounts_id', $RowData['accounts_id']);
        if ($RowData['status'] <> '10')
            throw new iBLError('projects_id', 'er0044');

        $loggedinUser = loggedUserID();

        $auth = new userauthorization();
        $authority = $auth->chkauthorityLevel('expenseheaders', 'Verify');
        if (($authority == 'none')
                or (($authority == 'own')
                and ($this->getRecordCreator('expenseheaders', $uid) <> $loggedinUser)
                and ($this->getRecordUserid('expenseheaders', $uid) <> $loggedinUser)))
            throw new iBLError('nocategory', 'er0038');

        $this->beginTransaction();
        try {
            $query = "UPDATE expenseheaders
						 SET
       						 status = '30',
       						 approvedate = now(),
       						 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 HoldExpense($uid) {
        $suid = $this->escapeString($uid);

        $DbObj = new dbObj();
        $RowData = $DbObj->fetchRow('expenseheaders', 'uid', $suid);

        if ($RowData['status'] >= '30')         // If Expense is already approved, cannot hold.
            throw new iBLError('nocategory', 'er0074');

        $this->UpdateStatusByUid($suid, '80');

        return;
    }

    public function ReleaseExpense($uid) {
        $suid = $this->escapeString($uid);

        $DbObj = new dbObj();
        $RowData = $DbObj->fetchRow('expenseheaders', 'uid', $suid);

        if ($RowData['status'] <> '80')         // If Expense is not held, cannot be released.
            throw new iBLError('nocategory', 'er0075');

        $this->UpdateStatusByUid($suid, '10');

        return;
    }

    public function UpdateStatusByUid($uid, $newStatus) {
        $suid = $this->escapeString($uid);
        $user = loggedUserID();

        $this->beginTransaction();
        try {
            $query = "UPDATE expenseheaders
			 SET
                             status = $newStatus,
			     changeby = '$user'
                       WHERE uid ='$suid' ";

            $conn = $this->getConnection();
            $conn->query($query);
            $this->chkQueryError($conn, $query);

            $this->commitTransaction();
        } catch (Exception $e) {
            $this->rollbackTransaction();
            throw $e;
        }
    }

    public function GetTotalExpense($uid) {

        $suid = $this->escapeString($uid);
        $query = "select sum(ed.amount) as totalamount
					from expensedetails ed, expenseheaders eh
					where eh.users_id = ed.users_id
					  and eh.projects_id = ed.projects_id
					  and eh.weekenddate = ed.weekenddate
					  and eh.uid = '$suid'";

        $Data = new dbObj();
        $RowData = $Data->getDatabyQuery($query);
        return ($RowData == null) ? 0 : $RowData[0]['totalamount'];
    }

}

?>
Return current item: TEMS -Time and Expense Management System