tinyqc/app/Models/Qc/ResultsModel.php
mahdahar dd7a058511 feat: Implement Monthly Entry interface and consolidate Entry API controller
- New EntryApiController (app/Controllers/Api/EntryApiController.php)
    - Centralized API for entry operations (daily/monthly data retrieval and saving)
    - getControls() - Fetch controls with optional date-based expiry filtering
    - getTests() - Get tests associated with a control
    - getDailyData() - Retrieve daily results for a date/control
    - getMonthlyData() - Retrieve monthly results with per-day data and comments
    - saveDaily() - Batch save daily results with validation
    - saveMonthly() - Batch save monthly results with statistics
  - New Monthly Entry View (app/Views/entry/monthly.php)
    - Calendar grid interface for entering monthly QC results
    - Month selector with quick navigation (prev/next/current)
    - Test selector to filter controls
    - 31-day grid per control with inline editing
    - Visual QC range indicators (green for in-range, red for out-of-range)
    - Weekend highlighting
    - Per-control monthly comment field
    - Keyboard shortcut (Ctrl+S) for saving
    - Change tracking with pending save indicator
  - Route Updates (app/Config/Routes.php)
    - Added /entry/monthly page route
    - Added /api/entry/daily GET endpoint
  - Model Updates
    - ResultsModel: Added updateMonthly() for upserting monthly results
    - ResultCommentsModel: Added upsertMonthly() for monthly comments
2026-01-20 16:47:11 +07:00

124 lines
3.7 KiB
PHP

<?php
namespace App\Models\Qc;
use App\Models\BaseModel;
class ResultsModel extends BaseModel {
protected $table = 'results';
protected $primaryKey = 'result_id';
protected $allowedFields = [
'control_id',
'test_id',
'res_date',
'res_value',
'res_comment',
'created_at',
'updated_at',
'deleted_at'
];
protected $useTimestamps = true;
protected $useSoftDeletes = true;
public function search($keyword = null) {
if ($keyword) {
return $this->groupStart()
->like('res_value', $keyword)
->groupEnd()
->findAll();
}
return $this->findAll();
}
/**
* Get results by date and control
*/
public function getByDateAndControl(string $date, int $controlId): array {
$builder = $this->db->table('results r');
$builder->select('
r.result_id as id,
r.control_id as controlId,
r.test_id as testId,
r.res_date as resDate,
r.res_value as resValue,
r.res_comment as resComment
');
$builder->where('r.res_date', $date);
$builder->where('r.control_id', $controlId);
$builder->where('r.deleted_at', null);
return $builder->get()->getResultArray();
}
/**
* Get results by month for a specific test (for monthly entry)
*/
public function getByMonth(int $testId, string $month): array {
$builder = $this->db->table('results r');
$builder->select('
r.result_id as id,
r.control_id as controlId,
r.test_id as testId,
r.res_date as resDate,
r.res_value as resValue,
r.res_comment as resComment
');
$builder->where('r.test_id', $testId);
$builder->where('r.res_date >=', $month . '-01');
$builder->where('r.res_date <=', $month . '-31');
$builder->where('r.deleted_at', null);
$builder->orderBy('r.res_date', 'ASC');
return $builder->get()->getResultArray();
}
/**
* Get results by control and month (for monthly entry calendar grid)
*/
public function getByControlAndMonth(int $controlId, int $testId, string $month): array {
$builder = $this->db->table('results r');
$builder->select('
r.result_id as id,
r.res_date as resDate,
r.res_value as resValue
');
$builder->where('r.control_id', $controlId);
$builder->where('r.test_id', $testId);
$builder->where('r.res_date >=', $month . '-01');
$builder->where('r.res_date <=', $month . '-31');
$builder->where('r.deleted_at', null);
$builder->orderBy('r.res_date', 'ASC');
return $builder->get()->getResultArray();
}
/**
* Upsert results (insert or update based on date/control/test)
*/
public function upsertResult(array $data): int {
// Check if record exists
$existing = $this->where('control_id', $data['control_id'])
->where('test_id', $data['test_id'])
->where('res_date', $data['res_date'])
->where('deleted_at', null)
->first();
if ($existing) {
$this->update($existing['resultId'], $data);
return $existing['resultId'];
} else {
return $this->insert($data, true);
}
}
/**
* Batch upsert results
*/
public function batchUpsertResults(array $results): array {
$ids = [];
foreach ($results as $result) {
$ids[] = $this->upsertResult($result);
}
return $ids;
}
}