Many fixes
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
/**
|
||||
* Модель настроек модулей
|
||||
*
|
||||
* Хранит переопределённые настройки модулей в БД.
|
||||
*/
|
||||
class ModuleSettingsModel extends Model
|
||||
{
|
||||
protected $table = 'module_settings';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = 'array';
|
||||
protected $useSoftDeletes = false;
|
||||
protected $allowedFields = [
|
||||
'module_code',
|
||||
'name',
|
||||
'description',
|
||||
'price_monthly',
|
||||
'price_yearly',
|
||||
'trial_days',
|
||||
'is_active',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
protected $useTimestamps = false;
|
||||
|
||||
/**
|
||||
* Получение настроек модуля по коду
|
||||
*
|
||||
* @param string $moduleCode
|
||||
* @return array|null
|
||||
*/
|
||||
public function getByModuleCode(string $moduleCode): ?array
|
||||
{
|
||||
return $this->where('module_code', $moduleCode)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновление или создание настроек модуля
|
||||
*
|
||||
* @param string $moduleCode
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public function upsert(string $moduleCode, array $data): bool
|
||||
{
|
||||
$existing = $this->getByModuleCode($moduleCode);
|
||||
|
||||
$data['module_code'] = $moduleCode;
|
||||
$data['updated_at'] = date('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 $this->insert($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получение всех активных настроек модулей
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAllActive(): array
|
||||
{
|
||||
return $this->where('is_active', 1)->findAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
<?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']);
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
/**
|
||||
* PlanModel - Модель тарифов
|
||||
*/
|
||||
class PlanModel extends Model
|
||||
{
|
||||
protected $table = 'plans';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = 'array';
|
||||
protected $useSoftDeletes = false;
|
||||
|
||||
protected $allowedFields = [
|
||||
'name',
|
||||
'description',
|
||||
'price',
|
||||
'currency',
|
||||
'billing_period',
|
||||
'max_users',
|
||||
'max_clients',
|
||||
'max_storage',
|
||||
'features',
|
||||
'is_active',
|
||||
'is_default',
|
||||
];
|
||||
|
||||
protected $useTimestamps = true;
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
|
||||
protected $validationRules = [
|
||||
'name' => 'required|min_length[2]|max_length[100]',
|
||||
'price' => 'required|numeric|greater_than_equal[0]',
|
||||
'max_users' => 'required|integer|greater_than[0]',
|
||||
'max_clients' => 'required|integer|greater_than[0]',
|
||||
'max_storage' => 'required|integer|greater_than[0]',
|
||||
];
|
||||
|
||||
protected $validationMessages = [
|
||||
'name' => [
|
||||
'required' => 'Название тарифа обязательно',
|
||||
'min_length' => 'Название должно быть минимум 2 символа',
|
||||
],
|
||||
'price' => [
|
||||
'required' => 'Цена обязательна',
|
||||
'numeric' => 'Цена должна быть числом',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Получение активных тарифов
|
||||
*/
|
||||
public function getActivePlans()
|
||||
{
|
||||
return $this->where('is_active', 1)->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Получение тарифа по умолчанию
|
||||
*/
|
||||
public function getDefaultPlan()
|
||||
{
|
||||
return $this->where('is_default', 1)->where('is_active', 1)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверка, является ли тариф системным
|
||||
*/
|
||||
public function isSystemPlan($planId)
|
||||
{
|
||||
$plan = $this->find($planId);
|
||||
return $plan !== null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user