Subversion Repositories SmartDukaan

Rev

Rev 17890 | Rev 17893 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * Exceptionalnlcs Controller
 *
 * @property Exceptionalnlc $Exceptionalnlc
 * @property PaginatorComponent $Paginator
 */
class ShippingsController extends AppController {

/**
 * Components
 *
 * @var array
 */
        public $components = array('Paginator');

        public function beforeFilter() {
                parent::beforeFilter();
                $this->apihost = Configure::read('pythonapihost');
                $this->mobileapihost = Configure::read('saholicapihost');
                $this->Auth->allow('isServicable');
        }
        

        public function index(){
                $userId = $this->Auth->User('id');
                $dataGiven = json_decode($this->request->data['cart_details']);
                if ($this->request->is('post')) {
                        $data = $this->request->data;
                }
                
                $suserId = base64_decode($_COOKIE['s_id']);
                $scartId = base64_decode($_COOKIE['s_cart']);
                $semailId = base64_decode($_COOKIE['s_email']);
                
                $pincode = 0;
                
                if($pincode==0 && isset($_COOKIE['s_pincode'])){
                        $pincode = base64_decode($_COOKIE['s_pincode']);
                }
                
                $cartItems = array();
                
                foreach ($dataGiven->cartItems as $key=>$obj) {
                    $itemobj = array(
                                        'itemId'   => $key,
                                'quantity' => $obj->quantity);
                        array_push($cartItems, $itemobj);
                }
                $postData = array(
                                        'cartItems'     =>      $cartItems
                                        );
                                        
                $params = array(
                                'cartMap' => urlencode(json_encode($postData)));
                
                $this->layout = 'cartinnerpages';
                $url = Configure::read('saholicapihost').'cart!validateCart?isLoggedIn=true&privateDealUser=true&userId='.$suserId.'&id='.$scartId.'&email='.$semailId;
                if($pincode!='0'){
                        $url = $url.'&pinCode='.$pincode;
                }
                $cartskus = $this->post_cartinfo_request($url,$params);
                if(isset($cartskus['response']) && $cartskus['response']=='error'){
                        $this->set(compact('cartskus'));
                        $this->render('/Users/cartdetails');
                        
                }elseif(isset($cartskus['cartMessages']) && count($cartskus['cartMessages'])>0){
                        setcookie('s_pincode', base64_encode($cartskus['pincode']), -1, '/');
                        $this->set(compact('cartskus'));
                        $this->render('/Users/cartdetails');
                }else{
                        $totalPayable = intval($dataGiven->totalCartValue) + intval($dataGiven->shippingCharges);
                        $this->Session->write('totalPayable', $totalPayable);
                        $this->layout = 'innerpages';
                        $url = Configure::read ( 'saholicapihost' ) . 'address?isLoggedIn=true&privateDealUser=true&userId=' . $suserId;
                        $response = $this->make_request ( $url, null );
                        $user_name = "";
                        $user_contact = "";
                        
                        if(empty($response ['addresses'])){
                                $this->redirect ( array (
                                                'action' => 'add' 
                                ));
                        }
                        
                        $firstshowaddress = array ();
                        $findedaddress = array ();
                        $moreshowaddress = array ();
                        $current_address_id = - 1;
                        $defaultpin = - 1;
                        $defaultcookiepin = - 1;
                        $defaultcheckedaddress = - 1;
                        $defaultpinaddress = - 1;
                        foreach ( $response ['addresses'] as $key => $value ) {
                                // debug($value['']);
                                
                                if ($value ['id'] == intval ( $response ['defaultAddress'] ) && intval ( $value ['pin'] ) == $pincode) {
                                        $defaultcookiepin = $key;
                                        $user_name = $value ['name'];
                                        $user_contact = $value ['phone'];
                                        if ($defaultcheckedaddress == - 1) {
                                                $defaultcheckedaddress = $value ['id'];
                                        }
                                } else if (intval ( $value ['pin'] ) == $pincode) {
                                        $current_address_id = $key;
                                        array_push ( $findedaddress, $key );
                                        if ($defaultpinaddress == - 1) {
                                                $defaultpinaddress = $value ['id'];
                                        }
                                } else if ($value ['id'] == intval ( $response ['defaultAddress'] )) {
                                        $defaultpin = $key;
                                        $user_name = $value ['name'];
                                        $user_contact = $value ['phone'];
                                } else {
                                        array_push ( $moreshowaddress, $key );
                                }
                        }
                        
                        $defaultselectedaddress = - 1;
                        if ($defaultpinaddress != - 1) {
                                $defaultselectedaddress = $defaultpinaddress;
                        }
                        if ($defaultcheckedaddress != - 1) {
                                $defaultselectedaddress = $defaultcheckedaddress;
                        }
                        
                        if ($defaultcookiepin != - 1) {
                                array_push ( $firstshowaddress, $defaultcookiepin );
                        }
                        
                        for($i = 0; $i < count ( $findedaddress ); $i ++) {
                                array_push ( $firstshowaddress, $findedaddress [$i] );
                        }
                        
                        if ($defaultpin != - 1) {
                                array_push ( $firstshowaddress, $defaultpin );
                        }
                        
                        for($i = 0; $i < count ( $moreshowaddress ); $i ++) {
                                array_push ( $firstshowaddress, $moreshowaddress [$i] );
                        }
                        
                        $this->set ( 'address', $response );
                        
                        $this->set ( compact ( 'firstshowaddress', 'totalPayable', 'user_name', 'user_contact', 'pincode', 'defaultselectedaddress', 'message' ) );
                }
        }
        public function add($user_name = null, $user_contact = null) {
                $this->layout = 'cartinnerpages';
                
                $totalPayable = $_SESSION ['totalPayable'];
                $pinval = base64_decode ( $_COOKIE ['s_pincode'] );
                ;
                $suserId = base64_decode ( $_COOKIE ['s_id'] );
                $scart = base64_decode ( $_COOKIE ['s_cart'] );
                
                if ($this->request->is ( 'post' )) {
                        $data = $this->request->data;
                        
                        $senddata ['name'] = $data ['name'];
                        $senddata ['line1'] = $data ['line1'];
                        $senddata ['line2'] = "";
                        $senddata ['city'] = $data ['city'];
                        $senddata ['state'] = $data ['state'];
                        $senddata ['pin'] = $data ['pin'];
                        $senddata ['phone'] = $data ['phone'];
                        $senddata ['country'] = 'India';
                        $url = Configure::read ( 'saholicapihost' ) . "address?userId=" . $suserId . "&isLoggedIn=true&cartId=" . $scart . "&isPrivateDealUser=true";
                        
                        $response = $this->post_request ( $url, $senddata );
                        
                        $scartId = base64_decode ( $_COOKIE ['s_cart'] );
                        $semailId = base64_decode ( $_COOKIE ['s_email'] );
                        $blank = array ();
                        $postData = array (
                                        'cartItems' => $blank 
                        );
                        
                        $params = array (
                                        'cartMap' => urlencode ( json_encode ( $postData ) ) 
                        );
                        $url = Configure::read ( 'saholicapihost' ) . 'cart!validateCart?isLoggedIn=true&privateDealUser=true&userId=' . $suserId . '&id=' . $scartId . '&email=' . $semailId;
                        // if($pin!='0'){
                        $url = $url . '&pinCode=' . $data ['pin'];
                        // }
                        $cartskus = $this->post_cartinfo_request ( $url, $params );
                        $checkestimate = 'true';
                        if ($cartskus ['maxEstimate'] == - 1) {
                                $checkestimate = 'false';
                        } else {
                                for($i = 0; $i < count ( $cartskus ['cartItems'] ); $i ++) {
                                        if ($cartskus ['cartItems'] [$i] ['estimate'] == - 1) {
                                                $checkestimate = 'false';
                                                break;
                                        }
                                }
                        }
                        
                        if ($checkestimate == 'false') {
                                // debug("I am here");
                                $this->Session->setFlash ( "Address is not serviceable", 'default', array (
                                                'class' => 'alert alert-danger custom_class' 
                                ) );
                                // $this->Session->setFlash('Address is not serviceable');
                                return $this->redirect ( array (
                                                'action' => 'index' 
                                ) );
                        } else {
                                $this->Session->setFlash ( "Address is  serviceable" );
                        }
                }
                
                $url = $this->apihost . "pincodeValidation/" . $pinval;
                $getstate = $this->make_request ( $url, null );
                // debug($getstate);
                $getstateval = "";
                if ($getstate != "{}") {
                        $getstateval = ucwords ( strtolower ( $getstate ['state'] ) );
                }
                $this->set ( compact ( 'getstateval', 'user_name', 'user_contact', 'pinval', 'totalPayable' ) );
        }
        public function submitcheck() {
                $this->autoRender = false;
                $this->request->onlyAllow ( 'ajax' );
                
                $data = $this->request->data;
                
                $senddata ['name'] = $data ['name'];
                $senddata ['line1'] = $data ['line1'];
                $senddata ['line2'] = "";
                $senddata ['city'] = $data ['city'];
                $senddata ['state'] = $data ['state'];
                $senddata ['pin'] = $data ['pin'];
                $senddata ['phone'] = $data ['phone'];
                $senddata ['country'] = 'India';
                $url = Configure::read ( 'saholicapihost' ) . "address?userId=" . $suserId . "&isLoggedIn=true&cartId=" . $scart . "&isPrivateDealUser=true";
                
                $response = $this->post_request($url,$senddata);
                $suserId = base64_decode ( $_COOKIE ['s_id'] );
                
                $scartId = base64_decode ( $_COOKIE ['s_cart'] );
                $semailId = base64_decode ( $_COOKIE ['s_email'] );
                $blank = array ();
                $postData = array (
                                'cartItems' => $blank 
                );
                
                $params = array (
                                'cartMap' => urlencode ( json_encode ( $postData ) ) 
                );
                $url = Configure::read ( 'saholicapihost' ) . 'cart!validateCart?isLoggedIn=true&privateDealUser=true&userId=' . $suserId . '&id=' . $scartId . '&email=' . $semailId;
                // if($pin!='0'){
                $url = $url . '&pinCode=' . $data ['pin'];
                // }
                $cartskus = $this->post_cartinfo_request ( $url, $params );
                $checkestimate = 'true';
                if ($cartskus ['maxEstimate'] == - 1) {
                        $checkestimate = 'false';
                } else {
                        for($i = 0; $i < count ( $cartskus ['cartItems'] ); $i ++) {
                                if ($cartskus ['cartItems'] [$i] ['estimate'] == - 1) {
                                        $checkestimate = 'false';
                                        break;
                                }
                        }
                }
                
                if ($checkestimate == 'false') {
                        setcookie ( 'address_message', "Address is not Servicable", - 1, '/' );
                }
                
                return $checkestimate;
        }
        public function getstate($pin_val) {
                $this->autoRender = false;
                $this->request->onlyAllow ( 'ajax' );
                $url = $this->apihost . "pincodeValidation/" . $pin_val;
                $getstate = $this->make_request ( $url, null );
                // $getstate['state'] = ucwords(strtolower($getstate['state']));
                $nothing = "nothing";
                if ($getstate != "{}") {
                        $getstate ['state'] = ucwords ( strtolower ( $getstate ['state'] ) );
                }
                return json_encode ( $getstate );
        }
        public function isServicable($pin) {
                $this->autoRender = false;
                $this->request->onlyAllow ( 'ajax' );
                // $url = "http://shop2020.in:8080/mobileapi/address!serviceable?pincode=".$pin;
                // $response = $this->make_request($url,null);
                
                $scartId = base64_decode ( $_COOKIE ['s_cart'] );
                $suserId = base64_decode ( $_COOKIE ['s_id'] );
                $semailId = base64_decode ( $_COOKIE ['s_email'] );
                $blank = array ();
                $postData = array (
                                'cartItems' => $blank 
                );
                
                $params = array (
                                'cartMap' => urlencode ( json_encode ( $postData ) ) 
                );
                $url = Configure::read ( 'saholicapihost' ) . 'cart!validateCart?isLoggedIn=true&privateDealUser=true&userId=' . $suserId . '&id=' . $scartId . '&email=' . $semailId;
                $url = $url . '&pinCode=' . $pin;
                $cartskus = $this->post_cartinfo_request ( $url, $params );
                $checkestimate = 'true';
                if ($cartskus ['maxEstimate'] == - 1) {
                        $checkestimate = 'false';
                } else {
                        for($i = 0; $i < count ( $cartskus ['cartItems'] ); $i ++) {
                                if ($cartskus ['cartItems'] [$i] ['estimate'] == - 1) {
                                        $checkestimate = 'false';
                                        break;
                                }
                        }
                }
                
                return $checkestimate;
        }
        
