backend-01

commit 8cc358dc7c7c033de9c71138489350e9f26ad86c

Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>

dashboard: fetch and calculate balance

 app/controllers/DashboardController.php | 16 ++++++++++++--
 app/daos/BillDAO.php | 2 
 app/daos/DashboardDAO.php | 28 +++++++++++++++++++++++++++
 app/views/dashboard.php | 5 ++++


diff --git a/app/controllers/DashboardController.php b/app/controllers/DashboardController.php
index d68ca85f1ac63f22721af62935abba4d0286d16c..487590623250e4c47e32d6e4ed1be82822b8288c 100644
--- a/app/controllers/DashboardController.php
+++ b/app/controllers/DashboardController.php
@@ -1,14 +1,24 @@
 <?php
 
 require_once __DIR__ . '/../daos/BillDAO.php';
+require_once __DIR__ . '/../daos/DashboardDAO.php';
 
 class DashboardController {
+  private $dashboardDAO;
+  private $billDAO;
+
+  public function __construct() {
+    $this->dashboardDAO = new DashboardDAO();
+    $this->billDAO = new BillDAO();
+  }
+
   public function index() {
-    $billDAO = new BillDAO();
-    $bills = $billDAO->findAllByUserId($_SESSION['user_id']);
+    $bills = $this->billDAO->findAllByUserId($_SESSION['user_id']);
+    $balance = $this->dashboardDAO->balance($_SESSION['user_id']);
 
     $data = [
-      'bills' => $bills
+      'bills' => $bills,
+      'balance' => $balance
     ];
 
     return Template::render('dashboard', $data);




diff --git a/app/daos/BillDAO.php b/app/daos/BillDAO.php
index 31dde8a35744caa3ebbe294a30b719b82123869b..4f5f5e23f2346b03e803fff9aaa095a910c9e3ac 100644
--- a/app/daos/BillDAO.php
+++ b/app/daos/BillDAO.php
@@ -95,7 +95,7 @@
     return $bills;
   }
 
-  public function create(Bill $bill, $tagIds) {
+  public function create($bill, $tagIds) {
     $this->db->beginTransaction();
 
     try {




diff --git a/app/daos/DashboardDAO.php b/app/daos/DashboardDAO.php
new file mode 100644
index 0000000000000000000000000000000000000000..9880d6e3a521835efa2a23d3d63268c220fe85cd
--- /dev/null
+++ b/app/daos/DashboardDAO.php
@@ -0,0 +1,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;
+  }
+}




diff --git a/app/views/dashboard.php b/app/views/dashboard.php
index b0b9331113e168a8751be160c3233ff24c2c44d5..835def81f8dc59917717e5a5753a5ad3348ad00e 100644
--- a/app/views/dashboard.php
+++ b/app/views/dashboard.php
@@ -6,6 +6,11 @@       + Adicionar Novo Gasto
     </a>
   </div>
 
+  <div class="balance-card bg-white p-6 mb-8 rounded-lg shadow-lg max-w-xs mx-auto">
+    <h2 class="text-2xl font-semibold text-gray-800 mb-4">Balanço</h2>
+    <p class="text-4xl font-bold <?php echo ($balance > 0) ? 'text-blue-600' : 'text-red-600' ?> mb-2">$<?= number_format($balance, 2) ?></p>
+  </div>
+
   <?php if (empty($bills)) : ?>
     <div class="text-center py-10 bg-white rounded-lg shadow-md">
       <p class="text-xl text-gray-600">Nenhum gasto encontrado.</p>