backend-01

ref: master

app/daos/DashboardDAO.php


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php

require_once __DIR__ . '/../../config/database.php';

class DashboardDAO {
  private $db;

  public function __construct() {
    $this->db = getDatabaseConnection();
  }

  public function balance($userId) {
    $sql = 'SELECT
      (SELECT SUM(amount) FROM bills WHERE user_id = :user_id) AS total_expenses,
      (SELECT SUM(amount) FROM incomes WHERE user_id = :user_id) AS total_income';

    $stmt = $this->db->prepare($sql);
    $stmt->bindParam(':user_id', $userId);
    $stmt->execute();

    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    // Calculate balance
    $balance = $result['total_income'] - $result['total_expenses'];

    return $balance;
  }
}