mivita/app/Models/Product.php
2026-01-23 17:35:23 +01:00

762 lines
26 KiB
PHP

<?php
namespace App\Models;
use App\Services\Util;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* App\Models\Product
*
* @property int $id
* @property string $name
* @property array|null $trans_name
* @property string $title
* @property array|null $trans_title
* @property string|null $copy
* @property array|null $trans_copy
* @property float|null $price
* @property float|null $price_ek
* @property float|null $tax
* @property float|null $price_old
* @property string|null $contents
* @property string|null $number
* @property array|null $icons
* @property string|null $description
* @property array|null $trans_description
* @property string|null $usage
* @property array|null $trans_usage
* @property string|null $ingredients
* @property array|null $trans_ingredients
* @property int|null $pos
* @property int $active
* @property int|null $amount
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductAttribute[] $attributes
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductCategory[] $categories
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductImage[] $images
* @property-write mixed $price_vk
*
* @method static bool|null forceDelete()
* @method static \Illuminate\Database\Query\Builder|\App\Models\Product onlyTrashed()
* @method static bool|null restore()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereAmount($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereContents($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereCopy($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereIcons($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereIngredients($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereNumber($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product wherePos($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product wherePrice($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product wherePriceEk($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product wherePriceOld($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereTax($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereTransCopy($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereTransDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereTransIngredients($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereTransName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereTransTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereTransUsage($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereUsage($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Product withTrashed()
* @method static \Illuminate\Database\Query\Builder|\App\Models\Product withoutTrashed()
*
* @property string|null $slug
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product findSimilarSlugs($attribute, $config, $slug)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereSlug($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product query()
*
* @property int|null $weight
* @property int|null $show_at
* @property array|null $action
* @property-read int|null $attributes_count
* @property-read int|null $categories_count
* @property-read int|null $images_count
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereAction($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereShowAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereWeight($value)
*
* @property int|null $points
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductImage[] $imagesActive
* @property-read int|null $images_active_count
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product wherePoints($value)
*
* @property string|null $identifier
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereIdentifier($value)
*
* @property int|null $upgrade_to_id
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereUpgradeToId($value)
*
* @property int|null $contents_total
* @property int|null $unit
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereContentsTotal($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereUnit($value)
*
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\CountryPrice[] $country_prices
* @property-read int|null $country_prices_count
* @property int|null $wp_number
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereWpNumber($value)
*
* @property bool|null $shipping_addon
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereShippingAddon($value)
*
* @property-read int|null $ingredients_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductIngredient[] $product_ingredients
* @property-read int|null $product_ingredients_count
* @property bool|null $no_commission
* @property array|null $show_on
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Ingredient[] $p_ingredients
* @property-read int|null $p_ingredients_count
*
* @method static \Illuminate\Database\Eloquent\Builder|Product whereNoCommission($value)
* @method static \Illuminate\Database\Eloquent\Builder|Product whereShowOn($value)
* @method static \Illuminate\Database\Eloquent\Builder|Product withUniqueSlugConstraints(\Illuminate\Database\Eloquent\Model $model, string $attribute, array $config, string $slug)
*
* @property string|null $ean
*
* @method static \Illuminate\Database\Eloquent\Builder|Product whereEan($value)
*
* @property bool|null $no_free_shipping
* @property bool|null $buying_restriction
* @property int|null $buying_restriction_amount
*
* @method static \Illuminate\Database\Eloquent\Builder|Product whereBuyingRestriction($value)
* @method static \Illuminate\Database\Eloquent\Builder|Product whereBuyingRestrictionAmount($value)
* @method static \Illuminate\Database\Eloquent\Builder|Product whereNoFreeShipping($value)
*
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ProductBuying> $product_buyings
* @property-read int|null $product_buyings_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ProductBuying> $product_buyings
* @property bool|null $sponsor_buying_points
* @property int|null $sponsor_buying_points_amount
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ProductBuying> $product_buyings
*
* @method static \Illuminate\Database\Eloquent\Builder|Product whereSponsorBuyingPoints($value)
* @method static \Illuminate\Database\Eloquent\Builder|Product whereSponsorBuyingPointsAmount($value)
*
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ProductBuying> $product_buyings
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\TransProduct> $translations
* @property-read int|null $translations_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ProductCategory> $product_categories
* @property-read int|null $product_categories_count
*
* @mixin \Eloquent
*/
class Product extends Model
{
/*identifiers
show_upgrade # in membership payment can upgrade this package to show order
show_order # in membership payment show always
upgrade # need upgrade_to_id set user->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',
'no_free_shipping' => 'bool',
'buying_restriction' => 'bool',
'buying_restriction_amount' => 'int',
'sponsor_buying_points' => 'bool',
'sponsor_buying_points_amount' => 'int',
];
use Sluggable;
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $fillable = [
'name',
'title',
'copy',
'price',
'price_ek',
'tax',
'price_old',
'points',
'weight',
'no_commission',
'no_free_shipping',
'contents',
'contents_total',
'unit',
'number',
'ean',
'wp_number',
'icons',
'description',
'usage',
'ingredients',
'pos',
'amount',
'active',
'show_at',
'show_on',
'shipping_addon',
'buying_restriction',
'buying_restriction_amount',
'sponsor_buying_points',
'sponsor_buying_points_amount',
'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 => 'ShopKunde',
2 => 'ShopBerater',
3 => 'ShopBeraterKunden',
4 => 'Auszeitparty',
7 => 'Registrierung Berater',
8 => 'Mitgliedschaft Berater',
9 => 'Onboarding Berater',
10 => 'zur internen Berechnung',
12 => 'Abo-BasisProdukt',
13 => 'Abo-AddonProdukt',
];
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',
];
/************* ✨ Codeium Command ⭐ *************/
/**
* Configure the model for auto-generating a slug.
*
* This method returns an array defining the attributes used
* to generate the slug for the model. In this case, the 'name'
* attribute is used as the source for generating the slug.
*
* @return array Configuration for slug generation.
*/
/****** e935bd41-f49b-4736-9603-2da86dc27f25 *******/
public function sluggable(): array
{
return [
'slug' => [
'source' => 'name',
],
];
}
public function product_buyings()
{
return $this->hasMany('App\Models\ProductBuying', 'product_id', 'id');
}
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 product_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 translations()
{
return $this->hasMany(TransProduct::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');
}
/**
* Bundle-Items: Enthaltene Produkte in diesem Set/Kit
*/
public function bundleItems()
{
return $this->belongsToMany(Product::class, 'product_bundles', 'product_id', 'bundle_product_id')
->withPivot('quantity', 'pos')
->orderBy('product_bundles.pos');
}
/**
* Bundle-Parents: Sets/Kits, die dieses Produkt enthalten
*/
public function bundleParents()
{
return $this->belongsToMany(Product::class, 'product_bundles', 'bundle_product_id', 'product_id')
->withPivot('quantity', 'pos');
}
/**
* Prüft ob dieses Produkt ein Set/Kit ist (enthält andere Produkte)
*/
public function isBundle(): bool
{
return $this->bundleItems()->count() > 0;
}
/**
* Holt Bundle-Items mit eager loading
*/
public function product_bundles()
{
return $this->hasMany(ProductBundle::class, 'product_id', 'id')->orderBy('pos');
}
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 setPointsAttribute($value)
{
$this->attributes['points'] = $value !== null ? Util::reFormatNumber($value) : null;
}
public function getFormattedPoints()
{
return isset($this->attributes['points']) ? Util::formatNumber($this->attributes['points']) : '';
}
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, $user = null)
{
if ($this->no_commission) {
return $price;
}
$user = $user ? $user : \Auth::user();
if ($user && $user->user_level) {
$margin = (($user->user_level->margin - 100) * -1) / 100;
$price = $price * $margin;
}
return $price;
}
private function calcPriceUserCommission($price, $user)
{
if ($this->no_commission) {
return $price;
}
$user = $user ? $user : \Auth::user();
if ($user && $user->user_level) {
$margin = $user->user_level->margin;
$price = $price / 100 * $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, $commission = false, $user = 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, $user) : $price;
$price = $commission ? $this->calcPriceUserCommission($price, $user) : $price;
return round($price, 2);
}
/* out */
public function getFormattedPriceWith(bool $net = true, bool $ufactor = true, $country = null, $commission = false)
{
return isset($this->attributes['price']) ? Util::formatNumber($this->getPriceWith($net, $ufactor, $country, $commission)) : '';
}
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);
return $trans != '' ? $trans : $this->{$key};
}
public function getTrans($key, $lang)
{
$trans = $this->translations->where('language', '=', $lang)->where('key', $key)->first();
return $trans ? $trans->value : '';
}
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, $commission = false)
{
$ret = '';
if ($country && isset($country->currency) && $country->currency) {
$price = $this->getPriceWith($net, $ufactor, $country, $commission);
$ret = formatNumber($price * $country->currency_faktor).' '.$country->currency_unit;
return '<br><span class="small">~'.$ret.'<span>';
}
return '';
}
}