Passed
Pull Request — master (#11)
by Fatih
04:48
created

InvoiceLine   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A bill() 0 3 1
A invoice() 0 3 1
A boot() 0 9 2
A related() 0 3 1
1
<?php
2
3
namespace NeptuneSoftware\Invoice\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Str;
7
use NeptuneSoftware\Invoice\Scopes\InvoiceScope;
8
9
class InvoiceLine extends Model
10
{
11
    protected $guarded = [];
12
13
    public $incrementing = false;
14
15
    protected $fillable = [
16
        'amount', 'tax', 'tax_details', 'invoice_id', 'description', 'invoiceable_id',
17
        'invoiceable_type', 'name', 'discount', 'quantity', 'is_free', 'is_complimentary'
18
    ];
19
20
    protected $casts = [
21
        'tax_details' => 'array'
22
    ];
23
24
    /**
25
     * InvoiceLine constructor.
26
     * @param array $attributes
27
     */
28
    public function __construct(array $attributes = [])
29
    {
30
        parent::__construct($attributes);
31
32
        $this->setTable(config('invoice.table_names.invoice_lines'));
33
    }
34
35
    protected static function boot()
36
    {
37
        parent::boot();
38
        static::creating(function ($model) {
39
            /**
40
             * @var \Illuminate\Database\Eloquent\Model $model
41
             */
42
            if (!$model->getKey()) {
43
                $model->{$model->getKeyName()} = Str::uuid()->toString();
44
            }
45
        });
46
    }
47
48
    public function invoice()
49
    {
50
        return $this->belongsTo(Invoice::class);
51
    }
52
53
    public function bill()
54
    {
55
        return $this->belongsTo(Bill::class, 'invoice_id')->withoutGlobalScope(InvoiceScope::class);
56
    }
57
58
    /**
59
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
60
     */
61
    public function related()
62
    {
63
        return $this->morphTo();
64
    }
65
}
66