315 lines
11 KiB
PHP
315 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Activity;
|
|
|
|
use App\Enums\ActivityType;
|
|
use App\Enums\LingkupAksesData;
|
|
use App\Exports\FormExport;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Imports\FormImport;
|
|
use App\Models\ActivityApprovalKonsolidasi;
|
|
use App\Models\ActivityForm;
|
|
use App\Models\ActivityLock;
|
|
use App\Models\Agency;
|
|
use App\Models\LogUserActivity;
|
|
use App\Models\ReferenceUnit;
|
|
use App\Models\SettingForm;
|
|
use App\Models\SettingFormDetail;
|
|
use Illuminate\Http\Request;
|
|
use App\Services\Activity\FormService;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Routing\Controllers\HasMiddleware;
|
|
use Illuminate\Routing\Controllers\Middleware;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class FormController implements HasMiddleware
|
|
{
|
|
// protected $title = 'Common Reporting Format (CRF)';
|
|
protected $template = 'modules.form';
|
|
protected $route = 'modules.laporan.crf';
|
|
protected $formService;
|
|
|
|
public function __construct(FormService $formService)
|
|
{
|
|
$this->formService = $formService;
|
|
}
|
|
|
|
public static function middleware(): array
|
|
{
|
|
$sector = request()->route('sector');
|
|
$code = request()->route('code');
|
|
|
|
return [
|
|
// //new Middleware('permission:/' . $sector . '/' . $code, except: ['getUserActivities']),
|
|
];
|
|
}
|
|
|
|
public function data($sector, $code, $inventoryYear, $instansi = null)
|
|
{
|
|
// Fetch the form from the database based on sector and code
|
|
$form = SettingForm::rowActive()->where('sector', $sector)->where('code', $code)->first();
|
|
|
|
if (!$form) {
|
|
abort(404, 'Page not found');
|
|
}
|
|
|
|
// Fetch form details
|
|
$formDetails = SettingFormDetail::rowActive()->where('form_code', $code)->orderByRowNum()->get();
|
|
|
|
if ($instansi && $instansi != 'all' && $instansi != 'none') {
|
|
$agency = Agency::where('name', $instansi)->rowActive()->first();
|
|
$instansi = $agency ? $agency->id : null;
|
|
}
|
|
|
|
// get Agencies for produsen data mapping
|
|
$user = Auth::user();
|
|
$scope = $user->getScope();
|
|
|
|
if ($scope === LingkupAksesData::INTERNAL->value) {
|
|
$instansi = $user->agency_id;
|
|
}
|
|
|
|
$agencies = Agency::whereHas('agencyActivity', function ($query) use ($sector, $code, $scope, $user) {
|
|
$query->where('form_sector', $sector)
|
|
->where('form_code', $code);
|
|
|
|
if ($scope === LingkupAksesData::INTERNAL->value) {
|
|
$query->where('agency_id', $user->agency_id);
|
|
}
|
|
})->get();
|
|
|
|
// Fetch activity form and activity form details using service
|
|
$activityForm = $this->formService->getActivityForm($sector, $code, $inventoryYear, $instansi);
|
|
if ($instansi == 'all') {
|
|
$activityFormId = $activityForm->pluck('id')->toArray();
|
|
} else {
|
|
$activityFormId = $activityForm->id ?? null;
|
|
}
|
|
|
|
$activityFormDetails = $this->formService->getActivityFormDetails($activityFormId, $instansi);
|
|
|
|
// Fetch all units & set the mappings
|
|
$allUnits = ReferenceUnit::rowActive()->get();
|
|
$unitsMap = $allUnits->keyBy('code');
|
|
$unitsByCategory = $allUnits->groupBy('category');
|
|
|
|
// For Copy data feature
|
|
// $availableYears = ActivityForm::select('inventory_year')->distinct()
|
|
// ->where('sector', $sector)->where('form_code', $code)->rowActive()
|
|
// ->orderBy('inventory_year', 'desc')->get()->pluck('inventory_year')->toArray();
|
|
|
|
$isLocked = ActivityLock::isLocked($inventoryYear);
|
|
$isApprove = false;
|
|
if ($instansi && $instansi != 'all' && $instansi != 'none') {
|
|
$isApprove = ActivityApprovalKonsolidasi::isApprove($activityFormId, $sector, $code, $inventoryYear, $instansi);
|
|
}
|
|
|
|
$internal = $instansi;
|
|
|
|
return compact(
|
|
'form',
|
|
'formDetails',
|
|
'activityForm',
|
|
'activityFormDetails',
|
|
'unitsMap',
|
|
'unitsByCategory',
|
|
'inventoryYear',
|
|
'isLocked',
|
|
'agencies',
|
|
'internal',
|
|
'isApprove'
|
|
);
|
|
}
|
|
|
|
public function show($sector, $code)
|
|
{
|
|
// Filter by Year
|
|
$inventoryYear = request('inventoryYear') ?? date('Y');
|
|
$instansi = request('instansi') ?? null;
|
|
|
|
// Retrieve data
|
|
$data = $this->data($sector, $code, $inventoryYear, $instansi);
|
|
|
|
return view($this->template.'.index', $data);
|
|
}
|
|
|
|
public function save(Request $request, $sector, $code)
|
|
{
|
|
$request->validate([
|
|
'data' => 'required|array',
|
|
]);
|
|
|
|
// Filter by Year
|
|
$inventoryYear = $request->input('inventoryYear') ?? date('Y');
|
|
$instansi = request('instansi') ?? null;
|
|
if ($instansi && $instansi != 'all' && $instansi != 'none') {
|
|
$agency = Agency::where('name', $instansi)->rowActive()->first();
|
|
$instansi = $agency ? $agency->id : null;
|
|
} else {
|
|
$instansi = null;
|
|
}
|
|
|
|
$data = $request->input('data');
|
|
$unitCodes = $request->input('unit_code');
|
|
|
|
try {
|
|
$this->formService->save($sector, $code, $inventoryYear, $data, $unitCodes, $instansi);
|
|
return redirect()->back()->with('success', 'Data berhasil disimpan.');
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->withErrors(['error' => 'Data gagal disimpan. Mohon dicoba kembali.' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function convertUnit(Request $request)
|
|
{
|
|
$fromUnit = $request->get('from');
|
|
$toUnit = $request->get('to');
|
|
$value = $request->get('value');
|
|
|
|
$convertedValue = $this->formService->convertUnit($fromUnit, $toUnit, $value);
|
|
|
|
if ($convertedValue !== null) {
|
|
$output = getFormattedValue($convertedValue);
|
|
return response()->json([
|
|
'success' => true,
|
|
'convertedValue' => $output,
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Konversi tidak ditemukan',
|
|
]);
|
|
}
|
|
|
|
public function import(Request $request, $sector, $code, $inventoryYear, $instansi = null)
|
|
{
|
|
$request->validate([
|
|
'import_file' => 'required|file|mimes:xlsx,xls|max:2048', // Adjust max file size as needed
|
|
]);
|
|
|
|
try {
|
|
$formDetails = SettingFormDetail::where('form_code', $code)->rowActive()->orderByRowNum()->get();
|
|
|
|
if ($instansi && $instansi != 'all' && $instansi != 'none') {
|
|
$agency = Agency::where('name', $instansi)->rowActive()->first();
|
|
$instansi = $agency ? $agency->id : null;
|
|
} else {
|
|
$instansi = null;
|
|
}
|
|
|
|
Excel::import(new FormImport($this->formService, $sector, $code, $inventoryYear, $formDetails, $instansi), $request->file('import_file'));
|
|
|
|
return redirect()->back()->with('success', 'Data berhasil diimpor.');
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->withErrors(['error' => 'Data gagal diimpor. Mohon dicoba kembali.' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function export($sector, $code, $inventoryYear, $instansi = null)
|
|
{
|
|
$data = $this->data($sector, $code, $inventoryYear, $instansi);
|
|
$data['inventoryYear'] = $inventoryYear;
|
|
$data['instansi'] = $instansi;
|
|
|
|
$isTemplate = request('isTemplate') ?? false;
|
|
$data['isTemplate'] = $isTemplate;
|
|
|
|
$instansiTitle = ($instansi && $instansi != 'all' && $instansi != 'none') ? ' (' . $instansi . ')' : '';
|
|
$titleExcel = $data['form']->name . ' ' . $inventoryYear . $instansiTitle;
|
|
|
|
if ($isTemplate) {
|
|
$titleExcel = 'Template ' . $titleExcel;
|
|
} else {
|
|
$titleExcel = 'Data Aktivitas ' . $titleExcel;
|
|
}
|
|
|
|
$data['titleExcel'] = $titleExcel;
|
|
$fileName = $titleExcel . '.xlsx';
|
|
|
|
return Excel::download(new FormExport($data), $fileName);
|
|
}
|
|
|
|
public function getUserActivities(Request $request)
|
|
{
|
|
if ($request->ajax()) {
|
|
$data = [];
|
|
$formId = $request->query('form_id');
|
|
|
|
if ($formId !== null && $formId !== '0') {
|
|
// $data = LogUserActivity::with('user')->where('form_id', $formId)
|
|
// ->orderBy('created_at', 'desc');
|
|
$data = LogUserActivity::rowActive()->orderByRowNum()->where('form_id', $formId);
|
|
}
|
|
|
|
$result = datatables()->of($data)
|
|
->addIndexColumn()
|
|
->addColumn('user_name', function ($row) {
|
|
return $row->user->name;
|
|
})
|
|
->editColumn('activity_type', function ($row) {
|
|
return ActivityType::from($row->activity_type)->label();
|
|
})
|
|
->editColumn('created_at', function ($row) {
|
|
return $row->created_at->format('d-m-Y H:i:s');
|
|
})
|
|
->make(true);
|
|
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
public function lock($sector, $code, $id)
|
|
{
|
|
try {
|
|
$this->formService->lockForm($id);
|
|
return redirect()->back()->with('success', 'Data Aktivitas berhasil dikunci.');
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->withErrors(['error' => 'Data Aktivitas gagal dikunci. Mohon dicoba kembali.']);
|
|
}
|
|
}
|
|
|
|
public function unlock($sector, $code, $id)
|
|
{
|
|
try {
|
|
$this->formService->unlockForm($id);
|
|
return redirect()->back()->with('success', 'Data Aktivitas berhasil dibuka.');
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->withErrors(['error' => 'Data Aktivitas gagal dibuka. Mohon dicoba kembali.']);
|
|
}
|
|
}
|
|
|
|
public function copyData(Request $request, $sector, $code)
|
|
{
|
|
try {
|
|
$fromYear = $request->input('from_year');
|
|
$toYear = $request->input('to_year');
|
|
|
|
$this->formService->copyData($sector, $code, $fromYear, $toYear);
|
|
|
|
return redirect()->back()->with('success', 'Data Aktivitas berhasil disalin.');
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->withErrors(['error' => 'Data Aktivitas gagal disalin. Mohon dicoba kembali.']);
|
|
}
|
|
}
|
|
|
|
public function approvalKonsolidasi($sector, $code, $inventoryYear, $instansi = null)
|
|
{
|
|
try {
|
|
$data = $this->data($sector, $code, $inventoryYear, $instansi);
|
|
if ($instansi && $instansi != 'all' && $instansi != 'none') {
|
|
$agency = Agency::where('name', $instansi)->rowActive()->first();
|
|
$instansi = $agency ? $agency->id : null;
|
|
}
|
|
|
|
$form = $data['activityForm'];
|
|
$this->formService->approvalKonsolidasi($form, $sector, $code, $inventoryYear, $instansi);
|
|
|
|
return redirect()->back()->with('success', 'Persetujuan Data Produsen berhasil diproses.');
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->withErrors(['error' => 'Persetujuan Data Produsen gagal diproses. Mohon dicoba kembali.' . $e->getMessage()]);
|
|
}
|
|
}
|
|
}
|