order_gizi/app/Http/Controllers/PesananController.php
2025-08-01 18:37:49 +07:00

290 lines
9.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Order;
use App\Models\OrderDetail;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
class PesananController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$payload = [
'title' => 'Pesanan Pending'
];
return view('dashboard.pesanan.pending.index', $payload);
}
public function getDataPending(){
$orders = DB::connection('dbOrderGizi')->table('public.order as o')
->leftJoin('public.order_detail as od', 'od.order_id', '=', 'o.order_id')
->where('o.statusenabled', true)
->select(
'o.order_id',
'o.entry_at',
'o.no_order',
'o.nama_pemesan',
'o.jenis_customer',
'o.cara_pembayaran',
'o.status_order',
'o.total_harga',
'o.bukti_pembayaran',
'o.note_dibatalkan',
'od.status_order as detail_status_order'
)->get()->groupBy('order_id');
$grouped = $orders->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,
'cara_pembayaran' => $first->cara_pembayaran,
'bukti_pembayaran' => $first->bukti_pembayaran,
'progress' => $progress,
'total_detail' => $totalDetail,
'selesai_detail' => $selesaiDetail,
'note_dibatalkan' => $first->note_dibatalkan,
];
})
->filter(function($item){
return $item['total_detail'] > 0 && $item['total_detail'] != $item['selesai_detail'];
})->values();
return response()->json([
'status' => true,
'rows' => $grouped->values(),
'total' => $grouped->count()
]);
}
public function actionOrder(Request $request, string $order_id){
DB::connection('dbOrderGizi')->beginTransaction();
try {
$order = Order::where('order_id', $order_id)->first();
$action = $request->query('action');
$payload = [
'pegawai_id_confirm_order' => auth()->user()->id,
'pegawai_name_confirm_order' => auth()->user()->full_name,
'pegawai_at_confirm_order' => Carbon::now(),
'status_order' => $action ?? 'Dibatalkan',
];
if(!$action){
$payload['note_dibatalkan'] = request('note_dibatalkan');
}
$order->update($payload);
DB::connection('dbOrderGizi')->commit();
return response()->json([
'status' => true,
'message' => $action ? 'Konfirmasi Order Gizi telah disetujui!' : 'Konfirmasi Order Gizi telah dibatalkan'
]);
} catch (\Throwable $th) {
DB::connection('dbOrderGizi')->rollBack();
return response()->json([
'status' => false,
'message' => 'Gagal melakukan Konfirmasi Order Gizi'
]);
//throw $th;
}
}
public function actionOrderViaBilling(Request $request, string $order_id){
DB::connection('dbOrderGizi')->beginTransaction();
try {
$order = Order::where('order_id', $order_id)->first();
$payload = [
'pegawai_id_confirm_order' => auth()->user()->id,
'pegawai_name_confirm_order' => auth()->user()->full_name,
'pegawai_at_confirm_order' => Carbon::now(),
'status_order' => 'Lunas',
'cara_pembayaran' => 'Billing'
];
$order->update($payload);
DB::connection('dbOrderGizi')->commit();
return response()->json([
'status' => true,
'message' =>'Konfirmasi Order Gizi telah disetujui!'
]);
} catch (\Throwable $th) {
DB::connection('dbOrderGizi')->rollBack();
return response()->json([
'status' => false,
'message' => 'Gagal melakukan Konfirmasi Order Gizi'
]);
//throw $th;
}
}
public function getDataOrderDetail($order_id){
$data = Order::with('orderDetail')->where('order_id', $order_id)->first();
return response()->json($data);
}
public function updateDetailStatusOrder($order_detail_id){
$orderDetail = OrderDetail::where('order_detail_id', $order_detail_id)->first();
$payload = [
'status_order' => 'Selesai',
'modified_at' => Carbon::now(),
];
$orderDetail->update($payload);
return response()->json([
'status' => true,
]);
}
public function indexSelesai()
{
$payload = [
'title' => 'Pesanan Selesai'
];
return view('dashboard.pesanan.selesai.index', $payload);
}
public function getDataSelesai(){
$orders = DB::connection('dbOrderGizi')->table('public.order as o')
->leftJoin('public.order_detail as od', 'od.order_id', '=', 'o.order_id')
->where(['o.statusenabled' => true])
->select(
'o.order_id',
'o.entry_at',
'o.no_order',
'o.nama_pemesan',
'o.jenis_customer',
'o.total_harga',
'o.cara_pembayaran',
'o.status_order',
'o.bukti_pembayaran',
'o.note_dibatalkan',
'od.status_order as detail_status_order'
)->get()->groupBy('order_id');
$grouped = $orders->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,
];
})
->filter(function($item){
return $item['total_detail'] > 0 && $item['total_detail'] === $item['selesai_detail'];
})
->values();
return response()->json([
'status' => true,
'rows' => $grouped->values(),
'total' => $grouped->count()
]);
}
public function pekerjaan(){
$data = [
'title' => 'List Pekerjaan Order Gizi'
];
return view('dashboard.pesanan.pekerjaan.index', $data);
}
public function getPekerjaan(){
$data = OrderDetail::with(['menu', 'paketMenu', 'order'])->whereHas('order', function($q){
$q->where('status_order', 'Lunas');
});
$tanggal = request('tanggal');
if(!empty($tanggal)){
$flattened = is_array($tanggal[0]) ? Arr::flatten($tanggal) : $tanggal;
$data->whereIn('tgl_antar', $flattened);
}else{
$now = Carbon::now()->format('Y-m-d');
$data->where('tgl_antar', $now);
}
$data = $data->get();
return response()->json([
'rows' => $data->values(),
'total' => $data->count()
]);
}
public function getPekerjaanDetail($order_detail_id){
$data = OrderDetail::where('order_detail_id', $order_detail_id)->with(['order', 'menu', 'paketMenu'])->first();
return response()->json([
'status' => true,
'data' => $data
]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}