public function ordersAction()
 {
     $ordersModel = Application_Model_Orders::getInstance();
     $cartiddetailsModel = Application_Model_Addtocart::getInstance();
     $orderaddressModel = Application_Model_OrderAddress::getInstance();
     $userdeliveryaddresssModel = Application_Model_UserDeliveryAddress::getInstance();
     $response = new stdClass();
     $method = $this->getRequest()->getParam('method');
     if ($method) {
         switch ($method) {
             case 'insertorders':
                 if ($this->getRequest()->isPost()) {
                     $addressId = $this->getRequest()->getPost('addressid');
                     $cartid = $this->getRequest()->getPost('cartid');
                     $cartid = json_decode($cartid);
                     $fetchcartiddetails = $cartiddetailsModel->selectcartiddetails($cartid);
                     if (!empty($fetchcartiddetails)) {
                         $productId = array();
                         $quantity = array();
                         foreach ($fetchcartiddetails as $key => $value) {
                             $productId[] = $value['product_id'];
                             $quantity[] = $value['quantity'];
                         }
                         $data['user_id'] = $fetchcartiddetails[0]['user_id'];
                         $data['total_amount'] = $this->getRequest()->getPost('totalamount');
                         $data['hotel_id'] = $fetchcartiddetails[0]['hotel_id'];
                         $data['quantity'] = json_encode($quantity);
                         $data['product_id'] = json_encode($productId);
                         $data['product_amount'] = $this->getRequest()->getPost('productamount');
                         $data['product_amount'] = json_decode($data['product_amount']);
                         $data['product_amount'] = json_encode($data['product_amount']);
                         $data['delivery_charge'] = $this->getRequest()->getPost('deliverycharge');
                         $insertorderid = $ordersModel->insertOrders($data);
                         if ($insertorderid) {
                             $selectuserdeliveryaddress = $userdeliveryaddresssModel->selectUserDeliveryAddress($addressId);
                             unset($selectuserdeliveryaddress['user_delivery_address_id']);
                             unset($selectuserdeliveryaddress['ordered_user_id']);
                             $selectuserdeliveryaddress['order_id'] = $insertorderid;
                             $insertorderaddress = $orderaddressModel->insertorderaddress($selectuserdeliveryaddress);
                             if ($insertorderaddress) {
                                 $response->message = 'successfull';
                                 $response->code = 200;
                             } else {
                                 $response->message = 'Fail';
                                 $response->code = 197;
                             }
                         } else {
                             $response->message = 'Fail';
                             $response->code = 197;
                         }
                     } else {
                         $response->message = 'CartId Should not be blank.';
                         $response->code = 401;
                         $response->data = Null;
                     }
                 } else {
                     $response->message = 'Invalid Request';
                     $response->code = 401;
                     $response->data = Null;
                 }
                 echo json_encode($response, true);
                 die;
                 break;
             case 'historyorders':
                 if ($this->getRequest()->isPost()) {
                     $userid = $this->getRequest()->getPost('user_id');
                     $offset = $this->getRequest()->getPost('offset');
                     $limit = $this->getRequest()->getPost('limit');
                     if (!empty($userid)) {
                         $fetchorderhistory = $ordersModel->selecthistoryorder($userid, $offset, $limit);
                         if (!empty($fetchorderhistory)) {
                             $response->message = 'successfull';
                             $response->code = 200;
                             $response->data = $fetchorderhistory;
                         } else {
                             $response->message = 'Could Not Serve The Request';
                             $response->code = 197;
                             $response->data = null;
                         }
                     } else {
                         $response->message = 'UserID Should not be blank';
                         $response->code = 401;
                         $response->data = NULL;
                     }
                 } else {
                     $response->message = 'Invalid Request';
                     $response->code = 401;
                     $response->data = Null;
                 }
                 echo json_encode($response, true);
                 die;
                 break;
             case 'orderstatus':
                 if ($this->getRequest()->isPost()) {
                     $orderid = $this->getRequest()->getPost('order_id');
                     if (!empty($orderid)) {
                         $fetchorderstatus = $ordersModel->selectorderstatus($orderid);
                         if (!empty($fetchorderstatus)) {
                             $response->message = 'successfull';
                             $response->code = 200;
                             $response->data = $fetchorderstatus[0];
                         } else {
                             $response->message = 'Could Not Serve The Request';
                             $response->code = 197;
                             $response->data = null;
                         }
                     } else {
                         $response->message = 'UserID or OrderID Should not be blank';
                         $response->code = 401;
                         $response->data = NULL;
                     }
                 } else {
                     $response->message = 'Invalid Request';
                     $response->code = 401;
                     $response->data = Null;
                 }
                 echo json_encode($response, true);
                 die;
                 break;
         }
     }
 }
 public function transactionProcessAction()
 {
     $usertransactionModal = Application_Model_UserTransactions::getInstance();
     $cartiddetailsModel = Application_Model_Addtocart::getInstance();
     $userdeliveryaddrmodal = Application_Model_UserDeliveryAddress::getInstance();
     $orderaddressmodal = Application_Model_OrderAddress::getInstance();
     $response = new stdClass();
     $method = $this->getRequest()->getParam('method');
     $objCore = Engine_Core_Core::getInstance();
     $this->_appSetting = $objCore->getAppSetting();
     if ($method) {
         switch ($method) {
             case 'inserttransaction':
                 if ($this->getRequest()->isPost()) {
                     $tranType = $this->getRequest()->getPost('transactiontype');
                     if ($tranType == 1) {
                         $orderid = $this->getRequest()->getPost('order_id');
                         $userid = $this->getRequest()->getPost('userid');
                         $tranAmount = $this->getRequest()->getPost('amount');
                         $tranDate = $this->getRequest()->getPost('date');
                         $tranStatus = $this->getRequest()->getPost('status');
                         $cod_code = mt_rand(100000, 999999);
                         $result = array_merge(['order_id' => $orderid], ['user_id' => $userid], ['tx_amount' => $tranAmount], ['tx_date' => $tranDate], ['tx_status' => $tranStatus], ['tx_type' => $tranType], ['COD_Code' => $cod_code]);
                         $transactionId = $usertransactionModal->insertUseTransactions($result);
                         if ($transactionId) {
                             $account_sid = $this->_appSetting->AccountSID;
                             $auth_token = $this->_appSetting->AuthToken;
                             $client = new Services_Twilio($account_sid, $auth_token);
                             $smsResponse = $client->account->messages->create(array('To' => "+919713902664", 'From' => "+17474002298", 'Body' => $cod_code));
                             if ($smsResponse) {
                                 $response->message = 'successfully Inserted,SMS Sent to your corresponding Mobile number.';
                                 $response->code = 200;
                                 $response->data['transaction_id'] = $transactionId;
                             } else {
                                 $response->message = 'Fail to send Message';
                                 $response->code = 197;
                             }
                         } else {
                             $response->message = 'Could Not Serve The Request';
                             $response->code = 197;
                         }
                     } else {
                         if ($tranType == 2) {
                             $orderid = $this->getRequest()->getPost('order_id');
                             $userid = $this->getRequest()->getPost('userid');
                             $tranAmount = $this->getRequest()->getPost('amount');
                             $tranDate = $this->getRequest()->getPost('date');
                             $tranStatus = $this->getRequest()->getPost('status');
                             $trancode = $this->getRequest()->getPost('transactioncode');
                             $cartid = $this->getRequest()->getPost('cartID');
                             $cartid = json_decode($cartid);
                             $data1 = array_merge(['order_id' => $orderid], ['user_id' => $userid], ['tx_amount' => $tranAmount], ['tx_date' => $tranDate], ['tx_status' => $tranStatus], ['tx_type' => $tranType], ['tx_code' => $trancode]);
                             $transactionId1 = $usertransactionModal->insertUseTransactions($data1);
                             if ($transactionId1) {
                                 $fetchorderid = $usertransactionModal->fetchorderdetails($transactionId1);
                                 if ($fetchorderid['tx_status'] == 1) {
                                     $deletecartiddetails = $cartiddetailsModel->deletecartiddetails($cartid);
                                     if ($deletecartiddetails) {
                                         $response->message = 'successfully Inserted Transaction Details,Cleared From Cart.';
                                         $response->code = 200;
                                         $response->data['transaction_id'] = $transactionId1;
                                     } else {
                                         $response->message = 'Could Not Serve The Request';
                                         $response->code = 197;
                                     }
                                 } else {
                                     $response->message = 'Could Not Serve The Request';
                                     $response->code = 197;
                                 }
                                 //                                }
                             } else {
                                 $response->message = 'Could Not Serve The Request';
                                 $response->code = 197;
                             }
                         } else {
                             $response->code = 198;
                             $response->message = "Required parameter not passed";
                         }
                     }
                 } else {
                     $response->message = 'Could Not Serve The Request';
                     $response->code = 401;
                     $response->data = NULL;
                 }
                 echo json_encode($response, true);
                 die;
                 break;
             case 'updatetransaction':
                 if ($this->getRequest()->isPost()) {
                     $txstatus = $this->getRequest()->getPost('status');
                     if (!empty($txstatus)) {
                         $data['tx_status'] = $txstatus;
                     }
                     $orderid = $this->getRequest()->getPost('order_id');
                     $userid = $this->getRequest()->getPost('userid');
                     if ($userid && $orderid) {
                         $update = $usertransactionModal->updateTransaction($userid, $orderid, $data);
                         if ($update) {
                             $response->message = 'successfull';
                             $response->code = 200;
                             $response->data = $update;
                         } else {
                             $response->message = 'Could Not Serve The Request';
                             $response->code = 197;
                             $response->data = $update;
                         }
                     } else {
                         $response->message = 'Could Not Serve The Request';
                         $response->code = 401;
                         $response->data = NULL;
                     }
                 } else {
                     $response->message = 'Invalid Request';
                     $response->code = 401;
                     $response->data = Null;
                 }
                 echo json_encode($response, true);
                 die;
                 break;
                 //                case 'insertCod_code':
                 //
                 //                    if ($this->getRequest()->isPost()) {
                 //
                 //                        $postData = $this->getRequest()->getParams();
                 //
                 //                        $orderid = '';
                 //                        if (isset($postData['order_id'])) {
                 //                            $orderid = $postData['order_id'];
                 //                        }
                 //
                 //                        if ($orderid != '') {
                 //
                 //                            $cod_code = mt_rand(100000, 999999);
                 //
                 //                            $exists = $usertransactionModal->checkOrderid($orderid, $cod_code);
                 //
                 //                            if (isset($exists)) {
                 //
                 ////                                $account_sid = 'AC91e39267660f124881c6eb071867b1fc';
                 ////                                $auth_token = 'cd5d73f4fef7d4fcb63a57e0558ca8d7';
                 //                                $account_sid = $this->_appSetting->AccountSID;
                 //
                 //                                $auth_token = $this->_appSetting->AuthToken;
                 //
                 //                                $client = new Services_Twilio($account_sid, $auth_token);
                 //
                 //                                $smsResponse = $client->account->messages->create(array(
                 ////                                    'To' => "+919850014148",
                 //                                    'To' => "+919713902664",
                 //                                    'From' => "+17474002298",
                 //                                    'Body' => $cod_code,
                 //                                ));
                 //
                 //                                $response->code = 200;
                 //                                $response->message = "SMS Sent to your corresponding Mobile number.";
                 //                                $response->data = 1;
                 //                            } else {
                 //                                $response->code = 100;
                 //                                $response->message = "OrderId is not correct.";
                 //                                $response->data = null;
                 //                            }
                 //                        } else {
                 //                            $response->code = 100;
                 //                            $response->message = "Orderid Should not be null";
                 //                            $response->data = null;
                 //                        }
                 //                    } else {
                 //                        $response->code = 401;
                 //                        $response->message = "Invalid request";
                 //                        $response->data = null;
                 //                    }
                 //                    echo json_encode($response, true);
                 //                    break;
             //                case 'insertCod_code':
             //
             //                    if ($this->getRequest()->isPost()) {
             //
             //                        $postData = $this->getRequest()->getParams();
             //
             //                        $orderid = '';
             //                        if (isset($postData['order_id'])) {
             //                            $orderid = $postData['order_id'];
             //                        }
             //
             //                        if ($orderid != '') {
             //
             //                            $cod_code = mt_rand(100000, 999999);
             //
             //                            $exists = $usertransactionModal->checkOrderid($orderid, $cod_code);
             //
             //                            if (isset($exists)) {
             //
             ////                                $account_sid = 'AC91e39267660f124881c6eb071867b1fc';
             ////                                $auth_token = 'cd5d73f4fef7d4fcb63a57e0558ca8d7';
             //                                $account_sid = $this->_appSetting->AccountSID;
             //
             //                                $auth_token = $this->_appSetting->AuthToken;
             //
             //                                $client = new Services_Twilio($account_sid, $auth_token);
             //
             //                                $smsResponse = $client->account->messages->create(array(
             ////                                    'To' => "+919850014148",
             //                                    'To' => "+919713902664",
             //                                    'From' => "+17474002298",
             //                                    'Body' => $cod_code,
             //                                ));
             //
             //                                $response->code = 200;
             //                                $response->message = "SMS Sent to your corresponding Mobile number.";
             //                                $response->data = 1;
             //                            } else {
             //                                $response->code = 100;
             //                                $response->message = "OrderId is not correct.";
             //                                $response->data = null;
             //                            }
             //                        } else {
             //                            $response->code = 100;
             //                            $response->message = "Orderid Should not be null";
             //                            $response->data = null;
             //                        }
             //                    } else {
             //                        $response->code = 401;
             //                        $response->message = "Invalid request";
             //                        $response->data = null;
             //                    }
             //                    echo json_encode($response, true);
             //                    break;
             case 'varifyCod_code':
                 if ($this->getRequest()->isPost()) {
                     $postData = $this->getRequest()->getParams();
                     $orderid = '';
                     if (isset($postData['order_id'])) {
                         $orderid = $postData['order_id'];
                     }
                     $codcode = '';
                     if (isset($postData['COD_Code'])) {
                         $codcode = $postData['COD_Code'];
                     }
                     $cartid = $this->getRequest()->getPost('cartID');
                     $cartid = json_decode($cartid);
                     if ($orderid != '' && $codcode != '' && $cartid != '') {
                         $exists = $usertransactionModal->verifycodCode($orderid, $codcode);
                         if ($exists) {
                             $fetchorderid = $usertransactionModal->fetchorderdetails($orderid);
                             if ($fetchorderid['tx_status'] == 1) {
                                 $deletecartiddetails = $cartiddetailsModel->deletecartiddetails($cartid);
                                 $response->code = 200;
                                 $response->message = "Code Verify Successfully";
                                 $response->data = $exists;
                             } else {
                                 $response->code = 100;
                                 $response->message = "You missed something";
                                 $response->data = null;
                             }
                         } else {
                             $response->code = 100;
                             $response->message = "Code Didnt Matched, Enter Correct Code.";
                             $response->data = null;
                         }
                     } else {
                         $response->code = 401;
                         $response->message = "Invalid request";
                         $response->data = null;
                     }
                 } else {
                     $response->code = 401;
                     $response->message = "Invalid request";
                     $response->data = null;
                 }
                 echo json_encode($response, true);
                 break;
             case 'verifyPhoneNumber':
                 if ($this->getRequest()->isPost()) {
                     $postData = $this->getRequest()->getParams();
                     $addressid = '';
                     if (isset($postData['addressid'])) {
                         $addressid = $postData['addressid'];
                     }
                     $oldphonenumber = '';
                     if (isset($postData['oldContactNo'])) {
                         $oldphonenumber = $postData['oldContactNo'];
                     }
                     $newphonenumber = '';
                     if (isset($postData['newContactNo'])) {
                         $newphonenumber = $postData['newContactNo'];
                     }
                     $fetchdetailsfromBoth = $userdeliveryaddrmodal->fetchDetails($addressid);
                     $orderaddressid = $fetchdetailsfromBoth['order_address_id'];
                     if ($addressid != '') {
                         $fetchaddressdetails = $userdeliveryaddrmodal->fetchUserDeliveryAddressByAddressID($addressid, $oldphonenumber);
                         if ($fetchaddressdetails) {
                             if ($oldphonenumber != '' && $newphonenumber != '') {
                                 $updateData['user_contact_number'] = $newphonenumber;
                                 $whereForUpdate = 'user_delivery_address_id=' . $addressid;
                                 $updateUserDeliveryAddress = $userdeliveryaddrmodal->updateUserDeliveryAddressWhere($updateData, $whereForUpdate);
                                 if (isset($updateUserDeliveryAddress)) {
                                     $updateData['user_contact_number'] = $newphonenumber;
                                     $whereForUpdate = 'order_address_id=' . $orderaddressid;
                                     $updateorderaddress = $orderaddressmodal->updateorderaddressWhere($updateData, $whereForUpdate);
                                     if (isset($updateorderaddress)) {
                                         $response->code = 200;
                                         $response->message = "Your Phone Number Updated Successfully";
                                         $response->data = 1;
                                     } else {
                                         $response->code = 100;
                                         $response->message = "Something Went wrong";
                                         $response->data = null;
                                     }
                                 } else {
                                     $response->code = 100;
                                     $response->message = "Something Went wrong.";
                                     $response->data = null;
                                 }
                             } else {
                                 $response->code = 100;
                                 $response->message = "Missing Parameter";
                                 $response->data = null;
                             }
                         } else {
                             $response->code = 401;
                             $response->message = "Incorrect Old Phone Number";
                             $response->data = null;
                         }
                     } else {
                         $response->code = 100;
                         $response->message = "AddressID Should not be null";
                         $response->data = null;
                     }
                 } else {
                     $response->code = 401;
                     $response->message = "Invalid request";
                     $response->data = null;
                 }
                 echo json_encode($response, true);
                 break;
         }
     } else {
         $response->message = 'Invalid Request';
         $response->code = 401;
         $response->data = Null;
         echo json_encode($response);
         die;
     }
 }