<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of datosbd
 *
 * @author Charly
 */
require_once __DIR__."../../conexion/conectar.php";

Class datosbd extends CONDB{
    var $mensajeerror = "";
    var $noerror = "";
    var $cMetodosPago = null;
    var $cFormasPago = null;
    var $cFormaPago = null;
    var $cProveedores = null;
    var $cFolioSiguiente = null;
    var $cMonedas = null;
    var $cDoce = null;
    var $cNombreProveedor = null;
    var $cPagoDocumentosRef = null;
    var $cTotalPagoDocumentosRef = null;
    var $cRutaPDF = null;
    var $cUUID = null;
    var $cCorreosProveedores = null;
    var $cNombreEmisor = null;
    
    function datosbd(){
        $this->conectar();
    }
    function getDoce($uuid){
	$q = "SELECT rfcemisor, tipodocto, serie, folio, rfcreceptor, nombrerec, fecha,iva, total,
	moneda, tipocambio, metodopago,formadepago,alta_manual,taxid,adicionala,archpdf,status FROM doce
	WHERE adicionala='$uuid'";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $this->cDoce = mysqli_fetch_assoc($r);
            return true;
	}else{
            $this->cDoce = null;
            return true;
	}
    }
    function getPagoDocumentosRef($uuid){
	$q = $queryDocs = "SELECT tipocambio,monto,numparcialidad,uuidcfdirelacionado,moneda FROM pago_documentos_ref
	WHERE doce_uuid='$uuid'";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $this->cPagoDocumentosRef = $r;
            return true;
	}else{
            $this->cPagoDocumentosRef = null;
            return true;
	}
    }
    function getTotalPagoDocumentosRef($uuid){
	$q = $queryDocs = "SELECT sum(monto) as sumatotal FROM pago_documentos_ref
	WHERE doce_uuid='$uuid'";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $tmp = mysqli_fetch_assoc($r);
            $this->cTotalPagoDocumentosRef = $tmp["sumatotal"];
            return true;
	}else{
            $this->cTotalPagoDocumentosRef = null;
            return true;
	}
    }
    function getProveedores(){
	$q = "SELECT rfcemisor,nombreemi FROM recibedoce r group by rfcemisor";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $this->cProveedores =$r;
            return true;
	}else{
            $this->cProveedores = null;
            return true;
	}
    }
    function getCorreosProveedor($rfc){
	$q = "SELECT group_concat(correo SEPARATOR ',') as correo FROM correos_proveedores where rfc='$rfc'";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $this->cCorreosProveedores = mysqli_fetch_assoc($r);
            return true;
	}else{
            $this->cCorreosProveedores = null;
            return true;
	}
    }
    function getNombreProveedor($rfc,$taxid){
        $cq = "";
        /*if($rfc == "XEXX010101000"){
            if(trim($taxid) != ""){
                $cq = " and taxid='$taxid'";
            }
        }*/
        
	$q = "SELECT nombreemi FROM recibedoce where rfcemisor='$rfc' $cq limit 1";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $this->cNombreProveedor = $r;
            return true;
	}else{
            $this->cNombreProveedor = null;
            return true;
	}
    }
    function getNombreEmisor($rfc){
	$q = "SELECT nombre FROM emisor where rfc='$rfc' limit 1";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $this->cNombreEmisor = mysqli_fetch_assoc($r);
            return true;
	}else{
            $this->cNombreEmisor = null;
            return true;
	}
    }
    function getRutaPDF($rfcemisor,$tipodocto){
	$q = "SELECT rutaPDF_P FROM emisor e inner join tdoctos t on e.idemisor=t.idemisor where e.rfc='$rfcemisor' and t.tipodocto='$tipodocto' limit 1";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $tmp = mysqli_fetch_assoc($r);
            $this->cRutaPDF = $tmp["rutaPDF_P"];
            return true;
	}else{
            $this->cRutaPDF = null;
            return true;
	}
    }
    function getUUID(){
	$q = "SELECT replace(uuid(),'-','') as uuid";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $tmp = mysqli_fetch_assoc($r);
            $this->cUUID = $tmp["uuid"];
            return true;
	}else{
            $this->cUUID = null;
            return true;
	}
    }
    
    function getMetodosPago(){
	$q = "SELECT c_metodopago as metodopago,descripcion FROM c_metodopago";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $this->cMetodosPago = $r;
            return true;
	}else{
            $this->cMetodosPago = null;
            return true;
	}
    }
    
    function getFormasPago(){
	$q = "SELECT c_formapago as formapago,descripcion FROM c_formapago";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $this->cFormasPago =$r;
            return true;
	}else{
            $this->cFormasPago = null;
            return true;
	}
    }
    
    function getFormaPago($formapago){
	$q = "SELECT c_formapago as formapago,descripcion FROM c_formapago where c_formapago='$formapago' limit 1";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $this->cFormaPago = mysqli_fetch_assoc($r);
            return true;
	}else{
            $this->cFormaPago = null;
            return true;
	}
    }
    
    function getMonedas(){
	$q = "SELECT c_moneda as moneda,descripcion FROM c_moneda where c_moneda in('MXN','USD','EUR')";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
        $this->mensajeerror = "";
        $this->noerror = 0;
	if(mysqli_num_rows($r) > 0){
            $this->cMonedas =$r;
            return true;
	}else{
            $this->cMonedas = null;
            return true;
	}
    }
    
    function getFolioSiguiente($tipoDocto){
	$q = "SELECT tipoDocto,serie,folio FROM config_folios_tipos_documento
	WHERE tipoDocto='$tipoDocto'";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
	$this->mensajeerror = "";
        $this->noerror = 0;	
	if(mysqli_num_rows($r) > 0){
            $this->cFolioSiguiente = mysqli_fetch_assoc($r);
            return true;
	}else{
            $this->cFolioSiguiente = null;
            return true;
	}
    }
    function incrementaFolio($tipoDocto){
	$q = "update config_folios_tipos_documento set folio=folio+1 WHERE tipoDocto='$tipoDocto'";
	if(!$r = mysqli_query($this->CONEXION,$q)){
            $this->mensajeerror = mysqli_error($this->CONEXION);
            $this->noerror = -1;
            return false;
	}
	if(mysqli_affected_rows($this->CONEXION) > 0){
            $this->mensajeerror = "";
            $this->noerror = 0;
            return true;
	}else{
            $this->mensajeerror = "No se afecto ningún registro.";
            $this->noerror = 201;
            return true;
	}
    }
    function Alert($mensaje){
        ?><script>
            if (typeof $j_6 != "undefined") {
                $j_6.messager.alert("Error.","<?=htmlentities($mensaje,ENT_QUOTES);?>","error");
            }else{
                $j_7.messager.alert("Error.","<?=htmlentities($mensaje,ENT_QUOTES);?>","error");
            }
            
        </script><?exit;
    }
    function InfoContinua($mensaje){
        ?><script>
            if (typeof $j_6 != "undefined") {
                $j_6.messager.alert("Aviso.","<?=htmlentities($mensaje,ENT_QUOTES);?>","info");
            }else{
                $j_7.messager.alert("Aviso.","<?=htmlentities($mensaje,ENT_QUOTES);?>","info");
            }
        </script><?
    }
    function Info($mensaje){
        ?><script>
            if (typeof $j_6 != "undefined") {
                $j_6.messager.alert("Aviso.","<?=htmlentities($mensaje,ENT_QUOTES);?>","info");
            }else{
                $j_7.messager.alert("Aviso.","<?=htmlentities($mensaje,ENT_QUOTES);?>","info");
            }
        </script><?exit;
    }
    function enviarNotificacion($uuid,$rfc){
        ?><script>enviaNotificacionDePago('<?=$uuid?>','<?=$rfc?>');</script><?
    }
    function Error($mensaje){
        ?><h1><?=$mensaje;?></h1><?exit;
    }
    function limpiaCursor($cursor){
        mysqli_free_result($cursor);
    }
    function setValueForm($idInput,$id){
        ?><script>$j_6("#<?=$idInput?>").val('<?=$id?>')</script><?
    }
    function setReadOnly($idInput){
        ?><script>$j_6("#<?=$idInput?>").attr('readonly','true')</script><?
    }
    function setDisabled($idInput){
        ?><script>$j_6("#<?=$idInput?>").attr('disabled','true')</script><?
    }
}
