Skip to content

Commit 7796336

Browse files
committed
introduce HasLabel
1 parent 93b11f5 commit 7796336

File tree

9 files changed

+92
-22
lines changed

9 files changed

+92
-22
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,41 @@ However, you might prefer to cast these properties to Enum objects for better ty
10271027

10281028
To enable Enum casting for these properties, follow these steps:
10291029

1030+
0. **Create custom `InvoiceState` and `InvoiceType` Enums** (optional):
1031+
1032+
If you're working with commonly used invoice states and types, you can use the enums provided by this package:
1033+
1034+
- `Elegantly\Invoices\Enums\InvoiceState`
1035+
- `Elegantly\Invoices\Enums\InvoiceType`
1036+
1037+
For custom states or types, you can define your own enums.
1038+
1039+
Make sure your custom enums implement the `Elegantly\Invoices\Contracts\HasLabel` contract, like so:
1040+
1041+
```php
1042+
namespace App\Enums;
1043+
1044+
use Elegantly\Invoices\Contracts\HasLabel;
1045+
1046+
enum InvoiceType: string implements HasLabel
1047+
{
1048+
case Invoice = 'invoice';
1049+
case Quote = 'quote';
1050+
case Credit = 'credit';
1051+
case Proforma = 'proforma';
1052+
1053+
public function getLabel(): string
1054+
{
1055+
return match ($this) {
1056+
self::Invoice => __('invoices::invoice.types.invoice'),
1057+
self::Quote => __('invoices::invoice.types.quote'),
1058+
self::Credit => __('invoices::invoice.types.credit'),
1059+
self::Proforma => __('invoices::invoice.types.proforma'),
1060+
};
1061+
}
1062+
}
1063+
```
1064+
10301065
1. **Create a Custom `Invoice` Model**:
10311066

10321067
Define your own `App\Models\Invoice` class that extends `\Elegantly\Invoices\Models\Invoice`.
@@ -1038,8 +1073,17 @@ namespace App\Models;
10381073
use Elegantly\Invoices\Enums\InvoiceState;
10391074
use Elegantly\Invoices\Enums\InvoiceType;
10401075

