update by CODEX

This commit is contained in:
Fiqh Pratama
2025-10-23 17:21:46 +07:00
parent 4e0ab80520
commit 6f9f0d34a3
12 changed files with 457 additions and 326 deletions
@@ -38,19 +38,26 @@ use Illuminate\Container\Attributes\Auth;
class FormUpCountryController extends Controller
{
protected AttachmentService $attachmentService;
/**
* Attachment categories (key => label) for Up Country forms.
*
* @var array<string,string>
*/
protected array $attachmentCategories = [
'bukti_allowance',
'bukti_transport_dalkot',
'bukti_transport_ankot',
'bukti_hotel',
'bukti_total',
'bukti_lainnya',
'bukti_allowance' => 'Bukti Allowance',
'bukti_transport_dalkot' => 'Bukti Transport Dalam Kota',
'bukti_transport_ankot' => 'Bukti Transport Antar Kota',
'bukti_hotel' => 'Bukti Hotel',
'bukti_total' => 'Bukti Total',
'bukti_lainnya' => 'Bukti Lainnya',
];
public function __construct(AttachmentService $attachmentService)
{
$this->attachmentService = $attachmentService;
view()->share('upCountryAttachmentCategories', $this->attachmentCategories);
view()->share('upCountryAttachmentCategoryKeys', array_keys($this->attachmentCategories));
}
public function index()
@@ -186,11 +193,12 @@ class FormUpCountryController extends Controller
public function create()
{
$this->checkAuthorization(auth()->user(), ['forms.country.create']);
$cabang = UserHasCabang::with('cabang')->where('user_id', auth()->user()->id)->first()->cabang;
$rayons = $this->getAccessibleRayonsForCurrentUser();
return view('backend.pages.forms.upcountry.create', [
'rayons' => Rayon::where('cabang_id', $cabang->id)->get(),
'attachmentCategories' => $this->attachmentCategories,
'rayons' => $rayons,
'attachmentCategories' => array_keys($this->attachmentCategories),
'attachmentCategoryLabels' => $this->attachmentCategories,
]);
}
@@ -207,7 +215,7 @@ class FormUpCountryController extends Controller
'transport_ankot' => 'nullable|numeric',
'hotel' => 'nullable|numeric',
'attachments' => 'nullable|array',
'attachments.*.file_category' => 'required|string|in:' . implode(',', $this->attachmentCategories),
'attachments.*.file_category' => 'required|string|in:' . implode(',', array_keys($this->attachmentCategories)),
'attachments.*.file_path' => ['nullable', 'file', 'mimes:jpg,jpeg,png,pdf', 'max:5120'],
'uc_plan' => 'nullable|file|max:51200',
]);
@@ -416,32 +424,19 @@ class FormUpCountryController extends Controller
$this->checkAuthorization(auth()->user(), ['forms.country.edit']);
$role = auth()->user()->getRoleNames()[0];
$cabang = UserHasCabang::with('cabang')->where('user_id', auth()->user()->id)->first()->cabang;
$regionId = Cabang::where('id', 1)->value('region_id');
$rayonData = Rayon::where('cabang_id', $cabang->id)->get();
if ($role == "Admin Region") {
$rayonData = DB::table('rayon')
->join('cabang', 'rayon.cabang_id', '=', 'cabang.id')
->join('region', 'cabang.region_id', '=', 'region.id')
->where('region.id', $regionId)
->select('rayon.*')
->get();
} elseif ($role == "Marketing Information System" || $role == "Superadmin") {
$rayonData = DB::table('rayon')
->join('cabang', 'rayon.cabang_id', '=', 'cabang.id')
->join('region', 'cabang.region_id', '=', 'region.id')
->whereNotNull('region.id')
->select('rayon.*')
->get();
}
$rayonData = $this->getAccessibleRayonsForCurrentUser();
$form = FormUpCountry::with(['rayon', 'attachments'])->findOrfail($id);
$form = FormUpCountry::with(['rayon.cabang', 'attachments'])->findOrfail($id);
if (($form->status != 'On Progress' && $form->status != 'Rejected') && $role == 'Medical Representatif') {
session()->flash('error', 'You cannot edit this form because it has been approved or closed.');
return redirect()->back();
}
if ($form->rayon && !$rayonData->contains('id', $form->rayon_id)) {
$rayonData->push($form->rayon);
$rayonData = $rayonData->sortBy('name')->values();
}
$existingAttachments = $form->attachments->map(function ($attachment) {
$filePath = $attachment->file_path;
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
@@ -462,10 +457,42 @@ class FormUpCountryController extends Controller
'rayons' => $rayonData,
'form' => $form,
'attachments' => $existingAttachments,
'attachmentCategories' => $this->attachmentCategories,
'attachmentCategories' => array_keys($this->attachmentCategories),
'attachmentCategoryLabels' => $this->attachmentCategories,
]);
}
protected function getAccessibleRayonsForCurrentUser()
{
$user = auth()->user();
$role = $user->getRoleNames()->first();
$rayonQuery = Rayon::with('cabang')->orderBy('name');
if (in_array($role, ['Superadmin', 'Marketing Information System'], true)) {
return $rayonQuery->get();
}
$userRegionData = $user->getMyCabangAndRegionId();
$regionContextId = $userRegionData['region'] !== '-' ? $userRegionData['region'] : null;
$cabangContextId = $userRegionData['cabang'] !== '-' ? $userRegionData['cabang'] : null;
if ($role === 'Admin Region' && $regionContextId) {
return $rayonQuery
->whereHas('cabang', function ($query) use ($regionContextId) {
$query->where('region_id', $regionContextId);
})
->get();
}
if ($cabangContextId) {
return $rayonQuery
->where('cabang_id', $cabangContextId)
->get();
}
return collect();
}
public function update(Request $request, $id)
{
$this->checkAuthorization(auth()->user(), ['forms.country.edit']);
@@ -481,7 +508,7 @@ class FormUpCountryController extends Controller
'transport_ankot' => 'nullable|numeric',
'hotel' => 'nullable|numeric',
'attachments' => 'nullable|array',
'attachments.*.file_category' => 'required|string|in:' . implode(',', $this->attachmentCategories),
'attachments.*.file_category' => 'required|string|in:' . implode(',', array_keys($this->attachmentCategories)),
'attachments.*.file_path' => ['nullable', 'file', 'mimes:jpg,jpeg,png,pdf', 'max:5120'],
'uc_plan' => 'nullable|file|max:51200',
]);