'float', 'max_items' => 'int', 'active' => 'bool', 'type' => 'int', 'shop' => 'bool', ]; protected $dates = [ 'from', 'to' ]; protected $fillable = [ 'type', 'name', 'description', 'from', 'to', 'shop', 'max_bugdet', 'max_items', 'active' ]; public $promotionType = [ 1 => 'verschenken', 2 => 'Nachlass (not built in!)', 3 => 'Gewinnspiel (not built in!)', ]; public function products() { return $this->belongsToMany(Product::class, 'promotion_admin_products') ->withPivot('id', 'own_price', 'price', 'tax', 'price_old', 'calcu_commission', 'max_items', 'used_items', 'shipping', 'active') ->withTimestamps(); } public function promotion_admin_products() { return $this->hasMany(PromotionAdminProduct::class); } public function promotion_admin_products_active() { return $this->hasMany(PromotionAdminProduct::class)->where('active', 1); } public function promotion_user_products() { return $this->hasMany(PromotionUserProduct::class); } public function promotion_users() { return $this->hasMany(PromotionUser::class); } public function getPromotionType(){ if(isset($this->promotionType[$this->type])){ return $this->promotionType[$this->type]; } } public function getFromAttribute($value) { if(!$value){ return ""; } return Carbon::parse($value)->format(\Util::formatDateDB()); } public function getFromRaw(){ return isset($this->attributes['from']) ? $this->attributes['from'] : NULL; } public function setFromAttribute( $value ) { $this->attributes['from'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL; } public function getToAttribute($value) { if(!$value){ return ""; } return Carbon::parse($value)->format(\Util::formatDateDB()); } public function getToRaw(){ return isset($this->attributes['to']) ? $this->attributes['to'] : NULL; } public function setToAttribute( $value ) { $this->attributes['to'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL; } public function setMaxBugdetAttribute( $value ) { $this->attributes['max_bugdet'] = $value ? Util::reFormatNumber($value) : null; } public function getFormattedMaxBugdet() { return isset($this->attributes['max_bugdet']) ? Util::formatNumber($this->attributes['max_bugdet']) : ""; } public function canDelete(){ if($this->promotion_users->count() === 0 && $this->promotion_user_products->count() === 0){ return true; } return false; } public static function getActiveAdminPromotionsAsArray(){ $query = PromotionAdmin::where('active', true) ->where(function ($query) { $query->where('from', '<', Carbon::now()) ->orWhereNull('from'); }) ->where(function ($query) { $query->where('to', '>=', Carbon::now()) ->orWhereNull('to'); }); return $query->pluck('name', 'id')->toArray(); } }