nhance/app/Models/PartnerStaffModel.php

102 lines
3.0 KiB
PHP
Executable File
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\Models;
use CodeIgniter\Model;
class PartnerStaffModel extends Model
{
protected $table = 'partner_staff'; // table name
protected $primaryKey = 'id'; // primary key
protected $useAutoIncrement = true;
// return results as array
protected $returnType = 'array';
protected $useSoftDeletes = false;
// allowed fields for insert/update
protected $allowedFields = ['name','email','mobile','emp_id','role_id','email_otp','is_active','created_by','updated_by','manager_id','retention_rate','nhance_branch_id'];
// automatic timestamps
protected $useTimestamps = true;
protected $createdField = 'created_on';
protected $updatedField = 'updated_on';
protected $dateFormat = 'datetime';
// validation rules
// protected $validationRules = [
// 'name' => 'required|min_length[2]|max_length[150]',
// 'email' => 'permit_empty|valid_email|max_length[150]',
// 'mobile' => 'permit_empty|regex_match[/^[0-9]{10,20}$/]',
// ];
// protected $validationMessages = [
// 'name' => [
// 'required' => 'Name is required',
// 'min_length' => 'Name must have at least 2 characters',
// 'max_length' => 'Name cannot exceed 150 characters'
// ],
// 'email' => [
// 'valid_email' => 'Please provide a valid email address',
// 'max_length' => 'Email cannot exceed 150 characters'
// ],
// 'mobile' => [
// 'regex_match' => 'Mobile number must be 1020 digits only'
// ]
// ];
// protected $skipValidation = false;
public function updateByKey($key)
{
$db = \Config\Database::connect();
$message = "";
// Start transaction
$db->transStart();
try {
// 1. Update manager
$db->table('partner_staff')
->where('id', $key)
->set('is_active', 0)
->update();
$managerRows = $db->affectedRows();
$message = "{$managerRows} manager(s), ";
// 2. Update agents under manager
$db->table('partner_agent')
->where('manager_id', $key)
->set('is_active', 0)
->update();
$agentRows = $db->affectedRows();
$message .= "{$agentRows} agent(s), ";
// 3. Update staff under manager
$db->table('partner_staff')
->where('manager_id', $key)
->set('is_active', 0)
->update();
$staffRows = $db->affectedRows();
$message .= "{$staffRows} staff(s)";
// Complete transaction
$db->transComplete();
// Check transaction status
if ($db->transStatus() === false) {
return "Transaction failed. No updates were applied.";
}
return $message . " are deleted successfully";
} catch (\Exception $e) {
// Rollback in case of exception
$db->transRollback();
return "Error: " . $e->getMessage();
}
}
}