140 lines
4.5 KiB
PHP
140 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Carbon\Carbon;
|
|
// use App\Models\Cashier;
|
|
// use App\Models\Product;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Exports\IncomeExport;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
|
|
class IncomeController extends Controller
|
|
{
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$bulan = $request->input('bulan', date('m'));
|
|
$tahun = $request->input('tahun', date('Y'));
|
|
|
|
// Ambil data penjualan berdasarkan bulan & tahun
|
|
$cashier = DB::table('cashiers')
|
|
->join('products', 'cashiers.produk_id', '=', 'products.id')
|
|
->select(
|
|
'cashiers.id as order_id',
|
|
'products.nama_produk as Nama_Produk',
|
|
'cashiers.jumlah_item as Jumlah_Item',
|
|
'cashiers.total as Total_Bayar',
|
|
'cashiers.tipe_bayar as Metode_Bayar',
|
|
'cashiers.createdAt as Waktu_Tanggal'
|
|
)
|
|
->whereMonth('cashiers.createdAt', $bulan)
|
|
->whereYear('cashiers.createdAt', $tahun)
|
|
->orderBy('cashiers.id', 'desc')
|
|
->get();
|
|
|
|
// Hitung total penjualan
|
|
$totalPenjualan = $cashier->sum('Total_Bayar');
|
|
|
|
// Hitung jumlah hari unik dalam bulan tersebut
|
|
$jumlahHariUnik = $cashier
|
|
->map(fn($item) => date('Y-m-d', strtotime($item->Waktu_Tanggal)))
|
|
->unique()
|
|
->count();
|
|
|
|
// Hitung rata-rata per hari
|
|
$rataRataPerHari = $jumlahHariUnik > 0 ? ($totalPenjualan / $jumlahHariUnik) : 0;
|
|
|
|
// Hitung jumlah produk terjual
|
|
$jumlahProdukTerjual = $cashier->sum('Jumlah_Item');
|
|
|
|
// Hitung rata-rata produk terjual per hari
|
|
$rataRataProdukPerHari = $jumlahHariUnik > 0 ? ($jumlahProdukTerjual / $jumlahHariUnik) : 0;
|
|
|
|
// Hitung metode bayar yang paling sering
|
|
$metodeBayarCount = $cashier->groupBy('Metode_Bayar')
|
|
->map(fn($group) => $group->count());
|
|
|
|
// Mendapatkan metode bayar yang paling sering digunakan
|
|
$metodeBayarNama = $metodeBayarCount->keys()->first();
|
|
$metodeBayarJumlah = $metodeBayarCount->max();
|
|
|
|
return view('income', compact(
|
|
'cashier',
|
|
'bulan',
|
|
'tahun',
|
|
'totalPenjualan',
|
|
'rataRataPerHari',
|
|
'jumlahProdukTerjual',
|
|
'rataRataProdukPerHari',
|
|
'metodeBayarNama',
|
|
'metodeBayarJumlah'
|
|
));
|
|
}
|
|
|
|
|
|
// public function index()
|
|
// {
|
|
// // Mengambil data dari database menggunakan query builder
|
|
// $cashier = DB::table('cashiers')
|
|
// ->join('products', 'cashiers.produk_id', '=', 'products.id')
|
|
// ->select(
|
|
// 'cashiers.id as No',
|
|
// 'products.nama_produk as Nama_Produk',
|
|
// 'cashiers.jumlah_item as Jumlah_Item',
|
|
// 'cashiers.total as Total_Bayar',
|
|
// 'cashiers.tipe_bayar as Metode_Bayar',
|
|
// 'cashiers.createdAt as Waktu_Tanggal'
|
|
// )
|
|
// ->get();
|
|
|
|
// // Mengirim data ke Blade view
|
|
// return view('income', compact('cashier'));
|
|
// }
|
|
|
|
public function exportExcel(Request $request)
|
|
{
|
|
$bulan = $request->input('bulan');
|
|
$tahun = $request->input('tahun');
|
|
|
|
if (!$bulan || !$tahun) {
|
|
return redirect()->back()->with('error', 'Silakan pilih bulan dan tahun terlebih dahulu.');
|
|
}
|
|
|
|
return Excel::download(new IncomeExport($bulan, $tahun), 'laporan_penjualan_'.$bulan.'_'.$tahun.'.xlsx');
|
|
}
|
|
|
|
public function exportPDF(Request $request)
|
|
{
|
|
$bulan = $request->input('bulan');
|
|
$tahun = $request->input('tahun');
|
|
|
|
if (!$bulan || !$tahun) {
|
|
return redirect()->back()->with('error', 'Silakan pilih bulan dan tahun terlebih dahulu.');
|
|
}
|
|
|
|
$data = DB::table('cashiers')
|
|
->join('products', 'cashiers.produk_id', '=', 'products.id')
|
|
->select(
|
|
'products.nama_produk as Nama_Produk',
|
|
'cashiers.jumlah_item as Jumlah_Item',
|
|
'cashiers.total as Total_Bayar',
|
|
'cashiers.tipe_bayar as Metode_Bayar',
|
|
'cashiers.createdAt as Waktu_Tanggal'
|
|
)
|
|
->whereMonth('cashiers.createdAt', $bulan)
|
|
->whereYear('cashiers.createdAt', $tahun)
|
|
->orderBy('cashiers.createdAt', 'desc')
|
|
->get();
|
|
|
|
$total = $data->sum('Total_Bayar');
|
|
|
|
$pdf = Pdf::loadView('exports.income_pdf', compact('data', 'bulan', 'tahun', 'total'));
|
|
return $pdf->stream('laporan_penjualan_'.$bulan.'_'.$tahun.'.pdf');
|
|
}
|
|
|
|
|
|
}
|