diff --git a/app/Models/ReportDuty.php b/app/Models/ReportDuty.php index b905645..9fb0596 100644 --- a/app/Models/ReportDuty.php +++ b/app/Models/ReportDuty.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; class ReportDuty extends Model @@ -25,6 +26,25 @@ class ReportDuty extends Model 'period_end' => 'datetime:Y-m-d H:i:s', ]; + public function scopeWithinPeriod(Builder $query, string $startAt, string $endAt): Builder + { + return $query + ->where('period_end', '>=', $startAt) + ->where('period_start', '<=', $endAt); + } + + public function scopeExactPeriod(Builder $query, string $startAt, string $endAt): Builder + { + return $query + ->where('period_start', '>=', $startAt) + ->where('period_end', '<=', $endAt); + } + + public function scopeOnlySubmitted(Builder $query): Builder + { + return $query->where('status_id', 2); + } + public function department() { return $this->belongsTo(Department::class, 'rf_department_id', 'department_id'); diff --git a/app/Services/StatisticsService.php b/app/Services/StatisticsService.php index 4ab49df..43ce97e 100644 --- a/app/Services/StatisticsService.php +++ b/app/Services/StatisticsService.php @@ -7,6 +7,7 @@ namespace App\Services; use App\Models\Department; use App\Models\MetrikaResult; use App\Models\Report; +use App\Models\ReportDuty; use App\Models\User; use App\Models\UserDepartment; use Carbon\Carbon; @@ -119,7 +120,7 @@ class StatisticsService foreach ($deptList as $dept) { $deptId = $dept->department_id; - $lastReportQuery = Report::where('rf_department_id', $deptId); + $lastReportQuery = ReportDuty::where('rf_department_id', $deptId); if ($isRangeOneDay) { $lastReportQuery->exactPeriod($startDate, $endDate); } else {