bp/app/Modules/CRM/Services/DealService.php

193 lines
4.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Modules\CRM\Services;
use App\Modules\CRM\Models\DealModel;
use App\Modules\CRM\Models\DealStageModel;
use CodeIgniter\Events\Events;
class DealService
{
protected DealModel $dealModel;
protected DealStageModel $stageModel;
public function __construct()
{
$this->dealModel = new DealModel();
$this->stageModel = new DealStageModel();
}
/**
* Создать новую сделку
*/
public function createDeal(array $data, int $userId): int
{
$data['created_by'] = $userId;
$dealId = $this->dealModel->insert($data);
if ($dealId) {
$deal = $this->dealModel->find($dealId);
Events::trigger('deal.created', [
'deal_id' => $dealId,
'deal' => $deal,
'user_id' => $userId,
]);
}
return $dealId;
}
/**
* Обновить сделку
*/
public function updateDeal(int $dealId, array $data, int $userId): bool
{
$oldDeal = $this->dealModel->find($dealId);
if (!$oldDeal) {
return false;
}
$result = $this->dealModel->update($dealId, $data);
if ($result) {
$newDeal = $this->dealModel->find($dealId);
Events::trigger('deal.updated', [
'deal_id' => $dealId,
'old_deal' => $oldDeal,
'new_deal' => $newDeal,
'changes' => $data,
'user_id' => $userId,
]);
}
return $result;
}
/**
* Изменить этап сделки
*/
public function changeStage(int $dealId, int $newStageId, int $userId): bool
{
$deal = $this->dealModel->find($dealId);
if (!$deal) {
return false;
}
$newStage = $this->stageModel->find($newStageId);
if (!$newStage) {
return false;
}
$oldStageId = $deal['stage_id'];
$result = $this->dealModel->update($dealId, ['stage_id' => $newStageId]);
if ($result) {
$updatedDeal = $this->dealModel->find($dealId);
Events::trigger('deal.stage_changed', [
'deal_id' => $dealId,
'deal' => $updatedDeal,
'old_stage_id' => $oldStageId,
'new_stage_id' => $newStageId,
'old_stage' => $this->stageModel->find($oldStageId),
'new_stage' => $newStage,
'user_id' => $userId,
]);
}
return $result;
}
/**
* Мягко удалить сделку
*/
public function deleteDeal(int $dealId, int $userId): bool
{
$deal = $this->dealModel->find($dealId);
if (!$deal) {
return false;
}
$result = $this->dealModel->delete($dealId);
if ($result) {
Events::trigger('deal.deleted', [
'deal_id' => $dealId,
'deal' => $deal,
'user_id' => $userId,
]);
}
return $result;
}
/**
* Восстановить удалённую сделку
*/
public function restoreDeal(int $dealId, int $userId): bool
{
return $this->dealModel->delete($dealId, false);
}
/**
* Получить сделку по ID
*/
public function getDeal(int $dealId): ?array
{
return $this->dealModel->find($dealId);
}
/**
* Получить сделки с фильтрами
*/
public function getDeals(
int $organizationId,
?int $stageId = null,
?int $assignedUserId = null,
?string $search = null,
?string $dateFrom = null,
?string $dateTo = null
): array {
return $this->dealModel->getDealsByOrganization(
$organizationId,
$stageId,
$assignedUserId,
$search,
$dateFrom,
$dateTo
);
}
/**
* Получить сделки для Канбана
*/
public function getDealsForKanban(int $organizationId): array
{
return $this->dealModel->getDealsGroupedByStage($organizationId);
}
/**
* Получить сделки для календаря
*/
public function getDealsForCalendar(int $organizationId, string $month): array
{
return $this->dealModel->getDealsForCalendar($organizationId, $month);
}
/**
* Получить статистику по сделкам
*/
public function getStats(int $organizationId): array
{
return $this->dealModel->getDealStats($organizationId);
}
/**
* Получить сделку по ID с JOIN-ами
*/
public function getDealWithJoins(int $dealId, int $organizationId): ?array
{
return $this->dealModel->getWithJoins($dealId, $organizationId);
}
}