Skip to content

Commit 367a6d2

Browse files
committed
refactor serial_number access on invoice
1 parent b04fd0d commit 367a6d2

File tree

6 files changed

+100
-49
lines changed

6 files changed

+100
-49
lines changed

src/GenerateSerialNumber.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ interface GenerateSerialNumber
88
{
99
public function __construct(string $format = null, string $prefix = null);
1010

11-
public function generate(int $count, int $serie = null, Carbon $date = null): string;
11+
public function generate(
12+
int $count,
13+
int $serie = null,
14+
string|int|null $year = null,
15+
string|int|null $month = null
16+
): string;
1217

1318
public function parse(string $serialNumber): array;
1419
}

src/Invoice.php

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,7 @@ class Invoice extends Model implements Attachable
4949
{
5050
use HasFactory;
5151

52-
/**
53-
* Allow setting serie on the fly for the generation of the serialNumber
54-
*/
55-
public ?int $serie = null;
56-
57-
/**
58-
* Allow setting prefix on the fly for the generation of the serialNumber
59-
*/
60-
public ?string $prefix = null;
61-
62-
protected $fillable = [
63-
'serial_number',
64-
'description',
65-
'seller_information',
66-
'buyer_information',
67-
'state',
68-
'due_at',
69-
'state_set_at',
70-
'tax_type',
71-
'tax_exempt',
72-
'type',
73-
];
52+
protected $guarded = [];
7453

7554
protected $casts = [
7655
'type' => InvoiceType::class,
@@ -88,10 +67,7 @@ public static function booted()
8867
{
8968
static::creating(function (Invoice $invoice) {
9069
if (config('invoices.serial_number.auto_generate')) {
91-
$invoice->serial_number = $invoice->generateSerialNumber(
92-
serie: $invoice->getSerialNumberSerie(),
93-
date: now()
94-
);
70+
$invoice->serial_number = $invoice->generateSerialNumber();
9571

9672
$invoice->serial_number_details = new ArrayObject(
9773
$invoice->parseSerialNumber()
@@ -171,17 +147,83 @@ public function getLatestSerialNumber(): ?string
171147
return $latestInvoice?->serial_number;
172148
}
173149

174-
public function getSerialNumberSerie(): ?int
150+
function initSerialNumberDetailst(): static
151+
{
152+
if (!$this->serial_number_details) {
153+
$this->serial_number_details = new ArrayObject();
154+
}
155+
return $this;
156+
}
157+
158+
public function setSerialNumberPrefix(string $value): static
159+
{
160+
$this->initSerialNumberDetailst();
161+
$this->serial_number_details['prefix'] = $value;
162+
return $this;
163+
}
164+
165+
public function setSerialNumberSerie(int $value): static
166+
{
167+
$this->initSerialNumberDetailst();
168+
$this->serial_number_details['serie'] = $value;
169+
return $this;
170+
}
171+
172+
public function setSerialNumberYear(int $value): static
173+
{
174+
$this->initSerialNumberDetailst();
175+
$this->serial_number_details['year'] = $value;
176+
return $this;
177+
}
178+
179+
public function setSerialNumberMonth(int $value): static
180+
{
181+
$this->initSerialNumberDetailst();
182+
$this->serial_number_details['month'] = $value;
183+
return $this;
184+
}
185+
186+
public function setSerialNumberCount(int $value): static
187+
{
188+
$this->initSerialNumberDetailst();
189+
$this->serial_number_details['count'] = $value;
190+
return $this;
191+
}
192+
193+
public function setSerialNumberDate(Carbon $value): static
175194
{
176-
return $this->serie;
195+
$this->initSerialNumberDetailst();
196+
$this->serial_number_details['year'] = (int) $value->format('Y');
197+
$this->serial_number_details['month'] = (int) $value->format('m');
198+
return $this;
177199
}
178200

179201
public function getSerialNumberPrefix(): ?string
180202
{
181-
return $this->prefix ?? config('invoices.serial_number.prefix');
203+
return data_get($this->serial_number_details, 'prefix', config('invoices.serial_number.prefix'));
204+
}
205+
206+
public function getSerialNumberSerie(): ?int
207+
{
208+
return data_get($this->serial_number_details, 'serie');
209+
}
210+
211+
public function getSerialNumberYear(): ?int
212+
{
213+
return data_get($this->serial_number_details, 'year');
214+
}
215+
216+
public function getSerialNumberMonth(): ?int
217+
{
218+
return data_get($this->serial_number_details, 'month');
219+
}
220+
221+
public function getSerialNumberCount(): ?int
222+
{
223+
return data_get($this->serial_number_details, 'count');
182224
}
183225

184-
public function generateSerialNumber(int $serie = null, Carbon $date = null): string
226+
public function generateSerialNumber(): string
185227
{
186228
$generator = new SerialNumberGenerator(prefix: $this->getSerialNumberPrefix());
187229
$latestSerialNumber = $this->getLatestSerialNumber();
@@ -194,8 +236,9 @@ public function generateSerialNumber(int $serie = null, Carbon $date = null): st
194236
}
195237

196238
return $generator->generate(
197-
serie: $serie,
198-
date: $date ?? now(),
239+
serie: $this->getSerialNumberSerie(),
240+
year: $this->getSerialNumberYear() ?? now()->format('Y'),
241+
month: $this->getSerialNumberMonth() ?? now()->format('m'),
199242
count: $latestCount + 1
200243
);
201244
}

src/InvoiceItem.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ class InvoiceItem extends Model
3030
{
3131
use HasFactory;
3232

33-
protected $fillable = [];
33+
protected $guarded = [];
3434

3535
protected $casts = [
3636
/**
3737
* This cast will be forwarded to the class defined in config at invoices.money_cast
3838
*/
39-
'unit_price' => MoneyCast::class.':currency',
40-
'unit_tax' => MoneyCast::class.':currency',
39+
'unit_price' => MoneyCast::class . ':currency',
40+
'unit_tax' => MoneyCast::class . ':currency',
4141
'metadata' => AsArrayObject::class,
4242
'tax_percentage' => 'float',
4343
];

src/SerialNumberGenerator.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ public function __construct(
1414
$this->prefix = $prefix ?? config('invoices.serial_number.prefix', '');
1515
}
1616

17-
public function generate(int $count, int $serie = null, Carbon $date = null): string
18-
{
17+
public function generate(
18+
int $count,
19+
int $serie = null,
20+
string|int|null $year = null,
21+
string|int|null $month = null,
22+
): string {
1923
return preg_replace_callback_array(
2024
[
2125
'/S+/' => function ($matches) use ($serie) {
22-
if (! $matches[0]) {
26+
if (!$matches[0]) {
2327
return '';
2428
}
2529
$slotLength = strlen($matches[0]);
26-
throw_if(! $serie, "The serial Number format includes a $slotLength long Serie (S), but no serie has been passed");
30+
throw_if(!$serie, "The serial Number format includes a $slotLength long Serie (S), but no serie has been passed");
2731

2832
$serieLength = strlen(strval($serie));
2933
throw_if(
@@ -38,10 +42,10 @@ public function generate(int $count, int $serie = null, Carbon $date = null): st
3842
STR_PAD_LEFT
3943
);
4044
},
41-
'/M+/' => fn ($matches) => $matches[0] && $date ? substr($date->format('m'), -strlen($matches[0])) : '',
42-
'/Y+/' => fn ($matches) => $matches[0] && $date ? substr($date->format('Y'), -strlen($matches[0])) : '',
45+
'/M+/' => fn ($matches) => $matches[0] && $month ? substr((string) $month, -strlen($matches[0])) : '',
46+
'/Y+/' => fn ($matches) => $matches[0] && $year ? substr((string) $year, -strlen($matches[0])) : '',
4347
'/C+/' => function ($matches) use ($count) {
44-
if (! $matches[0]) {
48+
if (!$matches[0]) {
4549
return '';
4650
}
4751
throw_if(
@@ -58,7 +62,7 @@ public function generate(int $count, int $serie = null, Carbon $date = null): st
5862
},
5963
// Must be kept last to avoid interfering with other callbacks
6064
'/P+/' => function ($matches) {
61-
if (! $matches[0]) {
65+
if (!$matches[0]) {
6266
return '';
6367
}
6468
$slotLength = strlen($matches[0]);

tests/Feature/InvoiceTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
/** @var Invoice */
3838
$invoice = Invoice::factory()->make();
3939

40-
$invoice->serie = 42;
40+
$invoice->setSerialNumberSerie(42);
4141

4242
expect($invoice->getSerialNumberSerie())->toBe(42);
4343

@@ -54,8 +54,8 @@
5454
/** @var Invoice */
5555
$invoice = Invoice::factory()->make();
5656

57-
$invoice->serie = 42;
58-
$invoice->prefix = 'ORG';
57+
$invoice->setSerialNumberSerie(42);
58+
$invoice->setSerialNumberPrefix('ORG');
5959

6060
expect($invoice->getSerialNumberSerie())->toBe(42);
6161
expect($invoice->getSerialNumberPrefix())->toBe('ORG');

tests/Unit/SerialNumberTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use Carbon\Carbon;
43
use Finller\Invoice\SerialNumberGenerator;
54

65
it('can generate serial number from format', function ($format, $prefix, $serie, $count, $expected) {
@@ -9,7 +8,7 @@
98
prefix: $prefix
109
);
1110

12-
$serialNumber = $generator->generate(count: $count, serie: $serie, date: Carbon::parse('2022-01-01'));
11+
$serialNumber = $generator->generate(count: $count, serie: $serie, year: "2022", month: '01');
1312

1413
expect($serialNumber)->toBe($expected);
1514
})->with([

0 commit comments

Comments
 (0)