Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
bill: allow to update/edit a bill
app/controllers/BillsController.php | 31 ++++++++++++++++++++++++++++++- app/daos/BillDAO.php | 30 +++++++++++++++++++++--------- app/models/Bill.php | 4 ++++ app/views/bill_edit.php | 15 ++++++++++++++-
diff --git a/app/controllers/BillsController.php b/app/controllers/BillsController.php index 910092015afd2517858f3abe275c770b03f82bc9..63ef3e74ed6c651e18a57dbd9d4f6496dc5f3f95 100644 --- a/app/controllers/BillsController.php +++ b/app/controllers/BillsController.php @@ -86,11 +86,17 @@ } } public function update($id) { + $fileUploadingService = new FileUploadingService(); + $bill = $this->billDAO->getBillById($id); + $data = $_POST; $title = $data['title']; $amount = $data['amount']; $dueDate = $data['due_date']; + $paid = $data['paid'] ?? $bill->isPaid(); $tags = $data['tags'] ?? []; + $pdfPath = $bill->getPdfPath(); + if (empty($title) || empty($amount) || empty($dueDate)) { // TODO: Flash messages @@ -99,7 +105,30 @@ return; } - $this->billDAO->updateBill($id, $title, $amount, $dueDate, $tags); + if (!empty($_FILES['pdf']['name'])) { + if ($pdfPath && file_exists(__DIR__ . '/../../' . $pdfPath)) { + error_log("File exists: " . $pdfPath); + $fileUploadingService->deleteOldFile($pdfPath); + } + + $uploadResult = $fileUploadingService->upload($_FILES['pdf'], 'bill_'); + + if ($uploadResult['success']) { + $pdfPath = $uploadResult['filePath']; + } elseif (isset($uploadResult['error'])) { + echo 'Error: ' . $uploadResult['error']; + throw new Exception('Error uploading file' . $uploadResult['error']); + return; + } + } + + $bill->setTitle($title); + $bill->setAmount($amount); + $bill->setDueDate($dueDate); + $bill->setPaid($paid); + $bill->setPdfPath($pdfPath); + + $this->billDAO->updateBill($bill, $tags); header('Location: /dashboard'); exit; diff --git a/app/daos/BillDAO.php b/app/daos/BillDAO.php index 7208c3046d59bc898b064fc927ac3650a5d00260..31dde8a35744caa3ebbe294a30b719b82123869b 100644 --- a/app/daos/BillDAO.php +++ b/app/daos/BillDAO.php @@ -153,24 +153,36 @@ $billData->amount, $billData->due_date, $billData->paid, $billData->user_id, - [] + [], + $billData->pdf_path ); } } - public function updateBill($id, $title, $amount, $due_date, $tags) { - $sql = 'UPDATE bills SET title = :title, amount = :amount, due_date = :due_date WHERE id = :id'; + 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->bindParam(':title', $title); - $stmt->bindParam(':amount', $amount); - $stmt->bindParam(':due_date', $due_date); - $stmt->bindParam(':id', $id); + $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($id); + $this->removeTagsFromBill($bill->getId()); foreach ($tags as $tagId) { - $this->addTagToBill($id, $tagId); + $this->addTagToBill($bill->getId(), $tagId); } } diff --git a/app/models/Bill.php b/app/models/Bill.php index 2f752e6ddc2a54c02dd0e04d98e254623372f611..46e76cc65df3e9572cc097db5924b57c72de86c7 100644 --- a/app/models/Bill.php +++ b/app/models/Bill.php @@ -37,6 +37,10 @@ public function isPaid() { return $this->paid; } + public function setTitle($title) { + $this->title = $title; + } + public function setAmount($amount) { $this->amount = $amount; } diff --git a/app/views/bill_edit.php b/app/views/bill_edit.php index 6b0f5bf8f49b490a57417952f6c609c2336a6804..c7e55c753221950af0378b0516ea2d81b7efe0bf 100644 --- a/app/views/bill_edit.php +++ b/app/views/bill_edit.php @@ -1,7 +1,7 @@ <div class="container mx-auto mt-10"> <h1 class="text-2xl font-bold text-gray-700 mb-6">Editar Gasto</h1> - <form action="/bills/edit/<?= $bill->getId() ?>" method="POST"> + <form action="/bills/edit/<?= $bill->getId() ?>" method="POST" enctype="multipart/form-data" class="bg-white p-6 rounded shadow-md"> <div class="mb-4"> <label for="title" class="block text-sm font-medium text-gray-700">TÃtulo</label> <input type="text" id="title" name="title" value="<?= htmlspecialchars($bill->getTitle()) ?>" @@ -31,6 +31,19 @@ <?php endforeach; ?> </div> </div> + + <div class="mb-4"> + <label for="pdf" class="block text-sm font-medium text-gray-700">Anexar boleto</label> + <input type="file" id="pdf" name="pdf" class="mt-1 block w-full text-gray-700"> + </div> + + <div class="mb-4"> + <label class="flex items-center text-sm font-medium text-gray-700"> + <input type="checkbox" name="paid" class="mr-2" checked="<?= $bill->isPaid() ?>" /> + Pago? + </label> + </div> + <div class="flex items-center space-x-4"> <button type="submit" class="bg-blue-600 text-white py-2 px-4 rounded hover:bg-blue-700">