<?php

namespace App\Modules\Invoices;

use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\ValidationModel;
use App\Modules\Invoices\Invoice;
use App\Modules\Stock\Stock;
use App\Modules\Stock\StockRma;
use App\Traits\SetCreatorAndUpdatorTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class InvoiceItem extends \Eloquent
{
    use \Venturecraft\Revisionable\RevisionableTrait;
    use SoftDeletes;
    use SetCreatorAndUpdatorTrait;
    use HasFactory;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'invoice_items';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['invoice_id', 'name', 'type', 'stock_id', 'quantity', 'price_sold_exc', 'discount', 'refunded', 'ignore_stock'];

    public function invoice()
    {
        return $this->belongsTo(Invoice::class);
    }

    public function stock()
    {
        return $this->belongsTo(Stock::class);
    }

    public function rmas()
    {
        return $this->morphMany(StockRma::class, 'from');
    }

    public function getPriceSoldExcAttribute()
    {
        return round($this->attributes['price_sold_exc'], 2);
    }
}
