Passed
Push — main ( a55882...7fcf59 )
by Jacobo
02:39
created

Invoice::getOperationDescription()   A

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
    ];
67
68
    protected $casts = [
69
        'date' => 'date',
70
        'type' => InvoiceType::class,
71
        'amount' => 'decimal:2',
72
        'tax' => 'decimal:2',
73
        'total' => 'decimal:2',
74
    ];
75
76
    public function breakdowns()
77
    {
78
        return $this->hasMany(Breakdown::class);
79
    }
80
81
    public function recipients()
82
    {
83
        return $this->hasMany(Recipient::class);
84
    }
85
86
    // VeriFactuInvoice Contract Implementation
87
88
    public function getInvoiceNumber(): string
89
    {
90
        return $this->number;
91
    }
92
93
    public function getIssueDate(): Carbon
94
    {
95
        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...
96
    }
97
98
    public function getInvoiceType(): string
99
    {
100
        return $this->type->value ?? (string) $this->type;
0 ignored issues
show
Bug introduced by
The property value does not exist on string.
Loading history...
101
    }
102
103
    public function getTotalAmount(): float
104
    {
105
        return (float) $this->total;
106
    }
107
108
    public function getTaxAmount(): float
109
    {
110
        return (float) $this->tax;
111
    }
112
113
    public function getCustomerName(): string
114
    {
115
        return $this->customer_name;
116
    }
117
118
    public function getCustomerTaxId(): ?string
119
    {
120
        return $this->customer_tax_id;
121
    }
122
123
    public function getBreakdowns(): Collection
124
    {
125
        return $this->breakdowns;
126
    }
127
128
    public function getRecipients(): Collection
129
    {
130
        return $this->recipients;
131
    }
132
133
    public function getPreviousHash(): ?string
134
    {
135
        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...
136
    }
137
138
    public function getOperationDescription(): string
139
    {
140
        return $this->description ?? 'Invoice issued';
141
    }
142
}