<?php
/**
 * BonVers Model
 * Represents payment slips from bons_vers table
 */

class BonVers {
    private $conn;
    private $table_name = "bons_vers";

    public $num_vers;
    public $date_vers;
    public $periode_p;
    public $operateur;
    public $type_vehicule;
    public $mt_ttc;
    public $obs;
    public $creer_par;
    public $date_creer;

    public function __construct($db) {
        $this->conn = $db;
    }

    public function getAll() {
        $query = "SELECT * FROM " . $this->table_name . " ORDER BY date_vers DESC";
        
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        
        return $stmt;
    }

    public function getById($num_vers) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE num_vers = ? LIMIT 0,1";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $num_vers);
        $stmt->execute();
        
        return $stmt;
    }

    public function getByMatriculeAndPeriode($matricule, $periode) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE operateur = ? AND periode_p = ? LIMIT 0,1";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $matricule);
        $stmt->bindParam(2, $periode);
        $stmt->execute();
        
        return $stmt;
    }

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " SET num_vers=:num_vers, date_vers=:date_vers, periode_p=:periode_p, operateur=:operateur, type_vehicule=:type_vehicule, mt_ttc=:mt_ttc, obs=:obs, creer_par=:creer_par, date_creer=:date_creer";
        
        $stmt = $this->conn->prepare($query);
        
        $this->num_vers = htmlspecialchars(strip_tags($this->num_vers));
        $this->date_vers = htmlspecialchars(strip_tags($this->date_vers));
        $this->periode_p = htmlspecialchars(strip_tags($this->periode_p));
        $this->operateur = htmlspecialchars(strip_tags($this->operateur));
        $this->type_vehicule = htmlspecialchars(strip_tags($this->type_vehicule));
        $this->mt_ttc = htmlspecialchars(strip_tags($this->mt_ttc));
        $this->obs = htmlspecialchars(strip_tags($this->obs));
        $this->creer_par = htmlspecialchars(strip_tags($this->creer_par));
        $this->date_creer = htmlspecialchars(strip_tags($this->date_creer));
        
        $stmt->bindParam(":num_vers", $this->num_vers);
        $stmt->bindParam(":date_vers", $this->date_vers);
        $stmt->bindParam(":periode_p", $this->periode_p);
        $stmt->bindParam(":operateur", $this->operateur);
        $stmt->bindParam(":type_vehicule", $this->type_vehicule);
        $stmt->bindParam(":mt_ttc", $this->mt_ttc);
        $stmt->bindParam(":obs", $this->obs);
        $stmt->bindParam(":creer_par", $this->creer_par);
        $stmt->bindParam(":date_creer", $this->date_creer);
        
        if ($stmt->execute()) {
            return true;
        }
        
        return false;
    }

    public function checkPeriodeExistence($matricule, $periode) {
        $query = "SELECT COUNT(*) as count FROM " . $this->table_name . " WHERE operateur = ? AND periode_p = ?";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $matricule);
        $stmt->bindParam(2, $periode);
        $stmt->execute();
        
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row['count'] > 0;
    }

    public function getByPeriode($periode) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE periode_p = ? ORDER BY num_vers DESC";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $periode);
        $stmt->execute();
        
        return $stmt;
    }

    public function getByOperateur($operateur) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE operateur = ? ORDER BY date_vers DESC";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $operateur);
        $stmt->execute();
        
        return $stmt;
    }
}
?>