Subversion Repositories SmartDukaan

Rev

Rev 17899 | Rev 17947 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17793 naman 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Exceptionalnlcs Controller
5
 *
6
 * @property Exceptionalnlc $Exceptionalnlc
7
 * @property PaginatorComponent $Paginator
8
 */
9
class ShippingsController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
18
	public function beforeFilter() {
19
		parent::beforeFilter();
20
		$this->apihost = Configure::read('pythonapihost');
21
		$this->mobileapihost = Configure::read('saholicapihost');
17838 manish.sha 22
		$this->Auth->allow('isServicable');
17793 naman 23
	}
24
 
25
 
26
	public function index(){
27
		$userId = $this->Auth->User('id');
17838 manish.sha 28
		$dataGiven = json_decode($this->request->data['cart_details']);
17793 naman 29
		if ($this->request->is('post')) {
30
			$data = $this->request->data;
31
		}
17871 manish.sha 32
 
17838 manish.sha 33
		$suserId = base64_decode($_COOKIE['s_id']);
17871 manish.sha 34
		$scartId = base64_decode($_COOKIE['s_cart']);
35
		$semailId = base64_decode($_COOKIE['s_email']);
36
 
37
		$pincode = 0;
38
 
39
		if($pincode==0 && isset($_COOKIE['s_pincode'])){
40
			$pincode = base64_decode($_COOKIE['s_pincode']);
17793 naman 41
		}
17838 manish.sha 42
 
17871 manish.sha 43
		$cartItems = array();
44
 
45
		foreach ($dataGiven->cartItems as $key=>$obj) {
46
		    $itemobj = array(
47
					'itemId'   => $key,
48
		    		'quantity' => $obj->quantity);
49
			array_push($cartItems, $itemobj);
17838 manish.sha 50
		}
17871 manish.sha 51
		$postData = array(
52
					'cartItems'	=>	$cartItems
53
					);
54
 
55
		$params = array(
56
				'cartMap' => urlencode(json_encode($postData)));
17838 manish.sha 57
 
17871 manish.sha 58
		$this->layout = 'cartinnerpages';
59
		$url = Configure::read('saholicapihost').'cart!validateCart?isLoggedIn=true&privateDealUser=true&userId='.$suserId.'&id='.$scartId.'&email='.$semailId;
60
		if($pincode!='0'){
61
			$url = $url.'&pinCode='.$pincode;
62
		}
63
		$cartskus = $this->post_cartinfo_request($url,$params);
64
		if(isset($cartskus['response']) && $cartskus['response']=='error'){
65
			$this->set(compact('cartskus'));
66
			$this->render('/Users/cartdetails');
67
 
68
		}elseif(isset($cartskus['cartMessages']) && count($cartskus['cartMessages'])>0){
69
			setcookie('s_pincode', base64_encode($cartskus['pincode']), -1, '/');
70
			$this->set(compact('cartskus'));
71
			$this->render('/Users/cartdetails');
72
		}else{
73
			$totalPayable = intval($dataGiven->totalCartValue) + intval($dataGiven->shippingCharges);
74
			$this->Session->write('totalPayable', $totalPayable);
75
			$this->layout = 'innerpages';
17882 naman 76
			$url = Configure::read ( 'saholicapihost' ) . 'address?isLoggedIn=true&privateDealUser=true&userId=' . $suserId;
77
			$response = $this->make_request ( $url, null );
78
			$user_name = "";
79
			$user_contact = "";
17871 manish.sha 80
 
17882 naman 81
			if(empty($response ['addresses'])){
82
				$this->redirect ( array (
83
						'action' => 'add' 
84
				));
17793 naman 85
			}
17871 manish.sha 86
 
17882 naman 87
			$firstshowaddress = array ();
88
			$findedaddress = array ();
89
			$moreshowaddress = array ();
90
			$current_address_id = - 1;
91
			$defaultpin = - 1;
92
			$defaultcookiepin = - 1;
93
			$defaultcheckedaddress = - 1;
94
			$defaultpinaddress = - 1;
95
			foreach ( $response ['addresses'] as $key => $value ) {
96
				// debug($value['']);
97
 
98
				if ($value ['id'] == intval ( $response ['defaultAddress'] ) && intval ( $value ['pin'] ) == $pincode) {
99
					$defaultcookiepin = $key;
100
					$user_name = $value ['name'];
101
					$user_contact = $value ['phone'];
102
					if ($defaultcheckedaddress == - 1) {
103
						$defaultcheckedaddress = $value ['id'];
104
					}
105
				} else if (intval ( $value ['pin'] ) == $pincode) {
106
					$current_address_id = $key;
107
					array_push ( $findedaddress, $key );
108
					if ($defaultpinaddress == - 1) {
109
						$defaultpinaddress = $value ['id'];
110
					}
111
				} else if ($value ['id'] == intval ( $response ['defaultAddress'] )) {
112
					$defaultpin = $key;
113
					$user_name = $value ['name'];
114
					$user_contact = $value ['phone'];
115
				} else {
116
					array_push ( $moreshowaddress, $key );
17871 manish.sha 117
				}
118
			}
17882 naman 119
 
120
			$defaultselectedaddress = - 1;
121
			if ($defaultpinaddress != - 1) {
122
				$defaultselectedaddress = $defaultpinaddress;
17793 naman 123
			}
17882 naman 124
			if ($defaultcheckedaddress != - 1) {
125
				$defaultselectedaddress = $defaultcheckedaddress;
17871 manish.sha 126
			}
17882 naman 127
 
128
			if ($defaultcookiepin != - 1) {
129
				array_push ( $firstshowaddress, $defaultcookiepin );
17871 manish.sha 130
			}
17882 naman 131
 
132
			for($i = 0; $i < count ( $findedaddress ); $i ++) {
133
				array_push ( $firstshowaddress, $findedaddress [$i] );
17871 manish.sha 134
			}
135
 
17882 naman 136
			if ($defaultpin != - 1) {
137
				array_push ( $firstshowaddress, $defaultpin );
138
			}
139
 
140
			for($i = 0; $i < count ( $moreshowaddress ); $i ++) {
141
				array_push ( $firstshowaddress, $moreshowaddress [$i] );
142
			}
143
 
144
			$this->set ( 'address', $response );
145
 
146
			$this->set ( compact ( 'firstshowaddress', 'totalPayable', 'user_name', 'user_contact', 'pincode', 'defaultselectedaddress', 'message' ) );
17793 naman 147
		}
17882 naman 148
	}
