bp/app/Modules/CRM/Models/ContactModel.php

67 lines
1.8 KiB
PHP

<?php
namespace App\Modules\CRM\Models;
use CodeIgniter\Model;
use App\Models\Traits\TenantScopedModel;
class ContactModel extends Model
{
use TenantScopedModel;
protected $table = 'contacts';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = \App\Modules\CRM\Entities\Contact::class;
protected $useSoftDeletes = true;
protected $allowedFields = [
'organization_id',
'customer_id',
'name',
'email',
'phone',
'position',
'is_primary',
'notes',
];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
/**
* Получить контакты клиента
*/
public function getContactsForCustomer(int $customerId): array
{
return $this->where('customer_id', $customerId)->findAll();
}
/**
* Получить основной контакт клиента
*/
public function getPrimaryContact(int $customerId): ?object
{
return $this->where('customer_id', $customerId)
->where('is_primary', true)
->first();
}
/**
* Получить список контактов для выпадающего списка
*/
public function getContactsList(int $organizationId): array
{
$contacts = $this->where('organization_id', $organizationId)
->orderBy('name', 'ASC')
->findAll();
$list = [];
foreach ($contacts as $contact) {
$list[$contact->id] = $contact->name . ($contact->email ? " ({$contact->email})" : '');
}
return $list;
}
}