78 lines
2.7 KiB
PHP
78 lines
2.7 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 = $form->status;
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
if ($cabangId && optional($form->rayon)->cabang_id !== $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:
|
||
|
|
return is_null($form->final_approved_at) || is_null($form->final_approved_by);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|