ultimatepos/Modules/Crm/Services/TelemarketingWorkflowService.php

74 lines
2.8 KiB
PHP

<?php
namespace Modules\Crm\Services;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Notification;
use Modules\Crm\Entities\CrmCallAssessment;
use Modules\Crm\Entities\CrmCompany;
use Modules\Crm\Entities\CrmPhoneFollowUp;
use Modules\Crm\Utils\CrmUtil;
use Modules\Crm\Utils\TelemarketingUtil;
class TelemarketingWorkflowService
{
public function afterAssessment(CrmCallAssessment $assessment, CrmPhoneFollowUp $phone, ?CrmCompany $company, User $user): void
{
$crmUtil = new CrmUtil();
$settings = TelemarketingUtil::mergeSettings($crmUtil->getCrmSettings($phone->business_id));
if (($assessment->cooperation_score ?? 0) >= 4 && ! empty($settings['auto_create_followup_on_interested'])) {
$days = max(1, (int) ($settings['followup_days_after_interested'] ?? 3));
$crmUtil->addFollowUp([
'contact_id' => $phone->lead_id,
'title' => __('crm::lang.telemarketing_followup_title', ['company' => $phone->company_name ?? $phone->mobile_number]),
'description' => $assessment->notes,
'start_datetime' => Carbon::now()->addDays($days)->format('Y-m-d H:i:s'),
'end_datetime' => Carbon::now()->addDays($days)->addHour()->format('Y-m-d H:i:s'),
'schedule_type' => 'call',
'status' => 'scheduled',
'followup_category_id' => null,
'user_id' => [$user->id],
], $user);
}
if ($assessment->partner_potential === 'high') {
$this->notifySupervisors($phone->business_id, __('crm::lang.high_partner_potential_alert', [
'company' => $phone->company_name ?? $phone->mobile_number,
]));
}
if ($assessment->sentiment === 'hostile') {
$phone->update(['block_auto_recall' => true]);
}
if ($company && $company->profile_completion >= 80) {
$offerService = new OfferMatchingService();
$offerService->matchForCompany($company, $assessment);
}
}
public function afterNoAnswerStreak(CrmPhoneFollowUp $phone, int $streak = 3): void
{
if ($streak >= 3) {
// Mark for SMS campaign follow-up
$phone->update(['status' => 'no_answer']);
}
}
protected function notifySupervisors(int $business_id, string $message): void
{
$supervisors = User::where('business_id', $business_id)
->whereHas('permissions', function ($q) {
$q->where('name', 'crm.telemarketing.supervisor_dashboard');
})
->get();
// Notification can be extended with a dedicated notification class
foreach ($supervisors as $supervisor) {
// placeholder for future notification
}
}
}