<?php
require_once('includes/definition.php');

class bank_accounts_model{
    private $db;
    private $query;
    private $validatename;
    private $validatename1;
    private $validatename2;

    public function __construct(){
        $this->db=Conectar::conexion();
        $this->query=array();
        $this->validatename=array();
        $this->validatename1=array();
        $this->validatename2=array();
    }

/**
* Consulta de cuentass.
*
* Consulta de cuentass registrados en caso de recibir parametro not null la consulta se hace por id*
*
* @return Array de cuentass registrados
* @param Int $datos id del usuario
*/
public function query_bank_accounts($datos){
    if (is_null($datos)) {   
        $consulta=$this->db->query("SELECT b.id_account as id,b.holder_account as titular,b.number_account as num_cuenta,ban.name_bank as banco,s.status_name as status,s.status_color as color FROM bank_accounts as b INNER JOIN status as s ON (b.id_status=s.id_status) INNER JOIN banks as ban ON (b.id_bank=ban.id_bank) ");
    }else{
        $consulta=$this->db->query("SELECT b.id_account as id,b.holder_account as titular,b.number_account as num_cuenta,b.id_bank as banco,b.id_status as status,ban.name_bank as banco_n FROM bank_accounts as b INNER JOIN banks as ban ON (b.id_bank=ban.id_bank) WHERE b.id_account='$datos'");
    }
    while($filas=$consulta->fetch_assoc()){
        $this->query[]=$filas;
    }
    return $this->query;
}


/**
* Chequear si nombre de cuentas existe.
*
*Funcion para consultar nombre de cuentas registrado y asi evitar duplicidad ya que es un campo unico Si recibe parametro id not null es para indicar que ignore ese id en la consulta*
*
* @return Boolean false si existe true lo contrario 
*/
public function check_number ($datos,$id){
    $datos=trim($datos);
    if (is_null($id)) {
        $consulta=$this->db->query("SELECT 1 FROM bank_accounts WHERE number_account='$datos'");
    }else{
        $consulta=$this->db->query("SELECT 1 FROM bank_accounts WHERE number_account='$datos' AND id_account !='$id' ");
    }
    $consulta2=$consulta->num_rows;
    if ($consulta2 > 0) {
        return false;
    }
    return true;
}
/**
* Consulta de bancos.
*
* Consulta de bancos registrados en caso de recibir parametro not null la consulta se hace por id*
*
* @return Array de bancos registrados
* @param Int $datos id del usuario
*/
public function query_banks($datos){
    if (is_null($datos)) {   
        $consulta=$this->db->query("SELECT b.id_bank as id,b.name_bank as name FROM banks as b WHERE b.id_status=1 ");
    }else{
        $consulta=$this->db->query("SELECT 1 FROM banks as b WHERE b.id_bank='$datos' AND b.id_status=1  ");
    }
    $query=[];
    while($filas=$consulta->fetch_assoc()){
        $query[]=$filas;
    }
    return $query;
}
/**
* Registrar cuentas.
*
*Funcion para registrar cuentas*
*
* @return Boolean  
*/
public function register_bank_account($bancos,$titular,$num_cuenta,$status,$user){
    $date=date('Y-m-d H:i:s');
    $insert=$this->db->query("INSERT INTO bank_accounts(id_bank,holder_account,number_account,id_status,id_user,create_date) VALUES ('$bancos','$titular','$num_cuenta','$status','$user','$date')");

    return $insert;
}
/**
* Editar cuentas.
*
*Funcion para editar cuentas*
*
* @return Boolean  
*/
public function edit_bank_account($bancos,$titular,$num_cuenta,$status,$user,$id){
    $date=date('Y-m-d H:i:s');
    $update=$this->db->query("UPDATE bank_accounts SET id_bank='$bancos',holder_account='$titular',number_account='$num_cuenta',id_status='$status',id_user='$user',create_date='$date' WHERE id_account='$id'");
    return $update;
}
/**
* Chequear si bank_account.
*
*Funcion para validar si bank_account existe*
*
* @return Boolean  
* @param Int $id id de bank_account
*/
public function check_bank_account($id){
    $consulta=$this->db->query("SELECT 1 FROM bank_accounts WHERE id_account='$id'");
    $consulta1=$consulta->num_rows;
    if ($consulta1 == 0) {
        return false;
    }
    return true;
}
/**
* Chequear si bank_account.
*
*Funcion para validar si bank_account existe*
*
* @return Boolean  
* @param Int $id id de bank_account
*/
public function check_balance_day($id){
    $consulta=$this->db->query("SELECT 1 FROM accounts_balance WHERE id_account='$id' AND id_status='1' ");
    $consulta1=$consulta->num_rows;
    if ($consulta1 > 0) {
        return false;
    }
    return true;
}
/**
* Eliminar cuentas.
*
*Funcion para eliminar cuentas*
*
* @return Boolean  
* @param Int $id id de cuentas
*/
public function delete_bank_account($id){
    $delete=$this->db->query("DELETE FROM bank_accounts WHERE id_account='$id'");
    return $delete;
}


/**
* Consulta de saldo en cuentass.
*
* Consulta de saldo en cuentass registrados en caso de recibir parametro not null la consulta se hace por id*
*
* @return Array de saldo en cuentass registrados
* @param Int $datos id del usuario
*/
public function query_balance($datos){

    $consulta=$this->db->query("SELECT b.id_account as id,b.holder_account as titular,b.number_account as num_cuenta,s.balance_account as saldo,ban.name_bank as banco FROM accounts_balance as s INNER JOIN bank_accounts as b ON (s.id_account=b.id_account) INNER JOIN banks as ban ON (b.id_bank=ban.id_bank) WHERE s.id_account='$datos' AND s.id_status='1' ");
    $query=[];
    while($filas=$consulta->fetch_assoc()){
        $query[]=$filas;
    }
    return $query;
}

/**
* Editar cuentas.
*
*Funcion para editar cuentas*
*
* @return Boolean  
*/
public function edit_balance($id,$saldo,$user){
    $create_date=date('Y-m-d H:i:s');
    $date=date('Y-m-d');

    $insert=$this->db->query("INSERT INTO accounts_balance(id_account,balance_account,date_balance,id_status,id_user,create_date) VALUES ('$id','$saldo','$date','1','$user','$create_date')");
    if ($insert) {
    $id=$this->db->insert_id;
    $insert=$this->db->query("INSERT INTO initial_balance(id_balance,balance_account) VALUES ('$id','$saldo')");
    }
    return $insert;
}

/**
* Cerrar cuentas.
*
*Funcion para Cerrar cuentas*
*
* @return Boolean  
*/
public function close_balance($id,$user){
    $create_date=date('Y-m-d H:i:s');
    $date=date('Y-m-d');

    $update=$this->db->query("UPDATE accounts_balance SET id_status='2',close_date='$create_date' WHERE id_account='$id' AND id_status='1' ");
    return $update;
}

}
?>