nhance_partner_be/app/Models/PartnerInsurancePayoutGridModel.php

138 lines
4.2 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class PartnerInsurancePayoutGridModel extends Model
{
protected $table = 'partner_insurance_payout_grid';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $allowedFields = [
'vehicle_type',
'fuel',
'insurer',
'rto',
'broker_name',
'segment',
'comp',
'tp',
'od',
'remarks',
'broker_excel_comp',
'broker_excel_tp',
'broker_excel_od',
'partner_comp',
'partner_tp',
'partner_od',
'created_by',
'updated_by',
];
// Timestamps — map to the column names in your DDL
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
// Validation
protected $validationRules = [
'vehicle_type' => 'required|max_length[100]',
'insurer' => 'required|max_length[45]',
'segment' => 'required|max_length[100]',
];
protected $validationMessages = [
'vehicle_type' => ['required' => 'Vehicle type is required.'],
'insurer' => ['required' => 'Insurer is required.'],
'segment' => ['required' => 'Segment is required.'],
];
protected $skipValidation = false;
// -------------------------------------------------------------------------
// Find by the natural UPSERT key
// -------------------------------------------------------------------------
public function findByUpsertKey(
string $vehicleType,
string $insurer,
?string $rto,
string $segment
): ?array {
return $this
->where('vehicle_type', $vehicleType)
->where('insurer', $insurer)
->where('rto', $rto ?? '')
->where('segment', $segment)
->first();
}
// -------------------------------------------------------------------------
// Bulk UPSERT helper
// Accepts an array of grid rows and inserts or updates each one.
// Returns ['inserted' => int, 'updated' => int]
// -------------------------------------------------------------------------
public function bulkUpsert(array $gridRows, ?int $userId = null): array
{
$inserted = 0;
$updated = 0;
foreach ($gridRows as $row) {
$existing = $this->findByUpsertKey(
$row['vehicle_type'],
$row['insurer'],
$row['rto'] ?? null,
$row['segment']
);
$row['updated_by'] = $userId;
if (!empty($existing)) {
$this->update($existing['id'], $row);
$updated++;
} else {
$row['created_by'] = $userId;
$this->insert($row);
$inserted++;
}
}
return ['inserted' => $inserted, 'updated' => $updated];
}
// -------------------------------------------------------------------------
// Fetch grid filtered by vehicle_type
// -------------------------------------------------------------------------
public function getByVehicleType(string $vehicleType): array
{
return $this
->where('vehicle_type', $vehicleType)
->orderBy('insurer', 'ASC')
->findAll();
}
// -------------------------------------------------------------------------
// Fetch grid filtered by insurer + rto
// -------------------------------------------------------------------------
public function getByInsurerAndRto(string $insurer, string $rto): array
{
return $this
->where('insurer', $insurer)
->where('rto', $rto)
->orderBy('vehicle_type', 'ASC')
->findAll();
}
// -------------------------------------------------------------------------
// Update a grid row by id
// -------------------------------------------------------------------------
public function updateGridById(int $id, array $data): bool
{
return $this->update($id, $data);
}
}