/**
  * Store a newly created resource in storage.
  *
  * @param  Request  $request
  * @return Response
  */
 public function store(Request $request)
 {
     $id = $request->input('id');
     if ($id == null) {
         return Response::json(array('result' => false, 'message' => 'Product code is valide'));
     }
     //GET PRODUCT
     $product_vol = Products_vol::where('code', $id)->get()->first();
     if ($product_vol == null) {
         return Response::json(array('result' => false, 'message' => 'Product can not defind'));
     }
     $product = Products::where('id', $product_vol->products_id)->first();
     $p_name = $product->name;
     //var_dump($p_name);
     $product_vol->name = $p_name . ' ' . $product_vol->name;
     //var_dump($product_vol->name);
     //ADD TO CART
     $cart = Cart::instance('cartoon');
     $cart->add(array('id' => $product_vol->code, 'name' => $product_vol->name, 'qty' => 1, 'price' => $product_vol->price));
     return Response::json(array('result' => true, 'product' => $product_vol->name, 'cart' => $cart->content(), 'total' => $cart->total(), 'count' => $cart->count()));
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  Request  $request
  * @return Response
  */
 public function update(Request $request)
 {
     $validator = Validator::make($request->all(), ['id' => 'required', 'publisher_id' => 'required', 'products_id' => 'required', 'point' => 'required', 'price' => 'required', 'status' => 'required', 'name' => 'required', 'image' => 'required']);
     if ($validator->fails()) {
         return Response::json(array('result' => false, 'data' => 'Request is valid.'));
     }
     $vol = Products_vol::where('id', $request->input('id'))->get()->first();
     if ($vol == null) {
         return Response::json(array('result' => false, 'data' => 'Object can not defind.'));
     }
     $vol->name = $request->input('name');
     $vol->products_id = $request->input('products_id');
     $vol->publisher_id = $request->input('publisher_id');
     $vol->point = $request->input('point');
     $vol->price = $request->input('price');
     $vol->status = $request->input('status');
     $vol->updated_at = Carbon::now('Asia/Bangkok');
     $vol->image = $request->input('image');
     $vol->tag = $request->input('tag');
     $vol->resume = $request->input('resume');
     $vol->keyword = $request->input('keyword');
     $result = $vol->save();
     if ($result) {
         $data = Products_vol::where('id', $vol->id)->get()->first();
         $data->publisher = \DB::table('publisher')->where('id', $data->publisher_id)->first();
         return Response::json(array('result' => true, 'data' => $data, 'now' => Carbon::now('Asia/Bangkok')));
     }
 }
 public function confirm(Request $request)
 {
     $now = new DateTime();
     $rules = array('name' => 'required', 'address' => 'required', 'trans' => 'required', 'phone' => 'required');
     $validator = \Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         return Response::json(array('result' => false, 'data' => 'กลับไปที่หน้าแรก โปรดตรวจสอบข้อมูลใหม่อีกครั้ง', 'message' => $validator->messages()->all()));
     }
     //$validator->errors()->all()
     $order = new Orders();
     $orders_number = $now->format("YmdHis") . '-' . \Auth::user()->id;
     $cart = Cart::instance('cartoon');
     if ($cart->count() <= 0) {
         if ($validator->fails()) {
             return Response::json(array('result' => false, 'data' => 'กลับไปที่หน้าแรก โปรดตรวจสอบข้อมูลใหม่อีกครั้ง'));
         }
     }
     DB::beginTransaction();
     try {
         $order->orders_number = $orders_number;
         $order->members_id = \Auth::user()->id;
         $order->members_name = $request->input('name');
         $order->address = $request->input('address');
         $order->phone = $request->input('phone');
         $order->ems = $request->input('trans');
         $order->total = $cart->total();
         $order->total_grand = $cart->total() + $request->input('trans');
         $order->created_at = Carbon::now('Asia/Bangkok');
         $order->updated_at = Carbon::now('Asia/Bangkok');
         $order->status = 1;
         $order->save();
         DB::commit();
     } catch (\Exception $e) {
         DB::rollBack();
         return Response::json(array('result' => false, 'data' => 'ยืนยันผิดพลาด โปรดตรวจสอบข้อมูลใหม่อีกครั้ง', 'message' => $e));
     }
     DB::beginTransaction();
     try {
         $now = Carbon::now('Asia/Bangkok');
         foreach ($cart->content() as $key => $item) {
             $pro = Products_vol::where('code', $item->id)->get()->first();
             $orders_detail = new Orders_detail();
             $orders_detail->orders_number = $order->orders_number;
             $orders_detail->product_vol_code = $item->id;
             $orders_detail->product_vol_name = $pro->name;
             $orders_detail->product_vol_qty = $item->qty;
             $orders_detail->product_vol_per = $pro->price;
             $orders_detail->created_at = $now;
             $orders_detail->updated_at = $now;
             $orders_detail->product_vol_price = $pro->price * $item->qty;
             $orders_detail->save();
             DB::commit();
         }
     } catch (\Exception $e) {
         DB::rollBack();
         return Response::json(array('result' => false, 'data' => 'ยืนยันผิดพลาด โปรดตรวจสอบข้อมูลใหม่อีกครั้ง', 'message' => $e));
     }
     $cart->destroy();
     $this->sendmail($order);
     //save notify
     $notify = new Notifications();
     $notify->members_id = \Auth::user()->id;
     $notify->type = "CREATE_ORDER";
     $notify->status = 0;
     $notify->created_at = $now;
     $notify->updated_at = $now;
     $notify->order_id = $order->id;
     $notify->message = "You have a new order !";
     $notify->save();
     return Response::json(array('result' => true, 'data' => $order));
 }
 public function init(Request $request)
 {
     $rules = array('page' => 'required', 'pageSize' => 'required');
     $validator = \Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         return \Response::json(array('result' => false, 'msg' => 'Please check your input again.'));
     }
     $pageSize = $request->input('pageSize');
     $page = $request->input('page');
     $skip = ($page - 1) * $pageSize;
     //$publisher_id = $request->input('publisher_id');
     //$products_id = $request->input('products_id');
     $keyword = $request->input('keyword');
     $productsid = $request->input('productsid');
     $count = 0;
     if ($keyword == null && $productsid == null) {
         $vol = Products_vol::orderBy('created_at', 'desc')->skip($skip)->take($pageSize)->get();
         $count = Products_vol::count();
         foreach ($vol as $key => $value) {
             # code...
             $value->product = Products::where('id', $value->products_id)->first();
         }
         return \Response::json(array('result' => true, 'data' => $vol, 'total' => $count, 'mode' => 'init'));
     } else {
         //$products = \DB::table('v_search_product')->where('name' , 'LIKE' , '%'.$keyword.'%')->first();
         $products_vol = Products_vol::where('products_id', $productsid)->skip($skip)->take($pageSize)->get();
         $count = Products_vol::where('products_id', $productsid)->count();
         foreach ($products_vol as $key => $value) {
             # code...
             $value->product = Products::where('id', $value->products_id)->first();
         }
         return \Response::json(array('result' => true, 'data' => $products_vol, 'total' => $count, 'productsid' => $productsid));
     }
     return \Response::json(array('result' => false, 'data' => $vol, 'total' => $count, 'mode' => 'false'));
 }