backend-01

ref: master

app/controllers/BillsController.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
<?php

require_once __DIR__ . '/../daos/BillDAO.php';
require_once __DIR__ . '/../daos/TagDAO.php';
require_once __DIR__ . '/../models/Bill.php';
require_once __DIR__ . '/../services/FileUploadingService.php';

class BillsController {
  private $billDAO;
  private $tagDAO;

  public function __construct() {
    $this->billDAO = new BillDAO();
    $this->tagDAO = new TagDAO();
  }

  public function index() {
    $tags = $this->tagDAO->getAllTagsFromUser($_SESSION['user_id']);

    $bills = $this->billDAO->getBillsByTagIdWithinRange(
      $_SESSION['user_id'],
      $_GET['tag_id'] ?? null ,
      $_GET['start_date'] ?? null,
      $_GET['end_date'] ?? null
    );

    return Template::render('bills', ['bills' => $bills, 'tags' => $tags]);
  }

  public function create() {
    $tags = $this->tagDAO->getAllTagsFromUser($_SESSION['user_id']);

    return Template::render('bill_create', ['tags' => $tags]);
  }

  public function store() {
    $data = $_POST;
    $pdfPath = null;

    if (!empty($data['pdf'])) {
      $fileUploadingService = new FileUploadingService();
      $uploadResult = $fileUploadingService->upload($_FILES['pdf'], 'bill_');

      if ($uploadResult['success']) {
        $pdfPath = $uploadResult['filePath'];
      } elseif (isset($uploadResult['error'])) {
        echo 'Error: ' . $uploadResult['error'];
        return;
      }
    }

    $bill = new Bill(
      null,
      $data['title'],
      $data['amount'],
      $data['due_date'],
      isset($data['paid']) ? true : false,
      $_SESSION['user_id'],
      [],
      $pdfPath
    );

    $this->billDAO->create($bill, $data['tags'] ?? []);

    header('Location: /dashboard');
    exit;
  }

  public function edit($id) {
    $bill = $this->billDAO->getBillById($id);
    $tags = $this->tagDAO->getAllTagsFromUser($_SESSION['user_id']);
    $billTags = $this->billDAO->getTagsByBillId($id);

    $tagIds = array_map(function($tag) {
      return $tag->id;
    }, $billTags);

    if ($bill) {
      return Template::render('bill_edit', [
        'bill' => $bill,
        'tags' => $tags,
        'tagIds' => $tagIds
      ]);
    } else {
      // TODO: Properly handle this shit
      echo "Bill not found.";
    }
  }

  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
      echo "All fields are required.";

      return;
    }

    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;
  }

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

    header('Location: /dashboard');
    exit;
  }
}