Skip to content

Commit

Permalink
Readded views, forms and controller for invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterZydra committed Jan 24, 2024
1 parent a48af9d commit d6c5b9e
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 0 deletions.
81 changes: 81 additions & 0 deletions app/Http/Controllers/InvoiceController.php
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');
}
}
128 changes: 128 additions & 0 deletions app/Models/Invoice.php
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);
}

}
2 changes: 2 additions & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Route::addController('mysecondroute', new MyController());
*/

use App\Http\Controllers\InvoiceController;
use App\Http\Controllers\PlotController;
use App\Http\Controllers\PriceController;
use App\Http\Controllers\ProductController;
Expand Down Expand Up @@ -38,6 +39,7 @@
Router::addController('cli', new WebCliController());
Router::addController('cli', new WebCliController(), 'POST');

Router::addModel('invoice', new InvoiceController());
Router::addModel('plot', new PlotController());
Router::addModel('price', new PriceController());
Router::addModel('product', new ProductController());
Expand Down
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;'
);
}
};
10 changes: 10 additions & 0 deletions resources/Lang/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

return [
'Actions' => 'Aktions',
'AddDeliveryNote' => 'Lieferschein hinzufügen',
'AddInvoice' => 'Rechnung hinzufügen',
'AddPlot' => 'Flurstück hinzufügen',
'AddPrice' => 'Preis hinzufügen',
'AddProduct' => 'Produkt hinzufügen',
Expand All @@ -14,9 +16,13 @@
'Create' => 'Erstellen',
'CreatedAt' => 'Erstellt am',
'DataManagement' => 'Datenverwaltung',
'Date' => 'Datum',
'Delete' => 'Löschen',
'DeliveryNote' => 'Lieferschein',
'Developer' => 'Entwickler',
'Edit' => 'Bearbeiten',
'EditDeliveryNote' => 'Lieferschein bearbeiten',
'EditInvoice' => 'Rechnung bearbeiten',
'EditPlot' => 'Flurstück bearbeiten',
'EditPrice' => 'Preis bearbeiten',
'EditProduct' => 'Produkt bearbeiten',
Expand All @@ -29,8 +35,10 @@
'Imprint' => 'Impressum',
'InsertSearchText' => 'Suchtext eingeben...',
'InvalidDataTypeForField' => 'Ungültiger Datentyp für das Feld "%s"',
'Invoice' => 'Rechnung',
'IsDiscontinued' => 'Ist abgekündigt',
'IsLocked' => 'Ist gesperrt',
'IsPaid' => 'Ist bezahlt',
'Lastname' => 'Nachname',
'Login' => 'Anmelden',
'Logout' => 'Abmelden',
Expand All @@ -56,6 +64,8 @@
'Recipient' => 'Empfänger',
'RepeatNewPassword' => 'Neues Passwort wiederholen',
'Save' => 'Speichern',
'ShowAllDeliveryNotes' => 'Alle Lieferschein anzeigen',
'ShowAllInvoices' => 'Alle Rechnungen anzeigen',
'ShowAllPlots' => 'Alle Flurstücke anzeigen',
'ShowAllPrices' => 'Alle Preise anzeigen',
'ShowAllProducts' => 'Alle Produkte anzeigen',
Expand Down
10 changes: 10 additions & 0 deletions resources/Lang/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

return [
'Actions' => 'Actions',
'AddDeliveryNote' => 'Add delivery note',
'AddInvoice' => 'Add invoice',
'AddPlot' => 'Add plot',
'AddPrice' => 'Add price',
'AddProduct' => 'Add product',
Expand All @@ -14,9 +16,13 @@
'Create' => 'Create',
'CreatedAt' => 'Created at',
'DataManagement' => 'Data management',
'Date' => 'Date',
'Delete' => 'Delete',
'DeliveryNote' => 'Delivery note',
'Developer' => 'Developer',
'Edit' => 'Edit',
'EditDeliveryNote' => 'Edit delivery note',
'EditInvoice' => 'Edit invoice',
'EditPlot' => 'Edit plot',
'EditPrice' => 'Edit price',
'EditProduct' => 'Edit product',
Expand All @@ -29,8 +35,10 @@
'Imprint' => 'Imprint',
'InsertSearchText' => 'Insert search text...',
'InvalidDataTypeForField' => 'Invalid data type for field "%s"',
'Invoice' => 'Invoice',
'IsDiscontinued' => 'Is discontinued',
'IsLocked' => 'Is locked',
'IsPaid' => 'Is paid',
'Lastname' => 'Lastname',
'Login' => 'Login',
'Logout' => 'Logout',
Expand All @@ -56,6 +64,8 @@
'Recipient' => 'Recipient',
'RepeatNewPassword' => 'Repeat new password',
'Save' => 'Save',
'ShowDeliveryNotes' => 'Show all delivery notes',
'ShowAllInvoices' => 'Show all invoices',
'ShowAllPlots' => 'Show all plots',
'ShowAllPrices' => 'Show all prices',
'ShowAllProducts' => 'Show all products',
Expand Down
18 changes: 18 additions & 0 deletions resources/Views/entities/invoice/create.php
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') ?>
Loading

0 comments on commit d6c5b9e

Please sign in to comment.