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

class system_payments_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_system_payments($datos,$fecha){
    if (is_null($datos)) {   
        $consulta=$this->db->query("SELECT b.id_payment as id,b.total_deposits as total_dep,b.amount_deposits as monto_dep,b.total_transfers as total_tra,b.amount_transfers as monto_tra,b.date_payment as fecha_pago,c.holder_account as titular,c.number_account as num_cuenta,ban.name_bank as banco FROM system_payments as b INNER JOIN bank_accounts as c ON (b.id_account=c.id_account) INNER JOIN banks as ban ON (c.id_bank=ban.id_bank) WHERE b.date_payment='$fecha' ");
    }else{
        $consulta=$this->db->query("SELECT b.id_payment as id,b.total_deposits as total_dep,b.amount_deposits as monto_dep,b.total_transfers as total_tra,b.amount_transfers as monto_tra,b.id_account as cuenta FROM system_payments as b WHERE b.id_payment='$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_date_payment ($datos){
    $datos=trim($datos);
    $date=date('Y-m-d');
    $consulta=$this->db->query("SELECT 1 FROM system_payments WHERE date_payment='$date' AND id_account ='$datos' ");
    $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_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 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) WHERE b.id_status='1' ");
    }else{
        $consulta=$this->db->query("SELECT 1 FROM bank_accounts as b WHERE b.id_account='$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_system_payment($cuentas,$cant_dep,$monto_dep,$cant_trans,$monto_trans,$user){
    $date=date('Y-m-d');
    $date_time=date('Y-m-d H:i:s');
    $insert=$this->db->query("INSERT INTO system_payments(id_account,total_deposits,amount_deposits,total_transfers,amount_transfers,date_payment,id_user,create_date) VALUES ('$cuentas','$cant_dep','$monto_dep','$cant_trans','$monto_trans','$date','$user','$date_time')");

    return $insert;
}
/**
* Editar cuentas.
*
*Funcion para editar cuentas*
*
* @return Boolean  
*/
public function edit_system_payment($id,$cant_dep,$monto_dep,$cant_trans,$monto_trans,$user){
    $date=date('Y-m-d');
    $date_time=date('Y-m-d H:i:s');

    $update=$this->db->query("UPDATE system_payments SET total_deposits='$cant_dep',amount_deposits='$monto_dep',total_transfers='$cant_trans',amount_transfers='$monto_trans',id_user='$user',create_date='$date_time' WHERE id_payment='$id' AND date_payment='$date' ");
    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_system_payment($id){
    $date=date('Y-m-d');
    $consulta=$this->db->query("SELECT 1 FROM system_payments WHERE id_payment='$id' AND date_payment='$date' ");
    $consulta1=$consulta->num_rows;
    if ($consulta1 == 0) {
        return false;
    }
    return true;
}

/**
* Eliminar sistema pago.
*
*Funcion para eliminar sistema pago*
*
* @return Boolean  
* @param Int $id id de sistema pago
*/
public function delete_system_payment($id){
    $delete=$this->db->query("DELETE FROM system_payments WHERE id_payment='$id'");
    return $delete;
}


}
?>