<?php

class ExpenseTaxSetup {

	/*************************************************
	/ This function add new ExpenseTax Setup
	*************************************************/
	
	public static function AddExpenseTaxSetup($data) {
		global $db;
      
      // Convert empty supplier_id to null to avoid foreign key violation
    if (isset($data['supplier_id']) && $data['supplier_id'] === '') {
        $data['supplier_id'] = null;
    }
			
		$id = $db->query_insert(TABLE_EXPENSE_TAX, $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 UpdateExpenseTaxSetup($pid,$data) {
		global $db;
      
     
     //  Convert empty supplier_id to null to avoid foreign key violation
    if (isset($data['supplier_id']) && $data['supplier_id'] === '') {
       $data['supplier_id'] = null;
    }
		$id = $db->query_update(TABLE_EXPENSE_TAX,$data,"id='$pid'");
		if($db->affected_rows > 0){
			//$db->query_insert("vrs_logs", $data);
			return $id;
		}else{
			return $id;
		}
	
	} // UpdateExpenseTax


	public static function GetExpenseTaxSetupById($id) {
		global $db;
		$query = "SELECT * from ".TABLE_EXPENSE_TAX." WHERE id ='$id'";
		//print($query);exit;
      
     //  error_log("QRY query3333 " . print_r($query, true));
		$rows = $db->query_first($query);
                if($db->affected_rows > 0){
                        return $rows;
                }else{
                        return false;
                }
	}//ExpenseTaxSetup


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

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

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

		if($db->affected_rows > 0){
		
			return $rows;
		}else{

			return false;
		}
	}//ExpenseTaxSetupSetup


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

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

    $prefix = date('Ymd', strtotime($date));
    $query = "SELECT MAX(voucher_no) as max_no FROM " . TABLE_EXPENSE_TAX . " WHERE voucher_no LIKE '{$prefix}%'";
    $result = $db->query_first($query);

    if ($result && !empty($result->max_no)) {
        $lastSequence = intval(substr($result->max_no, 8));
        $newSequence = str_pad($lastSequence + 1, 3, '0', STR_PAD_LEFT);
    } else {
        $newSequence = "001";
    }

    return $prefix . $newSequence;
}




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

?>
