App\Http\Controllers\OrdersController::closeOrder PHP Method

closeOrder() public method

Changes the status of an order to close, so the user can now check if received.
public closeOrder ( $order_id )
    public function closeOrder($order_id)
    {
        $user = \Auth::user();
        $order = Order::where('id', $order_id)->where('user_id', $user->id)->ofStatus('sent')->select('id', 'user_id', 'status', 'end_date', 'seller_id')->first();
        //checks if the orders is own by the user and if it is on open status
        if ($order) {
            //Mails and notifications are now sent in the save method for the order
            $order->status = 'closed';
            $order->end_date = DB::raw('NOW()');
            // $order->end_date = Carbon::now(); Esto lo cambie porque no me parece guardar la fecha de php en la bd...
            $order->save();
            Session::push('message', trans('store.orders_index.order_received') . ' (#' . $order->id . ')');
            Notice::create(['user_id' => $order->seller_id, 'sender_id' => $order->user_id, 'action_type_id' => 10, 'source_id' => $order->id, 'status' => 'new']);
            if (config('app.offering_user_points')) {
                //The order total points are passed to the seller
                $seller = User::findOrFail($order->seller_id);
                if ($seller) {
                    $order_content = OrderDetail::where('order_id', $order->id)->get();
                    $total_points = 0;
                    foreach ($order_content as $order_detail) {
                        $total_points += $order_detail->quantity * $order_detail->price;
                        $order_detail->status = 0;
                        $order_detail->delivery_date = DB::raw('NOW()');
                        $order_detail->save();
                        if ($order_detail->product->type != 'item') {
                            switch ($order_detail->product->type) {
                                case 'key':
                                    $virtualProductsId = VirtualProductOrder::select('virtual_product_id')->where('order_id', $order->id)->get()->toArray();
                                    VirtualProduct::where('product_id', $order_detail->product_id)->whereIn('id', $virtualProductsId)->update(['status' => 'closed']);
                                    break;
                            }
                        }
                    }
                    $seller->modifyPoints($total_points, 8, $order->id);
                }
            }
            return redirect(route('orders.show_orders'));
        } else {
            return redirect(route('orders.show_orders'));
        }
    }