Skip to content

Commit

Permalink
Added a component to assign the delivery notes to an invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterZydra committed Feb 11, 2024
1 parent 5f35d8c commit 65ecf7a
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
21 changes: 17 additions & 4 deletions app/Http/Controllers/InvoiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Models\DeliveryNote;
use App\Models\Invoice;
use Framework\Database\Query\SortOrder;
use Framework\Facades\Http;
Expand Down Expand Up @@ -41,15 +42,20 @@ public function create(): void
*/
public function store(): void
{
$year = Http::param('year');
$nr = Invoice::nextInvoiceNr(Http::param('year'));

(new Invoice())
->setYear(Http::param('year'))
->setNr(Invoice::nextInvoiceNr(Http::param('year')))
->setYear($year)
->setNr($nr)
->setInvoiceDate(date('Y-m-d'))
->setRecipientId(Http::param('recipient'))
->setIsPaid(false)
->save();

$invoice = Invoice::findByYearAndNr($year, $nr);

Http::redirect('invoice');
Http::redirect('invoice/edit?id=' . $invoice->getId());
}

/**
Expand All @@ -67,12 +73,19 @@ public function edit(): void
*/
public function update(): void
{
Invoice::findById(Http::param('id'))
$invoice = Invoice::findById(Http::param('id'))
->setInvoiceDate(Http::param('invoiceDate'))
->setRecipientId(Http::param('recipient'))
->setIsPaid(Http::param('isPaid'))
->save();

foreach (Http::param('deliveryNote') as $deliveryNoteId) {
$parts = explode('-', $deliveryNoteId);
DeliveryNote::findById($parts[0])
->setInvoiceId($parts[1] === "1" ? $invoice->getId(): null)
->save();
}

Http::redirect('invoice');
}

Expand Down
16 changes: 15 additions & 1 deletion app/Models/DeliveryNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Framework\Database\Database;
use Framework\Database\Query\ColType;
use Framework\Database\Query\Condition;
use Framework\Database\Query\WhereCombine;
use Framework\Database\QueryBuilder;
use Framework\Facades\Convert;

Expand Down Expand Up @@ -62,6 +63,19 @@ public function save(): self
return $this;
}

public static function allReadyForInvoice(Invoice $invoice): array
{
return self::all(
self::getQueryBuilder()
->where(ColType::Int, 'isInvoiceReady', Condition::Equal, 1)
->where(ColType::Int, 'year', Condition::Equal, $invoice->getYear())
->where(ColType::Int, 'recipientId', Condition::Equal, $invoice->getRecipientId())
->where(ColType::Null, 'invoiceId', Condition::Is, null)
->where(ColType::Int, 'invoiceId', Condition::Equal, $invoice->getId(), WhereCombine::Or)
->orderBy('nr')
);
}

public function getProduct(): Product
{
return Product::findById($this->getProductId());
Expand All @@ -80,7 +94,7 @@ public function getRecipient(): Recipient
public static function nextDeliveryNoteNr(int $year = null): int
{
if ($year === null) {
$year = intval(date("Y"));
$year = intval(date('Y'));
}

$dataSet = Database::executeBuilder(
Expand Down
9 changes: 8 additions & 1 deletion app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public function save(): self
return $this;
}

public static function findByYearAndNr(int $year, int $nr): self
{
return self::find(self::getQueryBuilder()
->where(ColType::Int, 'year', Condition::Equal, $year)
->where(ColType::Int, 'nr', Condition::Equal, $nr));
}

public function getRecipient(): Recipient
{
return Recipient::findById($this->getRecipientId());
Expand All @@ -58,7 +65,7 @@ public function getRecipient(): Recipient
public static function nextInvoiceNr(int $year = null): int
{
if ($year === null) {
$year = intval(date("Y"));
$year = intval(date('Y'));
}

$dataSet = Database::executeBuilder(
Expand Down
1 change: 1 addition & 0 deletions resources/Lang/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'Date' => 'Datum',
'Delete' => 'Löschen',
'DeliveryNote' => 'Lieferschein',
'DeliveryNotes' => 'Lieferscheine',
'Developer' => 'Entwickler',
'Edit' => 'Bearbeiten',
'EditDeliveryNote' => 'Lieferschein bearbeiten',
Expand Down
1 change: 1 addition & 0 deletions resources/Lang/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'Date' => 'Date',
'Delete' => 'Delete',
'DeliveryNote' => 'Delivery note',
'DeliveryNotes' => 'Delivery notes',
'Developer' => 'Developer',
'Edit' => 'Edit',
'EditDeliveryNote' => 'Edit delivery note',
Expand Down
24 changes: 24 additions & 0 deletions resources/Views/Components/deliveryNoteList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
// Optional passed variables:
// $invoiceId => Current invoice id
if (!isset($invoiceId)) {
$invoiceId = null;
}
?>
<label for="deliveryNotes" class="required"><?= __('DeliveryNotes') ?>:</label><br>
<?php
$index = 0;
/** @var \App\Models\DeliveryNote $deliveryNote */
foreach ($deliveryNotes as $deliveryNote) {
?>
<label>
<input type="hidden" name="deliveryNote[<?= $index ?>]" value="<?= $deliveryNote->getId() ?>-0">
<input type="checkbox" name="deliveryNote[<?= $index ?>]" value="<?= $deliveryNote->getId() ?>-1"
<?php
if ($deliveryNote->getInvoiceId() === $invoiceId) {
echo ' checked';
}
?>>
<?= $deliveryNote->getYear() ?> <?= $deliveryNote->getNr() ?>
</label><br>
<?php $index++; } ?>
11 changes: 10 additions & 1 deletion resources/Views/entities/invoice/edit.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php /** @var \App\Models\Invoice $invoice */ ?>
<?php
use \App\Models\DeliveryNote;

/** @var \App\Models\Invoice $invoice */
?>
<?= component('layout.header') ?>

<h1><?= __('EditInvoice') ?></h1>
Expand Down Expand Up @@ -32,6 +36,11 @@
<?= __('IsPaid') ?>
</label><br>

<?= component('deliveryNoteList', [
'deliveryNotes' => DeliveryNote::allReadyForInvoice($invoice),
'invoiceId' => $invoice->getId(),
]) ?><br>

<button><?= __('Save') ?></button>
</form>

Expand Down

0 comments on commit 65ecf7a

Please sign in to comment.