Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
13532 anikendra 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Categories Controller
5
 *
6
 * @property Category $Category
7
 * @property PaginatorComponent $Paginator
8
 */
9
class CategoriesController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
13794 anikendra 17
	public $apihost;
18
	public $limit;
13532 anikendra 19
 
13596 anikendra 20
	public function beforeFilter() {
21
		parent::beforeFilter();
22
		$this->Auth->allow('deals');
13794 anikendra 23
		$this->apihost = Configure::read('pythonapihost');
24
		$this->limit = Configure::read('dealsperpage');
18223 naman 25
		$this->mobileapi = Configure::read('saholicapihost');
19244 amit.gupta 26
		setcookie("fresh", '1', null, '/');
18223 naman 27
 
13596 anikendra 28
	}
29
 
13532 anikendra 30
/**
31
 * index method
32
 *
33
 * @return void
34
 */
35
	public function index() {
36
		// $this->Category->recursive = 0;
37
		// $this->set('categories', $this->Paginator->paginate());
13541 anikendra 38
		$userId = $this->request->query('user_id');
39
		$this->loadModel('UserCategory');
13689 anikendra 40
		$options = array('conditions' => array('user_id'=>$userId),'order'=>array('rank','asc'),'recursive'=>-1);
13541 anikendra 41
		$userCategories = $this->UserCategory->find('all',$options);		
13532 anikendra 42
		$this->response->type('json');
43
		$this->layout = 'ajax';
13541 anikendra 44
		$conditions = array(array('Category.parent_id !='=>0));
45
		if(!empty($userCategories)){
46
			foreach ($userCategories as $key => $value) {
47
				$categoryIds[] = $value['UserCategory']['category_id'];
48
			}
49
			array_push($conditions,array('Category.id'=>$categoryIds));
50
		}
51
		$this->Category->recursive = -1;		
13532 anikendra 52
		$categories = $this->Paginator->paginate(null,$conditions);
53
		$callback = $this->request->query('callback');
54
		$result = array('categories' => $categories);
55
		$this->set(array(
56
		    'result' => $result,
57
		    'callback' => $callback,
58
		    '_serialize' => array('result')
59
		));
60
		$this->render('/Elements/jsonp');
61
	}
62
 
13567 anikendra 63
	public function deals(){
13591 anikendra 64
		$userId = $this->request->query('user_id');
17962 manish.sha 65
		$error = $this->request->query('error');
13591 anikendra 66
		if(isset($userId) && !empty($userId)){
13597 anikendra 67
			$this->loadModel('User');
68
			$dbuser = $this->User->findById($userId);
69
			$this->Auth->login($dbuser['User']);	
13591 anikendra 70
		}
13583 anikendra 71
		$likedDeals = $disLikedDeals = array();
13794 anikendra 72
		// $this->loadModel('Api');
73
		// $apideals = $this->Api->getCategoryDeals($this->Auth->User('id'),1);
74
		// $categorydeals = $apideals['products'];
75
		$page = $this->request->query('page');
76
		if(!isset($page)){
77
			$page = 1;
78
		}
79
		$offset = ($page - 1) * $this->limit;
80
		$url = $this->apihost.'deals/'.$this->Auth->User('id').'?categoryId=0&limit='.$this->limit.'&offset='.$offset;
17683 naman 81
		//debug($url);
13794 anikendra 82
		$deals = $this->make_request($url,null);		
13583 anikendra 83
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));		
84
		if(!empty($myactions)) {
85
			foreach ($myactions['actions'] as $key => $value) {
86
				if($value['UserAction']['action'] == 'like'){
87
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
88
				}else{
89
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
90
				}
91
			}
92
		}
17962 manish.sha 93
 
94
		$errorstr = '';
95
 
96
		if(isset($error)){
97
			$errorstr = 'Oops!! Some Error Occured. <br> Please try after Some Time';
98
		}
13567 anikendra 99
		$rows = sizeof($categorydeals);
100
		if($rows>=1){
101
			$this->set('deals',$categorydeals);
13583 anikendra 102
			$this->set('likedDeals',$likedDeals);
103
			$this->set('disLikedDeals',$disLikedDeals);
17962 manish.sha 104
			$this->set('errorstr',$errorstr);
13567 anikendra 105
			$this->render('categorydeals');
106
		}else{
107
			foreach ($categorydeals as $key => $dealarr) {
108
				foreach ($dealarr as $key => $deal) {
109
					$deals[] = $deal[0];
110
				}				
111
			}
17962 manish.sha 112
			$this->set(compact('deals','likedDeals','disLikedDeals','errorstr'));
13567 anikendra 113
		}		
