<?php

namespace Modules\Order\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Modules\Attribute\Models\Attribute;
use Modules\Option\Models\Option;
use Modules\Product\Models\Product;
use Modules\Refund\Models\Refund;
use Modules\Store\Models\Store;

class OrderProduct extends Model
{
    public $timestamps = false;
    protected $fillable =
        [
            'count','total','product_cost','order_id','product_id','option_id','store_id'
        ];

    public function order()
    {
        return $this->belongsTo(Order::class)->withTrashed();
    }

    public function product()
    {
        return $this->belongsTo(Product::class)->withTrashed();
    }

    public function option()
    {
        return $this->belongsTo(Option::class,'option_id')->withTrashed();
    }
    public function store()
    {
        return $this->belongsTo(Store::class,'store_id')->withTrashed();
    }
    public function refunded()
    {
        $status = true;
        $refund = Refund::where('order_product_id',$this->id)->first();
        if($refund)
        {
            $status = false;
        }
        else
        {
            $date1 = Carbon::parse(date('Y-m-d'));
            $date2 = Carbon::parse($this->order->created_at);

            $days = $date1->diffInDays($date2);
            if($days > 7)
            {
                $status = false;
            }
        }
        return $status;
    }
}
