<?php

namespace App\Modules\Stock;

use Illuminate\Support\Facades\Config;
use App\Models\ValidationModel;
use App\Modules\Stock\Stock;
use App\Modules\Stock\Supplier;
use App\Modules\Bookings\Order;

class StockItem extends ValidationModel
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['stock_id', 'margin', 'business_margin', 'cost', 'retail', 'quantity', 'supplier_id', 'order_id'];

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

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['vat', 'total_cost', 'retail_excluding', 'profit', 'staff_price'];

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

    /**
     * Scope results to service items only.
     */
    public function scopeService($query)
    {
        return $query->where('type', 'service');
    }

    /**
     * Filter results to stock type only.
     */
    public function scopeStockOnly($query)
    {
        return $query->where('type', 'stock');
    }

    /**
     * Retrieves the vat amount for the product.
     *
     * @return float
     */
    public function getVatAttribute()
    {
        return number_format($this->attributes['cost'] * Config::get('settings.vat') / 100, 2, '.', '');
    }

    /**
     * Calculates the total cost of the product including vat,
     * that we are buying it for.
     *
     * @return float
     */
    public function getTotalCostAttribute()
    {
        return number_format($this->attributes['cost'] + $this->vat, 2, '.', '');
    }

    /**
     * Calculates the retail price excluding vat.
     *
     * @return float
     */
    public function getRetailExcludingAttribute()
    {
        return number_format($this->attributes['retail'] / $this->getDivisor(), 2, '.', '');
    }

    /**
     * Calculates the profit.
     *
     * @return float
     */
    public function getProfitAttribute()
    {
        return number_format($this->retail_excluding - $this->attributes['cost'], 2, '.', '');
    }

    /**
     * Calculates the price for a member of staff.
     *
     * @return float
     */
    public function getStaffPriceAttribute()
    {
        $divisor = Config::get('settings.staff_percentage') / 100 + 1;

        return number_format($this->total_cost * $divisor, 2, '.', '');
    }

    /**
     * Helper function that retrieves the divisor, knowing the VAT value.
     *
     * @return float
     */
    public static function getDivisor()
    {
        return Config::get('settings.vat') / 100 + 1;
    }

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

    /**
     * Returns the order associated with this part.
     *
     * @return App\Modules\Bookings\Order
     */
    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id');
    }
}
