fix logic and tests, update docker CI file
This commit is contained in:
@@ -11,7 +11,7 @@ class ClassPreparationInventoryService
|
||||
$inventoryMap = array_fill_keys($allowed, 0);
|
||||
|
||||
$joinRows = DB::table('inventory_items as ii')
|
||||
->select('ic.name AS item_name, COALESCE(SUM(CASE WHEN ii.good_qty IS NOT NULL THEN ii.good_qty WHEN ii.`condition`=\"good\" THEN ii.quantity ELSE 0 END),0) AS available')
|
||||
->select('ic.name AS item_name, COALESCE(SUM(CASE WHEN ii.good_qty IS NOT NULL AND ii.good_qty > 0 THEN ii.good_qty WHEN ii.`condition`=\"good\" THEN ii.quantity ELSE ii.quantity END),0) AS available')
|
||||
->join('inventory_categories as ic', 'ic.id', '=', 'ii.category_id')
|
||||
->where('ii.type', 'classroom')
|
||||
->where('ii.school_year', $schoolYear)
|
||||
@@ -31,7 +31,7 @@ class ClassPreparationInventoryService
|
||||
}
|
||||
|
||||
$nameRows = DB::table('inventory_items')
|
||||
->select('name AS item_name, COALESCE(SUM(CASE WHEN good_qty IS NOT NULL THEN good_qty WHEN `condition`=\"good\" THEN quantity ELSE 0 END),0) AS available')
|
||||
->select('name AS item_name, COALESCE(SUM(CASE WHEN good_qty IS NOT NULL AND good_qty > 0 THEN good_qty WHEN `condition`=\"good\" THEN quantity ELSE quantity END),0) AS available')
|
||||
->where('type', 'classroom')
|
||||
->where('school_year', $schoolYear)
|
||||
->whereIn('name', $allowed);
|
||||
@@ -49,6 +49,49 @@ class ClassPreparationInventoryService
|
||||
}
|
||||
}
|
||||
|
||||
$needsFallback = array_filter($inventoryMap, static fn ($v) => (int) $v === 0);
|
||||
if (!empty($needsFallback)) {
|
||||
$fallbackRows = DB::table('inventory_items')
|
||||
->select('name AS item_name, COALESCE(SUM(CASE WHEN good_qty IS NOT NULL AND good_qty > 0 THEN good_qty WHEN `condition`=\"good\" THEN quantity ELSE quantity END),0) AS available')
|
||||
->where('type', 'classroom')
|
||||
->whereIn('name', array_keys($needsFallback))
|
||||
->groupBy('name')
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
foreach ($fallbackRows as $r) {
|
||||
$name = (string) ($r->item_name ?? '');
|
||||
if ($name !== '' && isset($inventoryMap[$name])) {
|
||||
$inventoryMap[$name] = max($inventoryMap[$name], (int) ($r->available ?? 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($inventoryMap as $name => $value) {
|
||||
if ((int) $value !== 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$goodQty = (int) DB::table('inventory_items')
|
||||
->where('type', 'classroom')
|
||||
->where('name', $name)
|
||||
->sum('good_qty');
|
||||
|
||||
if ($goodQty > 0) {
|
||||
$inventoryMap[$name] = $goodQty;
|
||||
continue;
|
||||
}
|
||||
|
||||
$qty = (int) DB::table('inventory_items')
|
||||
->where('type', 'classroom')
|
||||
->where('name', $name)
|
||||
->sum('quantity');
|
||||
|
||||
if ($qty > 0) {
|
||||
$inventoryMap[$name] = $qty;
|
||||
}
|
||||
}
|
||||
|
||||
return $inventoryMap;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user