<?php
/**
 * This is a class file used for account operations.
 *
 * PHP version 5
 *
 * @category Admin_Tool
 * @package  Admin_Tool
 * @author   Aum Infosys <admin@auminfosys.com>
 * @license  Aum Infosys
 * @version  CVS:1.0.1
 * @link     http://auminfosys.com/
 * @filesource
 */

/**
 * This is a class named AccountMgmt.
 *
 * @category School Management System
 * @package  School Management System
 * @author   Aum Infosys <admin@auminfosys.com>
 * @license  Aum Infosys
 * @version  Release: 1.0.1
 * @link     http://auminfosys.com/
 *
 */

class AccountGroupMgmt
{
    /**
     * @var public $account_groupid This is the account GroupId feteched from the database(ams_accountmaster).
     */
    public $account_groupid;
	
	/**
     * @var public $account_groupname This is the account group name.
     */
    public $account_groupname;
	
	/**
     * @var public $account_type This is the account type.
     */
    public $account_type;

	/**
     * @var public $bullet This is the account bullet.
     */
    public $bullet;
	
	/**
     * @var public $account_msg This is the account message.
     */
    public $account_msg;

    /**
     * Constructor to inialize account_count to zero
     *
     */
    function AccountGroupMgmt()
    {
        $this->bullet = "<font size='2'>&raquo;</font>";
        $this->bullet = htmlentities($this->bullet);
        $this->account_count=0;
    }

    /**
     * This function is used to get a particular record.
     *
     * @param integer $account_groupid This is the database account_groupid.
     *
     * @return array
     */
    function getRecordById($account_groupid)
    {
        $qry = "select * from ams_accountgroup company_id='".$_SESSION['cCompany_id']."' ";

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

        $out=dbQuery($qry);

        if (dbRows($out)>0) {
            $this->account_count=1;

            $row=dbArray($out);
            extract($row);
			
            $this->account_groupid 	 = ($account_groupid);
			$this->account_groupname = ($account_groupname);
			$this->account_type 	 = ($account_type);
						
			dbFreeResult($out);
        }
    }
	
	/**
     * This function is used to get a particular record.
     *
     * @param integer $account_groupid This is the database account_groupid.
     *
     * @return array
     */
    function getDuplicateByName($account_groupid,$account_groupname)
    {
        $qry = "select account_groupname from ams_accountgroup where company_id='".$_SESSION['cCompany_id']."' ";

		if($account_groupid!=""){
       		$qry.=" and account_groupid <> '$account_groupid' ";
        }
	
        if ($account_groupname!="") {
            $qry.=" and account_groupname='$account_groupname' ";
        }

        $out=dbQuery($qry);

        if(dbRows($out)>0) {
            return true;
        }
		else
		{
			return false;
		}
    }
	
	
	
	 function getIdByName($account_groupname)
     {
        $qry = "select 
					account_groupid 
				from 
					ams_accountgroup 
				where 
					company_id='".$_SESSION['cCompany_id']."' ";

		
        if ($account_groupname!="") {
            $qry.=" and account_groupname='$account_groupname' ";
        }

        $out=dbQuery($qry);

        if(dbRows($out)>0) {
            $row=dbArray($out);
            extract($row);		
            return  $account_groupid;            
        }
		else
		{
			 $qry = "insert Into ams_accountgroup set
					   account_groupname='".$account_groupname."',
					   account_type='P',
					   company_id='".$_SESSION['cCompany_id']."',
					   blnBackup=0 ";
					   
			  dbQuery($qry);
			  return $this->account_groupid=dbInsertedId();

		}
    }
	/**
     * This is a function for getting the total no of records with/without condition
     *
     * @param integer $limit This is the limit of number or records.
     * @param integer $start This is the starting location of records.
     * @param string  $condition This is the condition for fetching records.	 
     * @param string  $order_field This is the field for fetching records orderby.	 
     * @param string  $order_type This is the order direction to fetch records orderby.	 	 
     *
     * @return Total Records Found
     */
    function getCountRecords($limit=0,$start=0,$condition="",$order_field="",$order_type="")
    {
		$this->account_count=0;
		
        $qry = "SELECT Count(*) AS Count FROM ams_accountgroup where company_id= '".$_SESSION["cCompany_id"]."' ";
		
		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";
        }
        $out=dbQuery($qry);