149
	public function add($user_name = null, $user_contact = null) {
150
		$this->layout = 'cartinnerpages';
17793 naman 151
 
17882 naman 152
		$totalPayable = $_SESSION ['totalPayable'];
153
		$pinval = base64_decode ( $_COOKIE ['s_pincode'] );
154
		;
155
		$suserId = base64_decode ( $_COOKIE ['s_id'] );
156
		$scart = base64_decode ( $_COOKIE ['s_cart'] );
157
 
158
		if ($this->request->is ( 'post' )) {
17793 naman 159
			$data = $this->request->data;
17882 naman 160
 
161
			$senddata ['name'] = $data ['name'];
162
			$senddata ['line1'] = $data ['line1'];
163
			$senddata ['line2'] = "";
164
			$senddata ['city'] = $data ['city'];
165
			$senddata ['state'] = $data ['state'];
166
			$senddata ['pin'] = $data ['pin'];
167
			$senddata ['phone'] = $data ['phone'];
168
			$senddata ['country'] = 'India';
169
			$url = Configure::read ( 'saholicapihost' ) . "address?userId=" . $suserId . "&isLoggedIn=true&cartId=" . $scart . "&isPrivateDealUser=true";
170
 
171
			$response = $this->post_request ( $url, $senddata );
172
 
173
			$scartId = base64_decode ( $_COOKIE ['s_cart'] );
174
			$semailId = base64_decode ( $_COOKIE ['s_email'] );
175
			$blank = array ();
176
			$postData = array (
177
					'cartItems' => $blank 
178
			);
179
 
180
			$params = array (
181
					'cartMap' => urlencode ( json_encode ( $postData ) ) 
182
			);
183
			$url = Configure::read ( 'saholicapihost' ) . 'cart!validateCart?isLoggedIn=true&privateDealUser=true&userId=' . $suserId . '&id=' . $scartId . '&email=' . $semailId;
184
			// if($pin!='0'){
185
			$url = $url . '&pinCode=' . $data ['pin'];
186
			// }
187
			$cartskus = $this->post_cartinfo_request ( $url, $params );
188
			$checkestimate = 'true';
189
			if ($cartskus ['maxEstimate'] == - 1) {
190
				$checkestimate = 'false';
191
			} else {
192
				for($i = 0; $i < count ( $cartskus ['cartItems'] ); $i ++) {
17904 amit.gupta 193
					if(array_key_exists('estimate',$cartskus ['cartItems'] [$i])){
194
						continue;
195
					}
196
					$estimate = $cartskus ['cartItems'] [$i] ['estimate'];
197
					if ($estimate==-1) {
17882 naman 198
						$checkestimate = 'false';
199
						break;
200
					}
201
				}
17793 naman 202
			}
203
 
17882 naman 204
			if ($checkestimate == 'false') {
205
				// debug("I am here");
206
				$this->Session->setFlash ( "Address is not serviceable", 'default', array (
207
						'class' => 'alert alert-danger custom_class' 
208
				) );
209
				// $this->Session->setFlash('Address is not serviceable');
210
				return $this->redirect ( array (
211
						'action' => 'index' 
212
				) );
213
			} else {
214
				$this->Session->setFlash ( "Address is  serviceable" );
17793 naman 215
			}
216
		}
217
 
17882 naman 218
		$url = $this->apihost . "pincodeValidation/" . $pinval;
219
		$getstate = $this->make_request ( $url, null );
17844 naman 220
		// debug($getstate);
17793 naman 221
		$getstateval = "";
17882 naman 222
		if ($getstate != "{}") {
223
			$getstateval = ucwords ( strtolower ( $getstate ['state'] ) );
17793 naman 224
		}
17882 naman 225
		$this->set ( compact ( 'getstateval', 'user_name', 'user_contact', 'pinval', 'totalPayable' ) );
17793 naman 226
	}
