<?php
class TransactionMgmt
{
	public $trans_id;
	public $user_id;
	public $user_message;
	public $trans_date;
	public $user_lastbal;
	public $user_usedbal;
	public $user_availbal;
	public $bullet;

	function TransactionMgmt()
	{
		$this->bullet = "<font size='2'>&raquo;</font>";
		$this->bullet = htmlentities($this->bullet);
		$this->trans_count = 0;
	}

	function getCountRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->trans_count = 0;

		$qry = "SELECT Count(*) AS Count FROM transactionmaster where 1=1 ";

		if ($condition != "") {
			$qry .= $condition;
		}

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$row = dbArray($out);
			extract($row);
			$this->trans_count = $Count;
			dbResult($out);
		}
		return $this->trans_count;
	}

	function getAllRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->trans_count = 0;

		$qry = "SELECT 
					trans_id,
					user_id,
					user_message,
					trans_date,
					user_lastbal,
					user_usedbal,
					user_availbal
				FROM 
					transactionmaster where user_id = " . $_SESSION['aUserId'];

		if ($condition != "") {
			$qry .= $condition;
		}


		if ($order_field != "" && $order_type != "") {
			$qry .= " order by $order_field $order_type";
		}

		if (intval($limit) > 0 && intval($start) >= 0) {
			$qry .= " Limit $start , $limit";
		}

		//print $qry;
		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$this->trans_count = dbRows($out);
			return $out;
		}

		return $out;

	}

	function add()
	{
		global $SQLDateFormat;

		$Qry1 = "";
		$Qry1 .= "user_id='" . Escape($this->user_id) . "',";
		$Qry1 .= "user_message='" . replaceHtmlEditor(Escape($this->user_message)) . "',";
		$Qry1 .= "user_lastbal='" . Escape($this->user_lastbal) . "',";
		$Qry1 .= "user_usedbal='" . Escape($this->user_usedbal) . "',";
		$Qry1 .= "user_availbal='" . Escape($this->user_availbal) . "',";
		$Qry1 .= "trans_date ='" . Escape(date('Y-m-d h:i:s')) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "insert into transactionmaster set " . $Qry1 . " ";
			dbQuery($qry);
			$this->trans_id = dbInsertedId();
		}

	}

	function edit()
	{
		global $SQLDateFormat;

		$Qry1 = "";
		$Qry1 .= "user_id='" . Escape($this->user_id) . "',";
		$Qry1 .= "user_message='" . replaceHtmlEditor(Escape($this->user_message)) . "',";
		$Qry1 .= "user_lastbal='" . Escape($this->user_lastbal) . "',";
		$Qry1 .= "user_usedbal='" . Escape($this->user_usedbal) . "',";
		$Qry1 .= "user_availbal='" . Escape($this->user_availbal) . "',";
		$Qry1 .= "trans_date ='" . Escape(date('Y-m-d h:i:s')) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "update transactionmaster set " . $Qry1 . " 
			where trans_id='" . $this->trans_id . "'";
			dbQuery($qry);
		}
	}

	function delete()
	{
		$qry = "delete from  transactionmaster 
             	where trans_id=" . $this->trans_id . " ";
		dbQuery($qry);
	}

	function updatecredit($trans_Id, $creditconuter)
	{
		if ($trans_Id != "") {
			$qry = "update transactionmaster set user_usedbal=$creditconuter,user_availbal= user_lastbal-$creditconuter
				where trans_id='$trans_Id'";
			dbQuery($qry);
		}
	}
	function getlastbalance($user_id)
	{
		if ($user_id != "") {
			$qry = "select user_availbal from transactionmaster
					where user_id=$user_id order by trans_id DESC,trans_date DESC limit 0,1";

			$out = dbQuery($qry);
			if (dbRows($out) > 0) {
				$row = dbArray($out);
				extract($row);
				return $user_availbal;
			} else {
				$qry = "SELECT sum(purchase_credit) As Total  From usercredit 
						   where user_id=" . $_SESSION['aUserId'];
				$out = dbQuery($qry);
				if (dbRows($out) > 0) {
					$row = dbArray($out);
					extract($row);
					return $Total;
				}
			}

		}

	}

	function getdaywiseBalance($date = "")
	{

		$this->trans_count = 0;
		$qry = "SELECT sum(user_usedbal) As Count FROM transactionmaster 
		        where Date(`trans_date`)='$date' And user_id=" . $_SESSION['aUserId'];

		if ($condition != "") {
			$qry .= $condition;
		}

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$row = dbArray($out);
			extract($row);
			$this->trans_count = $Count;
			dbResult($out);
		}
		return $this->trans_count;
	}
}
?>