         public function checkout(){
                $addressid = json_decode($this->request->data['addressid']);
                $pincode = 0;
                $cod = $this->request->query('cod');
                $userId = $this->request->query('user_id');
                
                if(isset($this->request->data->pincode)){
                        $pincode = $this->request->data->pincode;
                }
                if($pincode==0 && isset($_COOKIE['s_pincode'])){
                        $pincode = base64_decode($_COOKIE['s_pincode']);
                }
                
                $suserId = base64_decode($_COOKIE['s_id']);
                $scartId = base64_decode($_COOKIE['s_cart']);
                $semailId = base64_decode($_COOKIE['s_email']);
                
                if(isset($_COOKIE['txn_comp'])) {
                        unset($_COOKIE['txn_comp']);
                }
                
                if($cod==1){
                        $url = Configure::read('saholicapihost').'order?payment_option=3000&isLoggedIn=true&privateDealUser=true&userId='.$suserId.'&id='.$scartId.'&email='.$semailId.'&addressid='.$addressid;
                        if($pincode!='0'){
                                $url = $url.'&pinCode='.$pincode;
                        }
                        $orderCreationResponse = $this->post_cartinfo_request($url, null);
                        //{"response":{"success":true,"redirectUrl":"pay-success?paymentId=998593"}}
                        $redirecturl = $orderCreationResponse['response']['redirectUrl'];
                        if (strpos($redirecturl,'pay-success') === false) {
                            setcookie('txn_comp', 'no', -1, '/');
                        }
                        $this->layout = 'innerpages';
                        $next = $redirecturl;                                   
                        $redirectUrl = $this->getAutoLoginUrl($userId,$next);
                        $this->log($redirectUrl,'headers');
                        $this->set(compact('redirectUrl','next'));
                }else{
                        $dataGiven = json_decode($this->request->data['cart_details']);
                        $totalSkus= intval($dataGiven->totalSkus);
                        setcookie('txn_comp', 'no', -1, '/');
                        $this->layout = 'innerpages';
                        $next = "payment?cq=".$totalSkus.'&addressid='.$addressid;                                      
                        $redirectUrl = $this->getAutoLoginUrl($userId,$next);
                        $this->log($redirectUrl,'headers');
                        $this->set(compact('redirectUrl','next'));
                }
         }
}