70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\Crm\Utils;
|
|
|
|
use App\Utils\ModuleUtil;
|
|
|
|
class TelemarketingUtil
|
|
{
|
|
public static function defaultSettings(): array
|
|
{
|
|
return [
|
|
'enable_telemarketing' => 0,
|
|
'call_lock_minutes' => 15,
|
|
'daily_call_target_per_agent' => 50,
|
|
'auto_create_followup_on_interested' => 1,
|
|
'followup_days_after_interested' => 3,
|
|
'enable_call_recording' => 0,
|
|
'enable_ai_sentiment' => 0,
|
|
];
|
|
}
|
|
|
|
public static function mergeSettings(array $crm_settings): array
|
|
{
|
|
return array_merge(self::defaultSettings(), $crm_settings);
|
|
}
|
|
|
|
public static function isEnabled(int $business_id): bool
|
|
{
|
|
$crmUtil = new CrmUtil();
|
|
|
|
return ! empty(self::mergeSettings($crmUtil->getCrmSettings($business_id))['enable_telemarketing']);
|
|
}
|
|
|
|
public static function authorizeTelemarketing(ModuleUtil $moduleUtil, int $business_id, ?string $permission = null): void
|
|
{
|
|
if (! (auth()->user()->can('superadmin') || $moduleUtil->hasThePermissionInSubscription($business_id, 'crm_module'))) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
$user = auth()->user();
|
|
$commonUtil = new \App\Utils\Util();
|
|
$is_admin = $commonUtil->is_admin($user, $business_id);
|
|
$tm_enabled = self::isEnabled($business_id);
|
|
|
|
$has_base_access = $user->can('crm.telemarketing.access')
|
|
|| ($is_admin && $tm_enabled)
|
|
|| ($tm_enabled && ($user->can('crm.access_all_leads') || $user->can('crm.access_own_leads')));
|
|
|
|
if ($permission === 'crm.telemarketing.access' || empty($permission)) {
|
|
if (! $has_base_access) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (! $user->can($permission) && ! $user->can('superadmin') && ! $is_admin) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
}
|
|
|
|
public static function callLockMinutes(int $business_id): int
|
|
{
|
|
$crmUtil = new CrmUtil();
|
|
$settings = self::mergeSettings($crmUtil->getCrmSettings($business_id));
|
|
|
|
return max(1, (int) ($settings['call_lock_minutes'] ?? 15));
|
|
}
|
|
}
|