<?php

class PettyCashIn {

	/*************************************************
	/ This function add new ExpenseTax Setup
	*************************************************/
	
	public static function AddPettyCashIn($data) {
		global $db;
				
		$id = $db->query_insert(TABLE_PETTYCASHIN, $data);
		if($db->affected_rows > 0){
			//$db->query_insert("vrs_logs", $data);
			return $id;
		}else{
			return false;
		}
	
	} // AddExpenseTax

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


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


	public static function CheckPettyCashIn($name, $id) {
		global $db;
		$query = "SELECT * from ".TABLE_PETTYCASHIN." WHERE name ='$name' && id <> '$id'";
		//print($query);exit;
		$rows = $db->query_first($query);
                if($db->affected_rows > 0){
                        return $rows;
                }else{
                        return false;
                }
	}//PettyCashInSetup
	
	/**********************************************************
	/ This function will return PettyCashInSetup
	**********************************************************/

	public static function GetPettyCashInList($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT * from ".TABLE_PETTYCASHIN." ". $filter;
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);

		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//PettyCashInSetup


	/**********************************************************
	/ This function will delete the Expense Tax
	 **********************************************************/

	public static function DeletePettyCashIn($id) {
		global $db;
		$query = "Delete from " . TABLE_PETTYCASHIN . "  WHERE id='$id'";
		$rows = $db->query_first($query);
		if($db->affected_rows > 0){
			return true;
		}else{
			return false;
		}
	}//DeleteExpenseTax
  
 public static function GetPettyCashLedgerData($filter = "") {
    global $db;

    $cleanFilter = "";
    if (!empty($filter)) {
        $cleanFilter = trim($filter);
        $cleanFilter = preg_replace("/^WHERE\s+/i", "", $cleanFilter);
    }

    $whereClause = !empty($cleanFilter) ? "WHERE $cleanFilter" : "";

    $queryIn = "
        SELECT 
            pci.id,
            pci.date,
            pci.amount,
            pci.description,
            'inflow' AS type,
            pci.chq_no,
            b.bank_name AS bank_name,
            NULL AS supplier_name,
            NULL AS category_name,
            pci.create_date
        FROM " . TABLE_PETTYCASHIN . " pci
        LEFT JOIN " . TABLE_BANKSETUP . " b ON b.id = pci.bank_id
        $whereClause
    ";

    $queryOut = "
        SELECT 
            et.id,
            et.date,
            et.amount,
            et.description,
            'outflow' AS type,
            NULL AS chq_no,
            NULL AS bank_name,
            CASE 
                WHEN et.supplier_id IS NOT NULL AND et.supplier_id != 0 THEN s.name
                ELSE et.paid_to
            END AS supplier_name,
            c.name AS category_name,
            et.create_date
        FROM " . TABLE_EXPENSE_TAX . " et
        LEFT JOIN " . TABLE_SUPPLIERSETUP . " s ON s.id = et.supplier_id
        LEFT JOIN " . TABLE_EXPENSETAXCATEGORYSETUP . " c ON c.id = et.category_id
        WHERE et.payment_type = 'cash'
    ";

    if (!empty($cleanFilter)) {
        $queryOut .= " AND $cleanFilter";
    }

    $combinedQuery = "
        ($queryIn)
        UNION ALL
        ($queryOut)
        ORDER BY date ASC, id ASC
    ";

    $rows = $db->fetch_all_array($combinedQuery);
    return ($db->affected_rows > 0) ? $rows : false;
}

  
 public static function GetPettyCashOpeningBalance($filter = "") {
    global $db;

    $cleanFilter = "";
    if (!empty($filter)) {
        $cleanFilter = trim($filter);
        $cleanFilter = preg_replace("/^WHERE\s+/i", "", $cleanFilter);
    }

    $whereClause = !empty($cleanFilter) ? "WHERE $cleanFilter" : "";

    $queryIn = "
        SELECT 
            pci.id,
            pci.date,
            pci.amount,
            'inflow' AS type,
            pci.description,
            pci.chq_no,
            b.bank_name AS bank_name,
            NULL AS supplier_name,
            NULL AS category_name
        FROM " . TABLE_PETTYCASHIN . " pci
        LEFT JOIN " . TABLE_BANKSETUP . " b ON b.id = pci.bank_id
        $whereClause
    ";

    $queryOut = "
        SELECT 
            et.id,
            et.date,
            et.amount,
            'outflow' AS type,
            et.description,
            NULL AS chq_no,
            NULL AS bank_name,
            CASE 
                WHEN et.supplier_id IS NOT NULL AND et.supplier_id != 0 THEN s.name
                ELSE et.paid_to
            END AS supplier_name,
            c.name AS category_name
        FROM " . TABLE_EXPENSE_TAX . " et
        LEFT JOIN " . TABLE_SUPPLIERSETUP . " s ON s.id = et.supplier_id
        LEFT JOIN " . TABLE_EXPENSETAXCATEGORYSETUP . " c ON c.id = et.category_id
        WHERE et.payment_type = 'cash'
    ";

    if (!empty($cleanFilter)) {
        $queryOut .= " AND $cleanFilter";
    }

    $combinedQuery = "
        ($queryIn)
        UNION ALL
        ($queryOut)
    ";

    $rows = $db->fetch_all_array($combinedQuery);
    return ($db->affected_rows > 0) ? $rows : false;
}






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

?>
