Bicycle/App/Repositories/TransactionRepository.php
Egor Isaev ac1334c784 dev
2026-08-14 14:05:41 +03:00

57 lines
1.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @package Bicycle
* @author Egor Isaev
* @description TransactionRepository.php
* @copyright (c) 11/08/2026
*/
namespace App\Repositories;
use PDO;
use System\Classes\Repository;
/**
* Репозиторий таблицы transactions (income/expense/transfer).
*/
class TransactionRepository extends Repository
{
/**
* @param string|null $connection Имя подключения (Database::instance($connection)); null — по умолчанию
*/
public function __construct(?string $connection = null)
{
parent::__construct('transactions', connection: $connection);
}
/**
* Факт за месяц по статьям — сумма реальных операций (income/expense, не transfer — у тех
* categorie_id = null), в рублях (см. бюджет_текущий_план.md — summa * COALESCE(rate_to_rub, 1)).
* Для дашборда/плана: план/факт по категории.
*
* @param int $user_id
* @param int $year
* @param int $month
* @return array<int,float> [categorie_id => сумма]
*/
public function getMonthActualByCategory(int $user_id, int $year, int $month): array
{
$sql = "SELECT categorie_id, SUM(summa * COALESCE(rate_to_rub, 1)) AS s
FROM $this->table
WHERE user_id = :user_id AND categorie_id IS NOT NULL
AND YEAR(date) = :year AND MONTH(date) = :month
GROUP BY categorie_id";
$stmt = $this->pdo->prepare($sql);
$stmt->execute(['user_id' => $user_id, 'year' => $year, 'month' => $month]);
$result = [];
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$result[(int)$row['categorie_id']] = (float)$row['s'];
}
return $result;
}
}