nhance/app/Models/SalesContactPersonModel.php

95 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
/**
* Contact Person Model
*/
class SalesContactPersonModel extends Model
{
protected $table = 'sales_contact_persons';
protected $primaryKey = 'contact_id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'lead_id',
'name',
'mobile',
'designation',
'email',
'is_primary',
'created_by',
'updated_by'
];
// Dates
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
// Validation
protected $validationRules = [
'lead_id' => 'required|integer',
'name' => 'required|min_length[2]|max_length[100]',
'mobile' => 'required|min_length[10]|max_length[20]',
];
protected $validationMessages = [
'lead_id' => [
'required' => 'Lead ID is required',
],
'name' => [
'required' => 'Contact person name is required',
],
'mobile' => [
'required' => 'Mobile number is required',
],
];
protected $skipValidation = false;
/**
* Get all contacts for a lead
*/
public function getContactsByLead($leadId)
{
return $this->where('lead_id', $leadId)
->orderBy('is_primary', 'DESC')
->findAll();
}
/**
* Get primary contact for a lead
*/
public function getPrimaryContact($leadId)
{
return $this->where('lead_id', $leadId)
->where('is_primary', 1)
->first();
}
/**
* Set a contact as primary (unset others)
*/
public function setPrimaryContact($contactId, $leadId)
{
$this->db->transStart();
// Unset all primary contacts for this lead
$this->where('lead_id', $leadId)
->set(['is_primary' => 0])
->update();
// Set the specified contact as primary
$this->update($contactId, ['is_primary' => 1]);
$this->db->transComplete();
return $this->db->transStatus();
}
}