From 71775c0f67014612bcb017d4b910401482cb6e43 Mon Sep 17 00:00:00 2001 From: JBthePenguin Date: Mon, 7 Jun 2021 18:30:20 +0200 Subject: [PATCH] begin to calculate stock, next calculate outputed --- src/lib/Category.php | 11 ++--- src/lib/Entry.php | 15 +------ src/lib/Equipment.php | 48 +++++++++++++------- src/lib/Output.php | 20 +++++++++ src/schema.sql | 1 - src/templates/index.tpl | 54 ++++++++++++++--------- src/www/admin/entrees/deja_repertorie.php | 4 -- src/www/admin/entrees/non_repertorie.php | 1 - src/www/admin/index.php | 10 +---- 9 files changed, 96 insertions(+), 68 deletions(-) create mode 100644 src/lib/Output.php diff --git a/src/lib/Category.php b/src/lib/Category.php index ca69d87..84562f1 100644 --- a/src/lib/Category.php +++ b/src/lib/Category.php @@ -6,11 +6,6 @@ use Garradin\DB; class Category { - protected $columns_order = array( - 'id', - 'name', - ); - public function add($data = []) { DB::getInstance()->insert('plugin_materiels_category', $data); @@ -37,4 +32,10 @@ class Category { return DB::getInstance()->get('SELECT * FROM plugin_materiels_category ORDER BY name;'); } + + public function listAllEquipments($id) + { + return DB::getInstance()->get( + 'SELECT * FROM plugin_materiels_equipment WHERE category_id = ? ORDER BY designation;', $id); + } } diff --git a/src/lib/Entry.php b/src/lib/Entry.php index 9ea865f..21e6a86 100644 --- a/src/lib/Entry.php +++ b/src/lib/Entry.php @@ -6,25 +6,14 @@ use Garradin\DB; class Entry { - protected $columns_order = array( - 'id', - 'kind', - 'equipment_number', - 'equipment_id', - 'entry_date', - 'additional_comment', - ); - public function listKinds() { return array( 'Achat', 'Don', 'Récupération', - 'Location', - 'Retour de location', - 'Prêt', - 'Retour de prêt', + 'Location / Prêt', + 'Retour de location / prêt', ); } diff --git a/src/lib/Equipment.php b/src/lib/Equipment.php index 91f9320..7073614 100644 --- a/src/lib/Equipment.php +++ b/src/lib/Equipment.php @@ -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; + } } diff --git a/src/lib/Output.php b/src/lib/Output.php new file mode 100644 index 0000000..fa81984 --- /dev/null +++ b/src/lib/Output.php @@ -0,0 +1,20 @@ + - - Nombre en stock - Désignation - Categorie - - - - {foreach from=$eqmts item="eqmt"} - - {$eqmt.stock_number} - {$eqmt.designation} - {$eqmt.category.name} - - {linkbutton shape="edit" label="Modifier" href="modifier_materiel.php?id=%d"|args:$eqmt.id} - - - {/foreach} - - +

Matériel dont l'association est propriétaire

+{foreach from=$eqmts_owned_by_cat key='cat' item="eqmts"} + {if $eqmts} +

{$cat}

+ + + + + + + + + + + + {foreach from=$eqmts item="eqmt"} + + + + + + + + {/foreach} + +
DésignationStockSorti en location / prêtDisponible
{$eqmt.designation}{$eqmt.stock}00 + {linkbutton shape="edit" label="Modifier" href="modifier_materiel.php?id=%d"|args:$eqmt.id} +
+ {/if} +{/foreach} + +

Matériel dont l'association n'est pas propriétaire

+ +

Matériel déjà répertorié qui n'est plus en possession de l'association

{include file="admin/_foot.tpl"} diff --git a/src/www/admin/entrees/deja_repertorie.php b/src/www/admin/entrees/deja_repertorie.php index 7ea6867..8031290 100644 --- a/src/www/admin/entrees/deja_repertorie.php +++ b/src/www/admin/entrees/deja_repertorie.php @@ -24,10 +24,6 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) 'entry_date' => f('entry_date'), 'additional_comment' => f('additional_comment'), ]); - $eq = $eqmt->get((int) f('equipment_id')); - $eqmt->edit($eq->id, [ - 'stock_number' => (int) $eq->stock_number + (int) f('equipment_number'), - ]); Utils::redirect(PLUGIN_URL . 'entrees/index.php'); } } diff --git a/src/www/admin/entrees/non_repertorie.php b/src/www/admin/entrees/non_repertorie.php index 3c9ecc0..c5dfe79 100644 --- a/src/www/admin/entrees/non_repertorie.php +++ b/src/www/admin/entrees/non_repertorie.php @@ -41,7 +41,6 @@ if (f('save')) $eqmt = new Equipment; $eqmt_id = $eqmt->add([ 'category_id' => (int) f('category_id'), - 'stock_number' => (int) f('equipment_number'), 'designation' => ucfirst(strtolower(f('designation'))), ]); $entry->add([ diff --git a/src/www/admin/index.php b/src/www/admin/index.php index 8248e3f..f61213a 100644 --- a/src/www/admin/index.php +++ b/src/www/admin/index.php @@ -8,19 +8,13 @@ if ($plugin->needUpgrade()) } use Garradin\Plugin\Materiels\Equipment; -use Garradin\Plugin\Materiels\Category; require_once __DIR__ . '/_inc.php'; $eqmt = new Equipment; -$cat = new Category; -$eqmts = $eqmt->listAll(); +$eqmts_owned_by_cat = $eqmt->listAllOwnedByCategory(); -foreach ($eqmts as $key => $value) { - $eqmts[$key]->category = $cat->get($value->category_id); -} - -$tpl->assign(compact('eqmts')); +$tpl->assign(compact('eqmts_owned_by_cat')); $tpl->display(PLUGIN_ROOT . '/templates/index.tpl');