17882 naman 227
	public function submitcheck() {
228
		$this->autoRender = false;
229
		$this->request->onlyAllow ( 'ajax' );
230
 
17899 amit.gupta 231
		$suserId = base64_decode($_COOKIE['s_id']);
232
		$scartId = base64_decode($_COOKIE['s_cart']);
233
		$semailId = base64_decode($_COOKIE['s_email']);
234
 
17882 naman 235
		$data = $this->request->data;
236
 
237
		$senddata ['name'] = $data ['name'];
238
		$senddata ['line1'] = $data ['line1'];
239
		$senddata ['line2'] = "";
240
		$senddata ['city'] = $data ['city'];
241
		$senddata ['state'] = $data ['state'];
242
		$senddata ['pin'] = $data ['pin'];
243
		$senddata ['phone'] = $data ['phone'];
244
		$senddata ['country'] = 'India';
17899 amit.gupta 245
		$url = Configure::read ( 'saholicapihost' ) . "address?userId=" . $suserId . "&isLoggedIn=true&cartId=" . $scartId . "&isPrivateDealUser=true";
17882 naman 246
 
17899 amit.gupta 247
		$response = $this->post_cartinfo_request($url,$senddata);
17882 naman 248
		$suserId = base64_decode ( $_COOKIE ['s_id'] );
249
 
250
		$scartId = base64_decode ( $_COOKIE ['s_cart'] );
251
		$semailId = base64_decode ( $_COOKIE ['s_email'] );
252
		$blank = array ();
253
		$postData = array (
254
				'cartItems' => $blank 
255
		);
256
 
257
		$params = array (
258
				'cartMap' => urlencode ( json_encode ( $postData ) ) 
259
		);
260
		$url = Configure::read ( 'saholicapihost' ) . 'cart!validateCart?isLoggedIn=true&privateDealUser=true&userId=' . $suserId . '&id=' . $scartId . '&email=' . $semailId;
261
		// if($pin!='0'){
262
		$url = $url . '&pinCode=' . $data ['pin'];
263
		// }
264
		$cartskus = $this->post_cartinfo_request ( $url, $params );
17893 amit.gupta 265
		$checkestimate = $response ['defaultAddress'];
17882 naman 266
		if ($cartskus ['maxEstimate'] == - 1) {
17893 amit.gupta 267
			$checkestimate = '';
17882 naman 268
		} else {
269
			for($i = 0; $i < count ( $cartskus ['cartItems'] ); $i ++) {
17904 amit.gupta 270
				if(array_key_exists('estimate',$cartskus ['cartItems'] [$i])){
271
					continue;
272
				}
273
				$estimate = $cartskus ['cartItems'] [$i] ['estimate'];
274
				if ($estimate==-1) {
17893 amit.gupta 275
					$checkestimate = '';
17882 naman 276
					break;
277
				}
278
			}
279
		}
280
 
281
		return $checkestimate;
282
	}
283
	public function getstate($pin_val) {
284
		$this->autoRender = false;
285
		$this->request->onlyAllow ( 'ajax' );
286
		$url = $this->apihost . "pincodeValidation/" . $pin_val;
287
		$getstate = $this->make_request ( $url, null );
288
		// $getstate['state'] = ucwords(strtolower($getstate['state']));
17844 naman 289
		$nothing = "nothing";
17882 naman 290
		if ($getstate != "{}") {
291
			$getstate ['state'] = ucwords ( strtolower ( $getstate ['state'] ) );
17844 naman 292
		}
17882 naman 293
		return json_encode ( $getstate );
294
	}
