Skip to content

Commit dfaf6a2

Browse files
committed
allow any type and state
1 parent 6431709 commit dfaf6a2

File tree

6 files changed

+69
-41
lines changed

6 files changed

+69
-41
lines changed

database/factories/InvoiceFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public function definition()
2727
);
2828

2929
return [
30-
'type' => InvoiceType::Invoice,
31-
'state' => fake()->randomElement(InvoiceState::cases()),
30+
'type' => InvoiceType::Invoice->value,
31+
'state' => InvoiceState::Draft->value,
3232
'state_set_at' => fake()->dateTimeBetween($created_at),
3333
'updated_at' => fake()->dateTimeBetween($created_at),
3434
'created_at' => $created_at,

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->getLabel() }}</strong>
7+
<strong>{{ $invoice->type }}</strong>
88
</h1>
99
<p class="mb-5 text-sm">
10-
{{ $invoice->state->getLabel() }}
10+
{{ $invoice->state }}
1111
</p>
1212

1313
<table class="w-full">

src/InvoiceServiceProvider.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,24 @@ public function configurePackage(Package $package): void
3434
->hasMigration('migrate_serial_number_details_columns_to_invoices_table');
3535
}
3636

37-
public static function getSerialNumberPrefixConfiguration(?InvoiceType $type): ?string
37+
public static function getSerialNumberPrefixConfiguration(null|string|InvoiceType $type): ?string
3838
{
39+
$value = $type instanceof InvoiceType ? $type->value : $type;
40+
3941
/** @var string|array<string, string> $prefixes */
4042
$prefixes = config('invoices.serial_number.prefix', '');
4143

4244
if (is_string($prefixes)) {
4345
return $prefixes;
4446
}
4547

46-
return $prefixes[$type?->value] ?? null;
48+
return $prefixes[$value] ?? null;
4749
}
4850

49-
public static function getSerialNumberFormatConfiguration(?InvoiceType $type): string
51+
public static function getSerialNumberFormatConfiguration(null|string|InvoiceType $type): string
5052
{
53+
$value = $type instanceof InvoiceType ? $type->value : $type;
54+
5155
/** @var string|array<string, string> $formats */
5256
$formats = config('invoices.serial_number.format') ?? '';
5357

@@ -56,10 +60,10 @@ public static function getSerialNumberFormatConfiguration(?InvoiceType $type): s
5660
}
5761

5862
/** @var ?string $format */
59-
$format = $formats[$type?->value] ?? null;
63+
$format = $formats[$value] ?? null;
6064

6165
if (! $format) {
62-
throw new Exception("No serial number format defined in config for type: {$type?->value}.");
66+
throw new Exception("No serial number format defined in config for type: {$value}.");
6367
}
6468

6569
return $format;

src/Models/Invoice.php

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
* @property ?Invoice $parent
3636
* @property ?Invoice $quote
3737
* @property ?Invoice $credit
38-
* @property InvoiceType $type
38+
* @property string $type
39+
* @property string $state
40+
* @property ?Carbon $state_set_at
3941
* @property string $description
4042
* @property ?array<string, mixed> $seller_information
4143
* @property ?array<string, mixed> $buyer_information
42-
* @property InvoiceState $state
43-
* @property ?Carbon $state_set_at
4444
* @property ?Carbon $due_at
4545
* @property ?string $tax_type
4646
* @property ?string $tax_exempt
@@ -79,26 +79,30 @@ class Invoice extends Model implements Attachable
7979
use HasFactory;
8080

8181
protected $attributes = [
82-
'type' => InvoiceType::Invoice,
83-
'state' => InvoiceState::Draft,
82+
'type' => InvoiceType::Invoice->value,
83+
'state' => InvoiceState::Draft->value,
8484
];
8585

8686
protected $guarded = [];
8787

