44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class ValuationModel extends Model
|
|
{
|
|
protected $table = 'inv_valuations';
|
|
protected $primaryKey = 'id';
|
|
protected $returnType = 'array';
|
|
protected $useTimestamps = true;
|
|
protected $allowedFields = ['instrument_id', 'date', 'value'];
|
|
|
|
protected $validationRules = [
|
|
'instrument_id' => 'required|is_natural_no_zero',
|
|
'date' => 'required|valid_date[Y-m-d]',
|
|
'value' => 'required|decimal|greater_than_equal_to[0]',
|
|
];
|
|
|
|
/**
|
|
* Lista wycen z nazwa instrumentu, z opcjonalnym filtrem instrumentu.
|
|
* $f: instrument_id, from, to (wszystkie opcjonalne).
|
|
*/
|
|
public function filtered(array $f): array
|
|
{
|
|
$b = $this->db->table('inv_valuations v')
|
|
->select('v.*, i.name AS instrument_name')
|
|
->join('inv_instruments i', 'i.id = v.instrument_id', 'left');
|
|
|
|
if (! empty($f['instrument_id'])) {
|
|
$b->where('v.instrument_id', (int) $f['instrument_id']);
|
|
}
|
|
if (! empty($f['from'])) {
|
|
$b->where('v.date >=', $f['from']);
|
|
}
|
|
if (! empty($f['to'])) {
|
|
$b->where('v.date <=', $f['to']);
|
|
}
|
|
|
|
return $b->orderBy('v.date', 'DESC')->orderBy('v.id', 'DESC')->get()->getResultArray();
|
|
}
|
|
}
|