<?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 AccountSubGroupMgmt
{
    /**
     * @var public $subgroup_id This is the account SubGroupId feteched from the database(ams_accountmaster).
     */
    public $subgroup_id;
	
	/**
     * @var public $account_groupname This is the account group name.
     */
    public $subgroup_name;
	
	/**
     * 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($subgroup_id)
    {
        $qry = "select * from ams_accountsubgroup where company_id='".$_SESSION['cCompany_id']."' ";

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

        $out=dbQuery($qry);

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

            $row=dbArray($out);
            extract($row);
			
            $this->subgroup_id 	 = ($subgroup_id);
			$this->subgroup_name = ($subgroup_name);
						
			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($subgroup_id,$subgroup_name)
    {
        $qry = "select subgroup_name from ams_accountsubgroup where company_id='".$_SESSION['cCompany_id']."' ";

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

        $out=dbQuery($qry);

        if(dbRows($out)>0) {
            return true;
        }
		else
		{
			return false;
		}
    }
	
	/**
     * 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_accountsubgroup 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 * FROM ams_accountsubgroup 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 .= "subgroup_id='"	.$this->getMaxRecordNumber()."',";
		$Qry1 .= "subgroup_name='"	.Escape($this->subgroup_name)."',";
		$Qry1 .= "company_id='"		.Escape($_SESSION['cCompany_id'])."',";
		$Qry1 .= "blnBackup = '0',";	
		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
			$qry = "insert into ams_accountsubgroup set " . $Qry1 . " ";
			dbQuery($qry);
			
		}

		$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 .= "subgroup_name='"	.Escape($this->subgroup_name)."',";
		$Qry1 .= "blnBackup = '0',";				
        if ($Qry1 != "") {
        	$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
            $qry = "update ams_accountsubgroup set " . $Qry1 . " 
			where subgroup_id='".$this->subgroup_id."' 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_accountsubgroup 
             	where subgroup_id='".$this->subgroup_id."' 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(subgroup_id) as mxcode 
				FROM ams_accountsubgroup 
				WHERE company_id='".$_SESSION["cCompany_id"]."'";	
				
		$out = dbQuery($qry);
		$result = dbArray($out);
		$num =$result['mxcode'];
		if($num==0){
			dbQuery("TRUNCATE ams_accountsubgroup");
		}
		$num =$num + 1;
		return $num;
    }
}
?>