114
	}
115
/*
116
	*
13532 anikendra 117
 * view method
118
 *
119
 * @throws NotFoundException
120
 * @param string $id
121
 * @return void
122
 */
123
	public function view($id = null) {
18393 amit.gupta 124
		$userId = $this->request->query('user_id');
15378 anikendra 125
		if(isset($userId) && !empty($userId)) {
14930 anikendra 126
			$this->loadModel('User');
127
			$dbuser = $this->User->findById($userId);
128
			$this->Auth->login($dbuser['User']);
19345 naman 129
		}			
130
 
18390 naman 131
		//Sort - type, ordering
132
		$sort = $this->request->query('sort');
133
		$direction = $this->request->query('direction');
134
 
135
		//Filter - brand/subcategory		
136
		$brands = $this->request->query('brands');
137
		$subcategories = $this->request->query('subcategories');
138
 
18655 naman 139
		$searchableSubCategories = array('20'=>'Tempered Glasses', '27'=>'Back Cover', '29'=>'Batteries');
18390 naman 140
 
13583 anikendra 141
		$page = $this->request->query('page');
18063 naman 142
		$searchfor = $this->request->query('searchFor');
143
		// echo "page=>",$page;
17810 manish.sha 144
		$error = $this->request->query('error');
13583 anikendra 145
		if(!isset($page)){
146
			$page = 1;
16549 anikendra 147
		}
148
 
18207 amit.gupta 149
		//$filter = $this->request->query('filter');
15044 anikendra 150
		// $brands = $this->request->query('brands');
16549 anikendra 151
		if($id != 2) {
152
			//Fetch deals
153
			$likedDeals = $disLikedDeals = array();
18390 naman 154
			if(!empty($brands) && !empty($subcategories)){
155
				$filter = 'brand|subcategory';
156
			}			
157
			else if(!empty($brands)){
158
				$filter = 'brand';
159
			}
160
			else if(!empty($subcategories)){
161
				$filter = 'subcategory';
162
			}
17759 naman 163
 
18390 naman 164
			$sortlabel = 'recommended';
165
			if	($sort=='bestSellerPoints'){
166
				$sortlabel  = 'bestseller';
18063 naman 167
			}
18390 naman 168
			else if ($sort=='available_price' && $direction==-1){
169
				$sortlabel  = 'pricehigh';
16549 anikendra 170
			}
18390 naman 171
			else if ($sort=='available_price' && $direction==1){
172
				$sortlabel  = 'pricelow';
173
			}
17683 naman 174
 
175
			$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands,$subcategories);
16549 anikendra 176
			$response = $this->make_request($url,null);
177
			$deals = array();
17683 naman 178
 
179
			$response_count =1;
180
			if($response == '')
181
			{
182
				$response_count = 0;
183
			}
184
			// debug($response_count);
16549 anikendra 185
			if(!empty($response)){
186
				foreach ($response as $key => $value) {
187
					if(!empty($value)){
188
						$deals[] = $value;
189
					}
16098 anikendra 190
				}
191
			}
17683 naman 192
			#print_r($deals);
16549 anikendra 193
			$this->loadModel('Api');
194
			$myactions = $this->Api->getMyActions($this->Auth->User('id'));
195
			if(!empty($myactions)) {
196
				foreach ($myactions['actions'] as $key => $value) {
197
					if($value['UserAction']['action'] == 'like'){
198
						$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
199
					}else{
200
						$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
201
					}
13583 anikendra 202
				}
203
			}
16549 anikendra 204
			$this->loadModel('NotificationRule');
205
			$notification = $this->NotificationRule->getNotification($this->Auth->User('id'));	
206
		    $filterstr = '';
207
			if(isset($filter) && !empty($filter)){
17759 naman 208
 
17683 naman 209
			    $filterstr = '&filter='.$filter.'&brands='.$brands.'&subcategories='.$subcategories;
17695 naman 210
		  	} 
211
		  	$get_url = "'".$_SERVER['REQUEST_URI']."'";
212
		  	$urlArray = explode('=',$_SERVER['REQUEST_URI']);
213
		  	$last = $urlArray[sizeof($urlArray)-1];
214
 
17810 manish.sha 215
			$errorstr = '';
216
 
217
			if(isset($error)){
218
				$errorstr = 'Oops!! Some Error Occured. <br> Please try after Some Time';
219
			}
17759 naman 220
 
18390 naman 221
			$filterstr = '&brands='.$brands.'&subcategories='.$subcategories;
222
			$nexturl = "/categories/getdeals/".$id."/?page=".($page+1)."&sort=".$sort."&direction=".$direction."".$filterstr;			
18063 naman 223
 
19306 naman 224
			//check for offer start
225
			$user_id = $this->Auth->user('id');
226
			$cachekey = 'target-'.$user_id;
227
			$getoffer = Cache::read($cachekey,'target');
19327 naman 228
			$current_time = time();
19306 naman 229
			$offerresponse = "";
230
			if($getoffer === false){
19327 naman 231
				$offerurl = $this->apihost."getOfferForUser/?user_id=".$user_id;
19306 naman 232
				$offerresponse = $this->make_request($offerurl,null);
233
				Cache::write($cachekey , $offerresponse ,'target');
234
				if(!empty($offerresponse)){					
19327 naman 235
 
19306 naman 236
								if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
237
 
238
								}
239
								else{
240
									$offerresponse = "";
241
								}
242
				}
243
				else{
244
					$offerresponse = "";
245
				}
246
			}else{
19327 naman 247
				if(!empty($getoffer)){
248
					$offerresponse = $getoffer;
249
					if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
250
 
251
					}
252
					else{
253
						$offerresponse = "";
254
					}
255
				}
