<?php
/**
 * Tarif Model
 * Represents rates from tarifs table
 */

class Tarif {
    private $conn;
    private $table_name = "tarifs";

    public $num;
    public $type;
    public $prix_ht;
    public $tva;
    public $mt_tva;
    public $prix_ttc;
    public $timbre;
    public $ttc;

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

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

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

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

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " SET type=:type, prix_ht=:prix_ht, tva=:tva, mt_tva=:mt_tva, prix_ttc=:prix_ttc, timbre=:timbre, ttc=:ttc";
        
        $stmt = $this->conn->prepare($query);
        
        $this->type = htmlspecialchars(strip_tags($this->type));
        $this->prix_ht = htmlspecialchars(strip_tags($this->prix_ht));
        $this->tva = htmlspecialchars(strip_tags($this->tva));
        $this->mt_tva = htmlspecialchars(strip_tags($this->mt_tva));
        $this->prix_ttc = htmlspecialchars(strip_tags($this->prix_ttc));
        $this->timbre = htmlspecialchars(strip_tags($this->timbre));
        $this->ttc = htmlspecialchars(strip_tags($this->ttc));
        
        $stmt->bindParam(":type", $this->type);
        $stmt->bindParam(":prix_ht", $this->prix_ht);
        $stmt->bindParam(":tva", $this->tva);
        $stmt->bindParam(":mt_tva", $this->mt_tva);
        $stmt->bindParam(":prix_ttc", $this->prix_ttc);
        $stmt->bindParam(":timbre", $this->timbre);
        $stmt->bindParam(":ttc", $this->ttc);
        
        if ($stmt->execute()) {
            return true;
        }
        
        return false;
    }

    public function update($num) {
        $query = "UPDATE " . $this->table_name . " SET type=:type, prix_ht=:prix_ht, tva=:tva, mt_tva=:mt_tva, prix_ttc=:prix_ttc, timbre=:timbre, ttc=:ttc WHERE num = :num";
        
        $stmt = $this->conn->prepare($query);
        
        $this->type = htmlspecialchars(strip_tags($this->type));
        $this->prix_ht = htmlspecialchars(strip_tags($this->prix_ht));
        $this->tva = htmlspecialchars(strip_tags($this->tva));
        $this->mt_tva = htmlspecialchars(strip_tags($this->mt_tva));
        $this->prix_ttc = htmlspecialchars(strip_tags($this->prix_ttc));
        $this->timbre = htmlspecialchars(strip_tags($this->timbre));
        $this->ttc = htmlspecialchars(strip_tags($this->ttc));
        
        $stmt->bindParam(":type", $this->type);
        $stmt->bindParam(":prix_ht", $this->prix_ht);
        $stmt->bindParam(":tva", $this->tva);
        $stmt->bindParam(":mt_tva", $this->mt_tva);
        $stmt->bindParam(":prix_ttc", $this->prix_ttc);
        $stmt->bindParam(":timbre", $this->timbre);
        $stmt->bindParam(":ttc", $this->ttc);
        $stmt->bindParam(":num", $num);
        
        if ($stmt->execute()) {
            return true;
        }
        
        return false;
    }
}
?>