-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Readded views, forms and controller for invoice
- Loading branch information
1 parent
a48af9d
commit d6c5b9e
Showing
9 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Invoice; | ||
use Framework\Facades\Http; | ||
use Framework\Routing\BaseController; | ||
use Framework\Routing\ModelControllerInterface; | ||
|
||
class InvoiceController extends BaseController implements ModelControllerInterface | ||
{ | ||
/** | ||
* Show list of all models | ||
* Route: <base route> | ||
*/ | ||
public function index(): void | ||
{ | ||
view('entities.invoice.index', ['invoices' => Invoice::all()]); | ||
} | ||
|
||
/** | ||
* Show form to create one model | ||
* Route: <base route>/create | ||
*/ | ||
public function create(): void | ||
{ | ||
view('entities.invoice.create'); | ||
} | ||
|
||
/** | ||
* Create a new model with the informations from the create form | ||
* Route: <base route>/store | ||
*/ | ||
public function store(): void | ||
{ | ||
(new Invoice()) | ||
->setYear(Http::param('year')) | ||
->setNr(Invoice::nextInvoiceNr(Http::param('year'))) | ||
->setInvoiceDate(date('Y-m-d')) | ||
->setRecipientId(Http::param('recipient')) | ||
->setIsPaid(false) | ||
->save(); | ||
|
||
Http::redirect('invoice'); | ||
} | ||
|
||
/** | ||
* Show form to edit one model | ||
* Route: <base route>/edit | ||
*/ | ||
public function edit(): void | ||
{ | ||
view('entities.invoice.edit', ['invoice' => Invoice::findById(Http::param('id'))]); | ||
} | ||
|
||
/** | ||
* Update one model with the informations from the edit form | ||
* Route: <base route>/update | ||
*/ | ||
public function update(): void | ||
{ | ||
Invoice::findById(Http::param('id')) | ||
->setInvoiceDate(Http::param('invoiceDate')) | ||
->setRecipientId(Http::param('recipient')) | ||
->setIsPaid(Http::param('isPaid')) | ||
->save(); | ||
|
||
Http::redirect('invoice'); | ||
} | ||
|
||
/** | ||
* Delete one model | ||
* Route: <base route>/destroy | ||
*/ | ||
public function destroy(): void | ||
{ | ||
Invoice::delete(Http::param('id')); | ||
|
||
Http::redirect('invoice'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Framework\Database\BaseModel; | ||
use Framework\Database\Database; | ||
use Framework\Facades\Convert; | ||
|
||
class Invoice extends BaseModel | ||
{ | ||
private const YEAR = 'year'; | ||
private const NR = 'nr'; | ||
private const INVOICE_DATE = 'invoiceDate'; | ||
private const RECIPIENT_ID = 'recipientId'; | ||
private const IS_PAID = 'isPaid'; | ||
|
||
protected static function new(array $data = []): self | ||
{ | ||
return new self($data); | ||
} | ||
|
||
public function save(): self | ||
{ | ||
if ($this->getId() === null) { | ||
Database::prepared( | ||
'INSERT INTO ' . $this->getTableName() . ' (`year`, nr, invoiceDate, recipientId, isPaid) VALUES (?, ?, ?, ?, ?)', | ||
'iisii', | ||
$this->getYear(), | ||
$this->getNr(), | ||
$this->getInvoiceDate(), | ||
$this->getRecipientId(), | ||
Convert::boolToInt($this->getIsPaid()) | ||
); | ||
} else { | ||
Database::prepared( | ||
'UPDATE ' . $this->getTableName() . ' SET `year`=?, nr=?, invoiceDate=?, recipientId=?, isPaid=? WHERE id=?', | ||
'iisiii', | ||
$this->getYear(), | ||
$this->getNr(), | ||
$this->getInvoiceDate(), | ||
$this->getRecipientId(), | ||
Convert::boolToInt($this->getIsPaid()), | ||
$this->getId() | ||
); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function getRecipient(): Recipient | ||
{ | ||
return Recipient::findById($this->getRecipientId()); | ||
} | ||
|
||
public static function nextInvoiceNr(int $year = null): int | ||
{ | ||
if ($year === null) { | ||
$year = intval(date("Y")); | ||
} | ||
|
||
$dataSet = Database::prepared( | ||
'SELECT MAX(nr) + 1 AS nextId FROM ' . self::getTableName() . ' WHERE `year` = ?', | ||
'i', | ||
$year | ||
); | ||
if ($dataSet === false || $dataSet->num_rows !== 1) { | ||
return 1; | ||
} | ||
$row = $dataSet->fetch_assoc(); | ||
if ($row['nextId'] === null) { | ||
return 1; | ||
} | ||
return $row['nextId']; | ||
} | ||
|
||
/* Getter & Setter */ | ||
|
||
public function getYear(): int | ||
{ | ||
return $this->getDataInt(self::YEAR); | ||
} | ||
|
||
public function setYear(int $value): self | ||
{ | ||
return $this->setDataInt(self::YEAR, $value); | ||
} | ||
|
||
public function getNr(): int | ||
{ | ||
return $this->getDataInt(self::NR); | ||
} | ||
|
||
public function setNr(int $value): self | ||
{ | ||
return $this->setDataInt(self::NR, $value); | ||
} | ||
|
||
public function getInvoiceDate(): string | ||
{ | ||
return $this->getDataString(self::INVOICE_DATE); | ||
} | ||
|
||
public function setInvoiceDate(string $value): self | ||
{ | ||
return $this->setDataString(self::INVOICE_DATE, $value); | ||
} | ||
|
||
public function getRecipientId(): int | ||
{ | ||
return $this->getDataInt(self::RECIPIENT_ID); | ||
} | ||
|
||
public function setRecipientId(int $value): self | ||
{ | ||
return $this->setDataInt(self::RECIPIENT_ID, $value); | ||
} | ||
|
||
public function getIsPaid(): bool | ||
{ | ||
return $this->getDataBool(self::IS_PAID); | ||
} | ||
|
||
public function setIsPaid(bool $value): self | ||
{ | ||
return $this->setDataBool(self::IS_PAID, $value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
resources/Database/Migrations/2024_01_23_075021_create_invoices_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
use Framework\Database\Database; | ||
use Framework\Database\Migration; | ||
|
||
return new class extends Migration | ||
{ | ||
public function run(): void | ||
{ | ||
Database::unprepared( | ||
'CREATE TABLE invoices (' . | ||
'id INT auto_increment,' . | ||
'`year` INT NOT NULL,' . | ||
'nr INT NOT NULL,' . | ||
'invoiceDate DATE DEFAULT NULL,' . | ||
'recipientId INT NOT NULL,' . | ||
'isPaid tinyint(1) NOT NULL DEFAULT 0,' . | ||
'createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,' . | ||
'updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,' . | ||
'PRIMARY KEY (id),' . | ||
'CONSTRAINT `fkInvoiceRecipient` FOREIGN KEY (recipientId) REFERENCES recipients (id) ON DELETE CASCADE' . | ||
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;' | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?= component('layout.header') ?> | ||
|
||
<h1><?= __('AddInvoice') ?></h1> | ||
|
||
<p> | ||
<a href="/invoice"><?= __('ShowAllInvoices') ?></a> | ||
</p> | ||
|
||
<form action="store" method="post"> | ||
<label for="year" class="required"><?= __('Year') ?>:</label><br> | ||
<input id="year" name="year" type="number" value="<?= date("Y") ?>" required autofocus><br> | ||
|
||
<?= component('recipientSelect') ?><br> | ||
|
||
<button><?= __('Create') ?></button> | ||
</form> | ||
|
||
<?= component('layout.footer') ?> |
Oops, something went wrong.