Subversion Repositories SmartDukaan

Rev

Rev 17890 | Rev 17893 | 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 ++) {
193
					if ($cartskus ['cartItems'] [$i] ['estimate'] == - 1) {
194
						$checkestimate = 'false';
195
						break;
196
					}
197
				}
17793 naman 198
			}
199
 
17882 naman 200
			if ($checkestimate == 'false') {
201
				// debug("I am here");
202
				$this->Session->setFlash ( "Address is not serviceable", 'default', array (
203
						'class' => 'alert alert-danger custom_class' 
204
				) );
205
				// $this->Session->setFlash('Address is not serviceable');
206
				return $this->redirect ( array (
207
						'action' => 'index' 
208
				) );
209
			} else {
210
				$this->Session->setFlash ( "Address is  serviceable" );
17793 naman 211
			}
212
		}
213
 
17882 naman 214
		$url = $this->apihost . "pincodeValidation/" . $pinval;
215
		$getstate = $this->make_request ( $url, null );
17844 naman 216
		// debug($getstate);
17793 naman 217
		$getstateval = "";
17882 naman 218
		if ($getstate != "{}") {
219
			$getstateval = ucwords ( strtolower ( $getstate ['state'] ) );
17793 naman 220
		}
17882 naman 221
		$this->set ( compact ( 'getstateval', 'user_name', 'user_contact', 'pinval', 'totalPayable' ) );
17793 naman 222
	}
17882 naman 223
	public function submitcheck() {
224
		$this->autoRender = false;
225
		$this->request->onlyAllow ( 'ajax' );
226
 
227
		$data = $this->request->data;
228
 
229
		$senddata ['name'] = $data ['name'];
230
		$senddata ['line1'] = $data ['line1'];
231
		$senddata ['line2'] = "";
232
		$senddata ['city'] = $data ['city'];
233
		$senddata ['state'] = $data ['state'];
234
		$senddata ['pin'] = $data ['pin'];
235
		$senddata ['phone'] = $data ['phone'];
236
		$senddata ['country'] = 'India';
237
		$url = Configure::read ( 'saholicapihost' ) . "address?userId=" . $suserId . "&isLoggedIn=true&cartId=" . $scart . "&isPrivateDealUser=true";
238
 
17887 amit.gupta 239
		$response = $this->post_request($url,$senddata);
17882 naman 240
		$suserId = base64_decode ( $_COOKIE ['s_id'] );
241
 
242
		$scartId = base64_decode ( $_COOKIE ['s_cart'] );
243
		$semailId = base64_decode ( $_COOKIE ['s_email'] );
244
		$blank = array ();
245
		$postData = array (
246
				'cartItems' => $blank 
247
		);
248
 
249
		$params = array (
250
				'cartMap' => urlencode ( json_encode ( $postData ) ) 
251
		);
252
		$url = Configure::read ( 'saholicapihost' ) . 'cart!validateCart?isLoggedIn=true&privateDealUser=true&userId=' . $suserId . '&id=' . $scartId . '&email=' . $semailId;
253
		// if($pin!='0'){
254
		$url = $url . '&pinCode=' . $data ['pin'];
255
		// }
256
		$cartskus = $this->post_cartinfo_request ( $url, $params );
257
		$checkestimate = 'true';
258
		if ($cartskus ['maxEstimate'] == - 1) {
259
			$checkestimate = 'false';
260
		} else {
261
			for($i = 0; $i < count ( $cartskus ['cartItems'] ); $i ++) {
262
				if ($cartskus ['cartItems'] [$i] ['estimate'] == - 1) {
263
					$checkestimate = 'false';
264
					break;
265
				}
266
			}
267
		}
268
 
269
		if ($checkestimate == 'false') {
270
			setcookie ( 'address_message', "Address is not Servicable", - 1, '/' );
271
		}
272
 
273
		return $checkestimate;
274
	}
275
	public function getstate($pin_val) {
276
		$this->autoRender = false;
277
		$this->request->onlyAllow ( 'ajax' );
278
		$url = $this->apihost . "pincodeValidation/" . $pin_val;
279
		$getstate = $this->make_request ( $url, null );
280
		// $getstate['state'] = ucwords(strtolower($getstate['state']));
17844 naman 281
		$nothing = "nothing";
17882 naman 282
		if ($getstate != "{}") {
283
			$getstate ['state'] = ucwords ( strtolower ( $getstate ['state'] ) );
17844 naman 284
		}
17882 naman 285
		return json_encode ( $getstate );
286
	}
