49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class InvOperationModel extends Model
|
|
{
|
|
protected $table = 'inv_operations';
|
|
protected $primaryKey = 'id';
|
|
protected $returnType = 'array';
|
|
protected $useTimestamps = true;
|
|
protected $allowedFields = ['instrument_id', 'date', 'amount', 'type', 'description'];
|
|
|
|
protected $validationRules = [
|
|
'instrument_id' => 'required|is_natural_no_zero',
|
|
'date' => 'required|valid_date[Y-m-d]',
|
|
'amount' => 'required|decimal|greater_than[0]',
|
|
'type' => 'required|in_list[deposit,withdraw]',
|
|
'description' => 'permit_empty|max_length[255]',
|
|
];
|
|
|
|
/**
|
|
* Lista operacji inwestycyjnych z nazwa instrumentu, z filtrami.
|
|
* $f: from, to, instrument_id, type (wszystkie opcjonalne).
|
|
*/
|
|
public function filtered(array $f): array
|
|
{
|
|
$b = $this->db->table('inv_operations o')
|
|
->select('o.*, i.name AS instrument_name')
|
|
->join('inv_instruments i', 'i.id = o.instrument_id', 'left');
|
|
|
|
if (! empty($f['from'])) {
|
|
$b->where('o.date >=', $f['from']);
|
|
}
|
|
if (! empty($f['to'])) {
|
|
$b->where('o.date <=', $f['to']);
|
|
}
|
|
if (! empty($f['instrument_id'])) {
|
|
$b->where('o.instrument_id', (int) $f['instrument_id']);
|
|
}
|
|
if (! empty($f['type'])) {
|
|
$b->where('o.type', $f['type']);
|
|
}
|
|
|
|
return $b->orderBy('o.date', 'DESC')->orderBy('o.id', 'DESC')->get()->getResultArray();
|
|
}
|
|
}
|