256
 
19306 naman 257
			}
258
 
259
			//check for offer end 
260
 
261
 
19345 naman 262
			$this->set(compact('user_id','offerresponse','response_count','deals','id','likedDeals','disLikedDeals','page','sort','direction','notification','filter','brands','filterstr','subcategories','errorstr','nexturl','searchfor', 'searchableSubCategories', 'sortlabel'));
16549 anikendra 263
		}else{
16583 anikendra 264
			//Check for apk support of sharing
265
			$sharable = 0;
266
			if(isset($_COOKIE['shareApps']) && !empty($_COOKIE['shareApps'])) {
267
				$sharable = 1;
17183 anikendra 268
			}			
16704 anikendra 269
			$url = $this->apihost."appOffers/1";
270
			$appOffers = $this->make_request($url,null);
271
			$this->set(compact('page','id','sharable','appOffers'));
16549 anikendra 272
			$this->render('viewapps');
13583 anikendra 273
		}
13532 anikendra 274
	}
275
 
16549 anikendra 276
	public function getapps($id=null){
277
		$this->layout = 'ajax';
278
		$page = $this->request->query('page');
279
		if(!isset($page)){
280
			$page = 1;
281
		}
282
		$this->loadModel('AppOffer');
283
		$this->AppOffer->recursive = -1;
284
		$options = array('conditions'=>array('offer_active'=>1,'show'=>1),'limit' => Configure::read('searchresultsperpage'),'page'=>$page);
285
		$this->Paginator->settings = $options;
286
		try{
287
			$appOffers = $this->Paginator->paginate('AppOffer');
288
		} catch (NotFoundException $e) {
289
	        //get current page
290
	        $page = $this->request->params['named']['page'];
291
	     	$appOffers = array();   
292
		}
293
		debug($appOffers);		
294
		if(!empty($appOffers)){
295
			$this->set(compact('page','id','appOffers'));
296
			$this->render('/Elements/appoffers');
297
		}else{
298
			$this->render('/Elements/nooffers');
299
		}		
300
	}
301
 
