begin to calculate stock, next calculate outputed
This commit is contained in:
parent
c4f8b4fb87
commit
71775c0f67
9 changed files with 96 additions and 68 deletions
|
|
@ -7,13 +7,6 @@ use Garradin\Plugin\Materiels\Category;
|
|||
|
||||
class Equipment
|
||||
{
|
||||
protected $columns_order = array(
|
||||
'id',
|
||||
'category_id',
|
||||
'stock_number',
|
||||
'designation',
|
||||
);
|
||||
|
||||
public function add($data = [])
|
||||
{
|
||||
$db = DB::getInstance();
|
||||
|
|
@ -32,20 +25,43 @@ class Equipment
|
|||
return DB::getInstance()->first('SELECT * FROM plugin_materiels_equipment WHERE id = ?;', $id);
|
||||
}
|
||||
|
||||
public function listAll()
|
||||
{
|
||||
return DB::getInstance()->get('SELECT * FROM plugin_materiels_equipment ORDER BY designation;');
|
||||
}
|
||||
|
||||
public function listAllByCategory()
|
||||
{
|
||||
$cat = new Category;
|
||||
$cats = $cat->listAll();
|
||||
$category = new Category;
|
||||
$cats = $category->listAll();
|
||||
$eqmts_by_cat = array();
|
||||
foreach ($cats as $cat) {
|
||||
$eqmts_by_cat[$cat->name] = DB::getInstance()->get(
|
||||
'SELECT * FROM plugin_materiels_equipment WHERE category_id = ? ORDER BY designation;', $cat->id);
|
||||
$eqmts_by_cat[$cat->name] = $category->listAllEquipments($cat->id);
|
||||
}
|
||||
return $eqmts_by_cat;
|
||||
}
|
||||
|
||||
public function CalculateStock($id)
|
||||
{
|
||||
$entries = DB::getInstance()->firstColumn(
|
||||
"SELECT sum(equipment_number) FROM plugin_materiels_entry WHERE kind IN (
|
||||
'Achat', 'Don', 'Récupération') AND equipment_id = ?;", $id);
|
||||
$outputs = DB::getInstance()->firstColumn(
|
||||
"SELECT sum(equipment_number) FROM plugin_materiels_output WHERE kind IN (
|
||||
'Vente', 'Don', 'Besoin', 'Autre (perte, vol, ...)') AND equipment_id = ?;", $id);
|
||||
return $entries - $outputs;
|
||||
}
|
||||
|
||||
public function listAllOwnedByCategory()
|
||||
{
|
||||
$eqmts_by_cat = $this->listAllByCategory();
|
||||
$eqmts_owned_by_cat = array();
|
||||
foreach ($eqmts_by_cat as $cat => $eqmts) {
|
||||
$eqmts_owned = array();
|
||||
foreach ($eqmts as $eqmt) {
|
||||
$stock = $this->CalculateStock($eqmt->id);
|
||||
if ($stock) {
|
||||
$eqmt->stock = $this->CalculateStock($eqmt->id);
|
||||
array_push($eqmts_owned, $eqmt);
|
||||
}
|
||||
}
|
||||
$eqmts_owned_by_cat[$cat] = $eqmts_owned;
|
||||
}
|
||||
return $eqmts_owned_by_cat;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue