Invoice::getPreviousHash()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
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
        // New fields from PR #8
70
        'csv',
71
        'previous_invoice_number',
72
        'previous_invoice_date',
73
        'previous_invoice_hash',
74
        'is_first_invoice',
75
        'rectificative_type',
76
        'rectified_invoices',
77
        'rectification_amount',
78
        'is_subsanacion',
79
        'rejected_invoice_number',
80
        'rejection_date',
81
        'numero_instalacion',
82
        'aeat_estado_registro',
83
        'aeat_codigo_error',
84
        'aeat_descripcion_error',
85
        'has_aeat_warnings',
86
    ];
87
88
    protected $casts = [
89
        'date' => 'date',
90
        'operation_date' => 'date',
91
        'type' => InvoiceType::class,
92
        'amount' => 'decimal:2',
93
        'tax' => 'decimal:2',
94
        'total' => 'decimal:2',
95
        // New casts from PR #8
96
        'previous_invoice_date' => 'date',
97
        'rejection_date' => 'date',
98
        'is_first_invoice' => 'boolean',
99
        'is_subsanacion' => 'boolean',
100
        'has_aeat_warnings' => 'boolean',
101
        'rectified_invoices' => 'array',
102
        'rectification_amount' => 'array',
103
    ];
104
105
    public function breakdowns()
106
    {
107
        return $this->hasMany(Breakdown::class);
108
    }
109
110
    public function recipients()
111
    {
112
        return $this->hasMany(Recipient::class);
113
    }
114
115
    // VeriFactuInvoice Contract Implementation
116
117
    public function getInvoiceNumber(): string
118
    {
119
        return $this->number;
120
    }
121
122
    public function getIssueDate(): Carbon
123
    {
124
        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...
125
    }
126
127
    public function getInvoiceType(): string
128
    {
129
        return $this->type->value ?? (string) $this->type;
0 ignored issues
show
Bug introduced by
The property value does not exist on string.
Loading history...
130
    }
131
132
    public function getTotalAmount(): float
133
    {
134
        return (float) $this->total;
135
    }
136
137
    public function getTaxAmount(): float
138
    {
139
        return (float) $this->tax;
140
    }
141
142
    public function getCustomerName(): string
143
    {
144
        return $this->customer_name;
145
    }
146
147
    public function getCustomerTaxId(): ?string
148
    {
149
        return $this->customer_tax_id;
150
    }
151
152
    public function getBreakdowns(): Collection
153
    {
154
        return $this->breakdowns;
155
    }
156
157
    public function getRecipients(): Collection
158
    {
159
        return $this->recipients;
160
    }
161
162
    public function getPreviousHash(): ?string
163
    {
164
        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...
165
    }
166
167
    public function getOperationDescription(): string
168
    {
169
        return $this->description ?? 'Invoice issued';
170
    }
171
172
    public function getOperationDate(): ?Carbon
173
    {
174
        return $this->operation_date;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->operation_date returns the type string which is incompatible with the type-hinted return Carbon\Carbon|null.
Loading history...
175
    }
176
177
    public function getTaxPeriod(): ?string
178
    {
179
        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...
180
    }
181
182
    public function getCorrectionType(): ?string
183
    {
184
        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...
185
    }
186
187
    public function getExternalReference(): ?string
188
    {
189
        return $this->external_reference;
190
    }
191
}