/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate($shipment_id = '') { $model = new Booking(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); $awb = ''; if (is_numeric($shipment_id)) { $shipment = Shipment::model()->findByPk($shipment_id); if ($shipment instanceof Shipment) { $model->address = $shipment->shipper_address; $model->city = $shipment->shipper_city; $model->postal = $shipment->shipper_postal; $model->country = $shipment->shipper_country; $model->phone = $shipment->shipper_phone; $model->shipment_id = $shipment->id; $awb = $shipment->awb; } } if (isset($_POST['Booking'])) { $model->attributes = $_POST['Booking']; $model->setAttribute('booking_code', dechex(time())); if ($model->save()) { if (!empty($model->shipment_id) || $model->shipment_id != '') { $shipment->booking_id = $model->id; $tes = $shipment->update(); } Yii::app()->user->setFlash('success', 'Success to add new booking, ' . $model->booking_code); $this->redirect(array('index')); } } $this->render('create', array('model' => $model, 'awb' => $awb)); }
public function actionRequestPickUp() { $req = Yii::app()->request; $shipment_id = $req->getQuery('shipment_id'); if (!isset($_POST['Booking'])) { echo CJSON::encode($this->statusError('Must be in POST method')); Yii::app()->end(); } $booking = new Booking(); $booking->setAttributes($_POST['Booking']); $booking->setAttribute('booking_code', dechex(time())); if ($shipment_id != null) { $shipment = Shipment::model()->findByPk($shipment_id); if (!$shipment instanceof Shipment) { echo CJSON::encode($this->statusError('Your Shipment is invalid')); Yii::app()->end(); } } $trans = Yii::app()->db->beginTransaction(); try { if ($booking->save()) { if (isset($shipment) && $shipment instanceof Shipment) { Booking::model()->updateByPk($booking->id, array('customer_id' => $shipment->customer_id)); $shipment->setAttribute('booking_id', $booking->id); if (!$shipment->save()) { throw ServiceControllerException($shipment->getErrors()); } } else { if ($this->token instanceof Token) { $customer = Customer::model()->findByPk($this->token->customer_id); if (!$customer instanceof Customer) { throw new ServiceControllerException('You have to login first'); } else { Booking::model()->updateByPk($booking->id, array('customer_id' => $customer->id)); } } else { $customer = new Customer(); $customer->name = $booking->name; $customer->type = 'personal'; $customer->accountnr = 'WEB' . time(); if ($customer->save()) { $contact = new Contact(); $contact->parent_id = $customer->id; $contact->parent_model = 'Customer'; $contact->full_name = $booking->name; $contact->address = $booking->address; $contact->country = $booking->country; $contact->city = $booking->city; $contact->postal = $booking->postal; if ($contact->save()) { Booking::model()->updateByPk($booking->id, array('customer_id' => $customer->id)); } else { throw new ServiceControllerException($contact->getErrors()); } } else { throw new ServiceControllerException($customer->getErrors()); } } } $data = array('booking_code' => $booking->booking_code); } else { throw ServiceControllerException($booking->getErrors()); } $trans->commit(); } catch (ServiceControllerException $e) { $errors = $e->errors; $trans->rollback(); echo CJSON::encode($this->statusError($errors)); Yii::app()->end(); } echo CJSON::encode($this->statusSuccess($data)); Yii::app()->end(); }