Passed
Pull Request — master (#11)
by Burak
04:13
created

InvoiceLine::bill()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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\BillScope;
8
use NeptuneSoftware\Invoice\Scopes\InvoiceScope;
9
10
class InvoiceLine extends Model
11
{
12
    protected $guarded = [];
13
14
    public $incrementing = false;
15
16
    protected $fillable = [
17
        'amount', 'tax', 'tax_details', 'invoice_id', 'description', 'invoiceable_id',
18
        'invoiceable_type', 'name', 'discount', 'quantity', 'is_free', 'is_complimentary'
19
    ];
20
21
    protected $casts = [
22
        'tax_details' => 'array'
23
    ];
24
25
    /**
26
     * InvoiceLine constructor.
27
     * @param array $attributes
28
     */
29
    public function __construct(array $attributes = [])
30
    {
31
        parent::__construct($attributes);
32
33
        $this->setTable(config('invoice.table_names.invoice_lines'));
34
    }
35
36
    protected static function boot()
37
    {
38
        parent::boot();
39
        static::creating(function ($model) {
40
            /**
41
             * @var \Illuminate\Database\Eloquent\Model $model
42
             */
43
            if (!$model->getKey()) {
44
                $model->{$model->getKeyName()} = Str::uuid()->toString();
45
            }
46
        });
47
    }
48
49
    public function invoice()
50
    {
51
        return $this->belongsTo(Invoice::class);
52
    }
53
54
    public function bill()
55
    {
56
        return $this->belongsTo(Bill::class, 'invoice_id')
57
            ->withoutGlobalScope(InvoiceScope::class)
58
            ->withGlobalScope('bill', new BillScope());
59
    }
60
61
    /**
62
     * Get the owning invoiceable model.
63
     */
64
    public function invoiceable()
65
    {
66
        return $this->morphTo();
67
    }
68
}
69