57 lines
1.8 KiB
PHP
57 lines
1.8 KiB
PHP
<?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;
|
||
}
|
||
}
|