<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProductQuantity extends Model
{
    protected $fillable = [
    	'product_id', 'quantity', 'reason', 'created_at', 'updated_at', 'is_sales'
    ];

    public function product()
    {
    	return $this->belongsTo('App\Product');
    }

    protected $appends = ['margin', 'income', 'price'];

    public function getIncomeAttribute()
    {
        if (count($this->product)) {
        	return $this->product->product_price * $this->attributes['quantity'] * -1;
        }
    }

    public function getPriceAttribute()
    {
        if (count($this->product)) {
        	return $this->product->product_price;
        }
    }

    public function getMarginAttribute()
    {
        if (count($this->product)) {
            $margin = ($this->product->product_price - $this->product->product_cost) / $this->product->product_price;
            return round($margin * 100). '%';
        }
    }
}
