/** * Attach behavior to model * @param $owner */ public function attach($owner) { if (!$owner->isNewRecord && Yii::app()->controller instanceof Controller) { if (DiscountBehavior::$discounts === null) { DiscountBehavior::$discounts = Discount::model()->activeOnly()->applyDate()->findAll(); } parent::attach($owner); } }
/** * Delete discount * @param array $id */ public function actionDelete($id = array()) { if (Yii::app()->request->isPostRequest) { $model = Discount::model()->findAllByPk($_REQUEST['id']); if (!empty($model)) { foreach ($model as $m) { $m->delete(); } } if (!Yii::app()->request->isAjaxRequest) { $this->redirect('index'); } } }
echo CHtml::button('Contabilizar', array('id' => 'addDiscount', 'style' => 'margin-top: 12px;')); ?> </div> <div style="clear:both"></div> </div> <div style="width:35%; float:right;" id="extraDiscount"> <?php $criteria = new CDbCriteria(); $criteria->condition = "UserID = :userid AND BookID = :bookid"; $criteria->params = array(':userid' => Yii::app()->user->ID, ':bookid' => $model->ID); $modelExtra = Extra::model()->findAll($criteria); $modelDiscount = Discount::model()->findAll($criteria); echo "<h2 class='extra'>Suplementos</h2>"; if (count($modelExtra) == 0) { echo "<p>No hay suplementos</p>"; } foreach ($modelExtra as $extra) { echo "<p><span class='deleteExtra' id='" . $extra->ID . "'>X</span> " . $extra->Name . " (" . str_replace(',', '.', $extra->Value) . "€)</p>"; } echo "<h2 class='extra'>Descuentos</h2>"; if (count($modelDiscount) == 0) { echo "<p>No hay suplementos</p>"; } foreach ($modelDiscount as $discount) { echo "<p><span class='deleteDiscount' id='" . $discount->ID . "'>X</span> " . $discount->Name . " (" . $discount->Value . "%)</p>"; } ?>
public function actionUpdate() { $id = $_GET['id']; $userid = Commonmodel::getOrganID(); $organid = Commonmodel::getOrganID(); $title = $_POST['Title']; $rate = $_POST['Rate']; //$type=$_POST['Type']; $starttime = strtotime($_POST['StartTime']); $endtime = strtotime($_POST['EndTime']); $content = $_POST['Content']; $time = time(); $ownerID = $_POST['iddata']; $ownerID = substr($ownerID, 0, -1); $ownerID = explode(',', $ownerID); $ownerName = $_POST['namedata']; $ownerName = substr($ownerName, 0, -1); $ownerName = explode(',', $ownerName); $lice = $_POST['licedata']; $lice = substr($lice, 0, -1); $lice = explode(',', $lice); $carID = $_POST['cardata']; $carID = substr($carID, 0, -1); $carID = explode(',', $carID); $ownerPhone = $_POST['phonedata']; $ownerPhone = substr($ownerPhone, 0, -1); $ownerPhone = explode(',', $ownerPhone); $nickname = $_POST['nickdata']; $nickname = substr($nickname, 0, -1); $nickname = explode(',', $nickname); // $model = Discount::model()->updateAll(array('Title' => $title, 'Content' => $content, 'Rate' => $rate, 'StartTime' => $starttime, 'EndTime' => $endtime, 'UpdateTime' => $time), "ID={$id}"); $result = DiscountCode::model()->findAll('DiscountID=:id', array(':id' => $id)); if (!empty($result)) { foreach ($result as $value) { if (!in_array($value['CarID'], $carID)) { if (!empty($value['CarID'])) { $sql = "delete from tbl_discount_code where CarID={$value['CarID']} and DiscountID={$id}"; $result = Yii::app()->db->createCommand($sql)->execute(); } } else { $arr[] = $value['CarID']; } } foreach ($carID as $val) { if (!in_array($val, $arr)) { $code = $this->code(); $sql = "select a.OwnerID,b.Name,b.Phone,a.LicensePlate,b.NickName from tbl_car_info a,tbl_car_owner b\n\t\t\t\t\twhere a.ID='{$val}' and a.UserID='{$userid}' \n\t\t\t and a.OrganID='{$organid}' and a.OwnerID=b.ID"; $result = Yii::app()->db->createCommand($sql)->queryRow(); $data = Yii::app()->db->createCommand()->insert('tbl_discount_code', array('DiscountID' => $id, 'CarID' => $val, 'OwnerID' => $result['OwnerID'], 'OwnerName' => $result['Name'], 'OwnerPhone' => $result['Phone'], 'Code' => $code, 'LicensePlate' => $result['LicensePlate'], 'NickName' => $result['NickName'])); } } echo json_encode($data); } else { for ($i = 0; $i < count($ownerID); $i++) { $code = $this->code(); $data = Yii::app()->db->createCommand()->insert('tbl_discount_code', array('DiscountID' => $id, 'OwnerID' => $ownerID[$i], 'OwnerName' => $ownerName[$i], 'OwnerPhone' => $ownerPhone[$i], 'Code' => $code, 'LicensePlate' => $lice[$i], 'NickName' => $nickname[$i])); } echo json_encode($data); } }
/** * 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 the ID of the model to be loaded */ public function loadModel($id) { $model = null; if ($id > 0) { $model = Discount::model()->findByPk($id); } if ($model === null) { return new Discount(); } //throw new CHttpException(404,'The requested page does not exist.'); return $model; }
/** * Delete extra from Book */ public function actionDeleteExtra() { $model = Extra::model()->findByPK($_POST["extra"]); $model->delete(); $criteria = new CDbCriteria(); $criteria->condition = "UserID = :userid AND BookID = :bookid"; $criteria->params = array(':userid' => Yii::app()->user->ID, ':bookid' => $_POST["book"]); $modelExtra = Extra::model()->findAll($criteria); $modelDiscount = Discount::model()->findAll($criteria); echo "<h2 class='extra'>Suplementos</h2>"; if (count($modelExtra) == 0) { echo "<p>No hay suplementos</p>"; } foreach ($modelExtra as $extra) { echo "<p><span class='deleteExtra' id='" . $extra->ID . "'>X</span> " . $extra->Name . " (" . str_replace(',', '.', $extra->Value) . "€)</p>"; } echo "<h2 class='extra'>Descuentos</h2>"; if (count($modelDiscount) == 0) { echo "<p>No hay suplementos</p>"; } foreach ($modelDiscount as $discount) { echo "<p><span class='deleteDiscount' id='" . $discount->ID . "'>X</span> " . $discount->Name . " (" . $discount->Value . "%)</p>"; } echo ' <script> function calculaPrecio(start,finish,room,pax) { var request = $.ajax({ url: "' . $this->createURL("book/calculateprice") . '", type: "POST", data: { start : start, finish : finish, room : room, pax : pax, book : ' . $_POST["book"] . ' }, dataType: "html" }); request.done(function(msg) { $("#pvp").html(msg); }); } // Delete Extra from Book $(".deleteExtra").click(function(){ if(confirm("\\u00bfSeguro que quieres eliminar este suplemento?")) { var request = $.ajax({ url: "' . $this->createURL("book/deleteExtra") . '", type: "POST", dataType: "html", data: { extra : $(this).attr("id"), book : ' . $_POST["book"] . ' } }); request.done(function(msg) { $("#extraDiscount").html(msg); calculaPrecio($("#Book_Start").val(), $("#Book_Finish").val(), $("#Book_RoomID").val(), $("#Book_Pax").val()); }); } }); // Delete Discount from Book $(".deleteDiscount").click(function(){ if(confirm("\\u00bfSeguro que quieres eliminar este descuento?")) { var request = $.ajax({ url: "' . $this->createURL("book/deleteDiscount") . '", type: "POST", dataType: "html", data: { discount : $(this).attr("id"), book : ' . $_POST["book"] . ' } }); request.done(function(msg) { $("#extraDiscount").html(msg); calculaPrecio($("#Book_Start").val(), $("#Book_Finish").val(), $("#Book_RoomID").val(), $("#Book_Pax").val()); }); } }); </script> '; }