13579 anikendra 302
	public function getdeals($id = null) {
17695 naman 303
 
13808 anikendra 304
		$this->log('getdeal id '.$id,'api');
13583 anikendra 305
		$likedDeals = $disLikedDeals = array();
13579 anikendra 306
		$this->layout = 'ajax';
307
		$page = $this->request->query('page');
308
		if(!isset($page)){
309
			$page = 1;
310
		}
13808 anikendra 311
		$sort = $this->request->query('sort');
312
		$direction = $this->request->query('direction');
15044 anikendra 313
		// $filter = $this->request->query('filter');
314
		// $brands = $this->request->query('brands');
18390 naman 315
		$brands = $this->request->query('brands');
316
		$subcategories = $this->request->query('subcategories'); 	
17759 naman 317
 
18390 naman 318
		$likedDeals = $disLikedDeals = array();
319
		if(!empty($brands) && !empty($subcategories)){
17759 naman 320
			$filter = 'brand|subcategory';
18390 naman 321
		}
322
		else if(!empty($brands)){
15044 anikendra 323
			$filter = 'brand';
324
		}
18390 naman 325
		else if(!empty($subcategories)){
17759 naman 326
			$filter = 'subcategory';
327
		}
18390 naman 328
 
17759 naman 329
		$url = $this->getDealsApiUrl($page,$this->Auth->User('id'),$id,$sort,$direction,$filter,$brands,$subcategories);
16098 anikendra 330
		$response = $this->make_request($url,null);
331
		$deals = array();
332
		if(!empty($response)){
333
			foreach ($response as $key => $value) {
334
				if(!empty($value)){
335
					$deals[] = $value;
336
				}
337
			}
338
		}
13794 anikendra 339
		// if (!$this->Category->exists($id)) {
340
			// throw new NotFoundException(__('Invalid category'));
341
		// }
13579 anikendra 342
		$this->loadModel('Api');
13794 anikendra 343
		// $apideals = $this->Api->getDealsByCategory($this->Auth->User('id'),$id,$page);
344
		// $deals = $apideals['products'];
13583 anikendra 345
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
346
		if(!empty($myactions)) {
347
			foreach ($myactions['actions'] as $key => $value) {
348
				if($value['UserAction']['action'] == 'like'){
349
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
350
				}else{
351
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
352
				}
353
			}
354
		}
18390 naman 355
 
356
		$filterstr = '&brands='.$brands.'&subcategories='.$subcategories;
357
		$nexturl = "/categories/getdeals/".$id."/?page=".($page+1)."&sort=".$sort."&direction=".$direction."".$filterstr;
19306 naman 358
 
359
		//check for offer start
19327 naman 360
			$user_id = $this->Auth->user('id');
361
			$cachekey = 'target-'.$user_id;
362
			$getoffer = Cache::read($cachekey,'target');
363
			$current_time = time();
364
			$offerresponse = "";
365
			if($getoffer === false){
366
				$offerurl = $this->apihost."getOfferForUser/?user_id=".$user_id;
367
				$offerresponse = $this->make_request($offerurl,null);
368
				Cache::write($cachekey , $offerresponse ,'target');
369
				if(!empty($offerresponse)){					
370
 
371
								if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
372
 
373
								}
374
								else{
375
									$offerresponse = "";
376
								}
377
				}
378
				else{
379
					$offerresponse = "";
380
				}
381
			}else{
382
				if(!empty($getoffer)){
383
					$offerresponse = $getoffer;
384
					if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
385
 
386
					}
387
					else{
388
						$offerresponse = "";
389
					}
390
				}
391
 
19306 naman 392
			}
19327 naman 393
 
394
			//check for offer end 
19306 naman 395
 
19345 naman 396
		$this->set(compact('$user_id','offerresponse','nexturl','deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','filterstr'));
16098 anikendra 397
		if(!empty($deals) && !empty($deals[0])){
15026 anikendra 398
			$this->render('/Elements/deals');
399
		}else{
400
			$this->render('/Elements/nodeals');
401
		}
17695 naman 402
 
403
 
404
 
13579 anikendra 405
	}
18063 naman 406
 
18212 naman 407
	public function dealdetail($item_id = null){
19244 amit.gupta 408
		setcookie("fresh", '0', null, '/');
18212 naman 409
		$this->layout = "innerpages";
18223 naman 410
		$dealinfo = $deal = array();
411
		$likedDeals = $disLikedDeals = array();
412
		$url = $this->mobileapi.'entity/'.$item_id;
413
		$dealinfo = $this->make_request($url,null);
414
		$this->loadModel('Api');
415
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
416
		if(!empty($myactions)) {
417
			foreach ($myactions['actions'] as $key => $value) {
418
				if($value['UserAction']['action'] == 'like'){
419
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
420
				}else{
421
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
422
				}
423
			}
424
		}
425
		$this->set(compact('deal','dealinfo','likedDeals','disLikedDeals'));
18212 naman 426
	}
