/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { $this->vanchuyen->find($id)->delete(); return Redirect::route('vanchuyens.index'); }
public function postDatHang() { $data = Input::all(); $giohang = Session::get('giohang'); $rules = array(); //kiem tra hang ton kho $error = array(); foreach ($giohang as $v) { $kho = DB::table('ton_kho')->where("id", '=', $v['id_sp'])->where("soluong", ">=", $v['quantity'])->count(); if ($kho == 0) { $error[$v['id_sp']] = "Sản phẩm {$v['tensp']} đã hết hàng."; } } if (empty($error)) { foreach ($data as $key => $value) { if ($key != "id_hinhthuc" && $key != "ghichu") { $rules[$key] = "required|numeric"; } } $rules['id_hinhthuc'] = "required|numeric"; $valdator = Validator::make($data, $rules); if (!$valdator->fails() && !empty($data)) { // dung thi them hoa don // lay tong gia $giavanchuyen = Vanchuyen::find(Input::get('id_hinhthuc')); $total = 0; foreach ($giohang as $sp) { $total += $sp['gia'] * $sp['quantity']; } $total += $giavanchuyen->gia; date_default_timezone_set("Asia/Bangkok"); $date = new DateTime(); $donhang = new Donhang(); $donhang->ngaymua = $date; $donhang->noigiaohang = Session::get('diachi'); $donhang->ghichu = Input::get('ghichu'); $donhang->id_hinhthuc = Input::get('id_hinhthuc'); $donhang->tonggia = $total; $donhang->id_user = Session::get('id_taikhoan'); $donhang->save(); //them chi tiet don hang foreach ($giohang as $v) { $sp = new Ban(); $sp->id_sp = $v['id_sp']; $sp->id_dh = $donhang->id; $sp->soluong = $v['quantity']; $sp->gia = $v['gia']; $sp->save(); } Session::forget('giohang'); return Redirect::to("/")->with("successcart", "Đặt hàng thành công"); } else { return Redirect::to("/")->with("errorcart", "Đặt hàng thất bại. Xin bạn vui lòng thử lại sau"); } } else { //khi trong kho khong co hàng return Redirect::to("users/xem-giohang")->with("error", $error); // print_r($error); } }