/** * cancels one of the user orders. * * @return Response */ public function cancel($orderId, Request $request) { $user = \Auth::user(); /** * $route description * destination route after process the action * @var string */ $route = 'orders.pendingOrders'; /** * $message * provide control on the processed message to users * @var array */ $message['msg'] = trans('store.cancelled_order'); $message['class'] = 'alert alert-success'; /** * $order * retrieve the evaluated order information. So what, we can update * its status and return its items stock * @var [type] */ $order = Order::where('id', $orderId)->with('details')->where('user_id', $user->id)->ofStatus('open')->select('id', 'status', 'user_id')->first(); //checking if the user is a seller if (!$order) { $route = '/user/orders'; $order = Order::where('id', $orderId)->with('details')->where('seller_id', $user->id)->ofStatus('open')->select('id', 'status', 'user_id')->first(); } //checking if it is our order if ($order) { $order->status = 'cancelled'; $existsVirtual = false; //Returning the stock to products foreach ($order->details as $detail) { $product = Product::find($detail->product_id); $product->stock += $detail->quantity; $product->save(); if ($product->type != 'item') { $existsVirtual = true; } } if ($existsVirtual) { $virtualProductsId = VirtualProductOrder::select('virtual_product_id')->where('order_id', $order->id)->get()->toArray(); VirtualProduct::whereIn('id', $virtualProductsId)->update(['status' => 'open']); VirtualProductOrder::where('order_id', $order->id)->delete(); } $order->save(); } else { $message['msg'] = trans('store.no_order_message'); $message['class'] = 'alert alert-danger'; } Session::push('message', $message['msg']); Session::push('messageClass', $message['class']); Session::save(); return redirect($route); }