<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

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


/**
 * Class UserCredit
 *
 * @property int $id
 * @property int $auth_user_id
 * @property float|null $net
 * @property float|null $tax_rate
 * @property float|null $tax
 * @property float|null $total
 * @property string|null $credit
 * @property string|null $user_margins
 * @property bool $paid_out
 * @property bool $cancellation
 * @property int $status
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property User $user
 * @package App\Models
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit query()
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereAuthUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereCancellation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereCredit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereNet($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit wherePaidOut($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereTax($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereTaxRate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereTotal($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereUserMargins($value)
 * @property string|null $credit_number
 * @property string|null $date
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereCreditNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereDate($value)
 * @property object|null $user_credits
 * @method static \Illuminate\Database\Eloquent\Builder|UserCredit whereUserCredits($value)
 * @mixin \Eloquent
 */
class UserCredit extends Model
{
	protected $table = 'user_credits';

	protected $casts = [
		'auth_user_id' => 'int',
		'net' => 'float',
		'tax_rate' => 'float',
		'tax' => 'float',
		'total' => 'float',
		'paid_out' => 'bool',
		'cancellation' => 'bool',
		'status' => 'int',
		'credit' => 'array',
		'user_margins' => 'object',
		'user_credits' => 'object'
	];

	protected $fillable = [
		'auth_user_id',
		'credit_number',
		'net',
		'tax_rate',
		'tax',
		'total',
		'date',
		'credit',
		'user_margins',
		'user_credits',
		'paid_out',
		'cancellation',
		'status'
	];

	public static $statusTypes = [
        0 => 'offen',
        1 => 'bezahlt',
        2 => 'prüfen',
        10 => 'storniert'
    ];

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

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

	public function getDateAttribute($value)
    {
        if(!$value){
            return "";
        }
        return Carbon::parse($value)->format(\Util::formatDateDB());
    }


    public function setDateAttribute( $value ) {
        $this->attributes['date'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL;
    }
	public function getDateRaw(){
        return  isset($this->attributes['date']) ? $this->attributes['date'] : NULL;
    }

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

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

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


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

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