You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
monitor-doc/app/Http/Controllers/DocumentController.php

209 lines
6.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Company;
use App\Models\Document;
use App\Models\Type;
use App\Models\User;
use Illuminate\Http\Request;
class DocumentController extends Controller
{
public function index(Request $request)
{
$query = Document::with(['variety', 'category']);
if ($request->has('status')) {
if($request->status == 1) {
$query->whereDate('due_date', '<', now()->toDateString());
}
if($request->status == 2) {
$query->closeToExpired();
}
}
if ($request->has('sortBy') && $request->has('sortRule')) {
$query->orderBy($request->sortBy, $request->sortRule);
} else {
$query->orderBy('created_at', 'desc');
}
if ($request->q != null || $request->q != '') {
$query->where(function ($query) use ($request) {
$query->where('no_doc', 'like', '%'.$request->q.'%')
->orWhere('name', 'like', '%'.$request->q.'%')
->orWhere('company_name', 'like', '%'.$request->q.'%')
->orWhere('no', 'like', '%'.$request->q.'%');
});
}
return inertia('Document/Index', [
'docs' => $query->paginate(10),
'now' => now()->timezone('UTC'),
'now2' => now()->toDateTimeString(),
]);
}
public function create()
{
$user = User::find(auth()->user()->id);
if ($user->is_admin) {
$companies = Company::all();
} else {
$companies = Company::where('region_id', $user->region_id)->get();
}
return inertia('Document/Form', [
'companies' => $companies,
'types' => Type::all(),
'categories' => Category::all(),
]);
}
public function store(Request $request)
{
$request->validate([
"no_doc" => "nullable|string",
"name" => "required|string",
"type_id" => "required|exists:types,id",
"category_id" => "required|exists:categories,id",
"publisher" => "required|string",
"description" => "nullable",
"publish_date" => "nullable|date",
"due_date" => "required_if:type,0",
"status" => "required|in:0,1",
"type" => "required|in:0,1",
"company_id" => "required|exists:companies,id",
"document" => "required|file",
]);
if($request->type == Document::TYPE_TIDAK_TETAP) {
$request->validate([
"due_date" => "date|after_or_equal:".$request->publish_date
]);
}
if($request->status == Document::STATUS_YES) {
$request->validate([
"no_doc" => "required|string",
"publish_date" => "required|date",
]);
}
$doc = Document::make([
"no" => Document::count() + 1,
"no_doc" => $request->no_doc,
"name" => $request->name,
"type_id" => $request->type_id,
"category_id" => $request->category_id,
"publisher" => $request->publisher,
"description" => $request->description,
"publish_date" => $request->publish_date,
"due_date" => $request->due_date,
"status" => $request->status,
"type" => $request->type,
"company_id" => $request->company_id,
"user_id" => auth()->user()->id,
]);
$file = $request->file('document');
$file->store('documents', 'public');
$doc->document = $file->hashName();
$doc->save();
return redirect()->route("docs.index")
->with('message', ['type' => 'success', 'message' => 'The data has beed saved']);
}
public function edit(Document $doc)
{
$user = User::find(auth()->user()->id);
if ($user->is_admin) {
$companies = Company::all();
} else {
$companies = Company::where('region_id', $user->region_id)->get();
}
return inertia('Document/Form', [
'companies' => $companies,
'types' => Type::all(),
'categories' => Category::all(),
'doc' => $doc->load(['variety', 'category'])
]);
}
public function update(Request $request, Document $doc)
{
$request->validate([
"no_doc" => "nullable|string",
"name" => "required|string",
"type_id" => "required|exists:types,id",
"category_id" => "required|exists:categories,id",
"publisher" => "required|string",
"description" => "nullable",
"publish_date" => "nullable|date",
"due_date" => "required_if:type,1",
"status" => "required|in:0,1",
"type" => "required|in:0,1",
"company_id" => "required|exists:companies,id",
"document" => "nullable|file",
]);
if($request->type == Document::TYPE_TETAP) {
$request->validate([
"due_date" => "date|after_or_equal:".$request->publish_date
]);
}
if($request->status == Document::STATUS_YES) {
$request->validate([
"no_doc" => "required|string",
"publish_date" => "required|date",
]);
}
$doc->fill([
"no" => Document::count() + 1,
"no_doc" => $request->no_doc,
"name" => $request->name,
"type_id" => $request->type_id,
"category_id" => $request->category_id,
"publisher" => $request->publisher,
"description" => $request->description,
"publish_date" => $request->publish_date,
"due_date" => $request->due_date,
"status" => $request->status,
"type" => $request->type,
"company_id" => $request->company_id,
"user_id" => auth()->user()->id,
]);
if ($request->hasFile('document')) {
$file = $request->file('document');
$file->store('documents', 'public');
$doc->document = $file->hashName();
}
$doc->save();
return redirect()->route("docs.index")
->with('message', ['type' => 'success', 'message' => 'The data has beed updated']);
}
public function show(Document $doc)
{
return inertia('Document/Detail', [
'doc' => $doc->load(['variety', 'category', 'company.region.group']),
'doc_url' => asset('documents/'.$doc->document),
]);
}
public function destroy(Document $doc)
{
$doc->delete();
}
}