<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\ShoppingOrder
 *
 * @property int $id
 * @property int $shopping_user_id
 * @property int|null $auth_user_id
 * @property int $country_id
 * @property int $user_shop_id
 * @property float|null $total
 * @property float|null $shipping
 * @property float|null $subtotal
 * @property float|null $tax_rate
 * @property float|null $tax
 * @property float|null $total_shipping
 * @property int|null $weight
 * @property int|null $paid
 * @property string|null $txaction
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\User|null $auth_user
 * @property-read \App\Models\Country $country
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShoppingOrderItem[] $shopping_order_items
 * @property-read int|null $shopping_order_items_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShoppingPayment[] $shopping_payments
 * @property-read int|null $shopping_payments_count
 * @property-read \App\Models\ShoppingUser $shopping_user
 * @property-read \App\Models\UserShop $user_shop
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereAuthUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereCountryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder wherePaid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShipping($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShoppingUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereSubtotal($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTax($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTaxRate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTotal($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTotalShipping($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTxaction($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereUserShopId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereWeight($value)
 * @mixin \Eloquent
 * @property int|null $payment_for
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder wherePaymentFor($value)
 * @property int|null $member_id
 * @property string|null $mode
 * @property-read \App\User|null $member
 * @property-read \App\Models\UserHistory|null $user_history
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereMemberId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereMode($value)
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property string|null $user_deleted_at
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingOrder onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereUserDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingOrder withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingOrder withoutTrashed()
 * @property-read \App\Models\ShippingCountry $shipping_country
 * @property float|null $shipping_net
 * @property float|null $subtotal_shipping
 * @property int|null $points
 * @property int|null $shipped
 * @property string|null $tracking
 * @property string|null $wp_invoice_path
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder wherePoints($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShipped($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShippingNet($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereSubtotalWs($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTracking($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereWpInvoicePath($value)
 * @property array|null $wp_notice
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereWpNotice($value)
 * @property string|null $subtotal_full
 * @property-read \App\Models\ShoppingOrderMargin|null $shopping_order_margin
 * @property-read \App\Models\ShoppingPayment|null $shopping_payment_last
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereSubtotalFull($value)
 * @property string|null $discount
 * @property string|null $payment_credit
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereDiscount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder wherePaymentCredit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereSubtotalShipping($value)
 */
class ShoppingOrder extends Model
{
    protected $table = 'shopping_orders';

    use SoftDeletes;
    protected $dates = ['deleted_at'];

    protected $fillable = [
        'shopping_user_id',
        'auth_user_id',
        'member_id',
        'country_id',
        'user_shop_id',
        'total',
        'subtotal_full',
        'discount',
        'subtotal',
        'shipping',
        'shipping_net',
        'subtotal_shipping',
        'tax',
        'total_without_credit',
        'payment_credit',
        'total_shipping',
        'points',
        'weight',
        'paid',
        'txaction',
        'wp_invoice_path',
        'wp_notice',
        'mode',
        'shipped',
        'tracking'
    ];

    protected $casts = [
        'wp_notice' => 'array',
    ];

    public static $shippedTypes = [
        0 => 'offen',
        1 => 'in Bearbeitung',
        2 => 'versendet',
        3 => 'abgeschlossen',
        10 => 'storniert'
    ];

    public static $apiShippedTypes = [
        0 => 'open', //(Fullfilment durch Händler)',
        1 => 'process', //(Fullfilment durch MIVITA: nicht Versand)
        2 => 'sent', //(Fullfilment durch MIVITA: Versand erfolgt)'
        3 => 'close', //(Fullfilment durch MIVITA: Versand erfolgt)',
        10 => 'cancel'
    ];

    public static $shippedColors = [
        0 => 'warning',
        1 => 'info',
        2 => 'success',
        3 => 'secondary',
        10 => 'danger',
    ];




    public function shopping_user()
    {
        return $this->belongsTo('App\Models\ShoppingUser','shopping_user_id');
    }

    public function country()
    {
        return $this->belongsTo('App\Models\ShippingCountry','country_id');
    }

    public function shipping_country()
    {
        return $this->belongsTo('App\Models\ShippingCountry','country_id');
    }

    //can null
    public function member()
    {
        return $this->belongsTo('App\User','member_id');
    }
    //can null
    public function auth_user()
    {
        return $this->belongsTo('App\User','auth_user_id');
    }

    public function user_history()
    {
        return $this->hasOne('App\Models\UserHistory','shopping_order_id')->latest();
    }

    public function shopping_order_margin()
    {
        return $this->hasOne('App\Models\ShoppingOrderMargin','shopping_order_id')->latest();
    }

    public function shopping_order_items(){
        return $this->hasMany('App\Models\ShoppingOrderItem', 'shopping_order_id');
    }

    public function shopping_payments(){
        return $this->hasMany('App\Models\ShoppingPayment', 'shopping_order_id');
    }

    public function shopping_payment_last(){
        return $this->hasOne('App\Models\ShoppingPayment', 'shopping_order_id')->latest();
    }

    public function setUserHistoryValue($values = []){
        if($user_history = $this->user_history){
            foreach ($values as $key=>$val){
                $user_history->{$key} = $val;
            }
            $user_history->save();
        }
    }

    public function getLastShoppingPayment($key=false){
        $shopping_payment = $this->shopping_payments->last();
        if($shopping_payment){
            if($key === 'getPaymentType'){
                return $shopping_payment->getPaymentType();
            }
            if($key === 'reference'){
                return $shopping_payment->reference;
            }
        }
        return "";
    }

    public function getShippedType(){
        return isset(self::$shippedTypes[$this->shipped]) ? self::$shippedTypes[$this->shipped] : "";
    }

    public function getAPIShippedType(){
        return isset(self::$apiShippedTypes[$this->shipped]) ? self::$apiShippedTypes[$this->shipped] : "free";
    }

    public function getShippedColor(){
        return isset(self::$shippedColors[$this->shipped]) ? self::$shippedColors[$this->shipped] : "default";
    }

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

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

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

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

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

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

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

    public function getFormattedTax()
    {
        return formatNumber($this->attributes['tax']);
    }
    public function getFormattedTotalWithoutCredit()
    {
        return formatNumber($this->attributes['total_without_credit']);
    }
    public function getFormattedPaymentCredit()
    {
        return formatNumber($this->attributes['payment_credit']);
    }


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




    public function getItemsCount(){
        $count = 0;
        if($this->shopping_order_items){
            foreach ($this->shopping_order_items as $shopping_order_item){
                $count += $shopping_order_item->qty;
            }
        }

        return $count;
    }



}
