Files
portal-api/app/Repositories/Api/Order/OrderRepository.php
T
baodev0 7314313198 Refactor order payment process and enhance invoice details
- Updated `payOrder` method in `OrderControllerRefactor` to accept `orderNum` instead of `id` for better clarity and functionality.
- Implemented `findByOrderNum` method in `OrderRepository` to retrieve orders based on order number and customer ID.
- Enhanced `InvoiceDetailResource` to include VMware details and removed unnecessary fields.
- Added `service_plan_id` to `ProductResource` and updated the `Product` model to establish a relationship with `ServicePlan`.
- Created `ServicePlan` and `ServicePlanPlesk` models along with their respective controllers and migrations for managing hosting service plans.
- Updated API routes to include endpoints for managing service plans.
- Modified email templates to standardize support phone number across various customer communications.
2026-08-11 10:12:25 +07:00

92 lines
3.0 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
// -------------------------------------------------------------------------
public function hasPendingOrder(int $customerId): bool
{
return $this->model->where('customer_id', $customerId)->where('status', OrderStatus::PENDING->value)->exists();
}
// -------------------------------------------------------------------------
// findByOrderNum
// -------------------------------------------------------------------------
public function findByOrderNum(string $orderNum, int $customerId): ?Order
{
return $this->model->where('order_num', $orderNum)->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);
}
}