427
 
18288 naman 428
	public function saholicdeal($proid = null){
429
		$likedDeals = $disLikedDeals = array();
430
		$this->layout = 'ajax';
431
		$deal = array();
432
		$url = $this->apihost."getDealById/".$proid;
433
		$deal = $this->make_request($url,null);
434
		$this->loadModel('Api');
435
 
436
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
437
		if(!empty($myactions)) {
438
			foreach ($myactions['actions'] as $key => $value) {
439
				if($value['UserAction']['action'] == 'like'){
440
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
441
				}else{
442
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
443
				}
444
			}
445
		}
446
 
19327 naman 447
		//check for offer start
448
			$user_id = $this->Auth->user('id');
449
			$cachekey = 'target-'.$user_id;
450
			$getoffer = Cache::read($cachekey,'target');
451
			$current_time = time();
452
			$offerresponse = "";
453
			if($getoffer === false){
454
				$offerurl = $this->apihost."getOfferForUser/?user_id=".$user_id;
455
				$offerresponse = $this->make_request($offerurl,null);
456
				Cache::write($cachekey , $offerresponse ,'target');
457
				if(!empty($offerresponse)){					
458
 
459
								if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
460
 
461
								}
462
								else{
463
									$offerresponse = "";
464
								}
465
				}
466
				else{
467
					$offerresponse = "";
468
				}
469
			}else{
470
				if(!empty($getoffer)){
471
					$offerresponse = $getoffer;
472
					if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
473
 
474
					}
475
					else{
476
						$offerresponse = "";
477
					}
478
				}
479
 
480
			}
481
 
482
			//check for offer end 
18288 naman 483
 
19345 naman 484
		$this->set(compact('$user_id','offerresponse','deal','likedDeals','disLikedDeals'));
19327 naman 485
 
18288 naman 486
		$this->render('/Elements/saholicdeal');
487
 
488
 
489
	}
490
 
18063 naman 491
	public function getdealsforsearchterm($searchterm,$page = null){
492
		$likedDeals = $disLikedDeals = array();
493
		$this->layout = 'ajax';
494
		$page = $this->request->query('page');
18390 naman 495
		$subcategories = $this->request->query('subcategories'); 
18063 naman 496
		if(!isset($page)){
497
			$page = 1;
498
			$offset = 0;
499
			$limit=10;
500
		}
501
		else{
502
			$offset = ($page*20) - 30;
503
			$limit = 20;
504
		}
505
		$sort = $this->request->query('sort');
506
		$direction = $this->request->query('direction');
507
 
18390 naman 508
		$url = $this->apihost."searchSubCategory/?offset=".$offset."&limit=".$limit."&searchTerm=".urlencode($searchterm)."&subCategoryId=".$subcategories;
18063 naman 509
		$response = $this->make_request($url,null);
510
		$deals = array();
511
		if(!empty($response)){
512
			foreach ($response as $key => $value) {
513
				if(!empty($value)){
514
					$deals[] = $value;
515
				}
516
			}
517
		}
18115 amit.gupta 518
 
18063 naman 519
		$this->loadModel('Api');
18115 amit.gupta 520
 
18063 naman 521
		$myactions = $this->Api->getMyActions($this->Auth->User('id'));
522
		if(!empty($myactions)) {
523
			foreach ($myactions['actions'] as $key => $value) {
524
				if($value['UserAction']['action'] == 'like'){
525
					$likedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
526
				}else{
527
					$disLikedDeals[$value['UserAction']['store_product_id']] = $value['UserAction']['id'];
528
				}
529
			}
530
		}
531
 
18390 naman 532
		$nexturl = "/categories/getdealsforsearchterm/".urlencode($searchterm)."/?page=".($page+1).'&subcategories='.$subcategories;
19327 naman 533
 
534
		//check for offer start
535
			$user_id = $this->Auth->user('id');
536
			$cachekey = 'target-'.$user_id;
537
			$getoffer = Cache::read($cachekey,'target');
538
			$current_time = time();
539
			$offerresponse = "";
540
			if($getoffer === false){
541
				$offerurl = $this->apihost."getOfferForUser/?user_id=".$user_id;
542
				$offerresponse = $this->make_request($offerurl,null);
543
				Cache::write($cachekey , $offerresponse ,'target');
544
				if(!empty($offerresponse)){					
545
 
546
								if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
547
 
548
								}
549
								else{
550
									$offerresponse = "";
551
								}
552
				}
553
				else{
554
					$offerresponse = "";
555
				}
556
			}else{
557
				if(!empty($getoffer)){
558
					$offerresponse = $getoffer;
559
					if($offerresponse['startDate']/1000 <= $current_time && $offerresponse['endDate']/1000 >= $current_time ){
560
 
561
					}
562
					else{
563
						$offerresponse = "";
564
					}
565
				}
566
 
567
			}
