<?php

namespace App\Modules\Stock;

use App\Models\ValidationModel;
use App\Models\User;
use App\Modules\Stock\Stock;

class StockReport extends ValidationModel
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'stock_reports';

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [];

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [];

    /**
     * The model's attributes.
     *
     * @var array
     */
    protected $attributes = [
        'aknowledged'    => 0,
    ];

    /**
     * The relations to eager load on every query.
     *
     * @var array
     */
    protected $with = ['user'];

    /**
     *	The attributes that won't be assigned during
     *	a mass assignment.
     */
    protected $fillable = ['stock_id', 'old_quantity', 'new_quantity', 'created_at', 'user_id', 'aknowledged'];

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    public static function boot()
    {
        parent::boot();

        // When a transaction is created we want to
        // update the user_id field
        static::creating(function ($model) {
            $model->user_id = getAuthUserId();
        });
    }

    /**
     * Returns the stock associated with this report.
     *
     * @return App\Modules\Stock\Stock
     */
    public function stock()
    {
        return $this->belongsTo(Stock::class);
    }

    /**
     * Returns the user who created this report.
     *
     * @return App\User
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
