added form others
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Forms;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\FormOthers;
|
||||
use App\Models\Rayon;
|
||||
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;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Helpers\NextCloudHelper;
|
||||
|
||||
class FormOtherController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$this->checkAuthorization(auth()->user(), ['forms.other.view']);
|
||||
|
||||
$forms = FormOthers::where('user_id', auth()->user()->id)->get();
|
||||
return view('backend.pages.forms.other.index', [
|
||||
'forms' => $forms
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->checkAuthorization(auth()->user(), ['forms.other.create']);
|
||||
|
||||
return view('backend.pages.forms.other.create', [
|
||||
'kategori' => Kategori::whereNotIn('name', ['Up Country', 'Vehicle Running Cost', 'Entertainment & Presentation', 'Meeting & Seminar'])->get()
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$this->checkAuthorization(auth()->user(), ['forms.other.create']);
|
||||
|
||||
$request->validate([
|
||||
'kategori_id' => 'required',
|
||||
'tanggal' => 'required|date',
|
||||
'keterangan' => 'required',
|
||||
'total' => 'required',
|
||||
'bukti1' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
|
||||
'bukti2' => 'nullable|file|mimes:xlsx,xls,csv',
|
||||
'bukti3' => 'nullable|file|mimes:pdf',
|
||||
]);
|
||||
|
||||
$cabang = UserHasCabang::with('cabang')->where('user_id', auth()->user()->id)->first()->cabang;
|
||||
$region = Region::where('id', $cabang->region_id)->first();
|
||||
$kategori = Kategori::where('id', $request->kategori_id)->first();
|
||||
|
||||
$filePaths = [];
|
||||
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/other';
|
||||
|
||||
if ($request->hasFile('bukti1')) {
|
||||
$bukti1 = $request->file('bukti1');
|
||||
$filename = Str::uuid() . '.' . $bukti1->extension();
|
||||
|
||||
$filePaths['bukti1'] = $folderPath . '/' . $filename;
|
||||
|
||||
NextCloudHelper::uploadFile(
|
||||
$folderPath,
|
||||
$bukti1->getContent(),
|
||||
$filename
|
||||
);
|
||||
|
||||
}
|
||||
if ($request->hasFile('bukti2')) {
|
||||
$bukti2 = $request->file('bukti2');
|
||||
$filename = Str::uuid() . '.' . $bukti2->extension();
|
||||
|
||||
$filePaths['bukti2'] = $folderPath . '/' . $filename;
|
||||
|
||||
NextCloudHelper::uploadFile(
|
||||
$folderPath,
|
||||
$bukti2->getContent(),
|
||||
$filename
|
||||
);
|
||||
}
|
||||
if ($request->hasFile('bukti3')) {
|
||||
$bukti3 = $request->file('bukti3');
|
||||
$filename = Str::uuid() . '.' . $bukti3->extension();
|
||||
|
||||
$filePaths['bukti3'] = $folderPath . '/' . $filename;
|
||||
|
||||
NextCloudHelper::uploadFile(
|
||||
$folderPath,
|
||||
$bukti3->getContent(),
|
||||
$filename
|
||||
);
|
||||
}
|
||||
|
||||
// Generate sequence number and expense number
|
||||
$sequence_number = FormOthers::withTrashed()->where('expense_number', 'like', $region->code . '-' . $cabang->code . '-OTH-' . date('ym') . '%')->count() + 1;
|
||||
$formatted_sequence_number = str_pad($sequence_number, 4, '0', STR_PAD_LEFT);
|
||||
$expense_number = $region->code . '-' . $cabang->code . '-OTH-' . date('ym') . $formatted_sequence_number;
|
||||
|
||||
// Save the form data
|
||||
FormOthers::create([
|
||||
'expense_number' => $expense_number,
|
||||
'user_id' => auth()->user()->id,
|
||||
'kategori_id' => $request->kategori_id,
|
||||
'tanggal' => $request->tanggal,
|
||||
'keterangan' => $request->keterangan,
|
||||
'total' => $request->total,
|
||||
'bukti1' => $filePaths['bukti1'] ?? null,
|
||||
'bukti2' => $filePaths['bukti2'] ?? null,
|
||||
'bukti3' => $filePaths['bukti3'] ?? null,
|
||||
'account_number' => $kategori->account_number,
|
||||
'status' => 'On Progress',
|
||||
]);
|
||||
|
||||
session()->flash('success', 'Form has been created.');
|
||||
return redirect()->route('forms.other');
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$this->checkAuthorization(auth()->user(), ['forms.other.edit']);
|
||||
|
||||
$form = FormOthers::findOrfail($id);
|
||||
return view('backend.pages.forms.other.edit', [
|
||||
'form' => $form,
|
||||
'kategori' => Kategori::whereNotIn('name', ['Up Country', 'Vehicle Running Cost', 'Entertainment & Presentation', 'Meeting & Seminar'])->get()
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$this->checkAuthorization(auth()->user(), ['forms.other.edit']);
|
||||
|
||||
$request->validate([
|
||||
'kategori_id' => 'required',
|
||||
'tanggal' => 'required|date',
|
||||
'keterangan' => 'required',
|
||||
'total' => 'required',
|
||||
'bukti1' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
|
||||
'bukti2' => 'nullable|file|mimes:xlsx,xls,csv',
|
||||
'bukti3' => 'nullable|file|mimes:pdf',
|
||||
]);
|
||||
|
||||
$cabang = UserHasCabang::with('cabang')->where('user_id', auth()->user()->id)->first()->cabang;
|
||||
$region = Region::where('id', $cabang->region_id)->first();
|
||||
$kategori = Kategori::where('id', $request->kategori_id)->first();
|
||||
|
||||
$filePaths = [];
|
||||
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/other';
|
||||
|
||||
if ($request->hasFile('bukti1')) {
|
||||
$bukti1 = $request->file('bukti1');
|
||||
$filename = Str::uuid() . '.' . $bukti1->extension();
|
||||
|
||||
$filePaths['bukti1'] = $folderPath . '/' . $filename;
|
||||
|
||||
NextCloudHelper::uploadFile(
|
||||
$folderPath,
|
||||
$bukti1->getContent(),
|
||||
$filename
|
||||
);
|
||||
|
||||
}
|
||||
if ($request->hasFile('bukti2')) {
|
||||
$bukti2 = $request->file('bukti2');
|
||||
$filename = Str::uuid() . '.' . $bukti2->extension();
|
||||
|
||||
$filePaths['bukti2'] = $folderPath . '/' . $filename;
|
||||
|
||||
NextCloudHelper::uploadFile(
|
||||
$folderPath,
|
||||
$bukti2->getContent(),
|
||||
$filename
|
||||
);
|
||||
}
|
||||
if ($request->hasFile('bukti3')) {
|
||||
$bukti3 = $request->file('bukti3');
|
||||
$filename = Str::uuid() . '.' . $bukti3->extension();
|
||||
|
||||
$filePaths['bukti3'] = $folderPath . '/' . $filename;
|
||||
|
||||
NextCloudHelper::uploadFile(
|
||||
$folderPath,
|
||||
$bukti3->getContent(),
|
||||
$filename
|
||||
);
|
||||
}
|
||||
|
||||
// Save the form data
|
||||
$form = FormOthers::findOrfail($id);
|
||||
$form->update([
|
||||
'kategori_id' => $request->kategori_id,
|
||||
'tanggal' => $request->tanggal,
|
||||
'keterangan' => $request->keterangan,
|
||||
'total' => $request->total,
|
||||
'bukti1' => $filePaths['bukti1'] ?? $form->bukti1,
|
||||
'bukti2' => $filePaths['bukti2'] ?? $form->bukti2,
|
||||
'bukti3' => $filePaths['bukti3'] ?? $form->bukti3,
|
||||
'account_number' => $kategori->account_number,
|
||||
]);
|
||||
|
||||
session()->flash('success', 'Form has been updated.');
|
||||
return redirect()->route('forms.other');
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$this->checkAuthorization(auth()->user(), ['forms.other.delete']);
|
||||
|
||||
$form = FormOthers::findOrfail($id);
|
||||
$form->delete();
|
||||
|
||||
session()->flash('success', 'Form has been deleted.');
|
||||
return redirect()->route('forms.other');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user