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

278 lines
9.1 KiB
PHTML

<?php
namespace App\Http\Controllers;
use App\Imports\DocumentImport;
2 years ago
use App\Models\Category;
use App\Models\Company;
use App\Models\Document;
2 years ago
use App\Models\Type;
use App\Models\User;
2 years ago
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
2 years ago
use OpenSpout\Writer\Common\Creator\Style\StyleBuilder;
use Rap2hpoutre\FastExcel\FastExcel;
class DocumentController extends Controller
{
public function index(Request $request)
{
$query = Document::with(['variety', 'category', 'company']);
if ($request->has('status')) {
if($request->status == 1) {
2 years ago
$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 {
2 years ago
$query->orderBy('created_at', 'desc');
}
if ($request->q != null || $request->q != '') {
2 years ago
$query->where(function ($query) use ($request) {
$query->where('no_doc', 'like', '%'.$request->q.'%')
2 years ago
->orWhere('name', 'like', '%'.$request->q.'%')
2 years ago
->orWhere('no', 'like', '%'.$request->q.'%');
2 years ago
});
}
return inertia('Document/Index', [
'docs' => $query->paginate(10),
]);
}
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,
2 years ago
'types' => Type::all(),
'categories' => Category::all(),
]);
}
public function store(Request $request)
{
$request->validate([
2 years ago
"no_doc" => "nullable|string",
2 years ago
"name" => "required|string",
"type_id" => "required|exists:types,id",
"category_id" => "required|exists:categories,id",
"publisher" => "required|string",
"description" => "nullable",
2 years ago
"publish_date" => "nullable|date",
"due_date" => "required_if:type,0",
2 years ago
"status" => "required|in:0,1",
"type" => "required|in:0,1",
"company_id" => "required|exists:companies,id",
2 years ago
"document" => "required|file",
]);
2 years ago
if($request->type == Document::TYPE_TIDAK_TETAP) {
2 years ago
$request->validate([
"due_date" => "date|after_or_equal:".$request->publish_date
]);
}
2 years ago
if($request->status == Document::STATUS_YES) {
$request->validate([
"no_doc" => "required|string",
"publish_date" => "required|date",
]);
}
2 years ago
$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,
2 years ago
"user_id" => auth()->user()->id,
]);
2 years ago
$file = $request->file('document');
$file->store('documents', 'public');
2 years ago
$doc->document = $file->hashName();
2 years ago
$doc->save();
return redirect()->route("docs.index")
2 years ago
->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,
2 years ago
'types' => Type::all(),
'categories' => Category::all(),
'doc' => $doc->load(['variety', 'category'])
]);
}
public function update(Request $request, Document $doc)
{
$request->validate([
2 years ago
"no_doc" => "nullable|string",
"name" => "required|string",
"type_id" => "required|exists:types,id",
"category_id" => "required|exists:categories,id",
"publisher" => "required|string",
"description" => "nullable",
2 years ago
"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",
]);
2 years ago
if($request->type == Document::TYPE_TETAP) {
$request->validate([
"due_date" => "date|after_or_equal:".$request->publish_date
]);
}
2 years ago
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']),
2 years ago
'doc_url' => asset('documents/'.$doc->document),
]);
}
public function destroy(Document $doc)
{
$doc->delete();
}
2 years ago
public function import(Request $request)
2 years ago
{
$request->validate([
'file' => 'required|file'
]);
Excel::import(new DocumentImport, $request->file);
2 years ago
}
public function export(Request $request)
{
$query = Document::with(['variety', 'category', 'company.region.group'])->orderBy('created_at', 'desc');
$collections = collect([]);
foreach ($query->get() as $document) {
$collections->add([
'group' => $document->company->region->group->name,
'region' => $document->company->region->name,
'nama perusahaan' => $document->company->name,
'jenis' => $document->variety->name,
'kategori' => $document->category->name,
'no' => $document->no_doc,
'nama' => $document->name,
'penerbit' => $document->publisher,
'tipe' => $document->type == Document::TYPE_TETAP ? 'Tetap' : 'Tidak Tetap',
'tanggal terbit' => $document->publish_date->format('d-m-Y'),
'tanggal jatuh tempo' => $document->due_date->format('d-m-Y'),
'keterangan' => $document->description,
'file' => asset('documents/'.$document->document),
'status' => $document->status == Document::STATUS_YES ? 'Ya' : 'Tidak',
'catatan' => $document->due_status,
]);
}
if($request->type == 'pdf') {
return $this->exportAsPdf($collections);
}
if($request->type == 'excel') {
return $this->exportAsExcel($collections);
}
return $this->print($collections);
}
private function print($collections) {
return view('exports.documents', ['collections' => $collections->toArray()]);
2 years ago
}
private function exportAsPdf($collections)
{
$pdf = Pdf::setPaper('legal', 'landscape');
$pdf->loadView('exports.documents', ['collections' => $collections->toArray()]);
2 years ago
$date = now()->format('d-m-Y');
return $pdf->download("documents-$date.pdf");
}
private function exportAsExcel($collections)
{
$date = now()->format('d-m-Y');
$header_style = (new StyleBuilder())->setFontBold()->build();
return (new FastExcel($collections))
->headerStyle($header_style)
->download("document-$date.xlsx");
}
}