diff --git a/app/Services/Merchant/KitchenPusher.php b/app/Services/Merchant/KitchenPusher.php new file mode 100644 index 0000000..04fea93 --- /dev/null +++ b/app/Services/Merchant/KitchenPusher.php @@ -0,0 +1,59 @@ +loadMissing('qrCode', 'merchant'); + + // Only food menus feed a kitchen. + if (($order->qrCode?->type) !== QrCode::TYPE_MENU || ! $order->merchant) { + return; + } + + $items = collect((array) $order->items) + ->filter(fn ($i) => strtolower(trim((string) ($i['name'] ?? ''))) !== 'shipping') + ->map(fn ($i) => [ + 'name' => (string) ($i['name'] ?? ''), + 'quantity' => max(1, (int) ($i['qty'] ?? 1)), + 'unit_price_minor' => (int) round(((float) ($i['price_ghs'] ?? 0)) * 100), + ]) + ->filter(fn ($i) => $i['name'] !== '') + ->values() + ->all(); + + if ($items === []) { + return; + } + + try { + Http::withToken($key)->acceptJson()->asJson()->timeout(8) + ->post($url.'/kitchen/orders', [ + 'owner' => (string) $order->merchant->public_id, + 'reference' => (string) ($order->payment_reference ?: ('QO-'.$order->id)), + 'customer_name' => $order->customer_name, + 'items' => $items, + ]); + } catch (\Throwable $e) { + report($e); + } + } +} diff --git a/app/Services/Merchant/MerchantSaleService.php b/app/Services/Merchant/MerchantSaleService.php index 393d8f3..cf1b406 100644 --- a/app/Services/Merchant/MerchantSaleService.php +++ b/app/Services/Merchant/MerchantSaleService.php @@ -20,6 +20,7 @@ class MerchantSaleService private PaystackService $paystack, private BillingClient $billing, private SmsService $sms, + private KitchenPusher $kitchen, ) {} /** @@ -217,6 +218,7 @@ class MerchantSaleService ]); $this->notifyOrderPlaced($order->fresh(['qrCode', 'merchant'])); + $this->kitchen->push($order->fresh(['qrCode', 'merchant'])); return $order; } @@ -257,6 +259,7 @@ class MerchantSaleService ); $this->notifyOrderPlaced($order->fresh(['qrCode', 'merchant'])); + $this->kitchen->push($order->fresh(['qrCode', 'merchant'])); return $order; } diff --git a/config/kitchen.php b/config/kitchen.php new file mode 100644 index 0000000..15ccf30 --- /dev/null +++ b/config/kitchen.php @@ -0,0 +1,8 @@ + rtrim((string) env('KITCHEN_API_URL', ''), '/'), + 'key' => env('KITCHEN_API_KEY'), +];