<?php

class BankSetup {

	/*************************************************
	/ This function add new Bank Setup
	*************************************************/
	
	public static function AddBankSetup($data) {
		global $db;
				
		$id = $db->query_insert(TABLE_BANKSETUP, $data);
		if($db->affected_rows > 0){
			//$db->query_insert("vrs_logs", $data);
			return $id;
		}else{
			return false;
		}
	
	} // AddBank
  
  public static function GetBankBalance($bankId) {
    global $db;

    $bankId = (int)$bankId;

    // 1) Incoming: customer receipts (payment table)
    //    Only count non-cancelled rows
    $inRow = $db->query_first("
        SELECT SUM(amount) AS total_in
        FROM ".TABLE_PAYMENT."
        WHERE bank_name = '{$bankId}'
          AND (status IS NULL OR status <> 'Cancel')
    ");
    $totalIn = ($inRow && isset($inRow->total_in) && $inRow->total_in !== null)
        ? (float)$inRow->total_in
        : 0.0;

    // 2) Outgoing: purchase payments (purpayment table)
    $outPurRow = $db->query_first("
        SELECT SUM(amount) AS total_out
        FROM ".TABLE_PURPAYMENT."
        WHERE bank_name = '{$bankId}'
          AND (status IS NULL OR status <> 'Cancel')
    ");
    $totalOutPur = ($outPurRow && isset($outPurRow->total_out) && $outPurRow->total_out !== null)
        ? (float)$outPurRow->total_out
        : 0.0;

    // 3) Outgoing: expenses (expense table)
    //    Only non-deleted expenses
    $outExpRow = $db->query_first("
        SELECT SUM(amount) AS total_out
        FROM ".TABLE_EXPENSE."
        WHERE bank_id = '{$bankId}'
          AND (deleted IS NULL OR deleted = 0)
    ");
    $totalOutExp = ($outExpRow && isset($outExpRow->total_out) && $outExpRow->total_out !== null)
        ? (float)$outExpRow->total_out
        : 0.0;

    // Final balance
    $balance = $totalIn - ($totalOutPur + $totalOutExp);

    return $balance; // can be negative if already overspent
}


	/*************************************************
	/ This function update Bank
	*************************************************/
	
	public static function UpdateBankSetup($pid,$data) {
		global $db;
				
		$id = $db->query_update(TABLE_BANKSETUP,$data,"id='$pid'");
		
		if($db->affected_rows > 0){
			//$db->query_insert("vrs_logs", $data);
			
			return $id;
		}else{
			return $id;
		}
	
	} // UpdateBank


	public static function GetBankSetupById($id) {
	
		global $db;
		$query = "SELECT * from ".TABLE_BANKSETUP." WHERE id ='$id'";
		//print($query);exit;
		$rows = $db->query_first($query);
	
                if($db->affected_rows > 0){
			
                        return $rows;
                }else{
			
                        return false;
                }
	}//Bank


	public static function CheckBankAccount($account_number, $id) {
	
		global $db;
		$query = "SELECT * from ".TABLE_BANKSETUP." WHERE bank_account_number ='$account_number' && id <> '$id'";
		//print($query);exit;
		$rows = $db->query_first($query);
	
                if($db->affected_rows > 0){
			
                        return $rows;
                }else{
			
                        return false;
                }
	}//Bank
	
	/**********************************************************
	/ This function will return BankSetup
	**********************************************************/
		
	public static function GetBankSetupList($filter="",$pageno="null",$pagesize="null") {
	
		global $db;
		$query = "SELECT * from ".TABLE_BANKSETUP." ". $filter;
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);
	
			if($db->affected_rows > 0){
			
                        return $rows;
                }else{
			
                        return false;
                }
	}//BankSetup

	public static function GetNumRows() { global $db; return $db->GetNumRows(); }
	public static function GetNumberOfPages() { global $db; return $db->GetNumberOfPages(); }
		
	
}

?>
