middleware('active.account'); } public function index() { return view('user.order.index'); } public function detail($id) { $user = User::find(Auth::user()->id); $shopping_order = ShoppingOrder::findOrFail($id); if ($shopping_order->auth_user_id !== $user->id) { Log::channel(self::LOG_CHANNEL)->warning("Unauthorized access attempt to order #{$id} by user #{$user->id}"); abort(404); } if ($shopping_order->payment_for === 6 || $shopping_order->payment_for === 7) { Log::channel(self::LOG_CHANNEL)->info("Redirecting user #{$user->id} to customer order detail for order #{$id}"); return redirect(route('user_shop_order_detail', [$shopping_order->id])); } $shopping_order->getLastShoppingPayment(); return view('user.order.detail', [ 'shopping_order' => $shopping_order, 'isAdmin' => false, ]); } public function ordersDatatable() { $user = User::find(Auth::user()->id); $query = ShoppingOrder::with('shopping_user', 'shopping_payments') ->select('shopping_orders.*') ->where('auth_user_id', '=', $user->id) ->where('txaction', '!=', NULL); return \DataTables::eloquent($query) ->addColumn('id', function (ShoppingOrder $ShoppingOrder) { return ''; }) ->addColumn('created_at', function (ShoppingOrder $ShoppingOrder) { return $ShoppingOrder->created_at->format("d.m.Y"); }) ->addColumn('txaction', function (ShoppingOrder $ShoppingOrder) { return Payment::getShoppingOrderBadge($ShoppingOrder); }) ->addColumn('total_shipping', function (ShoppingOrder $ShoppingOrder) { return ''.$ShoppingOrder->getFormattedTotalShipping()." €"; }) ->addColumn('payment', function (ShoppingOrder $ShoppingOrder) { return $ShoppingOrder->getLastShoppingPayment('getPaymentType'); }) ->addColumn('shipped', function (ShoppingOrder $ShoppingOrder) { if ($ShoppingOrder->payment_for === 8) { return ''; } return ''.$ShoppingOrder->getShippedType().''; }) ->addColumn('payment_for', function (ShoppingOrder $ShoppingOrder) { return Payment::getPaymentForBadge($ShoppingOrder); }) ->addColumn('invoice', function (ShoppingOrder $ShoppingOrder) { return $ShoppingOrder->isInvoice() ? ' ' : '-'; }) ->addColumn('reference', function (ShoppingOrder $ShoppingOrder) { return $ShoppingOrder->getLastShoppingPayment('reference'); }) ->orderColumn('id', 'id $1') ->orderColumn('txaction', 'txaction $1') ->orderColumn('shipped', 'shipped $1') ->orderColumn('total_shipping', 'total_shipping $1') ->orderColumn('payment_for', 'payment_for $1') ->rawColumns(['id', 'txaction', 'payment_for', 'total_shipping', 'invoice', 'shipped']) ->make(true); } /* $for = me, ot-member, ot-customer, abo-ot-member, abo-ot-customer, abo-me */ public function delivery($for, $id = null) { $user = User::find(Auth::user()->id); $shopping_user = null; $delivery_id = null; if (strpos($for, 'ot') !== false) { $shopping_user = Shop::checkShoppingUser($id, $user); $delivery_id = $shopping_user->id; if (!Shop::checkShoppingCountry($for, $delivery_id) && !\Session()->has('custom-error')) { $country = Shop::getDeliveryCountry($for, $delivery_id); \Session()->flash('custom-error', $country.": ".__('validation.custom.shipping_not_found')); Log::channel(self::LOG_CHANNEL)->warning("Shipping country not found for user #{$user->id}, country: {$country}"); return redirect(route('user_order_my_delivery', [$for, $delivery_id])); } if ($for === 'abo-ot-customer') { if (AboHelper::hasAboByEmail($shopping_user->billing_email) && !\Session()->has('custom-error')) { \Session()->flash('custom-error', __('abo.error_email_has_abo', ['email' => $shopping_user->billing_email])); Log::channel(self::LOG_CHANNEL)->info("User #{$user->id} attempted to create abo for email that already has one: {$shopping_user->billing_email}"); return redirect(route('user_order_my_delivery', [$for, $delivery_id])); } } } if (Request::get('action') === 'next') { Yard::instance('shopping')->destroy(); if (strpos(Request::get('switchers-radio-is-for'), 'ot') !== false) { $delivery_id = $id; } return redirect(route('user_order_my_list', [Request::get('switchers-radio-is-for'), $delivery_id])); } return view('user.order.delivery', [ 'shopping_user' => $shopping_user, 'isAdmin' => false, 'isView' => 'customer', 'for' => $for, 'delivery_id' => $delivery_id, ]); } public function list($for, $id = null) { $user = User::find(Auth::user()->id); if ($for === 'abo-me' && AboHelper::userHasAbo($user)) { Log::channel(self::LOG_CHANNEL)->warning("User #{$user->id} attempted to create abo but already has one"); abort(403, 'User has an Abo. Cannot order.'); } $shopping_user = null; $delivery_id = null; if (strpos($for, 'ot') !== false) { $shopping_user = Shop::checkShoppingUser($id, $user); $delivery_id = $shopping_user->id; } if ($for === 'ot-customer' || $for === 'abo-ot-customer') { UserService::initCustomerYard($shopping_user, $for); } else { $shipping_country_id = Shop::checkShoppingCountry($for, $id); if (!$shipping_country_id) { $country = Shop::getDeliveryCountry($for, $id); \Session()->flash('custom-error', $country.": ".__('validation.custom.shipping_not_found')); Log::channel(self::LOG_CHANNEL)->warning("Shipping country not found for user #{$user->id}, country: {$country}"); return redirect(route('user_order_my_delivery', [$for, $delivery_id])); } UserService::initUserYard($user, $shipping_country_id, $for); } return view('user.order.list', [ 'shopping_user' => $shopping_user, 'user' => $user, 'isAdmin' => false, 'isView' => 'customer', 'for' => $for, 'template' => str_replace('abo-', '', $for), 'delivery_id' => $delivery_id, 'is_abo' => strpos($for, 'abo') !== false, 'comp_products' => Shop::getCompProducts($for), ]); } public function payment($for, $id = null) { $data = Request::all(); $user = User::find(Auth::user()->id); $rules = [ 'shipping_salutation' => 'required', 'shipping_firstname' => 'required', 'shipping_lastname' => 'required', 'shipping_address' => 'required', 'shipping_zipcode' => 'required', 'shipping_city' => 'required', 'shipping_state' => 'required', ]; $validator = Validator::make(Request::all(), $rules); if ($validator->fails()) { Log::channel(self::LOG_CHANNEL)->info("Validation failed for payment form", ['errors' => $validator->errors()->toArray()]); return back()->withErrors($validator)->withInput(Request::all()); } try { $this->checkSendYardForPayment($data, $id); } catch (\Exception $e) { Log::channel(self::LOG_CHANNEL)->error("Error checking yard for payment: " . $e->getMessage(), [ 'user_id' => $user->id, 'for' => $for, 'id' => $id ]); return back()->with('error', $e->getMessage()); } if (Yard::instance('shopping')->getNumComp() > 0) { if (!isset($data['switchers-comp-product'])) { $validator->errors()->add('switchers-comp-product', __('msg.please_select_compensation_product')); Log::channel(self::LOG_CHANNEL)->info("Compensation product not selected"); } else if (!is_array($data['switchers-comp-product'])) { $validator->errors()->add('switchers-comp-product', __('msg.please_select_compensation_product')); Log::channel(self::LOG_CHANNEL)->info("Compensation product selection is not an array"); } else if (count($data['switchers-comp-product']) !== Yard::instance('shopping')->getNumComp()) { $validator->errors()->add('switchers-comp-product', __('mdg.please_select_count_compensation_products', ['count' => Yard::instance('shopping')->getNumComp()])); Log::channel(self::LOG_CHANNEL)->info("Incorrect number of compensation products selected", [ 'required' => Yard::instance('shopping')->getNumComp(), 'selected' => count($data['switchers-comp-product']) ]); } if ($validator->errors()->count()) { return back()->withErrors($validator)->withInput(Request::all()); } } // Generate unique identifier do { $identifier = Util::getToken(); } while (ShoppingInstance::where('identifier', $identifier)->count()); // Prepare common data $data['is_from'] = 'user_order'; $data['is_for'] = $for; $data['is_abo'] = $data['is_abo'] ?? 0; $data['abo_interval'] = $data['abo_interval'] ?? 0; $data['shopping_user_id'] = $id; $data['user_price_infos'] = Yard::instance('shopping')->getUserPriceInfos(); $data['mode'] = config('app.mode') === 'test' ? 'test' : 'live'; // Remove unnecessary data unset($data['quantity']); unset($data['_token']); Log::channel(self::LOG_CHANNEL)->info("Processing payment for user #{$user->id}", [ 'for' => $for, 'identifier' => $identifier, 'is_abo' => $data['is_abo'] ]); if ($for === 'ot-customer' || $for === 'abo-ot-customer') { return $this->processCustomerPayment($user, $identifier, $data, $id, $for); } else { return $this->processUserPayment($user, $identifier, $data, $id, $for); } } /** * Process payment for customer orders */ private function processCustomerPayment($user, $identifier, $data, $id, $for) { $shopping_instance = ShoppingInstance::create([ 'identifier' => $identifier, 'user_shop_id' => $user->shop->id, 'payment' => 6, // Berater Shop to Customer Shop 'subdomain' => $user->shop->getSubdomain(), 'country_id' => Yard::instance('shopping')->getShippingCountryId(), 'language' => \App::getLocale(), 'amount' => Yard::instance('shopping')->totalWithShipping(2, '.', ''), 'status' => 0, 'shopping_user_id' => $id, 'shopping_data' => $data, 'back' => url()->previous(), ]); Yard::instance('shopping')->store($identifier); $yard_shopping_items = OrderPaymentService::getRestoredYardShoppingItems($shopping_instance); // Send Mail to Customer try { $this->customPaymentSendMail($user, $identifier, $yard_shopping_items, $data); Log::channel(self::LOG_CHANNEL)->info("Custom payment email sent successfully", [ 'identifier' => $identifier, 'user_id' => $user->id ]); } catch (\Exception $e) { Log::channel(self::LOG_CHANNEL)->error("Failed to send custom payment email: " . $e->getMessage(), [ 'identifier' => $identifier, 'user_id' => $user->id ]); } UserHistory::create([ 'user_id' => $user->id, 'action' => 'user_order_customer', 'status' => 1, 'product_id' => null, 'identifier' => $identifier, 'is_abo' => $data['is_abo'] ]); return redirect(route('user_order_my_custom_payment', ['identifier' => $identifier])); } /** * Process payment for user orders */ private function processUserPayment($user, $identifier, $data, $id, $for) { Shop::deleteCheckoutInstance(); ShoppingInstance::create([ 'identifier' => $identifier, 'user_shop_id' => 1, // is first faker shop for buy intern 'auth_user_id' => Auth::user()->id, 'payment' => 2, // Berater Shop 'subdomain' => url('/'), 'country_id' => Yard::instance('shopping')->getShippingCountryId(), 'language' => \App::getLocale(), 'amount' => Yard::instance('shopping')->totalWithShipping(2, '.', ''), 'status' => 0, 'shopping_user_id' => $id, 'shopping_data' => $data, 'back' => url()->previous(), ]); Yard::instance('shopping')->store($identifier); UserHistory::create([ 'user_id' => $user->id, 'action' => 'user_order_payment', 'status' => 1, 'product_id' => null, 'identifier' => $identifier, 'is_abo' => $data['is_abo'] ]); $path = route('checkout.checkout_card', ['identifier' => $identifier]); return redirect()->secure($path); } /** * Validate the yard before payment */ private function checkSendYardForPayment($data, $id) { $user = User::find(Auth::user()->id); $shopping_user = null; if (strpos($data['shipping_is_for'], 'ot') !== false) { $shopping_user = Shop::checkShoppingUser($id, $user); } $shipping_country_id = Shop::checkShoppingCountry($data['shipping_is_for'], $id); if (!$shipping_country_id) { $identifier = 'error-' . time() . mt_rand(1000000, 9999999); Yard::instance('shopping')->store($identifier); $logData = [ 'user_id' => Auth::user()->id, 'shopping_user_id' => $id, 'yard_identifier' => $identifier ]; MyLog::writeLog('payment', 'error', 'no shipping_country_id found | Yard identifier: ' . $identifier, $data); Log::channel(self::LOG_CHANNEL)->error("Shipping country not found", $logData); throw new \Exception(__('msg.shipping_country_was_not_found')); } // Must be the same shipping country if ($shipping_country_id != Yard::instance('shopping')->getShippingCountryId()) { $identifier = 'error-' . time() . mt_rand(1000000, 9999999); Yard::instance('shopping')->store($identifier); $logData = [ 'user_id' => Auth::user()->id, 'shopping_user_id' => $id, 'yard_identifier' => $identifier, 'expected' => $shipping_country_id, 'actual' => Yard::instance('shopping')->getShippingCountryId() ]; MyLog::writeLog('payment', 'error', 'shipping_country_id is not the same from Yard | Yard identifier: ' . $identifier, $data); Log::channel(self::LOG_CHANNEL)->error("Shipping country mismatch", $logData); throw new \Exception(__('msg.shipping_country_was_not_correctly')); } if ($data['shipping_is_for'] !== 'ot-customer') { if (Yard::instance('shopping')->shipping_free) { $identifier = 'error-' . time() . mt_rand(1000000, 9999999); Yard::instance('shopping')->store($identifier); $logData = [ 'user_id' => Auth::user()->id, 'shopping_user_id' => $id, 'yard_identifier' => $identifier ]; MyLog::writeLog('payment', 'error', 'Yard can by not shipping_free | Yard identifier: ' . $identifier, $data); Log::channel(self::LOG_CHANNEL)->error("Yard cannot be shipping free", $logData); throw new \Exception(__('msg.shopping_cart_was_shipping_free')); } } if ($data['shipping_is_for'] === 'ot-customer') { if (!$user->shop) { $identifier = 'error-' . time() . mt_rand(1000000, 9999999); Yard::instance('shopping')->store($identifier); $logData = [ 'user_id' => Auth::user()->id, 'shopping_user_id' => $id, 'yard_identifier' => $identifier ]; MyLog::writeLog('payment', 'error', 'User has no Shop for an User to Customer order| Yard identifier: ' . $identifier, $data); Log::channel(self::LOG_CHANNEL)->error("User has no shop for customer order", $logData); throw new \Exception(__('msg.shopping_cart_was_not_user_shop')); } } $shipping_price = Shop::getShippingPriceByShippingCountryId($shipping_country_id, Yard::instance('shopping')->weight()); // For other and has weight - check if (strpos($data['shipping_is_for'], 'ot') !== false && $data['shipping_is_for'] !== 'ot-customer' && Yard::instance('shopping')->weight() > 0) { if (!Yard::instance('shopping')->getShippingPrice() || Yard::instance('shopping')->getShippingPrice() == 0) { $identifier = 'error-' . time() . mt_rand(1000000, 9999999); Yard::instance('shopping')->store($identifier); $logData = [ 'user_id' => Auth::user()->id, 'shopping_user_id' => $id, 'yard_identifier' => $identifier, 'weight' => Yard::instance('shopping')->weight() ]; MyLog::writeLog('payment', 'error', 'Yard OT shipping_price is 0 | Yard identifier: ' . $identifier, $data); Log::channel(self::LOG_CHANNEL)->error("Shipping price cannot be zero for order with weight", $logData); throw new \Exception(__('msg.shipping_cost_cannot_be_0')); } if (Yard::instance('shopping')->getShippingPrice() != $shipping_price->price) { $identifier = 'error-' . time() . mt_rand(1000000, 9999999); Yard::instance('shopping')->store($identifier); $logData = [ 'user_id' => Auth::user()->id, 'shopping_user_id' => $id, 'yard_identifier' => $identifier, 'expected' => $shipping_price->price, 'actual' => Yard::instance('shopping')->getShippingPrice() ]; MyLog::writeLog('payment', 'error', 'Yard OT shipping_price is not the same from shipping_price | Yard identifier: ' . $identifier, $data); Log::channel(self::LOG_CHANNEL)->error("Shipping price mismatch", $logData); throw new \Exception(__('msg.shipping_costs_were_not_calculated_correctly')); } } if (($data['shipping_is_for'] == 'me' || $data['shipping_is_for'] == 'abo-me') && Yard::instance('shopping')->weight() > 0) { if (!Yard::instance('shopping')->getShippingPrice() || Yard::instance('shopping')->getShippingPrice() == 0) { $identifier = 'error-' . time() . mt_rand(1000000, 9999999); Yard::instance('shopping')->store($identifier); $logData = [ 'user_id' => Auth::user()->id, 'shopping_user_id' => $id, 'yard_identifier' => $identifier, 'weight' => Yard::instance('shopping')->weight() ]; MyLog::writeLog('payment', 'error', 'Yard ME shipping_price is 0 | Yard identifier: ' . $identifier, $data); Log::channel(self::LOG_CHANNEL)->error("Shipping price cannot be zero for personal order with weight", $logData); throw new \Exception(__('msg.shipping_cost_cannot_be_0')); } if(Shop::isCompProducts($data['shipping_is_for'])){ if (Yard::instance('shopping')->getShippingPrice() != $shipping_price->price_comp) { $identifier = 'error-' . time() . mt_rand(1000000, 9999999); Yard::instance('shopping')->store($identifier); $logData = [ 'user_id' => Auth::user()->id, 'shopping_user_id' => $id, 'yard_identifier' => $identifier, 'expected' => $shipping_price->price_comp, 'actual' => Yard::instance('shopping')->getShippingPrice() ]; MyLog::writeLog('payment', 'error', 'Yard ME shipping_price is not the same from shipping_price with comp products | Yard identifier: ' . $identifier, $data); Log::channel(self::LOG_CHANNEL)->error("Shipping price mismatch for personal order", $logData); throw new \Exception(__('msg.shipping_costs_were_not_calculated_correctly')); } if (Yard::instance('shopping')->getNumComp() != $shipping_price->num_comp) { $identifier = 'error-' . time() . mt_rand(1000000, 9999999); Yard::instance('shopping')->store($identifier); $logData = [ 'user_id' => Auth::user()->id, 'shopping_user_id' => $id, 'yard_identifier' => $identifier, 'expected' => $shipping_price->num_comp, 'actual' => Yard::instance('shopping')->getNumComp() ]; MyLog::writeLog('payment', 'error', 'Yard num_comp is not correct | Yard identifier: ' . $identifier, $data); Log::channel(self::LOG_CHANNEL)->error("Compensation product count mismatch", $logData); throw new \Exception(__('msg.compensation_products_cannot_be_0')); } }else{ if (Yard::instance('shopping')->getShippingPrice() != $shipping_price->price) { $identifier = 'error-' . time() . mt_rand(1000000, 9999999); Yard::instance('shopping')->store($identifier); $logData = [ 'user_id' => Auth::user()->id, 'shopping_user_id' => $id, 'yard_identifier' => $identifier, 'expected' => $shipping_price->price, 'actual' => Yard::instance('shopping')->getShippingPrice() ]; MyLog::writeLog('payment', 'error', 'Yard ME shipping_price is not the same from shipping_price without comp products | Yard identifier: ' . $identifier, $data); Log::channel(self::LOG_CHANNEL)->error("Shipping price mismatch for personal order", $logData); throw new \Exception(__('msg.shipping_costs_were_not_calculated_correctly')); } } } } public function datatable() { $isAbo = Request::get('is_abo'); $shippingIsFor = Request::get('shipping_is_for'); if ($shippingIsFor === 'me' || $shippingIsFor === 'abo-me') { $show_on_ids = $isAbo ? ['12', '13'] : ['2']; $query = Product::with('product_buyings') ->select('products.*') ->where('products.active', true) ->where(function($q) use ($show_on_ids) { foreach ($show_on_ids as $id) { $q->orWhereJsonContains('show_on', $id); } }) ->orderByRaw("CASE WHEN JSON_CONTAINS(show_on, ?, '$') THEN 1 WHEN JSON_CONTAINS(show_on, ?, '$') THEN 2 ELSE 3 END", [$show_on_ids[0], isset($show_on_ids[1]) ? $show_on_ids[1] : $show_on_ids[0]]); } else { $show_on_ids = $isAbo ? ['12', '13'] : ['3']; $query = Product::select('products.*') ->where('active', true) ->where(function($q) use ($show_on_ids) { foreach ($show_on_ids as $id) { $q->orWhereJsonContains('show_on', $id); } }) ->orderByRaw("CASE WHEN JSON_CONTAINS(show_on, ?, '$') THEN 1 WHEN JSON_CONTAINS(show_on, ?, '$') THEN 2 ELSE 3 END", [$show_on_ids[0], isset($show_on_ids[1]) ? $show_on_ids[1] : $show_on_ids[0]]); } Log::channel(self::LOG_CHANNEL)->info("Datatable query executed", [ 'is_abo' => $isAbo, 'shipping_is_for' => $shippingIsFor, 'show_on_ids' => $show_on_ids ]); return \DataTables::eloquent($query) ->addColumn('product', function (Product $product) { $cartItem = Yard::instance('shopping')->getCartItemByProduct($product->id); $qty = isset($cartItem->qty) ? $cartItem->qty : 0; $rowId = isset($cartItem->rowId) ? $cartItem->rowId : ''; return ''.$product->getLang('name').'
'; }) ->addColumn('abo', function (Product $product) { return AboHelper::getAboTypeBadge(AboHelper::getAboShowOn($product)); }) ->addColumn('picture', function (Product $product) { if(count($product->images)){ return ''; } return ""; }) ->addColumn('price_net', function (Product $product) { return ''.$product->getFormattedPriceWith(true, true, Yard::instance('shopping')->getUserCountry()). " €".''.$product->getFormattedPriceCurrencyWith(true, true, Yard::instance('shopping')->getUserCountry()).''; }) ->addColumn('price_gross', function (Product $product) { return ''.$product->getFormattedPriceWith(false, true, Yard::instance('shopping')->getUserCountry()). " €".''.$product->getFormattedPriceCurrencyWith(false, true, Yard::instance('shopping')->getUserCountry()).''; }) ->addColumn('price_vk_gross', function (Product $product) { return ''.$product->getFormattedPriceWith(false, false, Yard::instance('shopping')->getUserCountry()). " €".''.$product->getFormattedPriceCurrencyWith(false, false, Yard::instance('shopping')->getUserCountry()).''; }) ->addColumn('customer_price_net', function (Product $product) { return ''.$product->getFormattedPriceWith(true, false, Yard::instance('shopping')->getUserCountry()). " €".''.$product->getFormattedPriceCurrencyWith(true, false, Yard::instance('shopping')->getUserCountry()).''; }) ->addColumn('customer_price_gross', function (Product $product) { return ''.$product->getFormattedPriceWith(false, false, Yard::instance('shopping')->getUserCountry()). " €".''.$product->getFormattedPriceCurrencyWith(false, false, Yard::instance('shopping')->getUserCountry()).''; }) ->addColumn('my_commission_net', function (Product $product) { return ''.$product->getFormattedPriceWith(true, false, Yard::instance('shopping')->getUserCountry(), true). " €".''.$product->getFormattedPriceCurrencyWith(true, false, Yard::instance('shopping')->getUserCountry(), true).''; }) ->addColumn('action', function (Product $product) { return ''; }) ->filterColumn('product', function($query, $keyword) { if($keyword != ""){ $query->where('name', 'LIKE', '%'.$keyword.'%'); } }) ->orderColumn('name', 'name $1') ->orderColumn('product', 'name $1') ->orderColumn('number', 'number $1') ->orderColumn('points', 'points $1') ->orderColumn('price_net', 'price_net $1') ->orderColumn('price_gross', 'price_gross $1') ->orderColumn('price_vk_gross', 'price $1') ->orderColumn('customer_price_net', 'price $1') ->orderColumn('customer_price_gross', 'price $1') ->orderColumn('my_commission_net', 'price $1') ->orderColumn('contents_total', 'contents_total $1') ->orderColumn('weight', 'weight $1') ->orderColumn('abo', 'show_on $1') ->rawColumns(['add_card', 'price_net', 'price_gross', 'price_vk_gross', 'customer_price_net', 'customer_price_gross', 'my_commission_net', 'product', 'quantity', 'picture', 'abo', 'action']) ->make(true); } /** * Handle AJAX requests for cart operations */ public function performRequest() { if (!Request::ajax()) { Log::channel(self::LOG_CHANNEL)->warning("Non-AJAX request to performRequest method"); return response()->json(['response' => false, 'message' => 'Only AJAX requests are allowed']); } $data = Request::all(); $is_for = isset($data['shipping_is_for']) ? $data['shipping_is_for'] : 'ot-member'; $data['for'] = $is_for; $data['comp_products'] = Shop::getCompProducts($is_for); Log::channel(self::LOG_CHANNEL)->info("Performing cart action", [ 'action' => $data['action'] ?? 'unknown', 'is_for' => $is_for ]); if ($data['action'] === 'updateCart' && isset($data['product_id'])) { return $this->handleUpdateCart($data, $is_for); } if ($data['action'] === 'clearCart') { Yard::instance('shopping')->destroy(); Log::channel(self::LOG_CHANNEL)->info("Cart cleared"); return response()->json(['response' => true, 'data' => Yard::instance('shopping')->count(), 'html_card' => '', 'html_comp' => '']); } if ($data['action'] === 'updateShippingCountry') { return $this->handleUpdateShippingCountry($data, $is_for); } if ($data['action'] === 'updateCompProduct') { return $this->handleUpdateCompProduct($data, $is_for); } Log::channel(self::LOG_CHANNEL)->warning("Unknown action in performRequest", ['action' => $data['action'] ?? 'not set']); return response()->json(['response' => false, 'data' => $data]); } /** * Handle updating cart items */ private function handleUpdateCart($data, $is_for) { $product = Product::find($data['product_id']); if (!$product) { Log::channel(self::LOG_CHANNEL)->warning("Product not found for cart update", ['product_id' => $data['product_id']]); return response()->json(['response' => false, 'message' => 'Product not found']); } $image = ""; if ($product->images->count()) { $image = $product->images->first()->slug; } // Get the cart item if ($is_for === 'ot-customer' || $is_for === 'abo-ot-customer') { $cartItem = Yard::instance('shopping') ->add($product->id, $product->getLang('name'), 1, round($product->getPriceWith(Yard::instance('shopping')->getUserTaxFree(), false, Yard::instance('shopping')->getUserCountry()), 1), false, false, ['image' => $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'), 1, $product->getPriceWith(Yard::instance('shopping')->getUserTaxFree(), true, Yard::instance('shopping')->getUserCountry()), false, false, ['image' => $image, 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points, 'no_commission' => $product->no_commission, 'show_on' => $product->show_on]); } if (Yard::instance('shopping')->getUserTaxFree()) { Yard::setTax($cartItem->rowId, 0); } else { Yard::setTax($cartItem->rowId, $product->getTaxWith(Yard::instance('shopping')->getUserCountry())); } if (isset($data['qty']) && $data['qty'] > 0) { Yard::instance('shopping')->update($cartItem->rowId, $data['qty']); Log::channel(self::LOG_CHANNEL)->info("Cart item updated", [ 'product_id' => $product->id, 'product_name' => $product->getLang('name'), 'qty' => $data['qty'] ]); } else { // If 0 get the item by qty:1 and remove it Yard::instance('shopping')->remove($cartItem->rowId); Log::channel(self::LOG_CHANNEL)->info("Cart item removed", [ 'product_id' => $product->id, 'product_name' => $product->getLang('name') ]); } Yard::instance('shopping')->reCalculateShippingPrice(); $this->checkCompProduct(Yard::instance('shopping')->getNumComp()); $html_card = view("user.order.yard_view_form", $data)->render(); $html_comp = view("user.order.comp_product", $data)->render(); return response()->json(['response' => true, 'data' => $data, 'html_card' => $html_card, 'html_comp' => $html_comp]); } /** * Handle updating shipping country */ private function handleUpdateShippingCountry($data, $is_for) { if (isset($data['shipping_country_id'])) { $shipping_country = ShippingCountry::find($data['shipping_country_id']); if ($shipping_country) { Yard::instance('shopping')->setShippingCountryWithPrice($shipping_country->id, $is_for); $this->checkCompProduct(Yard::instance('shopping')->getNumComp()); Log::channel(self::LOG_CHANNEL)->info("Shipping country updated", [ 'shipping_country_id' => $shipping_country->id, 'shipping_country_name' => $shipping_country->name ?? 'unknown' ]); } else { Log::channel(self::LOG_CHANNEL)->warning("Shipping country not found", [ 'shipping_country_id' => $data['shipping_country_id'] ]); } } $html_card = view("user.order.yard_view_form", $data)->render(); $html_comp = view("user.order.comp_product", $data)->render(); return response()->json(['response' => true, 'data' => $data, 'html_card' => $html_card, 'html_comp' => $html_comp]); } /** * Handle updating compensation products */ private function handleUpdateCompProduct($data, $is_for) { $this->updateCompProduct($data); Yard::instance('shopping')->reCalculateShippingPrice(); Log::channel(self::LOG_CHANNEL)->info("Compensation product updated", [ 'comp_product_id' => $data['comp_product_id'] ?? null, 'comp_num' => $data['comp_num'] ?? null, 'count_comp_products' => $data['count_comp_products'] ?? null ]); $html_card = view("user.order.yard_view_form", $data)->render(); $html_comp = view("user.order.comp_product", $data)->render(); return response()->json(['response' => true, 'data' => $data, 'html_card' => $html_card, 'html_comp' => $html_comp]); } /** * Check and remove compensation products if needed */ private function checkCompProduct($count_comp_products) { foreach (Yard::instance('shopping')->content() as $row) { // If equal or greater, delete due to new shipping costs if ($row->options->comp && $row->options->comp > intval($count_comp_products)) { Yard::instance('shopping')->remove($row->rowId); Log::channel(self::LOG_CHANNEL)->info("Compensation product removed due to count change", [ 'product_id' => $row->id, 'product_name' => $row->name, 'comp_value' => $row->options->comp, 'required_comp' => $count_comp_products ]); } } } /** * Update compensation products */ private function updateCompProduct($data) { // Clear old foreach (Yard::instance('shopping')->content() as $row) { // If count_comp_products is smaller, the product was removed due to quantity // if comp_num equals the comp product, the product was removed due to new shipping costs //count_comp_products wie viele comp products werden gebraucht //comp_num welches comp product wird hinzugefügt if ($row->options->comp && ($row->options->comp == intval($data['comp_num']) || $row->options->comp > intval($data['count_comp_products']))) { Yard::instance('shopping')->remove($row->rowId); Log::channel(self::LOG_CHANNEL)->info("Compensation product removed during update", [ 'product_id' => $row->id, 'product_name' => $row->name, 'comp_value' => $row->options->comp, 'comp_num' => $data['comp_num'], 'count_comp_products' => $data['count_comp_products'] ]); } } if (isset($data['comp_product_id'])) { $product = Product::find($data['comp_product_id']); if ($product) { $image = ""; if ($product->images->count()) { $image = $product->images->first()->slug; } $cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, 0, false, false, [ 'image' => $image, 'slug' => $product->slug, 'weight' => 0, 'points' => 0, 'comp' => intval($data['comp_num']), 'product_id' => $product->id ] ); Yard::setTax($cartItem->rowId, 0); Log::channel(self::LOG_CHANNEL)->info("Compensation product added", [ 'product_id' => $product->id, 'product_name' => $product->getLang('name'), 'comp_num' => $data['comp_num'] ]); } else { Log::channel(self::LOG_CHANNEL)->warning("Compensation product not found", [ 'comp_product_id' => $data['comp_product_id'] ]); } } } /** * Display custom payment page */ public function customPayment($identifier) { try { $data = OrderPaymentService::getCustomPayment($identifier); Log::channel(self::LOG_CHANNEL)->info("Custom payment page accessed", ['identifier' => $identifier]); return view('user.order.payment.custom_payment', $data); } catch (\Exception $e) { Log::channel(self::LOG_CHANNEL)->error("Error accessing custom payment: " . $e->getMessage(), ['identifier' => $identifier]); abort(404, 'Custom payment not found'); } } /** * Send custom payment email */ private function customPaymentSendMail($user, $identifier, $yard_shopping_items, $data) { $bcc = []; $shopping_instance = ShoppingInstance::where('identifier', $identifier)->first(); if (!$shopping_instance) { Log::channel(self::LOG_CHANNEL)->error("Shopping instance not found for email", ['identifier' => $identifier]); throw new \Exception(__('msg.shopping_instance_not_found')); } $shopping_user = $data['shopping_user_id'] ? ShoppingUser::find($data['shopping_user_id']) : null; if (!$shopping_user) { Log::channel(self::LOG_CHANNEL)->error("Shopping user not found for email", ['shopping_user_id' => $data['shopping_user_id']]); throw new \Exception(__('msg.shopping_user_not_found')); } $route = route('checkout.checkout_card', ['identifier' => $identifier]); $billing_email = $shopping_user->billing_email; if (!$billing_email) { $billing_email = $data['mode'] === 'test' ? config('app.checkout_test_mail') : config('app.checkout_mail'); } $bcc[] = $data['mode'] === 'test' ? config('app.checkout_test_mail') : config('app.checkout_mail'); $bcc[] = $shopping_user->member ? $shopping_user->member->email : $user->email; Log::channel(self::LOG_CHANNEL)->info("Sending custom payment email", [ 'to' => $billing_email, 'bcc' => $bcc, 'identifier' => $identifier ]); Mail::to($billing_email) ->bcc($bcc) ->locale(\App::getLocale()) ->send(new MailCustomPaymet($route, $shopping_user, $shopping_instance, $yard_shopping_items, $data['mode'])); } }