clara/app/Http/Controllers/DocumentController.php

180 lines
6.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\DataTables\DocumentDataTable;
use App\Http\Requests\StoreDocumentRequest;
use App\Http\Requests\UpdateDocumentRequest;
use App\Models\Directorat;
use App\Models\Document;
use App\Models\DocumentDetail;
use App\Models\DocumentType;
use App\Models\Job;
use App\Models\SpecialCode;
use App\Models\SubDirectorat;
use App\Models\SubJob;
use App\Models\SubSubJob;
use Exception;
use Illuminate\Http\Request;
use Spatie\Activitylog\Facades\CauserResolver;
class DocumentController extends Controller
{
public $user;
public function __construct()
{
$this->middleware(function ($request, $next) {
//$this->user = Auth::guard('web')->user();
return $next($request);
});
//CauserResolver::setCauser($this->user);
}
/**
* Display a listing of the resource.
*/
public function index(DocumentDataTable $dataTable)
{
/*if (is_null($this->user) || !$this->user->can('app.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
}*/
return $dataTable->render('pages.app.document.index');
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
/*if (is_null($this->user) || !$this->user->can('app.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}*/
addVendor('chained-select');
$directorat = Directorat::all();
$special_code = SpecialCode::all();
$document_type = DocumentType::all();
return view('pages.app.document.create', compact('directorat', 'special_code', 'document_type'));
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreDocumentRequest $request)
{
/*if (is_null($this->user) || !$this->user->can('app.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}*/
// Validate the request...
$validated = $request->validated();
// Store the Document...
if ($validated) {
$validated['sequence'] = 1;
try {
$created = Document::create($validated);
if ($created) {
$detail = [
'document_id' => $created->id,
'document_type_id' => $request->document_type_id,
'tanggal_upload' => date('Y-m-d'),
'tanggal_dokumen' => $request->tanggal_dokumen,
'nomor_dokumen' => $request->nomor_dokumen,
'perihal' => $request->perihal,
'kode_cabang' => $request->kode_cabang,
'jumlah_halaman' => $request->jumlah_halaman,
'custom_field_1' => $request->custom_field_1,
'custom_field_2' => $request->custom_field_2,
'custom_field_3' => $request->custom_field_3,
'custom_field_4' => $request->custom_field_4,
'nama_nasabah' => $request->nama_nasabah,
'no_rekening' => $request->no_rekening,
'no_cif' => $request->no_cif,
'group' => $request->group,
];
DocumentDetail::create($detail);
}
return redirect()->route('document.index')->with('success', 'Document created successfully.');
} catch (Exception $e) {
return redirect()->route('document.index')->with('error', 'Document created failed.');
}
}
return false;
}
/**
* Display the specified resource.
*/
public function show(Document $documents)
{
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$document = Document::find($id);
$directorat = Directorat::all();
$sub_directorat = SubDirectorat::where('directorat_id', $document->directorat_id)->get();
$job = Job::where('sub_directorat_id', $document->sub_directorat_id)->get();
$sub_job = SubJob::where('job_id', $document->job_id)->get();
$sub_sub_job = SubSubJob::where('sub_job_id', $document->sub_job_id)->get();
$special_code = SpecialCode::all();
$document_type = DocumentType::all();
return view('pages.app.document.edit', compact('document', 'directorat', 'sub_directorat', 'job', 'sub_job', 'sub_sub_job', 'special_code', 'document_type'));
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateDocumentRequest $request, Document $documents)
{
/*if (is_null($this->user) || !$this->user->can('app.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
}*/
// Validate the request...
$validated = $request->validated();
// Update the Document...
if ($validated) {
try {
$document = Document::find($request->id);
$update = $document->update($validated);
return redirect()->route('document.index')->with('success', 'Document updated successfully.');
} catch (Exception $e) {
return redirect()->route('document.index')->with('error', 'Document updated failed.');
}
}
return false;
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Request $request)
{
$documents = Document::find($request->document);
$documents->delete();
echo json_encode(['status' => 'success', 'message' => 'Document deleted successfully.']);
}
}