fix(scanner): add team_id to collection_logs for robust stats tracking
- Created a database migration to add `team_id` to `collection_logs`. - Created a database migration to backfill `team_id` for past scans using `trip_id` and scanner's active team. - Updated `ScanService` to lookup the scanner's active team and store the `team_id` directly on the `CollectionLog`. - Modified the `CollectionTeam` model's `collectionLogs()` relation to be a direct `HasMany` using the new `team_id` instead of routing through `Trip`. - Updated `AdminTeamReportController` to query metrics directly from `collection_logs.team_id`, ensuring all past and future scans (including those done without an active trip) correctly increment team stats and daily charts.
This commit is contained in:
@@ -75,12 +75,10 @@ class AdminTeamReportController extends ApiController
|
||||
|
||||
$scanStats = Tenancy::withoutScope(function () use ($teamIds, $fromStr, $toStr) {
|
||||
return DB::table('collection_logs')
|
||||
->join('trips', 'trips.id', '=', 'collection_logs.trip_id')
|
||||
->select('trips.team_id', DB::raw('COUNT(collection_logs.id) as total_scans'))
|
||||
->whereIn('trips.team_id', $teamIds)
|
||||
->whereBetween('collection_logs.scanned_at', [$fromStr, $toStr])
|
||||
->whereNull('trips.deleted_at')
|
||||
->groupBy('trips.team_id')
|
||||
->select('team_id', DB::raw('COUNT(id) as total_scans'))
|
||||
->whereIn('team_id', $teamIds)
|
||||
->whereBetween('scanned_at', [$fromStr, $toStr])
|
||||
->groupBy('team_id')
|
||||
->get()
|
||||
->keyBy('team_id');
|
||||
});
|
||||
@@ -179,10 +177,10 @@ class AdminTeamReportController extends ApiController
|
||||
$kpis = $this->buildKpis($team->id, $tripIds, $fromDate, $toDate, $fromDT, $toDT);
|
||||
|
||||
// --- Daily scans (group by DATE) ---
|
||||
$dailyScans = Tenancy::withoutScope(function () use ($tripIds, $fromDT, $toDT) {
|
||||
$dailyScans = Tenancy::withoutScope(function () use ($team, $fromDT, $toDT) {
|
||||
return DB::table('collection_logs')
|
||||
->selectRaw('DATE(scanned_at) as date, COUNT(*) as scans')
|
||||
->whereIn('trip_id', $tripIds)
|
||||
->where('team_id', $team->id)
|
||||
->whereBetween('scanned_at', [$fromDT, $toDT])
|
||||
->groupByRaw('DATE(scanned_at)')
|
||||
->orderBy('date')
|
||||
@@ -191,10 +189,10 @@ class AdminTeamReportController extends ApiController
|
||||
});
|
||||
|
||||
// --- Weekly scans (group by YEARWEEK) ---
|
||||
$weeklyScans = Tenancy::withoutScope(function () use ($tripIds, $fromDT, $toDT) {
|
||||
$weeklyScans = Tenancy::withoutScope(function () use ($team, $fromDT, $toDT) {
|
||||
return DB::table('collection_logs')
|
||||
->selectRaw("DATE(DATE_SUB(scanned_at, INTERVAL WEEKDAY(scanned_at) DAY)) as week_start, COUNT(*) as scans")
|
||||
->whereIn('trip_id', $tripIds)
|
||||
->where('team_id', $team->id)
|
||||
->whereBetween('scanned_at', [$fromDT, $toDT])
|
||||
->groupByRaw("DATE(DATE_SUB(scanned_at, INTERVAL WEEKDAY(scanned_at) DAY))")
|
||||
->orderBy('week_start')
|
||||
@@ -314,10 +312,10 @@ class AdminTeamReportController extends ApiController
|
||||
->first();
|
||||
});
|
||||
|
||||
$scanStats = Tenancy::withoutScope(function () use ($tripIds, $fromDT, $toDT) {
|
||||
$scanStats = Tenancy::withoutScope(function () use ($teamId, $fromDT, $toDT) {
|
||||
return DB::table('collection_logs')
|
||||
->selectRaw('COUNT(*) as total_scans, SUM(COALESCE(weight_kg,0)) as total_scan_weight')
|
||||
->whereIn('trip_id', $tripIds)
|
||||
->where('team_id', $teamId)
|
||||
->whereBetween('scanned_at', [$fromDT, $toDT])
|
||||
->first();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user