10.April 2026
This commit is contained in:
parent
a00c42e770
commit
f58c709945
208 changed files with 19280 additions and 2914 deletions
|
|
@ -2,41 +2,45 @@
|
|||
|
||||
namespace App\Cron;
|
||||
|
||||
use Yard;
|
||||
use App\Models\UserAbo;
|
||||
use App\Http\Controllers\Pay\PayoneController;
|
||||
use App\Models\ShoppingOrder;
|
||||
use App\Models\ShoppingOrderItem;
|
||||
use App\Http\Controllers\Pay\PayoneController;
|
||||
use App\Models\UserAbo;
|
||||
use App\Services\AboOrderCart;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Yard;
|
||||
|
||||
class UserMakeOrder
|
||||
{
|
||||
private $userAbo;
|
||||
private $shopping_user;
|
||||
private $shopping_order;
|
||||
private $is_for;
|
||||
private $user;
|
||||
private $pay;
|
||||
|
||||
private $shopping_user;
|
||||
|
||||
private $shopping_order;
|
||||
|
||||
private $is_for;
|
||||
|
||||
private $user;
|
||||
|
||||
private $pay;
|
||||
|
||||
public function __construct(UserAbo $userAbo)
|
||||
{
|
||||
$this->userAbo = $userAbo;
|
||||
Log::info('UserMakeOrder initialisiert für UserAbo ID: ' . $userAbo->id);
|
||||
Log::info('UserMakeOrder initialisiert für UserAbo ID: '.$userAbo->id);
|
||||
}
|
||||
|
||||
|
||||
public function checkProducts()
|
||||
{
|
||||
Log::info('Überprüfe Produkte für UserAbo ID: ' . $this->userAbo->id);
|
||||
Log::info('Überprüfe Produkte für UserAbo ID: '.$this->userAbo->id);
|
||||
$ret = [];
|
||||
|
||||
if (!$this->userAbo->items || $this->userAbo->items->isEmpty()) {
|
||||
Log::warning('Keine Artikel für UserAbo ID: ' . $this->userAbo->id . ' gefunden');
|
||||
if (! $this->userAbo->items || $this->userAbo->items->isEmpty()) {
|
||||
Log::warning('Keine Artikel für UserAbo ID: '.$this->userAbo->id.' gefunden');
|
||||
|
||||
return $ret;
|
||||
}
|
||||
//preise prüfen, ob sie sich geändert haben?
|
||||
// preise prüfen, ob sie sich geändert haben?
|
||||
foreach ($this->userAbo->items as $item) {
|
||||
$ret[] = [
|
||||
'product_id' => $item->product_id,
|
||||
|
|
@ -52,75 +56,79 @@ class UserMakeOrder
|
|||
];
|
||||
}
|
||||
|
||||
Log::info('Produkte überprüft: ' . count($ret) . ' Artikel gefunden');
|
||||
Log::info('Produkte überprüft: '.count($ret).' Artikel gefunden');
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function makePayment($testmode = false)
|
||||
{
|
||||
Log::info('Starte Zahlungsvorgang für UserAbo ID: ' . $this->userAbo->id);
|
||||
Log::info('Starte Zahlungsvorgang für UserAbo ID: '.$this->userAbo->id);
|
||||
|
||||
try {
|
||||
$this->pay = new PayoneController();
|
||||
$this->pay = new PayoneController;
|
||||
$this->pay->init($this->shopping_user, $this->shopping_order);
|
||||
$amount = $this->shopping_order->subtotal_ws * 100;
|
||||
$amount = $this->shopping_order->total_shipping * 100;
|
||||
// $amount = Yard::instance($this->instance)->totalWithShipping(2, '.', '') * 100;
|
||||
|
||||
$this->pay->setAboPayment($this->userAbo, $amount, 'EUR');
|
||||
$this->pay->setPersonalData();
|
||||
$response = $this->pay->onlyPaymentResponse();
|
||||
\Log::info('Response: ' . json_encode($response));
|
||||
//$response = $this->pay->ResponseData(true);
|
||||
\Log::info('Response: '.json_encode($response));
|
||||
// $response = $this->pay->ResponseData(true);
|
||||
|
||||
Log::info('Zahlungsvorgang abgeschlossen für UserAbo ID: '.$this->userAbo->id.', Status: '.($response->status ?? 'unbekannt'));
|
||||
|
||||
Log::info('Zahlungsvorgang abgeschlossen für UserAbo ID: ' . $this->userAbo->id . ', Status: ' . ($response->status ?? 'unbekannt'));
|
||||
return $response;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Fehler bei Zahlungsvorgang für UserAbo ID: ' . $this->userAbo->id . ': ' . $e->getMessage());
|
||||
Log::error('Fehler bei Zahlungsvorgang für UserAbo ID: '.$this->userAbo->id.': '.$e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function getShoppingPayment()
|
||||
{
|
||||
Log::info('Rufe Zahlungsinformationen ab für UserAbo ID: ' . $this->userAbo->id);
|
||||
Log::info('Rufe Zahlungsinformationen ab für UserAbo ID: '.$this->userAbo->id);
|
||||
|
||||
if ($this->pay) {
|
||||
$payment = $this->pay->getShoppingPayment();
|
||||
Log::info('Zahlungsinformationen abgerufen: ' . ($payment ? 'erfolgreich' : 'nicht verfügbar'));
|
||||
Log::info('Zahlungsinformationen abgerufen: '.($payment ? 'erfolgreich' : 'nicht verfügbar'));
|
||||
|
||||
return $payment;
|
||||
}
|
||||
|
||||
Log::warning('Keine Zahlungsinformationen verfügbar für UserAbo ID: ' . $this->userAbo->id);
|
||||
Log::warning('Keine Zahlungsinformationen verfügbar für UserAbo ID: '.$this->userAbo->id);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function createShoppingUser()
|
||||
{
|
||||
Log::info('Erstelle Shopping-User für UserAbo ID: ' . $this->userAbo->id);
|
||||
//hier muss der letzte shopping_user verwendet werden
|
||||
Log::info('Erstelle Shopping-User für UserAbo ID: '.$this->userAbo->id);
|
||||
// hier muss der letzte shopping_user verwendet werden
|
||||
try {
|
||||
$this->shopping_user = AboOrderCart::makeCustomerDetail($this->userAbo);
|
||||
$this->shopping_user->created_at = now();
|
||||
$this->shopping_user->updated_at = now();
|
||||
$this->shopping_user->save();
|
||||
|
||||
Log::info('Shopping-User erstellt für UserAbo ID: ' . $this->userAbo->id . ', Neue User-ID: ' . $this->shopping_user->id);
|
||||
Log::info('Shopping-User erstellt für UserAbo ID: '.$this->userAbo->id.', Neue User-ID: '.$this->shopping_user->id);
|
||||
|
||||
return $this->shopping_user;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Fehler beim Erstellen des Shopping-Users für UserAbo ID: ' . $this->userAbo->id . ': ' . $e->getMessage());
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Fehler beim Erstellen des Shopping-Users für UserAbo ID: '.$this->userAbo->id.': '.$e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
|
||||
|
||||
Log::warning('Kein Shopping-User verfügbar für UserAbo ID: ' . $this->userAbo->id);
|
||||
return false;
|
||||
}
|
||||
|
||||
public function makeShoppingOrder()
|
||||
{
|
||||
Log::info('Erstelle Bestellung für UserAbo ID: ' . $this->userAbo->id);
|
||||
Log::info('Erstelle Bestellung für UserAbo ID: '.$this->userAbo->id);
|
||||
|
||||
try {
|
||||
if (!$this->shopping_user) {
|
||||
Log::error('Kein Shopping-User verfügbar für Bestellerstellung, UserAbo ID: ' . $this->userAbo->id);
|
||||
if (! $this->shopping_user) {
|
||||
Log::error('Kein Shopping-User verfügbar für Bestellerstellung, UserAbo ID: '.$this->userAbo->id);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -135,18 +143,18 @@ class UserMakeOrder
|
|||
$yardBefore = Yard::instance('shopping');
|
||||
$itemsBefore = $yardBefore->content();
|
||||
if ($itemsBefore->count() > 0) {
|
||||
Log::warning('UserMakeOrder: Yard war nicht leer nach initYard für Abo ID: ' . $this->userAbo->id . ', Items: ' . $itemsBefore->count());
|
||||
Log::warning('UserMakeOrder: Yard war nicht leer nach initYard für Abo ID: '.$this->userAbo->id.', Items: '.$itemsBefore->count());
|
||||
$yardBefore->destroy(); // Erzwinge Leerung
|
||||
}
|
||||
|
||||
//hier wird die Bestellung erstellt inkl aktueller Preise
|
||||
// hier wird die Bestellung erstellt inkl aktueller Preise
|
||||
AboOrderCart::makeOrderYard($this->userAbo);
|
||||
|
||||
$yard = Yard::instance('shopping');
|
||||
|
||||
// Debug: Logge welche Produkte im Cart sind
|
||||
$items = $yard->content();
|
||||
Log::info('UserMakeOrder: Produkte im Cart nach makeOrderYard für Abo ID: ' . $this->userAbo->id, [
|
||||
Log::info('UserMakeOrder: Produkte im Cart nach makeOrderYard für Abo ID: '.$this->userAbo->id, [
|
||||
'abo_id' => $this->userAbo->id,
|
||||
'item_count' => $items->count(),
|
||||
'items' => $items->map(function ($item) {
|
||||
|
|
@ -154,26 +162,57 @@ class UserMakeOrder
|
|||
'product_id' => $item->id,
|
||||
'name' => $item->name,
|
||||
'qty' => $item->qty,
|
||||
'rowId' => $item->rowId
|
||||
'rowId' => $item->rowId,
|
||||
];
|
||||
})->toArray()
|
||||
})->toArray(),
|
||||
]);
|
||||
|
||||
if (!$this->userAbo->shopping_user || !$this->userAbo->shopping_user->shopping_order || !$this->userAbo->shopping_user->shopping_order->user_shop) {
|
||||
Log::error('Fehlende Beziehungsdaten für Bestellerstellung, UserAbo ID: ' . $this->userAbo->id);
|
||||
$shoppingUserStamm = $this->userAbo->shopping_user;
|
||||
if (! $shoppingUserStamm) {
|
||||
Log::error('UserAbo ohne shopping_user (Stammdaten-Kunde), UserAbo ID: '.$this->userAbo->id);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Referenz fuer Shop/Mode: neueste Bestellung (hasOne shopping_order kann null sein z. B. wenn die
|
||||
// aelteste Order soft-deleted ist oder mehrere Orders existieren).
|
||||
$referenceOrder = $shoppingUserStamm->shopping_orders()
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
|
||||
if (! $referenceOrder || ! $referenceOrder->user_shop_id) {
|
||||
Log::error('Fehlende Beziehungsdaten fuer Bestellerstellung (Referenz-Bestellung ohne user_shop_id), UserAbo ID: '.$this->userAbo->id, [
|
||||
'shopping_user_id' => $shoppingUserStamm->id,
|
||||
'reference_order_id' => $referenceOrder?->id,
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$countryId = $yard->getShippingCountryId() ?? $referenceOrder->country_id;
|
||||
if (! $countryId) {
|
||||
Log::error('Kein country_id (Yard shipping_country_id und Referenz-Bestellung leer), UserAbo ID: '.$this->userAbo->id, [
|
||||
'yard_shipping_country_id' => $yard->getShippingCountryId(),
|
||||
'reference_order_id' => $referenceOrder->id,
|
||||
'reference_country_id' => $referenceOrder->country_id,
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->shopping_order = ShoppingOrder::create([
|
||||
'shopping_user_id' => $this->shopping_user->id,
|
||||
'auth_user_id' => $this->shopping_user->auth_user_id,
|
||||
'country_id' => $yard->getShippingCountryId(),
|
||||
'member_id' => $this->userAbo->member_id ?? $referenceOrder->member_id,
|
||||
'auth_user_id' => $this->userAbo->is_for === 'me'
|
||||
? ($this->userAbo->user_id ?? $referenceOrder->auth_user_id ?? $this->shopping_user->auth_user_id)
|
||||
: ($this->shopping_user->auth_user_id ?? $referenceOrder->auth_user_id),
|
||||
'country_id' => $countryId,
|
||||
'language' => \App::getLocale(),
|
||||
'user_shop_id' => $this->userAbo->shopping_user->shopping_order->user_shop->id,
|
||||
'user_shop_id' => (int) $referenceOrder->user_shop_id,
|
||||
'payment_for' => $this->shopping_user->getOrderPaymentFor(),
|
||||
'total' => $yard->total(2, '.', ''),
|
||||
'subtotal' => $yard->subtotal(2, '.', ''),
|
||||
'shipping' => $yard->shipping(2, '.', ','),
|
||||
'shipping' => $yard->shipping(2, '.', ''),
|
||||
'shipping_net' => $yard->shippingNet(2, '.', ''),
|
||||
'subtotal_ws' => $yard->subtotalWithShipping(2, '.', ''),
|
||||
'tax' => $yard->taxWithShipping(2, '.', ''),
|
||||
|
|
@ -183,21 +222,21 @@ class UserMakeOrder
|
|||
'is_abo' => 1,
|
||||
'abo_interval' => $this->userAbo->abo_interval ?? 0,
|
||||
'txaction' => 'prev',
|
||||
'mode' => $this->userAbo->shopping_user->shopping_order->mode,
|
||||
'mode' => $referenceOrder->mode,
|
||||
]);
|
||||
|
||||
Log::info('Bestellung erstellt für UserAbo ID: ' . $this->userAbo->id . ', Bestellnummer: ' . $this->shopping_order->id);
|
||||
Log::info('Bestellung erstellt für UserAbo ID: '.$this->userAbo->id.', Bestellnummer: '.$this->shopping_order->id);
|
||||
|
||||
$items = $yard->getContentByOrder();
|
||||
$itemCount = 0;
|
||||
|
||||
foreach ($items as $item) {
|
||||
if (!ShoppingOrderItem::where('shopping_order_id', $this->shopping_order->id)->where('row_id', $item->rowId)->count()) {
|
||||
if (! ShoppingOrderItem::where('shopping_order_id', $this->shopping_order->id)->where('row_id', $item->rowId)->count()) {
|
||||
$price_net = $yard->rowPriceNet($item, 2, '.', '');
|
||||
$tax = $item->price - $price_net;
|
||||
$data = [
|
||||
'shopping_order_id' => $this->shopping_order->id,
|
||||
'row_id' => $item->rowId,
|
||||
'row_id' => $item->rowId,
|
||||
'product_id' => $item->id,
|
||||
'comp' => $item->options->comp,
|
||||
'qty' => $item->qty,
|
||||
|
|
@ -208,21 +247,21 @@ class UserMakeOrder
|
|||
'price_vk_net' => $this->shopping_order->getPriceVkNetBy($item->id),
|
||||
'discount' => $item->options->no_commission ? 0 : $this->shopping_order->getUserDiscount(),
|
||||
'points' => $item->options->points,
|
||||
'slug' => $item->options->slug
|
||||
'slug' => $item->options->slug,
|
||||
];
|
||||
ShoppingOrderItem::create($data);
|
||||
$itemCount++;
|
||||
}
|
||||
}
|
||||
|
||||
Log::info('Bestellpositionen hinzugefügt für UserAbo ID: ' . $this->userAbo->id . ', Anzahl: ' . $itemCount);
|
||||
Log::info('Bestellpositionen hinzugefügt für UserAbo ID: '.$this->userAbo->id.', Anzahl: '.$itemCount);
|
||||
|
||||
$this->shopping_order->makeTaxSplit();
|
||||
Log::info('Steueraufteilung für Bestellung abgeschlossen, UserAbo ID: ' . $this->userAbo->id);
|
||||
Log::info('Steueraufteilung für Bestellung abgeschlossen, UserAbo ID: '.$this->userAbo->id);
|
||||
|
||||
return $this->shopping_order;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Fehler bei Bestellerstellung für UserAbo ID: ' . $this->userAbo->id . ': ' . $e->getMessage());
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Fehler bei Bestellerstellung für UserAbo ID: '.$this->userAbo->id.': '.$e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue