23-01-2026

This commit is contained in:
Kevin Adametz 2026-01-23 17:35:23 +01:00
parent a939cd51ef
commit a8b395e20d
248 changed files with 29342 additions and 4805 deletions

View file

@ -1,68 +1,154 @@
<?php
namespace App\Cron;
use App\User;
use App\Models\UserBusiness;
use App\Models\UserLevel;
use App\Services\HTMLHelper;
use App\Models\UserCreditItem;
use App\Mail\MailUserLevelUpdate;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Log;
use App\Repositories\CreditRepository;
class UserLevelUpdate
{
private $month;
private $year;
private $userLevels;
public function __construct($month, $year)
{
$this->month = $month;
$this->year = $year;
// Lade UserLevels für POS-Vergleich (wird für Prüfung benötigt)
$this->userLevels = UserLevel::where('active', 1)->orderBy('pos')->get()->keyBy('id');
}
public function getUserBusinessByMonthYear(){
return UserBusiness::select('user_businesses.*')
/**
* Holt alle UserBusiness Einträge die Level-Updates benötigen
* Mit Eager Loading für bessere Performance
*/
public function getUserBusinessByMonthYear()
{
return UserBusiness::select('user_businesses.*')
->with('user') // Eager Loading für User
->where('user_businesses.month', '=', $this->month)
->where('user_businesses.year', '=', $this->year)
->where('user_businesses.next_qual_user_level', '!=', NULL)
->get();
->whereNotNull('user_businesses.next_qual_user_level')
->whereRaw("JSON_LENGTH(user_businesses.next_qual_user_level) > 0")
->get();
}
public function makeUserLevelUpdate(UserBusiness $userBusiness, $send_update_mail){
/**
* Aktualisiert das User-Level basierend auf next_qual_user_level
* Berücksichtigt Arrays und einzelne Level-Objekte
* Prüft ob das neue Level höher ist als das aktuelle
*/
public function makeUserLevelUpdate(UserBusiness $userBusiness, $send_update_mail = false)
{
$ret = false;
if (!$userBusiness->user) {
Log::warning("UserLevelUpdate: UserBusiness {$userBusiness->id} hat kein User-Objekt");
return $ret;
}
$nextQualUserLevel = $userBusiness->next_qual_user_level;
if(!isset($nextQualUserLevel['hasUpdated']) && $userBusiness->user){
$userBusiness->user->m_level = $nextQualUserLevel['id'];
if (!is_array($nextQualUserLevel) || empty($nextQualUserLevel)) {
return $ret;
}
// next_qual_user_level ist ein einzelnes Level-Objekt
if (is_array($nextQualUserLevel) && isset($nextQualUserLevel['id'])) {
// return wenn bereits aktualisierte Level
if (isset($nextQualUserLevel['hasUpdated']) && $nextQualUserLevel['hasUpdated'] == 1) {
return $ret;
}
$newLevelId = $nextQualUserLevel['id'];
$newLevelPos = null;
// Lade Level-Objekt für POS-Vergleich
$newLevel = $this->userLevels->get($newLevelId);
if ($newLevel) {
$newLevelPos = $newLevel->pos;
}
// Prüfe ob das neue Level höher ist als das aktuelle
$currentUserLevel = null;
if ($userBusiness->user->m_level) {
$currentUserLevel = $this->userLevels->get($userBusiness->user->m_level);
}
// Nur updaten wenn das neue Level höher ist (POS > aktuelles Level POS)
if (!$currentUserLevel || !$newLevelPos) {
return $ret;
}
if ($newLevelPos <= $currentUserLevel->pos) {
return $ret;
}
}
// Update durchführen wenn ein höheres Level gefunden wurde
try {
$userBusiness->user->m_level = $newLevel['id'];
$userBusiness->user->save();
// Markiere das Level als aktualisiert in next_qual_user_level
$nextQualUserLevel['hasUpdated'] = 1;
$userBusiness->next_qual_user_level = $nextQualUserLevel;
$userBusiness->save();
$ret = $nextQualUserLevel['id'].' '.$nextQualUserLevel['name'];
if($send_update_mail){
self::sendUpdateMail($userBusiness->user, $userBusiness->total_qual_pp, $nextQualUserLevel['name']);
$ret = $newLevelId . ' ' . ($newLevel->name ?? 'Unbekannt');
if ($send_update_mail) {
try {
$this->sendUpdateMail(
$userBusiness->user,
$userBusiness->payline_points_qual_kp ?? 0,
$newLevel->name ?? 'Unbekannt'
);
} catch (\Exception $e) {
Log::warning("UserLevelUpdate: E-Mail konnte nicht gesendet werden für User {$userBusiness->user->id}: " . $e->getMessage());
// E-Mail-Fehler sollten das Update nicht verhindern
}
}
}
Log::info("UserLevelUpdate: User {$userBusiness->user->id} Level aktualisiert zu {$ret}");
} catch (\Exception $e) {
Log::error("UserLevelUpdate: Fehler beim Update von User {$userBusiness->user->id}: " . $e->getMessage());
throw $e;
}
return $ret;
}
private function sendUpdateMail(User $user, $tp, $to){
private function sendUpdateMail(User $user, $tp, $to)
{
$bcc = [];
$email = $user->email;
if(!$email){
if($user->mode === 'test'){
}else{
if (!$email) {
if ($user->mode === 'test') {
} else {
$email = config('app.checkout_mail');
}
}
if($user->mode === 'test'){
if ($user->mode === 'test') {
$bcc[] = config('app.checkout_test_mail');
}else{
} else {
$bcc[] = config('app.checkout_mail');
}
if (\App\Services\Util::isTestSystem()) {
$email = config('app.checkout_test_mail');
$bcc[] = config('app.checkout_test_mail');
}
Mail::to($email)->bcc($bcc)->locale($user->getLocale())->send(new MailUserLevelUpdate($tp, $to));
}
}

View file

@ -1,15 +1,10 @@
<?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;
@ -42,7 +37,7 @@ class UserMakeOrder
return $ret;
}
//preise prüfen, ob sie sich geändert haben?
foreach($this->userAbo->items as $item){
foreach ($this->userAbo->items as $item) {
$ret[] = [
'product_id' => $item->product_id,
'comp' => $item->comp,
@ -61,18 +56,20 @@ class UserMakeOrder
return $ret;
}
public function makePayment()
public function makePayment($testmode = false)
{
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);
$response = $this->pay->onlyPaymentResponse();
\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'));
return $response;
} catch (\Exception $e) {
@ -84,13 +81,13 @@ class UserMakeOrder
public function getShoppingPayment()
{
Log::info('Rufe Zahlungsinformationen ab für UserAbo ID: ' . $this->userAbo->id);
if($this->pay){
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;
}
@ -99,20 +96,20 @@ class UserMakeOrder
{
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;
}
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;
}
@ -120,19 +117,48 @@ class UserMakeOrder
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);
// WICHTIG: Yard komplett leeren vor jedem Abo, um sicherzustellen, dass keine Produkte
// aus vorherigen Abos im Cart bleiben
Yard::instance('shopping')->destroy();
// initYard akzeptiert nur einen Parameter (user_abo)
AboOrderCart::initYard($this->userAbo);
// Nochmalige Sicherheitsprüfung: Yard sollte leer sein
$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());
$yardBefore->destroy(); // Erzwinge Leerung
}
//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, [
'abo_id' => $this->userAbo->id,
'item_count' => $items->count(),
'items' => $items->map(function ($item) {
return [
'product_id' => $item->id,
'name' => $item->name,
'qty' => $item->qty,
'rowId' => $item->rowId
];
})->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);
return false;
@ -155,7 +181,7 @@ class UserMakeOrder
'points' => $yard->points(),
'weight' => $yard->weight(),
'is_abo' => 1,
'abo_interval' => 0,
'abo_interval' => $this->userAbo->abo_interval ?? 0,
'txaction' => 'prev',
'mode' => $this->userAbo->shopping_user->shopping_order->mode,
]);
@ -164,9 +190,9 @@ class UserMakeOrder
$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 = [
@ -188,16 +214,16 @@ class UserMakeOrder
$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;
}
}
}
}