Advertisement
Greenice

Yii 2 Controller Example

Jan 31st, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.58 KB | None | 0 0
  1. <?php
  2.  
  3. use Dompdf\Dompdf;
  4.  
  5. Yii::import('application.modules.order.utils.OrderViewHelper');
  6. Yii::import('application.modules.order.models._manager.OrderHistoriesManager');
  7. Yii::import('application.modules.order.models.payment.*');
  8. Yii::import('application.modules.customer.CustomerModule');
  9. Yii::import('application.modules.order.utils.OrderViewHelper');
  10. Yii::import('common.extensions.dompdf.autoload', true);
  11.  
  12. class OrderController extends EController {
  13.  
  14.     /**
  15.      * Specifies active menu and submenu in Sidebar Menu
  16.      */
  17.     public function init() {
  18.         $this->modelName = 'Order';
  19.         $this->layout = '//layouts/inner_wide';
  20.         return parent::init();
  21.     }
  22.  
  23.     public function filters() {
  24.         return CMap::mergeArray(parent::filters(), array(
  25.                     array(
  26.                         'common.extensions.filters.HttpsFilter + paynow',
  27.                     ),
  28.                         )
  29.         );
  30.     }
  31.  
  32.     public function actionView($id) {
  33.         if (app()->user->getIsGuest()) {
  34.             Yii::app()->user->returnUrl = Yii::app()->createUrl('/order/order/view', array(
  35.                 'id' => $id
  36.             ));
  37.  
  38.             $this->redirect(Yii::app()->getModule('user')->loginUrl);
  39.         }
  40.  
  41.         $order = $this->loadModel($id);
  42.        
  43.         if ($order->customer->user_id != app()->user->getId() && $order->cr_uid != app()->user->getId()) {
  44.             throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
  45.         }
  46.  
  47.         $notices = array();
  48.  
  49.         if ($order->isNotCompletedPaid()) {
  50.             $notices[] = $this->module->getConfig('orderNotPaidText');
  51.         }
  52.  
  53.         $orderExperiencesNotScheduled = $order->getNotScheduledExperiences();
  54.  
  55.         if (count($orderExperiencesNotScheduled) > 0) {
  56.             $notices[] = $this->module->getConfig('orderNotDateText');
  57.         }
  58.  
  59.         if (count($notices) > 0) {
  60.             $notices = '<strong>Notices: </strong><br>' . implode('<br>', $notices);
  61.             app()->user->setFlash('error', $notices);
  62.         }
  63.  
  64.         $this->render('single_order', array(
  65.             'model' => $order,
  66.             'message' => false
  67.         ));
  68.     }
  69.    
  70.     public function actionGenerated() {
  71.         $this->layout = '//layouts/inner_wide';
  72.  
  73.         $orderId = app()->user->getState('generatedOrderId', null);
  74.  
  75.         if($orderId == null){
  76.             $this->redirect(array('/customer/profile'));
  77.         }else{
  78. //            app()->user->setState('generatedOrderId', null);
  79.         }
  80.  
  81.         $order = $this->loadModel($orderId);
  82.  
  83.         $notices = array();
  84.  
  85.         if ($order->ordercustomertype_id != OrderCustomerType::TYPE_CORPORATE) {
  86.             if ($order->isNotCompletedPaid()) {
  87.                 $notices[] = $this->module->getConfig('orderNotPaidText');
  88.             }
  89.  
  90.             $orderExperiencesNotScheduled = $order->getNotScheduledExperiences();
  91.  
  92.             if (count($orderExperiencesNotScheduled) > 0) {
  93.                 $notices[] = $this->module->getConfig('orderNotDateText');
  94.             }
  95.         }
  96.        
  97.  
  98.         if (count($notices) > 0) {
  99.             $notices = implode('<br>', $notices);
  100.             app()->user->setFlash('notice', $notices);
  101.         }
  102.  
  103.         $this->render('single_order', array(
  104.             'model' => $order,
  105.             'message' => Yii::t('Thank you page', 'Thank you for the order.'),
  106.             'notify' => $order->ordercustomertype_id != OrderCustomerType::TYPE_CORPORATE,
  107.             'trackingCodes' => $order->commercial_type_id == CommercialType::COMMERCIAL_WEBSITE
  108.         ));
  109.     }
  110.  
  111.     public function actionPayNow($id) {
  112.         $order = $this->loadModel($id);
  113.  
  114.         if (!$order->isNotCompletedPaid()) {
  115.             throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
  116.         }
  117.  
  118.         $paymentMethod = null;
  119.  
  120.         if (isset($_POST['Order']['payment_option'])) {
  121.  
  122.             try {
  123.                 $paymentOption = $_POST['Order']['payment_option'];
  124.                 $order->payment_option = $paymentOption;
  125.                 $paymentMethod = PaymentMethod::PaymentMethodFactory($paymentOption, $order);
  126.                 $paymentMethod->attributes = isset($_POST[get_class($paymentMethod)]) ? $_POST[get_class($paymentMethod)] : array();
  127.  
  128.                 if ($paymentMethod->validate()) {
  129.  
  130.                     /* Process payment and Save to Order */
  131.                     $paymentResponse = $paymentMethod->process();
  132.                     if ($paymentResponse->hasError() === true) {
  133.                         user()->setFlash('error', t('<b>Payment Error</b> ' . $paymentResponse->getErrorMessage()));
  134.                     } else {
  135.  
  136.                         $order = $this->loadModel($id);
  137.                         $order->setOrderPaidStatus(true);
  138.  
  139.                         // send order confirmation email
  140.                         if($order->isNotCompletedPaid()){
  141.                             app()->notifier->send(array(
  142.                                 'code' => Notification::CODE_PARTIAL_PAYMENT,
  143.                                 'to' => $order->customer->email,
  144.                                 'type' => 'email',
  145.                                 'data' => array(
  146.                                     'order' => $order,
  147.                                 )
  148.                             ));
  149.                         }else{
  150.                             app()->notifier->send(array(
  151.                                 'code' => Notification::CODE_ORDER_CONFIRMATION,
  152.                                 'to' => $order->customer->email,
  153.                                 'type' => 'email',
  154.                                 'data' => array(
  155.                                     'order' => $order,
  156.                                     'user' => null,
  157.                                     'password' => null,
  158.                                     'payment' => $paymentMethod
  159.                                 )
  160.                             ));
  161.                         }
  162.  
  163.                         user()->setFlash('success', t('<b>Successfully:</b> Payment for Order: #{modelID} received successfully.', array('{modelID}' => $order->id)));
  164.                         $this->redirect('/order/order/view/id/' . $order->id);
  165.                     }
  166.                 }
  167.             } catch (Exception $e) {
  168.                 user()->setFlash('error', t('<b>Payment Error</b> ' . $e->getMessage()));
  169.             }
  170.         }
  171.  
  172.  
  173.         if (null == $paymentMethod) {
  174.             $order->payment_option = PaymentMethod::getDefaultInstantlyPaymentCode();
  175.         }
  176.  
  177.         $this->render('paynow', array(
  178.             'model' => $order,
  179.             'paymentmodel' => $paymentMethod,
  180.         ));
  181.     }
  182.  
  183.     /**
  184.      * Lists all order for current customer.
  185.      */
  186.     public function actionIndex() {
  187.         if (app()->user->getIsGuest()) {
  188.             $this->redirect(Yii::app()->getModule('user')->loginUrl);
  189.         }
  190.  
  191.         $model = new $this->modelName('search');
  192.         $customer = $model->getCurrentCustomer();
  193.         if (Yii::app()->user->getState('scope') == User::SCOPE_EMPLOYEE) {
  194.             $model->cr_uid = Yii::app()->user->id;
  195.         } else {
  196.             $model->customer_id = $customer->id;
  197.         }
  198.  
  199.         $this->render('index', array(
  200.             'model' => $model,
  201.         ));
  202.     }
  203.  
  204.     /**
  205.      * Lists all order experiences for current customer.
  206.      */
  207.     public function actionRides() {
  208.       if (app()->user->getIsGuest()) {
  209.         $this->redirect(Yii::app()->getModule('user')->loginUrl);
  210.       }
  211.  
  212.       $model = new OrderExperience('search');
  213.       $customer = Order::model()->getCurrentCustomer();
  214.       $model->customer_id = $customer->id;
  215.  
  216.       $this->render('rides', array(
  217.         'model' => $model,
  218.       ));
  219.     }
  220.  
  221.     public function actionRideView($id) {
  222.       if (app()->user->getIsGuest()) {
  223.         throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
  224.       }
  225.  
  226.       $orderExperience = OrderExperience::model()->findByPk($id);
  227.  
  228.       if ($orderExperience == null) {
  229.         throw new CHttpException(400, 'Invalid request. The Order Experience does not exists.');
  230.       }
  231.  
  232.       $this->render('rides_view', array(
  233.         'model' => $orderExperience
  234.       ));
  235.     }
  236.  
  237.     public function actionPrintCertificate($id)
  238.     {
  239.         if (app()->user->getIsGuest()) {
  240.             $orderId = app()->user->getState('generatedOrderId', null);
  241.             $order = $this->loadModel($orderId);
  242.  
  243.             $exists = false;
  244.             foreach ($order->experiences as $experience) {
  245.                 if ($experience->id == $id) {
  246.                     $exists = true;
  247.                     break;
  248.                 }
  249.             }
  250.  
  251.             if (!$exists) {
  252.                 throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
  253.             }
  254.  
  255.             $orderExperience = OrderExperience::model()->findByPk($id);
  256.  
  257.             if ($orderExperience === null) {
  258.                 throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
  259.             }
  260.         } else {
  261.             $orderExperience = OrderExperience::model()->findByPk($id);
  262.  
  263.             if ($orderExperience === null) {
  264.                 throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
  265.             }
  266.  
  267.             if ($orderExperience->order->customer->user_id != app()->user->getId()) {
  268.                 throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
  269.             }
  270.         }
  271.        
  272.         $experience = null;
  273.         if ($giftpackage = $orderExperience->getGiftPackage()) {
  274.             $experience = $giftpackage->name;
  275.             if ($giftpackage->price_type == 'custom') {
  276.                 $experience .= ' - ' . Yii::app()->format->formatCurrency($orderExperience->giftpackage_amount);
  277.             }
  278.         } else {
  279.             $experiences = array();
  280.             foreach ($orderExperience->drivingExperiences as $dE) {
  281.                 $trackVehicleModel = $dE->trackVehicle->vehicle->vehiclemodel;
  282.                 $title = $dE->discount_laps == 0 ? $dE->laps : $dE->laps . ' + ' . $dE->discount_laps;
  283.                 $title .= ' ' . Yii::t('Booking page', $dE->discount_laps == 0 ? $dE->trackVehicle->units : $dE->trackVehicle->free_units, $dE->discount_laps == 0 ? $dE->laps : $dE->discount_laps);
  284.                 $experiences[] = $trackVehicleModel->getMake()->name . ' ' . $trackVehicleModel->name . ' - ' . $title;
  285.             }
  286.             $experience = implode('<br>', $experiences);
  287.         }
  288.  
  289.         $from = null;
  290.         $customer = $orderExperience->order->customer;
  291.         if (!RetailerRepresentative::model()->getRepresentativeByCustomerId($customer->id)) {
  292.             $from = $customer->getCustomerFullName();
  293.         }
  294.         $code = OrderExperienceGiftPackageCode::findByExperience($orderExperience->id);
  295.  
  296.         $dompdf = new Dompdf();
  297.         $dompdf->loadHtml($this->renderPartial('gift_certificate', array(
  298.             'reference' => $orderExperience->order->getReference(),
  299.             'track' => $orderExperience->track->name,
  300.             'code' => $code ? $code->code : null,
  301.             'to' => $orderExperience->getStudent()->getCustomerFullName(),
  302.             'from' => $from,
  303.             'date' => $orderExperience->trackdatesession ? $orderExperience->trackdatesession->trackdate->date . ' ' . $orderExperience->trackdatesession->from : null,
  304.             'experience' => $experience,
  305.             'filesBaseUrl' => Yii::getPathOfAlias('webroot')
  306.         ), true));
  307.         $dompdf->setPaper('A4');
  308.         $dompdf->render();
  309.         $dompdf->stream("gift_certificate.pdf", array("Attachment" => false));
  310.         exit;
  311.     }
  312.  
  313.     public function actionCreate() {
  314.         throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
  315.     }
  316.  
  317.     /**
  318.      * Updates a particular model.
  319.      * If update is successful, the browser will be redirected to the 'view' page.
  320.      * @param integer $id the ID of the model to be updated
  321.      */
  322.     public function actionUpdate($id) {
  323.         throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
  324.     }
  325.  
  326.     /**
  327.      *
  328.      */
  329.     public function actionSendAfterOrderNotifications(){
  330.         $orderId = Yii::app()->request->getParam('order_id');
  331.  
  332.         /* @var Order $order */
  333.         $order = Order::model()->findByPk($orderId);
  334.  
  335.         $result = $order->sendAfterOrderNotifications();
  336.         die($result);
  337.     }
  338.  
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement