2024-12-12 18:18:26 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Forms;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\FormUpCountry;
|
|
|
|
|
use App\Models\Rayon;
|
2024-12-16 17:01:55 +07:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
use App\Models\Region;
|
|
|
|
|
use App\Models\Cabang;
|
|
|
|
|
use App\Models\UserHasCabang;
|
|
|
|
|
use App\Models\Kategori;
|
2024-12-18 12:56:16 +07:00
|
|
|
use Illuminate\Support\Str;
|
2024-12-24 15:12:24 +07:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use App\Mail\ExpenseApproved;
|
|
|
|
|
use App\Mail\ExpenseRejected;
|
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-02 18:21:03 +07:00
|
|
|
use App\Helpers\NextCloudHelper;
|
2025-01-02 15:19:43 +07:00
|
|
|
use App\Rules\FileType;
|
2024-12-12 18:18:26 +07:00
|
|
|
|
|
|
|
|
class FormUpCountryController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
2024-12-16 17:01:55 +07:00
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.country.view']);
|
|
|
|
|
|
2024-12-23 10:10:09 +07:00
|
|
|
$role = auth()->user()->getRoleNames()[0];
|
2024-12-26 18:37:34 +07:00
|
|
|
$forms = FormUpCountry::get();
|
|
|
|
|
|
2025-01-01 15:33:16 +07:00
|
|
|
if ($role == 'Admin Region' || $role == 'Marketing Operational Manager Region') {
|
2024-12-26 18:37:34 +07:00
|
|
|
$region_id = auth()->user()->getMyCabangAndRegionId()['region'];
|
|
|
|
|
|
|
|
|
|
if ($region_id) {
|
|
|
|
|
$users = UserHasCabang::whereHas('cabang', function ($query) use ($region_id) {
|
|
|
|
|
$query->where('region_id', $region_id);
|
|
|
|
|
})->get();
|
|
|
|
|
|
|
|
|
|
$userIds = $users->pluck('user_id')->toArray();
|
|
|
|
|
$forms = $forms->whereIn('user_id', $userIds);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-01 15:33:16 +07:00
|
|
|
else if ($role == 'Area Manager Cabang') {
|
|
|
|
|
$cabang_id = auth()->user()->getMyCabangAndRegionId()['cabang'];
|
|
|
|
|
|
|
|
|
|
if ($cabang_id) {
|
|
|
|
|
$users = UserHasCabang::where('cabang_id', $cabang_id)->get();
|
|
|
|
|
$userIds = $users->pluck('user_id')->toArray();
|
|
|
|
|
$forms = $forms->whereIn('user_id', $userIds);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if ($role == 'Medical Representatif') {
|
2024-12-26 18:37:34 +07:00
|
|
|
$forms = $forms->where('user_id', auth()->user()->id);
|
2025-01-01 15:33:16 +07:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$forms = $forms;
|
2024-12-23 10:10:09 +07:00
|
|
|
}
|
|
|
|
|
|
2024-12-26 13:28:06 +07:00
|
|
|
session()->put('redirect_url', route('forms.up-country'));
|
2024-12-16 17:01:55 +07:00
|
|
|
return view('backend.pages.forms.upcountry.index', [
|
|
|
|
|
'forms' => $forms
|
|
|
|
|
]);
|
|
|
|
|
}
|
2024-12-12 18:18:26 +07:00
|
|
|
|
2025-01-02 17:59:33 +07:00
|
|
|
public function detail($id)
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.country.view']);
|
|
|
|
|
|
|
|
|
|
$form = FormUpCountry::with(['rayon', 'user'])->findOrFail($id);
|
2025-01-02 18:21:03 +07:00
|
|
|
$form->bukti1 = $form->bukti1 ? NextCloudHelper::getFileUrl($form->bukti1) : null;
|
|
|
|
|
$form->bukti2 = $form->bukti2 ? NextCloudHelper::getFileUrl($form->bukti2) : null;
|
|
|
|
|
$form->bukti3 = $form->bukti3 ? NextCloudHelper::getFileUrl($form->bukti3) : null;
|
2025-01-02 17:59:33 +07:00
|
|
|
return response()->json($form);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-12 18:18:26 +07:00
|
|
|
public function create()
|
|
|
|
|
{
|
2024-12-16 17:01:55 +07:00
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.country.create']);
|
|
|
|
|
|
2024-12-12 18:18:26 +07:00
|
|
|
return view('backend.pages.forms.upcountry.create', [
|
|
|
|
|
'rayons' => Rayon::all()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 17:01:55 +07:00
|
|
|
public function store(Request $request)
|
2024-12-12 18:18:26 +07:00
|
|
|
{
|
2024-12-16 17:01:55 +07:00
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.country.create']);
|
|
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
'rayon_id' => 'required|exists:rayon,id',
|
|
|
|
|
'tanggal' => 'required',
|
|
|
|
|
'tujuan' => 'required',
|
|
|
|
|
'jarak' => 'required',
|
|
|
|
|
'allowance' => 'required',
|
|
|
|
|
'transport_dalkot' => 'required',
|
|
|
|
|
'transport_ankot' => 'required',
|
2024-12-27 13:34:59 +07:00
|
|
|
'bukti1' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,heic,heif|max:51200',
|
2025-01-02 15:19:43 +07:00
|
|
|
'bukti2' => ['nullable', 'file', new FileType(['php', 'exe', 'js', 'sh', 'bat', 'exe', 'sh', 'php', 'vbs'])],
|
|
|
|
|
'bukti3' => ['nullable', 'file', new FileType(['php', 'exe', 'js', 'sh', 'bat', 'exe', 'sh', 'php', 'vbs'])],
|
2024-12-16 17:01:55 +07:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$cabang = UserHasCabang::with('cabang')->where('user_id', auth()->user()->id)->first()->cabang;
|
|
|
|
|
$region = Region::where('id', $cabang->region_id)->first();
|
|
|
|
|
$kategori = Kategori::where('name', 'Up Country')->first();
|
|
|
|
|
|
|
|
|
|
$filePaths = [];
|
2024-12-19 10:51:27 +07:00
|
|
|
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/upcountry';
|
2024-12-16 17:01:55 +07:00
|
|
|
|
2024-12-18 12:56:16 +07:00
|
|
|
if ($request->hasFile('bukti1')) {
|
|
|
|
|
$bukti1 = $request->file('bukti1');
|
|
|
|
|
$filename = Str::uuid() . '.' . $bukti1->extension();
|
|
|
|
|
|
|
|
|
|
$filePaths['bukti1'] = $folderPath . '/' . $filename;
|
|
|
|
|
|
|
|
|
|
NextCloudHelper::uploadFile(
|
|
|
|
|
$folderPath,
|
|
|
|
|
$bukti1->getContent(),
|
|
|
|
|
$filename
|
|
|
|
|
);
|
2024-12-16 17:01:55 +07:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if ($request->hasFile('bukti2')) {
|
2024-12-18 12:56:16 +07:00
|
|
|
$bukti2 = $request->file('bukti2');
|
|
|
|
|
$filename = Str::uuid() . '.' . $bukti2->extension();
|
|
|
|
|
|
|
|
|
|
$filePaths['bukti2'] = $folderPath . '/' . $filename;
|
|
|
|
|
|
|
|
|
|
NextCloudHelper::uploadFile(
|
|
|
|
|
$folderPath,
|
|
|
|
|
$bukti2->getContent(),
|
|
|
|
|
$filename
|
|
|
|
|
);
|
2024-12-16 17:01:55 +07:00
|
|
|
}
|
|
|
|
|
if ($request->hasFile('bukti3')) {
|
2024-12-18 12:56:16 +07:00
|
|
|
$bukti3 = $request->file('bukti3');
|
|
|
|
|
$filename = Str::uuid() . '.' . $bukti3->extension();
|
|
|
|
|
|
|
|
|
|
$filePaths['bukti3'] = $folderPath . '/' . $filename;
|
|
|
|
|
|
|
|
|
|
NextCloudHelper::uploadFile(
|
|
|
|
|
$folderPath,
|
|
|
|
|
$bukti3->getContent(),
|
|
|
|
|
$filename
|
|
|
|
|
);
|
2024-12-16 17:01:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate sequence number and expense number
|
|
|
|
|
$sequence_number = FormUpCountry::withTrashed()->where('expense_number', 'like', $region->code . '-' . $cabang->code . '-UPC-' . date('ym') . '%')->count() + 1;
|
|
|
|
|
$formatted_sequence_number = str_pad($sequence_number, 4, '0', STR_PAD_LEFT);
|
|
|
|
|
$expense_number = $region->code . '-' . $cabang->code . '-UPC-' . date('ym') . $formatted_sequence_number;
|
2024-12-12 18:18:26 +07:00
|
|
|
|
2024-12-16 17:01:55 +07:00
|
|
|
// Save the form data
|
|
|
|
|
FormUpCountry::create([
|
|
|
|
|
'expense_number' => $expense_number,
|
|
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
|
'rayon_id' => $request->rayon_id,
|
|
|
|
|
'tanggal' => $request->tanggal,
|
|
|
|
|
'tujuan' => $request->tujuan,
|
|
|
|
|
'jarak' => $request->jarak,
|
|
|
|
|
'allowance' => $request->allowance,
|
|
|
|
|
'transport_dalkot' => $request->transport_dalkot,
|
|
|
|
|
'transport_ankot' => $request->transport_ankot,
|
|
|
|
|
'hotel' => $request->hotel,
|
2024-12-20 11:07:07 +07:00
|
|
|
'total' => $request->allowance + $request->transport_dalkot + $request->transport_ankot + $request->hotel,
|
2024-12-16 17:01:55 +07:00
|
|
|
'bukti1' => $filePaths['bukti1'] ?? null,
|
|
|
|
|
'bukti2' => $filePaths['bukti2'] ?? null,
|
|
|
|
|
'bukti3' => $filePaths['bukti3'] ?? null,
|
|
|
|
|
'account_number' => $kategori->account_number,
|
|
|
|
|
'status' => 'On Progress',
|
|
|
|
|
]);
|
|
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Insert', 'Insert New Expense at Form Entertainment Presentation (' . $expense_number . ')');
|
2024-12-16 17:01:55 +07:00
|
|
|
session()->flash('success', 'Form has been created.');
|
2024-12-23 10:42:27 +07:00
|
|
|
return redirect()->back();
|
2024-12-12 18:18:26 +07:00
|
|
|
}
|
|
|
|
|
|
2024-12-16 17:01:55 +07:00
|
|
|
public function edit($id)
|
2024-12-12 18:18:26 +07:00
|
|
|
{
|
2024-12-16 17:01:55 +07:00
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.country.edit']);
|
|
|
|
|
|
|
|
|
|
$form = FormUpCountry::with('rayon')->findOrfail($id);
|
2024-12-24 11:23:27 +07:00
|
|
|
if($form->status == 'Closed') {
|
|
|
|
|
session()->flash('error', 'Form has been closed, you cannot edit it.');
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-12 18:18:26 +07:00
|
|
|
return view('backend.pages.forms.upcountry.edit', [
|
2024-12-16 17:01:55 +07:00
|
|
|
'rayons' => Rayon::all(),
|
|
|
|
|
'form' => $form
|
2024-12-12 18:18:26 +07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 17:01:55 +07:00
|
|
|
public function update(Request $request, $id)
|
2024-12-12 18:18:26 +07:00
|
|
|
{
|
2024-12-16 17:01:55 +07:00
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.country.edit']);
|
|
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
'rayon_id' => 'required|exists:rayon,id',
|
|
|
|
|
'tanggal' => 'required',
|
|
|
|
|
'tujuan' => 'required',
|
|
|
|
|
'jarak' => 'required',
|
|
|
|
|
'allowance' => 'required',
|
|
|
|
|
'transport_dalkot' => 'required',
|
|
|
|
|
'transport_ankot' => 'required',
|
|
|
|
|
'hotel' => 'required',
|
2024-12-27 13:34:59 +07:00
|
|
|
'bukti1' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,heic,heif|max:51200',
|
2025-01-02 15:19:43 +07:00
|
|
|
'bukti2' => ['nullable', 'file', new FileType(['php', 'exe', 'js', 'sh', 'bat', 'exe', 'sh', 'php', 'vbs'])],
|
|
|
|
|
'bukti3' => ['nullable', 'file', new FileType(['php', 'exe', 'js', 'sh', 'bat', 'exe', 'sh', 'php', 'vbs'])],
|
2024-12-16 17:01:55 +07:00
|
|
|
]);
|
|
|
|
|
|
2024-12-23 10:10:09 +07:00
|
|
|
$form = FormUpCountry::findOrfail($id);
|
2024-12-24 11:23:27 +07:00
|
|
|
if($form->status == 'Closed') {
|
|
|
|
|
session()->flash('error', 'Form has been closed, you cannot edit it.');
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-23 10:10:09 +07:00
|
|
|
$cabang = UserHasCabang::with('cabang')->where('user_id', $form->user_id)->first()->cabang;
|
2024-12-16 17:01:55 +07:00
|
|
|
$region = Region::where('id', $cabang->region_id)->first();
|
|
|
|
|
$kategori = Kategori::where('name', 'Up Country')->first();
|
|
|
|
|
|
|
|
|
|
$filePaths = [];
|
2024-12-19 10:51:27 +07:00
|
|
|
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/upcountry';
|
2024-12-16 17:01:55 +07:00
|
|
|
|
|
|
|
|
if ($request->hasFile('bukti1')) {
|
2024-12-18 12:56:16 +07:00
|
|
|
$bukti1 = $request->file('bukti1');
|
|
|
|
|
$filename = Str::uuid() . '.' . $bukti1->extension();
|
|
|
|
|
|
|
|
|
|
$filePaths['bukti1'] = $folderPath . '/' . $filename;
|
|
|
|
|
|
|
|
|
|
NextCloudHelper::uploadFile(
|
|
|
|
|
$folderPath,
|
|
|
|
|
$bukti1->getContent(),
|
|
|
|
|
$filename
|
|
|
|
|
);
|
|
|
|
|
|
2024-12-16 17:01:55 +07:00
|
|
|
}
|
|
|
|
|
if ($request->hasFile('bukti2')) {
|
2024-12-18 12:56:16 +07:00
|
|
|
$bukti2 = $request->file('bukti2');
|
|
|
|
|
$filename = Str::uuid() . '.' . $bukti2->extension();
|
|
|
|
|
|
|
|
|
|
$filePaths['bukti2'] = $folderPath . '/' . $filename;
|
|
|
|
|
|
|
|
|
|
NextCloudHelper::uploadFile(
|
|
|
|
|
$folderPath,
|
|
|
|
|
$bukti2->getContent(),
|
|
|
|
|
$filename
|
|
|
|
|
);
|
2024-12-16 17:01:55 +07:00
|
|
|
}
|
|
|
|
|
if ($request->hasFile('bukti3')) {
|
2024-12-18 12:56:16 +07:00
|
|
|
$bukti3 = $request->file('bukti3');
|
|
|
|
|
$filename = Str::uuid() . '.' . $bukti3->extension();
|
|
|
|
|
|
|
|
|
|
$filePaths['bukti3'] = $folderPath . '/' . $filename;
|
|
|
|
|
|
|
|
|
|
NextCloudHelper::uploadFile(
|
|
|
|
|
$folderPath,
|
|
|
|
|
$bukti3->getContent(),
|
|
|
|
|
$filename
|
|
|
|
|
);
|
2024-12-16 17:01:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save the form data
|
|
|
|
|
$form->update([
|
|
|
|
|
'rayon_id' => $request->rayon_id,
|
|
|
|
|
'tanggal' => $request->tanggal,
|
|
|
|
|
'tujuan' => $request->tujuan,
|
|
|
|
|
'jarak' => $request->jarak,
|
|
|
|
|
'allowance' => $request->allowance,
|
|
|
|
|
'transport_dalkot' => $request->transport_dalkot,
|
|
|
|
|
'transport_ankot' => $request->transport_ankot,
|
|
|
|
|
'hotel' => $request->hotel,
|
2024-12-20 11:07:07 +07:00
|
|
|
'total' => $request->allowance + $request->transport_dalkot + $request->transport_ankot + $request->hotel,
|
2024-12-16 17:01:55 +07:00
|
|
|
'bukti1' => $filePaths['bukti1'] ?? $form->bukti1,
|
|
|
|
|
'bukti2' => $filePaths['bukti2'] ?? $form->bukti2,
|
|
|
|
|
'bukti3' => $filePaths['bukti3'] ?? $form->bukti3,
|
|
|
|
|
'account_number' => $kategori->account_number,
|
|
|
|
|
]);
|
2024-12-12 18:18:26 +07:00
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Update', 'Update Record at Form Entertainment Presentation (' . $form->expense_number . ')');
|
2024-12-16 17:01:55 +07:00
|
|
|
session()->flash('success', 'Form has been updated.');
|
2024-12-23 10:42:27 +07:00
|
|
|
return redirect()->back();
|
2024-12-12 18:18:26 +07:00
|
|
|
}
|
|
|
|
|
|
2025-01-02 17:59:33 +07:00
|
|
|
public function approve(Request $request, $id)
|
2024-12-23 10:10:09 +07:00
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['approval.approve']);
|
|
|
|
|
|
|
|
|
|
$form = FormUpCountry::findOrfail($id);
|
2025-01-02 17:59:33 +07:00
|
|
|
$approved_data = [
|
|
|
|
|
'allowance' => $request->allowance ? $form->allowance : 0,
|
|
|
|
|
'transport_dalkot' => $request->transport_dalkot ? $form->transport_dalkot : 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-02 17:59:33 +07:00
|
|
|
'status' => 'Approved',
|
|
|
|
|
|
|
|
|
|
'approved_allowance' => $approved_data['allowance'],
|
|
|
|
|
'approved_transport_dalkot' => $approved_data['transport_dalkot'],
|
|
|
|
|
'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
|
|
|
$heads = $form->user->getCabangAndRegionHead();
|
|
|
|
|
$name = $form->user->name;
|
|
|
|
|
$expense_number = $form->expense_number;
|
|
|
|
|
$tanggal = $form->tanggal;
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
2024-12-25 13:37:49 +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);
|
2024-12-24 15:12:24 +07:00
|
|
|
|
2024-12-25 13:37:49 +07:00
|
|
|
// // Send bulk email
|
2025-01-02 18:41:20 +07:00
|
|
|
// Mail::to($recipients)->send(new ExpenseApproved(
|
|
|
|
|
// $name,
|
|
|
|
|
// $expense_number,
|
|
|
|
|
// $tanggal,
|
|
|
|
|
// $total
|
|
|
|
|
// ));
|
2024-12-24 15:12:24 +07:00
|
|
|
|
2024-12-25 13:37:49 +07:00
|
|
|
// send whatsapp message
|
|
|
|
|
WhatsappHelper::approveExpense($phoneNumbers, $expense_number);
|
|
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Approve', 'Approve Expense at Form Entertainment Presentation (' . $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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function reject($id)
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['approval.approve']);
|
|
|
|
|
|
|
|
|
|
$form = FormUpCountry::findOrfail($id);
|
|
|
|
|
$form->update([
|
|
|
|
|
'status' => 'Rejected'
|
|
|
|
|
]);
|
|
|
|
|
|
2024-12-24 15:12:24 +07:00
|
|
|
$heads = $form->user->getCabangAndRegionHead();
|
|
|
|
|
$name = $form->user->name;
|
|
|
|
|
$expense_number = $form->expense_number;
|
|
|
|
|
$tanggal = $form->tanggal;
|
|
|
|
|
$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
|
|
|
|
|
|
|
|
// Send bulk email
|
|
|
|
|
Mail::to($recipients)->send(new ExpenseRejected(
|
|
|
|
|
$name,
|
|
|
|
|
$expense_number,
|
|
|
|
|
$tanggal,
|
|
|
|
|
$total
|
|
|
|
|
));
|
|
|
|
|
|
2024-12-25 13:37:49 +07:00
|
|
|
// send whatsapp message
|
|
|
|
|
WhatsappHelper::rejectExpense($phoneNumbers, $expense_number);
|
|
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Reject', 'Reject Expense at Form Entertainment Presentation (' . $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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function finalApprove($id)
|
|
|
|
|
{
|
|
|
|
|
$this->checkAuthorization(auth()->user(), ['final_approval.approve']);
|
|
|
|
|
|
|
|
|
|
$form = FormUpCountry::findOrfail($id);
|
|
|
|
|
if($form->approved_at == null) {
|
|
|
|
|
session()->flash('error', 'Form must be approved by MIS first.');
|
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,
|
|
|
|
|
'status' => 'Closed'
|
|
|
|
|
]);
|
|
|
|
|
|
2024-12-26 13:49:00 +07:00
|
|
|
$heads = $form->user->getCabangAndRegionHead();
|
|
|
|
|
$name = $form->user->name;
|
|
|
|
|
$expense_number = $form->expense_number;
|
|
|
|
|
$tanggal = $form->tanggal;
|
|
|
|
|
$total = $form->total;
|
|
|
|
|
|
|
|
|
|
// Collect all recipients
|
|
|
|
|
$recipients = [$form->user->email, auth()->user()->email];
|
|
|
|
|
$phoneNumbers = [$form->user->phone, auth()->user()->phone];
|
|
|
|
|
|
|
|
|
|
if ($heads['cabang_head']) {
|
|
|
|
|
$recipients[] = $heads['cabang_head']['email'];
|
|
|
|
|
$phoneNumbers[] = $heads['cabang_head']['phone'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($heads['region_head']) {
|
|
|
|
|
$recipients[] = $heads['region_head']['email'];
|
|
|
|
|
$phoneNumbers[] = $heads['region_head']['phone'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove duplicate email addresses, if any
|
|
|
|
|
$recipients = array_unique($recipients);
|
|
|
|
|
$phoneNumbers = array_unique($phoneNumbers);
|
|
|
|
|
|
|
|
|
|
// Send bulk email
|
|
|
|
|
Mail::to($recipients)->send(new FinalApprove(
|
|
|
|
|
$name,
|
|
|
|
|
$expense_number,
|
|
|
|
|
$tanggal,
|
|
|
|
|
$total
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// send whatsapp message
|
|
|
|
|
WhatsappHelper::finalApprove($phoneNumbers, $expense_number);
|
|
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Final Approve', 'Final Approve Expense at Form Entertainment Presentation (' . $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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function open($id)
|
|
|
|
|
{
|
2024-12-26 17:28:16 +07:00
|
|
|
$this->checkAuthorization(auth()->user(), ['final_approval.open']);
|
2024-12-23 10:10:09 +07:00
|
|
|
|
|
|
|
|
$form = FormUpCountry::findOrfail($id);
|
|
|
|
|
$form->update([
|
|
|
|
|
'final_approved_at' => null,
|
|
|
|
|
'final_approved_by' => null,
|
|
|
|
|
'status' => 'Approved'
|
|
|
|
|
]);
|
|
|
|
|
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Open', 'Open Expense at Form Entertainment Presentation (' . $form->expense_number . ')');
|
2024-12-23 10:10:09 +07:00
|
|
|
session()->flash('success', 'Form has been opened.');
|
2024-12-23 10:42:27 +07:00
|
|
|
return redirect()->back();
|
2024-12-23 10:10:09 +07:00
|
|
|
}
|
|
|
|
|
|
2024-12-16 17:01:55 +07:00
|
|
|
public function destroy($id)
|
2024-12-12 18:18:26 +07:00
|
|
|
{
|
2024-12-16 17:01:55 +07:00
|
|
|
$this->checkAuthorization(auth()->user(), ['forms.country.delete']);
|
|
|
|
|
|
|
|
|
|
$form = FormUpCountry::findOrfail($id);
|
2025-01-01 17:14:46 +07:00
|
|
|
AuditTrailHelper::AddAuditTrail('Delete', 'Delete Record at Form Entertainment Presentation (' . $form->expense_number . ')');
|
|
|
|
|
|
2024-12-16 17:01:55 +07:00
|
|
|
$form->delete();
|
2024-12-12 18:18:26 +07:00
|
|
|
|
2024-12-16 17:01:55 +07:00
|
|
|
session()->flash('success', 'Form has been deleted.');
|
2024-12-23 10:42:27 +07:00
|
|
|
return redirect()->back();
|
2024-12-12 18:18:26 +07:00
|
|
|
}
|
|
|
|
|
}
|