backend-01

ref: master

app/daos/BillDAO.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
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php

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

class BillDAO {
  private $db;

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

  public function findAllByUserId($userId) {
    $sql = 'SELECT b.*, GROUP_CONCAT(t.name, ", ") AS tags
            FROM bills b
            LEFT JOIN bill_tags bt ON b.id = bt.bill_id
            LEFT JOIN tags t ON bt.tag_id = t.id
            WHERE b.user_id = :user_id
            GROUP BY b.id';

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

    $bills = [];

    while ($billData = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $bills[] = new Bill(
        $billData['id'],
        $billData['title'],
        $billData['amount'],
        $billData['due_date'],
        $billData['paid'],
        $billData['user_id'],
        $billData['tags']
      );
    }

    return $bills;
  }

  public function getBillsByTagIdWithinRange($userId, $tagId, $startDate, $endDate) {
    $sql = 'SELECT b.*, GROUP_CONCAT(t.name, ", ") AS tags
            FROM bills b
            LEFT JOIN bill_tags bt ON b.id = bt.bill_id
            LEFT JOIN tags t ON bt.tag_id = t.id
            WHERE b.user_id = :user_id';

    if ($tagId) {
      $sql .= ' AND b.id IN (
        SELECT bill_id FROM bill_tags WHERE tag_id = :tag_id
      )';
    }

    if ($startDate) {
      $sql .= ' AND b.due_date >= :start_date';
    }

    if ($endDate) {
      $sql .= ' AND b.due_date <= :end_date';
    }

    $sql .= ' GROUP BY b.id ORDER BY b.due_date DESC';

    $stmt = $this->db->prepare($sql);
    if ($tagId) {
      $stmt->bindValue(':tag_id', $tagId);
    }

    if ($startDate) {
      $stmt->bindValue(':start_date', $startDate);
    }

    if ($endDate) {
      $stmt->bindValue(':end_date', $endDate);
    }

    $stmt->bindValue(':user_id', $userId);
    $stmt->execute();

    $bills = [];

    while ($billData = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $bills[] = new Bill(
        $billData['id'],
        $billData['title'],
        $billData['amount'],
        $billData['due_date'],
        $billData['paid'],
        $billData['user_id'],
        $billData['tags']
      );
    }

    return $bills;
  }

  public function create($bill, $tagIds) {
    $this->db->beginTransaction();

    try {
      $stmt = $this->db->prepare('
        INSERT INTO bills (title, amount, due_date, paid, user_id, pdf_path)
        VALUES (:title, :amount, :due_date, :paid, :user_id, :pdf_path)
      ');

      $stmt->bindValue(':title', $bill->getTitle());
      $stmt->bindValue(':amount', $bill->getAmount());
      $stmt->bindValue(':due_date', $bill->getDueDate());
      $stmt->bindValue(':paid', $bill->isPaid());
      $stmt->bindValue(':user_id', $bill->getUserId());
      $stmt->bindValue(':pdf_path', $bill->getPdfPath());
      $stmt->execute();

      $billId = $this->db->lastInsertId();

      foreach ($tagIds as $tagId) {
        $stmt = $this->db->prepare('
          INSERT INTO bill_tags (bill_id, tag_id)
          VALUES (:bill_id, :tag_id)
        ');

        $stmt->bindParam(':bill_id', $billId);
        $stmt->bindParam(':tag_id', $tagId);
        $stmt->execute();
      }

      $this->db->commit();

      $bill->setId($billId);
      $bill->setTags($tagIds);

      return $bill;
    } catch (Exception $e) {
      $this->db->rollBack();
      throw $e;
    }
  }

  public function getBillById($id) {
    $sql = 'SELECT * FROM bills WHERE id = :id';
    $stmt = $this->db->prepare($sql);
    $stmt->bindParam(':id', $id);
    $stmt->execute();

    $billData = $stmt->fetch(PDO::FETCH_OBJ);

    if ($billData) {
      return new Bill(
        $billData->id,
        $billData->title,
        $billData->amount,
        $billData->due_date,
        $billData->paid,
        $billData->user_id,
        [],
        $billData->pdf_path
      );
    }
  }

  public function updateBill($bill, $tags) {
    $sql = 'UPDATE bills
      SET title = :title,
          amount = :amount,
          due_date = :due_date,
          paid = :paid,
          pdf_path = :pdf_path
      WHERE id = :id';

    $stmt = $this->db->prepare($sql);
    $stmt->bindValue(':id', $bill->getId());

    $stmt->bindValue(':title', $bill->getTitle());
    $stmt->bindValue(':amount', $bill->getAmount());
    $stmt->bindValue(':due_date', $bill->getDueDate());
    $stmt->bindValue(':paid', $bill->isPaid());
    $stmt->bindValue(':pdf_path', $bill->getPdfPath());

    $stmt->execute();

    $this->removeTagsFromBill($bill->getId());

    foreach ($tags as $tagId) {
      $this->addTagToBill($bill->getId(), $tagId);
    }
  }

  private function removeTagsFromBill($billId) {
    $sql = 'DELETE FROM bill_tags WHERE bill_id = :bill_id';
    $stmt = $this->db->prepare($sql);
    $stmt->bindParam(':bill_id', $billId);
    $stmt->execute();
  }

  private function addTagToBill($billId, $tagId) {
    $sql = 'INSERT INTO bill_tags (bill_id, tag_id) VALUES (:bill_id, :tag_id)';
    $stmt = $this->db->prepare($sql);
    $stmt->bindParam(':bill_id', $billId);
    $stmt->bindParam(':tag_id', $tagId);
    $stmt->execute();
  }

  public function getTagsByBillId($billId) {
    $sql = 'SELECT t.id, t.name
      FROM tags t
      JOIN bill_tags bt ON t.id = bt.tag_id
      WHERE bt.bill_id = :bill_id';
    $stmt = $this->db->prepare($sql);
    $stmt->bindParam(':bill_id', $billId);
    $stmt->execute();

    return $stmt->fetchAll(PDO::FETCH_OBJ);
  }

  public function destroy($id) {
    $this->db->beginTransaction();

    try {
      $stmt = $this->db->prepare('DELETE FROM bills WHERE id = :id');
      $stmt->bindParam(':id', $id);
      $stmt->execute();

      $stmt = $this->db->prepare('DELETE FROM bill_tags WHERE bill_id = :bill_id');
      $stmt->bindParam(':bill_id', $id);
      $stmt->execute();

      $this->db->commit();
    } catch (Exception $e) {
      $this->db->rollBack();
      throw $e;
    }
  }
}