23-01-2026
This commit is contained in:
parent
a939cd51ef
commit
a8b395e20d
248 changed files with 29342 additions and 4805 deletions
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Yard;
|
||||
|
|
@ -20,102 +21,178 @@ class AboOrderCart
|
|||
private static $customer_detail;
|
||||
|
||||
|
||||
public static function initYard($user_abo){
|
||||
public static function initYard($user_abo)
|
||||
{
|
||||
|
||||
// WICHTIG: Statische Variablen zurücksetzen, um sicherzustellen, dass keine Daten
|
||||
// aus vorherigen Abos verwendet werden
|
||||
self::$user_abo = $user_abo;
|
||||
Yard::instance('shopping')->destroy();
|
||||
self::$is_for = null;
|
||||
self::$customer_detail = null;
|
||||
|
||||
// Yard komplett leeren - wichtig für Batch-Verarbeitung mehrerer Abos
|
||||
$yard = Yard::instance('shopping');
|
||||
$itemsBeforeDestroy = $yard->content()->count();
|
||||
$yard->destroy();
|
||||
|
||||
\Log::info('AboOrderCart::initYard: Yard geleert', [
|
||||
'abo_id' => $user_abo->id,
|
||||
'items_vor_destroy' => $itemsBeforeDestroy
|
||||
]);
|
||||
$itemsAfterDestroy = $yard->content()->count();
|
||||
\Log::info('AboOrderCart::initYard: Yard geleert', [
|
||||
'abo_id' => $user_abo->id,
|
||||
'items_after_destroy' => $itemsAfterDestroy
|
||||
]);
|
||||
|
||||
self::$customer_detail = self::makeCustomerDetail($user_abo);
|
||||
if($user_abo->is_for === 'me'){
|
||||
if ($user_abo->is_for === 'me') {
|
||||
self::$is_for = 'abo-me';
|
||||
if($user_abo->user && $user_abo->user->account->same_as_billing){
|
||||
if ($user_abo->user && $user_abo->user->account->same_as_billing) {
|
||||
$country_id = $user_abo->user->account->country_id;
|
||||
}else{
|
||||
} else {
|
||||
$country_id = $user_abo->user->account->shipping_country_id;
|
||||
}
|
||||
if($country_id && $shipping_country = ShippingCountry::whereCountryId($country_id)->first()){
|
||||
if($shipping_country->shipping && $shipping_country->shipping->active){
|
||||
if ($country_id && $shipping_country = ShippingCountry::whereCountryId($country_id)->first()) {
|
||||
if ($shipping_country->shipping && $shipping_country->shipping->active) {
|
||||
UserService::initUserYard($user_abo->user, $shipping_country->id, 'abo-me');
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
abort(403, 'Fehler: Versandland nicht gefunden');
|
||||
}
|
||||
if($user_abo->is_for === 'ot'){
|
||||
if ($user_abo->is_for === 'ot') {
|
||||
self::$is_for = 'abo-ot-customer';
|
||||
UserService::initCustomerYard(self::$customer_detail, 'abo-ot-customer');
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public static function makeOrderYard($user_abo)
|
||||
{
|
||||
{
|
||||
// WICHTIG: Statische Variablen explizit setzen für dieses Abo
|
||||
self::$user_abo = $user_abo;
|
||||
if($user_abo->is_for === 'ot'){
|
||||
if ($user_abo->is_for === 'ot') {
|
||||
self::$is_for = 'abo-ot-customer';
|
||||
}
|
||||
if($user_abo->is_for === 'me'){
|
||||
if ($user_abo->is_for === 'me') {
|
||||
self::$is_for = 'abo-me';
|
||||
}
|
||||
foreach($user_abo->user_abo_items as $abo_item){
|
||||
|
||||
// WICHTIG: Yard IMMER leeren, um sicherzustellen, dass keine Produkte aus vorherigen Aufrufen vorhanden sind
|
||||
// Dies ist besonders wichtig bei wiederholten Aufrufen der detail-Funktion (z.B. durch AJAX-Requests)
|
||||
$yard = Yard::instance('shopping');
|
||||
$itemsBefore = $yard->content()->count();
|
||||
$yard->destroy();
|
||||
|
||||
if ($itemsBefore > 0) {
|
||||
\Log::warning('AboOrderCart::makeOrderYard: Yard war nicht leer vor makeOrderYard und wurde geleert', [
|
||||
'abo_id' => $user_abo->id,
|
||||
'items_before' => $itemsBefore
|
||||
]);
|
||||
}
|
||||
|
||||
// Sicherstellen, dass die Items für dieses spezifische Abo geladen werden
|
||||
// Verwende fresh() um sicherzustellen, dass wir die aktuellen Daten haben
|
||||
$abo_items = $user_abo->user_abo_items()->get();
|
||||
\Log::info('AboOrderCart::makeOrderYard: Füge Produkte zum Cart hinzu', [
|
||||
'abo_id' => $user_abo->id,
|
||||
'item_count' => $abo_items->count(),
|
||||
'items' => $abo_items->map(function ($item) {
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'product_id' => $item->product_id,
|
||||
'qty' => $item->qty,
|
||||
'comp' => $item->comp
|
||||
];
|
||||
})->toArray()
|
||||
]);
|
||||
|
||||
foreach ($abo_items as $abo_item) {
|
||||
self::addProductToCart($abo_item);
|
||||
}
|
||||
Yard::instance('shopping')->reCalculateShippingPrice();
|
||||
|
||||
$user_abo->amount = Yard::instance('shopping')->totalWithShipping(2, '.', '')*100;
|
||||
$user_abo->amount = Yard::instance('shopping')->totalWithShipping(2, '.', '') * 100;
|
||||
$user_abo->save();
|
||||
}
|
||||
|
||||
private static function addProductToCart($item){
|
||||
private static function addProductToCart($item)
|
||||
{
|
||||
|
||||
$product = Product::find($item->product_id);
|
||||
$tax_free = Yard::instance('shopping')->getUserTaxFree();
|
||||
$user_country = Yard::instance('shopping')->getUserCountry();
|
||||
|
||||
if($product){
|
||||
if($item->comp){
|
||||
$cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, 0, false, false,
|
||||
['image' => '', 'slug' => $product->slug, 'weight' => 0, 'points' => 0,
|
||||
'comp' => $item->comp, 'product_id' => $product->id]);
|
||||
if ($product) {
|
||||
if ($item->comp) {
|
||||
$cartItem = Yard::instance('shopping')->add(
|
||||
$product->id,
|
||||
$product->getLang('name'),
|
||||
1,
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
[
|
||||
'image' => '',
|
||||
'slug' => $product->slug,
|
||||
'weight' => 0,
|
||||
'points' => 0,
|
||||
'comp' => $item->comp,
|
||||
'product_id' => $product->id
|
||||
]
|
||||
);
|
||||
Yard::setTax($cartItem->rowId, 0);
|
||||
return true;
|
||||
}
|
||||
if(self::$is_for === 'ot-customer' || self::$is_for === 'abo-ot-customer'){
|
||||
if (self::$is_for === 'ot-customer' || self::$is_for === 'abo-ot-customer') {
|
||||
$cartItem = Yard::instance('shopping')
|
||||
->add($product->id, $product->getLang('name'), $item->qty,
|
||||
round($product->getPriceWith($tax_free, false, $user_country, false, self::$user_abo->user), 1), false, false,
|
||||
['image' => '', 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points, 'no_commission' => $product->no_commission, 'show_on' => $product->show_on]);
|
||||
}else{
|
||||
->add(
|
||||
$product->id,
|
||||
$product->getLang('name'),
|
||||
$item->qty,
|
||||
round($product->getPriceWith($tax_free, false, $user_country, false, self::$user_abo->user), 1),
|
||||
false,
|
||||
false,
|
||||
['image' => '', 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points, 'no_commission' => $product->no_commission, 'show_on' => $product->show_on]
|
||||
);
|
||||
} else {
|
||||
$cartItem = Yard::instance('shopping')
|
||||
->add($product->id, $product->getLang('name'), $item->qty,
|
||||
$product->getPriceWith($tax_free, true, $user_country, false, self::$user_abo->user), false, false,
|
||||
['image' => '', 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points, 'no_commission' => $product->no_commission, 'show_on' => $product->show_on]);
|
||||
->add(
|
||||
$product->id,
|
||||
$product->getLang('name'),
|
||||
$item->qty,
|
||||
$product->getPriceWith($tax_free, true, $user_country, false, self::$user_abo->user),
|
||||
false,
|
||||
false,
|
||||
['image' => '', 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points, 'no_commission' => $product->no_commission, 'show_on' => $product->show_on]
|
||||
);
|
||||
}
|
||||
if($tax_free){
|
||||
if ($tax_free) {
|
||||
Yard::setTax($cartItem->rowId, 0);
|
||||
}else{
|
||||
} else {
|
||||
Yard::setTax($cartItem->rowId, $product->getTaxWith($user_country));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function checkNumOfCompProducts($user_abo){
|
||||
public static function checkNumOfCompProducts($user_abo)
|
||||
{
|
||||
|
||||
if($user_abo->is_for === 'me'){
|
||||
if ($user_abo->is_for === 'me') {
|
||||
$needNumComp = Yard::instance('shopping')->getNumComp();
|
||||
if($needNumComp > 0){
|
||||
if ($needNumComp > 0) {
|
||||
$UserAboItems = UserAboItem::where('user_abo_id', $user_abo->id)->where('comp', '>', 0)->get();
|
||||
if(count($UserAboItems) === $needNumComp){
|
||||
if (count($UserAboItems) === $needNumComp) {
|
||||
return true;
|
||||
}
|
||||
|
||||
//need to add
|
||||
if(count($UserAboItems) < $needNumComp){
|
||||
if (count($UserAboItems) < $needNumComp) {
|
||||
$product = Product::whereActive(true)->where('shipping_addon', true)->whereJsonContains('show_on', '12')->orderBy('pos', 'DESC')->first();
|
||||
for($i = count($UserAboItems); $i <= $needNumComp; $i++){
|
||||
for ($i = count($UserAboItems); $i <= $needNumComp; $i++) {
|
||||
$UserAboItem = UserAboItem::create([
|
||||
'user_abo_id' => $user_abo->id,
|
||||
'product_id' => $product->id,
|
||||
|
|
@ -127,14 +204,14 @@ class AboOrderCart
|
|||
}
|
||||
}
|
||||
//need to remove
|
||||
if(count($UserAboItems) > $needNumComp){
|
||||
foreach($UserAboItems as $UserAboItem){
|
||||
if($UserAboItem->comp > $needNumComp){
|
||||
if (count($UserAboItems) > $needNumComp) {
|
||||
foreach ($UserAboItems as $UserAboItem) {
|
||||
if ($UserAboItem->comp > $needNumComp) {
|
||||
$UserAboItem->delete();
|
||||
}
|
||||
}
|
||||
foreach (Yard::instance('shopping')->content() as $row) {
|
||||
if($row->options->comp > $needNumComp) {
|
||||
if ($row->options->comp > $needNumComp) {
|
||||
Yard::instance('shopping')->remove($row->rowId);
|
||||
}
|
||||
}
|
||||
|
|
@ -143,17 +220,33 @@ class AboOrderCart
|
|||
}
|
||||
}
|
||||
|
||||
public static function getCustomerDetail(){
|
||||
public static function getCustomerDetail()
|
||||
{
|
||||
return self::$customer_detail;
|
||||
}
|
||||
|
||||
/* Need this, can change the address */
|
||||
public static function makeCustomerDetail($user_abo){
|
||||
/* Need this, can change the address */
|
||||
public static function makeCustomerDetail($user_abo)
|
||||
{
|
||||
|
||||
if($user_abo->is_for === 'me'){
|
||||
if ($user_abo->is_for === 'me') {
|
||||
//only on Abo!
|
||||
$user = $user_abo->user;
|
||||
$shopping_user = new ShoppingUser();
|
||||
|
||||
// WICHTIG: Wenn bereits ein shopping_user existiert, diesen replizieren um alle Felder zu behalten
|
||||
// Ansonsten neues Objekt erstellen
|
||||
if ($user_abo->shopping_user) {
|
||||
$shopping_user = $user_abo->shopping_user->replicate();
|
||||
\Log::info('AboOrderCart::makeCustomerDetail: ShoppingUser repliziert für Abo ID: ' . $user_abo->id, [
|
||||
'abo_id' => $user_abo->id,
|
||||
'original_shopping_user_id' => $user_abo->shopping_user->id
|
||||
]);
|
||||
} else {
|
||||
$shopping_user = new ShoppingUser();
|
||||
\Log::info('AboOrderCart::makeCustomerDetail: Neuer ShoppingUser erstellt für Abo ID: ' . $user_abo->id);
|
||||
}
|
||||
|
||||
// Account-Daten überschreiben/aktualisieren
|
||||
$shopping_user->billing_salutation = $user->account->salutation;
|
||||
$shopping_user->billing_company = $user->account->company;
|
||||
$shopping_user->billing_firstname = $user->account->first_name;
|
||||
|
|
@ -164,8 +257,13 @@ class AboOrderCart
|
|||
$shopping_user->billing_city = $user->account->city;
|
||||
$shopping_user->billing_country_id = $user->account->country_id;
|
||||
$shopping_user->billing_phone = $user->account->phone;
|
||||
$shopping_user->billing_email = $user->email ?? null;
|
||||
|
||||
if($user->account->same_as_billing){
|
||||
// Auth User ID setzen falls noch nicht gesetzt
|
||||
if (!$shopping_user->auth_user_id) {
|
||||
$shopping_user->auth_user_id = $user->id;
|
||||
}
|
||||
if ($user->account->same_as_billing) {
|
||||
$shopping_user->shipping_salutation = $user->account->salutation;
|
||||
$shopping_user->shipping_company = $user->account->company;
|
||||
$shopping_user->shipping_firstname = $user->account->first_name;
|
||||
|
|
@ -176,7 +274,9 @@ class AboOrderCart
|
|||
$shopping_user->shipping_city = $user->account->city;
|
||||
$shopping_user->shipping_country_id = $user->account->country_id;
|
||||
$shopping_user->shipping_phone = $user->account->phone;
|
||||
}else{
|
||||
$shopping_user->shipping_postnumber = $user->account->shipping_postnumber;
|
||||
$shopping_user->same_as_billing = 1;
|
||||
} else {
|
||||
$shopping_user->shipping_salutation = $user->account->shipping_salutation;
|
||||
$shopping_user->shipping_company = $user->account->shipping_company;
|
||||
$shopping_user->shipping_firstname = $user->account->shipping_firstname;
|
||||
|
|
@ -187,15 +287,15 @@ class AboOrderCart
|
|||
$shopping_user->shipping_city = $user->account->shipping_city;
|
||||
$shopping_user->shipping_country_id = $user->account->shipping_country_id;
|
||||
$shopping_user->shipping_phone = $user->account->shipping_phone;
|
||||
$shopping_user->shipping_postnumber = $user->account->shipping_postnumber;
|
||||
$shopping_user->same_as_billing = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if($user_abo->is_for === 'ot'){
|
||||
|
||||
if ($user_abo->is_for === 'ot') {
|
||||
//look for the primary user of this abo
|
||||
$shopping_user = $user_abo->shopping_user->replicate();
|
||||
}
|
||||
return $shopping_user;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue