diff --git a/src/lib/Category.php b/src/lib/Category.php index 46ad77c..37d4d85 100644 --- a/src/lib/Category.php +++ b/src/lib/Category.php @@ -7,32 +7,38 @@ use Garradin\DB; class Category { public function add($data = []) + // add new category { DB::getInstance()->insert('plugin_materiels_category', $data); } public function edit($id, $data = []) + // edit a specific category { $db = DB::getInstance(); $db->update('plugin_materiels_category', $data, $db->where('id', $id)); } public function delete($id) + // delete a specific category { DB::getInstance()->delete('plugin_materiels_category', 'id = ' . $id); } public function get($id) + // get and return a specific category { return DB::getInstance()->first('SELECT * FROM plugin_materiels_category WHERE id = ?;', $id); } public function listAll() + // return a list of all categories ordered by name { return DB::getInstance()->get('SELECT * FROM plugin_materiels_category ORDER BY name;'); } public function listAllEquipments($id) + // return a list of all equipments for a specific category ordered by equipments's designation { return DB::getInstance()->get( 'SELECT * FROM plugin_materiels_equipment WHERE category_id = ? ORDER BY designation;', $id); diff --git a/src/lib/Equipment.php b/src/lib/Equipment.php index f0e8368..f113477 100644 --- a/src/lib/Equipment.php +++ b/src/lib/Equipment.php @@ -41,7 +41,8 @@ class Equipment return $eqmts_by_cat; } - public function CalculateStock($id) + public function CalculateOwned($id) + // return number of equipments owned { $entries = DB::getInstance()->firstColumn( "SELECT sum(equipment_number) FROM plugin_materiels_entry WHERE kind IN ( @@ -52,7 +53,8 @@ class Equipment return $entries - $outputs; } - public function CalculateOutOfStock($id) + public function CalculateOwnedOut($id) + // return number of equipments owned out { $entries = DB::getInstance()->firstColumn( "SELECT sum(equipment_number) FROM plugin_materiels_entry WHERE kind = 'Retour de location / prêt' AND equipment_id = ?;", $id); @@ -62,6 +64,7 @@ class Equipment } public function CalculateNoOwned($id) + // return number of equipments no owned { $entries = DB::getInstance()->firstColumn( "SELECT sum(equipment_number) FROM plugin_materiels_entry WHERE kind = 'Location / Prêt' AND equipment_id = ?;", $id); @@ -71,28 +74,33 @@ class Equipment } public function AllListsAll($eqmts) + // construct and return 3 lists with a specific list of equipments: + // equipments owned, equipments no owned and equipments just listed { $eqmts_owned = array(); $eqmts_no_owned = array(); $eqmts_just_listed = array(); foreach ($eqmts as $eqmt) { - $stock = $this->CalculateStock($eqmt->id); - if ($stock) { - $eqmt->stock = $stock; - $out_of_stock = $this->CalculateOutOfStock($eqmt->id); - if ($out_of_stock) { - $eqmt->out_of_stock = $out_of_stock; + $owned = $this->CalculateOwned($eqmt->id); + if ($owned) { + // add equipment to list of equiments owned with his number and number of out + $eqmt->owned = $owned; + $owned_out = $this->CalculateOwnedOut($eqmt->id); + if ($owned_out) { + $eqmt->owned_out = $owned_out; } else { - $eqmt->out_of_stock = 0; + $eqmt->owned_out = 0; } array_push($eqmts_owned, $eqmt); } $no_owned = $this->CalculateNoOwned($eqmt->id); if ($no_owned) { + // add equipment to list of equiments no owned with his number $eqmt->no_owned = $no_owned; array_push($eqmts_no_owned, $eqmt); } - if ($stock + $no_owned == 0) { + if ($owned + $no_owned == 0) { + // add equipment to list of equiments just listed array_push($eqmts_just_listed, $eqmt); } } @@ -172,7 +180,7 @@ class Equipment foreach ($eqmts_by_cat as $cat => $eqmts) { $eqmts_released = array(); foreach ($eqmts as $eqmt) { - $released = $this->CalculateOutOfStock($eqmt->id); + $released = $this->CalculateOwnedOut($eqmt->id); if ($released) { $eqmt->released = $released; array_push($eqmts_released, $eqmt); diff --git a/src/templates/categories/materiels_par_categorie.tpl b/src/templates/categories/materiels_par_categorie.tpl index 2051f97..e2eae6c 100644 --- a/src/templates/categories/materiels_par_categorie.tpl +++ b/src/templates/categories/materiels_par_categorie.tpl @@ -23,9 +23,9 @@ {foreach from=$eqmts_owned item="eqmt"}