<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

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

/**
 * Class ShoppingOrderMargin
 *
 * @property int $id
 * @property int $shopping_order_id
 * @property int $user_id
 * @property float|null $net_price
 * @property float|null $net_discount
 * @property float|null $net_amount
 * @property int|null $m_sponsor_id
 * @property float|null $net_partner_commission
 * @property Carbon|null $from
 * @property bool $paid
 * @property bool $cancellation
 * @property int $status
 * @property string|null $content
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property User $user
 * @property ShoppingOrder $shopping_order
 * @package App\Models
 * @property float|null $from_payment_credit
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin query()
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereCancellation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereFrom($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereFromPaymentCredit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereMSponsorId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereNetAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereNetDiscount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereNetPartnerCommission($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereNetPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin wherePaid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereShoppingOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereUserId($value)
 * @property \Illuminate\Support\Carbon|null $partner_commission_pending_to
 * @property bool|null $partner_commission_paid
 * @property-read User|null $m_sponsor
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin wherePartnerCommissionPaid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin wherePartnerCommissionPendingTo($value)
 * @property int|null $user_credit_id
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrderMargin whereUserCreditId($value)
 * @property bool $order_paid
 * @property bool $out_paid
 * @property \Illuminate\Support\Carbon|null $margin_pending_to
 * @property bool|null $margin_paid
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ShoppingOrderMargin whereMarginPaid($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ShoppingOrderMargin whereMarginPendingTo($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ShoppingOrderMargin whereOrderPaid($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ShoppingOrderMargin whereOutPaid($value)
 * @mixin \Eloquent
 */
class ShoppingOrderMargin extends Model
{
	protected $table = 'shopping_order_margins';

	protected $casts = [
		'shopping_order_id' => 'int',
		'user_id' => 'int',
		'net_price' => 'float',
		'net_discount' => 'float',
		'net_amount' => 'float',
        'from_payment_credit' => 'float',
		'm_sponsor_id' => 'int',
		'net_partner_commission' => 'float',
		'order_paid' => 'bool', //ist die Bestellung bezahlt?
		'out_paid' => 'bool', //ist die  Provision Marge ausgezahlt?
		'cancellation' => 'bool', //ist die Stornierung beantragt?
		'status' => 'int', //status der Bestellung
		'margin_pending_to' => 'datetime', 
		'margin_paid' => 'bool', 
		'partner_commission_pending_to' => 'datetime', 
		'partner_commission_paid' => 'bool',
		'user_credit_id' => 'int',
	];

	protected $dates = [
		'from'
	];

	protected $fillable = [
		'shopping_order_id',
		'user_id',
		'net_price',
		'net_discount',
		'net_amount',
        'from_payment_credit',
		'm_sponsor_id',
		'net_partner_commission',
		'from',
		'order_paid',
		'out_paid',
		'cancellation',
		'status',
		'margin_pending_to',
		'margin_paid',
		'partner_commission_pending_to',
		'partner_commission_paid',
		'user_credit_id',
		'content'
	];


	public static $statusTypes = [
        0 => 'user order',
		1 => 'Registrierung',
        2 => 'Mitgliedschaft',
        3 => 'Guthabenaufladung',
        4 => 'VP.Bestellung Abholung',
        5 => 'VP.Bestellung Lieferung',
        6 => 'VP.Kundenbestellung',
        7 => 'from promotion',
        8 => 'from shop',
        9 => 'storniert'
    ];

	private $storedContent;

	public function user()
	{
		return $this->belongsTo(User::class, 'user_id');
	}

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

    public function m_sponsor_id()
    {
        return $this->belongsTo(User::class, 'm_sponsor_id');
    }


	public function m_sponsor()
    {
        return $this->belongsTo(User::class, 'm_sponsor_id');
    }

	public function hasPartnerCommission(){
		if($this->m_sponsor_id  || $this->net_partner_commission > 0){
			return true;
		}
		return false;
	}

	public function getFormattedNetDiscount()
    {
        return formatNumber($this->attributes['net_discount']);
    }

	public function getFormattedNetPartnerCommission()
    {
        return formatNumber($this->attributes['net_partner_commission']);
    }

	public function getRestoreContent($key = false)
    {	
		if(!$this->storedContent){
			$this->storedContent = unserialize($this->attributes['content']);
		}
        if (is_array($this->storedContent)) {
			switch ($key) {
				case 'items':
					return $this->storedContent['items'];
					break;
				case 'commission':
						return $this->storedContent['commission'];
						break;
					
			}
			return $this->storedContent;
        }
    }
}
