104 lines
5.1 KiB
PHP
104 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function index(){
|
|
$data = [
|
|
'title' => 'Dashboard'
|
|
];
|
|
return view('dashboard.index', $data);
|
|
}
|
|
|
|
public function dataDashboard(){
|
|
$startDate = request()->filled('start_date')
|
|
? Carbon::parse(request('start_date'))->startOfDay()
|
|
: Carbon::now()->startOfDay();
|
|
|
|
$endDate = request()->filled('end_date')
|
|
? Carbon::parse(request('end_date'))->endOfDay()
|
|
: Carbon::now()->endOfDay();
|
|
$data = DB::connection('dbOrderGizi')->table('public.order as o')
|
|
->leftJoin('public.order_detail as od', 'od.order_id', '=', 'o.order_id')
|
|
->where('o.statusenabled', true)
|
|
->whereBetween('o.entry_at', [$startDate, $endDate])
|
|
->select(
|
|
'o.order_id',
|
|
'o.entry_at',
|
|
'o.no_order',
|
|
'o.nama_pemesan',
|
|
'o.jenis_customer',
|
|
'o.total_harga',
|
|
'o.status_order',
|
|
'o.cara_pembayaran',
|
|
'o.bukti_pembayaran',
|
|
'o.note_dibatalkan',
|
|
'od.status_order as detail_status_order')
|
|
->get()->groupBy('order_id');
|
|
$grouped = $data->map(function($items){
|
|
$first = $items->first();
|
|
$totalDetail = $items->count();
|
|
$selesaiDetail = $items->where('detail_status_order', 'Selesai')->count();
|
|
$progress = $totalDetail > 0 ? round(($selesaiDetail / $totalDetail) * 100) : 0;
|
|
return [
|
|
'order_id' => $first->order_id,
|
|
'entry_at' => $first->entry_at,
|
|
'no_order' => $first->no_order,
|
|
'nama_pemesan' => $first->nama_pemesan,
|
|
'jenis_customer' => $first->jenis_customer,
|
|
'total_harga' => $first->total_harga,
|
|
'status_order' => $first->status_order,
|
|
'bukti_pembayaran' => $first->bukti_pembayaran,
|
|
'cara_pembayaran' => $first->cara_pembayaran,
|
|
'progress' => $progress,
|
|
'total_detail' => $totalDetail,
|
|
'selesai_detail' => $selesaiDetail,
|
|
'note_dibatalkan' => $first->note_dibatalkan,
|
|
];
|
|
})->values();
|
|
$pemasukanKaryawan = $grouped->where('jenis_customer', 'Karyawan RSAB Harapan Kita')->whereNotIn('status_order', ['Belum Bayar'])->sum('total_harga');
|
|
$pemasukanMasyarakat = $grouped->where('jenis_customer', 'Masyarakat Umum')->whereNotIn('status_order', ['Belum Bayar'])->sum('total_harga');
|
|
$pemasukanKeluargaPasien = $grouped->where('jenis_customer', 'Keluarga Pasien / Penunggu Pasien')->whereNotIn('status_order', ['Belum Bayar'])->sum('total_harga');
|
|
|
|
$totalPemasukan = $grouped->whereNotIn('status_order', ['Belum Bayar'])->sum('total_harga');
|
|
$pesananPending = $grouped->whereNotIn('status_order', ['Lunas', 'Dibatalkan', 'Belum Bayar'])->count();
|
|
$pesananLunas = $grouped->where('status_order', 'Lunas')->count();
|
|
$pesananBatal = $grouped->where('status_order', 'Dibatalkan')->count();
|
|
$totalPesanan = $grouped->whereNotIn('status_order', ['Belum Bayar'])->count();
|
|
|
|
$pesananSelesai = DB::connection('dbOrderGizi')->table('public.order as o')
|
|
->where('o.statusenabled', true)
|
|
->whereBetween('o.entry_at', [$startDate, $endDate])
|
|
->whereNotExists(function($query){
|
|
$query->select(DB::raw(1))
|
|
->from('public.order_detail as od')
|
|
->whereColumn('od.order_id', 'o.order_id')
|
|
->where('od.status_order', '!=', 'Selesai');
|
|
})->count();
|
|
|
|
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => 'Berhasil mendapatkan data',
|
|
'data' => [
|
|
'totalPesanan' => $totalPesanan,
|
|
'totalPemasukan' => $totalPemasukan,
|
|
'pesananPending' => $pesananPending,
|
|
'pesananBatal' => $pesananBatal,
|
|
'pesananLunas' => $pesananLunas,
|
|
'pesananSelesai' => $pesananSelesai,
|
|
'pemasukanKaryawan' => $pemasukanKaryawan,
|
|
'pemasukanMasyarakat' => $pemasukanMasyarakat,
|
|
'pemasukanKeluargaPasien' => $pemasukanKeluargaPasien,
|
|
],
|
|
'rows' => $grouped->values(),
|
|
'total' => $grouped->count(),
|
|
],200);
|
|
}
|
|
}
|