<?php

namespace App\Modules\Stock;

use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\ValidationModel;
use App\Modules\Stock\Stock;
use App\Modules\Stock\StockPurchaseOrder;
use App\Modules\Stock\StockPurchaseOrderItemSerial;
use App\Traits\SetCreatorAndUpdatorTrait;

class StockPurchaseOrderItem extends ValidationModel
{
    use SoftDeletes;
    use SetCreatorAndUpdatorTrait;

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

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];

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

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'stock_purchase_order_id'         => 'required',
        'stock_id'                        => 'required',
        'unit_cost'                       => 'required',
        'quantity'                        => 'required',

    ];

    /**
     *	The attributes that won't be assigned during
     *	a mass assignment.
     */
    protected $fillable = ['stock_purchase_order_id','stock_id', 'unit_cost', 'quantity', 'received_quantity', 'refused_quantity', 'created_by', 'updated_by'];

    // ==================================================================
    //
    // Discontinued setter and getter
    //
    // ------------------------------------------------------------------

    /**
     * Retrieves the total amount paid for the product.
     *
     * @return float
     */
    public function getTotalPaidAttribute()
    {
        $order = StockPurchaseOrder::find($this->stock_purchase_order_id);

        return round(($this->unit_cost * (1 + $order->vat / 100)) * $this->quantity, 2);
    }

    // ==================================================================
    //
    // Relationships
    //
    // ------------------------------------------------------------------


    /**
     *	Inverse one to many relationship.
     *	Return the stock that made this order.
     */
    public function stock()
    {
        return $this->belongsTo(Stock::class)->withTrashed();
    }

    /**
     * The purchase order that this item belongs to.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function order()
    {
        return $this->belongsTo(StockPurchaseOrder::class, 'stock_purchase_order_id');
    }

    /**
     * The purchase order that this item belongs to.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function serials()
    {
        return $this->hasMany(StockPurchaseOrderItemSerial::class, 'order_item_id');
    }

    /**
     *	One to many relationship.
     *	Return all the items for a stock id.
     */
    // public function serials()
    // {
    // 	return $this->hasMany('App\Modules\Stock\StockSerial');
    // }

    /**
     *	Mark all the ordered as refused.
     */
    public function refuseAll()
    {
        $this->refused_quantity = $this->quantity;
        $this->save();
    }

    public function pendingItems()
    {
        return $this->quantity - $this->refused_quantity - $this->received_quantity;
    }
}
