1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <?php
// Pour mes test j'inclus l'objet pdo dans le constructeur, par la suite je ferai une extension de l'objet PDO
// Pour mes test je passe les vérifications de type et autre
class Budget{
public $pdo = false; // Objet de connexion PDO
public $year = 0; // Année ciblée
public $data = array(); // Tableau de données
public function __construct($pdo=false,$year=0){
$this->pdo = $pdo;
$this->year = $year; // Si 0 = année actuelle bien sûr
$this->data = $this->_getGroupes(0);
}
public function _getGroupes($idp=0){ // idp = id du groupe parent
$a = array();
$req = "SELECT * FROM budget_groupes WHERE idp='".$idp."'";
$qry = $this->pdo->query($req);
while($r = $qry->fetch(PDO::FETCH_ASSOC)){
$a[$r['id']] = $r;
$a[$r['id']]['sub'] = ($r['idp'] > 0) ? $this->_getGroupes($r['idp']) : false; // Récursivité
$a[$r['id']]['poste'] = $this->_getPostes($r['id']);
$a[$r['id']]['budget'] = 0; // C'EST ICI LE PROBLEME, comment obtenir le total du budget des différents groupes et/ou postes enfants
}
return $a;
}
public function _getPostes($idg=0){ // idg = id du groupe auquel appartient les postes
$a = array();
$req = "SELECT * FROM budget_postes WHERE idg='".$idg."'";
$qry = $this->pdo->query($req);
while($r = $qry->fetch(PDO::FETCH_ASSOC)){
$a[$r['id']] = $r;
$a[$r['id']]['budget'] = $this->_getBugetPoste($r['id']);
}
return $a;
}
public function _getBudgetPoste($id=0){ // id = id du poste ciblé
$req = "SELECT * FROM budget_prices WHERE idp='".$id."' AND year='".$this->year."'";
$qry = $this->pdo->query($req);
// Un poste ne peux avoir qu'un seul budget pour une année, pas besoin de boucle
return ($qry->rowCount() > 0) ? $qry->fetch(PDO::FETCH_ASSOC) : array('id'=>0,'idp'=>$id,'year'=>$this->year,'price'=>0,'solde'=>0);
}
}
$o = new Budget($pdo,2022);
print_r($o->data);
?> |