568
 
569
			//check for offer end 
570
 
19345 naman 571
		$this->set(compact('$user_id','offerresponse','deals','id','page','likedDeals','disLikedDeals','sort','direction','brands','filter','nexturl'));
18063 naman 572
		if(!empty($deals) && !empty($deals[0])){
573
			$this->render('/Elements/deals');
574
		}else{
575
			$this->render('/Elements/nodeals');
576
		}
577
 
578
 
579
	}
19306 naman 580
 
19345 naman 581
	public function target(){
19306 naman 582
		$this->layout = "innerpages";
19345 naman 583
		$user_id = $this->request->query('user_id');
584
		if(isset($user_id) && !empty($user_id)) {
585
			$this->loadModel('User');
586
			$dbuser = $this->User->findById($user_id);
587
			$this->Auth->login($dbuser['User']);
588
		}
19306 naman 589
// 		$url = $this->apihost."getOfferForUser/?user_id=47";
590
// 		$response = $this->make_request($url,null);
19345 naman 591
 
19306 naman 592
		$cachekey = 'target-'.$user_id;
593
		$getoffer = Cache::read($cachekey,'target');
19333 naman 594
		$current_time = time();
19306 naman 595
		$response = "";
596
		if($getoffer === false){
19333 naman 597
			$offerurl = $this->apihost."getOfferForUser/?user_id=".user_id;
19306 naman 598
			$response = $this->make_request($offerurl,null);
599
			Cache::write($cachekey , $response ,'target');
600
			if(!empty($response)){
19333 naman 601
 
602
								if($response['startDate']/1000 <= $current_time && $response['endDate']/1000 >= $current_time ){
19306 naman 603
 
604
								}
605
								else{
19333 naman 606
									$response = "";
19306 naman 607
									}
608
				}
609
				else{
19333 naman 610
					$response = "";
19306 naman 611
				}
612
		}else{
19333 naman 613
			if(!empty($getoffer)){
614
					$response = $getoffer;
615
					if($response['startDate']/1000 <= $current_time && $response['endDate']/1000 >= $current_time ){
616
 
617
					}
618
					else{
619
						$response = "";
620
					}
621
				}
19306 naman 622
		}
623
 
624
		$maxpercentage = 0;
19345 naman 625
		$needtoachieveval = 0;
626
		$needtoachieveper = 0;
19306 naman 627
// 		debug($response);
628
 
629
		if(isset($response['target2_cash_back_percetage']) && !empty($response['target2_cash_back_percetage'])){
630
			$maxpercentage = $response['target2_cash_back_percetage'];
631
		}
632
		else{
633
			if(isset($response['target1_cash_back_percetage']) && !empty($response['target1_cash_back_percetage'])){
634
				$maxpercentage = $response['target1_cash_back_percetage'];
635
			}
636
		}
637
 
19345 naman 638
		if(!empty($response)){
639
			if(isset($response['pending_order_value']) && isset($response['delivered_order_value']) && isset($response['target2']) && isset($response['target1'])){
640
				if(($response['pending_order_value']+$response['delivered_order_value']) < $response['target1']){
641
					$needtoachieveval = $response['target1'] - ($response['pending_order_value']+$response['delivered_order_value']);
642
					$needtoachieveper = $response['target1_cash_back_percetage'];
643
				}else if(($response['pending_order_value']+$response['delivered_order_value']) < $response['target2']) {
644
					$needtoachieveval = $response['target2'] - ($response['pending_order_value']+$response['delivered_order_value']);
645
					$needtoachieveper = $response['target2_cash_back_percetage'];
646
				}
647
 
648
			}
649
		}
650
 
19306 naman 651
		$user_email = $this->Auth->user('email');
652
		$subcat = Configure::read('arrSubCategory');
19345 naman 653
		$this->set(compact('response','maxpercentage','user_email','subcat','needtoachieveval','needtoachieveper'));
19306 naman 654
	}
