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))); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Donhang(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Donhang'])) { $model->attributes = $_POST['Donhang']; if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('create', array('model' => $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); } }