182 lines
6.7 KiB
PHP
182 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function overview(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'date' => ['nullable', 'date'],
|
|
'lokasi_kode' => ['nullable', 'string', 'max:20'],
|
|
'low_stock_limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
|
'low_stock_threshold' => ['nullable', 'integer', 'min:0', 'max:1000000'],
|
|
]);
|
|
|
|
$date = $data['date'] ?? now()->toDateString();
|
|
$start = $date . ' 00:00:00';
|
|
$end = $date . ' 23:59:59';
|
|
|
|
$basePenjualan = DB::table('penjualan')->whereBetween('waktu', [$start, $end]);
|
|
$pendapatan = (int) (clone $basePenjualan)->sum('total');
|
|
$transaksi = (int) (clone $basePenjualan)->count();
|
|
|
|
$itemTerjual = (int) DB::table('penjualan_barang as pb')
|
|
->join('penjualan as p', 'p.id', '=', 'pb.penjualan_id')
|
|
->whereBetween('p.waktu', [$start, $end])
|
|
->sum('pb.jumlah');
|
|
|
|
$margin = (int) DB::table('penjualan_barang as pb')
|
|
->join('penjualan as p', 'p.id', '=', 'pb.penjualan_id')
|
|
->leftJoin('produk as pr', 'pr.id', '=', 'pb.produk_id')
|
|
->whereBetween('p.waktu', [$start, $end])
|
|
->selectRaw('COALESCE(SUM((pb.harga - COALESCE(pr.harga_beli, 0)) * pb.jumlah),0) as margin')
|
|
->value('margin');
|
|
|
|
$role = $request->user()?->role ?: 'admin';
|
|
$marginValue = $role === 'admin' ? $margin : null;
|
|
|
|
$summary = [
|
|
'pendapatan' => $pendapatan,
|
|
'transaksi' => $transaksi,
|
|
'item_terjual' => $itemTerjual,
|
|
'margin' => $marginValue,
|
|
];
|
|
|
|
$chartStart = Carbon::parse($date)->subDays(6)->toDateString();
|
|
$chartRows = DB::table('penjualan')
|
|
->selectRaw('DATE(waktu) as tanggal, SUM(total) as total')
|
|
->whereBetween('waktu', [$chartStart . ' 00:00:00', $end])
|
|
->groupBy('tanggal')
|
|
->orderBy('tanggal')
|
|
->get()
|
|
->keyBy('tanggal');
|
|
|
|
$chart = [];
|
|
for ($i = 6; $i >= 0; $i--) {
|
|
$d = Carbon::parse($date)->subDays($i)->toDateString();
|
|
$chart[] = [
|
|
'tanggal' => $d,
|
|
'total' => (int) ($chartRows->get($d)?->total ?? 0),
|
|
];
|
|
}
|
|
|
|
$topProducts = DB::table('penjualan_barang as pb')
|
|
->join('penjualan as p', 'p.id', '=', 'pb.penjualan_id')
|
|
->join('produk as pr', 'pr.id', '=', 'pb.produk_id')
|
|
->whereBetween('p.waktu', [$start, $end])
|
|
->selectRaw('pr.id, pr.nama, pr.barcode, SUM(pb.jumlah) as qty, SUM(pb.total) as total')
|
|
->groupBy('pr.id', 'pr.nama', 'pr.barcode')
|
|
->orderByDesc('qty')
|
|
->limit(10)
|
|
->get()
|
|
->map(function ($r) {
|
|
return [
|
|
'id' => (int) $r->id,
|
|
'nama' => $r->nama,
|
|
'barcode' => $r->barcode,
|
|
'qty' => (int) $r->qty,
|
|
'total' => (int) $r->total,
|
|
];
|
|
});
|
|
|
|
$limit = (int) ($data['low_stock_limit'] ?? 10);
|
|
$thresholdToko = (int) ($data['low_stock_threshold'] ?? $this->getSetting('stok.low_threshold_toko', 5));
|
|
$thresholdGudang = (int) $this->getSetting('stok.low_threshold_gudang', 5);
|
|
|
|
$lowStockToko = $this->lowStockByKode('TOKO', $thresholdToko, $limit);
|
|
$lowStockGudang = $this->lowStockByKode('GUDANG', $thresholdGudang, $limit);
|
|
|
|
$recent = DB::table('penjualan')
|
|
->orderByDesc('waktu')
|
|
->limit(10)
|
|
->get()
|
|
->map(function ($r) {
|
|
return [
|
|
'id' => (int) $r->id,
|
|
'nomor_penjualan' => $r->nomor_penjualan,
|
|
'waktu' => $r->waktu,
|
|
'total' => (int) $r->total,
|
|
'metode_pembayaran' => $r->metode_pembayaran,
|
|
];
|
|
});
|
|
|
|
$dataIssues = [
|
|
'tanpa_barcode' => (int) DB::table('produk')
|
|
->whereNull('barcode')
|
|
->orWhere('barcode', '')
|
|
->count(),
|
|
'tanpa_harga_jual' => (int) DB::table('produk')
|
|
->whereNull('harga_jual')
|
|
->orWhere('harga_jual', 0)
|
|
->count(),
|
|
];
|
|
|
|
return response()->json([
|
|
'date' => $date,
|
|
'summary' => $summary,
|
|
'chart_7d' => $chart,
|
|
'top_products' => $topProducts,
|
|
'low_stock_toko' => $lowStockToko,
|
|
'low_stock_gudang' => $lowStockGudang,
|
|
'recent' => $recent,
|
|
'data_issues' => $dataIssues,
|
|
]);
|
|
}
|
|
|
|
private function getSetting(string $key, $default = null)
|
|
{
|
|
$value = DB::table('settings')->where('key', $key)->value('value');
|
|
return $value === null ? $default : $value;
|
|
}
|
|
|
|
private function lowStockByKode(string $kode, int $threshold, int $limit): array
|
|
{
|
|
$lokasi = DB::table('lokasi')->where('kode', $kode)->first();
|
|
if (!$lokasi) {
|
|
return [
|
|
'lokasi' => null,
|
|
'threshold' => $threshold,
|
|
'data' => [],
|
|
];
|
|
}
|
|
|
|
$lokasiId = (int) $lokasi->id;
|
|
$rows = DB::table('produk')
|
|
->select('produk.id', 'produk.nama', 'produk.barcode', 'produk.sku')
|
|
->selectRaw(
|
|
'COALESCE(SUM(CASE WHEN pergerakan_stok.ke_lokasi_id = ? THEN pergerakan_stok.jumlah ELSE 0 END),0) - ' .
|
|
'COALESCE(SUM(CASE WHEN pergerakan_stok.dari_lokasi_id = ? THEN pergerakan_stok.jumlah ELSE 0 END),0) as stok',
|
|
[$lokasiId, $lokasiId]
|
|
)
|
|
->leftJoin('pergerakan_stok', 'pergerakan_stok.produk_id', '=', 'produk.id')
|
|
->groupBy('produk.id', 'produk.nama', 'produk.barcode', 'produk.sku')
|
|
->having('stok', '<=', $threshold)
|
|
->orderBy('stok')
|
|
->limit($limit)
|
|
->get()
|
|
->map(function ($r) {
|
|
return [
|
|
'id' => (int) $r->id,
|
|
'nama' => $r->nama,
|
|
'barcode' => $r->barcode,
|
|
'sku' => $r->sku,
|
|
'stok' => (int) $r->stok,
|
|
];
|
|
})
|
|
->all();
|
|
|
|
return [
|
|
'lokasi' => ['id' => (int) $lokasi->id, 'kode' => $lokasi->kode, 'nama' => $lokasi->nama],
|
|
'threshold' => $threshold,
|
|
'data' => $rows,
|
|
];
|
|
}
|
|
}
|