295
	public function isServicable($pin) {
296
		$this->autoRender = false;
297
		$this->request->onlyAllow ( 'ajax' );
17793 naman 298
		// $url = "http://shop2020.in:8080/mobileapi/address!serviceable?pincode=".$pin;
299
		// $response = $this->make_request($url,null);
17882 naman 300
 
301
		$scartId = base64_decode ( $_COOKIE ['s_cart'] );
302
		$suserId = base64_decode ( $_COOKIE ['s_id'] );
303
		$semailId = base64_decode ( $_COOKIE ['s_email'] );
304
		$blank = array ();
305
		$postData = array (
306
				'cartItems' => $blank 
307
		);
308
 
309
		$params = array (
310
				'cartMap' => urlencode ( json_encode ( $postData ) ) 
311
		);
312
		$url = Configure::read ( 'saholicapihost' ) . 'cart!validateCart?isLoggedIn=true&privateDealUser=true&userId=' . $suserId . '&id=' . $scartId . '&email=' . $semailId;
313
		$url = $url . '&pinCode=' . $pin;
314
		$cartskus = $this->post_cartinfo_request ( $url, $params );
315
		$checkestimate = 'true';
316
		if ($cartskus ['maxEstimate'] == - 1) {
317
			$checkestimate = 'false';
318
		} else {
319
			for($i = 0; $i < count ( $cartskus ['cartItems'] ); $i ++) {
17904 amit.gupta 320
				if(array_key_exists('estimate',$cartskus ['cartItems'] [$i])){
321
					continue;
322
				}
323
				$estimate = $cartskus ['cartItems'] [$i] ['estimate'];
324
				if ($estimate==-1) {
17882 naman 325
					$checkestimate = 'false';
326
					break;
327
				}
328
			}
329
		}
330
 
331
		return $checkestimate;
332
	}
333
 
17871 manish.sha 334
	 public function checkout(){
335
	 	$addressid = json_decode($this->request->data['addressid']);
17846 manish.sha 336
	 	$pincode = 0;
17871 manish.sha 337
		$cod = $this->request->query('cod');
17846 manish.sha 338
		$userId = $this->request->query('user_id');
339
 
340
		if(isset($this->request->data->pincode)){
341
			$pincode = $this->request->data->pincode;
342
		}
343
		if($pincode==0 && isset($_COOKIE['s_pincode'])){
344
			$pincode = base64_decode($_COOKIE['s_pincode']);
345
		}
346
 
17871 manish.sha 347
		$suserId = base64_decode($_COOKIE['s_id']);
348
		$scartId = base64_decode($_COOKIE['s_cart']);
349
		$semailId = base64_decode($_COOKIE['s_email']);
350
 
351
		if(isset($_COOKIE['txn_comp'])) {
352
			unset($_COOKIE['txn_comp']);
353
		}
354
 
355
		if($cod==1){
356
			$url = Configure::read('saholicapihost').'order?payment_option=3000&isLoggedIn=true&privateDealUser=true&userId='.$suserId.'&id='.$scartId.'&email='.$semailId.'&addressid='.$addressid;
357
		 	if($pincode!='0'){
358
				$url = $url.'&pinCode='.$pincode;
359
			}
360
			$orderCreationResponse = $this->post_cartinfo_request($url, null);
361
			//{"response":{"success":true,"redirectUrl":"pay-success?paymentId=998593"}}
362
			$redirecturl = $orderCreationResponse['response']['redirectUrl'];
363
			if (strpos($redirecturl,'pay-success') === false) {
17885 manish.sha 364
			    setcookie('txn_comp', 'no', -1, '/');
17871 manish.sha 365
			}
366
			$this->layout = 'innerpages';
367
			$next = $redirecturl;					
368
			$redirectUrl = $this->getAutoLoginUrl($userId,$next);
369
			$this->log($redirectUrl,'headers');
370
			$this->set(compact('redirectUrl','next'));
371
		}else{
372
			$dataGiven = json_decode($this->request->data['cart_details']);
373
			$totalSkus= intval($dataGiven->totalSkus);
17885 manish.sha 374
			setcookie('txn_comp', 'no', -1, '/');
17871 manish.sha 375
			$this->layout = 'innerpages';
17892 manish.sha 376
			$next = "payment?cq=".$totalSkus.'&addressid='.$addressid;					
17871 manish.sha 377
			$redirectUrl = $this->getAutoLoginUrl($userId,$next);
378
			$this->log($redirectUrl,'headers');
379
			$this->set(compact('redirectUrl','next'));
380
		}
381
	 }
17793 naman 382
}