462 lines
16 KiB
PHP
462 lines
16 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Services\ShoppingUserService;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
/**
|
|
* App\Models\ShoppingUser
|
|
*
|
|
* @property int $id
|
|
* @property int|null $auth_user_id
|
|
* @property string|null $billing_salutation
|
|
* @property string|null $billing_company
|
|
* @property string|null $billing_firstname
|
|
* @property string|null $billing_lastname
|
|
* @property string|null $billing_address
|
|
* @property string|null $billing_address_2
|
|
* @property string|null $billing_zipcode
|
|
* @property string|null $billing_city
|
|
* @property int $billing_country_id
|
|
* @property string|null $billing_phone
|
|
* @property string|null $billing_email
|
|
* @property int $accepted_data_checkbox
|
|
* @property int $same_as_billing
|
|
* @property string|null $shipping_salutation
|
|
* @property string|null $shipping_company
|
|
* @property string|null $shipping_firstname
|
|
* @property string|null $shipping_lastname
|
|
* @property string|null $shipping_address
|
|
* @property string|null $shipping_address_2
|
|
* @property string|null $shipping_zipcode
|
|
* @property string|null $shipping_city
|
|
* @property int $shipping_country_id
|
|
* @property string|null $shipping_phone
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShoppingOrder[] $Shopping_orders
|
|
* @property-read int|null $shopping_orders_count
|
|
* @property-read \App\Models\Country $billing_country
|
|
* @property-read \App\Models\Country $shipping_country
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereAcceptedDataCheckbox($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereAuthUserId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingAddress($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingAddress2($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingCity($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingCompany($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingCountryId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingEmail($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingFirstname($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingLastname($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingPhone($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingSalutation($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereBillingZipcode($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereSameAsBilling($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingAddress($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingAddress2($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingCity($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingCompany($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingCountryId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingFirstname($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingLastname($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingPhone($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingSalutation($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingZipcode($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereUpdatedAt($value)
|
|
*
|
|
* @property int|null $orders
|
|
* @property-read \App\Models\ShoppingOrder $shopping_order
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShoppingOrder[] $shopping_orders
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereOrders($value)
|
|
*
|
|
* @property int|null $abo_options
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereAboOptions($value)
|
|
*
|
|
* @property int|null $member_id
|
|
* @property int|null $number
|
|
* @property bool $is_like
|
|
* @property array|null $notice
|
|
* @property-read \App\User|null $auth_user
|
|
* @property-read \App\User|null $member
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereIsLike($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereMemberId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereNotice($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereNumber($value)
|
|
*
|
|
* @property bool|null $has_buyed
|
|
* @property bool|null $subscribed
|
|
* @property int|null $wp_order_number
|
|
* @property string|null $wp_order_date
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereHasBuyed($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereSubscribed($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereWpOrderDate($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereWpOrderNumber($value)
|
|
*
|
|
* @property \Illuminate\Support\Carbon|null $deleted_at
|
|
* @property string|null $user_deleted_at
|
|
*
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingUser onlyTrashed()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereDeletedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereUserDeletedAt($value)
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingUser withTrashed()
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingUser withoutTrashed()
|
|
*
|
|
* @property string|null $mode
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereMode($value)
|
|
*
|
|
* @property bool|null $faker_mail
|
|
* @property string|null $shipping_email
|
|
* @property string|null $is_for
|
|
* @property string|null $is_from
|
|
* @property int|null $shopping_user_id
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereFakerMail($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereIsFor($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereIsFrom($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShippingEmail($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingUser whereShoppingUserId($value)
|
|
*
|
|
* @property int|null $homeparty_id
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingUser whereHomepartyId($value)
|
|
*
|
|
* @property int|null $shopping_collect_order_id
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingUser whereShoppingCollectOrderId($value)
|
|
*
|
|
* @property string|null $remarks
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingUser whereRemarks($value)
|
|
*
|
|
* @property string|null $language
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingUser whereLanguage($value)
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
class ShoppingUser extends Model
|
|
{
|
|
protected $table = 'shopping_users';
|
|
|
|
use SoftDeletes;
|
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
protected $fillable = [
|
|
'auth_user_id',
|
|
'member_id',
|
|
'number',
|
|
'is_like',
|
|
'language',
|
|
'billing_salutation',
|
|
'billing_company',
|
|
'billing_firstname',
|
|
'billing_lastname',
|
|
'billing_address',
|
|
'billing_address_2',
|
|
'billing_zipcode',
|
|
'billing_city',
|
|
'billing_country_id',
|
|
'billing_phone',
|
|
'billing_email',
|
|
'faker_mail',
|
|
'shipping_email',
|
|
'accepted_data_checkbox',
|
|
'same_as_billing',
|
|
'shipping_salutation',
|
|
'shipping_company',
|
|
'shipping_firstname',
|
|
'shipping_lastname',
|
|
'shipping_address',
|
|
'shipping_address_2',
|
|
'shipping_zipcode',
|
|
'shipping_city',
|
|
'shipping_country_id',
|
|
'shipping_phone',
|
|
'shipping_postnumber',
|
|
'has_buyed',
|
|
'subscribed',
|
|
'notice',
|
|
'remarks',
|
|
'mode',
|
|
'is_for',
|
|
'is_from',
|
|
'shopping_user_id',
|
|
'homeparty_id',
|
|
'shopping_collect_order_id',
|
|
'wp_order_number',
|
|
'wp_order_date',
|
|
];
|
|
|
|
protected $casts = [
|
|
'notice' => 'array',
|
|
'is_like' => 'bool',
|
|
'faker_mail' => 'bool',
|
|
'accepted_data_checkbox' => 'bool',
|
|
'same_as_billing' => 'bool',
|
|
'has_buyed' => 'bool',
|
|
'subscribed' => 'bool',
|
|
'wp_order_number' => 'int',
|
|
];
|
|
|
|
// can null
|
|
public function member()
|
|
{
|
|
return $this->belongsTo('App\User', 'member_id');
|
|
}
|
|
|
|
public function auth_user()
|
|
{
|
|
return $this->belongsTo('App\User', 'auth_user_id');
|
|
}
|
|
|
|
public function billing_country()
|
|
{
|
|
return $this->belongsTo('App\Models\Country', 'billing_country_id');
|
|
}
|
|
|
|
public function shipping_country()
|
|
{
|
|
return $this->belongsTo('App\Models\Country', 'shipping_country_id');
|
|
}
|
|
|
|
public function shopping_orders()
|
|
{
|
|
return $this->hasMany('App\Models\ShoppingOrder', 'shopping_user_id');
|
|
}
|
|
|
|
public function shopping_order()
|
|
{
|
|
return $this->hasOne('App\Models\ShoppingOrder', 'shopping_user_id');
|
|
}
|
|
|
|
/**
|
|
* Accessor für das language Attribut.
|
|
* Gibt die App-Locale zurück, wenn keine Sprache gesetzt ist.
|
|
*
|
|
* @param string|null $value
|
|
* @return string Sprachcode (de, en, es)
|
|
*/
|
|
public function getLanguageAttribute($value): string
|
|
{
|
|
return $value ?: \App::getLocale();
|
|
}
|
|
|
|
/**
|
|
* Alias für getLanguageAttribute - für Konsistenz mit anderen Models.
|
|
*
|
|
* @return string Sprachcode (de, en, es)
|
|
*/
|
|
public function getLocale(): string
|
|
{
|
|
return $this->language;
|
|
}
|
|
|
|
/**
|
|
* Liefert die verfügbaren Sprachen aus der Config als Array für Select-Felder.
|
|
*
|
|
* @return array ['code' => 'Native Name', ...]
|
|
*/
|
|
public static function getAvailableLanguages(): array
|
|
{
|
|
$locales = config('localization.supportedLocales', []);
|
|
$languages = [];
|
|
|
|
foreach ($locales as $code => $locale) {
|
|
$languages[$code] = $locale['native'] ?? $locale['name'] ?? $code;
|
|
}
|
|
|
|
return $languages;
|
|
}
|
|
|
|
public function setNotice($key, $value)
|
|
{
|
|
$notice = $this->notice;
|
|
$notice[$key] = $value;
|
|
$this->notice = $notice;
|
|
$this->save();
|
|
}
|
|
|
|
public function getNotice($key)
|
|
{
|
|
return isset($this->notice[$key]) ? $this->notice[$key] : false;
|
|
}
|
|
|
|
public function removeNotice($key)
|
|
{
|
|
$notice = $this->notice;
|
|
if (isset($notice[$key])) {
|
|
unset($notice[$key]);
|
|
}
|
|
$this->notice = $notice;
|
|
$this->save();
|
|
}
|
|
|
|
public function firstEntryByNumber()
|
|
{
|
|
|
|
if ($this->number > 0) {
|
|
if ($shopping_user = ShoppingUser::where('number', $this->number)->orderBy('created_at', 'ASC')->first()) {
|
|
return $shopping_user;
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function lastEntryByNumber()
|
|
{
|
|
|
|
if ($this->number > 0) {
|
|
if ($shopping_user = ShoppingUser::where('number', $this->number)->orderBy('created_at', 'DESC')->first()) {
|
|
return $shopping_user;
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getOrderPaymentFor()
|
|
{
|
|
|
|
switch ($this->is_from) {
|
|
case 'wizard':
|
|
return 1;
|
|
case 'membership':
|
|
return 2;
|
|
case 'user_order':
|
|
return ($this->is_for === 'me' || $this->is_for === 'abo_me') ? 3 : 4;
|
|
case 'homeparty':
|
|
return 5;
|
|
case 'shopping':
|
|
return 6;
|
|
case 'extern':
|
|
return 7;
|
|
case 'collection':
|
|
return 8;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public function setIsForAttribute($value)
|
|
{
|
|
if ($value === 'abo-me' || $value === 'me') {
|
|
$this->attributes['is_for'] = 'me';
|
|
|
|
return;
|
|
}
|
|
if ($value === 'ot-member' || $value === 'ot-customer' || $value === 'abo-ot-member' || $value === 'abo-ot-customer') {
|
|
$this->attributes['is_for'] = 'ot';
|
|
|
|
return;
|
|
}
|
|
$this->attributes['is_for'] = $value;
|
|
}
|
|
|
|
public function getAPIShippedType()
|
|
{
|
|
if ($this->shopping_order) {
|
|
return $this->shopping_order->getAPIShippedType();
|
|
}
|
|
|
|
return 'free';
|
|
}
|
|
|
|
public function getFullNameAsArray()
|
|
{
|
|
return [
|
|
'company' => $this->billing_company,
|
|
'salutation' => $this->billing_salutation,
|
|
'firstname' => $this->billing_firstname,
|
|
'lastname' => $this->billing_lastname,
|
|
'email' => $this->billing_email,
|
|
|
|
];
|
|
}
|
|
|
|
public function getAllOrdersByMember()
|
|
{
|
|
return ShoppingUserService::getAllOrdersByMember($this);
|
|
}
|
|
|
|
public function getDeliveryCountry($get_country = false)
|
|
{
|
|
if ($this->same_as_billing == 1) {
|
|
if ($this->billing_country_id) {
|
|
if ($get_country) {
|
|
return $this->billing_country;
|
|
}
|
|
|
|
return $this->billing_country->getLocated();
|
|
}
|
|
} else {
|
|
if ($this->shipping_country_id) {
|
|
if ($get_country) {
|
|
return $this->shipping_country;
|
|
}
|
|
|
|
return $this->shipping_country->getLocated();
|
|
}
|
|
}
|
|
|
|
return 'not set';
|
|
}
|
|
|
|
/**
|
|
* Prüft ob es sich um eine Packstation/Paketbox-Lieferung handelt
|
|
*/
|
|
public function isPackstationDelivery(): bool
|
|
{
|
|
return ! empty($this->shipping_postnumber);
|
|
}
|
|
|
|
/**
|
|
* Liefert die effektive Lieferadresse (berücksichtigt same_as_billing)
|
|
*/
|
|
public function getEffectiveShippingAddress(): array
|
|
{
|
|
if ($this->same_as_billing) {
|
|
return [
|
|
'salutation' => $this->billing_salutation,
|
|
'company' => $this->billing_company,
|
|
'firstname' => $this->billing_firstname,
|
|
'lastname' => $this->billing_lastname,
|
|
'address' => $this->billing_address,
|
|
'address_2' => $this->billing_address_2,
|
|
'zipcode' => $this->billing_zipcode,
|
|
'city' => $this->billing_city,
|
|
'country_id' => $this->billing_country_id,
|
|
'phone' => $this->billing_phone,
|
|
'postnumber' => null, // Bei same_as_billing keine Packstation
|
|
];
|
|
}
|
|
|
|
return [
|
|
'salutation' => $this->shipping_salutation,
|
|
'company' => $this->shipping_company,
|
|
'firstname' => $this->shipping_firstname,
|
|
'lastname' => $this->shipping_lastname,
|
|
'address' => $this->shipping_address,
|
|
'address_2' => $this->shipping_address_2,
|
|
'zipcode' => $this->shipping_zipcode,
|
|
'city' => $this->shipping_city,
|
|
'country_id' => $this->shipping_country_id,
|
|
'phone' => $this->shipping_phone,
|
|
'postnumber' => $this->shipping_postnumber,
|
|
];
|
|
}
|
|
}
|