'int', 'product_id' => 'int', 'own_price' => 'bool', 'price' => 'float', 'tax' => 'float', 'price_old' => 'float', 'calcu_commission' => 'bool', 'max_items' => 'int', 'used_items' => 'int', 'shipping' => 'bool', 'active' => 'bool' ]; protected $fillable = [ 'promotion_admin_id', 'product_id', 'own_price', 'price', 'tax', 'price_old', 'calcu_commission', 'max_items', 'used_items', 'shipping', 'active' ]; public function product() { return $this->belongsTo(Product::class); } public function promotion_admin() { return $this->belongsTo(PromotionAdmin::class); } public function promotion_user_products() { return $this->hasMany(PromotionUserProduct::class); } public function setPriceAttribute( $value ) { $this->attributes['price'] = $value ? Util::reFormatNumber($value) : null; } public function getFormattedPrice() { return isset($this->attributes['price']) ? Util::formatNumber($this->attributes['price']) : ""; } public function getRealPrice() { if($this->own_price && $this->price){ return $this->price; } if($this->product && $this->product->price){ return $this->product->price; } return 0; } public function getFormattedRealPrice() { return Util::formatNumber($this->getRealPrice()); } /*price calcu Marign single_commission Factor*/ private function calcPriceCommissionFactor($price){ //einzelrabatt aus produkt $margin = 100; if(isset($this->product) && $this->product->single_commission){ $margin = $this->product->value_commission; }else{ //rabatt aus user level $user = Auth::user(); if($user && $user->user_level && $user->user_level->user_level_margins){ if($user_level_margin = $user->user_level->user_level_margins_re->first()){ $margin = $user_level_margin->trading_margin; } } } $margin = (100 - $margin) / 100; $price = $price * $margin; return $price; } /*price net*/ private function calcPriceNet($price){ $tax = isset($this->product->tax) ? $this->product->tax : 0; $tax_rate = ($tax + 100) / 100; return $price / $tax_rate; } //price calu with public function getPriceWith(Bool $net = true){ $price = $this->getRealPrice(); $price = $net ? $this->calcPriceNet($price) : $price; $price = $this->calcu_commission ? $this->calcPriceCommissionFactor($price) : $price; return round($price, 2); } /*out*/ public function getFormattedPriceWith(Bool $net = true) { return Util::formatNumber($this->getPriceWith($net)); } public function canDelete(){ if($this->promotion_user_products->count() === 0){ return true; } return false; } public function getPromotionUserProducts($user_promotion, $key = false) { $user_promotion_product = $user_promotion->hasPromotionUserProducts($this->id); if($user_promotion_product){ if(!$key){ return $user_promotion_product; } if(isset($user_promotion_product->{$key})){ return $user_promotion_product->{$key}; }else{ return 0; } } return 0; //return $this->hasMany(PromotionUserProduct::class)->where('promotion_admin_product_id', $promotion_admin_product->id)->first(); } }