80 lines
2.3 KiB
PHP
80 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Api\ServiceClient;
|
|
|
|
use App\Support\ProductBillingCycles;
|
|
use App\Models\Service;
|
|
use App\Repositories\Api\BaseApiRepository;
|
|
|
|
class ServiceClientRepository extends BaseApiRepository implements ServiceClientRepositoryInterface
|
|
{
|
|
public function getModel()
|
|
{
|
|
return Service::class;
|
|
}
|
|
|
|
/**
|
|
* Lấy toàn bộ dịch vụ của customer (mọi trạng thái)
|
|
*/
|
|
public function getActiveServices(int $customerId, ?string $productType = null, ?int $limit = 10)
|
|
{
|
|
$query = $this->model
|
|
->with([
|
|
'product.servicePlan',
|
|
'addons',
|
|
'invoice.order',
|
|
'orderItem' => fn($q) => $q->with(['product.servicePlan', 'os.osGroup', 'order']),
|
|
])
|
|
->where('customer_id', $customerId)
|
|
->orderByRaw("FIELD(status, 'active', 'pending', 'suspended', 'expired', 'cancelled', 'terminated')")
|
|
->orderBy('started_at', 'desc');
|
|
|
|
if ($productType) {
|
|
$query->where('type', $productType);
|
|
}
|
|
|
|
return $limit === null ? $query->get() : $query->paginate($limit);
|
|
}
|
|
|
|
public function findActiveBySlug(string $slug)
|
|
{
|
|
return $this->model
|
|
->with(['osVersions.osGroup', 'category', 'pricings' => fn ($q) => $q->where('is_active', 1)])
|
|
->where('slug', $slug)
|
|
->where('is_active', 1)
|
|
->first();
|
|
}
|
|
|
|
public function getPricing(int $productId, string $billingCycle)
|
|
{
|
|
$product = $this->model->find($productId);
|
|
|
|
if (!ProductBillingCycles::allows($product->product_type, $billingCycle)) {
|
|
return null;
|
|
}
|
|
|
|
$pricing = $product->pricings()
|
|
->where('billing_cycle', $billingCycle)
|
|
->where('is_active', 1)
|
|
->first();
|
|
|
|
return $pricing ? $pricing->price : null;
|
|
}
|
|
|
|
public function getDetailService(string $uuid, int $customerId)
|
|
{
|
|
return $this->model
|
|
->with([
|
|
'addons',
|
|
'orderItem.os.osGroup',
|
|
'orderItem.product',
|
|
'orderItem.order',
|
|
'vmware.ipAddress',
|
|
'hostingAccount.server',
|
|
])
|
|
->where('uuid', $uuid)
|
|
->where('customer_id', $customerId)
|
|
->first();
|
|
}
|
|
}
|