|
|
|
@ -3,8 +3,10 @@
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Mail\DocumentShare;
|
|
|
|
|
use App\Models\Category;
|
|
|
|
|
use App\Models\Department;
|
|
|
|
|
use App\Models\Document;
|
|
|
|
|
use App\Models\Type;
|
|
|
|
|
use App\Models\TypeDoc;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
@ -17,268 +19,113 @@ class DocumentController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$query = Document::with(['department', 'type', 'shares']);
|
|
|
|
|
$query = Document::with(['type', 'category']);
|
|
|
|
|
|
|
|
|
|
if ($request->has('sortBy') && $request->has('sortRule')) {
|
|
|
|
|
$query->orderBy($request->sortBy, $request->sortRule);
|
|
|
|
|
} else {
|
|
|
|
|
$query->orderBy('created_at');
|
|
|
|
|
$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('company_name', 'like', '%'.$request->q.'%')
|
|
|
|
|
->orWhere('pic_name', 'like', '%'.$request->q.'%')
|
|
|
|
|
->orWhere('name', 'like', '%'.$request->q.'%')
|
|
|
|
|
->orWhere('email', 'like', '%'.$request->q.'%');
|
|
|
|
|
->orWhere('company_name', 'like', '%'.$request->q.'%')
|
|
|
|
|
->orWhere('no', 'like', '%'.$request->q.'%');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($request->department_id != '') {
|
|
|
|
|
$query->where('department_id', $request->department_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($request->status != '') {
|
|
|
|
|
$query->where('status', $request->status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($request->type_doc_id != '') {
|
|
|
|
|
$query->where('type_doc_id', $request->type_doc_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return inertia('Document/Index', [
|
|
|
|
|
'docs' => $query->paginate(10),
|
|
|
|
|
'types' => TypeDoc::all(),
|
|
|
|
|
'departments' => Department::all(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
|
|
|
|
return inertia('Document/Form', [
|
|
|
|
|
'types' => TypeDoc::all(),
|
|
|
|
|
'departments' => Department::all(),
|
|
|
|
|
'types' => Type::all(),
|
|
|
|
|
'categories' => Category::all(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'no_doc' => 'required|string',
|
|
|
|
|
'company_name' => 'required|string',
|
|
|
|
|
'first_person_name' => 'required|string',
|
|
|
|
|
'second_person_name' => 'required|string',
|
|
|
|
|
'start_date' => 'required|date',
|
|
|
|
|
'end_date' => 'required|date|after_or_equal:'.$request->start_date,
|
|
|
|
|
'type_doc_id' => 'required|exists:type_docs,id',
|
|
|
|
|
'department_id' => 'required|exists:departments,id',
|
|
|
|
|
'pic_name' => 'required|string',
|
|
|
|
|
'email' => 'required|email',
|
|
|
|
|
'document' => 'required|file',
|
|
|
|
|
'note' => 'nullable',
|
|
|
|
|
'status' => 'required|numeric',
|
|
|
|
|
'reminders' => 'nullable|array',
|
|
|
|
|
'name' => 'required|string'
|
|
|
|
|
"no_doc" => "required|string",
|
|
|
|
|
"name" => "required|string",
|
|
|
|
|
"company_name" => "required|string",
|
|
|
|
|
"type_id" => "required|exists:types,id",
|
|
|
|
|
"category_id" => "required|exists:categories,id",
|
|
|
|
|
"publisher" => "required|string",
|
|
|
|
|
"description" => "nullable",
|
|
|
|
|
"publish_date" => "required|date",
|
|
|
|
|
"due_date" => "required_if:type,1",
|
|
|
|
|
"status" => "required|in:0,1",
|
|
|
|
|
"type" => "required|in:0,1",
|
|
|
|
|
"group" => "required|string",
|
|
|
|
|
"region" => "required|string",
|
|
|
|
|
"document" => "required|file",
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$lastDocs = Document::orderBy('created_at', 'desc')->first();
|
|
|
|
|
$lastDocs = $lastDocs ? $lastDocs : Document::make(['no' => 0]);
|
|
|
|
|
$docs = Document::make([
|
|
|
|
|
'no' => $lastDocs->no + 1,
|
|
|
|
|
'name' => $request->name,
|
|
|
|
|
'no_doc' => $request->no_doc,
|
|
|
|
|
'company_name' => $request->company_name,
|
|
|
|
|
'first_person_name' => $request->first_person_name,
|
|
|
|
|
'second_person_name' => $request->second_person_name,
|
|
|
|
|
'start_date' => $request->start_date,
|
|
|
|
|
'end_date' => $request->end_date,
|
|
|
|
|
'pic_name' => $request->pic_name,
|
|
|
|
|
'email' => $request->email,
|
|
|
|
|
'note' => $request->note,
|
|
|
|
|
'type_doc_id' => $request->type_doc_id,
|
|
|
|
|
'department_id' => $request->department_id,
|
|
|
|
|
'status' => Document::ACTIVE, //DOCUMENT CREATED ALWAYS ACTIVE
|
|
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
|
if($request->publish_date == Document::TYPE_TETAP) {
|
|
|
|
|
$request->validate([
|
|
|
|
|
"due_date" => "date|after_or_equal:".$request->publish_date
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$doc = Document::make([
|
|
|
|
|
"no" => Document::count() + 1,
|
|
|
|
|
"no_doc" => $request->no_doc,
|
|
|
|
|
"name" => $request->name,
|
|
|
|
|
"company_name" => $request->company_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,
|
|
|
|
|
"group" => $request->group,
|
|
|
|
|
"region" => $request->region,
|
|
|
|
|
"user_id" => auth()->user()->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$file = $request->file('document');
|
|
|
|
|
$file->store('documents', 'public');
|
|
|
|
|
$docs->document = $file->hashName();
|
|
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
$docs->save();
|
|
|
|
|
$doc->document = $file->hashName();
|
|
|
|
|
|
|
|
|
|
if ($request->has('reminders')) {
|
|
|
|
|
foreach ($request->reminders as $reminder) {
|
|
|
|
|
$docs->reminders()->updateOrCreate(['date' => $reminder]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DB::commit();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('docs.index')
|
|
|
|
|
->with('message', ['type' => 'success', 'message' => 'The data has beed saved']);
|
|
|
|
|
$doc->save();
|
|
|
|
|
|
|
|
|
|
return redirect("docs.index")
|
|
|
|
|
->with('message', ['type' => 'success', 'message' => 'The data has beed saved']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit(Document $doc)
|
|
|
|
|
{
|
|
|
|
|
return inertia('Document/Form', [
|
|
|
|
|
'types' => TypeDoc::all(),
|
|
|
|
|
'departments' => Department::all(),
|
|
|
|
|
'doc' => $doc->load(['reminders'])
|
|
|
|
|
'types' => Type::all(),
|
|
|
|
|
'categories' => Category::all(),
|
|
|
|
|
'doc' => $doc->load(['type', 'category'])
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(Request $request, Document $doc)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'no_doc' => 'required|string',
|
|
|
|
|
'company_name' => 'required|string',
|
|
|
|
|
'first_person_name' => 'required|string',
|
|
|
|
|
'second_person_name' => 'required|string',
|
|
|
|
|
'start_date' => 'required|date',
|
|
|
|
|
'end_date' => 'required|date|after_or_equal:'.$request->start_date,
|
|
|
|
|
'type_doc_id' => 'required|exists:type_docs,id',
|
|
|
|
|
'department_id' => 'required|exists:departments,id',
|
|
|
|
|
'pic_name' => 'required|string',
|
|
|
|
|
'email' => 'required|email',
|
|
|
|
|
'document' => 'nullable|file',
|
|
|
|
|
'note' => 'nullable',
|
|
|
|
|
'name' => 'required|string',
|
|
|
|
|
'status' => 'required|numeric',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$doc->fill([
|
|
|
|
|
'no_doc' => $request->no_doc,
|
|
|
|
|
'name' => $request->name,
|
|
|
|
|
'company_name' => $request->company_name,
|
|
|
|
|
'first_person_name' => $request->first_person_name,
|
|
|
|
|
'second_person_name' => $request->second_person_name,
|
|
|
|
|
'start_date' => $request->start_date,
|
|
|
|
|
'end_date' => $request->end_date,
|
|
|
|
|
'pic_name' => $request->pic_name,
|
|
|
|
|
'email' => $request->email,
|
|
|
|
|
'note' => $request->note,
|
|
|
|
|
'type_doc_id' => $request->type_doc_id,
|
|
|
|
|
'department_id' => $request->department_id,
|
|
|
|
|
'status' => Document::UPDATE // DOCUEMENT UPDATED IS ALWAYS UPDATE
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$file = $request->file('document');
|
|
|
|
|
if ($file != null) {
|
|
|
|
|
$file->store('documents', 'public');
|
|
|
|
|
$doc->document = $file->hashName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
$doc->save();
|
|
|
|
|
|
|
|
|
|
$doc->reminders()->delete();
|
|
|
|
|
if ($request->has('reminders')) {
|
|
|
|
|
foreach ($request->reminders as $reminder) {
|
|
|
|
|
$doc->reminders()->updateOrCreate(['date' => $reminder]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DB::commit();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('docs.index')
|
|
|
|
|
->with('message', ['type' => 'success', 'message' => 'The data has beed saved']);
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show(Document $doc)
|
|
|
|
|
{
|
|
|
|
|
return inertia('Document/Detail', [
|
|
|
|
|
'doc' => $doc->load(['department', 'type', 'creator', 'reminders', 'shares']),
|
|
|
|
|
'doc' => $doc->load(['type', 'category']),
|
|
|
|
|
'doc_url' => asset('documents/'.$doc->document),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function export(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$query = Document::with(['department', 'type', 'creator'])->orderBy('created_at');
|
|
|
|
|
|
|
|
|
|
if ($request->q != null || $request->q != '') {
|
|
|
|
|
$query->where(function ($query) use ($request) {
|
|
|
|
|
$query->where('no_doc', 'like', '%'.$request->q.'%')
|
|
|
|
|
->orWhere('company_name', 'like', '%'.$request->q.'%')
|
|
|
|
|
->orWhere('pic_name', 'like', '%'.$request->q.'%')
|
|
|
|
|
->orWhere('email', 'like', '%'.$request->q.'%');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($request->department_id != '') {
|
|
|
|
|
$query->where('department_id', $request->department_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($request->status != '') {
|
|
|
|
|
$query->where('status', $request->status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($request->type_doc_id != '') {
|
|
|
|
|
$query->where('type_doc_id', $request->type_doc_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$collections = collect([]);
|
|
|
|
|
foreach ($query->get() as $document) {
|
|
|
|
|
$collections->add([
|
|
|
|
|
'no dokumen' => $document->no_doc,
|
|
|
|
|
'nama' => $document->name,
|
|
|
|
|
'jenis dokumen' => $document->type->name,
|
|
|
|
|
'nama perusahaan' => $document->company_name,
|
|
|
|
|
'nama pihak pertama' => $document->first_person_name,
|
|
|
|
|
'nama pihak kedua' => $document->second_person_name,
|
|
|
|
|
'tanggal mulai' => $document->start_date,
|
|
|
|
|
'tanggal selesai' => $document->end_date,
|
|
|
|
|
'department' => $document->department->name,
|
|
|
|
|
'nama pic' => $document->pic_name,
|
|
|
|
|
'email' => $document->email,
|
|
|
|
|
'catata' => $document->note,
|
|
|
|
|
'status' => $document->status,
|
|
|
|
|
'user_creator' => $document->creator->name,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$date = now()->format('d-m-y');
|
|
|
|
|
$header_style = (new StyleBuilder())->setFontBold()->build();
|
|
|
|
|
|
|
|
|
|
return (new FastExcel($collections))
|
|
|
|
|
->headerStyle($header_style)
|
|
|
|
|
->download("documents-$date.xlsx");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function share(Request $request, Document $doc)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'shares' => 'array',
|
|
|
|
|
'shares.*.share_to' => 'required|email'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
|
|
|
|
|
$doc->shares()->delete();
|
|
|
|
|
|
|
|
|
|
foreach ($request->shares as $share) {
|
|
|
|
|
$user = User::where('email', $share['share_to'])->first();
|
|
|
|
|
if ($user != null) {
|
|
|
|
|
$doc->shares()->updateOrCreate(['user_id' => $user->id, 'share_to' => $share['share_to']]);
|
|
|
|
|
} else {
|
|
|
|
|
$doc->shares()->updateOrCreate(['share_to' => $share['share_to']]);
|
|
|
|
|
}
|
|
|
|
|
Mail::to($share['share_to'])->queue(new DocumentShare($doc));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('docs.index')
|
|
|
|
|
->with('message', ['type' => 'success', 'message' => 'Document success shared']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function destroy(Document $doc)
|
|
|
|
|
{
|
|
|
|
|
$doc->shares()->delete();
|
|
|
|
|
$doc->reminders()->delete();
|
|
|
|
|
$doc->delete();
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|