<?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 VoucherMasterMgmt 
{
    /**
     * @var public $company_id This is the company_id.
     */
    public $company_id;
	
	/**
       * @var public $company_id This is the voucher GroupId feteched from the database(ams_vouchermaster).
     */
    public $voucher_id;
	
	/**
     * @var public $voucher_no This is the voucher_no .
     */
    public $voucher_no;
	
	/**
     * @var public $receipt_no This is the voucher receipt_no.
     */
    public $receipt_no;
	
	/**
     * @var public $voucher_type This is the voucher_type.
     */
    public $voucher_type;
	
	/**
     * @var public $voucher_check This is the voucher_check.
     */
    public $voucher_check;
	
	/**
     * @var public $voucher_date This is the voucher_date.
     */
    public $voucher_date;
	
	/**
     * @var public $voucher_totalamount This is the voucher_totalamount.
     */
    public $voucher_totalamount;
	
	/**
     * @var public $voucher_creditdetail This is the voucher_creditdetail.
     */
    public $voucher_creditdetail;
	
	/**
     * @var public $voucher_debitdetail This is voucher_debitdetail.
     */
    public $voucher_debitdetail;
	
	/**
     * @var public $login_id This is the voucher login_id.
     */
    public $login_id;
	/**
     * @var public $createdon This is the voucher createdon  .
     */
    public $createdon;
     /**
     * @var public $deleteId List of Id's to be deleted.
     */
    public $deletevoucher_Id;

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

    /**
     * Constructor to inialize voucher_count to zero
     *
     */
    function VoucherMasterMgmt()
    {
        $this->bullet = "<font size='2'>&raquo;</font>";
        $this->bullet = htmlentities($this->bullet);
        $this->voucher_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($voucher_id)
    {
        $qry = "select * from ams_vouchermaster where 1=1 ";

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

        $out=dbQuery($qry);

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

            $row=dbArray($out);
            extract($row);
			
            $this->company_id=($company_id);
	    
			$this->account_type=($account_type);
			$this->voucher_no=($voucher_no);
			$this->receipt_no=($receipt_no);
			$this->voucher_type=($voucher_type);
			$this->voucher_check=($voucher_check);
			$this->voucher_date=($voucher_date);
			$this->voucher_totalamount=($voucher_totalamount);
			$this->voucher_creditdetail=($voucher_creditdetail);
			$this->voucher_debitdetail=($voucher_debitdetail);
			$this->login_id = ($login_id);
			$this->createdon = ($createdon);
			
			dbFreeResult($out);
        }
    }*/
	
	/**
     * 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->voucher_count=0;
		
        $qry = "SELECT Count(*) AS Count FROM ams_vouchermaster 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->voucher_count=$Count;            
			dbFreeResult($out);
        }
		return $this->voucher_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->voucher_count=0;
		
        $qry = "SELECT 
					vm.*,
					vd.*
				FROM 
					ams_vouchermaster vm,
				    ams_voucherdetail vd
				WHERE 
					vm.voucher_id = vd.voucher_id AND
					vm.company_id='".$_SESSION['cCompany_id']."' AND 
					vd.company_id='".$_SESSION['cCompany_id']."' AND
				    vm.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";
        }
		
		
		$out=dbQuery($qry);
		
        if (dbRows($out)>0) {
            $this->voucher_count=dbRows($out);
			return $out;							
        }
		
		return $out;		
    }
      
	function getcountRecords($limit=0,$start=0,$condition="",$order_field="",$order_type=""){
        $qry = "SELECT 
					DISTINCT(vm.voucher_id) 
		        FROM 
					ams_vouchermaster vm, 
					ams_voucherdetail vd 
				WHERE 
					vm.company_id = '".$_SESSION['cCompany_id']."' AND 
					vd.company_id = '".$_SESSION['cCompany_id']."' AND 
					vm.voucher_id = vd.voucher_id AND
					vm.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";
        }
		
		$out=dbQuery($qry);
		
		if (dbRows($out)>0) {
            return dbRows($out);							
        }
		return dbRows($out);
	}
	  
	/**
     * This is a function for adding new record for account.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function add()
    {   
		global $SQLDateFormat;
		
		$Qry1 = "";
		
		$Qry1 .= "company_id='"	         .Escape($_SESSION['cCompany_id'])."',";		
		$Qry1 .= "year_id='"	         .Escape($_SESSION['cYear'])."',";	
		$Qry1 .= "voucher_no='"			 .Escape($this->voucher_no)."',";			
		$Qry1 .= "receipt_no='"			 .Escape($this->receipt_no)."',";			
		$Qry1 .= "voucher_type='"	     .Escape($this->voucher_type)."',";			
		$Qry1 .= "voucher_check='"		 .Escape($this->voucher_check)."',";
		$Qry1 .= "voucher_date =		  STR_TO_DATE('".Escape($this->voucher_date)."','$SQLDateFormat'),";			
 		$Qry1 .= "voucher_totalamount='" .Escape($this->voucher_totalamount)."',";			
		$Qry1 .= "voucher_creditdetail='".Escape($this->voucher_creditdetail)."',";			
		$Qry1 .= "voucher_debitdetail='" .Escape($this->voucher_debitdetail)."',";			
		$Qry1 .= "login_id='"			 .Escape($_SESSION['cUserId'])."',";			
		$Qry1 .= "createdon='"			 .Escape(date('Y-m-d h:i:s'))."',";
		$Qry1 .= "blnBackup = '0',";		
		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
	        $qry = "insert into ams_vouchermaster set " . $Qry1 . " ";
			dbQuery($qry);
			$this->voucher_id=dbInsertedId();
		}

		$this->voucher_msg = "<strong>".$this->bullet." Voucher 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()
    {
		global $SQLDateFormat;

        $Qry1 = "";
        //$Qry1 .= "company_id='"	         .Escape($_SESSION['cCompany_id'])."',";		
		//$Qry1 .= "voucher_id='"			 .Escape($this->voucher_id)."',";
		$Qry1 .= "voucher_no='"			 .Escape($this->voucher_no)."',";			
		$Qry1 .= "receipt_no='"			 .Escape($this->receipt_no)."',";			
		$Qry1 .= "voucher_type='"	     .Escape($this->voucher_type)."',";			
		$Qry1 .= "voucher_check='"		 .Escape($this->voucher_check)."',";			
		$Qry1 .= "voucher_date=           STR_TO_DATE('".Escape($this->voucher_date)."','$SQLDateFormat'),";	
		$Qry1 .= "voucher_totalamount='" .Escape($this->voucher_totalamount)."',";			
		$Qry1 .= "voucher_creditdetail='".Escape($this->voucher_creditdetail)."',";			
		$Qry1 .= "voucher_debitdetail='" .Escape($this->voucher_debitdetail)."',";
		$Qry1 .= "blnBackup = '0',";		
		//$Qry1 .= "login_id='"					.Escape($_SESSION['cUserId'])."',";			
		//$Qry1 .= "createdon='"					.Escape(date('Y-m-d h:i:s'))."',";		
						
        if ($Qry1 != "") {
        	$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
             $qry = "update ams_vouchermaster set " . $Qry1 . " 
			where voucher_id = ".$this->voucher_id." and company_id='".$_SESSION["cCompany_id"]."'";
            dbQuery($qry);
      	}
		$this->voucher_msg= "<strong>".$this->bullet." Voucher 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_vouchermaster 
             	where voucher_id in (".$this-> voucher_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 getMaxVoucherNumber() 
    {
		$qry = "SELECT MAX(voucher_no) as mxcode
				FROM ams_vouchermaster
				WHERE company_id='".$_SESSION["cCompany_id"]."' AND
				 year_id='".$_SESSION["cYear"]."' ";	
				
		$out = dbQuery($qry);
		$result = dbArray($out);
		$num =$result['mxcode'];
		/*if($num==0){
			dbQuery("TRUNCATE ams_vouchermaster");
		}*/
		$num =$num + 1;
		return $num;
    }

    function getVoucherDistinctDates($limit=0,$start=0,$condition="",$order_field="",$order_type="")
	{
	  $qry = "SELECT GROUP_CONCAT( voucher_id ) AS voucher_ids, voucher_date 
	          FROM ams_vouchermaster 
			  WHERE company_id ='".$_SESSION['cCompany_id']."'";
	   
	   if($condition != ""){
	   		$qry .= $condition ;
	   }
	   
	   $qry .= "GROUP BY voucher_date" ;
	
	   $out=dbQuery($qry);

        if (dbRows($out)>0) {
            return $out;
		}
		return $out ;
	}

	
	/*function getVoucherMonthlyTarij($limit=0,$start=0,$condition="",$order_field="",$order_type=""){
		$qry = "SELECT 
					am.account_id, 
					am.account_name,
					ad.account_crdr,
					vd.voucher_id,
					SUM(vd.voucher_creditamount) AS voucher_creditamount, 
					SUM(vd.voucher_debitamount) AS voucher_debitamount
				FROM
					ams_accountmaster am,
					ams_voucherdetail vd,
					ams_accountmasterdetail ad,
					ams_vouchermaster vm 
				WHERE
					am.account_id = vd.account_id AND
					am.account_id = ad.account_id AND
					vm.voucher_id = vd.voucher_id AND
					am.account_groupid != '1' AND
					am.account_groupid != '2' AND					 
					am.company_id = '".$_SESSION['cCompany_id']."' AND 
					ad.company_id = '".$_SESSION['cCompany_id']."' AND
					vm.company_id = '".$_SESSION['cCompany_id']."' AND
					vd.company_id = '".$_SESSION['cCompany_id']."' AND
					ad.year_id = '".$_SESSION['cYear']."' ".$condition."
				GROUP BY am.account_id ORDER BY am.account_name ASC ";
		
		$out=dbQuery($qry);

        if (dbRows($out)>0) {
            return $out;
		}
		return 0 ;
	}*/
	
	function getNewAccountOpeningBalance($financial_date="",$startdate="",$account_id=""){
		
		$condition = "";		
		
		$qry = "SELECT account_opbalance,account_ids,account_crdr,
					SUM(voucher_creditamount) AS voucher_creditamount, 
					SUM(voucher_debitamount) AS voucher_debitamount,
					(SUM(voucher_creditamount) - SUM(voucher_debitamount)) AS TOTAL
				FROM ams_vouchermaster vm, ams_voucherdetail vd,(SELECT account_opbalance,am.account_id as account_ids,account_crdr
					FROM ams_accountmaster am,ams_accountmasterdetail ad
					WHERE am.account_id = '$account_id'
					AND am.company_id = '".$_SESSION['cCompany_id']."'
					AND ad.company_id = '".$_SESSION['cCompany_id']."'
					AND am.account_id = ad.account_id
					AND ad.year_id = '".$_SESSION['cYear']."'
					) AS temp
				WHERE vm.voucher_id = vd.voucher_id
					AND vm.company_id = '".$_SESSION['cCompany_id']."'
					AND vd.company_id = '".$_SESSION['cCompany_id']."'													
					AND voucher_date >= '$financial_date'
					AND voucher_date < '$startdate'
					AND vd.account_id = '$account_id' ";
		
		$out=dbQuery($qry);

        if (dbRows($out)>0) {
            return $out;
		}
		return 0;
	}
	
	function getNewAccountClosingBalance($startdate="",$enddate="",$account_id=""){
	
		$qry = "SELECT account_opbalance,account_ids,account_crdr,
					SUM(voucher_creditamount) AS voucher_creditamount, 
					SUM(voucher_debitamount) AS voucher_debitamount,
					(SUM(voucher_creditamount) - SUM(voucher_debitamount)) AS TOTAL
				FROM ams_vouchermaster vm, ams_voucherdetail vd,(SELECT account_opbalance,am.account_id as account_ids,account_crdr
					FROM ams_accountmaster am,ams_accountmasterdetail ad
					WHERE am.account_id = '$account_id'
					AND am.company_id = '".$_SESSION['cCompany_id']."'
					AND ad.company_id = '".$_SESSION['cCompany_id']."'
					AND am.account_id = ad.account_id
					AND ad.year_id = '".$_SESSION['cYear']."'
					) AS temp
				WHERE vm.voucher_id = vd.voucher_id
					AND vm.company_id = '".$_SESSION['cCompany_id']."'
					AND vd.company_id = '".$_SESSION['cCompany_id']."'													
					AND voucher_date >= '$startdate'
					AND voucher_date <= '$enddate'
					AND vd.account_id = '$account_id' ";
		
		
		$out=dbQuery($qry);

        if (dbRows($out)>0) {
            return $out;
		}
		return 0 ;
	}
	
	/**
     * 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 getMaxReceiptNumber(){
		$qry = "SELECT MAX(receipt_no) as mxcode 
				FROM ams_vouchermaster
				WHERE company_id='".$_SESSION["cCompany_id"]."' AND
				      year_id='".$_SESSION["cYear"]."'";	
				
		$out = dbQuery($qry);
		$result = dbArray($out);
		$num =$result['mxcode'];
		$num =$num + 1;
		return $num;
    }
	
	/**
     * This is a function for getting the opening balance upto given data
     * @param string  $voucher_date This is the condition for fetching records.	 
     * @return Total Opening Balance
     */
	/*function getAccountOpeningBalance($financial_date,$startdate,$account_id){
		$qryOpeningBalance = "SELECT 
								(SELECT account_opbalance
								FROM ams_accountmaster, ams_accountmasterdetail
								WHERE ams_accountmaster.account_id = '$account_id'
								AND ams_accountmaster.company_id = '".$_SESSION["cCompany_id"]."'
								AND ams_accountmasterdetail.company_id = '".$_SESSION["cCompany_id"]."'
								AND ams_accountmaster.account_id = ams_accountmasterdetail.account_id
								AND ams_accountmasterdetail.year_id = '".$_SESSION['cYear']."'
								) AS account_opbalance,
								sum( voucher_creditamount ) AS voucher_creditamount, 
								sum( voucher_debitamount ) AS voucher_debitamount
							FROM ams_vouchermaster vm, ams_voucherdetail vd,ams_accountmaster am
							WHERE vm.voucher_id = vd.voucher_id
								AND vm.company_id = '".$_SESSION["cCompany_id"]."'
								AND vd.company_id = '".$_SESSION["cCompany_id"]."'
								AND am.company_id = '".$_SESSION["cCompany_id"]."'								
								AND voucher_date < '$startdate'
								AND voucher_date >= '$financial_date'
								AND vd.account_id = am.account_id  
								AND am.account_id = '$account_id'";
								
		$ouOpeningBalance = dbQuery($qryOpeningBalance);
		
		$opbalance=0;
		        
		if (dbRows($ouOpeningBalance)>0) {
            
				$rowOpeningBalance = dbArray($ouOpeningBalance);
				
				$account_id;
				$account_opbalance 		= $rowOpeningBalance["account_opbalance"];
				$voucher_creditamount  	= $rowOpeningBalance["voucher_creditamount"];
				$voucher_debitamount  	= $rowOpeningBalance["voucher_debitamount"];			
								
				$opbalance  =  $account_opbalance + ($voucher_creditamount - $voucher_debitamount);					
				
				dbFreeResult($ouOpeningBalance);
		}
		return $opbalance;
	}	*/
	
	/**
     * This is a function for getting the opening balance upto given data
     * @param string  $voucher_date This is the condition for fetching records.	 
     * @return Total Opening Balance
     */
	function getOpeningBalance($voucher_date,$financial_date){
		$qryOpeningBalance = "SELECT 
								(SELECT account_opbalance
								FROM ams_accountmaster,ams_accountmasterdetail
								WHERE account_groupid = '1'
								AND ams_accountmaster.company_id = '".$_SESSION["cCompany_id"]."'
								AND ams_accountmasterdetail.company_id = '".$_SESSION["cCompany_id"]."'
								AND ams_accountmaster.account_id = ams_accountmasterdetail.account_id
								AND ams_accountmasterdetail.year_id = '".$_SESSION['cYear']."'
								) AS account_opbalance,
								sum( voucher_creditamount ) AS voucher_creditamount, 
								sum( voucher_debitamount ) AS voucher_debitamount
							FROM ams_vouchermaster vm, ams_voucherdetail vd,ams_accountmaster am
							WHERE vm.voucher_id = vd.voucher_id
								AND vm.company_id = '".$_SESSION["cCompany_id"]."'
								AND vd.company_id = '".$_SESSION["cCompany_id"]."'
								AND am.company_id = '".$_SESSION["cCompany_id"]."'								
								AND voucher_date >= '$financial_date'
								AND voucher_date < '$voucher_date'
								AND vd.account_id = am.account_id  
								AND am.account_groupid = '1'";
								
		$ouOpeningBalance = dbQuery($qryOpeningBalance);
		
		$opbalance=0;
		        
		if (dbRows($ouOpeningBalance)>0) {
            
				$rowOpeningBalance = dbArray($ouOpeningBalance);
		
				$account_opbalance 		= $rowOpeningBalance["account_opbalance"];
				$voucher_creditamount  	= $rowOpeningBalance["voucher_creditamount"];
				$voucher_debitamount  	= $rowOpeningBalance["voucher_debitamount"];			
				
				$opbalance  =  $account_opbalance - $voucher_creditamount + $voucher_debitamount;					
				
				dbFreeResult($ouOpeningBalance);
		}
		return $opbalance;
	}
	
	/**
     * This is a function for getting the opening balance upto given data
     * @param string  $voucher_date This is the condition for fetching records.	 
     * @return Total Opening Balance
     */
	function getClosingBalance($voucher_date,$financial_date)
	{
		$qryClosingBalance = "SELECT 
								(SELECT account_opbalance
								FROM ams_accountmaster am,
									 ams_accountmasterdetail ad
								WHERE account_groupid = '1'
								AND am.company_id = '".$_SESSION["cCompany_id"]."'
								AND ad.company_id = '".$_SESSION["cCompany_id"]."'
								AND ad.year_id = '".$_SESSION['cYear']."'
								AND am.account_id = ad.account_id) AS account_opbalance,
								sum(voucher_creditamount) AS voucher_creditamount, 
								sum(voucher_debitamount) AS voucher_debitamount
							FROM ams_vouchermaster vm, ams_voucherdetail vd,ams_accountmaster am
							WHERE vm.voucher_id = vd.voucher_id
								AND vm.company_id = '".$_SESSION["cCompany_id"]."'
								AND vd.company_id = '".$_SESSION["cCompany_id"]."'
								AND am.company_id = '".$_SESSION["cCompany_id"]."'		
								AND voucher_date >= '$financial_date'
								AND voucher_date <= '$voucher_date'
								AND vd.account_id = am.account_id  
								AND am.account_groupid = '1'";
								
		$ouClosingBalance = dbQuery($qryClosingBalance);
		
		$opbalance=0;
		        
		if (dbRows($ouClosingBalance)>0) {
            
				$rowClosingBalance = dbArray($ouClosingBalance);
		
				$account_opbalance 		= $rowClosingBalance["account_opbalance"];
				$voucher_creditamount  	= $rowClosingBalance["voucher_creditamount"];
				$voucher_debitamount  	= $rowClosingBalance["voucher_debitamount"];			
				
				$opbalance  =  $account_opbalance - $voucher_creditamount + $voucher_debitamount;					
				
				dbFreeResult($ouClosingBalance);
		}
		return $opbalance;
	}
	
	function getDayBookEntry($limit=0,$start=0,$condition="",$order_field="",$order_type="")
	{
	  $arrayDayBook = array();
	  
	  $qryDebit = "SELECT aam.account_name, 
	  					  voucher_debitamount
				   FROM 
				   		ams_voucherdetail avd, 
						ams_accountmaster aam
						
				   WHERE 
			 			aam.account_id = avd.account_id
						AND avd.company_id = '".$_SESSION["cCompany_id"]."'
						AND aam.company_id = '".$_SESSION["cCompany_id"]."'
						AND voucher_creditamount =0
						 ";
	   
	   if($condition != ""){
	   		$qryDebit .= $condition ;
	   }
	   	
	   $outDebit=dbQuery($qryDebit);

		$i=0;
        
		if (dbRows($outDebit)>0) {
            while($rowDebit = dbArray($outDebit)) {
					$arrayDayBook[$i]["DebitAccount"] = $rowDebit["account_name"];
					$arrayDayBook[$i]["DebitAmount"]  = $rowDebit["voucher_debitamount"];
				$i++;
			}
		}
		
		$qryCredit = "SELECT aam.account_name, 
	  					  voucher_creditamount
				   	  FROM 
				   		ams_voucherdetail avd, 
						ams_accountmaster aam
						
				  	  WHERE 
			 			aam.account_id = avd.account_id
						AND avd.company_id = '".$_SESSION["cCompany_id"]."'
						AND aam.company_id = '".$_SESSION["cCompany_id"]."'
						AND voucher_debitamount =0
						 " ;
	   
	   if($condition != ""){
	   		$qryCredit .= $condition ;
	   }
	   	
	   $outCredit=dbQuery($qryCredit);

	   $i=0;
       if (dbRows($outCredit)>0) {
            while($rowCredit = dbArray($outCredit)) {
		  		$arrayDayBook[$i]["CreditAccount"] = $rowCredit["account_name"];
				$arrayDayBook[$i]["CreditAmount"]  = $rowCredit["voucher_creditamount"];
				$i++;
			}
		}		
		
		return $arrayDayBook;
	}
	
	function getGeneralReport($limit=0,$start=0,$condition="",$order_field="",$order_type=""){
		$qry = " SELECT 
					vm.voucher_no,
					vm.receipt_no,
					vm.voucher_date,
					SUM(vd.voucher_creditamount) AS voucher_creditamount,
					SUM(vd.voucher_debitamount) AS voucher_debitamount
				FROM 
					ams_vouchermaster vm,
					ams_voucherdetail vd
				WHERE
					vm.voucher_id = vd.voucher_id AND
					vm.company_id = '".$_SESSION['cCompany_id']."' AND
					vd.company_id = '".$_SESSION['cCompany_id']."' ";
		if($condition != ""){
			$qry .= $condition ;
	   	}
		$qry .= " GROUP BY vm.voucher_no ";
		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);
		$i=0;
		$j=0;		
		
		$arraygetGeneral = array();
				
        if (dbRows($out)>0) {
        	while($rowDebit = dbArray($out)){
			
				if($rowDebit["voucher_creditamount"] >0) {										
					
					$arraygetGeneral[$i]["voucher_no"] 			 = $rowDebit["voucher_no"];
					$arraygetGeneral[$i]["voucher_date"]		 = $rowDebit["voucher_date"];
					$arraygetGeneral[0]["voucher_lastdate"]		 = $rowDebit["voucher_date"];
					$arraygetGeneral[$i]["voucher_creditamount"] = $rowDebit["voucher_creditamount"];
					$i++;
				
				}
				if($rowDebit["voucher_debitamount"] >0) {
				
					$arraygetGeneral[$j]["receipt_no"]           = $rowDebit["receipt_no"];
					$arraygetGeneral[$j]["receipt_date"]		 = $rowDebit["voucher_date"];
					$arraygetGeneral[0]["receipt_lastdate"]		 = $rowDebit["voucher_date"];
					$arraygetGeneral[$j]["voucher_debitamount"]  = $rowDebit["voucher_debitamount"];
					$j++;				
				}
				
			}
		}
		return $arraygetGeneral;
	}  
	
	/*function profitandloss()
	{
	  $profitandloss=array();
	  
	}*/
	
	
	
	
	
	
	/*function getBalanceSheet($limit=0,$start=0,$condition="",$order_field="",$order_type="")
	{
	  $arrayDayBook = array();
	  
	  $qryDebit = "SELECT group_concat(account_id),account_type FROM  ams_accountmaster  WHERE company_id = '".$_SESSION["cCompany_id"]."' group by account_type ";
	   
	   if($condition != ""){
	   		$qryDebit .= $condition ;
	   }
	   	
	   $outDebit=dbQuery($qryDebit);

		$i=0;
        
		if (dbRows($outDebit)>0) {
            while($rowDebit = dbArray($outDebit)) {
			    $arrayDayBook[$i]["DebitAccount"] = $rowDebit["account_name"];
				$arrayDayBook[$i]["DebitAmount"]  = $rowDebit["voucher_debitamount"];
				$i++;
			}
		}
		
		$qryCredit = "SELECT aam.account_name, 
	  					  voucher_creditamount
				   	  FROM 
				   		ams_voucherdetail avd, 
						ams_accountmaster aam
						
				  	  WHERE 
			 			aam.account_id = avd.account_id
						AND avd.company_id = '".$_SESSION["cCompany_id"]."'
						AND aam.company_id = '".$_SESSION["cCompany_id"]."'
						AND voucher_debitamount =0
						" ;
	   
	   if($condition != ""){
	   		$qryCredit .= $condition ;
	   }
	   	
	   $outCredit=dbQuery($qryCredit);

	   $i=0;
       if (dbRows($outCredit)>0) {
            while($rowCredit = dbArray($outCredit)) {
				$arrayDayBook[$i]["CreditAccount"] = $rowCredit["account_name"];
				$arrayDayBook[$i]["CreditAmount"]  = $rowCredit["voucher_creditamount"];
				$i++;
			}
		}		
		
		return $arrayDayBook;
	}*/

	function profit_loss_balance_sheet_adjustment($condition = "",$effect = ""){
		$qry = "SELECT 
				am.account_id, 
				am.account_name,
				ad.account_crdr,
				vd.voucher_id,
				ad.account_opbalance,
				sub.subgroup_name,
				(SUM(vd.voucher_creditamount) - SUM(vd.voucher_debitamount)) AS TOTAL
			FROM(
					((ams_accountmaster am LEFT OUTER JOIN ams_voucherdetail vd ON am.account_id = vd.account_id)
					INNER JOIN ams_accountmasterdetail ad ON am.account_id = ad.account_id
					INNER JOIN ams_accountsubgroup sub ON sub.subgroup_id = am.subgroup_id)
			)
			WHERE(
				(vd.voucher_id in (SELECT voucher_id 
								  FROM ams_vouchermaster vm 
								  WHERE vm.company_id = '".$_SESSION['cCompany_id']."' ".$condition.")
			) OR (vd.voucher_id IS NULL)) AND
			
			am.account_effect = '".$effect."' AND
			am.company_id = '".$_SESSION['cCompany_id']."' AND 
			sub.company_id = '".$_SESSION['cCompany_id']."' AND 
			ad.company_id = '".$_SESSION['cCompany_id']."' AND
			ad.year_id = '".$_SESSION['cYear']."'
			
			GROUP BY am.account_id ORDER BY am.subgroup_id ASC ";
	
		$result = dbQuery($qry);
		
		return $result;
	}	 
}
?>