public function actionDonhang()
 {
     $criteria = new CDbCriteria();
     $iduser = Yii::app()->user->id;
     // $criteria->addCondition('id_user',$iduser);
     $criteria->compare('id_user', $iduser);
     $criteria->compare('status', 1);
     $list = Giohang::model()->findAll($criteria);
     $sum = 0;
     // luu vao don hang
     // tinh tong tien trong don hang
     foreach ($list as $data) {
         $sum = $sum + $data->cost * $data->number;
     }
     $donhang = new Donhang();
     $donhang->tongtien = $sum;
     $donhang->user_id = $iduser;
     $donhang->thoigian = time();
     $donhang->save();
     // luu vao chi tiet don hang
     foreach ($list as $data) {
         $model = new Ctdhang();
         $model->id_book = $data->id_book;
         $model->soluong = $data->number;
         $model->gia = $data->cost;
         $model->id_donhang = $donhang->id;
         $model->save();
         $data->delete();
     }
     $this->render('view', array('model' => $this->loadModel(3)));
 }
 public function getChuyenHang($id)
 {
     $cthd = Ban::select("ban.id_sp", "ban.soluong", "sanpham.tensp")->join("sanpham", "sanpham.id", '=', "ban.id_sp")->where("id_dh", '=', $id)->get();
     $error = array();
     foreach ($cthd as $v) {
         $kho = DB::table('ton_kho')->where("id", '=', $v->id_sp)->where("soluong", ">=", $v->soluong)->count();
         if ($kho == 0) {
             $error[$v->id_sp] = "Sản phẩm \"{$v->tensp}\" có ID= {$v->id_sp} đã hết hàng hoặc không đủ số lượng.";
         }
     }
     if (empty($error)) {
         date_default_timezone_set("Asia/Bangkok");
         $date = new DateTime();
         $dh = Donhang::find($id);
         $dh->xem = 1;
         $dh->ngaygiao = $date;
         $dh->id_trangthai = 3;
         $dh->save();
         return Redirect::to("dat_hang/donhang-dachuyen");
     } else {
         return Redirect::to("dat_hang/xemdon-dathang")->with("error", $error);
     }
 }
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return Donhang the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = Donhang::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 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);
     }
 }