260 lines
9.8 KiB
PHP
260 lines
9.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Barang;
|
|
use App\Models\Pesanan;
|
|
use App\Models\Transaksi;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\Facades\Response;
|
|
|
|
class LaporanController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->middleware(\App\Http\Middleware\CheckRole::class . ':admin');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$totalPendapatanBulanIni = Transaksi::where('status', 'completed')
|
|
->whereMonth('created_at', Carbon::now()->month)
|
|
->whereYear('created_at', Carbon::now()->year)
|
|
->sum('total_pembayaran');
|
|
|
|
$totalPendapatanTahunIni = Transaksi::where('status', 'completed')
|
|
->whereYear('created_at', Carbon::now()->year)
|
|
->sum('total_pembayaran');
|
|
|
|
$totalPesananBulanIni = Pesanan::whereMonth('created_at', Carbon::now()->month)
|
|
->whereYear('created_at', Carbon::now()->year)
|
|
->count();
|
|
|
|
$totalPelanggan = User::where('role', 'customer')->count();
|
|
|
|
return view('admin.laporan.index', compact(
|
|
'totalPendapatanBulanIni',
|
|
'totalPendapatanTahunIni',
|
|
'totalPesananBulanIni',
|
|
'totalPelanggan'
|
|
));
|
|
}
|
|
|
|
public function generate(Request $request)
|
|
{
|
|
$jenis = $request->input('jenis', 'transaksi');
|
|
$periode = $request->input('periode', 'bulan-ini');
|
|
$tanggalMulai = $request->has('tanggal_mulai') ? $request->input('tanggal_mulai') : null;
|
|
$tanggalSelesai = $request->has('tanggal_selesai') ? $request->input('tanggal_selesai') : null;
|
|
|
|
// Menentukan range tanggal berdasarkan periode
|
|
switch ($periode) {
|
|
case 'hari-ini':
|
|
$startDate = Carbon::today();
|
|
$endDate = Carbon::today()->endOfDay();
|
|
break;
|
|
case 'minggu-ini':
|
|
$startDate = Carbon::now()->startOfWeek();
|
|
$endDate = Carbon::now()->endOfWeek();
|
|
break;
|
|
case 'bulan-ini':
|
|
$startDate = Carbon::now()->startOfMonth();
|
|
$endDate = Carbon::now()->endOfMonth();
|
|
break;
|
|
case 'custom':
|
|
$startDate = $tanggalMulai ? Carbon::parse($tanggalMulai) : Carbon::now()->startOfMonth();
|
|
$endDate = $tanggalSelesai ? Carbon::parse($tanggalSelesai)->endOfDay() : Carbon::now()->endOfMonth();
|
|
break;
|
|
default:
|
|
$startDate = Carbon::now()->startOfMonth();
|
|
$endDate = Carbon::now()->endOfMonth();
|
|
}
|
|
|
|
if ($jenis == 'transaksi') {
|
|
$data = Transaksi::with(['user', 'pesanan.barang'])
|
|
->whereBetween('created_at', [$startDate, $endDate])
|
|
->where('status', 'dibayar')
|
|
->latest()
|
|
->get();
|
|
|
|
$totalPendapatan = $data->where('status', 'dibayar')->sum('total_pembayaran');
|
|
$totalTransaksi = $data->count();
|
|
$statusTransaksi = [
|
|
'pending' => $data->where('status', 'pending')->count(),
|
|
'paid' => $data->where('status', 'paid')->count(),
|
|
'completed' => $data->where('status', 'completed')->count(),
|
|
'cancelled' => $data->where('status', 'cancelled')->count(),
|
|
];
|
|
|
|
return view('admin.laporan.generate', compact(
|
|
'data',
|
|
'jenis',
|
|
'periode',
|
|
'startDate',
|
|
'endDate',
|
|
'totalPendapatan',
|
|
'totalTransaksi',
|
|
'statusTransaksi'
|
|
));
|
|
} else {
|
|
// Laporan Barang
|
|
$data = Barang::withCount(['pesanan' => function ($query) use ($startDate, $endDate) {
|
|
$query->whereBetween('created_at', [$startDate, $endDate])
|
|
->where('status', '!=', 'cancelled');
|
|
}])
|
|
->orderByDesc('pesanan_count')
|
|
->get();
|
|
|
|
$totalBarang = $data->count();
|
|
$totalTerjual = $data->sum('pesanan_count');
|
|
$totalNilai = $data->sum(function($item) {
|
|
return $item->pesanan_count * $item->harga;
|
|
});
|
|
|
|
return view('admin.laporan.generate', compact(
|
|
'data',
|
|
'jenis',
|
|
'periode',
|
|
'startDate',
|
|
'endDate',
|
|
'totalBarang',
|
|
'totalTerjual',
|
|
'totalNilai'
|
|
));
|
|
}
|
|
}
|
|
|
|
public function penjualan(Request $request)
|
|
{
|
|
$tahun = $request->input('tahun', Carbon::now()->year);
|
|
$bulan = $request->input('bulan', Carbon::now()->month);
|
|
|
|
$transaksi = Transaksi::where('status', 'completed')
|
|
->when($bulan !== 'all', function ($query) use ($bulan) {
|
|
return $query->whereMonth('created_at', $bulan);
|
|
})
|
|
->when($tahun, function ($query) use ($tahun) {
|
|
return $query->whereYear('created_at', $tahun);
|
|
})
|
|
->with(['user', 'pesanan'])
|
|
->latest()
|
|
->get();
|
|
|
|
$pendapatanPerHari = Transaksi::where('status', 'completed')
|
|
->when($bulan !== 'all', function ($query) use ($bulan) {
|
|
return $query->whereMonth('created_at', $bulan);
|
|
})
|
|
->when($tahun, function ($query) use ($tahun) {
|
|
return $query->whereYear('created_at', $tahun);
|
|
})
|
|
->select(DB::raw('DATE(created_at) as tanggal'), DB::raw('SUM(total_pembayaran) as total'))
|
|
->groupBy('tanggal')
|
|
->orderBy('tanggal')
|
|
->get();
|
|
|
|
return view('admin.laporan.penjualan', compact('transaksi', 'pendapatanPerHari', 'tahun', 'bulan'));
|
|
}
|
|
|
|
public function stok()
|
|
{
|
|
$barang = Barang::orderBy('stok')->get();
|
|
|
|
$kategoriCount = Barang::select('kategori', DB::raw('count(*) as total'))
|
|
->groupBy('kategori')
|
|
->orderBy('total', 'desc')
|
|
->get();
|
|
|
|
return view('admin.laporan.stok', compact('barang', 'kategoriCount'));
|
|
}
|
|
|
|
public function transaksi(Request $request)
|
|
{
|
|
$status = $request->input('status', 'all');
|
|
$startDate = $request->input('start_date');
|
|
$endDate = $request->input('end_date');
|
|
|
|
$transaksi = Transaksi::with(['user', 'pesanan'])
|
|
->when($status !== 'all', function ($query) use ($status) {
|
|
return $query->where('status', $status);
|
|
})
|
|
->when($startDate, function ($query) use ($startDate) {
|
|
return $query->whereDate('created_at', '>=', $startDate);
|
|
})
|
|
->when($endDate, function ($query) use ($endDate) {
|
|
return $query->whereDate('created_at', '<=', $endDate);
|
|
})
|
|
->latest()
|
|
->get();
|
|
|
|
$statusCount = [
|
|
'pending' => Transaksi::where('status', 'pending')->count(),
|
|
'paid' => Transaksi::where('status', 'paid')->count(),
|
|
'completed' => Transaksi::where('status', 'completed')->count(),
|
|
'cancelled' => Transaksi::where('status', 'cancelled')->count(),
|
|
];
|
|
|
|
return view('admin.laporan.transaksi', compact('transaksi', 'statusCount', 'status', 'startDate', 'endDate'));
|
|
}
|
|
|
|
public function download(Request $request)
|
|
{
|
|
$jenis = $request->input('jenis', 'transaksi');
|
|
$tanggal_mulai = $request->input('tanggal_mulai');
|
|
$tanggal_selesai = $request->input('tanggal_selesai');
|
|
|
|
$startDate = Carbon::parse($tanggal_mulai);
|
|
$endDate = Carbon::parse($tanggal_selesai)->endOfDay();
|
|
|
|
if ($jenis == 'transaksi') {
|
|
$data = Transaksi::with(['user', 'pesanan.barang'])
|
|
->whereBetween('created_at', [$startDate, $endDate])
|
|
->where('status', 'dibayar')
|
|
->latest()
|
|
->get();
|
|
|
|
$totalPendapatan = $data->where('status', 'dibayar')->sum('total_pembayaran');
|
|
$totalTransaksi = $data->count();
|
|
|
|
$filename = 'laporan_' . $jenis . '_' . $startDate->format('dmY') . '_' . $endDate->format('dmY') . '.html';
|
|
|
|
return response()
|
|
->view('admin.laporan.pdf', compact(
|
|
'jenis',
|
|
'tanggal_mulai',
|
|
'tanggal_selesai',
|
|
'transaksi',
|
|
'totalPendapatan',
|
|
'totalTransaksi'
|
|
))
|
|
->header('Content-Type', 'text/html')
|
|
->header('Content-Disposition', 'attachment; filename="' . $filename . '"');
|
|
} else {
|
|
// Laporan Barang
|
|
$barang = Barang::withCount(['pesanan' => function ($query) use ($startDate, $endDate) {
|
|
$query->whereBetween('created_at', [$startDate, $endDate]);
|
|
}])
|
|
->orderByDesc('pesanan_count')
|
|
->get();
|
|
|
|
$filename = 'laporan_' . $jenis . '_' . $startDate->format('dmY') . '_' . $endDate->format('dmY') . '.html';
|
|
|
|
return response()
|
|
->view('admin.laporan.pdf', compact(
|
|
'jenis',
|
|
'tanggal_mulai',
|
|
'tanggal_selesai',
|
|
'barang'
|
|
))
|
|
->header('Content-Type', 'text/html')
|
|
->header('Content-Disposition', 'attachment; filename="' . $filename . '"');
|
|
}
|
|
}
|
|
}
|