345 lines
12 KiB
PHP
345 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Dashboard;
|
|
|
|
use App\Exports\ReportExport;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\ReportRequest;
|
|
use App\Models\Announcement;
|
|
use App\Models\Media;
|
|
use App\Models\Report;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class ReportController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
return view('dashboard.reports.index', [
|
|
'title' => 'Laporan Bukti Tayang',
|
|
'mediaCollection' => Media::all(),
|
|
'media' => $request->input('media', []),
|
|
'status' => $request->input('status', null),
|
|
'date' => $request->input('date', null),
|
|
'from_month' => $request->input('from_month', null),
|
|
'to_month' => $request->input('to_month', null),
|
|
'year' => $request->input('year', null),
|
|
]);
|
|
}
|
|
|
|
public function create($id)
|
|
{
|
|
return view('dashboard.reports.create', [
|
|
'title' => 'Buat Laporan',
|
|
'mediaOrderId' => $id,
|
|
]);
|
|
}
|
|
|
|
public function store(ReportRequest $request, $id)
|
|
{
|
|
$validatedData = $request->validated();
|
|
|
|
try {
|
|
$validatedData['media_id'] = Auth::user()->company->media->id;
|
|
$validatedData['order_id'] = decrypt_id($id);
|
|
$validatedData['proof'] = store_file($validatedData['proof'], '/uploads/reports')['randomFileName'];
|
|
|
|
Report::create($validatedData);
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->with('failed-status', 'Gagal menambahkan laporan.');
|
|
}
|
|
|
|
return redirect()->route('dashboard.cooperations.process', ['id' => $id, 'process' => 'reports'])->with('success-status', 'Berhasil menambahkan laporan.');
|
|
}
|
|
|
|
public function edit($mediaOrderId, $id)
|
|
{
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
return view('dashboard.reports.edit', [
|
|
'title' => 'Buat Laporan',
|
|
'report' => $report,
|
|
'mediaOrderId' => $mediaOrderId,
|
|
]);
|
|
}
|
|
|
|
public function update(ReportRequest $request, $mediaOrderId, $id)
|
|
{
|
|
$validatedData = $request->validated();
|
|
$validatedData['media_id'] = Auth::user()->company->media->id;
|
|
$validatedData['order_id'] = decrypt_id($mediaOrderId);
|
|
$validatedData['status'] = '0';
|
|
|
|
try {
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
if ($request->hasFile('image')) {
|
|
$validatedData['proof'] = store_file($validatedData['proof'], '/uploads/reports')['randomFileName'];
|
|
|
|
delete_file('app/public/uploads/reports/'.$report->proof);
|
|
} else {
|
|
$validatedData['proof'] = $report->proof;
|
|
}
|
|
|
|
$updatedReport = $report->update($validatedData);
|
|
|
|
if ($updatedReport) {
|
|
return redirect()->back()->with('success-status', 'Berhasil mengubah laporan.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal mengubah laporan.');
|
|
}
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->with('failed-status', 'Gagal mengubah laporan.');
|
|
}
|
|
}
|
|
|
|
public function show($mediaOrderId, $id)
|
|
{
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
return view('dashboard.reports.show', [
|
|
'title' => 'Detail Laporan',
|
|
'report' => $report,
|
|
'mediaOrderId' => $mediaOrderId,
|
|
]);
|
|
}
|
|
|
|
public function showIndex($id)
|
|
{
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
return view('dashboard.reports.index.show', [
|
|
'title' => 'Detail Penyelesaian Kerja Sama',
|
|
'report' => $report,
|
|
]);
|
|
}
|
|
|
|
public function processIndex($id)
|
|
{
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
return view('dashboard.reports.index.process', [
|
|
'title' => 'Detail Penyelesaian Kerja Sama',
|
|
'report' => $report,
|
|
]);
|
|
}
|
|
|
|
public function accept($id)
|
|
{
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
$acceptedReport = $report->update([
|
|
'status' => '1',
|
|
]);
|
|
|
|
// Announcements
|
|
$announcementData = [
|
|
'headline' => 'Laporan bukti tayang kerja sama <u>'.$report->order->cooperation->name.'</u> telah <span class="text-primary">disetujui</span>',
|
|
'content' => '<p>Admin telah menyetujui laporan bukti tayang Anda.</p>',
|
|
'enhancer_id' => Auth::id(),
|
|
];
|
|
$createdAnnouncement = Announcement::create($announcementData);
|
|
$createdAnnouncement->users()->attach($report->media->company->user->id, ['category' => 'system']);
|
|
// End Announcements
|
|
|
|
if ($acceptedReport) {
|
|
return redirect()->back()->with('success-status', 'Berhasil menyetujui laporan bukti tayang.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal menyetujui laporan bukti tayang');
|
|
}
|
|
}
|
|
|
|
public function reject($mediaOrderId, $id)
|
|
{
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
return view('dashboard.reports.reject', [
|
|
'title' => 'Tolak Laporan',
|
|
'report' => $report,
|
|
'mediaOrderId' => $mediaOrderId,
|
|
]);
|
|
}
|
|
|
|
public function rejectAction(Request $request, $mediaOrderId, $id)
|
|
{
|
|
$validatedData = $request->validate([
|
|
'rejection_reason' => ['required'],
|
|
], [
|
|
'rejection_reason.required' => 'Alasan penolakan wajib diisi.',
|
|
]);
|
|
|
|
try {
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
$rejectedReport = $report->update([
|
|
'status' => '2',
|
|
'rejection_reason' => $validatedData['rejection_reason'],
|
|
]);
|
|
|
|
// Announcements
|
|
$announcementData = [
|
|
'headline' => 'Laporan bukti tayang kerja sama <u>'.$report->order->cooperation->name.'</u> telah <span class="text-danger">ditolak</span>',
|
|
'content' => '<p>Admin telah menolak laporan bukti tayang Anda.</p>',
|
|
'enhancer_id' => Auth::id(),
|
|
];
|
|
$createdAnnouncement = Announcement::create($announcementData);
|
|
$createdAnnouncement->users()->attach($report->media->company->user->id, ['category' => 'system']);
|
|
// End Announcements
|
|
|
|
if ($rejectedReport) {
|
|
return redirect()->back()->with('success-status', 'Berhasil menolak laporan bukti tayang.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal menolak laporan bukti tayang');
|
|
}
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->with('failed-status', 'Gagal menolak laporan bukti tayang');
|
|
}
|
|
}
|
|
|
|
public function rejectActionIndex(Request $request, $id)
|
|
{
|
|
$validatedData = $request->validate([
|
|
'rejection_reason' => ['required'],
|
|
], [
|
|
'rejection_reason.required' => 'Alasan penolakan wajib diisi.',
|
|
]);
|
|
|
|
try {
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
|
|
$rejectedReport = $report->update([
|
|
'status' => '2',
|
|
'rejection_reason' => $validatedData['rejection_reason'],
|
|
]);
|
|
|
|
// Announcements
|
|
$announcementData = [
|
|
'headline' => 'Laporan bukti tayang kerja sama <u>'.$report->order->cooperation->name.'</u> telah <span class="text-danger">ditolak</span>',
|
|
'content' => '<p>Admin telah menolak laporan bukti tayang Anda.</p>',
|
|
'enhancer_id' => Auth::id(),
|
|
];
|
|
$createdAnnouncement = Announcement::create($announcementData);
|
|
$createdAnnouncement->users()->attach($report->media->company->user->id, ['category' => 'system']);
|
|
// End Announcements
|
|
|
|
if ($rejectedReport) {
|
|
return redirect()->back()->with('success-status', 'Berhasil menolak laporan bukti tayang.');
|
|
} else {
|
|
return redirect()->back()->with('failed-status', 'Gagal menolak laporan bukti tayang');
|
|
}
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->with('failed-status', 'Gagal menolak laporan bukti tayang');
|
|
}
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$report = Report::findOrFail(decrypt_id($id));
|
|
delete_file('app/public/uploads/reports/'.$report->proof);
|
|
$report->forceDelete();
|
|
|
|
return response()->json(['message' => 'Berhasil menghapus laporan bukti tayang.']);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['message' => 'Gagal menghapus laporan bukti tayang.'], 500);
|
|
}
|
|
}
|
|
|
|
public function checkLink(Request $request)
|
|
{
|
|
$linkExists = Report::where('link', $request->link)
|
|
->where('media_id', Auth::user()->company->media->id)
|
|
->exists();
|
|
|
|
return response()->json([
|
|
'exists' => $linkExists,
|
|
]);
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
try {
|
|
$export_type = $request->input('export_type', 'excel');
|
|
|
|
$media = $request->input('media', null);
|
|
$status = $request->input('status', null);
|
|
$year = $request->input('year', null);
|
|
$from_month = $request->input('from_month', null);
|
|
$to_month = $request->input('to_month', null);
|
|
$date = $request->input('date', null);
|
|
|
|
if ($export_type === 'excel') {
|
|
$formattedNow = Carbon::parse(now())->locale('id')->translatedFormat('d-m-Y');
|
|
|
|
return Excel::download(new ReportExport($media, $status, $year, $from_month, $to_month, $date), 'reports-'.$formattedNow.'.xlsx');
|
|
} elseif ($export_type === 'pdf') {
|
|
if (is_role(['1', '5'])) {
|
|
$query = Report::with(['media.company', 'order.cooperation']);
|
|
} elseif (is_role(['3'])) {
|
|
$mediaId = Auth::user()->company->media->id;
|
|
$query = Report::with(['media.company', 'order.cooperation'])->where('media_id', '=', $mediaId);
|
|
}
|
|
|
|
if (! empty($media) && is_array($media)) {
|
|
$query->whereIn('media_id', $media);
|
|
}
|
|
|
|
if ($status) {
|
|
switch ($status) {
|
|
case 'waiting':
|
|
$query->where('status', '0');
|
|
break;
|
|
case 'accepted':
|
|
$query->where('status', '1');
|
|
break;
|
|
case 'rejected':
|
|
$query->where('status', '2');
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($year) {
|
|
$query->whereYear('created_at', $year);
|
|
}
|
|
|
|
if ($from_month && $to_month) {
|
|
$start = Carbon::parse($from_month)->startOfMonth();
|
|
$end = Carbon::parse($to_month)->endOfMonth();
|
|
|
|
$query->whereBetween('created_at', [$start, $end]);
|
|
}
|
|
|
|
if ($date) {
|
|
$query->whereDate('created_at', $date);
|
|
}
|
|
|
|
$reports = $query->get();
|
|
|
|
$pdf = Pdf::loadView('dashboard.reports.prints.collection', compact('reports'));
|
|
|
|
return $pdf->stream(slugify('laporan bukti tayang ').'.pdf');
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->with('failed-status', 'Gagal melakukan proses export data.');
|
|
}
|
|
|
|
}
|
|
|
|
public function singlePrint()
|
|
{
|
|
try {
|
|
$report = Report::with(['media.company', 'order.cooperation'])->first();
|
|
|
|
$pdf = Pdf::loadView('dashboard.reports.prints.single', compact('report'));
|
|
|
|
return $pdf->stream(slugify('laporan bukti tayang '.$report->title.' '.$report->media->name).'.pdf');
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->with('failed-status', 'Gagal melakukan proses print data.');
|
|
}
|
|
}
|
|
}
|