204 lines
5.7 KiB
PHP
204 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
/**
|
|
* Модель подписок организаций на модули
|
|
*
|
|
* @property int $id
|
|
* @property int $organization_id
|
|
* @property string $module_code
|
|
* @property string $status
|
|
* @property string|null $expires_at
|
|
* @property string|null $created_at
|
|
*/
|
|
class OrganizationSubscriptionModel extends Model
|
|
{
|
|
protected $table = 'organization_subscriptions';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $useSoftDeletes = false;
|
|
protected $allowedFields = [
|
|
'organization_id',
|
|
'module_code',
|
|
'status',
|
|
'expires_at',
|
|
'created_at',
|
|
];
|
|
protected $useTimestamps = false;
|
|
|
|
/**
|
|
* Проверка активности модуля для организации
|
|
*
|
|
* @param int $organizationId
|
|
* @param string $moduleCode
|
|
* @return bool
|
|
*/
|
|
public function isModuleActive(int $organizationId, string $moduleCode): bool
|
|
{
|
|
$subscription = $this->getSubscription($organizationId, $moduleCode);
|
|
|
|
if (!$subscription) {
|
|
return false;
|
|
}
|
|
|
|
return $subscription['status'] === 'active';
|
|
}
|
|
|
|
/**
|
|
* Получение подписки организации на модуль
|
|
*
|
|
* @param int $organizationId
|
|
* @param string $moduleCode
|
|
* @return array|null
|
|
*/
|
|
public function getSubscription(int $organizationId, string $moduleCode): ?array
|
|
{
|
|
return $this->where('organization_id', $organizationId)
|
|
->where('module_code', $moduleCode)
|
|
->first();
|
|
}
|
|
|
|
/**
|
|
* Проверка что организация в триальном периоде для модуля
|
|
*
|
|
* @param int $organizationId
|
|
* @param string $moduleCode
|
|
* @return bool
|
|
*/
|
|
public function isInTrial(int $organizationId, string $moduleCode): bool
|
|
{
|
|
$subscription = $this->getSubscription($organizationId, $moduleCode);
|
|
|
|
if (!$subscription) {
|
|
return false;
|
|
}
|
|
|
|
return $subscription['status'] === 'trial';
|
|
}
|
|
|
|
/**
|
|
* Получение количества дней до окончания подписки/триала
|
|
*
|
|
* @param int $organizationId
|
|
* @param string $moduleCode
|
|
* @return int|null
|
|
*/
|
|
public function getDaysUntilExpire(int $organizationId, string $moduleCode): ?int
|
|
{
|
|
$subscription = $this->getSubscription($organizationId, $moduleCode);
|
|
|
|
if (!$subscription || empty($subscription['expires_at'])) {
|
|
return null;
|
|
}
|
|
|
|
$expiresAt = new \DateTime($subscription['expires_at']);
|
|
$now = new \DateTime();
|
|
$diff = $expiresAt->diff($now);
|
|
|
|
// Если подписка уже истекла
|
|
if ($expiresAt < $now) {
|
|
return 0;
|
|
}
|
|
|
|
return $diff->days;
|
|
}
|
|
|
|
/**
|
|
* Получение всех активных модулей для организации
|
|
*
|
|
* @param int $organizationId
|
|
* @return array
|
|
*/
|
|
public function getActiveModules(int $organizationId): array
|
|
{
|
|
$subscriptions = $this->where('organization_id', $organizationId)
|
|
->whereIn('status', ['active', 'trial'])
|
|
->findAll();
|
|
|
|
return array_column($subscriptions, 'module_code');
|
|
}
|
|
|
|
/**
|
|
* Запуск триального периода для модуля
|
|
*
|
|
* @param int $organizationId
|
|
* @param string $moduleCode
|
|
* @param int $trialDays
|
|
* @return bool
|
|
*/
|
|
public function startTrial(int $organizationId, string $moduleCode, int $trialDays): bool
|
|
{
|
|
// Проверяем, есть ли уже подписка
|
|
$existing = $this->getSubscription($organizationId, $moduleCode);
|
|
|
|
$expiresAt = new \DateTime();
|
|
$expiresAt->modify("+{$trialDays} days");
|
|
|
|
$data = [
|
|
'organization_id' => $organizationId,
|
|
'module_code' => $moduleCode,
|
|
'status' => 'trial',
|
|
'expires_at' => $expiresAt->format('Y-m-d H:i:s'),
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
];
|
|
|
|
if ($existing) {
|
|
return $this->update($existing['id'], $data);
|
|
}
|
|
|
|
return (bool) $this->insert($data);
|
|
}
|
|
|
|
/**
|
|
* Активация подписки на модуль
|
|
*
|
|
* @param int $organizationId
|
|
* @param string $moduleCode
|
|
* @param int $months
|
|
* @return bool
|
|
*/
|
|
public function activate(int $organizationId, string $moduleCode, int $months = 1): bool
|
|
{
|
|
$existing = $this->getSubscription($organizationId, $moduleCode);
|
|
|
|
$expiresAt = new \DateTime();
|
|
$expiresAt->modify("+{$months} months");
|
|
|
|
$data = [
|
|
'organization_id' => $organizationId,
|
|
'module_code' => $moduleCode,
|
|
'status' => 'active',
|
|
'expires_at' => $expiresAt->format('Y-m-d H:i:s'),
|
|
];
|
|
|
|
if ($existing) {
|
|
return $this->update($existing['id'], $data);
|
|
}
|
|
|
|
$data['created_at'] = date('Y-m-d H:i:s');
|
|
return (bool) $this->insert($data);
|
|
}
|
|
|
|
/**
|
|
* Отмена подписки на модуль
|
|
*
|
|
* @param int $organizationId
|
|
* @param string $moduleCode
|
|
* @return bool
|
|
*/
|
|
public function cancel(int $organizationId, string $moduleCode): bool
|
|
{
|
|
$existing = $this->getSubscription($organizationId, $moduleCode);
|
|
|
|
if (!$existing) {
|
|
return false;
|
|
}
|
|
|
|
return $this->update($existing['id'], ['status' => 'cancelled']);
|
|
}
|
|
}
|