<?php
class CommandeArticle {
    public static function getCommandeArticleByNoCommande($noCommande) {
        $query = "SELECT * FROM commandearticles".ID_GARAGE." WHERE NoCommande=:nocommande ORDER BY Position";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nocommande", $noCommande);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getCommandeArticleByTypeData($typeData) {
        $query = "SELECT * FROM commandearticles".ID_GARAGE." WHERE TypeData = :typedata";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":typedata", $typeData);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getCommandeArticleByNoCommandeAndNoArticle($noCommande, $noArticle) {
        $query = "SELECT * FROM commandearticles".ID_GARAGE." WHERE NoCommande=:nocommande AND NoArticle=:noarticle";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nocommande", $noCommande);
        $statement->bindParam(":noarticle", $noArticle);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getCommandeArticleByNoCommandeAndNoArticleAndDescription($noCommande, $noArticle, $description) {
        $query = "SELECT * FROM commandearticles".ID_GARAGE." WHERE NoCommande=:nocommande AND NoArticle=:noarticle' AND Description LIKE :description";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nocommande", $noCommande);
        $statement->bindParam(":noarticle", $noArticle);
        $statement->bindParam(":description", $description);
        $statement->execute();
        return $statement->fetchAll();
    }
    
    public static function getCommandeArticleOuvertByNoCommande($noCommande) {
        $query = "SELECT * FROM commandearticles".ID_GARAGE." WHERE NbRecu!=Quantite AND NoCommande=:nocommande";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nocommande", $noCommande);
        $statement->execute();
        return $statement->fetchAll();
    }

    

    public static function getCommandeArticleByNoCommandeAndTypeDataTo0($noCommande) {
        $query = "SELECT * FROM commandearticles".ID_GARAGE." WHERE NoCommande=:nocommande AND TypeData=0 ORDER BY Position";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nocommande", $noCommande);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getAbsoluteTVAByNoCommadneAndTypeDataTo0($noCommande) {
        $query = "SELECT DISTINCT ABS(TVA) FROM commandearticles".ID_GARAGE." WHERE NoCommande=:nocommande AND TypeData=0 ORDER BY ABS(TVA)";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nocommande", $noCommande);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getSumByNoArticleGenreFOrG($noarticle, $cpt_annee) {
        $query = "SELECT SUM(commandearticles".ID_GARAGE.".Quantite) AS Quantite, SUM(commandearticles".ID_GARAGE.".PrixMulti) AS Prix FROM commandearticles".ID_GARAGE.", commande".ID_GARAGE." WHERE commandearticles".ID_GARAGE.".NoArticle = :noarticle AND commandearticles".ID_GARAGE.".NoCommande = commande".ID_GARAGE.".NoCommande AND commande".ID_GARAGE.".DateCommande LIKE '$cpt_annee%' AND (commande".ID_GARAGE.".Genre = 'F' OR commande".ID_GARAGE.".Genre = 'G')";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":noarticle", $noarticle);
        $statement->execute();
        return $statement->fetch();
    }

    public static function getSumByNoArticleGenreN($noarticle, $cpt_annee) {
        $query = "SELECT SUM(commandearticles".ID_GARAGE.".Quantite) AS Quantite, SUM(commandearticles".ID_GARAGE.".PrixMulti) AS Prix FROM commandearticles".ID_GARAGE.", commande".ID_GARAGE." WHERE commandearticles".ID_GARAGE.".NoArticle = : AND commandearticles".ID_GARAGE.".NoCommande = commande".ID_GARAGE.".NoCommande AND commande".ID_GARAGE.".DateCommande LIKE '$cpt_annee%' AND commande".ID_GARAGE.".Genre = 'N'";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":noarticle", $noarticle);
        $statement->execute();
        return $statement->fetch();
    }

    public static function insert($noCommande, $position, $typeData, $noArticle, $description, $tva, $gm, $quantite, $prixBase, $rabais, $prixMulti) {
        $query = "INSERT INTO commandearticles".ID_GARAGE." (NoCommande, Position, TypeData, NoArticle, Description, TVA, GM, Quantite, PrixBase, Rabais, PrixMulti";
        $query.=") VALUES (:nocommande, :position,:typedata, :noarticle, :description, :tva, :gm, :quantite, :prixbase, :rabais, :prixmulti)";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nocommande", $noCommande);
        $statement->bindParam(":position", $position);
        $statement->bindParam(":typedata", $typeData);
        $statement->bindParam(":noarticle", $noArticle);
        $statement->bindParam(":description", $description);
        $statement->bindParam(":tva", $tva);
        $statement->bindParam(":gm", $gm);
        $statement->bindParam(":quantite", $quantite);
        $statement->bindParam(":prixbase", $prixBase);
        $statement->bindParam(":rabais", $rabais);
        $statement->bindParam(":prixmulti", $prixMulti);
        return $statement->execute();
    }

    public static function deleteByNoCommande($noCommande) {
        $query = "DELETE FROM commandearticles".ID_GARAGE." WHERE NoCommande=:nocommande";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nocommande", $noCommande);
        return $statement->execute();
    }

    public static function getSumQuantiteByNoCommande($noCommande, $GM = null) : float {
        if(is_null($GM)) {
            $GM = Config::getGMMO();
        }
        $query = "SELECT SUM(Quantite) FROM commandearticles".ID_GARAGE." WHERE NoCommande=:nocommande AND GM=:gm";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nocommande", $noCommande);
        $statement->bindParam(":gm", $GM);
        $statement->execute();
        $result = $statement->fetch();
        if($result) {
            if(is_null($result[0])) {
                return 0.0;
            }
            return $result[0];
        }
        return 0.0;
    }
}