        if (dbRows($out)>0) {
			$row=dbArray($out);
            extract($row);		
            $this->account_count=$Count;            
			dbFreeResult($out);
        }
		return $this->account_count;
    }
	
	/**
     * This is a function for getting the all records with/without condition
     *
     * @param integer $limit This is the limit of number or records.
     * @param integer $start This is the starting location of records.
     * @param string  $condition This is the condition for fetching records.	 
     * @param string  $order_field This is the field for fetching records orderby.	 
     * @param string  $order_type This is the order direction to fetch records orderby.	 	 
     *
     * @return Mix Result Set
     */
    function getAllRecords($limit=0,$start=0,$condition="",$order_field="",$order_type="")
    {
		$this->account_count=0;
		
        $qry = "SELECT 
					account_groupid,
					account_groupname,
					account_type AS acc_type_code,
					case account_type when 'P' then 'Primary Group' when 'D' then 'Derived Group' end As account_type
				FROM 
					ams_accountgroup where company_id= '".$_SESSION["cCompany_id"]."' ";
		
		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";
        }

		$out=dbQuery($qry);
		
        if (dbRows($out)>0) {
            $this->account_count=dbRows($out);
			return $out;							
        }
		
		return $out;		
    }
		
		
	/**
     * This is a function for adding new record for account.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function add()
    {
		$Qry1 = "";

		$Qry1 .= "account_groupid='"	.$this->getMaxRecordNumber()."',";
		$Qry1 .= "account_groupname='"	.Escape($this->account_groupname)."',";
		$Qry1 .= "account_type='"		.Escape($this->account_type)."',";			
		$Qry1 .= "company_id='"		    .Escape($_SESSION['cCompany_id'])."',";			
		$Qry1 .= "blnBackup = '0',";
		
		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
			$qry = "insert into ams_accountgroup set " . $Qry1 . " ";
           // echo $qry;
			dbQuery($qry);
			$this->account_groupid=dbInsertedId();
		}

		$this->account_msg = "<strong>".$this->bullet." Account Inserted Successfully.</strong>";
	}
	
	/**
     * This is a function for editing a Account record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function edit()
    {
        $Qry1 = "";

		$Qry1 .= "account_groupname='"	.Escape($this->account_groupname)."',";
		$Qry1 .= "account_type='"		.Escape($this->account_type)."',";			
		$Qry1 .= "blnBackup = '0',";
						
        if ($Qry1 != "") {
        	$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
            $qry = "update ams_accountgroup set " . $Qry1 . " 
			where account_groupid in (".$this->account_groupid.") and company_id='".$_SESSION["cCompany_id"]."'";
            dbQuery($qry);
      	}
		$this->account_msg= "<strong>".$this->bullet." Account updated successfully.</strong>";
	}
	
	/**
     * This is a function for deleting the Account record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function delete()
    {
	    $qry = "delete from  ams_accountgroup 
             	where account_groupid=".$this->account_groupid." and company_id='".$_SESSION["cCompany_id"]."'";
		dbQuery($qry);
	}
	
	/**
     * This is a function for getting the max no of records with/without condition
     *
     * @param string  $condition This is the condition for fetching records.	 
     * @return Total Records Found
     */
    function getMaxRecordNumber()
    {
		$qry = "SELECT MAX(account_groupid) as mxcode 
				FROM ams_accountgroup";	
				
		$out = dbQuery($qry);
		$result = dbArray($out);
		$num =$result['mxcode'];
		if($num==0){
			dbQuery("TRUNCATE ams_accountgroup");
		}
		$num =$num + 1;
		return $num;
    }
}
?>