Esempio n. 1
0
 /**
  * Execute the command.
  *
  * @return void
  */
 public function handle()
 {
     $selfFirmUser = User::with('firm')->where('role_id', User::ADMIN)->first();
     $clientCompany = User::with('firm')->where('id', $this->order->user_id)->first();
     $products = $this->order->products_in_order;
     $firm = $clientCompany->firm;
     $productsArr = [];
     foreach ($products as $product) {
         $productsArr[] = ['product_name' => $product->product_name, 'product_amount' => $product->product_amount, 'product_price' => $product->product_price];
     }
     $date = DateTime::createFromFormat('Y-m-d H:i:s', $this->order->created_at);
     $date = strtotime($date->format('d F Y'));
     $pdf = App::make('dompdf.wrapper');
     $viewType = null;
     if ($this->isTorg) {
         $viewType = 'documents.torg_12';
     } else {
         $viewType = 'documents.schet_factura';
     }
     $pdf->loadView($viewType, ['orderNumber' => $this->order->id, 'orderDate' => date('d.m.Y', $date), 'firm' => $firm, 'selfFirm' => $selfFirmUser->firm, 'products' => $productsArr]);
     //$pdf->setOrientation('landscape');
     $pdf->setPaper('A4', 'landscape');
     $documents = Config::get('documents');
     $whereAreClientDocuments = $documents['documents_folder'];
     //client_{id}
     if (!Storage::disk('local')->exists($whereAreClientDocuments . DIRECTORY_SEPARATOR . 'client_' . $this->order->user_id)) {
         Storage::makeDirectory($whereAreClientDocuments . DIRECTORY_SEPARATOR . 'client_' . $this->order->user_id);
     }
     //paymentDocs
     if (!Storage::disk('local')->exists($whereAreClientDocuments . DIRECTORY_SEPARATOR . 'client_' . $this->order->user_id . DIRECTORY_SEPARATOR . 'paymentDocs')) {
         Storage::makeDirectory($whereAreClientDocuments . DIRECTORY_SEPARATOR . 'client_' . $this->order->user_id . DIRECTORY_SEPARATOR . 'paymentDocs');
     }
     $clientFolder = storage_path() . DIRECTORY_SEPARATOR . 'app' . $whereAreClientDocuments . DIRECTORY_SEPARATOR . 'client_' . $this->order->user_id . DIRECTORY_SEPARATOR . 'paymentDocs';
     //(torg12/schetfactura)_{orderID}_{depoName}_date_{currentDate}
     $fileNameTemplate = $documents['client_invoice_template'];
     $fileNameTemplate = Utils::mb_str_replace('{docType}', Order::getDocTypeName($this->isTorg ? Order::AUCTION_12_TYPE : Order::INVOICE_ACCT_TYPE), $fileNameTemplate);
     $fileNameTemplate = Utils::mb_str_replace('{orderID}', $this->order->id, $fileNameTemplate);
     $depo = Stantion::find($this->order->products_in_order[0]->stantion_id);
     $depoName = Utils::mb_str_replace(' ', '', $depo->stantion_name);
     $depoName = Utils::translit($depoName);
     $fileNameTemplate = Utils::mb_str_replace('{depoName}', $depoName, $fileNameTemplate);
     $fileNameTemplate = Utils::mb_str_replace('{currentDate}', time(), $fileNameTemplate);
     $pdf->save($clientFolder . DIRECTORY_SEPARATOR . $fileNameTemplate);
     $docs = new Document();
     $docs->type = $this->isTorg ? Order::AUCTION_12_TYPE : Order::INVOICE_ACCT_TYPE;
     $docs->user_id = $this->order->user_id;
     $docs->order_id = $this->order->id;
     $docs->file_name = $clientFolder . DIRECTORY_SEPARATOR . $fileNameTemplate;
     $docs->save();
     Bus::dispatch(new SendEmailWithPaymentDocs($docs->file_name, $this->isTorg, $this->order));
 }
Esempio n. 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', 'Произошла ошибка в работе сайта. Мы уже исправляем эту проблему. Попробуйте через некоторое время.');
     }
 }