mivita/app/Cron/UserMakeOrder.php
2025-04-01 10:36:47 +02:00

203 lines
No EOL
8.2 KiB
PHP

<?php
namespace App\Cron;
use Yard;
use App\User;
use stdClass;
use App\Services\Shop;
use App\Models\Product;
use App\Models\UserAbo;
use App\Models\ShoppingOrder;
use App\Services\UserService;
use App\Models\ShippingCountry;
use App\Models\ShoppingOrderItem;
use App\Http\Controllers\Pay\PayoneController;
use App\Services\AboOrderCart;
use Illuminate\Support\Facades\Log;
class UserMakeOrder
{
private $userAbo;
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);
}
public function checkProducts()
{
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');
return $ret;
}
//preise prüfen, ob sie sich geändert haben?
foreach($this->userAbo->items as $item){
$ret[] = [
'product_id' => $item->product_id,
'comp' => $item->comp,
'qty' => $item->qty,
'price' => $item->price,
'price_net' => $item->price_net,
'tax_rate' => $item->tax_rate,
'tax' => $item->tax,
'price_vk_net' => $item->price_vk_net,
'discount' => $item->discount,
'points' => $item->points,
];
}
Log::info('Produkte überprüft: ' . count($ret) . ' Artikel gefunden');
return $ret;
}
public function makePayment()
{
Log::info('Starte Zahlungsvorgang für UserAbo ID: ' . $this->userAbo->id);
try {
$this->pay = new PayoneController();
$this->pay->init($this->shopping_user, $this->shopping_order);
$amount = $this->shopping_order->subtotal_ws * 100;
$this->pay->setAboPayment($this->userAbo, $amount, 'EUR');
$this->pay->setPersonalData();
$response = $this->pay->ResponseData(true);
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());
throw $e;
}
}
public function getShoppingPayment()
{
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'));
return $payment;
}
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
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);
return $this->shopping_user;
} catch (\Exception $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);
try {
if (!$this->shopping_user) {
Log::error('Kein Shopping-User verfügbar für Bestellerstellung, UserAbo ID: ' . $this->userAbo->id);
return false;
}
AboOrderCart::initYard($this->userAbo, $this->shopping_user);
//hier wird die Bestellung erstellt inkl aktueller Preise
AboOrderCart::makeOrderYard($this->userAbo);
$yard = Yard::instance('shopping');
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);
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(),
'language' => \App::getLocale(),
'user_shop_id' => $this->userAbo->shopping_user->shopping_order->user_shop->id,
'payment_for' => $this->shopping_user->getOrderPaymentFor(),
'total' => $yard->total(2, '.', ''),
'subtotal' => $yard->subtotal(2, '.', ''),
'shipping' => $yard->shipping(2, '.', ','),
'shipping_net' => $yard->shippingNet(2, '.', ''),
'subtotal_ws' => $yard->subtotalWithShipping(2, '.', ''),
'tax' => $yard->taxWithShipping(2, '.', ''),
'total_shipping' => $yard->totalWithShipping(2, '.', ''),
'points' => $yard->points(),
'weight' => $yard->weight(),
'is_abo' => 1,
'abo_interval' => 0,
'txaction' => 'prev',
'mode' => $this->userAbo->shopping_user->shopping_order->mode,
]);
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()){
$price_net = $yard->rowPriceNet($item, 2, '.', '');
$tax = $item->price - $price_net;
$data = [
'shopping_order_id' => $this->shopping_order->id,
'row_id' => $item->rowId,
'product_id' => $item->id,
'comp' => $item->options->comp,
'qty' => $item->qty,
'price' => $item->price,
'price_net' => $price_net,
'tax_rate' => $item->taxRate,
'tax' => $tax,
'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
];
ShoppingOrderItem::create($data);
$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);
return $this->shopping_order;
} catch (\Exception $e) {
Log::error('Fehler bei Bestellerstellung für UserAbo ID: ' . $this->userAbo->id . ': ' . $e->getMessage());
throw $e;
}
}
}