$orderId], $data, ['addons_meta' => !empty($addons) ? $addons : null]), ); } // ------------------------------------------------------------------------- // 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); } }