Operacje: nowrap dla daty/kwoty, ucinanie opisu i stronicowanie 10/str
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -10,7 +10,7 @@ class OperationModel extends Model
|
||||
protected $primaryKey = 'id';
|
||||
protected $returnType = 'array';
|
||||
protected $useTimestamps = true;
|
||||
protected $allowedFields = ['date', 'amount', 'type', 'category_id', 'description'];
|
||||
protected $allowedFields = ['date', 'amount', 'type', 'category_id', 'receipt_id', 'description'];
|
||||
|
||||
protected $validationRules = [
|
||||
'date' => 'required|valid_date[Y-m-d]',
|
||||
@@ -23,14 +23,39 @@ class OperationModel extends Model
|
||||
/**
|
||||
* Lista operacji z nazwa kategorii, z filtrami.
|
||||
* $f: from, to, category_id, type (wszystkie opcjonalne).
|
||||
* Gdy $perPage > 0, zwraca tylko jedna strone (LIMIT/OFFSET).
|
||||
*/
|
||||
public function filtered(array $f): array
|
||||
public function filtered(array $f, int $perPage = 0, int $page = 1): array
|
||||
{
|
||||
$b = $this->db->table('operations o')
|
||||
->select('o.*, c.name AS category_name, p.name AS parent_name')
|
||||
->join('categories c', 'c.id = o.category_id', 'left')
|
||||
->join('categories p', 'p.id = c.parent_id', 'left');
|
||||
|
||||
$this->applyFilters($b, $f);
|
||||
|
||||
$b->orderBy('o.date', 'DESC')->orderBy('o.id', 'DESC');
|
||||
|
||||
if ($perPage > 0) {
|
||||
$b->limit($perPage, max(0, ($page - 1) * $perPage));
|
||||
}
|
||||
|
||||
return $b->get()->getResultArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Liczba operacji pasujacych do filtrow (do stronicowania).
|
||||
*/
|
||||
public function countFiltered(array $f): int
|
||||
{
|
||||
$b = $this->db->table('operations o');
|
||||
$this->applyFilters($b, $f);
|
||||
|
||||
return $b->countAllResults();
|
||||
}
|
||||
|
||||
private function applyFilters($b, array $f): void
|
||||
{
|
||||
if (! empty($f['from'])) {
|
||||
$b->where('o.date >=', $f['from']);
|
||||
}
|
||||
@@ -43,8 +68,6 @@ class OperationModel extends Model
|
||||
if (! empty($f['type'])) {
|
||||
$b->where('o.type', $f['type']);
|
||||
}
|
||||
|
||||
return $b->orderBy('o.date', 'DESC')->orderBy('o.id', 'DESC')->get()->getResultArray();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user