<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Invoice extends Model
{
    protected $fillable = ['invoice_url','amount','booking_id', 'buyer_company', 'address', 'contact_no', 'po_number', 'adjustment'];

    public function booking(){
        return $this->belongsTo(Booking::class);
    }

    public function invoiceDetails() {
        return $this->hasMany(InvoiceDetails::class);
    }

    public function invoiceStatuses() {
        return $this->hasMany(InvoiceStatuses::class);
    }

    public static function boot() {
        parent::boot();
        self::deleting(function($invoice) {
            $invoice->invoiceDetails()->each(function($invoiceDetails) {
                $invoiceDetails->delete();
            });
            $invoice->invoiceStatuses()->each(function($invoiceStatuses) {
                $invoiceStatuses->delete();
            });
        });
    }
}
