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

class AccountData extends DBCommonFunctions
{

	public function createRow($aid, $aname, $ad1, $ad2, $acity, $astate, $apostal, $acontact, $aemail, $astat, $abillcycle, $alastbilldate)
	{
		global $accounts_status_array;

		if ($aid == ''
		or $aname == ''
		or $abillcycle == ''
		or $astat == ''
		or $alastbilldate == '')
			throw new iInvalidArgumentException();

		if (!isset($astat))
			throw new iInvalidDataException();

		$said = $this->escapeString($aid);
		$saname= $this->escapeString($aname);
		$sad1= $this->escapeString($ad1);
		$sad2 = $this->escapeString($ad2);
		$sacity = $this->escapeString($acity);
		$sastate = $this->escapeString($astate);
		$sapostal = $this->escapeString($apostal);
		$sacontact= $this->escapeString($acontact);
		$saemail= $this->escapeString($aemail);
		$sastat= $this->escapeString($astat);
		$sabillcycle = $this->escapeString($abillcycle);
		$alastbilldate = tesAddDate($alastbilldate, -1);
		$salastbilldate = $this->escapeString($alastbilldate);
		$loggedinUser = loggedUserID();

		$auth = new userauthorization();
		if ($auth->chkauthorityLevel('accounts', 'Create') == 'none')
			throw new iBLError('nocategory', 'er0041');

		$this->beginTransaction();
		try
		{
			$query = "INSERT INTO accounts (accounts_id, name, address1, address2, city, state, postalcode, contact, email, status, billcycle, lastbilldate, createby, changeby, createat, changeat)
									VALUES('$said', '$saname', '$sad1', '$sad2', '$sacity', '$sastate', '$sapostal', '$sacontact', '$saemail', '$sastat', '$sabillcycle', '$salastbilldate', '$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, $aname, $ad1, $ad2, $acity, $astate, $apostal, $acontact, $aemail, $astat, $abillcycle, $alastbilldate)
	{
		global $accounts_status_array;

		if ($uid == ''
		or $aname == ''
		or $abillcycle == ''
		or $astat == ''
		or $alastbilldate == '')
			throw new iInvalidArgumentException();

		if (!isset($astat))
			throw new iInvalidDataException();

		$saname= $this->escapeString($aname);
		$sad1= $this->escapeString($ad1);
		$sad2 = $this->escapeString($ad2);
		$sacity = $this->escapeString($acity);
		$sastate = $this->escapeString($astate);
		$sapostal = $this->escapeString($apostal);
		$sacontact= $this->escapeString($acontact);
		$saemail= $this->escapeString($aemail);
		$sastat= $this->escapeString($astat);
		$sabillcycle = $this->escapeString($abillcycle);
		$salastbilldate = $this->escapeString($alastbilldate);
		$loggedinUser = loggedUserID();

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

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

		$this->beginTransaction();
		try
		{
			$query = "UPDATE accounts
						 SET 
      						 name = '$saname',
      						 address1 = '$sad1',
      						 address2 ='$sad2',
      						 city = '$sacity',
      						 state = '$sastate',
      						 postalcode = '$sapostal',
      						 contact = '$sacontact',
      						 email = '$saemail',
      						 billcycle = '$sabillcycle',
      						 lastbilldate = '$salastbilldate',
      						 status = '$sastat',
      						 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 getBillbydate($accountid)
	{
		$weekendday = $_SESSION['company']['weekendday'];
		$DbObj = new dbObj();
		$whereclause = "accounts_id = '$accountid' ";
		$rowData = $DbObj->fetchRowbyWhereClause('accounts', $whereclause);
		if ($rowData == null)
		return null;

		//Set the lastbilldate if it is not set....
		if (($rowData['lastbilldate'] == null) or ($rowData['lastbilldate'] == 0))
		{$lastbilldate = ' ';}
		else
		{$lastbilldate = $rowData['lastbilldate'];}
			
		$billcycle =$rowData['billcycle'];
			
		If ($lastbilldate == ' ')
		{
			switch ($billcycle)
			{
				case 'Weekly':
					$wedate= getWeekEndDate('',$weekendday);
					$billbydate= tesAddDate($wedate, -7);
					break;
				case 'Bi-Weekly':
					$wedate= getWeekEndDate('',$weekendday);
					$billbydate= tesAddDate($wedate, -14);
					break;
				case 'Monthly':
					$billbydate= getMonthEnd('','Prev');
					$billbydate= getMonthEnd($billbydate, 'Prev');
					break;
				case 'Bi-Monthly':
					$billbydate= getBiMonthlyDate('','Prev');
					$billbydate= getBiMonthlyDate($billbydate,'Prev');
					break;
				default:
					$billbydate= tesAddDate('today' ,-1);
			}
		}
		else
		{
			switch ($billcycle)
			{
				case 'Weekly':
					$billbydate= tesAddDate($lastbilldate, 7);
					break;
				case 'Bi-Weekly':
					$billbydate= tesAddDate($lastbilldate, +14);
					break;
				case 'Monthly':
					$billbydate= getMonthEnd($lastbilldate, 'Next');
					break;
				case 'Bi-Monthly':
					$billbydate= getBiMonthlyDate($lastbilldate,'Prev');
					break;
				default:
					$billbydate= tesAddDate('today' ,-1);
			}
		}

		Return $billbydate;
	}

	public function getLastBilldate($accountid)
	{
			
		$DbObj = new dbObj();
		$whereclause = "accounts_id = '$accountid' ";
		$rowData = $DbObj->fetchRowbyWhereClause('accounts', $whereclause);
		if ($rowData == null)
		return null;

		//Set the lastbilldate if it is not set....
		if (($rowData['lastbilldate'] == null) or ($rowData['lastbilldate'] == 0))
			{$lastbilldate = ' ';}
		else
			{$lastbilldate = $rowData['lastbilldate'];}
		Return $lastbilldate;
			
	}
}


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