/** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request) { $validationRules = ['oferta' => 'accepted', 'userID' => 'integer|required|in:' . Auth::user()->id]; $v = Validator::make($request->all(), $validationRules); if ($v->fails()) { return redirect()->route('confirmOrder', ['user_id' => Auth::user()->id])->withErrors($v->errors()); // $newRequest = Request::create('confirmOrder/'.Auth::user()->id, 'POST', [], [], [], [],['blat'=>$v->errors()]); // return Route::dispatch($newRequest)->getContent(); } $userID = $request->userID; if (Auth::user()->id == (int) $userID) { $productsByDepoArr = []; $newProductsByDepoArr = []; $orderNumbers = []; DB::transaction(function () use($userID, &$productsByDepoArr, &$orderNumbers, &$newProductsByDepoArr) { $products = ProductCart::with('product.condition', 'price.stantion')->where('user_id', $userID)->get(); foreach ($products as $productCart) { $productsByDepoArr[$productCart->price->stantion[0]->id][] = [$productCart->product->name . '( состояние - ' . $productCart->product->condition->condition . ')', $productCart->product_count, $productCart->price->price, $productCart->price->id, $productCart->product->id, $productCart->price->stantion[0]->stantion_name, $productCart->price->nds]; $productCart->price->amount -= $productCart->product_count; $productCart->price->save(); } $userCompany = User::with('firm')->where('id', $userID)->first(); $status = Status::where('is_first', Order::IS_FIRST)->first(); foreach ($productsByDepoArr as $depoID => $productsArr) { $order = new Order(); $order->status_id = $status->id; $order->user_id = $userID; $order->firm_id = $userCompany->firm->id; $order->email = Auth::user()->email; $order->save(); $orderNumbers[] = $order->id; foreach ($productsArr as $product) { $productsInOrder = new ProductsInOrder(); $productsInOrder->order_id = $order->id; $productsInOrder->product_name = $product[0]; $productsInOrder->product_price = $product[2]; $productsInOrder->product_amount = $product[1]; $productsInOrder->stantion_id = $depoID; $productsInOrder->price_id = $product[3]; $productsInOrder->product_id = $product[4]; $productsInOrder->stantion_name = $product[5]; $productsInOrder->nds = $product[6]; $productsInOrder->save(); } $newProductsByDepoArr[$order->id] = $productsArr; //Запускаем команду на формирование счета Bus::dispatch(new CreateInvoice($order, Stantion::find($depoID))); } ProductCart::where('user_id', $userID)->delete(); }); $files = Document::where('user_id', Auth::user()->id)->where(function ($query) use($orderNumbers) { foreach ($orderNumbers as $number) { $query->orWhere('file_name', 'like', '%' . Order::getDocTypeName(Order::INVOICE_TYPE) . '_' . $number . '_%'); } })->get(); $fileNames = []; foreach ($files as $file) { $fileNames[] = $file->file_name; } $messageParams = []; $messageParams['productByDepoWithOrderIdAsKey'] = $newProductsByDepoArr; // //Запускаем команду на отправку email Bus::dispatch(new SendEmailWithInvoices($messageParams, $fileNames)); // Bus::dispatch(new SendWithTanksForOrder($messageParams)); return view('orders.success', ['p' => 'purchases', 'ordersAmount' => count($productsByDepoArr)]); } else { return redirect('fatal_error')->with('alert-danger', 'Произошла ошибка в работе сайта. Мы уже исправляем эту проблему. Попробуйте через некоторое время.'); } }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { // $price = null; // DB::transaction(function() use($id, &$price) { // $productCart = ProductCart::find($id); // $price = Price::with('product')->where('id',$productCart->price_id)->first(); // $price->amount += $productCart->product_count; // $price->save(); // $productCart->delete(); // }); ProductCart::destroy($id); // return back()->with('alert-success','Товар '.$price->product[0]->name.' удален'); return back()->with('alert-success', 'Товар удален'); }