2024-12-19 10:51:27 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Forms;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\FormMeetingSeminar;
|
|
|
|
|
use App\Models\Rayon;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use App\Models\Region;
|
|
|
|
|
use App\Models\Cabang;
|
|
|
|
|
use App\Models\UserHasCabang;
|
|
|
|
|
use App\Models\Kategori;
|
|
|
|
|
use App\Helpers\NextCloudHelper;
|
|
|
|
|
use Illuminate\Support\Str;
|
2024-12-24 15:12:24 +07:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use App\Mail\ExpenseApproved;
|
2025-01-16 15:56:22 +07:00
|
|
|
use App\Mail\ExpenseApproved2;
|
2024-12-24 15:12:24 +07:00
|
|
|
use App\Mail\ExpenseRejected;
|
2025-01-09 13:22:56 +07:00
|
|
|
use App\Mail\ExpenseCreated;
|
2024-12-26 13:49:00 +07:00
|
|
|
use App\Mail\FinalApprove;
|
2024-12-25 13:37:49 +07:00
|
|
|
use App\Helpers\WhatsappHelper;
|
2025-01-01 17:14:46 +07:00
|
|
|
use App\Helpers\AuditTrailHelper;
|
2025-01-09 13:22:56 +07:00
|
|
|
use App\Models\Admin;
|
2025-01-09 14:17:28 +07:00
|
|
|
use App\Helpers\BudgetHelper;
|
2025-01-17 16:17:53 +07:00
|
|
|
use App\Helpers\NotificationHelper;
|
2025-10-13 00:14:28 +07:00
|
|
|
use App\Helpers\OutstandingHelper;
|
2025-01-23 17:46:38 +07:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2025-03-05 13:44:58 +07:00
|
|
|
use App\Services\BrevoService;
|
2025-10-13 17:28:34 +07:00
|
|
|
use App\Services\AttachmentService;
|
|
|
|
|
use App\Enums\AttachmentTableName;
|
|
|
|
|
use App\Models\AttachmentForm;
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
use Illuminate\Validation\ValidationException;
|
2025-05-05 19:40:38 +07:00
|
|
|
use Carbon\Carbon;
|
2024-12-19 10:51:27 +07:00
|
|
|
|
|
|
|
|
class FormMeetingSeminarController extends Controller
|
|
|
|
|
{
|
2025-10-13 17:28:34 +07:00
|
|
|
protected AttachmentService $attachmentService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array<string,string>
|
|
|
|
|
*/
|
|
|
|
|
protected array $attachmentCategories = [
|
|
|
|
|
'bukti_allowance' => 'Bukti Allowance',
|
|
|
|
|
'bukti_transport_ankot' => 'Bukti Transport Antar Kota',
|
|
|
|
|
'bukti_hotel' => 'Bukti Hotel',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Blocked file extensions (case insensitive).
|
|
|
|
|
*
|
|
|
|
|
* @var array<int,string>
|
|
|
|
|
*/
|
|
|
|
|
protected array $blockedExtensions = ['exe', 'dll', 'sh', 'bat', 'cmd', 'msi'];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Image extensions that can be previewed inline.
|
|
|
|
|
*
|
|
|
|
|
* @var array<int,string>
|
|
|
|
|
*/
|
|
|
|
|
protected array $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
|
|
|
|
|
|
|
|
|
|
public function __construct(AttachmentService $attachmentService)
|
|
|
|
|
{
|
|
|
|
|
$this->attachmentService = $attachmentService;
|
|
|
|
|
view()->share('meetingAttachmentCategories', $this->attachmentCategories);
|
|
|
|
|
view()->share('meetingAttachmentCategoryKeys', array_keys($this->attachmentCategories));
|
|
|
|
|
view()->share('meetingBlockedExtensions', $this->blockedExtensions);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 10:51:27 +07:00
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.meeting.view']);
|
2025-05-25 15:14:23 +07:00
|
|
|
|
2024-12-23 10:10:09 +07:00
|
|
|
$role = auth()->user()->getRoleNames()[0];
|
2025-05-05 19:40:38 +07:00
|
|
|
$startDay = (int) env('STARTING_DATE', 11);
|
|
|
|
|
$closingDay = (int) env('CLOSING_DATE', 10);
|
2025-10-13 00:14:28 +07:00
|
|
|
$userRegionData = auth()->user()->getMyCabangAndRegionId();
|
|
|
|
|
$regionContextId = $userRegionData['region'] !== '-' ? $userRegionData['region'] : null;
|
|
|
|
|
$cabangContextId = $userRegionData['cabang'] !== '-' ? $userRegionData['cabang'] : null;
|
2025-05-05 19:40:38 +07:00
|
|
|
|
|
|
|
|
// Referensi waktu sekarang
|
|
|
|
|
$now = Carbon::now();
|
|
|
|
|
|
2025-06-19 19:39:20 +07:00
|
|
|
// Determine the period
|
2025-05-05 19:40:38 +07:00
|
|
|
if ($now->day >= $startDay) {
|
2025-06-19 19:39:20 +07:00
|
|
|
$currentPeriodStartDate = Carbon::create($now->year, $now->month, $startDay)->startOfDay();
|
|
|
|
|
$currentPeriodClosingDate = Carbon::create($now->copy()->addMonth()->year, $now->copy()->addMonth()->month, $closingDay)->endOfDay();
|
2025-05-05 19:40:38 +07:00
|
|
|
} else {
|
2025-06-19 19:39:20 +07:00
|
|
|
$currentPeriodStartDate = Carbon::create($now->copy()->subMonth()->year, $now->copy()->subMonth()->month, $startDay)->startOfDay();
|
|
|
|
|
$currentPeriodClosingDate = Carbon::create($now->year, $now->month, $closingDay)->endOfDay();
|
2025-05-05 19:40:38 +07:00
|
|
|
}
|
2025-01-18 15:01:28 +07:00
|
|
|
|
2025-06-19 19:39:20 +07:00
|
|
|
// Calculate the actual start date for data retrieval (1 month before currentPeriodStartDate)
|
2025-06-26 19:47:57 +07:00
|
|
|
$dataRetrievalStartDate = $currentPeriodStartDate->copy()->subMonth()->subDay()->startOfDay();
|
2025-05-25 15:14:23 +07:00
|
|
|
|
2025-06-19 19:39:20 +07:00
|
|
|
$forms = FormMeetingSeminar::whereBetween('tanggal', [$dataRetrievalStartDate, $currentPeriodClosingDate])
|
2025-03-04 15:20:23 +07:00
|
|
|
->orderBy('tanggal', 'desc')
|
2025-01-15 22:29:07 +07:00
|
|
|
->with('user')
|
2025-01-12 13:34:35 +07:00
|
|
|
->get();
|
|
|
|
|
|
2025-01-11 13:31:44 +07:00
|
|
|
$availableBudget = null;
|
2025-06-19 19:39:20 +07:00
|
|
|
$periodeMonth = strtolower($currentPeriodStartDate->format('F'));
|
|
|
|
|
$periodeYear = (int) $currentPeriodStartDate->format('Y');
|
2024-12-26 18:37:34 +07:00
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
if ($role == 'Admin Region' || $role == 'Marketing Operational Manager Region') {
|
2025-10-13 00:14:28 +07:00
|
|
|
if ($regionContextId) {
|
|
|
|
|
$users = UserHasCabang::whereHas('cabang', function ($query) use ($regionContextId) {
|
|
|
|
|
$query->where('region_id', $regionContextId);
|
2025-05-25 15:14:23 +07:00
|
|
|
})->get();
|
2024-12-26 18:37:34 +07:00
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
$userIds = $users->pluck('user_id')->toArray();
|
|
|
|
|
$forms = $forms->whereIn('user_id', $userIds);
|
|
|
|
|
}
|
|
|
|
|
} elseif ($role == 'Area Manager Cabang') {
|
2025-10-13 00:14:28 +07:00
|
|
|
if ($cabangContextId) {
|
|
|
|
|
$users = UserHasCabang::where('cabang_id', $cabangContextId)->get();
|
2025-01-01 15:33:16 +07:00
|
|
|
$userIds = $users->pluck('user_id')->toArray();
|
|
|
|
|
$forms = $forms->whereIn('user_id', $userIds);
|
2025-05-25 15:14:23 +07:00
|
|
|
|
2025-10-13 00:14:28 +07:00
|
|
|
$availableBudget = BudgetHelper::getAvailableBudget($cabangContextId, $periodeMonth, $periodeYear);
|
2025-01-01 15:33:16 +07:00
|
|
|
}
|
2025-05-25 15:14:23 +07:00
|
|
|
} elseif ($role == 'Medical Representatif') {
|
2024-12-26 18:37:34 +07:00
|
|
|
$forms = $forms->where('user_id', auth()->user()->id);
|
2025-05-25 15:14:23 +07:00
|
|
|
$cabang_id = UserHasCabang::where('user_id', auth()->user()->id)->first()?->cabang_id;
|
|
|
|
|
|
|
|
|
|
if ($cabang_id) {
|
|
|
|
|
$availableBudget = BudgetHelper::getAvailableBudget($cabang_id, $periodeMonth, $periodeYear);
|
|
|
|
|
}
|
2024-12-23 10:10:09 +07:00
|
|
|
}
|
2024-12-19 10:51:27 +07:00
|
|
|
|
2025-10-13 00:14:28 +07:00
|
|
|
$outstandingForms = OutstandingHelper::filterForms(
|
|
|
|
|
$forms,
|
|
|
|
|
$role,
|
|
|
|
|
$currentPeriodStartDate,
|
|
|
|
|
$currentPeriodClosingDate,
|
|
|
|
|
[
|
|
|
|
|
'region_id' => $regionContextId,
|
|
|
|
|
'cabang_id' => $cabangContextId,
|
|
|
|
|
]
|
|
|
|
|
);
|
2025-10-12 20:47:43 +07:00
|
|
|
|
2024-12-26 13:28:06 +07:00
|
|
|
session()->put('redirect_url', route('forms.meeting'));
|
2025-05-25 15:14:23 +07:00
|
|
|
|
|
|
|
|
return view('backend.pages.forms.meeting.index', [
|
|
|
|
|
'forms' => $forms,
|
2025-10-12 20:47:43 +07:00
|
|
|
'availableBudget' => $availableBudget,
|
|
|
|
|
'outstandingCount' => $outstandingForms->count(),
|
|
|
|
|
'outstandingTotal' => $outstandingForms->sum('total'),
|
2025-10-13 00:14:28 +07:00
|
|
|
'outstandingIds' => $outstandingForms->pluck('id')->toArray(),
|
2025-05-25 15:14:23 +07:00
|
|
|
]);
|
|
|
|
|
}
|
2024-12-19 10:51:27 +07:00
|
|
|
|
2025-01-04 13:16:00 +07:00
|
|
|
public function detail($id)
|
|
|
|
|
{
|
2025-01-16 15:56:22 +07:00
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.meeting.view']);
|
2025-01-04 13:16:00 +07:00
|
|
|
|
2025-10-13 17:28:34 +07:00
|
|
|
$form = FormMeetingSeminar::with(['user', 'attachments'])->findOrFail($id);
|
|
|
|
|
$attachments = $this->formatAttachmentCollection($form);
|
|
|
|
|
|
|
|
|
|
$form->bukti_allowance = $this->getAttachmentDownloadUrlByCategory($attachments, 'bukti_allowance');
|
|
|
|
|
$form->bukti_transport_ankot = $this->getAttachmentDownloadUrlByCategory($attachments, 'bukti_transport_ankot');
|
|
|
|
|
$form->bukti_hotel = $this->getAttachmentDownloadUrlByCategory($attachments, 'bukti_hotel');
|
|
|
|
|
$form->setAttribute('formatted_attachments', $attachments);
|
2025-01-04 13:16:00 +07:00
|
|
|
|
|
|
|
|
return response()->json($form);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-04 13:53:53 +07:00
|
|
|
public function view($id)
|
|
|
|
|
{
|
2025-01-16 15:56:22 +07:00
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.meeting.view']);
|
2025-01-04 13:53:53 +07:00
|
|
|
|
2025-10-13 17:28:34 +07:00
|
|
|
$form = FormMeetingSeminar::with(['user', 'attachments'])->findOrFail($id);
|
|
|
|
|
$attachments = $this->formatAttachmentCollection($form);
|
2025-01-04 13:53:53 +07:00
|
|
|
|
|
|
|
|
return view('backend.pages.forms.meeting.view', [
|
|
|
|
|
'form' => $form,
|
2025-10-13 17:28:34 +07:00
|
|
|
'attachments' => $attachments,
|
2025-01-04 13:53:53 +07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 10:51:27 +07:00
|
|
|
public function create()
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.meeting.create']);
|
|
|
|
|
|
|
|
|
|
return view('backend.pages.forms.meeting.create');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request)
|
2025-05-25 15:14:23 +07:00
|
|
|
{
|
2024-12-19 10:51:27 +07:00
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.meeting.create']);
|
|
|
|
|
|
2025-10-13 17:28:34 +07:00
|
|
|
$validator = Validator::make($request->all(), [
|
2025-03-04 15:20:23 +07:00
|
|
|
'tanggal' => 'required|date',
|
2025-10-13 17:28:34 +07:00
|
|
|
'allowance' => 'nullable|numeric|min:0',
|
|
|
|
|
'transport_ankot' => 'nullable|numeric|min:0',
|
|
|
|
|
'hotel' => 'nullable|numeric|min:0',
|
|
|
|
|
'attachments' => 'nullable|array',
|
|
|
|
|
'attachments.*.file_category' => 'nullable|string|in:' . implode(',', array_keys($this->attachmentCategories)),
|
|
|
|
|
'attachments.*.file_path' => 'nullable|file', // 10 MB
|
2024-12-19 10:51:27 +07:00
|
|
|
]);
|
2025-10-13 17:28:34 +07:00
|
|
|
$this->addAttachmentValidationRules($validator, $request);
|
|
|
|
|
$validator->validate();
|
2024-12-19 10:51:27 +07:00
|
|
|
|
2025-05-05 19:40:38 +07:00
|
|
|
$tanggal = Carbon::parse($request->tanggal);
|
|
|
|
|
$startDay = (int) env('STARTING_DATE', 11);
|
|
|
|
|
$closingDay = (int) env('CLOSING_DATE', 10);
|
|
|
|
|
$now = Carbon::now();
|
|
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
// Tentukan periode aktif berdasarkan tanggal saat ini
|
2025-05-05 19:40:38 +07:00
|
|
|
if ($now->day >= $startDay) {
|
|
|
|
|
$startDate = Carbon::create($now->year, $now->month, $startDay);
|
|
|
|
|
$closingDate = Carbon::create($now->copy()->addMonth()->year, $now->copy()->addMonth()->month, $closingDay);
|
|
|
|
|
} else {
|
|
|
|
|
$startDate = Carbon::create($now->copy()->subMonth()->year, $now->copy()->subMonth()->month, $startDay);
|
|
|
|
|
$closingDate = Carbon::create($now->year, $now->month, $closingDay);
|
|
|
|
|
}
|
2025-01-18 15:01:28 +07:00
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
// Validasi apakah tanggal pengajuan masuk ke periode saat ini
|
2025-05-05 19:40:38 +07:00
|
|
|
if ($tanggal->lt($startDate) || $tanggal->gt($closingDate)) {
|
|
|
|
|
session()->flash('error', "Gagal, tanggal tidak berada dalam periode input: {$startDate->format('d M')} - {$closingDate->format('d M')}");
|
2025-01-19 14:57:01 +07:00
|
|
|
return redirect()->back();
|
2025-01-18 15:01:28 +07:00
|
|
|
}
|
|
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
$cabang = UserHasCabang::with('cabang')->where('user_id', auth()->user()->id)->first()?->cabang;
|
|
|
|
|
if (!$cabang) {
|
|
|
|
|
session()->flash('error', 'Data cabang tidak ditemukan.');
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$region = Region::find($cabang->region_id);
|
2025-01-16 15:56:22 +07:00
|
|
|
$kategori = Kategori::where('name', 'Meeting / Seminar')->first();
|
2024-12-19 10:51:27 +07:00
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
// Hitung periode dari tanggal input (bukan dari today)
|
|
|
|
|
if ($tanggal->day >= $startDay) {
|
|
|
|
|
$periodeDate = Carbon::create($tanggal->year, $tanggal->month, $startDay);
|
|
|
|
|
} else {
|
|
|
|
|
$periodeDate = Carbon::create($tanggal->copy()->subMonth()->year, $tanggal->copy()->subMonth()->month, $startDay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$periodeMonth = strtolower($periodeDate->format('F'));
|
|
|
|
|
$periodeYear = (int) $periodeDate->format('Y');
|
|
|
|
|
|
2024-12-19 10:51:27 +07:00
|
|
|
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/meeting';
|
|
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
// Generate nomor expense
|
|
|
|
|
$sequence_number = FormMeetingSeminar::withTrashed()
|
|
|
|
|
->where('expense_number', 'like', $region->code . '-' . $cabang->code . '-MTG-' . date('ym') . '%')
|
|
|
|
|
->count() + 1;
|
|
|
|
|
|
2024-12-19 10:51:27 +07:00
|
|
|
$formatted_sequence_number = str_pad($sequence_number, 4, '0', STR_PAD_LEFT);
|
|
|
|
|
$expense_number = $region->code . '-' . $cabang->code . '-MTG-' . date('ym') . $formatted_sequence_number;
|
|
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
// Hitung nominal
|
2025-01-04 13:16:00 +07:00
|
|
|
$data_nominal = [
|
2025-10-13 17:28:34 +07:00
|
|
|
'allowance' => (float) ($request->allowance ?? 0),
|
|
|
|
|
'transport_ankot' => (float) ($request->transport_ankot ?? 0),
|
|
|
|
|
'hotel' => (float) ($request->hotel ?? 0),
|
2025-01-04 13:16:00 +07:00
|
|
|
];
|
2025-10-13 17:28:34 +07:00
|
|
|
$totalNominal = array_sum($data_nominal);
|
2025-01-04 13:16:00 +07:00
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
// Validasi budget
|
|
|
|
|
$availableBudget = BudgetHelper::getAvailableBudget($cabang->id, $periodeMonth, $periodeYear);
|
|
|
|
|
|
2025-01-09 14:17:28 +07:00
|
|
|
// Check if the total nominal exceeds the available budget
|
2025-10-13 17:28:34 +07:00
|
|
|
if ($totalNominal > $availableBudget) {
|
2025-01-19 14:57:01 +07:00
|
|
|
session()->flash('error', 'Alokasi budget bulanan cabang tidak mencukupi,silahkan ajukan pada periode expense berikutnya');
|
2025-01-09 14:17:28 +07:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 17:28:34 +07:00
|
|
|
DB::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
$attachmentsPayload = $this->buildAttachmentPayload($request, $folderPath);
|
|
|
|
|
|
|
|
|
|
$form = FormMeetingSeminar::create([
|
|
|
|
|
'expense_number' => $expense_number,
|
|
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
|
'tanggal' => $request->tanggal,
|
|
|
|
|
'allowance' => $data_nominal['allowance'],
|
|
|
|
|
'transport_ankot' => $data_nominal['transport_ankot'],
|
|
|
|
|
'hotel' => $data_nominal['hotel'],
|
|
|
|
|
'total' => $totalNominal,
|
|
|
|
|
'bukti_allowance' => null,
|
|
|
|
|
'bukti_transport_ankot' => null,
|
|
|
|
|
'bukti_hotel' => null,
|
|
|
|
|
'account_number' => $kategori?->account_number,
|
|
|
|
|
'status' => 'On Progress',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!empty($attachmentsPayload)) {
|
|
|
|
|
$this->attachmentService->addMultipleAttachments(
|
|
|
|
|
$form->id,
|
|
|
|
|
AttachmentTableName::FORM_MEETING_SEMINAR,
|
|
|
|
|
$attachmentsPayload
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
|
} catch (ValidationException $exception) {
|
|
|
|
|
DB::rollBack();
|
|
|
|
|
throw $exception;
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
DB::rollBack();
|
|
|
|
|
|
|
|
|
|
Log::error('Failed to create Meeting Seminar form', [
|
|
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
|
'message' => $th->getMessage(),
|
|
|
|
|
'trace' => $th->getTraceAsString(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
session()->flash('error', 'Terjadi kesalahan saat menyimpan data, silakan coba lagi.');
|
|
|
|
|
return redirect()->back()->withInput();
|
|
|
|
|
}
|
2024-12-19 10:51:27 +07:00
|
|
|
|
2025-01-09 13:22:56 +07:00
|
|
|
$name = auth()->user()->name;
|
2025-03-04 15:20:23 +07:00
|
|
|
$tanggal = $form->tanggal;
|
2025-01-09 13:22:56 +07:00
|
|
|
$total = $form->total;
|
|
|
|
|
|
2025-01-16 16:42:06 +07:00
|
|
|
// Send notification to MIS and Admin Region and Area Manager Cabang
|
2025-03-03 20:13:30 +07:00
|
|
|
$roles = ['Marketing Information System', 'Admin Region', 'Area Manager Cabang'];
|
2025-01-16 16:42:06 +07:00
|
|
|
$recipients = [$form->user->email, auth()->user()->email];
|
|
|
|
|
$phoneNumbers = [$form->user->phone, auth()->user()->phone];
|
2025-01-17 16:17:53 +07:00
|
|
|
$receiver_ids = [];
|
2025-01-16 16:42:06 +07:00
|
|
|
|
2025-01-09 13:22:56 +07:00
|
|
|
// Collect all recipients (emails and phone numbers) for the specified roles
|
2025-03-04 12:24:36 +07:00
|
|
|
$cabang = UserHasCabang::where('user_id', $form->user_id)->first();
|
|
|
|
|
$cabang_id = $cabang ? $cabang->cabang_id : null;
|
|
|
|
|
|
|
|
|
|
$cabangData = $cabang_id ? Cabang::where('id', $cabang_id)->first() : null;
|
|
|
|
|
$region_id = $cabangData ? $cabangData->region->id : null;
|
|
|
|
|
|
2025-01-09 13:22:56 +07:00
|
|
|
foreach ($roles as $role) {
|
2025-03-04 12:24:36 +07:00
|
|
|
$users = Admin::getUserByRoleName($role)->filter(function ($user) use ($role, $cabang_id, $region_id) {
|
2025-01-16 16:42:06 +07:00
|
|
|
if ($role === 'Marketing Information System') {
|
2025-03-04 12:24:36 +07:00
|
|
|
return true; // Tidak ada filter cabang untuk peran ini
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$user->cabang || !$user->cabang->cabang) {
|
|
|
|
|
return false; // Pastikan user memiliki cabang
|
2025-01-16 16:42:06 +07:00
|
|
|
}
|
|
|
|
|
|
2025-03-04 12:24:36 +07:00
|
|
|
if ($role === 'Admin Region' || $role === 'Marketing Operational Manager Region') {
|
|
|
|
|
return $user->cabang->cabang->region->id === $region_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $user->cabang->cabang->id === $cabang_id;
|
2025-01-16 16:42:06 +07:00
|
|
|
});
|
|
|
|
|
|
2025-01-09 13:22:56 +07:00
|
|
|
foreach ($users as $user) {
|
|
|
|
|
$recipients[] = $user->email;
|
|
|
|
|
$phoneNumbers[] = $user->phone;
|
2025-01-17 16:17:53 +07:00
|
|
|
$receiver_ids[] = $user->id;
|
2025-01-09 13:22:56 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove duplicate email addresses, if any
|
|
|
|
|
$recipients = array_unique($recipients);
|
2025-01-17 16:17:53 +07:00
|
|
|
$phoneNumbers = array_unique($phoneNumbers);
|
|
|
|
|
$receiver_ids = array_unique($receiver_ids);
|
2025-01-09 13:22:56 +07:00
|
|
|
|
|
|
|
|
// Generate URL and send WhatsApp notification
|
|
|
|
|
$url = route('forms.meeting.view', $form->id);
|
2025-01-16 15:56:22 +07:00
|
|
|
WhatsappHelper::newExpense($phoneNumbers, $expense_number, $url, $tanggal, $total, $name);
|
2025-01-09 13:22:56 +07:00
|
|
|
|
|
|
|
|
// Send bulk email
|
2025-03-05 13:44:58 +07:00
|
|
|
$brevoService = app(BrevoService::class);
|
2025-01-23 17:46:38 +07:00
|
|
|
foreach ($recipients as $recipient) {
|
|
|
|
|
try {
|
2025-03-05 13:44:58 +07:00
|
|
|
$mail = new ExpenseCreated(
|
2025-01-23 17:46:38 +07:00
|
|
|
$name,
|
|
|
|
|
$expense_number,
|
|
|
|
|
$tanggal,
|
|
|
|
|
$total,
|
|
|
|
|
$url
|
2025-03-05 13:44:58 +07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$response = $brevoService->expenseCreated($recipient, $name, $mail);
|
|
|
|
|
|
|
|
|
|
if (isset($response['success']) && $response['success'] === false) {
|
|
|
|
|
Log::warning("Email to {$recipient} failed: " . ($response['message'] ?? 'Unknown error'));
|
|
|
|
|
}
|
2025-01-23 17:46:38 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error("Failed to send email to {$recipient}: " . $e->getMessage());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-09 13:22:56 +07:00
|
|
|
|
2025-01-23 17:46:38 +07:00
|
|
|
NotificationHelper::newExpense('meeting-seminar', $form->id, $form->user_id, $expense_number, $receiver_ids);
|
2025-01-17 16:17:53 +07:00
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Insert', 'Insert New Expense at Form Meeting Seminar (' . $expense_number . ')');
|
2024-12-19 10:51:27 +07:00
|
|
|
session()->flash('success', 'Form has been created.');
|
2024-12-23 10:42:27 +07:00
|
|
|
return redirect()->back();
|
2024-12-19 10:51:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit($id)
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.meeting.edit']);
|
2025-01-07 17:23:42 +07:00
|
|
|
$role = auth()->user()->getRoleNames()[0];
|
2024-12-19 10:51:27 +07:00
|
|
|
|
2025-10-13 17:28:34 +07:00
|
|
|
$form = FormMeetingSeminar::with('attachments')->findOrFail($id);
|
2025-01-07 17:23:42 +07:00
|
|
|
if($form->status != 'On Progress' && $role == 'Medical Representatif') {
|
|
|
|
|
session()->flash('error', 'You cannot edit this form because it has been approved or rejected.');
|
2024-12-24 11:23:27 +07:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2025-10-13 17:28:34 +07:00
|
|
|
|
|
|
|
|
$attachments = $this->formatAttachmentCollection($form);
|
|
|
|
|
|
2024-12-19 10:51:27 +07:00
|
|
|
return view('backend.pages.forms.meeting.edit', [
|
2025-10-13 17:28:34 +07:00
|
|
|
'form' => $form,
|
|
|
|
|
'attachments' => $attachments,
|
2024-12-19 10:51:27 +07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.meeting.edit']);
|
2025-01-07 17:23:42 +07:00
|
|
|
$role = auth()->user()->getRoleNames()[0];
|
2025-10-13 17:28:34 +07:00
|
|
|
$form = FormMeetingSeminar::with('attachments')->findOrFail($id);
|
2024-12-19 10:51:27 +07:00
|
|
|
|
2025-10-13 17:28:34 +07:00
|
|
|
$validator = Validator::make($request->all(), [
|
2025-03-04 15:20:23 +07:00
|
|
|
'tanggal' => 'required|date',
|
2025-10-13 17:28:34 +07:00
|
|
|
'allowance' => 'nullable|numeric|min:0',
|
|
|
|
|
'transport_ankot' => 'nullable|numeric|min:0',
|
|
|
|
|
'hotel' => 'nullable|numeric|min:0',
|
|
|
|
|
'attachments' => 'nullable|array',
|
|
|
|
|
'attachments.*.file_category' => 'nullable|string|in:' . implode(',', array_keys($this->attachmentCategories)),
|
|
|
|
|
'attachments.*.file_path' => 'nullable|file', // 10 MB
|
2024-12-19 10:51:27 +07:00
|
|
|
]);
|
2025-10-13 17:28:34 +07:00
|
|
|
$this->addAttachmentValidationRules($validator, $request);
|
|
|
|
|
$validator->validate();
|
2024-12-19 10:51:27 +07:00
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
if ($form->status != 'On Progress' && $role == 'Medical Representatif') {
|
2025-01-07 17:23:42 +07:00
|
|
|
session()->flash('error', 'You cannot edit this form because it has been approved or rejected.');
|
2024-12-24 11:23:27 +07:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
$cabang = UserHasCabang::with('cabang')->where('user_id', $form->user_id)->first()?->cabang;
|
|
|
|
|
if (!$cabang) {
|
|
|
|
|
session()->flash('error', 'Cabang tidak ditemukan.');
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$region = Region::find($cabang->region_id);
|
2025-01-16 15:56:22 +07:00
|
|
|
$kategori = Kategori::where('name', 'Meeting / Seminar')->first();
|
2024-12-19 10:51:27 +07:00
|
|
|
|
|
|
|
|
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/meeting';
|
|
|
|
|
|
2025-10-13 17:28:34 +07:00
|
|
|
$attachmentsPayload = $this->buildAttachmentPayload($request, $folderPath);
|
2024-12-19 10:51:27 +07:00
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
// Hitung nominal total
|
2025-01-04 13:16:00 +07:00
|
|
|
$data_nominal = [
|
2025-10-13 17:28:34 +07:00
|
|
|
'allowance' => (float) ($request->allowance ?? 0),
|
|
|
|
|
'transport_ankot' => (float) ($request->transport_ankot ?? 0),
|
|
|
|
|
'hotel' => (float) ($request->hotel ?? 0),
|
2025-01-04 13:16:00 +07:00
|
|
|
];
|
|
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
$totalNominal = array_sum($data_nominal);
|
|
|
|
|
|
|
|
|
|
// Hitung periode dari tanggal form yang diinput
|
|
|
|
|
$tanggal = Carbon::parse($request->tanggal);
|
|
|
|
|
$startDay = (int) env('STARTING_DATE', 11);
|
|
|
|
|
if ($tanggal->day >= $startDay) {
|
|
|
|
|
$periodeDate = Carbon::create($tanggal->year, $tanggal->month, $startDay);
|
|
|
|
|
} else {
|
|
|
|
|
$periodeDate = Carbon::create($tanggal->copy()->subMonth()->year, $tanggal->copy()->subMonth()->month, $startDay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$periodeMonth = strtolower($periodeDate->format('F'));
|
|
|
|
|
$periodeYear = (int) $periodeDate->format('Y');
|
|
|
|
|
|
|
|
|
|
// Ambil budget tersedia
|
|
|
|
|
$availableBudget = BudgetHelper::getAvailableBudget($cabang->id, $periodeMonth, $periodeYear);
|
2025-10-13 17:28:34 +07:00
|
|
|
if ($totalNominal > $availableBudget) {
|
2025-01-09 14:17:28 +07:00
|
|
|
session()->flash('error', 'The total nominal exceeds the available budget.');
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 17:28:34 +07:00
|
|
|
DB::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
$form->update([
|
|
|
|
|
'tanggal' => $request->tanggal,
|
|
|
|
|
'allowance' => $data_nominal['allowance'],
|
|
|
|
|
'transport_ankot' => $data_nominal['transport_ankot'],
|
|
|
|
|
'hotel' => $data_nominal['hotel'],
|
|
|
|
|
'total' => $totalNominal,
|
|
|
|
|
'account_number' => $kategori?->account_number,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!empty($attachmentsPayload)) {
|
|
|
|
|
$this->attachmentService->addMultipleAttachments(
|
|
|
|
|
$form->id,
|
|
|
|
|
AttachmentTableName::FORM_MEETING_SEMINAR,
|
|
|
|
|
$attachmentsPayload
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
DB::rollBack();
|
|
|
|
|
|
|
|
|
|
Log::error('Failed to update Meeting Seminar form', [
|
|
|
|
|
'form_id' => $form->id,
|
|
|
|
|
'message' => $th->getMessage(),
|
|
|
|
|
'trace' => $th->getTraceAsString(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
session()->flash('error', 'Terjadi kesalahan saat memperbarui data, silakan coba lagi.');
|
|
|
|
|
return redirect()->back()->withInput();
|
|
|
|
|
}
|
2024-12-19 10:51:27 +07:00
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Update', 'Update Expense at Form Meeting Seminar (' . $form->expense_number . ')');
|
2024-12-19 10:51:27 +07:00
|
|
|
session()->flash('success', 'Form has been updated.');
|
2024-12-23 10:42:27 +07:00
|
|
|
return redirect()->back();
|
2024-12-19 10:51:27 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-13 17:28:34 +07:00
|
|
|
public function deleteAttachment(Request $request, $formId, $attachmentId)
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.meeting.edit']);
|
|
|
|
|
|
|
|
|
|
$form = FormMeetingSeminar::findOrFail($formId);
|
|
|
|
|
$role = auth()->user()->getRoleNames()[0];
|
|
|
|
|
|
|
|
|
|
if (($form->status !== 'On Progress' && $form->status !== 'Rejected') && $role === 'Medical Representatif') {
|
|
|
|
|
return response()->json(['message' => 'You cannot modify attachments once the form is approved or closed.'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attachment = AttachmentForm::where('id', $attachmentId)
|
|
|
|
|
->where('form_id', $form->id)
|
|
|
|
|
->where('table_name', AttachmentTableName::FORM_MEETING_SEMINAR)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if (!$attachment) {
|
|
|
|
|
return response()->json(['message' => 'Attachment not found.'], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->attachmentService->deleteAttachment($attachment->id);
|
|
|
|
|
|
|
|
|
|
AuditTrailHelper::AddAuditTrail('Delete Attachment', 'Delete Attachment at Form Meeting Seminar (' . $form->expense_number . ')');
|
|
|
|
|
|
|
|
|
|
return response()->json(['message' => 'Attachment deleted successfully.']);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 17:46:38 +07:00
|
|
|
public function approve(Request $request, $id)
|
2024-12-23 10:10:09 +07:00
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['approval.approve']);
|
|
|
|
|
|
|
|
|
|
$form = FormMeetingSeminar::findOrfail($id);
|
2025-01-23 17:46:38 +07:00
|
|
|
|
|
|
|
|
$approved_data = [
|
|
|
|
|
'allowance' => $request->allowance ? $form->allowance : 0,
|
|
|
|
|
'transport_ankot' => $request->transport_ankot ? $form->transport_ankot : 0,
|
|
|
|
|
'hotel' => $request->hotel ? $form->hotel : 0,
|
|
|
|
|
];
|
|
|
|
|
|
2024-12-23 10:10:09 +07:00
|
|
|
$form->update([
|
|
|
|
|
'approved_at' => now(),
|
|
|
|
|
'approved_by' => auth()->user()->id,
|
2025-01-16 15:56:22 +07:00
|
|
|
'status' => 'Approved 1',
|
2025-01-23 17:46:38 +07:00
|
|
|
'approved_allowance' => $approved_data['allowance'],
|
|
|
|
|
'approved_transport_ankot' => $approved_data['transport_ankot'],
|
|
|
|
|
'approved_hotel' => $approved_data['hotel'],
|
|
|
|
|
'approved_total' => array_sum($approved_data),
|
2024-12-23 10:10:09 +07:00
|
|
|
]);
|
|
|
|
|
|
2024-12-24 15:12:24 +07:00
|
|
|
$name = $form->user->name;
|
|
|
|
|
$expense_number = $form->expense_number;
|
2025-03-04 15:20:23 +07:00
|
|
|
$tanggal = $form->tanggal;
|
2025-01-16 15:56:22 +07:00
|
|
|
$total = $form->total;
|
2024-12-24 15:12:24 +07:00
|
|
|
|
|
|
|
|
// Collect all recipients
|
2025-03-03 20:13:30 +07:00
|
|
|
$roles = ['Marketing Information System', 'Admin Region', 'Area Manager Cabang', 'Marketing Operational Manager Region'];
|
2024-12-26 13:40:17 +07:00
|
|
|
$recipients = [$form->user->email, auth()->user()->email];
|
|
|
|
|
$phoneNumbers = [$form->user->phone, auth()->user()->phone];
|
2025-01-17 16:17:53 +07:00
|
|
|
$receiver_ids = [];
|
2024-12-24 15:12:24 +07:00
|
|
|
|
2025-03-04 12:24:36 +07:00
|
|
|
// Collect all recipients (emails and phone numbers) for the specified roles
|
|
|
|
|
$cabang = UserHasCabang::where('user_id', $form->user_id)->first();
|
|
|
|
|
$cabang_id = $cabang ? $cabang->cabang_id : null;
|
|
|
|
|
|
|
|
|
|
$cabangData = $cabang_id ? Cabang::where('id', $cabang_id)->first() : null;
|
|
|
|
|
$region_id = $cabangData ? $cabangData->region->id : null;
|
|
|
|
|
|
2025-01-16 16:42:06 +07:00
|
|
|
foreach ($roles as $role) {
|
2025-03-04 12:24:36 +07:00
|
|
|
$users = Admin::getUserByRoleName($role)->filter(function ($user) use ($role, $cabang_id, $region_id) {
|
2025-01-16 16:42:06 +07:00
|
|
|
if ($role === 'Marketing Information System') {
|
2025-03-04 12:24:36 +07:00
|
|
|
return true; // Tidak ada filter cabang untuk peran ini
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$user->cabang || !$user->cabang->cabang) {
|
|
|
|
|
return false; // Pastikan user memiliki cabang
|
2025-01-16 16:42:06 +07:00
|
|
|
}
|
|
|
|
|
|
2025-03-04 12:24:36 +07:00
|
|
|
if ($role === 'Admin Region' || $role === 'Marketing Operational Manager Region') {
|
|
|
|
|
return $user->cabang->cabang->region->id === $region_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $user->cabang->cabang->id === $cabang_id;
|
2025-01-16 16:42:06 +07:00
|
|
|
});
|
2024-12-24 15:12:24 +07:00
|
|
|
|
2025-01-16 16:42:06 +07:00
|
|
|
foreach ($users as $user) {
|
|
|
|
|
$recipients[] = $user->email;
|
|
|
|
|
$phoneNumbers[] = $user->phone;
|
2025-01-17 16:17:53 +07:00
|
|
|
$receiver_ids[] = $user->id;
|
2025-01-16 16:42:06 +07:00
|
|
|
}
|
2024-12-24 15:12:24 +07:00
|
|
|
}
|
|
|
|
|
|
2025-01-16 15:56:22 +07:00
|
|
|
// Remove duplicate data, if any
|
2024-12-24 15:12:24 +07:00
|
|
|
$recipients = array_unique($recipients);
|
2024-12-25 13:37:49 +07:00
|
|
|
$phoneNumbers = array_unique($phoneNumbers);
|
2025-01-17 16:17:53 +07:00
|
|
|
$receiver_ids = array_unique($receiver_ids);
|
2024-12-24 15:12:24 +07:00
|
|
|
|
2025-01-16 15:56:22 +07:00
|
|
|
// send whatsapp message
|
2025-01-04 14:09:28 +07:00
|
|
|
$url = route('forms.meeting.view', $form->id);
|
2025-01-16 15:56:22 +07:00
|
|
|
WhatsappHelper::approveExpense($phoneNumbers, $expense_number, $url, $tanggal, $total, $name);
|
2024-12-25 13:37:49 +07:00
|
|
|
|
2025-03-05 13:44:58 +07:00
|
|
|
// Send bulk email
|
|
|
|
|
$brevoService = app(BrevoService::class);
|
2025-01-23 17:46:38 +07:00
|
|
|
foreach ($recipients as $recipient) {
|
|
|
|
|
try {
|
2025-03-05 13:44:58 +07:00
|
|
|
$mail = new ExpenseApproved(
|
2025-01-23 17:46:38 +07:00
|
|
|
$name,
|
|
|
|
|
$expense_number,
|
|
|
|
|
$tanggal,
|
|
|
|
|
$total,
|
|
|
|
|
$url
|
2025-03-05 13:44:58 +07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$response = $brevoService->expenseApproved($recipient, $name, $mail);
|
|
|
|
|
|
|
|
|
|
if (isset($response['success']) && $response['success'] === false) {
|
|
|
|
|
Log::warning("Email to {$recipient} failed: " . ($response['message'] ?? 'Unknown error'));
|
|
|
|
|
}
|
2025-01-23 17:46:38 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error("Failed to send email to {$recipient}: " . $e->getMessage());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-09 13:22:56 +07:00
|
|
|
|
2025-01-23 17:46:38 +07:00
|
|
|
NotificationHelper::approveExpense('meeting-seminar', $form->id, $form->user_id, $expense_number, $receiver_ids);
|
2025-01-17 16:17:53 +07:00
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Approve', 'Approve Expense at Form Meeting Seminar (' . $form->expense_number . ')');
|
2024-12-23 10:10:09 +07:00
|
|
|
session()->flash('success', 'Form has been approved.');
|
2024-12-23 10:42:27 +07:00
|
|
|
return redirect()->back();
|
2024-12-23 10:10:09 +07:00
|
|
|
}
|
|
|
|
|
|
2025-01-16 15:56:22 +07:00
|
|
|
public function approve2($id)
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['approval2.approve']);
|
|
|
|
|
|
|
|
|
|
$form = FormMeetingSeminar::findOrfail($id);
|
|
|
|
|
$form->update([
|
|
|
|
|
'approved2_at' => now(),
|
|
|
|
|
'approved2_by' => auth()->user()->id,
|
|
|
|
|
'status' => 'Approved 2',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$name = $form->user->name;
|
|
|
|
|
$expense_number = $form->expense_number;
|
2025-03-04 15:20:23 +07:00
|
|
|
$tanggal = $form->tanggal;
|
2025-01-16 15:56:22 +07:00
|
|
|
$total = $form->total;
|
|
|
|
|
|
2025-03-03 20:13:30 +07:00
|
|
|
$roles = ['Marketing Information System', 'Area Manager Cabang', 'Marketing Operational Manager Region', 'Head of Sales Marketing'];
|
2025-01-16 15:56:22 +07:00
|
|
|
$recipients = [$form->user->email, auth()->user()->email];
|
|
|
|
|
$phoneNumbers = [$form->user->phone, auth()->user()->phone];
|
2025-01-17 16:17:53 +07:00
|
|
|
$receiver_ids = [];
|
2025-01-16 15:56:22 +07:00
|
|
|
|
2025-03-04 12:24:36 +07:00
|
|
|
// Collect all recipients (emails and phone numbers) for the specified roles
|
|
|
|
|
$cabang = UserHasCabang::where('user_id', $form->user_id)->first();
|
|
|
|
|
$cabang_id = $cabang ? $cabang->cabang_id : null;
|
|
|
|
|
|
|
|
|
|
$cabangData = $cabang_id ? Cabang::where('id', $cabang_id)->first() : null;
|
|
|
|
|
$region_id = $cabangData ? $cabangData->region->id : null;
|
2025-01-16 16:42:06 +07:00
|
|
|
|
|
|
|
|
foreach ($roles as $role) {
|
2025-03-04 12:24:36 +07:00
|
|
|
$users = Admin::getUserByRoleName($role)->filter(function ($user) use ($role, $cabang_id, $region_id) {
|
|
|
|
|
if ($role === 'Marketing Information System' || $role === 'Head of Sales Marketing') {
|
|
|
|
|
return true; // Tidak ada filter cabang untuk peran ini
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$user->cabang || !$user->cabang->cabang) {
|
|
|
|
|
return false; // Pastikan user memiliki cabang
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($role === 'Admin Region' || $role === 'Marketing Operational Manager Region') {
|
|
|
|
|
return $user->cabang->cabang->region->id === $region_id;
|
2025-01-16 16:42:06 +07:00
|
|
|
}
|
|
|
|
|
|
2025-03-04 12:24:36 +07:00
|
|
|
return $user->cabang->cabang->id === $cabang_id;
|
2025-01-16 16:42:06 +07:00
|
|
|
});
|
2025-01-16 15:56:22 +07:00
|
|
|
|
2025-01-16 16:42:06 +07:00
|
|
|
foreach ($users as $user) {
|
|
|
|
|
$recipients[] = $user->email;
|
|
|
|
|
$phoneNumbers[] = $user->phone;
|
2025-01-17 16:17:53 +07:00
|
|
|
$receiver_ids[] = $user->id;
|
2025-01-16 16:42:06 +07:00
|
|
|
}
|
2025-01-16 15:56:22 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove duplicate data, if any
|
|
|
|
|
$recipients = array_unique($recipients);
|
|
|
|
|
$phoneNumbers = array_unique($phoneNumbers);
|
2025-01-17 16:17:53 +07:00
|
|
|
$receiver_ids = array_unique($receiver_ids);
|
2025-01-16 15:56:22 +07:00
|
|
|
|
|
|
|
|
// send whatsapp message
|
|
|
|
|
$url = route('forms.meeting.view', $form->id);
|
|
|
|
|
WhatsappHelper::approve2Expense($phoneNumbers, $expense_number, $url, $tanggal, $total, $name);
|
|
|
|
|
|
2025-03-05 13:44:58 +07:00
|
|
|
// Send bulk email
|
|
|
|
|
$brevoService = app(BrevoService::class);
|
2025-01-23 17:46:38 +07:00
|
|
|
foreach ($recipients as $recipient) {
|
|
|
|
|
try {
|
2025-03-05 13:44:58 +07:00
|
|
|
$mail = new ExpenseApproved2(
|
2025-01-23 17:46:38 +07:00
|
|
|
$name,
|
|
|
|
|
$expense_number,
|
|
|
|
|
$tanggal,
|
|
|
|
|
$total,
|
|
|
|
|
$url
|
2025-03-05 13:44:58 +07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$response = $brevoService->expenseApproved2($recipient, $name, $mail);
|
|
|
|
|
|
|
|
|
|
if (isset($response['success']) && $response['success'] === false) {
|
|
|
|
|
Log::warning("Email to {$recipient} failed: " . ($response['message'] ?? 'Unknown error'));
|
|
|
|
|
}
|
2025-01-23 17:46:38 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error("Failed to send email to {$recipient}: " . $e->getMessage());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-16 15:56:22 +07:00
|
|
|
|
2025-01-23 17:46:38 +07:00
|
|
|
NotificationHelper::approve2Expense('meeting-seminar', $form->id, $form->user_id, $expense_number, $receiver_ids);
|
2025-01-17 16:17:53 +07:00
|
|
|
|
2025-01-16 15:56:22 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Approve', 'Approve Expense at Form Meeting Seminar (' . $form->expense_number . ')');
|
|
|
|
|
session()->flash('success', 'Form has been approved.');
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 17:46:38 +07:00
|
|
|
public function finalApprove($id)
|
2024-12-23 10:10:09 +07:00
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['final_approval.approve']);
|
2025-05-25 15:14:23 +07:00
|
|
|
$form = FormMeetingSeminar::findOrFail($id);
|
2024-12-23 10:10:09 +07:00
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
if ($form->approved_at === null || $form->approved2_at === null) {
|
2025-01-16 15:56:22 +07:00
|
|
|
session()->flash('error', 'Form must be approved by both approvers before final approval.');
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-25 15:14:23 +07:00
|
|
|
$cabang_id = UserHasCabang::where('user_id', $form->user_id)->first()?->cabang_id;
|
|
|
|
|
if (!$cabang_id) {
|
|
|
|
|
session()->flash('error', 'Data cabang tidak ditemukan.');
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hitung periode dari tanggal form
|
|
|
|
|
$tanggal = Carbon::parse($form->tanggal);
|
|
|
|
|
$startDay = (int) env('STARTING_DATE', 11);
|
|
|
|
|
|
|
|
|
|
if ($tanggal->day >= $startDay) {
|
|
|
|
|
$periodeDate = Carbon::create($tanggal->year, $tanggal->month, $startDay);
|
|
|
|
|
} else {
|
|
|
|
|
$periodeDate = Carbon::create($tanggal->copy()->subMonth()->year, $tanggal->copy()->subMonth()->month, $startDay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$periodeMonth = strtolower($periodeDate->format('F'));
|
|
|
|
|
$periodeYear = (int) $periodeDate->format('Y');
|
|
|
|
|
|
|
|
|
|
$availableBudget = BudgetHelper::getAvailableBudget($cabang_id, $periodeMonth, $periodeYear);
|
|
|
|
|
if($form->approved_total > $availableBudget) {
|
2025-01-23 17:46:38 +07:00
|
|
|
session()->flash('error', 'Budget cabang untuk periode ini tidak mencukupi, silahkan ajukan pada periode expense berikutnya');
|
2024-12-23 10:42:27 +07:00
|
|
|
return redirect()->back();
|
2024-12-23 10:10:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$form->update([
|
|
|
|
|
'final_approved_at' => now(),
|
|
|
|
|
'final_approved_by' => auth()->user()->id,
|
2025-01-16 15:56:22 +07:00
|
|
|
'status' => 'Closed',
|
2024-12-23 10:10:09 +07:00
|
|
|
]);
|
|
|
|
|
|
2024-12-26 13:49:00 +07:00
|
|
|
$name = $form->user->name;
|
|
|
|
|
$expense_number = $form->expense_number;
|
2025-03-04 15:20:23 +07:00
|
|
|
$tanggal = $form->tanggal;
|
2025-01-09 13:22:56 +07:00
|
|
|
$total = $form->approved_total;
|
2024-12-26 13:49:00 +07:00
|
|
|
|
2025-01-16 16:42:06 +07:00
|
|
|
$roles = ['Head of Sales Marketing', 'Marketing Operational Manager Region'];
|
2024-12-26 13:49:00 +07:00
|
|
|
$recipients = [$form->user->email, auth()->user()->email];
|
|
|
|
|
$phoneNumbers = [$form->user->phone, auth()->user()->phone];
|
|
|
|
|
|
2025-03-04 12:24:36 +07:00
|
|
|
$cabang = UserHasCabang::where('user_id', $form->user_id)->first();
|
|
|
|
|
$cabang_id = $cabang ? $cabang->cabang_id : null;
|
|
|
|
|
|
|
|
|
|
$cabangData = $cabang_id ? Cabang::where('id', $cabang_id)->first() : null;
|
|
|
|
|
$region_id = $cabangData ? $cabangData->region->id : null;
|
|
|
|
|
|
2025-01-16 16:42:06 +07:00
|
|
|
foreach ($roles as $role) {
|
2025-03-04 12:24:36 +07:00
|
|
|
$users = Admin::getUserByRoleName($role)->filter(function ($user) use ($role, $cabang_id, $region_id) {
|
|
|
|
|
if ($role === 'Marketing Information System' || $role === 'Head of Sales Marketing') {
|
|
|
|
|
return true; // Tidak ada filter cabang untuk peran ini
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$user->cabang || !$user->cabang->cabang) {
|
|
|
|
|
return false; // Pastikan user memiliki cabang
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($role === 'Admin Region' || $role === 'Marketing Operational Manager Region') {
|
|
|
|
|
return $user->cabang->cabang->region->id === $region_id;
|
2025-01-16 16:42:06 +07:00
|
|
|
}
|
|
|
|
|
|
2025-03-04 12:24:36 +07:00
|
|
|
return $user->cabang->cabang->id === $cabang_id;
|
2025-01-16 16:42:06 +07:00
|
|
|
});
|
2024-12-26 13:49:00 +07:00
|
|
|
|
2025-01-16 16:42:06 +07:00
|
|
|
foreach ($users as $user) {
|
|
|
|
|
$recipients[] = $user->email;
|
|
|
|
|
$phoneNumbers[] = $user->phone;
|
|
|
|
|
}
|
2024-12-26 13:49:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove duplicate email addresses, if any
|
|
|
|
|
$recipients = array_unique($recipients);
|
|
|
|
|
$phoneNumbers = array_unique($phoneNumbers);
|
|
|
|
|
|
|
|
|
|
// send whatsapp message
|
2025-01-04 14:09:28 +07:00
|
|
|
$url = route('forms.meeting.view', $form->id);
|
2025-01-16 15:56:22 +07:00
|
|
|
WhatsappHelper::finalApprove($phoneNumbers, $expense_number, $url, $tanggal, $total, $name);
|
2024-12-26 13:49:00 +07:00
|
|
|
|
2025-01-09 13:22:56 +07:00
|
|
|
// Send bulk email
|
2025-01-23 17:46:38 +07:00
|
|
|
foreach ($recipients as $recipient) {
|
|
|
|
|
try {
|
|
|
|
|
Mail::to($recipient)->send(new FinalApprove(
|
|
|
|
|
$name,
|
|
|
|
|
$expense_number,
|
|
|
|
|
$tanggal,
|
|
|
|
|
$total,
|
|
|
|
|
$url
|
|
|
|
|
));
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error("Failed to send email to {$recipient}: " . $e->getMessage());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-09 13:22:56 +07:00
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Final Approve', 'Final Approve Expense at Form Meeting Seminar (' . $form->expense_number . ')');
|
2024-12-23 10:10:09 +07:00
|
|
|
session()->flash('success', 'Form has been final approved.');
|
2024-12-23 10:42:27 +07:00
|
|
|
return redirect()->back();
|
2024-12-23 10:10:09 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-12 20:47:43 +07:00
|
|
|
public function reject(Request $request, $id)
|
2024-12-23 10:10:09 +07:00
|
|
|
{
|
2025-01-16 15:56:22 +07:00
|
|
|
$this->checkAuthorization(auth()->user(), ['approval.reject']);
|
2024-12-23 10:10:09 +07:00
|
|
|
|
2025-10-12 20:47:43 +07:00
|
|
|
$request->validate([
|
|
|
|
|
'remarks' => 'required|string|max:500',
|
|
|
|
|
]);
|
|
|
|
|
|
2024-12-23 10:10:09 +07:00
|
|
|
$form = FormMeetingSeminar::findOrfail($id);
|
|
|
|
|
$form->update([
|
2025-10-12 20:47:43 +07:00
|
|
|
'status' => 'Rejected',
|
|
|
|
|
'remarks' => $request->remarks,
|
2024-12-23 10:10:09 +07:00
|
|
|
]);
|
|
|
|
|
|
2024-12-24 15:12:24 +07:00
|
|
|
$heads = $form->user->getCabangAndRegionHead();
|
|
|
|
|
$name = $form->user->name;
|
|
|
|
|
$expense_number = $form->expense_number;
|
2025-03-04 15:20:23 +07:00
|
|
|
$tanggal = $form->tanggal;
|
2024-12-24 15:12:24 +07:00
|
|
|
$total = $form->total;
|
|
|
|
|
|
|
|
|
|
// Collect all recipients
|
2024-12-26 13:40:17 +07:00
|
|
|
$recipients = [$form->user->email, auth()->user()->email];
|
|
|
|
|
$phoneNumbers = [$form->user->phone, auth()->user()->phone];
|
2024-12-24 15:12:24 +07:00
|
|
|
|
|
|
|
|
if ($heads['cabang_head']) {
|
|
|
|
|
$recipients[] = $heads['cabang_head']['email'];
|
2024-12-25 13:37:49 +07:00
|
|
|
$phoneNumbers[] = $heads['cabang_head']['phone'];
|
2024-12-24 15:12:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($heads['region_head']) {
|
|
|
|
|
$recipients[] = $heads['region_head']['email'];
|
2024-12-25 13:37:49 +07:00
|
|
|
$phoneNumbers[] = $heads['region_head']['phone'];
|
2024-12-24 15:12:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove duplicate email addresses, if any
|
|
|
|
|
$recipients = array_unique($recipients);
|
2024-12-25 13:37:49 +07:00
|
|
|
$phoneNumbers = array_unique($phoneNumbers);
|
2024-12-24 15:12:24 +07:00
|
|
|
|
2025-01-16 15:56:22 +07:00
|
|
|
// send whatsapp message
|
2025-01-04 14:09:28 +07:00
|
|
|
$url = route('forms.meeting.view', $form->id);
|
2025-10-26 14:14:06 +07:00
|
|
|
WhatsappHelper::rejectExpense($phoneNumbers, $expense_number, $url, $tanggal, $total, $name, $request->remarks);
|
2024-12-25 13:37:49 +07:00
|
|
|
|
2025-01-09 13:22:56 +07:00
|
|
|
// Send bulk email
|
2025-03-05 13:44:58 +07:00
|
|
|
$brevoService = app(BrevoService::class);
|
2025-01-23 17:46:38 +07:00
|
|
|
foreach ($recipients as $recipient) {
|
|
|
|
|
try {
|
2025-03-05 13:44:58 +07:00
|
|
|
$mail = new ExpenseRejected(
|
2025-01-23 17:46:38 +07:00
|
|
|
$name,
|
|
|
|
|
$expense_number,
|
|
|
|
|
$tanggal,
|
|
|
|
|
$total,
|
2025-10-26 14:14:06 +07:00
|
|
|
$url,
|
|
|
|
|
$request->remarks
|
2025-03-05 13:44:58 +07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$response = $brevoService->expenseRejected($recipient, $name, $mail);
|
|
|
|
|
|
|
|
|
|
if (isset($response['success']) && $response['success'] === false) {
|
|
|
|
|
Log::warning("Email to {$recipient} failed: " . ($response['message'] ?? 'Unknown error'));
|
|
|
|
|
}
|
2025-01-23 17:46:38 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error("Failed to send email to {$recipient}: " . $e->getMessage());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-09 13:22:56 +07:00
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Reject', 'Reject Expense at Form Meeting Seminar (' . $form->expense_number . ')');
|
2024-12-23 10:10:09 +07:00
|
|
|
session()->flash('success', 'Form has been rejected.');
|
2024-12-23 10:42:27 +07:00
|
|
|
return redirect()->back();
|
2024-12-23 10:10:09 +07:00
|
|
|
}
|
|
|
|
|
|
2025-01-16 15:56:22 +07:00
|
|
|
public function open($id)
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['final_approval.open']);
|
|
|
|
|
|
|
|
|
|
$form = FormMeetingSeminar::findOrfail($id);
|
|
|
|
|
$form->update([
|
|
|
|
|
'final_approved_at' => null,
|
|
|
|
|
'final_approved_by' => null,
|
|
|
|
|
'status' => 'Approved'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
AuditTrailHelper::AddAuditTrail('Open', 'Open Expense at Form Meeting Seminar (' . $form->expense_number . ')');
|
|
|
|
|
session()->flash('success', 'Form has been opened.');
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 10:51:27 +07:00
|
|
|
public function destroy($id)
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.meeting.delete']);
|
2025-01-07 17:23:42 +07:00
|
|
|
$role = auth()->user()->getRoleNames()[0];
|
2024-12-19 10:51:27 +07:00
|
|
|
|
|
|
|
|
$form = FormMeetingSeminar::findOrfail($id);
|
2025-01-07 17:23:42 +07:00
|
|
|
if($form->status != 'On Progress' && $role == 'Medical Representatif') {
|
|
|
|
|
session()->flash('error', 'You cannot edit this form because it has been approved or rejected.');
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Delete', 'Delete Record at Form Meeting Seminar (' . $form->expense_number . ')');
|
|
|
|
|
|
2024-12-19 10:51:27 +07:00
|
|
|
$form->delete();
|
|
|
|
|
|
|
|
|
|
session()->flash('success', 'Form has been deleted.');
|
2024-12-23 10:42:27 +07:00
|
|
|
return redirect()->back();
|
2024-12-19 10:51:27 +07:00
|
|
|
}
|
2025-10-13 17:28:34 +07:00
|
|
|
|
|
|
|
|
protected function addAttachmentValidationRules(\Illuminate\Validation\Validator $validator, Request $request): void
|
|
|
|
|
{
|
|
|
|
|
$validator->after(function ($validator) use ($request) {
|
|
|
|
|
$inputAttachments = $request->input('attachments', []);
|
|
|
|
|
$fileAttachments = $request->file('attachments', []);
|
|
|
|
|
|
|
|
|
|
foreach ($inputAttachments as $index => $input) {
|
|
|
|
|
$category = $input['file_category'] ?? null;
|
|
|
|
|
$file = $fileAttachments[$index]['file_path'] ?? null;
|
|
|
|
|
|
|
|
|
|
if ($file && !$category) {
|
|
|
|
|
$validator->errors()->add("attachments.{$index}.file_category", 'Attachment category is required.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($category && !$file) {
|
|
|
|
|
$validator->errors()->add("attachments.{$index}.file_path", 'Attachment file is required.');
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$file) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$extension = strtolower((string) $file->getClientOriginalExtension());
|
|
|
|
|
if ($this->isBlockedExtension($extension)) {
|
|
|
|
|
$validator->errors()->add("attachments.{$index}.file_path", 'File type is not allowed.');
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mimeType = (string) $file->getMimeType();
|
|
|
|
|
if (in_array($extension, $this->imageExtensions, true) && strpos($mimeType, 'image/') !== 0) {
|
|
|
|
|
$validator->errors()->add("attachments.{$index}.file_path", 'Invalid image file.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($extension === 'pdf' && $mimeType !== 'application/pdf') {
|
|
|
|
|
$validator->errors()->add("attachments.{$index}.file_path", 'Invalid PDF file.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($fileAttachments as $index => $files) {
|
|
|
|
|
$file = $files['file_path'] ?? null;
|
|
|
|
|
if ($file && !isset($inputAttachments[$index])) {
|
|
|
|
|
$validator->errors()->add("attachments.{$index}.file_category", 'Attachment category is required.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function buildAttachmentPayload(Request $request, string $folderPath): array
|
|
|
|
|
{
|
|
|
|
|
$payload = [];
|
|
|
|
|
$inputs = $request->input('attachments', []);
|
|
|
|
|
$files = $request->file('attachments', []);
|
|
|
|
|
|
|
|
|
|
foreach ($inputs as $index => $input) {
|
|
|
|
|
$category = $input['file_category'] ?? null;
|
|
|
|
|
$file = $files[$index]['file_path'] ?? null;
|
|
|
|
|
|
|
|
|
|
if (!$category || !$file) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$extension = strtolower((string) $file->getClientOriginalExtension());
|
|
|
|
|
|
|
|
|
|
if ($this->isBlockedExtension($extension)) {
|
|
|
|
|
throw ValidationException::withMessages([
|
|
|
|
|
"attachments.{$index}.file_path" => 'File type is not allowed.',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$filename = Str::uuid() . '.' . $extension;
|
|
|
|
|
$filePath = rtrim($folderPath, '/') . '/' . $filename;
|
|
|
|
|
|
|
|
|
|
$uploadResponse = NextCloudHelper::uploadFile(
|
|
|
|
|
$folderPath,
|
|
|
|
|
$file->getContent(),
|
|
|
|
|
$filename
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (is_array($uploadResponse) && isset($uploadResponse['success']) && $uploadResponse['success'] === false) {
|
|
|
|
|
throw ValidationException::withMessages([
|
|
|
|
|
"attachments.{$index}.file_path" => $uploadResponse['message'] ?? 'Failed to upload attachment.',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$payload[] = [
|
|
|
|
|
'file_category' => $category,
|
|
|
|
|
'file_path' => $filePath,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $payload;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function formatAttachmentCollection(FormMeetingSeminar $form): array
|
|
|
|
|
{
|
|
|
|
|
if (!$form->relationLoaded('attachments')) {
|
|
|
|
|
$form->load('attachments');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attachments = $form->attachments->map(function (AttachmentForm $attachment) {
|
|
|
|
|
if (!$attachment->file_path) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->formatAttachmentData(
|
|
|
|
|
$attachment->id,
|
|
|
|
|
$attachment->file_category,
|
|
|
|
|
$attachment->file_path
|
|
|
|
|
);
|
|
|
|
|
})->filter()->values()->all();
|
|
|
|
|
|
|
|
|
|
$legacySources = [
|
|
|
|
|
'bukti_allowance' => $form->bukti_allowance,
|
|
|
|
|
'bukti_transport_ankot' => $form->bukti_transport_ankot,
|
|
|
|
|
'bukti_hotel' => $form->bukti_hotel,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach ($legacySources as $category => $path) {
|
|
|
|
|
if ($path) {
|
|
|
|
|
$attachments[] = $this->formatAttachmentData(null, $category, $path, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $attachments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function formatAttachmentData(?int $id, ?string $category, string $filePath, bool $canDelete = true): array
|
|
|
|
|
{
|
|
|
|
|
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
|
|
|
|
|
$previewType = $this->determinePreviewType($extension);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => $id,
|
|
|
|
|
'file_category' => $category,
|
|
|
|
|
'category_label' => $this->getCategoryLabel($category),
|
|
|
|
|
'file_path' => $filePath,
|
|
|
|
|
'download_url' => NextCloudHelper::getFileUrl($filePath),
|
|
|
|
|
'preview_url' => in_array($previewType, ['image', 'pdf'], true) ? NextCloudHelper::getPreviewUrl($filePath) : null,
|
|
|
|
|
'preview_type' => $previewType,
|
|
|
|
|
'filename' => basename($filePath),
|
|
|
|
|
'extension' => $extension,
|
|
|
|
|
'can_delete' => $canDelete && !is_null($id),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function determinePreviewType(?string $extension): string
|
|
|
|
|
{
|
|
|
|
|
$extension = strtolower((string) $extension);
|
|
|
|
|
|
|
|
|
|
if (in_array($extension, $this->imageExtensions, true)) {
|
|
|
|
|
return 'image';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($extension === 'pdf') {
|
|
|
|
|
return 'pdf';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'other';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function isBlockedExtension(?string $extension): bool
|
|
|
|
|
{
|
|
|
|
|
return in_array(strtolower((string) $extension), $this->blockedExtensions, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getAttachmentDownloadUrlByCategory(array $attachments, string $category): ?string
|
|
|
|
|
{
|
|
|
|
|
foreach ($attachments as $attachment) {
|
|
|
|
|
if (($attachment['file_category'] ?? null) === $category) {
|
|
|
|
|
return $attachment['download_url'] ?? null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getCategoryLabel(?string $category): string
|
|
|
|
|
{
|
|
|
|
|
if (!$category) {
|
|
|
|
|
return 'Lampiran';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($this->attachmentCategories[$category])) {
|
|
|
|
|
return $this->attachmentCategories[$category];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ucwords(str_replace('_', ' ', $category));
|
|
|
|
|
}
|
2025-10-12 20:47:43 +07:00
|
|
|
}
|