feat: update attendance and cash overview methods to support date range filtering for improved analysis
This commit is contained in:
parent
b3f59708a6
commit
735b7fa9ed
@ -26,10 +26,10 @@ public function index(Request $request): Response
|
||||
'start_date' => $request->query('start_date', ''),
|
||||
'end_date' => $request->query('end_date', ''),
|
||||
],
|
||||
'attendance' => $this->analysisService->getAttendance(),
|
||||
'attendance' => $this->analysisService->getAttendance($startDate, $endDate),
|
||||
'myAttendance' => $user ? $this->analysisService->getMyAttendance($user, $startDate, $endDate) : null,
|
||||
'isManager' => $this->analysisService->isManager($user),
|
||||
'cashOverview' => $this->analysisService->getCashOverview(),
|
||||
'cashOverview' => $this->analysisService->getCashOverview($startDate, $endDate),
|
||||
'rawMaterialStock' => $this->analysisService->getRawMaterialStock(),
|
||||
'productStock' => $this->analysisService->getProductStock(),
|
||||
'revenueSummary' => $this->analysisService->getRevenueSummary($startDate, $endDate),
|
||||
|
||||
@ -21,22 +21,25 @@
|
||||
use App\Models\RawMaterialPrice;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AnalysisService
|
||||
{
|
||||
public function getAttendance(): array
|
||||
public function getAttendance(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
|
||||
$employeeQuery = Employee::query();
|
||||
$attendanceQuery = Attendance::query()->where('attendance_date', Carbon::today());
|
||||
$leaveRequestQuery = LeaveRequest::query()
|
||||
->approved()
|
||||
->where('start_date', '<=', Carbon::today())
|
||||
->where('end_date', '>=', Carbon::today());
|
||||
$attendanceQuery = Attendance::query();
|
||||
$leaveRequestQuery = LeaveRequest::query()->approved();
|
||||
|
||||
if ($startDate && $endDate) {
|
||||
$attendanceQuery->whereBetween('attendance_date', [$startDate, $endDate]);
|
||||
$leaveRequestQuery->where('start_date', '<=', $endDate)->where('end_date', '>=', $startDate);
|
||||
}
|
||||
|
||||
if (! $isSuper) {
|
||||
$employeeId = $user?->employee?->id;
|
||||
@ -122,21 +125,39 @@ public function getMyAttendance(User $user, ?Carbon $startDate = null, ?Carbon $
|
||||
];
|
||||
}
|
||||
|
||||
public function getCashOverview(): array
|
||||
public function getCashOverview(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
|
||||
$totalBalanceQuery = CashAccount::query();
|
||||
$transactionQuery = CashTransaction::query()->whereDate('created_at', Carbon::today());
|
||||
/** @var Builder $transactionQuery */
|
||||
$transactionQuery = CashTransaction::query();
|
||||
if ($startDate && $endDate) {
|
||||
$transactionQuery->whereBetween('created_at', [$startDate, $endDate]);
|
||||
}
|
||||
|
||||
if (! $isSuper) {
|
||||
$transactionQuery->where('created_by_id', $user->id);
|
||||
$totalBalanceQuery->whereHas('transactions', fn ($q) => $q->where('created_by_id', $user->id));
|
||||
}
|
||||
|
||||
$totalBalance = $totalBalanceQuery->sum('balance');
|
||||
$totalBalance = 0;
|
||||
if ($startDate && $endDate) {
|
||||
$balanceSummary = (clone $transactionQuery)
|
||||
->selectRaw("
|
||||
COALESCE(SUM(CASE WHEN type = 'deposit' THEN amount ELSE 0 END), 0) as total_deposit,
|
||||
COALESCE(SUM(CASE WHEN type = 'withdrawal' THEN amount ELSE 0 END), 0) as total_withdrawal
|
||||
")
|
||||
->first();
|
||||
$totalBalance = (int) (($balanceSummary->total_deposit ?? 0) - ($balanceSummary->total_withdrawal ?? 0));
|
||||
} else {
|
||||
$totalBalanceQuery = CashAccount::query();
|
||||
if (! $isSuper) {
|
||||
$totalBalanceQuery->whereHas('transactions', fn ($q) => $q->where('created_by_id', $user->id));
|
||||
}
|
||||
$totalBalance = (int) $totalBalanceQuery->sum('balance');
|
||||
}
|
||||
|
||||
$summary = $transactionQuery
|
||||
->selectRaw("
|
||||
COUNT(*) as total_transactions,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user