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-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']);
|
|
|
|
|
|
|
|
|
|
$forms = FormUpCountry::with('rayon')->where('user_id', auth()->user()->id)->get();
|
|
|
|
|
return view('backend.pages.forms.upcountry.index', [
|
|
|
|
|
'forms' => $forms
|
|
|
|
|
]);
|
|
|
|
|
}
|
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',
|
|
|
|
|
'hotel' => '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('name', 'Up Country')->first();
|
|
|
|
|
|
|
|
|
|
$filePaths = [];
|
|
|
|
|
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/upcountry/';
|
|
|
|
|
|
|
|
|
|
// check if folder not exists
|
|
|
|
|
// if (!Storage::disk('nextcloud')->exists(trim($folderPath, '/'))) {
|
|
|
|
|
// Storage::disk('nextcloud')->makeDirectory(trim($folderPath, '/'));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Store files to Nextcloud and collect their paths
|
|
|
|
|
if ($request->hasFile('bukti1')) {
|
|
|
|
|
$filePaths['bukti1'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti1'), $request->file('bukti1')->getClientOriginalName());
|
|
|
|
|
}
|
|
|
|
|
if ($request->hasFile('bukti2')) {
|
|
|
|
|
$filePaths['bukti2'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti2'), $request->file('bukti2')->getClientOriginalName());
|
|
|
|
|
}
|
|
|
|
|
if ($request->hasFile('bukti3')) {
|
|
|
|
|
$filePaths['bukti3'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti3'), $request->file('bukti3')->getClientOriginalName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
'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.up-country');
|
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-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',
|
|
|
|
|
'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('name', 'Up Country')->first();
|
|
|
|
|
|
|
|
|
|
$filePaths = [];
|
|
|
|
|
$folderPath = '/expense/' . $region->code . '/' . $cabang->code . '/upcountry/';
|
|
|
|
|
|
|
|
|
|
// check if folder not exists
|
|
|
|
|
// if (!Storage::disk('nextcloud')->exists(trim($folderPath, '/'))) {
|
|
|
|
|
// Storage::disk('nextcloud')->makeDirectory(trim($folderPath, '/'));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Store files to Nextcloud and collect their paths
|
|
|
|
|
if ($request->hasFile('bukti1')) {
|
|
|
|
|
$filePaths['bukti1'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti1'), $request->file('bukti1')->getClientOriginalName());
|
|
|
|
|
}
|
|
|
|
|
if ($request->hasFile('bukti2')) {
|
|
|
|
|
$filePaths['bukti2'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti2'), $request->file('bukti2')->getClientOriginalName());
|
|
|
|
|
}
|
|
|
|
|
if ($request->hasFile('bukti3')) {
|
|
|
|
|
$filePaths['bukti3'] = Storage::disk('nextcloud')->putFileAs($folderPath, $request->file('bukti3'), $request->file('bukti3')->getClientOriginalName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save the form data
|
|
|
|
|
$form = FormUpCountry::findOrfail($id);
|
|
|
|
|
$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,
|
|
|
|
|
'total' => $request->total,
|
|
|
|
|
'bukti1' => $filePaths['bukti1'] ?? $form->bukti1,
|
|
|
|
|
'bukti2' => $filePaths['bukti2'] ?? $form->bukti2,
|
|
|
|
|
'bukti3' => $filePaths['bukti3'] ?? $form->bukti3,
|
|
|
|
|
'account_number' => $kategori->account_number,
|
|
|
|
|
'status' => 'On Progress',
|
|
|
|
|
]);
|
2024-12-12 18:18:26 +07:00
|
|
|
|
2024-12-16 17:01:55 +07:00
|
|
|
session()->flash('success', 'Form has been updated.');
|
|
|
|
|
return redirect()->route('forms.up-country');
|
2024-12-12 18:18:26 +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);
|
|
|
|
|
$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.');
|
|
|
|
|
return redirect()->route('forms.up-country');
|
2024-12-12 18:18:26 +07:00
|
|
|
}
|
|
|
|
|
}
|