1076+
/**
1077+
* @property InvoiceType $type
1078+
* @property InvoiceState $state
1079+
*/
10411080
class Invoice extends \Elegantly\Invoices\Models\Invoice
10421081
{
1082+
protected $attributes = [
1083+
'type' => InvoiceType::Invoice->value,
1084+
'state' => InvoiceState::Draft->value,
1085+
];
1086+
10431087
protected function casts(): array
10441088
{
10451089
return [

resources/views/default/invoice.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<tr>
55
<td class="p-0 align-top">
66
<h1 class="mb-1 text-2xl">
7-
<strong>{{ $invoice->type }}</strong>
7+
<strong>{{ $invoice->getTypeLabel() }}</strong>
88
</h1>
99
<p class="mb-5 text-sm">
10-
{{ $invoice->state }}
10+
{{ $invoice->getStateLabel() }}
1111
</p>
1212

1313
<table class="w-full">

src/Concerns/FormatForPdf.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Elegantly\Invoices\Concerns;
66

77
use Brick\Money\Money;
8+
use Illuminate\Support\Facades\App;
89
use NumberFormatter;
910

1011
trait FormatForPdf
@@ -20,8 +21,10 @@ public function formatPercentage(null|float|int $percentage, ?string $locale = n
2021
return null;
2122
}
2223

23-
$formatter = new NumberFormatter($locale ?? app()->getLocale(), NumberFormatter::PERCENT);
24+
$percentage = ($percentage > 1) ? ($percentage / 100) : $percentage;
2425

25-
return $formatter->format(($percentage > 1) ? ($percentage / 100) : $percentage);
26+
$formatter = new NumberFormatter($locale ?? App::getLocale(), NumberFormatter::PERCENT);
27+
28+
return $formatter->format($percentage);
2629
}
2730
}

src/Contracts/HasLabel.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Elegantly\Invoices\Contracts;
6+
7+
interface HasLabel
8+
{
9+
public function getLabel(): ?string;
10+
}

src/Enums/InvoiceState.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Elegantly\Invoices\Enums;
66

7-
enum InvoiceState: string
7+
use Elegantly\Invoices\Contracts\HasLabel;
8+
9+
enum InvoiceState: string implements HasLabel
810
{
911
case Draft = 'draft';
1012
case Pending = 'pending';

src/Enums/InvoiceType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Elegantly\Invoices\Enums;
66

7-
enum InvoiceType: string
7+
use Elegantly\Invoices\Contracts\HasLabel;
8+
9+
enum InvoiceType: string implements HasLabel
810
{
911
case Invoice = 'invoice';
1012
case Quote = 'quote';

src/InvoiceServiceProvider.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace Elegantly\Invoices;
66

7+
use BackedEnum;
78
use Elegantly\Invoices\Commands\DenormalizeInvoicesCommand;
8-
use Elegantly\Invoices\Enums\InvoiceType;
99
use Exception;
1010
use Spatie\LaravelPackageTools\Package;
1111
use Spatie\LaravelPackageTools\PackageServiceProvider;
1212

13+
use function Illuminate\Support\enum_value;
14+
1315
class InvoiceServiceProvider extends PackageServiceProvider
1416
{
1517
public function configurePackage(Package $package): void
@@ -34,9 +36,10 @@ public function configurePackage(Package $package): void
3436
->hasMigration('migrate_serial_number_details_columns_to_invoices_table');
3537
}
3638

37-
public static function getSerialNumberPrefixConfiguration(null|string|InvoiceType $type): ?string
39+
public static function getSerialNumberPrefixConfiguration(null|string|BackedEnum $type): ?string
3840
{
39-
$value = $type instanceof InvoiceType ? $type->value : $type;
41+
/** @var null|int|string */
42+
$value = enum_value($type);
4043

4144
/** @var string|array<string, string> $prefixes */
4245
$prefixes = config('invoices.serial_number.prefix', '');
@@ -48,9 +51,10 @@ public static function getSerialNumberPrefixConfiguration(null|string|InvoiceTyp
4851
return $prefixes[$value] ?? null;
4952
}
5053

51-
public static function getSerialNumberFormatConfiguration(null|string|InvoiceType $type): string
54+
public static function getSerialNumberFormatConfiguration(null|string|BackedEnum $type): string
5255
{
53-
$value = $type instanceof InvoiceType ? $type->value : $type;
56+
/** @var null|int|string */
57+
$value = enum_value($type);
5458

5559
/** @var string|array<string, string> $formats */
5660
$formats = config('invoices.serial_number.format') ?? '';

src/Models/Invoice.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Brick\Money\Money;
88
use Carbon\Carbon;
99
use Elegantly\Invoices\Casts\Discounts;
10+
use Elegantly\Invoices\Contracts\HasLabel;
1011
use Elegantly\Invoices\Database\Factories\InvoiceFactory;
1112
use Elegantly\Invoices\Enums\InvoiceState;
1213
use Elegantly\Invoices\Enums\InvoiceType;
@@ -465,12 +466,12 @@ public function getLogo(): ?string
465466
return null;
466467
}
467468

468-
public function getType(): string|InvoiceType
469+
public function getType(): string|HasLabel
469470
{
470471
return InvoiceType::tryFrom($this->type) ?? $this->type;
471472
}
472473

473-
public function getState(): string|InvoiceState
474+
public function getState(): string|HasLabel
474475
{
475476
return InvoiceState::tryFrom($this->state) ?? $this->state;
476477
}

src/Pdf/PdfInvoice.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Carbon\Carbon;
1010
use Dompdf\Dompdf;
1111
use Elegantly\Invoices\Concerns\FormatForPdf;
12+
use Elegantly\Invoices\Contracts\HasLabel;
1213
use Elegantly\Invoices\Enums\InvoiceState;
1314
use Elegantly\Invoices\Enums\InvoiceType;
1415
use Elegantly\Invoices\InvoiceDiscount;
@@ -25,10 +26,6 @@ class PdfInvoice
2526
{
2627
use FormatForPdf;
2728

28-
public string $type;
29-
30-
public string $state;
31-
3229
public string $template;
3330

3431
/**
@@ -40,8 +37,8 @@ class PdfInvoice
4037
* @param array<string, mixed> $templateData
4138
*/
4239
public function __construct(
43-
InvoiceType|string $type = InvoiceType::Invoice,
44-
InvoiceState|string $state = InvoiceState::Draft,
40+
public HasLabel|string $type = InvoiceType::Invoice,
41+
public HasLabel|string $state = InvoiceState::Draft,
4542
public ?string $serial_number = null,
4643
public ?Carbon $created_at = null,
4744
public ?Carbon $due_at = null,
@@ -63,9 +60,6 @@ public function __construct(
6360

6461
public ?string $logo = null,
6562
) {
66-
$this->type = $type instanceof InvoiceType ? $type->getLabel() : $type;
67-
$this->state = $state instanceof InvoiceState ? $state->getLabel() : $state;
68-
6963
// @phpstan-ignore-next-line
7064
$this->logo = $logo ?? config('invoices.pdf.logo') ?? config('invoices.default_logo');
7165
// @phpstan-ignore-next-line
@@ -74,6 +68,16 @@ public function __construct(
7468
$this->templateData = config('invoices.pdf.template_data') ?? [];
7569
}
7670

71+
public function getTypeLabel(): ?string
72+
{
73+
return $this->type instanceof HasLabel ? $this->type->getLabel() : $this->type;
74+
}
75+
76+
public function getStateLabel(): ?string
77+
{
78+
return $this->state instanceof HasLabel ? $this->state->getLabel() : $this->state;
79+
}
80+
7781
public function getFilename(): string
7882
{
7983
return str($this->serial_number)

0 commit comments

Comments
 (0)