Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
bill: allow to annex a pdf file
app/controllers/BillsController.php | 16 +++++++++++++++- app/daos/BillDAO.php | 5 +++-- app/models/Bill.php | 12 +++++++++++- app/views/bill_create.php | 7 ++++++- migrations/add_pdf_column.sql | 1 +
diff --git a/app/controllers/BillsController.php b/app/controllers/BillsController.php index fbafefe31c24055153b5629fbaa7b9fd836fd727..9d5063f6277c3e65772c0d33e39b975ed50160a3 100644 --- a/app/controllers/BillsController.php +++ b/app/controllers/BillsController.php @@ -34,6 +34,19 @@ } public function store() { $data = $_POST; + + $pdfPath = null; + if (isset($_FILES['pdf']) && $_FILES['pdf']['error'] === UPLOAD_ERR_OK) { + $uploadDir = '../storage/uploads/'; + $fileName = uniqid() . '_' . basename($_FILES['pdf']['name']); + $pdfPath = $uploadDir . $fileName; + + if (!move_uploaded_file($_FILES['pdf']['tmp_name'], $pdfPath)) { + echo "Failed to upload PDF."; + return; + } + } + $bill = new Bill( null, $data['title'], @@ -41,7 +54,8 @@ $data['amount'], $data['due_date'], isset($data['paid']) ? true : false, $_SESSION['user_id'], - [] + [], + $pdfPath ); $this->billDAO->create($bill, $data['tags']); diff --git a/app/daos/BillDAO.php b/app/daos/BillDAO.php index 5b57045bfeed168afda2718765641d2d6c1b5514..7208c3046d59bc898b064fc927ac3650a5d00260 100644 --- a/app/daos/BillDAO.php +++ b/app/daos/BillDAO.php @@ -100,8 +100,8 @@ $this->db->beginTransaction(); try { $stmt = $this->db->prepare(' - INSERT INTO bills (title, amount, due_date, paid, user_id) - VALUES (:title, :amount, :due_date, :paid, :user_id) + 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()); @@ -109,6 +109,7 @@ $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(); diff --git a/app/models/Bill.php b/app/models/Bill.php index 18cec99b4cf17f00241059660735715bdd3ba052..2f752e6ddc2a54c02dd0e04d98e254623372f611 100644 --- a/app/models/Bill.php +++ b/app/models/Bill.php @@ -5,11 +5,12 @@ private $title; private $amount; private $dueDate; private $paid; + private $pdfPath; private $userId; private $tags; - public function __construct($id, $title, $amount, $dueDate, $paid, $userId, $tags) { + public function __construct($id, $title, $amount, $dueDate, $paid, $userId, $tags, $pdfPath = null) { $this->id = $id; $this->title = $title; $this->amount = $amount; @@ -17,6 +18,7 @@ $this->dueDate = $dueDate; $this->paid = $paid; $this->userId = $userId; $this->tags = $tags; + $this->pdfPath = $pdfPath; } public function getTitle() { @@ -57,6 +59,14 @@ } public function setTags($tags) { $this->tags = $tags; + } + + public function getPdfPath() { + return $this->pdfPath; + } + + public function setPdfPath($pdfPath) { + $this->pdfPath = $pdfPath; } public function toArray() { diff --git a/app/views/bill_create.php b/app/views/bill_create.php index eb2ae5d2b32e6a390ded886f9120ed75d3a10dca..1eaeb94efc287407fbb47b833d15e3e32f07f151 100644 --- a/app/views/bill_create.php +++ b/app/views/bill_create.php @@ -1,7 +1,7 @@ <div class="container mx-auto mt-10"> <h1 class="text-2xl font-bold text-gray-700 mb-6">Adicionar Novo Gasto</h1> - <form action="/bills/create" method="POST" class="bg-white p-6 rounded shadow-md"> + <form action="/bills/create" 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" class="mt-1 block w-full border border-gray-300 rounded p-2" required /> @@ -28,6 +28,11 @@ = htmlspecialchars($tag->getName()) ?> </label> </div> <?php endforeach; ?> + </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"> diff --git a/migrations/add_pdf_column.sql b/migrations/add_pdf_column.sql new file mode 100644 index 0000000000000000000000000000000000000000..057536fbd0c590a9a4b9aa6a1966d8e55e8aef6c --- /dev/null +++ b/migrations/add_pdf_column.sql @@ -0,0 +1 @@ +ALTER TABLE bills ADD COLUMN pdf_path TEXT;