Files
portal-api/app/Repositories/Api/Order/OrderRepository.php
T
2026-09-23 15:17:18 +07:00

112 lines
3.8 KiB
PHP

<?php
namespace App\Repositories\Api\Order;
use App\Enums\Invoice\InvoiceStatus;
use App\Enums\Order\OrderStatus;
use App\Models\Order;
use App\Models\OrderItem;
use App\Repositories\Api\BaseApiRepository;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
class OrderRepository extends BaseApiRepository implements OrderRepositoryInterface
{
public function getModel(): string
{
return Order::class;
}
// -------------------------------------------------------------------------
// createItem
// -------------------------------------------------------------------------
public function createItem(int $orderId, array $data): OrderItem
{
$addons = $data['addons'] ?? [];
unset($data['addons']);
return OrderItem::create(
array_merge(['order_id' => $orderId], $data, ['addons_meta' => !empty($addons) ? $addons : null]),
);
}
// -------------------------------------------------------------------------
// processPayment
// -------------------------------------------------------------------------
public function processPayment(Order $order, string $method): Order
{
$order->update([
'status' => OrderStatus::ACTIVE->value,
'payment_method' => $method,
]);
if ($order->invoice) {
$order->invoice->update([
'status' => InvoiceStatus::PAID->value,
'payment_method' => $method,
'paid_at' => now(),
]);
}
return $order->fresh(['orderItems', 'invoice', 'customer']);
}
// -------------------------------------------------------------------------
// hasPendingOrder
// -------------------------------------------------------------------------
/**
* Chặn khách tạo đơn mới khi còn đơn chưa thanh toán.
*
* Bỏ qua đơn import từ WHMCS (legacy_id khác null): dữ liệu cũ có rất nhiều đơn ở trạng
* thái pending, tính cả chúng thì sau khi đồng bộ gần như mọi khách hàng cũ đều bị khoá
* không đặt được đơn mới.
*/
public function hasPendingOrder(int $customerId): bool
{
return $this->model
->where('customer_id', $customerId)
->where('status', OrderStatus::PENDING->value)
->whereNull('legacy_id')
->exists();
}
// -------------------------------------------------------------------------
// findByOrderNum
// -------------------------------------------------------------------------
public function findByOrderNum(string $orderNum, int $customerId): ?Order
{
return $this->model->where('order_num', $orderNum)->where('customer_id', $customerId)->first();
}
// -------------------------------------------------------------------------
// findForCustomer
// -------------------------------------------------------------------------
public function findForCustomer(int $id, int $customerId): ?Order
{
return $this->model->where('id', $id)->where('customer_id', $customerId)->first();
}
// -------------------------------------------------------------------------
// getOrders
// -------------------------------------------------------------------------
public function getOrders(int $customerId, array $filters): LengthAwarePaginator
{
$query = $this->model->where('customer_id', $customerId);
if (!empty($filters['total_price'])) {
$query->where('total', $filters['total_price']);
}
if (!empty($filters['pending']) && filter_var($filters['pending'], FILTER_VALIDATE_BOOLEAN)) {
$query->where('status', OrderStatus::PENDING->value);
}
return $query->orderByDesc('created_at')->paginate(10);
}
}