<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

/**
 * Class PromotionUserOrder
 *
 * @property int $id
 * @property int $promotion_admin_id
 * @property int $promotion_user_id
 * @property int $promotion_user_product_id
 * @property int $product_id
 * @property int $shopping_order_item_id
 * @property int $shopping_order_id
 * @property int $shopping_user_id
 * @property int|null $qty
 * @property float|null $price
 * @property float|null $price_net
 * @property float|null $tax_rate
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property Product $product
 * @property PromotionAdmin $promotion_admin
 * @property PromotionUser $promotion_user
 * @property PromotionUserProduct $promotion_user_product
 * @property ShoppingOrder $shopping_order
 * @property ShoppingOrderItem $shopping_order_item
 * @property ShoppingUser $shopping_user
 * @package App\Models
 * @property int|null $status
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder query()
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder wherePriceNet($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder whereProductId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder wherePromotionAdminId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder wherePromotionUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder wherePromotionUserProductId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder whereQty($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder whereShoppingOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder whereShoppingOrderItemId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder whereShoppingUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder whereTaxRate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder whereUpdatedAt($value)
 * @property bool|null $pick_up
 * @method static \Illuminate\Database\Eloquent\Builder|PromotionUserOrder wherePickUp($value)
 * @mixin \Eloquent
 */
class PromotionUserOrder extends Model
{
	protected $table = 'promotion_user_orders';

	protected $casts = [
		'promotion_admin_id' => 'int',
		'promotion_user_id' => 'int',
		'promotion_user_product_id' => 'int',
		'product_id' => 'int',
		'shopping_order_item_id' => 'int',
		'shopping_order_id' => 'int',
		'shopping_user_id' => 'int',
		'qty' => 'int',
		'price' => 'float',
		'price_net' => 'float',
		'tax_rate' => 'float',
		'status' => 'int',
		'pick_up' => 'bool',
	];

	protected $fillable = [
		'promotion_admin_id',
		'promotion_user_id',
		'promotion_user_product_id',
		'product_id',
		'shopping_order_item_id',
		'shopping_order_id',
		'shopping_user_id',
		'qty',
		'price',
		'price_net',
		'tax_rate',
		'status',
		'pick_up'
	];

	public static $statusTypes = [
        0 => 'bestellt',
        10 => 'storniert'
    ];

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

	public function promotion_admin()
	{
		return $this->belongsTo(PromotionAdmin::class);
	}

	public function promotion_user()
	{
		return $this->belongsTo(PromotionUser::class);
	}

	public function promotion_user_product()
	{
		return $this->belongsTo(PromotionUserProduct::class);
	}

	public function shopping_order()
	{
		return $this->belongsTo(ShoppingOrder::class);
	}

	public function shopping_order_item()
	{
		return $this->belongsTo(ShoppingOrderItem::class);
	}

	public function shopping_user()
	{
		return $this->belongsTo(ShoppingUser::class);
	}

	public function getStatusType(){
        return isset(self::$statusTypes[$this->status]) ? self::$statusTypes[$this->status] : "";
    }

	public function setStatusTxAction($txaction){

		switch ($txaction) {
			case 'none':
				$this->status = 10;
			break;
			case 'open':
				$this->status = 0;
			break;
			case 'paid':
				$this->status = 3;
			break;
		}
		$this->save();
	}
	
	public function setStatusShipped($shipped){
		$this->status = $shipped === 'cancel' ? 10 : 0;
		$this->save();
	}
}