287
	public function isServicable($pin) {
288
		$this->autoRender = false;
289
		$this->request->onlyAllow ( 'ajax' );
17793 naman 290
		// $url = "http://shop2020.in:8080/mobileapi/address!serviceable?pincode=".$pin;
291
		// $response = $this->make_request($url,null);
17882 naman 292
 
293
		$scartId = base64_decode ( $_COOKIE ['s_cart'] );
294
		$suserId = base64_decode ( $_COOKIE ['s_id'] );
295
		$semailId = base64_decode ( $_COOKIE ['s_email'] );
296
		$blank = array ();
297
		$postData = array (
298
				'cartItems' => $blank 
299
		);
300
 
301
		$params = array (
302
				'cartMap' => urlencode ( json_encode ( $postData ) ) 
303
		);
304
		$url = Configure::read ( 'saholicapihost' ) . 'cart!validateCart?isLoggedIn=true&privateDealUser=true&userId=' . $suserId . '&id=' . $scartId . '&email=' . $semailId;
305
		$url = $url . '&pinCode=' . $pin;
306
		$cartskus = $this->post_cartinfo_request ( $url, $params );
307
		$checkestimate = 'true';
308
		if ($cartskus ['maxEstimate'] == - 1) {
309
			$checkestimate = 'false';
310
		} else {
311
			for($i = 0; $i < count ( $cartskus ['cartItems'] ); $i ++) {
312
				if ($cartskus ['cartItems'] [$i] ['estimate'] == - 1) {
313
					$checkestimate = 'false';
314
					break;
315
				}
316
			}
317
		}
318
 
319
		return $checkestimate;
320
	}
321
 
17871 manish.sha 322
	 public function checkout(){
323
	 	$addressid = json_decode($this->request->data['addressid']);
17846 manish.sha 324
	 	$pincode = 0;
17871 manish.sha 325
		$cod = $this->request->query('cod');
17846 manish.sha 326
		$userId = $this->request->query('user_id');
327
 
328
		if(isset($this->request->data->pincode)){
329
			$pincode = $this->request->data->pincode;
330
		}
331
		if($pincode==0 && isset($_COOKIE['s_pincode'])){
332
			$pincode = base64_decode($_COOKIE['s_pincode']);
333
		}
334
 
17871 manish.sha 335
		$suserId = base64_decode($_COOKIE['s_id']);
336
		$scartId = base64_decode($_COOKIE['s_cart']);
337
		$semailId = base64_decode($_COOKIE['s_email']);
338
 
339
		if(isset($_COOKIE['txn_comp'])) {
340
			unset($_COOKIE['txn_comp']);
341
		}
342
 
343
		if($cod==1){
344
			$url = Configure::read('saholicapihost').'order?payment_option=3000&isLoggedIn=true&privateDealUser=true&userId='.$suserId.'&id='.$scartId.'&email='.$semailId.'&addressid='.$addressid;
345
		 	if($pincode!='0'){
346
				$url = $url.'&pinCode='.$pincode;
347
			}
348
			$orderCreationResponse = $this->post_cartinfo_request($url, null);
349
			//{"response":{"success":true,"redirectUrl":"pay-success?paymentId=998593"}}
350
			$redirecturl = $orderCreationResponse['response']['redirectUrl'];
351
			if (strpos($redirecturl,'pay-success') === false) {
17885 manish.sha 352
			    setcookie('txn_comp', 'no', -1, '/');
17871 manish.sha 353
			}
354
			$this->layout = 'innerpages';
355
			$next = $redirecturl;					
356
			$redirectUrl = $this->getAutoLoginUrl($userId,$next);
357
			$this->log($redirectUrl,'headers');
358
			$this->set(compact('redirectUrl','next'));
359
		}else{
360
			$dataGiven = json_decode($this->request->data['cart_details']);
361
			$totalSkus= intval($dataGiven->totalSkus);
17885 manish.sha 362
			setcookie('txn_comp', 'no', -1, '/');
17871 manish.sha 363
			$this->layout = 'innerpages';
17892 manish.sha 364
			$next = "payment?cq=".$totalSkus.'&addressid='.$addressid;					
17871 manish.sha 365
			$redirectUrl = $this->getAutoLoginUrl($userId,$next);
366
			$this->log($redirectUrl,'headers');
367
			$this->set(compact('redirectUrl','next'));
368
		}
369
	 }
17793 naman 370
}