Passed
Push — main ( 7fcf59...53a5b5 )
by Jacobo
02:34
created

Invoice::getOperationDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Squareetlabs\VeriFactu\Models;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\SoftDeletes;
9
use Illuminate\Database\Eloquent\Factories\HasFactory;
10
use Squareetlabs\VeriFactu\Enums\InvoiceType;
11
use Squareetlabs\VeriFactu\Contracts\VeriFactuInvoice;
12
use Illuminate\Support\Collection;
13
use Carbon\Carbon;
14
15
class Invoice extends Model implements VeriFactuInvoice
16
{
17
    use HasFactory;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Database\Eloquent\Factories\HasFactory requires the property $factoryClass which is not provided by Squareetlabs\VeriFactu\Models\Invoice.
Loading history...
18
    use SoftDeletes;
19
20
    protected static function newFactory()
21
    {
22
        return \Database\Factories\Squareetlabs\VeriFactu\Models\InvoiceFactory::new();
23
    }
24
25
    protected static function booted()
26
    {
27
        static::saving(function ($invoice) {
28
            // Preparar datos para el hash
29
            $hashData = [
30
                'issuer_tax_id' => $invoice->issuer_tax_id,
31
                'invoice_number' => $invoice->number,
32
                'issue_date' => $invoice->date instanceof \Illuminate\Support\Carbon ? $invoice->date->format('Y-m-d') : $invoice->date,
33
                'invoice_type' => $invoice->type instanceof \BackedEnum ? $invoice->type->value : (string) $invoice->type,
0 ignored issues
show
Bug introduced by
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
                'total_tax' => (string) $invoice->tax,
35
                'total_amount' => (string) $invoice->total,
36
                'previous_hash' => $invoice->previous_hash ?? '', // Si implementas encadenamiento
37
                'generated_at' => now()->format('c'),
38
            ];
39
            $hashResult = \Squareetlabs\VeriFactu\Helpers\HashHelper::generateInvoiceHash($hashData);
40
            $invoice->hash = $hashResult['hash'];
41
        });
42
    }
43
44
    protected $table = 'invoices';
45
46
    protected $fillable = [
47
        'uuid',
48
        'number',
49
        'date',
50
        'customer_name',
51
        'customer_tax_id',
52
        'customer_country',
53
        'issuer_name',
54
        'issuer_tax_id',
55
        'issuer_country',
56
        'amount',
57
        'tax',
58
        'total',
59
        'type',
60
        'external_reference',
61
        'description',
62
        'status',
63
        'issued_at',
64
        'cancelled_at',
65
        'hash',
66
        'operation_date',
67
        'tax_period',
68
        'correction_type',
69
    ];
70
71
    protected $casts = [
72
        'date' => 'date',
73
        'operation_date' => 'date',
74
        'type' => InvoiceType::class,
75
        'amount' => 'decimal:2',
76
        'tax' => 'decimal:2',
77
        'total' => 'decimal:2',
78
    ];
79
80
    public function breakdowns()
81
    {
82
        return $this->hasMany(Breakdown::class);
83
    }
84
85
    public function recipients()
86
    {
87
        return $this->hasMany(Recipient::class);
88
    }
89
90
    // VeriFactuInvoice Contract Implementation
91
92
    public function getInvoiceNumber(): string
93
    {
94
        return $this->number;
95
    }
96
97
    public function getIssueDate(): Carbon
98
    {
99
        return $this->date;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->date returns the type string which is incompatible with the type-hinted return Carbon\Carbon.
Loading history...
100
    }
101
102
    public function getInvoiceType(): string
103
    {
104
        return $this->type->value ?? (string) $this->type;
0 ignored issues
show
Bug introduced by
The property value does not exist on string.
Loading history...
105
    }
106
107
    public function getTotalAmount(): float
108
    {
109
        return (float) $this->total;
110
    }
111
112
    public function getTaxAmount(): float
113
    {
114
        return (float) $this->tax;
115
    }
116
117
    public function getCustomerName(): string
118
    {
119
        return $this->customer_name;
120
    }
121
122
    public function getCustomerTaxId(): ?string
123
    {
124
        return $this->customer_tax_id;
125
    }
126
127
    public function getBreakdowns(): Collection
128
    {
129
        return $this->breakdowns;
130
    }
131
132
    public function getRecipients(): Collection
133
    {
134
        return $this->recipients;
135
    }
136
137
    public function getPreviousHash(): ?string
138
    {
139
        return $this->previous_hash ?? null;
0 ignored issues
show
Bug introduced by
The property previous_hash does not exist on Squareetlabs\VeriFactu\Models\Invoice. Did you mean previous?
Loading history...
140
    }
141
142
    public function getOperationDescription(): string
143
    {
144
        return $this->description ?? 'Invoice issued';
145
    }
146
147
    public function getOperationDate(): ?Carbon
148
    {
149
        return $this->operation_date;
0 ignored issues
show
Bug introduced by
The property operation_date does not seem to exist on Squareetlabs\VeriFactu\Models\Invoice. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
150
    }
151
152
    public function getTaxPeriod(): ?string
153
    {
154
        return $this->tax_period;
0 ignored issues
show
Bug introduced by
The property tax_period does not seem to exist on Squareetlabs\VeriFactu\Models\Invoice. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
155
    }
156
157
    public function getCorrectionType(): ?string
158
    {
159
        return $this->correction_type;
0 ignored issues
show
Bug introduced by
The property correction_type does not seem to exist on Squareetlabs\VeriFactu\Models\Invoice. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
160
    }
161
162
    public function getExternalReference(): ?string
163
    {
164
        return $this->external_reference;
165
    }
166
}