Die orange Ampel bei abgeschlossenen Bestellungen ist KEIN ERP-Fehler: der
Endpoint /api/wp/status liefert für versendete Bestellungen bereits sent/close
(Live-DB: 111× shipped=2, 1× shipped=3). Ursache liegt shopseitig (Cron/Auth) —
wird per SSH geprüft. Unabhängig davon die latenten Mapping-Lücken geschlossen:
ERP (ShoppingOrder::$apiShippedTypes):
- Status 5 (Wartestellung) fehlte und lieferte den Default 'free' (im Shop gar
kein Licht). Jetzt 5 => 'process' (Shop-Ampel orange = in Bearbeitung).
Connector (partnercenter-api, Teil von v1.6.0 — noch nicht ausgerollt):
- admin/post-list.php: Ampel-Switch deckt jetzt 'pick_up' (Abholung ⇒ grün) und
'cancel' (storniert ⇒ neue rote Kugel) ab; vorher fielen beide auf ⚠️.
- woocommerce/order-status.php: 'pick_up' und 'cancel' gelten jetzt als
Endzustände und fallen aus der stündlichen Nachfrage-Liste
(_partnercenter_api_unclear_order_ids) — vorher wurden 12 stornierte
Bestellungen dauerhaft weiter gepollt.
Test: AP-38-Mapping prüft alle shipped-Werte (kein stiller 'free'-Fallback mehr).
Suite 338 grün.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
506 lines
17 KiB
PHP
506 lines
17 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)
|
|
*
|
|
* @property string|null $wp_label_url
|
|
* @property string|null $wp_label_path
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|ShoppingOrder whereWpLabelUrl($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|ShoppingOrder whereWpLabelPath($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_label_url',
|
|
'wp_label_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',
|
|
'shipped_status_locked' => 'boolean',
|
|
'sale_stock_exempt' => '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)',
|
|
// AP-38: Wartestellung fehlte hier und lieferte den Default 'free' (im Shop
|
|
// gar kein Licht). „In Bearbeitung"-Semantik ⇒ 'process' (Shop-Ampel orange).
|
|
5 => 'process', // Wartestellung (noch nicht versendet)
|
|
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 '';
|
|
}
|
|
|
|
/**
|
|
* AP-33a (Kundenentscheid 16.07.2026): „versandfertig" = bezahlte Bestellung.
|
|
* Zahlart Rechnung (fnc) gilt sofort als bezahlt (Versand vor Zahlungseingang),
|
|
* Vorkasse erst bei Geldeingang, API-Bestellungen (txaction=extern) immer.
|
|
*/
|
|
public function isReadyToShip(): bool
|
|
{
|
|
if (in_array($this->txaction, ['non', 'failed'], true)) {
|
|
return false;
|
|
}
|
|
if ($this->paid || in_array($this->txaction, ['paid', 'extern'], true)) {
|
|
return true;
|
|
}
|
|
|
|
return $this->shopping_payment_last?->clearingtype === 'fnc';
|
|
}
|
|
|
|
/**
|
|
* Über die Partnercenter-API (WooCommerce) übertragene Bestellung — Rechnung
|
|
* existiert im externen Shop (wp_invoice_path), im System wird keine erstellt.
|
|
*/
|
|
public function isApiOrder(): bool
|
|
{
|
|
return $this->txaction === 'extern' || $this->wp_invoice_path !== null;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|