nhance/app/Models/VehicleModel.php
2025-10-14 17:10:32 +05:30

90 lines
2.7 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class VehicleModel extends Model
{
protected $table = 'vehicle';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'id',
'vehicle_no',
'type',
'description',
'owner',
'branch_id',
'old_onwer',
'rc',
'is_active',
'created_by',
'updated_by',
'rto_id',
];
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = ["checkAndADDCreatedByValue"];
protected $afterInsert = [];
protected $beforeUpdate = ["checkAndUpdateUpdatedByValue"];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
protected function checkAndADDCreatedByValue(array $data)
{
// Check if 'updated_by' value is null or empty
if (empty($data['data']['created_by'])) {
// Set 'updated_by' value to the current session user ID
$data['data']['created_by'] = get_session_userid();
}
return $data;
}
protected function checkAndUpdateUpdatedByValue(array $data)
{
// Check if 'updated_by' value is null or empty
if (empty($data['data']['updated_by'])) {
// Set 'updated_by' value to the current session user ID
$data['data']['updated_by'] = get_session_userid();
}
return $data;
}
public function getVehicleMasterList(){
return $this->db->table('vehicle')
->select("
vehicle.*,
CASE
WHEN clients.client_type = 1 THEN 'Group'
WHEN clients.client_type = 2 THEN 'Individual'
ELSE '-'
END AS owner_type,
clients.client_name as owner_name,
client_branch.branch_name as owner_branch,
vehicle_type.vehicle_type
")
->join('clients', 'vehicle.owner = clients.id')
->join('client_branch', 'vehicle.branch_id = client_branch.id', 'left')
->join('vehicle_type', 'vehicle.type = vehicle_type.id', 'left')
->where('vehicle.is_active', 1)
->orderBy('vehicle.id', 'desc')
->get()
->getResultArray();
}
}