payment_order_id to the package in the payment api */ protected $table = 'products'; protected $casts = [ 'trans_name' => 'array', 'trans_title' => 'array', 'trans_copy' => 'array', 'icons' => 'array', 'trans_description' => 'array', 'trans_usage' => 'array', 'trans_ingredients' => 'array', 'show_on' => 'array', 'action' => 'array', 'wp_number' => 'int', 'shipping_addon' => 'bool', 'active' => 'bool', 'no_commission' => 'bool', ]; use Sluggable; use SoftDeletes; protected $dates = ['deleted_at']; protected $fillable = [ 'name', 'title', 'copy', 'price', 'price_ek', 'tax', 'price_old', 'points', 'weight', 'no_commission', 'contents', 'contents_total', 'unit', 'number', 'ean', 'wp_number', 'icons', 'description', 'usage', 'ingredients', 'pos', 'amount', 'active', 'show_at', 'show_on', 'shipping_addon', 'identifier', 'action', 'upgrade_to_id' ]; public $identifiers_types = [ '' => '-', 'show_upgrade' => 'Kann geupdatet werden', 'show_order' => 'Wird immer als Option angezeigt', 'upgrade_product' => 'Produkt upgrade zur Produkt ID', 'upgrade_member' => 'Berater upgrade zur Karriere ID', 'proportional_voucher' => 'Anteiliger Gutschein Berater', ]; public $unitTypes = [ 0 => '', 1 => 'ml', 2 => 'g', 3 => 'Liter', 4 => 'KG', ]; public $showATs = [ 0 => 'Nur Kunden Shop', 1 => 'Kunden + Berater Shop', 2 => 'Nur Berater Shop', 3 => 'Registrierung / Mitgliedschaft Berater', 4 => 'Nur Mitgliedschaft Berater', 5 => 'Onboarding Berater', 6 => 'Onboarding Berater + Berater Shop', 7 => 'zur internen Berechnung', ]; public $showONs = [ 1 => 'KundenShop', 2 => 'BeraterShop', 3 => 'Auszeitparty', 4 => 'Registrierung Berater', 5 => 'Mitgliedschaft Berater', 6 => 'Onboarding Berater', 10 => 'zur internen Berechnung', ]; public $actions = [ 0 => 'payment_for_account', 1 => 'payment_for_shop', 2 => 'payment_for_shop_upgrade', 4 => 'payment_for_lead_upgrade', ]; public $actionNames = [ 0 => 'Zahlung für Mitgliedschaft', 1 => 'Zahlung für Mitgliedschaft + Shop', 2 => 'Bei Zahlung Shop upgrade auf ID', 4 => 'Bei Zahlung Karriere upgrade auf ID', ]; public function sluggable() { return [ 'slug' => [ 'source' => 'name' ] ]; } public function attributes(){ return $this->hasMany('App\Models\ProductAttribute', 'product_id', 'id'); } public function categories(){ return $this->hasMany('App\Models\ProductCategory', 'product_id', 'id')->orderBy('pos', 'DESC'); } public function images(){ return $this->hasMany('App\Models\ProductImage', 'product_id', 'id')->orderBy('pos'); } public function imagesActive(){ return $this->hasMany('App\Models\ProductImage', 'product_id', 'id')->where('active', true)->orderBy('pos'); } public function getImageUrl(){ if(count($this->imagesActive)){ return route('product_image', [$this->imagesActive->first()->slug]); } return ""; } public function getProductUrl(){ return 'https://mivita.shop/produkte/alle-produkte/'.$this->slug; } public function country_prices() { return $this->hasMany(CountryPrice::class, 'product_id'); } public function p_ingredients() { return $this->belongsToMany(Ingredient::class, 'product_ingredients') ->withPivot('id') ->withTimestamps(); } public function product_ingredients() { return $this->hasMany(ProductIngredient::class, 'product_ingredients', 'id'); } public function getActionName($id = 0){ if(isset($this->actions[$id])){ return $this->actions[$id]; } return false; } public function getUpgradeToIdName($from){ if($from === 'payment_for_shop_upgrade'){ $value = Product::find($this->upgrade_to_id); } if($from === 'payment_for_lead_upgrade'){ $value = UserLevel::find($this->upgrade_to_id); } if($value){ return $value->name; } return "not found"; } public function _format_number($value){ return preg_replace("/[^0-9,]/", "", $value); } public function setPriceAttribute( $value ) { $this->attributes['price'] = $value !== null ? Util::reFormatNumber($value) : null; } public function setPriceEkAttribute( $value ) { $this->attributes['price_ek'] = $value !== null ? Util::reFormatNumber($value) : null; } public function setTaxAttribute( $value ) { $this->attributes['tax'] = $value !== null ? Util::reFormatNumber($value) : null; } public function setPriceOldAttribute( $value ) { $this->attributes['price_old'] = $value !== null ? Util::reFormatNumber($value) : null; } public function getFormattedPrice() { return isset($this->attributes['price']) ? Util::formatNumber($this->attributes['price']) : ""; } public function getFormattedPriceEk() { return isset($this->attributes['price_ek']) ? Util::formatNumber($this->attributes['price_ek']) : ""; } public function getFormattedPriceOld() { return isset($this->attributes['price_old']) ? Util::formatNumber($this->attributes['price_old']) : ""; } /*price by user Factor*/ private function calcPriceUserFactor($price){ if($this->no_commission){ return $price; } if(\Auth::user() && \Auth::user()->user_level){ $margin = ((\Auth::user()->user_level->margin -100)*-1) / 100; $price = $price * $margin; } return $price; } /*price net*/ private function calcPriceNet($price, $country=null){ $tax = $this->getTaxWith($country); $tax_rate = ($tax + 100) / 100; return $price / $tax_rate; } //price calu with public function getPriceWith(Bool $net = true, Bool $ufactor = true, $country = null){ $price = isset($this->attributes['price']) ? $this->attributes['price'] : null; $cprice = $country ? $this->getCPrice($country) : null; $price = $cprice ? $cprice : $price; $price = $net ? $this->calcPriceNet($price, $country) : $price; $price = $ufactor ? $this->calcPriceUserFactor($price) : $price; return round($price, 2); } /*out*/ public function getFormattedPriceWith(Bool $net = true, Bool $ufactor = true, $country = null) { return isset($this->attributes['price']) ? Util::formatNumber($this->getPriceWith($net, $ufactor, $country)) : ""; } public function getTaxWith($country = null){ $tax = isset($this->attributes['tax']) ? $this->attributes['tax'] : null; $ctax = $country ? $this->getCTax($country) : null; return $ctax !== null ? $ctax : $tax; } public function getFormattedPriceOldWith(Bool $net = true, Bool $ufactor = true, $country = null) { $price = isset($this->attributes['price_old']) ? $this->attributes['price_old'] : null; $cprice = $country ? $this->getCPriceOld($country) : null; $price = $cprice ? $cprice : $price; $price = $net ? $this->calcPriceNet($price, $country) : $price; $price = $ufactor ? $this->calcPriceUserFactor($price) : $price; $price = round($price, 2); return isset($price) ? Util::formatNumber($price) : ""; } public function getFormattedTax($country = null) { return isset($this->attributes['tax']) ? Util::formatNumber($this->getTaxWith($country), 0) : ""; } public function getBasePriceFormattedFullWith(Bool $net = true, Bool $ufactor = true, $country = null){ $price = $this->getPriceWith($net, $ufactor, $country); if($price = $this->getBasePriceWith($price)){ $unit = $this->attributes['unit']; //ml g if($unit === 1 || $unit === 2){ return Util::formatNumber($price) . ' € / 100 '.$this->getUnitType(); } //l kg if($unit === 3 || $unit === 4){ return Util::formatNumber($price) . ' € / 1 '.$this->getUnitType(); } } return ""; } public function getBasePriceWith($price){ if(isset($this->attributes['unit']) && isset($this->attributes['contents_total']) && $this->attributes['contents_total'] != 0){ $unit = $this->attributes['unit']; //ml g if($unit === 1 || $unit === 2){ return $price * 100 / $this->attributes['contents_total']; } //l kg if($unit === 3 || $unit === 4){ return $price * 1000 / $this->attributes['contents_total']; } } return ""; } public function getBasePriceFormattedFull(){ if($price = $this->getBasePrice()){ $unit = $this->attributes['unit']; //ml g if($unit === 1 || $unit === 2){ return Util::formatNumber($price) . ' € / 100 '.$this->getUnitType(); } //l kg if($unit === 3 || $unit === 4){ return Util::formatNumber($price) . ' € / 1 '.$this->getUnitType(); } } return ""; } public function getBasePriceFormatted(){ if($price = $this->getBasePrice()){ return Util::formatNumber($price); } return ""; } public function getBasePrice(){ if(isset($this->attributes['unit']) && isset($this->attributes['contents_total']) && $this->attributes['contents_total'] != 0){ $unit = $this->attributes['unit']; //ml g if($unit === 1 || $unit === 2){ return $this->attributes['price'] * 100 / $this->attributes['contents_total']; } //l kg if($unit === 3 || $unit === 4){ return $this->attributes['price'] * 1000 / $this->attributes['contents_total']; } } return ""; } public function getUnitType(){ return isset($this->unitTypes[$this->unit]) ? $this->unitTypes[$this->unit] : '-'; } public function getShowAtType(){ return isset($this->showATs[$this->show_at]) ? $this->showATs[$this->show_at] : '-'; } public function getShowOnTypes(){ $ret = []; if($this->show_on && is_array($this->show_on)){ foreach($this->show_on as $show){ $ret[] = isset($this->showONs[$show]) ? $this->showONs[$show] : '-'; } } return $ret; } public function setPosAttribute($value){ $this->attributes['pos'] = is_numeric($value) ? $value : null; } public function getLang($key) { $lang = \App::getLocale(); if ($lang == 'de') { return $this->{$key}; } $trans = $this->getTrans($key, $lang); if (!$trans || $trans == '') { return $this->{$key}; } return $trans; } public function getTrans($key, $lang) { $key = 'trans_' . $key; if (!empty($this->{$key}[$lang])) { return $this->{$key}[$lang]; } } public function getTranNames() { $ret = ""; foreach ((array) $this->trans_name as $value){ $ret .= $value.', '; } return rtrim($ret, ', '); } public function getCountryPrice(Country $country){ if($country->own_eur){ return $this->country_prices->where('country_id', '=', $country->id)->first() ?: new CountryPrice(); } return new CountryPrice(); } public function getCPrice(Country $country){ return $this->getCountryPrice($country)->c_price; } public function getCTax(Country $country){ return $this->getCountryPrice($country)->c_tax; } public function getCPriceOld(Country $country){ return $this->getCountryPrice($country)->c_price_old; } public function getCCurrency(Country $country){ return $this->getCountryPrice($country)->c_currency; } public function getRealPrice(Country $country){ if($country->own_eur && $this->getCPrice($country)){ return $this->getCPrice($country); } return $this->price; } public function getFormattedPriceCurrencyWith(Bool $net = true, Bool $ufactor = true, Country $country = null){ $ret = ""; if($country && isset($country->currency) && $country->currency){ $price = $this->getPriceWith($net, $ufactor, $country); $ret = formatNumber($price * $country->currency_faktor)." ".$country->currency_unit; return '
~'.$ret.''; } return "" ; } }