142 lines
3.4 KiB
PHP
142 lines
3.4 KiB
PHP
<?php
|
||
|
||
namespace App\Modules\CRM\Services;
|
||
|
||
use App\Modules\CRM\Models\DealModel;
|
||
use App\Modules\CRM\Models\DealStageModel;
|
||
|
||
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;
|
||
|
||
return $this->dealModel->insert($data);
|
||
}
|
||
|
||
/**
|
||
* Обновить сделку
|
||
*/
|
||
public function updateDeal(int $dealId, array $data, int $userId): bool
|
||
{
|
||
$oldDeal = $this->dealModel->find($dealId);
|
||
if (!$oldDeal) {
|
||
return false;
|
||
}
|
||
|
||
return $this->dealModel->update($dealId, $data);
|
||
}
|
||
|
||
/**
|
||
* Изменить этап сделки
|
||
*/
|
||
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;
|
||
}
|
||
|
||
return $this->dealModel->update($dealId, ['stage_id' => $newStageId]);
|
||
}
|
||
|
||
/**
|
||
* Мягко удалить сделку
|
||
*/
|
||
public function deleteDeal(int $dealId, int $userId): bool
|
||
{
|
||
$deal = $this->dealModel->find($dealId);
|
||
if (!$deal) {
|
||
return false;
|
||
}
|
||
|
||
return $this->dealModel->delete($dealId);
|
||
}
|
||
|
||
/**
|
||
* Восстановить удалённую сделку
|
||
*/
|
||
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);
|
||
}
|
||
}
|