88-
protected $casts = [
89-
'type' => InvoiceType::class,
90-
'state_set_at' => 'datetime',
91-
'due_at' => 'datetime',
92-
'state' => InvoiceState::class,
93-
'seller_information' => 'array',
94-
'buyer_information' => 'array',
95-
'metadata' => 'array',
96-
'discounts' => Discounts::class,
97-
'subtotal_amount' => MoneyCast::class.':currency',
98-
'discount_amount' => MoneyCast::class.':currency',
99-
'tax_amount' => MoneyCast::class.':currency',
100-
'total_amount' => MoneyCast::class.':currency',
101-
];
88+
/**
89+
* @return array<string, string>
90+
*/
91+
protected function casts(): array
92+
{
93+
return [
94+
'state_set_at' => 'datetime',
95+
'due_at' => 'datetime',
96+
'seller_information' => 'array',
97+
'buyer_information' => 'array',
98+
'metadata' => 'array',
99+
'discounts' => Discounts::class,
100+
'subtotal_amount' => MoneyCast::class.':currency',
101+
'discount_amount' => MoneyCast::class.':currency',
102+
'tax_amount' => MoneyCast::class.':currency',
103+
'total_amount' => MoneyCast::class.':currency',
104+
];
105+
}
102106

103107
public static function booted()
104108
{
@@ -463,11 +467,21 @@ public function getLogo(): ?string
463467
return null;
464468
}
465469

470+
public function getType(): string|InvoiceType
471+
{
472+
return InvoiceType::tryFrom($this->type) ?? $this->type;
473+
}
474+
475+
public function getState(): string|InvoiceState
476+
{
477+
return InvoiceState::tryFrom($this->state) ?? $this->state;
478+
}
479+
466480
public function toPdfInvoice(): PdfInvoice
467481
{
468482
return new PdfInvoice(
469-
type: $this->type,
470-
state: $this->state,
483+
type: $this->getType(),
484+
state: $this->getState(),
471485
serial_number: $this->serial_number,
472486
due_at: $this->due_at,
473487
created_at: $this->created_at,

src/Models/InvoiceItem.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ class InvoiceItem extends Model
3939

4040
protected $guarded = [];
4141

42-
protected $casts = [
43-
/**
44-
* This cast will be forwarded to the class defined in config at invoices.money_cast
45-
*/
46-
'unit_price' => MoneyCast::class.':currency',
47-
'unit_tax' => MoneyCast::class.':currency',
48-
'metadata' => AsArrayObject::class,
49-
'tax_percentage' => 'float',
50-
];
42+
/**
43+
* @return array<string, string>
44+
*/
45+
protected function casts(): array
46+
{
47+
return [
48+
'unit_price' => MoneyCast::class.':currency',
49+
'unit_tax' => MoneyCast::class.':currency',
50+
'metadata' => AsArrayObject::class,
51+
'tax_percentage' => 'float',
52+
];
53+
}
5154

5255
/**
5356
* @return BelongsTo<Invoice, $this>

src/Pdf/PdfInvoice.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class PdfInvoice
2222
{
2323
use FormatForPdf;
2424

25+
public string $type;
26+
27+
public string $state;
28+
2529
public string $template;
2630

2731
/**
@@ -32,8 +36,8 @@ class PdfInvoice
3236
* @param array<string, mixed> $templateData
3337
*/
3438
public function __construct(
35-
public InvoiceType $type = InvoiceType::Invoice,
36-
public InvoiceState $state = InvoiceState::Draft,
39+
InvoiceType|string $type = InvoiceType::Invoice,
40+
InvoiceState|string $state = InvoiceState::Draft,
3741
public ?string $serial_number = null,
3842
public ?Carbon $created_at = null,
3943
public ?Carbon $due_at = null,
@@ -53,6 +57,9 @@ public function __construct(
5357

5458
public ?string $logo = null,
5559
) {
60+
$this->type = $type instanceof InvoiceType ? $type->getLabel() : $type;
61+
$this->state = $state instanceof InvoiceState ? $state->getLabel() : $state;
62+
5663
// @phpstan-ignore-next-line
5764
$this->logo = $logo ?? config('invoices.pdf.logo') ?? config('invoices.default_logo');
5865
// @phpstan-ignore-next-line

0 commit comments

Comments
 (0)