<?php

class FamilleActes
{

    /**
     * @var Mention $codes
     */
    private $mention;

    /**
     * @var string $texte
     */
    private $texte;



    /**
     * @var array $listesCode
     */
    private $listesCode;

    /**z
     * @var integer $seuil
     */
    private $seuil;

    public function __construct($mention, $texte,$listesCode, $seuil){
        $this->mention = $mention;
        $this->texte = $texte;
        $this->listesCode = $listesCode;
        $this->seuil = $seuil;
    }

    /**
     * @return array
     */
    public function getCodes(){
        return  $this->codes;
    }

    /**
     * @return string
     */
    public function getTexte()
    {
        return $this->texte;
    }

    /**
     * @return integer
     */
    public function getSeuil(){
        return $this->seuil;
    }

    /**
     * @return Mention
     */
    public function getMention()
    {
        return $this->mention;
    }

    public function getFamillesActesOid($iCTI_connexion)
    {
        $famillesOid = array();
        $famillesCodes = "'" . implode("', '", $this->codes) . "'";

            $sql = <<<SQL
            SELECT
                t_etude_cardio_familles_actes.oid,
                t_etude_cardio_familles_actes.code
             FROM pmsi.t_etude_cardio_familles_actes
            WHERE  t_etude_cardio_familles_actes.code IN ($famillesCodes)
            ORDER BY 1
                    ;
SQL;
            $result = $iCTI_connexion->query($sql);
            while ($row = $iCTI_connexion->fetchAssoc($result)) {
                $famillesOid[$row['code']] = $row['oid'];
            }


        return $famillesOid;
    }

// public function getNbActes($iCTI_connexion, $year)
//    {
//
//        $count = 0;
//        $famillesCodes = "'" . implode("', '", $this->codes) . "'";
//
//        $sql = <<<SQL
//            SELECT
//            count(acte_id) AS count
//               FROM pmsi.p_rss_cardio
//               JOIN pmsi.t_etude_cardio_familles_actes ON p_rss_cardio.cardio_famille_acte_id = t_etude_cardio_familles_actes.oid
//                WHERE  t_etude_cardio_familles_actes.code IN ($famillesCodes)
//                AND EXTRACT('Year' From date_acte) like $year;
//SQL;
//
//        $result = $iCTI_connexion->query($sql);
//        $record = $iCTI_connexion->fetchAssoc($result);
//
//        if ($record) {
//            $count = $record['count'];
//        }
//        return $count;
//    }

    public function getNbActes($iCTI_connexion, $year)
    {

        $count = 0;
        $listesCodeSql = "'" . implode("', '", $this->listesCode) . "'";

        $sql = <<<SQL
            SELECT
                count(acte_id) AS count
            FROM pmsi.p_rss_cardio
            JOIN pmsi.t_listes ON t_listes.oid = liste_id
			WHERE t_listes.code IN($listesCodeSql) 
                AND EXTRACT('Year' From date_acte) like $year;
SQL;

        $result = $iCTI_connexion->query($sql);
        $record = $iCTI_connexion->fetchAssoc($result);

        if ($record) {
            $count = $record['count'];
        }
        return $count;
    }

    public function checkSeuilActes($iCTI_connexion, $year, $nbActes = null)
    {
        if ($nbActes == null) {
            $nbActes = $this->getNbActes($iCTI_connexion, $year);
        }
        return $nbActes >= $this->seuil;
    }

}