86 lines
2.4 KiB
PHP
86 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Api\Order;
|
|
|
|
use App\Models\Order;
|
|
use App\Repositories\Api\BaseApiRepository;
|
|
|
|
class OrderRepository extends BaseApiRepository implements OrderRepositoryInterface
|
|
{
|
|
public function getModel()
|
|
{
|
|
return Order::class;
|
|
}
|
|
|
|
public function createItem($orderId, array $data)
|
|
{
|
|
$addons = $data['addons'] ?? [];
|
|
unset($data['addons']); // Remove 'addons' from data before creating OrderItem
|
|
|
|
$orderItem = \App\Models\OrderItem::create(array_merge(
|
|
['order_id' => $orderId],
|
|
$data
|
|
));
|
|
|
|
if (!empty($addons)) {
|
|
foreach ($addons as $addonData) {
|
|
$addon = \App\Models\Addon::find($addonData['addon_id']);
|
|
|
|
if ($addon) {
|
|
\App\Models\OrderAddon::create([
|
|
'order_item_id' => $orderItem->id,
|
|
'addon_id' => $addon->id,
|
|
'qty' => $addonData['qty'],
|
|
'price' => $addon->price // Unit price
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $orderItem;
|
|
}
|
|
|
|
public function processPayment($order, $method)
|
|
{
|
|
// Update Order Status
|
|
$order->update([
|
|
'status' => \App\Enums\Order\OrderStatus::ACTIVE->value,
|
|
]);
|
|
|
|
// Update Invoice Status
|
|
if ($order->invoice) {
|
|
$order->invoice->update([
|
|
'status' => 2, // Paid
|
|
'date_paid' => now()
|
|
]);
|
|
}
|
|
|
|
return $order;
|
|
}
|
|
|
|
public function hasPendingOrder($customerId)
|
|
{
|
|
return $this->model->where('customer_id', $customerId)
|
|
->where('status', \App\Enums\Order\OrderStatus::PENDING->value)
|
|
->exists();
|
|
}
|
|
|
|
public function getOrders($customerId, array $filters)
|
|
{
|
|
$query = $this->model->where('customer_id', $customerId);
|
|
|
|
if (isset($filters['total_price'])) {
|
|
$query->where('amount', $filters['total_price']);
|
|
}
|
|
|
|
if (isset($filters['pending'])) {
|
|
// If pending=1 or true, filter by PENDING status
|
|
if (filter_var($filters['pending'], FILTER_VALIDATE_BOOLEAN)) {
|
|
$query->where('status', \App\Enums\Order\OrderStatus::PENDING->value);
|
|
}
|
|
}
|
|
|
|
return $query->orderBy('created_at', 'desc')->paginate(10);
|
|
}
|
|
}
|