<?php

namespace App\Modules\Stock;

use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\ValidationModel;
use App\Models\User;
use App\Models\Status;
use App\Modules\Stock\Supplier;
use App\Modules\Stock\PaymentMethods;
use App\Modules\Stock\StockPurchaseOrderItem;
use App\Traits\SetCreatorAndUpdatorTrait;

class AbstractStockPurchaseOrder extends ValidationModel
{
    use SoftDeletes;
    use SetCreatorAndUpdatorTrait;

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

    /**
     * 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_cost', 'total_paid', 'total_cost', 'items_cost'];

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'reference_number'   => 'required',
        'supplier_id'        => 'required',
        'vat'                => 'required',
    ];

    /**
     *	The attributes that won't be assigned during
     *	a mass assignment.
     */
    protected $fillable = ['supplier_id', 'reference_number', 'ordered_by', 'status_id', 'delivery_cost', 'discount_amount', 'vat', 'created_by', 'updated_by', 'payments_id','xeroId', 'xero_synced_at', 'xero_sync_error', 'xero_sync_status', 'xero_purchase_order_id'];

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

    /**
     * Retrieves the total cost  excluding VAT.
     *
     * @return float
     */
    public function getTotalCostAttribute()
    {
        return round($this->items_cost + $this->delivery_cost - $this->discount_amount, 4);
    }

    /**
     *
     */
    public function getItemsCostAttribute()
    {
        $items = $this->items;
        $itemsCost = 0;

        foreach ($items as $item) {
            $itemsCost += $item->unit_cost * ($item->quantity - $item->refused_quantity);
        }

        return $itemsCost;
    }

    /**
     * Retrieves the total paid (total cost + total vat).
     *
     * @return float
     */
    public function getTotalPaidAttribute()
    {
        return round($this->total_cost * (1 + $this->vat / 100), 4);
    }

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


    /**
     *	One to many relationship.
     *	Return all the items for a stock id.
     */
    public function items()
    {
        return $this->hasMany(StockPurchaseOrderItem::class);
    }

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

    /**
     *	Inverse one to many relationship.
     *	Return the order status.
     */
    public function status()
    {
        return $this->belongsTo(Status::class);
    }

    /**
     *	Inverse one to many relationship.
     *	Return the supplier of this order.
     */
    public function supplier()
    {
        return $this->belongsTo(Supplier::class);
    }

    public function payment_method()
    {
        return $this->belongsTo(PaymentMethods::class, 'payments_id');
    }
}
