gruene-seele/app/Models/ShoppingOrder.php

467 lines
15 KiB
PHP

<?php
namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
/**
* App\Models\ShoppingOrder
*
* @property int $id
* @property int $shopping_user_id
* @property int|null $auth_user_id
* @property int $country_id
* @property int $user_shop_id
* @property float|null $total
* @property float|null $shipping
* @property float|null $subtotal
* @property float|null $tax_rate
* @property float|null $tax
* @property float|null $total_shipping
* @property int|null $weight
* @property int|null $paid
* @property string|null $txaction
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property-read User|null $auth_user
* @property-read Country $country
* @property-read Collection|ShoppingOrderItem[] $shopping_order_items
* @property-read int|null $shopping_order_items_count
* @property-read Collection|ShoppingPayment[] $shopping_payments
* @property-read int|null $shopping_payments_count
* @property-read ShoppingUser $shopping_user
* @property-read UserShop $user_shop
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereAuthUserId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereCountryId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder wherePaid($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShipping($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShoppingUserId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereSubtotal($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTax($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTaxRate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTotal($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTotalShipping($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTxaction($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereUserShopId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereWeight($value)
*
* @property int|null $payment_for
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder wherePaymentFor($value)
*
* @property int|null $member_id
* @property string|null $mode
* @property-read User|null $member
* @property-read UserHistory|null $user_history
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereMemberId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereMode($value)
*
* @property Carbon|null $deleted_at
* @property string|null $user_deleted_at
*
* @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingOrder onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereUserDeletedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingOrder withTrashed()
* @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingOrder withoutTrashed()
*
* @property-read ShippingCountry $shipping_country
* @property float|null $shipping_net
* @property float|null $subtotal_shipping
* @property int|null $points
* @property int|null $shipped
* @property string|null $tracking
* @property string|null $wp_invoice_path
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder wherePoints($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShipped($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShippingNet($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereSubtotalWs($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTracking($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereWpInvoicePath($value)
*
* @property array|null $wp_notice
*
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereWpNotice($value)
*
* @property string|null $subtotal_full
* @property-read ShoppingOrderMargin|null $shopping_order_margin
* @property-read ShoppingPayment|null $shopping_payment_last
*
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereSubtotalFull($value)
*
* @property string|null $discount
* @property string|null $payment_credit
*
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereDiscount($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder wherePaymentCredit($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereSubtotalShipping($value)
*
* @property string|null $total_without_credit
*
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereTotalWithoutCredit($value)
*
* @property array|null $invoice
* @property Carbon|null $shipped_at
*
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereInvoice($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereShippedAt($value)
*
* @property string|null $invoice_number
*
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereInvoiceNumber($value)
*
* @property int|null $promotion_user_id
* @property string|null $shipping_option
* @property-read PromotionUser|null $promotion_user
*
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder wherePromotionUserId($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereShippingOption($value)
*
* @property array|null $delivery
*
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereDelivery($value)
*
* @property bool $user_white_label
*
* @method static \Illuminate\Database\Eloquent\Builder<static>|ShoppingOrder whereUserWhiteLabel($value)
*
* @property array<array-key, mixed>|null $cancellation_invoice
*
* @method static \Illuminate\Database\Eloquent\Builder<static>|ShoppingOrder whereCancellationInvoice($value)
*
* @mixin \Eloquent
*/
class ShoppingOrder extends Model
{
protected $table = 'shopping_orders';
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $fillable = [
'shopping_user_id',
'auth_user_id',
'promotion_user_id',
'user_shop_id',
'member_id',
'payment_for',
'country_id',
'total',
'subtotal_full',
'discount',
'subtotal',
'shipping',
'shipping_net',
'subtotal_shipping',
'tax',
'total_without_credit',
'payment_credit',
'total_shipping',
'points',
'weight',
'paid',
'invoice',
'cancellation_invoice',
'delivery',
'user_white_label',
'invoice_number',
'txaction',
'wp_invoice_path',
'wp_notice',
'mode',
'shipped',
'shipped_at',
'shipping_option',
'tracking',
];
protected $casts = [
'wp_notice' => 'array',
'invoice' => 'array',
'cancellation_invoice' => 'array',
'delivery' => 'array',
'shipped_at' => 'datetime',
'user_white_label' => 'boolean',
];
public static $shippedTypes = [
0 => 'offen',
1 => 'in Bearbeitung',
2 => 'versendet',
3 => 'abgeschlossen',
5 => 'Wartestellung',
4 => 'Abholung',
10 => 'storniert',
];
public static $paymentForTypes = [
0 => '',
1 => 'Registrierung',
2 => 'Mitgliedschaft',
3 => 'Guthabenaufladung', // guthaben
4 => 'VP.Bestellung Abholung',
5 => 'VP.Bestellung Lieferung',
6 => 'VP.Kundenbestellung',
7 => 'Promotion',
8 => 'Shop',
9 => '-',
10 => 'extern',
11 => '',
];
public static $paymentForColors = [
0 => 'default',
1 => 'warning',
2 => 'warning',
3 => 'secondary',
4 => 'secondary',
5 => 'secondary',
6 => 'info',
7 => 'dark',
8 => 'info',
9 => 'default',
10 => 'info',
11 => 'default',
];
public static $apiShippedTypes = [
0 => 'open', // (Fullfilment durch Händler)',
1 => 'process', // (Fullfilment: nicht Versand)
2 => 'sent', // (Fullfilment: Versand erfolgt)'
3 => 'close', // (Fullfilment: Versand erfolgt)',
4 => 'pick_up', // (Fullfilment: Versand erfolgt)',
10 => 'cancel',
];
public static $shippedColors = [
0 => 'warning',
1 => 'info',
2 => 'success',
3 => 'secondary',
4 => 'success',
5 => 'warning-dark',
10 => 'danger',
];
public function shopping_user()
{
return $this->belongsTo('App\Models\ShoppingUser', 'shopping_user_id');
}
public function country()
{
return $this->belongsTo('App\Models\ShippingCountry', 'country_id');
}
public function shipping_country()
{
return $this->belongsTo('App\Models\ShippingCountry', 'country_id');
}
public function promotion_user()
{
return $this->belongsTo('App\Models\PromotionUser', 'promotion_user_id');
}
public function user_shop()
{
return $this->belongsTo('App\Models\UserShop', 'user_shop_id');
}
// can null
public function member()
{
return $this->belongsTo('App\User', 'member_id');
}
// can null
public function auth_user()
{
return $this->belongsTo('App\User', 'auth_user_id');
}
public function user_history()
{
return $this->hasOne('App\Models\UserHistory', 'shopping_order_id')->latest();
}
public function shopping_order_margin()
{
return $this->hasOne('App\Models\ShoppingOrderMargin', 'shopping_order_id')->latest();
}
public function shopping_order_items()
{
return $this->hasMany('App\Models\ShoppingOrderItem', 'shopping_order_id');
}
/**
* Mindestens eine Bestellzeile mit White-Label-Produkt (Lieferschein mit Etikett-Infos).
*/
public function hasWhitelabelProducts(): bool
{
return $this->shopping_order_items()
->whereHas('product', function ($query): void {
$query->where('whitelabel', true);
})
->exists();
}
public function shopping_payments()
{
return $this->hasMany('App\Models\ShoppingPayment', 'shopping_order_id');
}
public function shopping_payment_last()
{
return $this->hasOne('App\Models\ShoppingPayment', 'shopping_order_id')->latest();
}
public function setUserHistoryValue($values = [])
{
if ($user_history = $this->user_history) {
foreach ($values as $key => $val) {
$user_history->{$key} = $val;
}
$user_history->save();
}
}
public function getLastShoppingPayment($key = false)
{
$shopping_payment = $this->shopping_payments->last();
if ($shopping_payment) {
if ($key === 'getPaymentType') {
return $shopping_payment->getPaymentType();
}
if ($key === 'reference') {
return $shopping_payment->reference;
}
}
return '';
}
public function getShippedType()
{
return isset(self::$shippedTypes[$this->shipped]) ? self::$shippedTypes[$this->shipped] : '';
}
public function getAPIShippedType()
{
return isset(self::$apiShippedTypes[$this->shipped]) ? self::$apiShippedTypes[$this->shipped] : 'free';
}
public function getShippedColor()
{
return isset(self::$shippedColors[$this->shipped]) ? self::$shippedColors[$this->shipped] : 'default';
}
public function getPaymentForType()
{
return isset(self::$paymentForTypes[$this->payment_for]) ? self::$paymentForTypes[$this->payment_for] : '';
}
public function getPaymentForColor()
{
return isset(self::$paymentForColors[$this->payment_for]) ? self::$paymentForColors[$this->payment_for] : '';
}
public function getFormattedTotal()
{
return formatNumber($this->attributes['total']);
}
public function getFormattedSubtotalFull()
{
return formatNumber($this->attributes['subtotal_full']);
}
public function getFormattedSubtotal()
{
return formatNumber($this->attributes['subtotal']);
}
public function getFormattedDiscount()
{
return formatNumber($this->attributes['discount']);
}
public function getFormattedShipping()
{
return formatNumber($this->attributes['shipping']);
}
public function getFormattedShippingNet()
{
return formatNumber($this->attributes['shipping_net']);
}
public function getFormattedSubtotalShipping()
{
return formatNumber($this->attributes['subtotal_shipping']);
}
public function getFormattedTax()
{
return formatNumber($this->attributes['tax']);
}
public function getFormattedTotalWithoutCredit()
{
return formatNumber($this->attributes['total_without_credit']);
}
public function getFormattedPaymentCredit()
{
return formatNumber($this->attributes['payment_credit']);
}
public function getFormattedTotalShipping()
{
return formatNumber($this->attributes['total_shipping']);
}
public function getItemsCount()
{
$count = 0;
if ($this->shopping_order_items) {
foreach ($this->shopping_order_items as $shopping_order_item) {
$count += $shopping_order_item->qty;
}
}
return $count;
}
public function isInvoice()
{
return $this->user_invoice ? true : false;
}
public function isPickUp()
{
return $this->shipping_option === 'pick_up' ? true : false;
}
public function isTax()
{
return $this->tax > 0 ? true : false;
}
}