92 lines
3.3 KiB
PHP
92 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class OutstandingHelper
|
|
{
|
|
/**
|
|
* Filter a collection of forms to retrieve outstanding items based on role-driven rules.
|
|
*
|
|
* @param Collection $forms
|
|
* @param string $role
|
|
* @param Carbon $periodStart
|
|
* @param Carbon $periodEnd
|
|
* @param array $context
|
|
* @return Collection
|
|
*/
|
|
public static function filterForms(Collection $forms, string $role, Carbon $periodStart, Carbon $periodEnd, array $context = []): Collection
|
|
{
|
|
$regionId = $context['region_id'] ?? null;
|
|
$cabangId = $context['cabang_id'] ?? null;
|
|
|
|
return $forms->filter(function ($form) use ($role, $periodStart, $periodEnd, $regionId, $cabangId) {
|
|
$tanggal = Carbon::parse($form->tanggal);
|
|
if ($tanggal->lt($periodStart) || $tanggal->gt($periodEnd)) {
|
|
return false;
|
|
}
|
|
|
|
$status = (string) $form->status;
|
|
$normalizedStatus = strtolower($status);
|
|
|
|
if (in_array($normalizedStatus, ['rejected', 'closed'], true)) {
|
|
return false;
|
|
}
|
|
|
|
switch ($role) {
|
|
case 'Admin Region':
|
|
if ($status !== 'On Progress') {
|
|
return false;
|
|
}
|
|
if ($regionId && optional($form->user)->region_id !== $regionId) {
|
|
return false;
|
|
}
|
|
return true;
|
|
|
|
case 'Area Manager Cabang':
|
|
if ($status !== 'Approved 1') {
|
|
return false;
|
|
}
|
|
|
|
$formCabangId = optional($form->rayon)->cabang_id
|
|
?? optional(optional($form->user)->cabang)->cabang_id
|
|
?? optional(optional(optional($form->user)->cabang)->cabang)->id;
|
|
if ($cabangId !== null && (string) $formCabangId !== (string) $cabangId) {
|
|
return false;
|
|
}
|
|
return true;
|
|
|
|
case 'Marketing Information System':
|
|
return $status === 'Approved 1';
|
|
|
|
case 'Marketing Operational Manager Region':
|
|
if ($status !== 'Approved 2') {
|
|
return false;
|
|
}
|
|
if (!is_null($form->final_approved_at) || !is_null($form->final_approved_by)) {
|
|
return false;
|
|
}
|
|
if ($regionId && optional($form->user)->region_id !== $regionId) {
|
|
return false;
|
|
}
|
|
return true;
|
|
|
|
case 'Head of Sales Marketing':
|
|
if ($status !== 'Approved 2') {
|
|
return false;
|
|
}
|
|
return is_null($form->final_approved_at) || is_null($form->final_approved_by);
|
|
|
|
default:
|
|
if (!in_array($normalizedStatus, ['on progress', 'approved 1', 'approved 2'], true)) {
|
|
return false;
|
|
}
|
|
|
|
return is_null($form->final_approved_at) || is_null($form->final_approved_by);
|
|
}
|
|
});
|
|
}
|
|
}
|