97 lines
3.5 KiB
PHP
97 lines
3.5 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, // Or PROCESSING depending on flow, but user said 'thanh toán thực hiện order tiếp' usually implies Active/Paid.
|
|
// Let's assume ACTIVE means Paid/Completed effectively for order flow permission
|
|
'payment_method' => $method, // Column might not exist in standard migration, checking migration...
|
|
// Migration 2024_01_31_155621_order.php does NOT have payment_method or paid_at.
|
|
// It has 'status' (tinyInteger), 'date' (int), 'date_paid' (int) in invoice but not order.
|
|
// Order table has: customer_id, date, user_id, discount_id, invoice_id, discount_value, amount, note, client_ip, status.
|
|
// So we can only update status.
|
|
// BUT order table has 'payment_id' in Model $fillable but migration didn't show it in up() method I saw earlier (2024_01_31_155621_order.php).
|
|
// Let's check migration again if needed. But safe bet is update status.
|
|
|
|
// Wait, user request: "phải thanh toán order mới thực hiện được order tiếp theo"
|
|
// So logic checks for PENDING. If we change to ACTIVE (1) or PROCESSING (3), it allows new order.
|
|
]);
|
|
|
|
// 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);
|
|
}
|
|
}
|