mivita/dev/app-bak/Models/Homeparty.php
2025-10-20 17:42:08 +02:00

234 lines
7.7 KiB
PHP

<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use App\Models\Country;
use Illuminate\Database\Eloquent\Model;
use PHPUnit\Framework\Constraint\Count;
use Illuminate\Database\Eloquent\Collection;
/**
* Class Homeparty
*
* @property int $id
* @property Carbon $date
* @property string $name
* @property string $place
* @property string $description
* @property int $pos
* @property int $completed
* @property int $status
* @property bool $order_to
* @property bool $active
* @property bool $default
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Collection|User[] $users
* @package App\Models
* @property int|null $auth_user_id
* @property-read \App\User|null $auth_user
* @property-read \App\Models\HomepartyUser|null $homeparty_host
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\HomepartyUser[] $homeparty_users
* @property-read int|null $homeparty_users_count
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereAuthUserId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereCompleted($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereDefault($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereOrderTo($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty wherePlace($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty wherePos($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereStatus($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereUpdatedAt($value)
* @property string|null $token
* @property bool|null $token_active
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\HomepartyUser[] $homeparty_guests
* @property-read int|null $homeparty_guests_count
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereToken($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereTokenActive($value)
* @property int|null $country_id
* @property int|null $step
* @property array|null $settings
* @property array|null $order
* @property array|null $card_info
* @property-read Country|null $country
* @property-read Collection|\App\Models\HomepartyUserOrderItem[] $homeparty_order_items
* @property-read int|null $homeparty_order_items_count
* @method static \Illuminate\Database\Eloquent\Builder|Homeparty whereCardInfo($value)
* @method static \Illuminate\Database\Eloquent\Builder|Homeparty whereCountryId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Homeparty whereOrder($value)
* @method static \Illuminate\Database\Eloquent\Builder|Homeparty whereSettings($value)
* @method static \Illuminate\Database\Eloquent\Builder|Homeparty whereStep($value)
* @property array|null $trans_description
* @method static \Illuminate\Database\Eloquent\Builder|Homeparty whereTransDescription($value)
* @mixin \Eloquent
*/
class Homeparty extends Model
{
protected $table = 'homeparties';
protected $casts = [
'pos' => 'int',
'completed' => 'int',
'status' => 'int',
'step' => 'int',
'country_id' => 'int',
'order_to' => 'bool',
'active' => 'bool',
'default' => 'bool',
'token_active' => 'bool',
'settings' => 'array',
'order' => 'array',
'card_info' => 'array',
'trans_description' => 'array',
];
protected $dates = [
'date'
];
protected $hidden = [
'token'
];
protected $fillable = [
'auth_user_id',
'date',
'name',
'place',
'country_id',
'pos',
'completed',
'status',
'step',
'order_to',
'active',
'default',
'token',
'token_active',
'settings',
'order',
'card_info'
];
private $user_country;
public function auth_user()
{
return $this->belongsTo('App\User', 'auth_user_id');
}
public function country()
{
return $this->belongsTo('App\Models\Country', 'country_id');
}
public function homeparty_users()
{
return $this->hasMany('App\Models\HomepartyUser', 'homeparty_id');
}
public function homeparty_host()
{
return $this->hasOne('App\Models\HomepartyUser', 'homeparty_id')->where('is_host', true);
}
public function homeparty_guests()
{
return $this->hasMany('App\Models\HomepartyUser', 'homeparty_id')->where('is_host', false);
}
public function homeparty_order_items(){
return $this->hasMany('App\Models\HomepartyUserOrderItem','homeparty_id');
}
public function getLang($key, $default = true)
{
$lang = \App::getLocale();
if ($lang == 'de') {
return $this->{$key};
}
return $this->getTrans($key, $lang, $default);
}
public function getTrans($key, $lang, $default = true)
{
if ($lang == 'de') {
return $this->{$key};
}
if($key === 'description' && !empty($this->trans_description[$lang])){
return $this->trans_description[$lang];
}
if($default){
return !empty($this->{$key}) ? $this->{$key} : '';
}
return "";
}
public function getLangOrDefault($key, $default = true)
{
$dkey = 'default_'.$key;
$value = $this->getLang($key, $default);
if(!$value || $value == ""){
return __('homeparty.welcome_copy');
}
return $value;
}
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 getTokenLink(){
return url('homeparty/'.$this->token);
}
public function getCardInfo($key)
{
return isset($this->card_info[$key]) ? $this->card_info[$key] : null;
}
public function getUserCountry(){
if(!$this->user_country){
if($user_country_id = $this->getCardInfo('user_country_id')){
$this->user_country = Country::findOrFail($user_country_id);
}
}
return $this->user_country;
}
public function isPriceCurrency()
{
$user_country = $this->getUserCountry();
return ($user_country && $user_country->currency) ? true : false;
}
public function getPriceCurrencyUnit()
{
$user_country = $this->getUserCountry();
return ($user_country && $user_country->currency) ? $user_country->currency_unit : false;
}
}