/**
  * List all meal follow restaurant
  */
 public function indexAction()
 {
     $this->view->headTitle('Ship information');
     $order_id = session_id();
     //$this->_getParam('order_id', false);
     $this->view->order_id = $order_id;
     $obj_country = new Models_Country();
     $arr_province = $obj_country->getAllProvincesWithCode();
     //		echo '<pre>';print_r($arr_province);die;
     $this->view->arr_province = $arr_province;
     /**
      * Get customer's information
      */
     $data = @$_SESSION['cart_customer'];
     if (null == $data || false == $data) {
         /**
          * Get logged user
          */
         $user = Vi_Registry::getLoggedInUser();
         //		    echo '<pre>';print_r($user);die;
         if (false != $user) {
             $data = array('full_name' => trim($user['full_name']), 'address' => $user['address'], 'address' => $user['address'], 'note' => $user['suite_apt_note'], 'city' => $user['city'], 'state' => $user['state'], 'zip_code' => $user['zipcode'], 'phone1' => $user['phone1'], 'phone2' => $user['phone2'], 'phone3' => $user['phone3'], 'email' => $user['email']);
         }
     }
     $this->view->data = $data;
     /**
      * Expired year
      */
     $years = array();
     for ($i = date('Y'); $i <= date('Y') + 7; $i++) {
         $years[] = $i;
     }
     $this->view->years = $years;
     $this->view->cartErrorMsg = @$_SESSION['card_error_msg'];
     $_SESSION['card_error_msg'] = null;
     $this->view->mark = strtoupper($_SESSION['cart'][$order_id]['order_service']);
     $this->view->date = $_SESSION['cart'][$order_id]['date'];
     $this->view->time = $_SESSION['cart'][$order_id]['time'];
     $order_id = session_id();
     if (isset($_SESSION['cart'][$order_id])) {
         $this->view->subtotal = $_SESSION['cart'][$order_id]['subtotal'];
         $this->view->tax = $_SESSION['cart'][$order_id]['tax'];
         $this->view->shipping = $_SESSION['cart'][$order_id]['shipping'];
         $this->view->ordertotal = $_SESSION['cart'][$order_id]['ordertotal'];
     }
 }
 public function finishAction()
 {
     $order_id = session_id();
     $this->view->headTitle('Reservation');
     $objReser = new Models_Reservation();
     $resId = $this->_getParam('rid', false);
     if (false === $resId) {
         $this->_redirect('');
     }
     /**
      * Get restaurant
      */
     $objRes = new Models_Restaurant();
     $res = $objRes->find($resId)->toArray();
     $res = current($res);
     if (false == $res || '0' == $res['reser_onoff']) {
         $this->_redirect('');
     }
     /**
      * Check active restaurant
      */
     $this->_checkReservationOfRestaurant($res);
     /**
      * Get search condition
      */
     $t = $this->_getParam('t', false);
     $search = $this->session->reserSearch;
     if (null == $search || false == $t || 0 >= $search['quantity']) {
         $this->_redirect('restaurant/reservation/index/rid/' . $resId);
     }
     $t = base64_decode($t);
     /**
      * Update search
      */
     $search['date'] = date('m/d/Y', $t);
     $search['time'] = date('G', $t) * 3600 + ceil(date('i', $t) / 30) * 1800;
     $search['unixTime'] = $t;
     $this->session->reserSearch = $search;
     //        echo '<pre>';print_r($search);die;
     /**
      * Check availabe at current time
      */
     $exitResers = $objReser->searchExistRerservation($t, $t, $res['reser_quantity'] - $search['quantity']);
     if (0 < count($exitResers)) {
         /**
          * Some users are faster :)
          */
         $this->_redirect('restaurant/reservation/index/rid/' . $resId);
     }
     /**
      * Submit?
      */
     $error = '';
     $data = $this->_getParam('data', false);
     //        echo '<pre>';print_r($data);die;
     if (false != $data) {
         //            echo '<pre>';print_r($data);die;
         /**
          * Insert reservation
          */
         $newData = array('restaurant_id' => $resId, 'time' => $search['unixTime'], 'quantity' => $search['quantity'], 'created_date' => time(), 'deposit' => '1' == $res['reser_deposit_onoff'] ? $res['reser_deposit'] : 0, 'full_name' => $data['full_name'], 'address' => $data['address'], 'note' => $data['note'], 'city' => $data['city'], 'state' => $data['state'], 'zip_code' => $data['zip_code'], 'phone' => $data['phone1'] . "." . $data['phone2'] . "." . $data['phone3'], 'email' => $data['email'], 'special_request' => $data['special_request']);
         $newReserId = $objReser->insert($newData);
         if ('1' == $res['reser_deposit_onoff']) {
             /**************************************************************************
              * Make payment
              */
             require_once 'libs/paypal/CallerService.php';
             /**
              * Fist name, last name
              */
             $firstName = trim(substr($data['full_name'], 0, strpos($data['full_name'] . ' ', ' ') + 1));
             $lastName = trim(substr($data['full_name'], strlen($firstName)));
             /**
              * Get required parameters from the web form for the request
              */
             $paymentType = urlencode('Sale');
             $firstName = urlencode($firstName);
             $lastName = urlencode($lastName);
             $creditCardType = urlencode($data['card_type']);
             $creditCardNumber = urlencode($data['card_number']);
             $expDateMonth = urlencode($data['card_month']);
             // Month must be padded with leading zero
             $padDateMonth = str_pad($expDateMonth, 2, '0', STR_PAD_LEFT);
             $expDateYear = urlencode($data['card_year']);
             $cvv2Number = urlencode($data['card_cvv']);
             $address1 = urlencode($data['address']);
             $address2 = urlencode('');
             $city = urlencode($data['city']);
             $state = urlencode($data['state']);
             $zip = urlencode($data['zip_code']);
             $amount = urlencode(number_format($res['reser_deposit'], 2, '.', ''));
             $currencyCode = "CAD";
             //            $state = 'NL';
             $country = 'CA';
             /* Construct the request string that will be sent to PayPal.
                The variable $nvpstr contains all the variables and is a
                name value pair string with & as a delimiter */
             $nvpstr = "&PAYMENTACTION={$paymentType}&AMT={$amount}&CREDITCARDTYPE={$creditCardType}&ACCT={$creditCardNumber}&EXPDATE=" . $padDateMonth . $expDateYear . "&CVV2={$cvv2Number}&FIRSTNAME={$firstName}&LASTNAME={$lastName}&STREET={$address1}&CITY={$city}&STATE={$state}" . "&ZIP={$zip}&COUNTRYCODE={$country}&CURRENCYCODE={$currencyCode}";
             //            echo $nvpstr;die;
             /* Make the API call to PayPal, using API signature.
                The API response is stored in an associative array called $resArray */
             $resArray = hash_call("doDirectPayment", $nvpstr);
             //            echo '<pre>';print_r($resArray);die;
             /* Display the API response back to the browser.
                If the response from PayPal was a success, display the response parameters'
                If the response was an error, display the errors received using APIError.php.
                */
             $ack = strtoupper($resArray["ACK"]);
             /**
              * End payment
              **************************************************************************/
         }
         if ('1' == $res['reser_deposit_onoff'] && $ack != "SUCCESS") {
             $error = @$resArray['L_LONGMESSAGE0'];
             /**
              * Delete reservation
              */
             $objReser->delete(array('reservation_id=?' => $newReserId));
         } else {
             /**
              * Success
              */
             /**
              * Send email to admin and user
              */
             $objUser = new Models_User();
             $objMail = new Models_Mail();
             $config = Vi_Registry::getConfig();
             $cardNumber = @$data['card_number'];
             $cardNumber[4] = 'X';
             $cardNumber[5] = 'X';
             $cardNumber[6] = 'X';
             $cardNumber[7] = 'X';
             $cardNumber[8] = 'X';
             $cardNumber[9] = 'X';
             $cardNumber[10] = 'X';
             $cardNumber[11] = 'X';
             $data['card_number'] = $cardNumber;
             $cartDetail = "\n                     <table width='90%'>\n                            <tr>\n                                <td width='55%'></td>\n                                <td width='15%'></td>\n                                <td width='15%'></td>\n                                <td width='15%'></td>\n                            </tr>\n                            <tr>\n                                <td style='color: #442006; font-weight: bold;'>SHIPPING ADDRESS</td>\n                                <td colspan='3'  style='color: #442006; font-weight: bold;'>RESTAURANT</td>\n                            </tr> \n                            <tr>\n                               <td valign='top'>\n                                   <br/>\n                                   <table width='400px;'>\n            \n                                       <tr>\n                                           <td width='30%' align='right' style='background-color: #EEEEEE;'>Full Name:</td>\n                                           <td width='70%' style='color: #442006;'>{$data['full_name']}</td>\n                                       </tr>\n                                       <tr>\n                                           <td align='right' style='background-color: #EEEEEE;'>Address:</td>\n                                           <td style='color: #442006;'>{$data['address']}</td>\n            \n                                       </tr>\n                                       <tr>\n                                           <td align='right' style='background-color: #EEEEEE;'>Postal Code:</td>\n                                           <td style='color: #442006;'>{$data['zip_code']}</td>\n                                       </tr>\n                                       <tr>\n                                           <td align='right' style='background-color: #EEEEEE;'>Phone:</td>\n            \n                                           <td style='color: #442006;'>{$data['phone1']}.{$data['phone2']}.{$data['phone3']}</td>\n                                       </tr>\n                                       <tr>\n                                           <td align='right' style='background-color: #EEEEEE;'>Special Requests:</td>\n            \n                                           <td style='color: #442006;'>{$data['special_request']}</td>\n                                       </tr>\n                                   </table>\n                                   <br/>\n                                   \n                               </td>\n                               <td valign='top'>\n                                   <br/>\n                                   <table width='400px;'>\n            \n                                       <tr>\n                                           <td width='30%' align='right' style='background-color: #EEEEEE;'>Name:</td>\n                                           <td width='70%' style='color: #442006;'>{$res['name']}</td>\n                                       </tr>\n                                       <tr>\n                                           <td align='right' style='background-color: #EEEEEE;'>Address:</td>\n                                           <td style='color: #442006;'>{$res['street']}, {$res['city']}, {$res['state']}</td>\n                                       </tr>\n                                       <tr>\n                                           <td width='30%' align='right' style='background-color: #EEEEEE;'>Phone:</td>\n                                           <td width='70%' style='color: #442006;'>{$res['phone']}</td>\n                                       </tr>" . ('1' == $res['reser_deposit_onoff'] ? "\n                                       <tr>\n                                           <td colspan='2' style='color: #442006; font-weight: bold;'><br/>PAYMENT METHOD</td>\n                                       </tr>\n                                       <tr>\n                                           <td  align='right' colspan='2'>{$data['card_type']}: {$data['card_number']}</td>\n                                       </tr>" : "") . "\n                                   </table>\n                                   <br/>\n                               </td>\n            \n                            </tr>\n                         </table>   \n                         \n                         <table width='90%'>   \n                            <tr style='background-color: #DDDDDD;'>\n                                <td width='25%' style='color: #AD5B21; font-weight: bold;'> Date</td>\n                                <td width='25%' style='color: #AD5B21; font-weight: bold;'> Time</td>\n                                <td width='25%' style='color: #AD5B21; font-weight: bold;'> Party Size</td>\n                                <td width='25%' style='color: #AD5B21; font-weight: bold;'> Deposit</td>\n                            </tr>\n                             <tr>\n                                <td style='border-bottom: 1px solid #DDDDDD;'> " . date('m/d/Y', $t) . " </td>\n                                <td style='border-bottom: 1px solid #DDDDDD;'>  " . date('g:i A', $t) . "</td>\n                                <td style='border-bottom: 1px solid #DDDDDD;'> {$search['quantity']} " . (1 == $search['quantity'] ? 'person' : 'people') . " </td>\n                                <td style='border-bottom: 1px solid #DDDDDD;'>\$ " . ('1' == $res['reser_deposit_onoff'] ? number_format($res['reser_deposit'] * 1.0, 2) : 0) . "</td>\n                            </tr>\n                            \n                        </table>\n                ";
             $data['card_detail'] = $cartDetail;
             //            echo '<pre>';print_r($data);die;
             $admin = $objUser->getByUserName('admin');
             $objMail->sendHtmlMail('reservation_order', $data, array($admin['email'], $data['email'], $res['owner_email']));
             /**
              * Redirec to success page
              */
             $objContent = new Models_ScontentLang();
             $this->_redirect($objContent->getUrlWithoutAppBaseUrl(22));
             return;
         }
     }
     /**
      * Data for view
      */
     $this->view->res = $res;
     $this->view->arr_restaurant = $res;
     $this->view->resId = $resId;
     $this->view->search = $search;
     $this->view->data = $data;
     $this->view->error = $error;
     $this->view->address_restaurant = $res['street'] . " " . $res['city'] . " " . $res['state'];
     /**
      * Expired year
      */
     $years = array();
     for ($i = date('Y'); $i <= date('Y') + 7; $i++) {
         $years[] = $i;
     }
     $this->view->years = $years;
     /**
      * All province
      */
     $obj_country = new Models_Country();
     $arr_province = $obj_country->getAllProvincesWithCode();
     $this->view->arr_province = $arr_province;
 }