add CRM module start
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\CRM\Controllers;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Modules\CRM\Models\ContactModel;
|
||||
use App\Modules\Clients\Models\ClientModel;
|
||||
|
||||
class ContactsController extends BaseController
|
||||
{
|
||||
protected ContactModel $contactModel;
|
||||
protected ClientModel $clientModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->contactModel = new ContactModel();
|
||||
$this->clientModel = new ClientModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Список контактов
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$organizationId = $this->requireActiveOrg();
|
||||
|
||||
$contacts = $this->contactModel
|
||||
->where('organization_id', $organizationId)
|
||||
->orderBy('created_at', 'DESC')
|
||||
->findAll();
|
||||
|
||||
return $this->renderTwig('@CRM/contacts/index', [
|
||||
'title' => 'Контакты',
|
||||
'contacts' => $contacts,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Форма создания контакта
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$organizationId = $this->requireActiveOrg();
|
||||
|
||||
$clients = $this->clientModel
|
||||
->where('organization_id', $organizationId)
|
||||
->findAll();
|
||||
|
||||
return $this->renderTwig('@CRM/contacts/form', [
|
||||
'title' => 'Новый контакт',
|
||||
'actionUrl' => '/crm/contacts',
|
||||
'clients' => $clients,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Сохранить новый контакт
|
||||
*/
|
||||
public function store()
|
||||
{
|
||||
$organizationId = $this->requireActiveOrg();
|
||||
|
||||
$data = [
|
||||
'organization_id' => $organizationId,
|
||||
'customer_id' => $this->request->getPost('customer_id') ?: null,
|
||||
'name' => $this->request->getPost('name'),
|
||||
'email' => $this->request->getPost('email') ?: null,
|
||||
'phone' => $this->request->getPost('phone') ?: null,
|
||||
'position' => $this->request->getPost('position') ?: null,
|
||||
'is_primary' => $this->request->getPost('is_primary') ? 1 : 0,
|
||||
'notes' => $this->request->getPost('notes') ?: null,
|
||||
];
|
||||
|
||||
$this->contactModel->save($data);
|
||||
$contactId = $this->contactModel->getInsertID();
|
||||
|
||||
if ($contactId) {
|
||||
return redirect()->to('/crm/contacts')->with('success', 'Контакт успешно создан');
|
||||
}
|
||||
|
||||
return redirect()->back()->with('error', 'Ошибка при создании контакта')->withInput();
|
||||
}
|
||||
|
||||
/**
|
||||
* Форма редактирования контакта
|
||||
*/
|
||||
public function edit(int $id)
|
||||
{
|
||||
$organizationId = $this->requireActiveOrg();
|
||||
|
||||
$contact = $this->contactModel->find($id);
|
||||
|
||||
if (!$contact || $contact->organization_id !== $organizationId) {
|
||||
return redirect()->to('/crm/contacts')->with('error', 'Контакт не найден');
|
||||
}
|
||||
|
||||
$clients = $this->clientModel
|
||||
->where('organization_id', $organizationId)
|
||||
->findAll();
|
||||
|
||||
return $this->renderTwig('@CRM/contacts/form', [
|
||||
'title' => 'Редактирование контакта',
|
||||
'actionUrl' => "/crm/contacts/{$id}",
|
||||
'contact' => $contact,
|
||||
'clients' => $clients,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновить контакт
|
||||
*/
|
||||
public function update(int $id)
|
||||
{
|
||||
$organizationId = $this->requireActiveOrg();
|
||||
|
||||
$contact = $this->contactModel->find($id);
|
||||
|
||||
if (!$contact || $contact->organization_id !== $organizationId) {
|
||||
return redirect()->to('/crm/contacts')->with('error', 'Контакт не найден');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'customer_id' => $this->request->getPost('customer_id') ?: null,
|
||||
'name' => $this->request->getPost('name'),
|
||||
'email' => $this->request->getPost('email') ?: null,
|
||||
'phone' => $this->request->getPost('phone') ?: null,
|
||||
'position' => $this->request->getPost('position') ?: null,
|
||||
'is_primary' => $this->request->getPost('is_primary') ? 1 : 0,
|
||||
'notes' => $this->request->getPost('notes') ?: null,
|
||||
];
|
||||
|
||||
$this->contactModel->update($id, $data);
|
||||
|
||||
return redirect()->to('/crm/contacts')->with('success', 'Контакт обновлён');
|
||||
}
|
||||
|
||||
/**
|
||||
* Удалить контакт
|
||||
*/
|
||||
public function destroy(int $id)
|
||||
{
|
||||
$organizationId = $this->requireActiveOrg();
|
||||
|
||||
$contact = $this->contactModel->find($id);
|
||||
|
||||
if (!$contact || $contact->organization_id !== $organizationId) {
|
||||
return redirect()->to('/crm/contacts')->with('error', 'Контакт не найден');
|
||||
}
|
||||
|
||||
$this->contactModel->delete($id);
|
||||
|
||||
return redirect()->to('/crm/contacts')->with('success', 'Контакт удалён');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user