@@ -75,12 +75,7 @@ class InventoryController extends BaseController
|
||||
$selectedSem = $selectedSemRaw === null ? (string) $this->semester : trim((string) $selectedSemRaw);
|
||||
|
||||
$builder = $this->itemModel->where('type', $type);
|
||||
if (strtolower($selectedYear) !== 'all') {
|
||||
$builder->where('school_year', $selectedYear);
|
||||
}
|
||||
if ($selectedSem !== '') {
|
||||
$builder->where('semester', $selectedSem);
|
||||
}
|
||||
$this->applyInventoryMovementPeriodFilter($builder, 'inventory_items', $selectedYear, $selectedSem);
|
||||
|
||||
$items = $builder->orderBy('name', 'ASC')->findAll();
|
||||
$categories = $this->catModel->optionsForType($type);
|
||||
@@ -474,17 +469,26 @@ class InventoryController extends BaseController
|
||||
// Distinct school years for a given inventory type (newest first).
|
||||
private function getSchoolYearsForType(string $type): array
|
||||
{
|
||||
// Pull distinct non-null school_year values for this type
|
||||
$years = $this->itemModel
|
||||
->select('school_year')
|
||||
->where('type', $type)
|
||||
->where('school_year IS NOT NULL', null, false)
|
||||
->groupBy('school_year')
|
||||
->orderBy('school_year', 'DESC')
|
||||
->findColumn('school_year') ?? [];
|
||||
$rows = $this->db->table('inventory_movements m')
|
||||
->distinct()
|
||||
->select('m.school_year')
|
||||
->join('inventory_items i', 'i.id = m.item_id', 'inner')
|
||||
->where('m.school_year IS NOT NULL', null, false)
|
||||
->where("TRIM(m.school_year) <>", '')
|
||||
->orderBy('m.school_year', 'DESC');
|
||||
|
||||
if (strtolower($type) !== 'all') {
|
||||
$rows->where('i.type', $type);
|
||||
}
|
||||
|
||||
$rows = $rows->get()
|
||||
->getResultArray();
|
||||
|
||||
// Normalize & de-dup
|
||||
$years = array_values(array_unique(array_filter(array_map('trim', $years))));
|
||||
$years = array_values(array_unique(array_filter(array_map(
|
||||
static fn(array $row): string => trim((string)($row['school_year'] ?? '')),
|
||||
$rows
|
||||
))));
|
||||
|
||||
// Ensure the current year (from config) appears as an option, even if no rows yet
|
||||
$currentYear = $this->schoolYear;
|
||||
@@ -496,6 +500,31 @@ class InventoryController extends BaseController
|
||||
return $years;
|
||||
}
|
||||
|
||||
private function applyInventoryMovementPeriodFilter($builder, string $itemAlias, ?string $schoolYear, ?string $semester = null): void
|
||||
{
|
||||
$schoolYear = trim((string) $schoolYear);
|
||||
$semester = trim((string) $semester);
|
||||
$clauses = ["m.item_id = {$itemAlias}.id"];
|
||||
|
||||
if ($schoolYear !== '' && strtolower($schoolYear) !== 'all') {
|
||||
$clauses[] = 'm.school_year = ' . $this->db->escape($schoolYear);
|
||||
}
|
||||
|
||||
if ($semester !== '') {
|
||||
$clauses[] = 'm.semester = ' . $this->db->escape($semester);
|
||||
}
|
||||
|
||||
if (count($clauses) <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$builder->where(
|
||||
'EXISTS (SELECT 1 FROM inventory_movements m WHERE ' . implode(' AND ', $clauses) . ')',
|
||||
null,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
private function recordMovement(
|
||||
int $itemId,
|
||||
int $qtyChange,
|
||||
@@ -1095,7 +1124,7 @@ class InventoryController extends BaseController
|
||||
|
||||
// Filter by school year unless "all"
|
||||
if (!$isAllYears) {
|
||||
$qbItems->where('i.school_year', $selectedYear);
|
||||
$this->applyInventoryMovementPeriodFilter($qbItems, 'i', $selectedYear);
|
||||
}
|
||||
|
||||
// Optional: filter by type if provided
|
||||
@@ -1123,7 +1152,7 @@ class InventoryController extends BaseController
|
||||
return view('inventory/summary', [
|
||||
'selectedYear' => $selectedYear,
|
||||
'currentYear' => $this->schoolYear,
|
||||
'schoolYears' => $this->getSchoolYearsForType('book') ?: [$this->schoolYear, 'All'],
|
||||
'schoolYears' => $this->getSchoolYearsForType($selectedType) ?: [$this->schoolYear, 'All'],
|
||||
'rows' => $rows,
|
||||
'totals' => $totals,
|
||||
// optionally pass the selected type if your view supports it
|
||||
@@ -1202,7 +1231,7 @@ class InventoryController extends BaseController
|
||||
}
|
||||
|
||||
// -------- Year dropdown options --------
|
||||
$schoolYears = $this->getSchoolYearsForType('book') ?: [$this->schoolYear, 'All'];
|
||||
$schoolYears = $this->getSchoolYearsForType($selectedType) ?: [$this->schoolYear, 'All'];
|
||||
|
||||
return view('inventory/summary', [
|
||||
'selectedYear' => $selectedYear,
|
||||
|
||||
Reference in New Issue
Block a user