public function changeStatus(ChangeStatusOrderRequest $request) { $idOrder = $request->input('id'); //get current status $currentStatus = Order::find($idOrder)->status; $newStatus = $request->input('status'); if ($currentStatus == $this::CANCEL) { return redirect_errors('Order canceled cannot changer status!'); } if ($currentStatus == $this::PENDING && $newStatus > $this::DELIVERING) { return redirect_errors('Order is Pending can only changer Cancel or Delivering status!'); } if ($currentStatus == $this::DELIVERING && $newStatus > $this::DELIVERED && $newStatus < $this::DELIVERING && $newStatus != $this::CANCEL) { return redirect_errors('Order is Delivering can only changer Cancel or Delivered status!'); } if ($currentStatus == $this::DELIVERED && $newStatus < $this::DELIVERED && $newStatus != $this::CANCEL) { return redirect_errors('Order is Delivered can only changer Cancel or Charged status!'); } if ($currentStatus == $this::CHARGED) { return redirect_errors('Order charged cannot changer status!'); } $model = new Order(); DB::beginTransaction(); try { $order = $model->find(intval($idOrder)); $order->status = intval($newStatus); $order->save(); //if cancel then qty return if ($newStatus == $this::CANCEL) { $model = new Order(); $order = $model->viewOrder($idOrder); $details = $order['order']['detail']; foreach ($details as $detail) { $qtyReturn = $detail['quantity']; $currentQty = Cd::find($detail['product_id'])->quantity; $currentBuyTime = Cd::find($detail['product_id'])->buy_time; Cd::find($detail['product_id'])->update(['quantity' => $currentQty + $qtyReturn, 'buy_time' => $currentBuyTime + $qtyReturn]); } // $customer=User::find('user_id'); // if(is_null($customer)){ // // } // Mail::send('auth.message_order', ['name' => 'you'], // function ($message) use ($data) { // $message // ->to($data['email'], $data['name']) // ->from('*****@*****.**') // ->subject('Your order canceled!'); // }); } DB::commit(); return redirect()->back()->with('success', 'Updated status'); } catch (\Exception $e) { DB::rollback(); return redirect_errors('Updated Fails!'); } }
public function changeQuantityItem(ChangeQtyItemRequest $request) { $dataRequest = $request->all(); //check exist if (is_object(Cart::get(Input::get('idrow')))) { if (Cd::find($dataRequest['id'])->quantity - $dataRequest['qty'] >= 0) { Cart::update($dataRequest['idrow'], ['qty' => $dataRequest['qty']]); return redirect()->back()->with('success', 'Updated item!'); } else { return redirect_errors("Sorry we aren't Cd for You"); } } else { return redirect_errors('You haven"t this Item!'); } }
public function order() { //get Session option $option = Session::get('option'); //get Cart $cart = Cart::content(); //get type order $type = $option['type']; //payment method $payMethod = Session::get('pay_med'); if ($option['type'] == 'logged') { $order = new Order(); DB::beginTransaction(); try { $idUser = array_get(session('user'), 'id'); $order->user_id = $idUser; $order->delivery_detail_id = 0; $order->comment = $payMethod['comment']; $order->save(); $idOrder = Order::where('user_id', $idUser)->orderBy('id', 'desc')->first()->id; //add order detail and sub amount of product foreach ($cart as $item) { $root_price = Cd::find($item['id'])->root_price; $modelOrderDetail = new OrderDetail(); $modelOrderDetail->order_id = $idOrder; $modelOrderDetail->product_id = $item['id']; $modelOrderDetail->quantity = $item['qty']; $modelOrderDetail->root_price = $root_price; $modelOrderDetail->price = $item->price; $modelOrderDetail->save(); //sub qty product $cd = Cd::find($item['id']); if ($cd->quantity - $item['qty'] <= 0) { throw new QtyCDException(); } $cd->quantity = $cd->quantity - $item['qty']; $cd->buy_time = $cd->buy_time + $item['qty']; $cd->save(); } DB::commit(); $this->clear(); return redirect_success('FrontendController@index', 'Ordered, Check your email to check detail'); } catch (QtyCDException $e) { DB::rollback(); return redirect_errors($e->notify()); } catch (\Exception $e) { DB::rollback(); return redirect_errors("Sorry cannot to this deal!"); } } else { $deliveryDetail = Session::get('shi_add'); $order = new Order(); $modelDeliverDetail = new DeliveryDetail(); $modelDeliverDetail = autoAssignDataToProperty($modelDeliverDetail, $deliveryDetail); // dd($modelDeliverDetail); DB::beginTransaction(); try { $orderHuman = $modelDeliverDetail; $modelDeliverDetail->save(); $newDeliverDetail = new DeliveryDetail(); $delivery = $newDeliverDetail->getDeliveryDetail($deliveryDetail); $order->user_id = 0; $order->delivery_detail_id = $delivery->id; $order->comment = $payMethod['comment']; $order->save(); $idOrder = Order::where('delivery_detail_id', $delivery->id)->first()->id; //add order detail and sub amount of product foreach ($cart as $item) { $modelOrderDetail = new OrderDetail(); $modelOrderDetail->order_id = $idOrder; $modelOrderDetail->product_id = $item['id']; $modelOrderDetail->quantity = $item['qty']; $modelOrderDetail->price = $item['price']; $modelOrderDetail->save(); // $cd = Cd::find($item['id']); if ($cd->quantity - $item['qty'] <= 0) { throw new QtyCDException(); } else { $cd->quantity = $cd->quantity - $item['qty']; } } DB::commit(); $this->clear(); //send // Mail::send('auth.mail_welcome', ['last_name' => $orderHuman->lastname, 'key' => $key_active, 'password' => $pass], function ($message) use ($data) { // $message // ->to($data['email'], $data['name']) // ->from('*****@*****.**') // ->subject('Thank you for your buying!'); // }); return redirect_success('FrontendController@index', 'Ordered, Check your email to check detail'); } catch (QtyCDException $e) { DB::rollback(); return redirect_errors($e->notify()); } catch (\Exception $e) { DB::rollback(); return redirect_errors("Sorry cannot to this deal!"); } } }
public function destroy(DestroyProductRequest $request) { $id = Input::get('id'); //check delete if (Cd::find($id)->update(['status' => Cd::IN_ACTIVE])) { return redirect()->back()->with('success', 'Deleted'); } else { return redirect_errors('Have error,Cannot delete'); } //end check delete }