tinyqc/app/Models/DailyResultModel.php
mahdahar 18b85815ce refactor: standardize codebase with BaseModel and new conventions
- Add BaseModel with automatic camel/snake case conversion
- Add stringcase_helper with camel_to_snake(), snake_to_camel() functions
- Update all models to extend BaseModel for consistent data handling
- Update API controllers with standardized JSON response format
- Remove legacy v1 PHP application directory
- Consolidate documentation into AGENTS.md, delete VIEWS_RULES.md
2026-01-15 10:44:09 +07:00

62 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use App\Models\BaseModel;
class DailyResultModel extends BaseModel
{
protected $table = 'daily_result';
protected $primaryKey = 'daily_result_id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $useTimestamps = true;
protected $allowedFields = ['control_ref_id', 'test_ref_id', 'resdate', 'resvalue', 'rescomment'];
public function getByMonth($controlId, $testId, $yearMonth)
{
$startDate = $yearMonth . '-01';
$endDate = $yearMonth . '-31';
$builder = $this->db->table('daily_result');
$builder->select('*');
$builder->where('control_ref_id', $controlId);
$builder->where('test_ref_id', $testId);
$builder->where('resdate >=', $startDate);
$builder->where('resdate <=', $endDate);
$builder->orderBy('resdate', 'ASC');
return $builder->get()->getResultArray();
}
public function getByControlMonth($controlId, $yearMonth)
{
$startDate = $yearMonth . '-01';
$endDate = $yearMonth . '-31';
$builder = $this->db->table('daily_result');
$builder->select('*');
$builder->where('control_ref_id', $controlId);
$builder->where('resdate >=', $startDate);
$builder->where('resdate <=', $endDate);
return $builder->get()->getResultArray();
}
public function saveResult($data)
{
$builder = $this->db->table('daily_result');
$existing = $builder->select('*')
->where('control_ref_id', $data['control_ref_id'])
->where('test_ref_id', $data['test_ref_id'])
->where('resdate', $data['resdate'])
->get()
->getRowArray();
if ($existing) {
return $builder->where('daily_result_id', $existing['daily_result_id'])->update($data);
} else {
return $builder->insert($data);
}
}
}