<?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 AccountMasterMgmt
{
    /**
     * @var public $account_id This is the account GroupId feteched from the database(ams_accountmaster).
     */
    public $account_id;
	
	/**
     * @var public $account_groupid This is the account group id.
     */
    public $account_groupid;
	
	/**
     * @var public $account_type This is the account type.
     */
    public $account_type;
	
	/**
     * @var public $account_name This is the account name.
     */
    public $account_name;
	
	/**
     * @var public $account_opbalance This is the account opening balance.
     */
    public $account_opbalance;
	
	/**
     * @var public $account_crdr This is the account credit/debit.
     */
    public $account_crdr;
	
	/**
     * @var public $account_baldate This is the account balance date.
     */
    public $account_baldate;
	
	/**
     * @var public $account_address This is the account address.
     */
    public $account_address;
	
	/**
     * @var public $account_phone This is the account phone.
     */
    public $account_phone;
	
	/**
     * @var public $account_effect This is the account effect.
     */
    public $account_effect;
	
	/**
     * @var public $account_subgroupid This is the account sub-group id.
     */
    public $subgroup_id;

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

    /**
     * Constructor to inialize account_count to zero
     *
     */
    function AccountMasterMgmt()
    {
        $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_id)
    {
        $qry = "select * from ams_accountmaster where 1=1 ";

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

        $out=dbQuery($qry);

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

            $row=dbArray($out);
            extract($row);
			
            $this->account_id=($account_id);
	    	$this->account_groupid=($account_groupid);
			$this->account_type=($account_type);
			$this->account_name=($account_name);
			$this->account_opbalance=($account_opbalance);
			$this->account_crdr=($account_crdr);
			$this->account_baldate=($account_baldate);
			$this->account_address=($account_address);
			$this->account_phone=($account_phone);
			$this->account_effect=($account_effect);
			$this->account_subgroupid=($account_subgroupid);
			
			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_id,$account_name)
    {
        $qry = "select 
					account_name 
				from 
					ams_accountmaster 
				where 
					company_id='".$_SESSION['cCompany_id']."' ";

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

        $out=dbQuery($qry);

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

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

        $out=dbQuery($qry);

        if(dbRows($out)>0) {
            $row=dbArray($out);
            extract($row);		
            return  $account_id;            
        }
    }
	
	/**
     * 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_accountmaster 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 
					mstr.*,
					mstrdetail.account_opbalance,
					mstrdetail.account_crdr,
					grp.account_groupname,
					subgroup.subgroup_name
				FROM 
					ams_accountmaster mstr,
					ams_accountmasterdetail mstrdetail,
					ams_accountgroup grp,
					ams_accountsubgroup subgroup
				WHERE 
					mstr.account_id = mstrdetail.account_id AND
					mstr.account_groupid = grp.account_groupid AND
					mstr.subgroup_id = subgroup.subgroup_id AND
				
					mstr.company_id='".$_SESSION['cCompany_id']."' AND 
					mstrdetail.company_id='".$_SESSION['cCompany_id']."' AND 
					grp.company_id='".$_SESSION['cCompany_id']."' AND 
					subgroup.company_id='".$_SESSION['cCompany_id']."' AND 
					mstrdetail.year_id = '".$_SESSION['cYear']."'";
					
					
		/*  $qry = "SELECT 
					mstr.*,
					mstrdetail.account_opbalance,
					mstrdetail.account_crdr,
					grp.account_groupname,
					subgroup.subgroup_name
				FROM 
					ams_accountmaster mstr,
					ams_accountmasterdetail mstrdetail,
					ams_accountgroup grp,
					ams_accountsubgroup subgroup
				WHERE 
					mstr.account_id = mstrdetail.account_id AND
					mstr.account_groupid = grp.account_groupid AND
					mstr.subgroup_id = subgroup.subgroup_id AND
				
					mstr.company_id='".$_SESSION['cCompany_id']."' AND 
					mstrdetail.company_id='".$_SESSION['cCompany_id']."' AND 
					grp.company_id='".$_SESSION['cCompany_id']."' AND 
					subgroup.company_id='".$_SESSION['cCompany_id']."' AND 
					mstrdetail.year_id = '".$_SESSION['cYear']."'";*/
		
		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->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 = "";
		$this->account_id = $this->getMaxRecordNumber();
		$Qry1 .= "account_id='"	        .Escape($this->account_id)."',";
		$Qry1 .= "account_groupid='"	.Escape($this->account_groupid)."',";
		$Qry1 .= "account_type='"		.Escape($this->account_type)."',";			
		$Qry1 .= "account_name='"		.Escape($this->account_name)."',";			
		$Qry1 .= "account_baldate='"	.Escape($this->account_baldate)."',";
		$Qry1 .= "account_address='"	.replaceHtmlEditor(Escape($this->account_address))."',";		
		$Qry1 .= "account_phone='"		.Escape($this->account_phone)."',";			
		$Qry1 .= "account_effect='"		.Escape($this->account_effect)."',";			
		$Qry1 .= "subgroup_id='"		.Escape($this->subgroup_id)."',";			
		$Qry1 .= "company_id='"	        .Escape($_SESSION['cCompany_id'])."',";			
		$Qry1 .= "blnBackup = '0',";
		
		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
			$qry = "insert into ams_accountmaster set " . $Qry1 . " ";
			dbQuery($qry);
			//$this->account_id=dbInsertedId();
			$this->addedit_accountmasterdetail("add");
		}

		$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 addedit_accountmasterdetail($action){
		$sql = "SELECT accountdetail_id 
				FROM ams_accountmasterdetail 
				WHERE account_id = '".$this->account_id."' AND 
				      year_id = '".$_SESSION['cYear']."' AND 
					  company_id = '".$_SESSION['cCompany_id']."' ";
		$result = dbQuery($sql);
		
		$Qry1 = "";
		$Qry1 .= "account_id='"	        .Escape($this->account_id)."',";
		$Qry1 .= "account_opbalance='"	.Escape($this->account_opbalance)."',";			
		$Qry1 .= "account_crdr='"		.Escape($this->account_crdr)."',";			
		$Qry1 .= "company_id='"	        .Escape($_SESSION['cCompany_id'])."',";			
		$Qry1 .= "year_id='"	        .Escape($_SESSION['cYear'])."',";			
		$Qry1 .= "blnBackup = '0',";
		
		if($Qry1 != "")	{	
			if(dbRows($result) == 0){
				$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
				$qry = "insert into ams_accountmasterdetail set " . $Qry1 . " ";
				dbQuery($qry);
			}
			elseif(dbRows($result) > 0 && $action=="edit"){
				$record = dbArray($result);
				$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
				$qry = "UPDATE ams_accountmasterdetail SET " . $Qry1 . " 
						WHERE accountdetail_id = ".$record['accountdetail_id']." AND 
							  year_id = '".$_SESSION['cYear']."' AND 
							  company_id = '".$_SESSION['cCompany_id']."' ";
				dbQuery($qry);
			}
			elseif(dbRows($result) > 0 && $action=="delete"){
				$record = dbArray($result);
				$qry = "DELETE FROM  ams_accountmasterdetail 
						WHERE account_id IN (".$this->account_id.") AND 
							  company_id='".$_SESSION["cCompany_id"]."'";
				dbQuery($qry);
			}
		}
	}
	
	/**
     * 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_groupid='"	.Escape($this->account_groupid)."',";
		$Qry1 .= "account_type='"		.Escape($this->account_type)."',";			
		$Qry1 .= "account_name='"		.Escape($this->account_name)."',";			
		$Qry1 .= "account_baldate='"	.Escape($this->account_baldate)."',";
		$Qry1 .= "account_address='"	.replaceHtmlEditor(Escape($this->account_address))."',";			
		$Qry1 .= "account_phone='"		.Escape($this->account_phone)."',";			
		$Qry1 .= "account_effect='"		.Escape($this->account_effect)."',";			
		$Qry1 .= "subgroup_id='"		.Escape($this->subgroup_id)."',";	
		$Qry1 .= "blnBackup = '0',";		
						
        if ($Qry1 != "") {
        	$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
            $qry = "update ams_accountmaster set " . $Qry1 . " 
			where account_id = ".$this->account_id." and company_id='".$_SESSION["cCompany_id"]."'";
            dbQuery($qry);
			$this->addedit_accountmasterdetail("edit");
      	}
		$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_accountmaster 
             	where account_id in (".$this->account_id.") and company_id='".$_SESSION["cCompany_id"]."'";
		dbQuery($qry);
		$this->addedit_accountmasterdetail("delete");
	}
	
	/**
     * 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_id) as mxcode 
				FROM ams_accountmaster";	
				
		$out = dbQuery($qry);
		$result = dbArray($out);
		$num =$result['mxcode'];
		/*if($num==0){
			dbQuery("TRUNCATE ams_accountmaster");
		}*/
		$num =$num + 1;
		return $num;
    }
	
	function addedit_profit_loss($amt,$type){
				
		$qry = "SELECT account_id 
				FROM ams_accountmaster 
				WHERE account_name = 'Profit-Loss A/c' AND company_id = '".$_SESSION['cCompany_id']."'";								
		$result = dbQuery($qry);
		
		if(dbRows($result)>0){ // edit Profit-Loss A/C
			$record = dbArray($result);
			$this->account_id = $record['account_id'];
			$this->account_opbalance = $amt;
			$this->account_crdr = $type;
			$this->addedit_accountmasterdetail("edit");
			
		} else{ // add Profit-Loss A/C
			
			$account_groupid= "";
			$subgroup_id ="";
			
			$qry = "SELECT account_groupid 
					FROM ams_accountgroup 
					WHERE account_groupname = 'Profit & Loss A/C' AND company_id = '".$_SESSION['cCompany_id']."'";
			$result_group = dbQuery($qry);
			if(dbRows($result_group)>0){
				$record_group = dbArray($result_group);
				$account_groupid = $record_group['account_groupid'];
			}
						
			$qry = "SELECT subgroup_id 
					FROM ams_accountsubgroup 
					WHERE subgroup_name = 'Cash Tr.' AND company_id = '".$_SESSION['cCompany_id']."'";
			$result_subgroup = dbQuery($qry);
			if(dbRows($result_subgroup)>0){
				$record_subgroup = dbArray($result_subgroup);
				$subgroup_id = $record_subgroup['subgroup_id'];
			}
					
			if($account_groupid!="" && $subgroup_id!=""){	
				$this->account_id 		 = $obj->getMaxRecordNumber();
				$this->account_groupid 	 = $account_groupid;
				$this->subgroup_id 		 = $subgroup_id;
				$this->account_opbalance = $amt;
				$this->account_crdr 	 = $type;
				$this->account_type 	 = "Other Account";
				$this->account_name 	 = "Profit-Loss A/c";
				$this->account_baldate 	 = date('Y-m-d');
				$this->account_effect 	 = "Balance Sheet";
				$this->year_id 			 = $_SESSION['cYear'];
				$this->company_id 		 = $_SESSION['cCompany_id'];
				$this->add();
			}
		}
	}
}
?>