13532 anikendra 655
/**
656
 * add method
657
 *
658
 * @return void
659
 */
660
	public function add() {
661
		if ($this->request->is('post')) {
662
			$this->Category->create();
663
			if ($this->Category->save($this->request->data)) {
664
				$this->Session->setFlash(__('The category has been saved.'));
665
				return $this->redirect(array('action' => 'index'));
666
			} else {
667
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
668
			}
669
		}
670
	}
671
 
672
/**
673
 * edit method
674
 *
675
 * @throws NotFoundException
676
 * @param string $id
677
 * @return void
678
 */
679
	public function edit($id = null) {
680
		if (!$this->Category->exists($id)) {
681
			throw new NotFoundException(__('Invalid category'));
682
		}
683
		if ($this->request->is(array('post', 'put'))) {
684
			if ($this->Category->save($this->request->data)) {
685
				$this->Session->setFlash(__('The category has been saved.'));
686
				return $this->redirect(array('action' => 'index'));
687
			} else {
688
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
689
			}
690
		} else {
691
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
692
			$this->request->data = $this->Category->find('first', $options);
693
		}
694
	}
695
 
696
/**
697
 * delete method
698
 *
699
 * @throws NotFoundException
700
 * @param string $id
701
 * @return void
702
 */
703
	public function delete($id = null) {
704
		$this->Category->id = $id;
705
		if (!$this->Category->exists()) {
706
			throw new NotFoundException(__('Invalid category'));
707
		}
708
		$this->request->onlyAllow('post', 'delete');
709
		if ($this->Category->delete()) {
710
			$this->Session->setFlash(__('The category has been deleted.'));
711
		} else {
712
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
713
		}
714
		return $this->redirect(array('action' => 'index'));
715
	}
716
 
717
/**
718
 * admin_index method
719
 *
720
 * @return void
721
 */
722
	public function admin_index() {
723
		$this->Category->recursive = 0;
724
		$this->set('categories', $this->Paginator->paginate());
725
	}
726
 
727
/**
728
 * admin_view method
729
 *
730
 * @throws NotFoundException
731
 * @param string $id
732
 * @return void
733
 */
734
	public function admin_view($id = null) {
735
		if (!$this->Category->exists($id)) {
736
			throw new NotFoundException(__('Invalid category'));
737
		}
738
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
739
		$this->set('category', $this->Category->find('first', $options));
740
	}
741
 
742
/**
743
 * admin_add method
744
 *
745
 * @return void
746
 */
747
	public function admin_add() {
748
		if ($this->request->is('post')) {
749
			// print_r($this->request->data);die;
750
			$this->Category->create();
751
			if ($this->Category->save($this->request->data)) {
752
				$this->Session->setFlash(__('The category has been saved.'));
753
				return $this->redirect(array('action' => 'index'));
754
			} else {
755
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
756
			}
757
		}
758
		$this->set('parent_id',$this->Category->find('list'));
759
	}
760
 
761
/**
762
 * admin_edit method
763
 *
764
 * @throws NotFoundException
765
 * @param string $id
766
 * @return void
767
 */
768
	public function admin_edit($id = null) {
769
		if (!$this->Category->exists($id)) {
770
			throw new NotFoundException(__('Invalid category'));
771
		}
772
		if ($this->request->is(array('post', 'put'))) {
773
			if ($this->Category->save($this->request->data)) {
774
				$this->Session->setFlash(__('The category has been saved.'));
775
				return $this->redirect(array('action' => 'index'));
776
			} else {
777
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
778
			}
779
		} else {
780
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
781
			$this->request->data = $this->Category->find('first', $options);
782
		}
783
	}
784
 
785
/**
786
 * admin_delete method
787
 *
788
 * @throws NotFoundException
789
 * @param string $id
790
 * @return void
791
 */
792
	public function admin_delete($id = null) {
793
		$this->Category->id = $id;
794
		if (!$this->Category->exists()) {
795
			throw new NotFoundException(__('Invalid category'));
796
		}
797
		$this->request->onlyAllow('post', 'delete');
798
		if ($this->Category->delete()) {
799
			$this->Session->setFlash(__('The category has been deleted.'));
800
		} else {
801
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
802
		}
803
		return $this->redirect(array('action' => 'index'));
804
	}}