<?php 

require_once "conexion.php";

class ModeloComprobante {

    static public function MdlMostrarComprobante($tabla, $item, $valor){

        if($item != null){

            $stmt = Conexion:: conectar()->prepare("SELECT * FROM $tabla WHERE $item = :$item");

            $stmt -> bindParam(":".$item, $valor, PDO::PARAM_STR);
            
            $stmt -> execute();

            return $stmt -> fetch();

        }else {

            $stmt = Conexion:: conectar()->prepare("SELECT * FROM $tabla");
            
            $stmt -> execute();

            return $stmt -> fetchAll();
        }

        $stmt -> close();

        $stmt = null;
    }

    static public function MdlMostrarComprobante1($tabla, $item, $valor){

        if($item != null){

            $stmt = Conexion:: conectar()->prepare("SELECT * FROM $tabla WHERE $item = :$item ORDER BY id DESC");

            $stmt -> bindParam(":".$item, $valor, PDO::PARAM_STR);
            
            $stmt -> execute();

            return $stmt -> fetch();

        }else {

            $stmt = Conexion:: conectar()->prepare("SELECT * FROM $tabla ");
            
            $stmt -> execute();

            return $stmt -> fetchAll();
        }

        $stmt -> close();

        $stmt = null;
    }
     /** REGISTRO DE COMPROBANTE */

     static public function mdlIngresarComprobante($tabla, $datos){

        $stmt = Conexion:: conectar()->prepare("INSERT INTO $tabla(nombre, operacion, serie, numero) VALUES (:nombre, :operacion, :serie, :numero)");
        
        $stmt -> bindParam(":nombre", $datos["nombre"], PDO::PARAM_STR);
        $stmt -> bindParam(":operacion", $datos["operacion"], PDO::PARAM_STR);
        $stmt -> bindParam(":serie", $datos["serie"], PDO::PARAM_STR);
        $stmt -> bindParam(":numero", $datos["numero"], PDO::PARAM_STR);

        if($stmt -> execute()){
            return "ok";
        }else{
            return "error";
        }
        
        $stmt -> close();

        $stmt = null;

        
    }

    /*=============================================
	EDITAR COMPROBANTE
	=============================================*/
    static public function mdlEditarComprobante($tabla, $datos){

        $stmt = Conexion::conectar()->prepare("UPDATE $tabla SET nombre = :nombre, operacion = :operacion, serie = :serie, numero = :numero WHERE id = :id");

        $stmt -> bindParam(":id", $datos["id"], PDO::PARAM_STR);
		$stmt -> bindParam(":nombre", $datos["nombre"], PDO::PARAM_STR);
		$stmt -> bindParam(":operacion", $datos["operacion"], PDO::PARAM_STR);
        $stmt -> bindParam(":serie", $datos["serie"], PDO::PARAM_STR);
        $stmt -> bindParam(":numero", $datos["numero"], PDO::PARAM_STR);

		if($stmt -> execute()){

			return "ok";
            
		
		}else{

			return "error";	

		}

		$stmt -> close();

		$stmt = null;

    }

     /*=============================================
	BORRAR COMPROBANTE
	=============================================*/

	static public function mdlBorrarComprobante($tabla, $datos){

		$stmt = Conexion::conectar()->prepare("DELETE FROM $tabla WHERE id = :id");

		$stmt -> bindParam(":id", $datos, PDO::PARAM_INT);

		if($stmt -> execute()){

			return "ok";
		
		}else{

			return "error";	

		}

		$stmt -> close();

		$stmt = null;


	}
}