Luciano
10/7/2019 - 7:37 PM

Invoice Model for Laravel 5.X

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

// Activity Log
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\Contracts\Activity;

class Formality extends Model
{
    use SoftDeletes, LogsActivity;

    //
    protected $table    = 'formalities';
    protected $fillable = [
        'number','title','date','expiration_date','status_id','category_id','customer_type_id','book_id','group_id','shortname','letter','sales_point','iva_value_id','total_iva','total',
        'total_pending','on_account','comment','discharge_date','discharge_text','customer_id','customer_doc_type_id','customer_iva_condition_id', 'customer_name', 'customer_contact',
        'customer_address', 'customer_phone','customer_email','customer_doc_number','concept_id','is_printed','service_date_from','service_date_to','payment_due_date','cae','cae_due_date',
        'cae_request_date'];
    protected $dates    = ['created_at','updated_at','deleted_at'];

    // Activity Log
    protected static $logFillable = true;
    protected static $submitEmptyLogs = false;
    protected static $logOnlyDirty = true;
    protected static $logName = 'formality';

    public function tapActivity(Activity $activity, string $eventName)
    {
        $activity->group_id = $this->group_id;
    }

    // *************************************************
    public function book(){
        return $this->belongsTo('App\Book','book_id','id');
    }

    public function doc_type(){
        return $this->belongsTo('App\DocType','doc_type_id','id');
    }

    public function concept(){
        return $this->belongsTo('App\Concept','formality_concept_id','id');
    }

    public function customer(){
        return $this->belongsTo('App\Customer','customer_id','id')->withTrashed();
    }

    public function details()
    {
        return $this->hasMany('App\FormalityDetail','formality_id');
    }

    public function imputed_on()
    {
        return $this->hasMany('App\FormalityDetail','linked_formality_id');
    }

    public function status(){
        return $this->belongsTo('App\Status','status_id','id');
    }

    public function payments()
    {
        return $this->hasMany('App\Payment','formality_id');
    }

    // Order relationships
    public function customer_type(){
        return $this->belongsTo('App\CustomerType','customer_type_id','id');
    }

    public function category(){
        return $this->belongsTo('App\OrderCategory','category_id','id');
    }

    public function iva_value(){
        return $this->belongsTo('App\IvaValue','iva_value_id','id');
    }

    public function works()
    {
        return $this->hasMany('App\Work');
    }
}