Files
kulakpos_web/public/restaurant/Modules/Restaurant/app/Http/Resources/OrderResource.php

126 lines
5.0 KiB
PHP

<?php
namespace Modules\Restaurant\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class OrderResource extends JsonResource
{
/**
* Transform the resource into an array.
*/
public function toArray(Request $request): array
{
return [
// ===== Basic Order Info =====
'id' => $this->id,
'order_number' => $this->order_number,
'reference' => $this->reference,
'order_type' => $this->order_type,
'status' => $this->status,
'order_date' => $this->order_date,
'order_source' => $this->order_source,
'customer_id' => $this->customer_id,
'waiter_id' => $this->waiter_id,
// 'driver_id' => $this->driver_id,
// ===== Customer Info =====
'customer' => [
'id' => $this->customer_id,
'name' => $this->customer?->name,
'phone' => $this->customer?->phone,
'address' => $this->delivery_address,
],
// ===== Table / Staff Info =====
'table' => [
'id' => $this->table_id,
'name' => $this->table?->name,
],
'waiter' => [
'id' => $this->waiter_id,
'name' => $this->waiter?->first_name,
],
// TODO
// 'driver' => [
// 'id' => $this->driver_id,
// 'name' => $this->driver?->first_name,
// ],
// ===== Payment Info =====
'payment' => [
'method_id' => $this->payment_method_id,
'method_name' => $this->paymentMethod?->name,
'status' => (bool) $this->payment_status,
'reference' => $this->payment_reference,
'paid_at' => optional($this->paid_at)->toDateTimeString(),
],
// ===== Financials =====
'subtotal' => $this->subtotal,
'discount' => $this->discount,
'tax' => $this->tax,
'service_charge' => $this->service_charge,
'tip_amount' => $this->tip_amount,
'grand_total' => $this->grand_total,
// ===== Time Info =====
'estimated_ready_time' => optional($this->estimated_ready_time)->toDateTimeString(),
'ready_at' => optional($this->ready_at)->toDateTimeString(),
'handed_over_at' => optional($this->handed_over_at)->toDateTimeString(),
'delivered_at' => optional($this->delivered_at)->toDateTimeString(),
// ===== Meta / Extra =====
'meta' => $this->meta ? json_decode($this->meta, true) : null,
'created_at' => optional($this->created_at)->toDateTimeString(),
'updated_at' => optional($this->updated_at)->toDateTimeString(),
// ===== Order Items =====
'items' => $this->items->map(function ($item) {
return [
'id' => $item->id,
'food_item_id' => $item->food_item_id,
'food_variant_id' => $item->food_variant_id,
'food_name' => $item->food?->name,
'food_description' => $item->food?->description,
'food_image' => $item->food?->image,
'quantity' => $item->quantity,
'price' => $item->price,
'discount' => $item->discount,
'tax' => $item->tax,
'total' => $item->total,
// ===== Variant Info =====
'variant' => $item->variation ? [
'id' => $item->variation->id,
'name' => $item->variation->name,
'sku' => $item->variation->sku,
'unit_id' => $item->variation?->unit_id,
'price' => $item->variation->price,
'offer_price' => $item->variation->offer_price,
'discount' => $item->variation->discount,
'offer_start_at' => optional($item->variation->offer_start_at)->toDateTimeString(),
'offer_end_at' => optional($item->variation->offer_end_at)->toDateTimeString(),
] : null,
// ===== Addons =====
'addons' => $item->addons ? json_decode($item->addons, true) : [],
// ===== Optional Ingredients (for kitchen / stock use) =====
// 'ingredients' => $item->food?->ingredients->map(function ($ing) {
// return [
// 'id' => $ing->id,
// 'name' => $ing->name,
// 'quantity_required' => $ing->pivot->quantity_required ?? 0,
// 'unit_id' => $ing->unit ?? 'g',
// ];
// }),
'status' => $item->status,
];
}),
];
}
}