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:
Developer
2026-07-06 15:59:35 +08:00
parent 78c246c72f
commit aa8fdaa5aa
8 changed files with 166 additions and 20 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Services\Scan;
use App\Models\CollectionLog;
use App\Models\CollectionTeam;
use App\Models\DropOffPoint;
use App\Models\QrCode;
use App\Models\Trip;
@@ -86,8 +87,13 @@ class ScanService
'scanned_by_user_id' => $scanner->id,
])->save();
$team = CollectionTeam::where('scanner_id', $scanner->id)
->where('status', CollectionTeam::STATUS_ACTIVE)
->first();
$log = CollectionLog::create([
'qr_code_id' => $code->id,
'team_id' => $team?->id,
'household_id' => $code->assigned_to_household_id,
'drop_off_point_id' => $dropOff->id,
'scanned_by_user_id' => $scanner->id,