예제 #1
0
 public function show($id)
 {
     if (Auth::user()->can('read-ticket')) {
         $data['ticket'] = self::API()->find(['id' => $id]);
         if ($data['ticket']) {
             $data['title'] = "Ticket #" . $data['ticket']->id;
             $data['menu_actions'] = [Form::editItem(route('tickets.edit', $id), "Edit This Ticket", Auth::user()->can('update-ticket'))];
             $data['ticket']['posts'] = PostsController::API()->all(['where' => ['ticket_id|=|' . $id], "order" => ['posts.created_at|ASC'], "paginate" => "false"]);
             $data['ticket']['history'] = TicketHistory::where('ticket_id', '=', $id)->orderBy('created_at')->get();
             $data['statuses'] = Status::where('id', TICKET_WFF_STATUS_ID)->orWhere('id', TICKET_SOLVED_STATUS_ID)->get();
             $data['draft_post'] = Post::where("ticket_id", $id)->where("status_id", POST_DRAFT_STATUS_ID)->where("author_id", Auth::user()->active_contact->id)->first();
             $data['important_post'] = null;
             if (in_array($data['ticket']->status_id, [TICKET_SOLVED_STATUS_ID, TICKET_WFF_STATUS_ID])) {
                 foreach ($data['ticket']['posts']->reverse() as $post) {
                     if ($post->ticket_status_id != $data['ticket']->status_id) {
                         break;
                     } else {
                         $data['important_post'] = $post;
                     }
                 }
             }
             $links = [];
             $temp = TicketLink::where("ticket_id", "=", $id)->get();
             foreach ($temp as $elem) {
                 $links[] = $elem->linked_ticket_id;
             }
             $data['ticket']['links'] = self::API()->all(['where' => ['tickets.id|IN|' . implode(":", $links)]]);
             $linked_to = [];
             $temp = TicketLink::where("linked_ticket_id", "=", $id)->get();
             foreach ($temp as $elem) {
                 $linked_to[] = $elem->ticket_id;
             }
             $data['ticket']['linked_to'] = self::API()->all(['where' => ['tickets.id|IN|' . implode(":", $linked_to)]]);
             if (isset($data['important_post'])) {
                 switch ($data['ticket']->status_id) {
                     case TICKET_WFF_STATUS_ID:
                         $data['important_post']->alert_type = "danger";
                         break;
                     case TICKET_SOLVED_STATUS_ID:
                         $data['important_post']->alert_type = "success";
                         break;
                 }
             }
             return view('tickets/show', $data);
         } else {
             return redirect()->back()->withErrors(['404 The following Ticket coudn\'t be found']);
         }
     } else {
         return redirect()->back()->withErrors(['Access denied to tickets show page']);
     }
 }
예제 #2
0
 /**
  * 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', 'Произошла ошибка в работе сайта. Мы уже исправляем эту проблему. Попробуйте через некоторое время.');
     }
 }