Subversion Repositories SmartDukaan

Rev

Rev 20636 | Rev 20869 | 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']);
                        $url = $this->apihost . "pincodeValidation/" . $pincode;
                        $getstate = $this->make_request ( $url, null );         
                        if ($getstate != "{}") {
                                $stateres = ucwords ( strtolower ( $getstate ['state'] ) );
                        }
                }
                
                $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 = "";
                        $codAvailable = $cartskus['cod'];
                        //$codAvailable = false;
                        $this->Session->write('cod', $codAvailable);
                        $creditTicketSize = 0;
                        
                        $creditorAssociated = $response ['creditorAssociated'];
                        if(!isset($response ['userSanctions'])){
                                $creditLimit = 0;
                        }else{
                                $creditLimit = 0;
                                $creditTicketSize = intval($response ['userSanctions'][0]['ticket_size']);
                                foreach ( $response ['userSanctions'] as $key => $value ) {
                                        $creditLimit = $creditLimit + intval($value ['credit_limit']) - (intval($value['credit_blocked'])+intval($value['loan']));
                                }
                        }
                        $this->Session->write('creditorAssociated', $creditorAssociated);
                        $this->Session->write('creditLimit', $creditLimit);
                        $this->Session->write('creditTicketSize', $creditTicketSize);
                        $taxInvoiceEnabledUser = $response ['taxInvoiceEnabledUser'];
                        $this->Session->write('taxInvoiceEnabledUser', $taxInvoiceEnabledUser);
                        
                        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 ) {
                                
                                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 ( 'stateres','firstshowaddress', 'totalPayable', 'user_name', 'user_contact', 'pincode', 'defaultselectedaddress', 'message', 'codAvailable', 'taxInvoiceEnabledUser', 'creditorAssociated', 'creditLimit', 'creditTicketSize') );
                }
        }
        public function add($user_name = null, $user_contact = null) {
                $this->layout = 'cartinnerpages';
                
                $totalPayable = $_SESSION ['totalPayable'];
                $codAvailable = $_SESSION ['cod'];
                $creditLimit = $_SESSION ['creditLimit'];
                $creditorAssociated = $_SESSION ['creditorAssociated'];
                $creditTicketSize = $_SESSION ['creditTicketSize'];
                $taxInvoiceEnabledUser = $_SESSION['taxInvoiceEnabledUser'];
                //$pinval = base64_decode ( $_COOKIE ['s_pincode'] );
                $pinval='';
                $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(array_key_exists('estimate',$cartskus ['cartItems'] [$i])){
                                                continue;
                                        }
                                        $estimate = $cartskus ['cartItems'] [$i] ['estimate'];
                                        if ($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', 'codAvailable', 'creditorAssociated', 'creditLimit', 'creditTicketSize', 'taxInvoiceEnabledUser'));
        }
        public function submitcheck() {
                $this->autoRender = false;
                $this->request->onlyAllow ( 'ajax' );
                
                $suserId = base64_decode($_COOKIE['s_id']);
                $scartId = base64_decode($_COOKIE['s_cart']);
                $semailId = base64_decode($_COOKIE['s_email']);
                
                $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=" . $scartId . "&isPrivateDealUser=true";
                
                $response = $this->post_cartinfo_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 = $response ['defaultAddress'];
                if ($cartskus ['maxEstimate'] == - 1) {
                        $checkestimate = '';
                } 
                else if ($cartskus ['maxEstimate'] == - 2) {
                        $checkestimate = 'non_service';
                } else {
                        for($i = 0; $i < count ( $cartskus ['cartItems'] ); $i ++) {
                                if(array_key_exists('estimate',$cartskus ['cartItems'] [$i])){
                                        continue;
                                }
                                $estimate = $cartskus ['cartItems'] [$i] ['estimate'];
                                if ($estimate==-1) {
                                        $checkestimate = '';
                                        break;
                                }
                        }
                }
                
                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 if ($cartskus ['maxEstimate'] == -2) {
                        $checkestimate = 'false';
                } 
                else {
                        for($i = 0; $i < count ( $cartskus ['cartItems'] ); $i ++) {
                                if(array_key_exists('estimate',$cartskus ['cartItems'] [$i])){
                                        continue;
                                }
                                $estimate = $cartskus ['cartItems'] [$i] ['estimate'];
                                if ($estimate==-1) {
                                        $checkestimate = 'false';
                                        break;
                                }
                        }
                }
                
                return $checkestimate;
        }
        
         public function checkout(){
                $addressid = $this->request->data['addressid'];
                $pincode = 0;
                $cod = $this->request->query('cod');
                $userId = $this->request->query('user_id');
                
                //Added token validation to ensure only user 
                $testCheck = Configure::read('requireusercheck');
                $tokenValidated = 0;
                if($testCheck){
                        $tokenValidated = $this->checkToken($userId);
                }else{
                        $tokenValidated = 1;
                }
        
                if(!empty($userId) && $tokenValidated==1){                      
                        $url = $this->getAutoLoginUrl($userId,$url);
        
                        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=COD~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, '/');
                                } else {
                                        $order_det = array();
                                        $this->loadModel('Order');
                                        $order_det['user_id'] = $this->Auth->User('id');
                                        $order_det['store_id'] = 4;
                                        $order_det['order_url'] = $this->mobileapihost.$redirecturl;
                                        $this->log(print_r($order_det,1),'ordersdet');
                                        $this->Order->create();
                                        
                                        if ($this->Order->save($order_det)) {
                                                $id = $this->Order->getLastInsertID();
                                                $order = $this->Order->find('first',array('conditions'=>array('id'=>$id),'recursive'=>-1));
                                                $apihost = Configure::read('pythonapihost');
                                                $url = $apihost."storeorder";
                                                
                                                $this->log(print_r($url,1),'ordersdet');
                                                
                                                $this->log(print_r($order,1),'ordersdet');
                                                $response = array();
        //                                      $params = array('sourceId'=>$order['Order']['store_id'],'orderId'=>$order['Order']['id'],'subTagId'=>$order['Order']['sub_tag'],'userId'=>$order['Order']['user_id'],'rawHtml'=>$order['Order']['rawhtml'],'orderSuccessUrl'=>$order['Order']['order_url']);
                                                if(!empty($order)) {
                                                        $params = array('sourceId'=>$order['Order']['store_id'],'orderId'=>$order['Order']['id'],'subTagId'=>$order['Order']['sub_tag'],'userId'=>$order['Order']['user_id'],'rawHtml'=>$order['Order']['rawhtml'],'orderSuccessUrl'=>$order['Order']['order_url']);
                                                        $jsonVar = json_encode($params);
                                                        $response =  $this->make_request($url,$jsonVar);
                                                }else{
                                                        $result = array('success'=>false,'message'=>'Empty order array');
                                                        $response = $result;
                                                }
                                                $this->log(print_r('response',1),'ordersdet');
                                                $this->log(print_r($response,1),'ordersdet');
                                                if(!empty($response) && $response['result']) {
                                                        if($response['htmlRequired'] == 1) {
                                                                $this->loadModel('Rawhtml');
                                                                $data = array('order_id' => $order['Order']['id'],'url' => $response['url'], 'status' => 'new');
                                                                $this->Rawhtml->create();
                                                                $this->Rawhtml->save($data);
                                                                $this->log(print_r('insideif',1),'ordersdet');
                                                                $result =  $response;
                                                                $sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
                                                        }
                                                        else {
                                                                $result =array('success'=>true,'message'=> $response['result']);
                                                                $sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
                                                                $this->log(print_r('insideelse',1),'ordersdet');
                                                        }
                                                        $this->log(print_r($sql,1),'ordersdet');
                                                        $this->Order->query($sql);
                                                        
                                                }                                       
                                        }
                                }
                                $this->layout = 'innerpages';
                                $redirecturl = str_replace("pay-success","pay-success1", $redirecturl);
                                $next = $redirecturl;                                   
                                $redirectUrl = $this->getBigUtsavAutoLoginUrl($userId,$next);
                                $this->log($redirectUrl,'headers');
                                $this->set(compact('redirectUrl','next'));
                        }else{
                                $dataGiven = json_decode($this->request->data['cart_details']);
                                $payMethod = $this->request->data['paymethod'];
                                if(isset($payMethod) && $payMethod=='cap_float'){
                                        $url = Configure::read('saholicapihost').'order?payment_option=456789&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, '/');
                                        } else {
                                                $order_det = array();
                                                $this->loadModel('Order');
                                                $order_det['user_id'] = $this->Auth->User('id');
                                                $order_det['store_id'] = 4;
                                                $order_det['order_url'] = $this->mobileapihost.$redirecturl;
                                                $this->log(print_r($order_det,1),'ordersdet');
                                                $this->Order->create();
                                                
                                                if ($this->Order->save($order_det)) {
                                                        $id = $this->Order->getLastInsertID();
                                                        $order = $this->Order->find('first',array('conditions'=>array('id'=>$id),'recursive'=>-1));
                                                        $apihost = Configure::read('pythonapihost');
                                                        $url = $apihost."storeorder";
                                                        
                                                        $this->log(print_r($url,1),'ordersdet');
                                                        
                                                        $this->log(print_r($order,1),'ordersdet');
                                                        $response = array();
                                                        if(!empty($order)) {
                                                                $params = array('sourceId'=>$order['Order']['store_id'],'orderId'=>$order['Order']['id'],'subTagId'=>$order['Order']['sub_tag'],'userId'=>$order['Order']['user_id'],'rawHtml'=>$order['Order']['rawhtml'],'orderSuccessUrl'=>$order['Order']['order_url']);
                                                                $jsonVar = json_encode($params);
                                                                $response =  $this->make_request($url,$jsonVar);
                                                        }else{
                                                                $result = array('success'=>false,'message'=>'Empty order array');
                                                                $response = $result;
                                                        }
                                                        $this->log(print_r('response',1),'ordersdet');
                                                        $this->log(print_r($response,1),'ordersdet');
                                                        if(!empty($response) && $response['result']) {
                                                                if($response['htmlRequired'] == 1) {
                                                                        $this->loadModel('Rawhtml');
                                                                        $data = array('order_id' => $order['Order']['id'],'url' => $response['url'], 'status' => 'new');
                                                                        $this->Rawhtml->create();
                                                                        $this->Rawhtml->save($data);
                                                                        $this->log(print_r('insideif',1),'ordersdet');
                                                                        $result =  $response;
                                                                        $sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
                                                                }
                                                                else {
                                                                        $result =array('success'=>true,'message'=> $response['result']);
                                                                        $sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
                                                                        $this->log(print_r('insideelse',1),'ordersdet');
                                                                }
                                                                $this->log(print_r($sql,1),'ordersdet');
                                                                $this->Order->query($sql);
                                                                
                                                        }                                       
                                                }
                                        }
                                        $this->layout = 'innerpages';
                                        $next = $redirecturl;                                   
                                        $redirectUrl = $this->getBigUtsavAutoLoginUrl($userId,$next);
                                        $redirectToCart = true;
                                        $this->log($redirectUrl,'headers');
                                        $this->set(compact('redirectUrl','next','redirectToCart'));
                                }else{
                                        $totalSkus= intval($dataGiven->totalSkus);
                                        setcookie('txn_comp', 'no', -1, '/');
                                        $this->layout = 'innerpages';
                                        $next = "payment1?cq=".$totalSkus.'&addressid='.$addressid;                                     
                                        $redirectUrl = $this->getBigUtsavAutoLoginUrl($userId,$next);
                                        $this->log($redirectUrl,'headers');
                                        $this->set(compact('redirectUrl','next'));
                                }
                        }
                } else {
                        if($tokenValidated == 0){
                                $url = '/special/native/login';
                        }elseif($tokenValidated == -1){
                                $url = '/abouts/askforupdate';
                        }
                        $this->redirect($url);
                }
         }
}