<?php
class Article {
    public static function getArticleByNoArticleRR($noArticleRR) {
        $query = "SELECT * FROM articles WHERE NoArticleRR=:noarticleRR";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":noarticleRR", $noArticleRR);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getArticleByNoArticle($noArticle) {
        $query = "SELECT * FROM articles WHERE NoArticle=:noarticle";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":noarticle", $noArticle);
        $statement->execute();
        return $statement->fetch();
    }

    public static function getArticleByCodeRabais($codeRabais) {
        $query = "SELECT * FROM articles WHERE CodeRabais=:codeRabais";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":codeRabais", $codeRabais);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getArticleChainByNoArticle($noArticle) {
        $query = "SELECT A2.NoArticle FROM articles A1, articles A2 WHERE A1.NoArticle = :noarticle AND A1.Description = A2.NoArticleRR";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":noarticle", $noArticle);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getArticleStockByNoArticleRR($noArticleRR) {
        $query = "SELECT * FROM articles, stock".ID_GARAGE." WHERE articles.NoArticleRR=:noarticleRR AND articles.NoArticleRR=stock".ID_GARAGE.".NoArticleRR";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":noarticleRR", $noArticleRR);
        $statement->execute();
        return $statement->fetchAll();
    }

    // TODO duplicate of getPasDeStockLieuxArticle ?
    public static function getPasDeStockArticle($noArticleRR) {
        $query = "SELECT *, 0 as EnStock FROM articles WHERE articles.NoArticleRR=:noarticleRR";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":noarticleRR", $noArticleRR);
        $statement->execute();
        return $statement->fetchAll();
    }

    // TODO duplicate of getArticleStockByNoArticleRR ?
    public static function getArticleStockLieuxByNoArticleRR($noArticleRR) {
        $query = "SELECT a.*, s.LieuxStock, s.EnStock, s.EnTransfert FROM articles a LEFT JOIN stock".ID_GARAGE." s ON a.NoArticleRR = s.NoArticleRR WHERE a.NoArticleRR=:noarticleRR";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":noarticleRR", $noArticleRR);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getArticleStockByNoArticle($noArticle) {
        $query = "SELECT stock".ID_GARAGE.".EnStock, stock".ID_GARAGE.".EnTransfert, articles.Packages FROM articles, stock".ID_GARAGE." WHERE NoArticle = :article AND articles.NoArticleRR=stock".ID_GARAGE.".NoArticleRR";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":article", $noArticle);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getPasDeStockLieuxArticle($noArticleRR) {
        $query = "SELECT *, '' as LieuxStock, 0.00 as EnStock, 0.00 as EnTransfert FROM articles WHERE NoArticle=:noarticleRR";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":noarticleRR", $noArticleRR);
        $statement->execute();
        return $statement->fetchAll();
    }

    

    
}