Subversion Repositories SmartDukaan

Rev

Rev 19677 | Rev 20102 | 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
 * Orders Controller
5
 *
6
 * @property Order $Order
7
 * @property PaginatorComponent $Paginator
8
 */
9
class OrdersController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
13672 anikendra 18
	public function beforeFilter() {		
13591 anikendra 19
		parent::beforeFilter();
15093 anikendra 20
		$this->Auth->allow('add','mine','pendingcashbacks','all');
13672 anikendra 21
		$this->apihost = Configure::read('pythonapihost');
13591 anikendra 22
	}
23
 
17287 amit.gupta 24
 
25
	public function getOrderFilters($type='user') {
26
		$cachekey = 'orderfilters-'.$type;
27
		$filters = Cache::read($cachekey);
28
		if(empty($filters)) {
29
			$url = $this->apihost."orderfilters/?type=".$type;
30
			$response = $this->make_request($url, null);
31
			echo $response;
32
			if(!empty($response)){
33
				$filters = $response;
34
				Cache::write($cachekey,$filters);
35
			}
36
		}
37
        return $filters;
38
	}
39
 
13816 anikendra 40
	public function mine() {
15824 anikendra 41
		$type = $this->request->query('type');
13816 anikendra 42
		$page = $this->request->query('page');
43
		$page = isset($page)?$page:1;
13682 anikendra 44
		$userId = $this->request->query('user_id');
45
		if(isset($userId) && !empty($userId)){
46
			$this->loadModel('User');
47
			$dbuser = $this->User->findById($userId);
48
			$this->Auth->login($dbuser['User']);	
49
		}
13672 anikendra 50
		$this->layout = "innerpages";
13815 anikendra 51
		$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=$page&window=10";
15824 anikendra 52
		if(isset($type) && !empty($type)) {
53
			$url .= '&type='.$type;
54
		}
13672 anikendra 55
		$response = $this->make_request($url,null);
13815 anikendra 56
		$totalPages = $response['totalPages'];
13672 anikendra 57
		if(!empty($response['data'])){
58
			$this->set('orders',$response['data']);
59
		}
13752 anikendra 60
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
13944 anikendra 61
		$storemapping = Configure::read('storemapping');
62
		$activestores = Configure::read('activestores');
14224 anikendra 63
		$amazonorderurl = Configure::read('amazonorderurl');
64
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl'));
13672 anikendra 65
	}
66
 
15217 anikendra 67
	public function by($userId) {
68
		$page = $this->request->query('page');
69
		$page = isset($page)?$page:1;		
70
		$this->layout = "innerpages";
71
		$url = $this->apihost."storeorder/user/".$userId."?page=$page&window=10";
72
		$response = $this->make_request($url,null);
73
		$totalPages = $response['totalPages'];
74
		if(!empty($response['data'])){
75
			$this->set('orders',$response['data']);
76
		}
77
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
78
		$storemapping = Configure::read('storemapping');
79
		$activestores = Configure::read('activestores');
80
		$amazonorderurl = Configure::read('amazonorderurl');
81
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl'));
82
	}
83
 
13762 anikendra 84
	public function pendingcashbacks() {
85
		$userId = $this->request->query('user_id');
86
		if(isset($userId) && !empty($userId)){
87
			$this->loadModel('User');
88
			$dbuser = $this->User->findById($userId);
89
			$this->Auth->login($dbuser['User']);	
90
		}
91
		$this->layout = "innerpages";
17724 amit.gupta 92
		$url = $this->apihost."storeorder/user/".$userId."?page=1&window=50&searchMap=%7B\"subOrders.cashBackStatus\"%3A%20%7B\"%24in\"%3A%20%5B\"Approved\"%2C%20\"Pending\"%5D%7D%7D";
13762 anikendra 93
		$response = $this->make_request($url,null);
13993 anikendra 94
		// debug($response);
95
		$creditedOrders = $pendingOrders = $approvedOrders = array();
96
		$creditedAmount = $pendingAmount = $approvedAmount = 0;
13762 anikendra 97
		if(!empty($response['data'])){
13993 anikendra 98
			foreach ($response['data'] as $key => $order) {
14111 anikendra 99
				if(!empty($order['subOrders'])){
100
					foreach ($order['subOrders'] as $key => $suborder) {
101
						$suborder['storeId'] = $order['storeId'];
15035 amit.gupta 102
						if($order['storeId']!=4){
15036 amit.gupta 103
							$suborder['merchantOrderId'] = $order['merchantOrderId'];
104
						} else {
15035 amit.gupta 105
							$suborder['merchantOrderId'] = $suborder['merchantSubOrderId'];
106
						}
14700 anikendra 107
						if(!empty($order['orderTrackingUrl'])){
108
							$suborder['orderSuccessUrl'] = $order['orderTrackingUrl'];
109
						}
14111 anikendra 110
						switch($suborder['cashBackStatus']){
111
							case 'Approved':
112
							$approvedOrders[] = $suborder;
113
							$approvedAmount += $suborder['cashBackAmount'];
114
							break;
115
							case 'Pending':
116
							$pendingOrders[] = $suborder;
117
						}
13993 anikendra 118
					}
119
				}
120
			}
13762 anikendra 121
		}
14673 anikendra 122
		$url = $this->apihost.'pending-cashbacks/user/'.$userId;
123
		$result = $this->make_request($url,null);
124
		$pendingAmount = $result['amount'];
13993 anikendra 125
		//Get pending cashbacks
126
		$url = $this->apihost.'pending-refunds/user/'.$userId;
127
		$pendingCashbacks = $this->make_request($url,null);
128
		//Get credited cashbacks
129
		$url = $this->apihost.'refund/user/'.$userId;
14068 anikendra 130
		$creditedCashbacks = $this->make_request($url,null);
19499 naman 131
 
132
		$totalcreditedcashback = 0;
133
		if(!empty($creditedCashbacks)){
134
			$totalcreditedcashback = $creditedCashbacks['credited'];
135
		}
136
 
16893 naman 137
		$creditKeyArray = array();
138
		$creditValueArray = array();
16907 naman 139
		$total_credited_amount = 0;
14026 anikendra 140
		if(!empty($creditedCashbacks)){
13993 anikendra 141
			foreach ($creditedCashbacks['data'] as $key => $value) {
14068 anikendra 142
				$creditedAmount += $value['userAmount'];				
143
				$data = array('subOrders.batchId'=>$value['batch']);
144
				$jsonVar = json_encode($data);
145
				$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=1&window=50&searchMap=$jsonVar";
146
				$creditedOrders[$value['batch']] = $this->make_request($url,null);
16907 naman 147
				$total_credited_amount =$total_credited_amount + $value['userAmount'];
17109 naman 148
				if($value['type']== 'Order')
149
				{
150
					$creditValueArray['amount'] = $value['userAmount'];
151
					$creditValueArray['type'] = $value['type'];
152
					$creditValueArray['fortbatchid'] = $value['batch'];
153
					$creditValueArray['creditedDate'] = date('Y-m-d',strtotime($value['timestamp']));
19552 naman 154
					$creditValueArray['description'] = '';
17109 naman 155
					// $creditKeyArray[date('Y-m-d',strtotime($value['timestamp']))] = $creditValueArray;
19552 naman 156
					$creditKeyArray[$value['batch'].$value['type']] = $creditValueArray;
17109 naman 157
				}
19499 naman 158
				elseif($value['type']== 'Refund'){
159
					$creditValueArray['amount'] = $value['userAmount'];
160
					$creditValueArray['type'] = $value['type'];
161
					$creditValueArray['fortbatchid'] = $value['batch'];
162
					$creditValueArray['creditedDate'] = date('Y-m-d',strtotime($value['timestamp']));
163
					$creditValueArray['description'] = $value['description'];
19552 naman 164
					$creditKeyArray[$value['batch'].$value['type']] = $creditValueArray;
19499 naman 165
 
166
				}
167
				elseif($value['type']== 'Adjustment'){
168
					$creditValueArray['amount'] = $value['userAmount'];
169
					$creditValueArray['type'] = $value['type'];
170
					$creditValueArray['fortbatchid'] = $value['batch'];
171
					$creditValueArray['creditedDate'] = date('Y-m-d',strtotime($value['timestamp']));
172
					$creditValueArray['description'] = $value['description'];
19552 naman 173
					$creditKeyArray[$value['batch'].$value['type']] = $creditValueArray;
19499 naman 174
				}
19785 naman 175
				elseif($value['type']== 'Offer'){
176
					$creditValueArray['amount'] = $value['userAmount'];
177
					$creditValueArray['type'] = $value['type'];
178
					$creditValueArray['fortbatchid'] = $value['batch'];
179
					$creditValueArray['creditedDate'] = date('Y-m-d',strtotime($value['timestamp']));
180
					$creditValueArray['description'] = $value['description'];
181
					$creditKeyArray[$value['batch'].$value['type']] = $creditValueArray;
182
				}
13993 anikendra 183
			}
184
		}
14026 anikendra 185
		$storemapping = Configure::read('storemapping');
186
		$activestores = Configure::read('activestores');
16628 anikendra 187
		//App related cashbacks
188
		$this->loadModel('UserAppCashback');
189
		$this->loadModel('UserAppInstall');
190
		//Compute last two fortnight ids
191
		$fortnightIds = array();
192
		if(date('d',time())<=15){
16784 anikendra 193
			$fortnightIds[] = 2*(date('m',time())-1)-1;
16629 anikendra 194
			$fortnightIds[] = 2*(date('m',time())-1);
16628 anikendra 195
		}else{
16784 anikendra 196
			$fortnightIds[] = 2*(date('m',time())-1);
16628 anikendra 197
			$fortnightIds[] = 2*(date('m',time())-1)+1;
198
		}
199
		$cashBacks = array();
16893 naman 200
 
201
		// Approved Start
202
 
16840 naman 203
		$url = $this->apihost.'appUserCashBack/'.$userId.'/Approved';
204
		$getapproved = $this->make_request($url,null);
205
		$fortnight = array();
206
		$fortnight_amount = array();
207
		$counter = 0;
208
		$total_approved_amount = 0;
209
		$current_date =  date("Y");
210
		foreach ($getapproved["UserAppCashBack"] as $key => $value) {
211
			 $fortnight[$counter] = $value["fortnightOfYear"];
212
			 $fortnight_amount[$counter] = $value["amount"];
213
			 $total_approved_amount += $value["amount"];
214
			 $counter++;
215
		}
216
		$approvedFortnight = array();
217
		for($i=0; $i<count($fortnight); $i++){
18172 manish.sha 218
			$url = $this->apihost.'appUserBatchDrillDown/'.$userId.'/'.$getapproved["UserAppCashBack"][$i]['fortnightOfYear'].'/'.$getapproved["UserAppCashBack"][$i]['yearVal'];
16983 naman 219
			$approvedFortnight[$i] = $this->make_request($url,null);
16840 naman 220
		// $url = $this->apihost.'appUserBatchDrillDown/1/16/2015';
221
		// $approvedFortnight[] = $this->make_request($url,null);
222
		}
16983 naman 223
		// debug($approvedFortnight);
16840 naman 224
		$this->set(compact('fortnight','total_approved_amount','fortnight_amount','approvedFortnight'));		
16893 naman 225
	// Approved End	
16840 naman 226
 
16893 naman 227
	// App Credit Start
228
		$creditedFortnight = array();
229
		$url = $this->apihost.'appUserCashBack/'.$userId.'/Credited';
230
		$getcredited = $this->make_request($url,null);
17110 naman 231
		// debug($getcredited);
16893 naman 232
		foreach ($getcredited['UserAppCashBack'] as $key => $value) {
16840 naman 233
 
17109 naman 234
 
235
 
16893 naman 236
			$url = $this->apihost.'appUserBatchDrillDown/'.$userId.'/'.$value['fortnightOfYear'].'/'.$value['yearVal'];
237
			$creditedFortnight[$value['fortnightOfYear']] = $this->make_request($url,null);
238
 
239
			$creditValueArray['amount'] = $value['amount'];
17104 anikendra 240
			// $total_credited_amount = $total_credited_amount + $value['amount'];
16893 naman 241
			$creditValueArray['type'] = 'App';
242
			$creditValueArray['fortbatchid'] = $value['fortnightOfYear'];
17109 naman 243
			$creditValueArray['creditedDate'] = $value['creditedDate'];
19552 naman 244
			$creditValueArray['description'] = '';
245
			$creditKeyArray[$value['batchCreditId'].'App'] = $creditValueArray;
17109 naman 246
			// echo "total credit",$total_credited_amount;
16893 naman 247
			// echo $url;
248
		}
249
 
250
		ksort($creditKeyArray);
16907 naman 251
		$this->set(compact('getcredited','creditedFortnight','creditKeyArray','total_credited_amount'));
16893 naman 252
		// debug($creditedFortnight);
253
	// App Credit End
254
 
255
 
16628 anikendra 256
		foreach ($fortnightIds AS $fortnightId){
16712 naman 257
			$appInstalls = array();
16628 anikendra 258
			$options = array('conditions'=>array('fortnightOfYear'=>$fortnightId,'user_id'=>$this->Auth->User('id')),'fields'=>array('status','amount'));
259
			$temp = $this->UserAppCashback->find('first',$options);
16681 anikendra 260
			if(isset($temp) && !empty($temp)){
261
				$cashBacks[$temp['UserAppCashback']['status']]['amount'] = $temp['UserAppCashback']['amount'];
16712 naman 262
				$cashBacks[$temp['UserAppCashback']['status']]['fortnightOfYear'] = $fortnightId;
263
				// debug($fortnightId);
16681 anikendra 264
				$options = array('conditions'=>array('fortnightOfYear'=>$fortnightId,'user_id'=>$this->Auth->User('id')),'fields'=>array('sum(payoutAmount) AS amount','sum(installCount) AS installs','transaction_date'),'group'=>'transaction_date');
265
				$installs = $this->UserAppInstall->find('all',$options);			
266
				if(!empty($installs)){
267
					foreach ($installs as $key => $value) {
268
						$appInstalls[$value['UserAppInstall']['transaction_date']] = $value[0];					
269
					}
270
					$cashBacks[$temp['UserAppCashback']['status']]['installs'] = $appInstalls;
16628 anikendra 271
				}
272
			}
273
		}
19676 naman 274
 
275
// 		offerresponse start
276
 
277
		$cachekey = 'target-'.$userId;
278
		$getoffer = Cache::read($cachekey,'target');
279
 
280
		$offerresponse = "";
281
		$getcashback = 0;
282
		if($getoffer === false){
19677 naman 283
			$offerurl = $this->apihost."getOfferForUser/?user_id=".$userId;
19676 naman 284
			$offerresponse = $this->make_request($offerurl,null);
285
			Cache::write($cachekey , $offerresponse ,'target');
286
			if(empty($offerresponse)){
287
				$offerresponse = "";
288
			}
289
		}else{
290
			if(!empty($getoffer)){
291
				$offerresponse  = $getoffer;
292
 
293
				}
294
		}
295
 
296
 
297
		if(!empty($offerresponse)){
298
			$totalval = $offerresponse['delivered_order_value'] + $offerresponse['pending_order_value'];
299
 
300
			if($totalval< $offerresponse['target1']){
301
				$getcashback = 0;
302
			}
303
			else if($totalval< $offerresponse['target2']){
304
				if($offerresponse['maxCashBack']< intval(($totalval*$offerresponse['target1_cash_back_percetage'])/100)){
305
					$getcashback = $offerresponse['maxCashBack'];
306
				}
307
				else{
308
					$getcashback = intval(($totalval*$offerresponse['target1_cash_back_percetage'])/100);
309
				}
310
 
311
			}
312
			else{
313
				if($offerresponse['maxCashBack']< intval(($totalval*$offerresponse['target2_cash_back_percetage'])/100)){
314
					$getcashback = $offerresponse['maxCashBack'];
315
				}
316
				else{
317
					$getcashback = intval(($totalval*$offerresponse['target2_cash_back_percetage'])/100);
318
				}
319
 
320
			}
321
		}
322
// 		offerresponse end
323
 
19667 naman 324
		$targetuser = Configure::read('targetuser'); 
19676 naman 325
		$this->set(compact('getcashback','targetuser','userId','storemapping','activestores','pendingOrders','approvedOrders','creditedOrders','pendingCashbacks','creditedCashbacks','pendingAmount','approvedAmount','creditedAmount','cashBacks'));
13762 anikendra 326
	}
327
 
16840 naman 328
	public function getAppByDate($date) {
329
		$url = $this->apihost.'appUserBatchDateDrillDown/1/'.$date;
330
		$getApp = $this->make_request($url,null);
331
		echo $getApp;
332
	}
333
 
17354 naman 334
	public function usercashbacks($userId) {	
335
		$this->set('byUser',$userId);
15217 anikendra 336
		$this->layout = "innerpages";
17724 amit.gupta 337
		$url = $this->apihost."storeorder/user/".$userId."?page=1&window=50&searchMap=%7B\"subOrders.cashBackStatus\"%3A%20%7B\"%24in\"%3A%20%5B\"Approved\"%2C%20\"Pending\"%5D%7D%7D";
15217 anikendra 338
		$response = $this->make_request($url,null);
339
		// debug($response);
340
		$creditedOrders = $pendingOrders = $approvedOrders = array();
341
		$creditedAmount = $pendingAmount = $approvedAmount = 0;
17724 amit.gupta 342
		echo $response['data'];
15217 anikendra 343
		if(!empty($response['data'])){
344
			foreach ($response['data'] as $key => $order) {
345
				if(!empty($order['subOrders'])){
346
					foreach ($order['subOrders'] as $key => $suborder) {
347
						$suborder['storeId'] = $order['storeId'];
348
						if($order['storeId']!=4){
349
							$suborder['merchantOrderId'] = $order['merchantOrderId'];
350
						} else {
351
							$suborder['merchantOrderId'] = $suborder['merchantSubOrderId'];
352
						}
353
						if(!empty($order['orderTrackingUrl'])){
354
							$suborder['orderSuccessUrl'] = $order['orderTrackingUrl'];
355
						}
17724 amit.gupta 356
						$suborder['cashBackStatus'];
15217 anikendra 357
						switch($suborder['cashBackStatus']){
358
							// case 'Credited to wallet'://Credited
359
							// $creditedOrders[] = $suborder;
360
							// break;
361
							case 'Approved':
362
							$approvedOrders[] = $suborder;
363
							$approvedAmount += $suborder['cashBackAmount'];
364
							break;
365
							case 'Pending':
366
							$pendingOrders[] = $suborder;
367
							// $pendingAmount += $suborder['cashBackAmount'];
368
						}
369
					}
370
				}
371
			}
372
		}
373
		$url = $this->apihost.'pending-cashbacks/user/'.$userId;
374
		$result = $this->make_request($url,null);
375
		$pendingAmount = $result['amount'];
376
		//Get pending cashbacks
377
		$url = $this->apihost.'pending-refunds/user/'.$userId;
378
		$pendingCashbacks = $this->make_request($url,null);
379
		//Get credited cashbacks
380
		$url = $this->apihost.'refund/user/'.$userId;
381
		$creditedCashbacks = $this->make_request($url,null);
19499 naman 382
 
383
		$totalcreditedcashback = 0;
384
		if(!empty($creditedCashbacks)){
385
			$totalcreditedcashback = $creditedCashbacks['credited'];
386
		}
387
 
17201 naman 388
		$creditKeyArray = array();
389
		$creditValueArray = array();
390
		$total_credited_amount = 0;
15217 anikendra 391
		if(!empty($creditedCashbacks)){
392
			foreach ($creditedCashbacks['data'] as $key => $value) {
393
				$creditedAmount += $value['userAmount'];				
394
				$data = array('subOrders.batchId'=>$value['batch']);
395
				$jsonVar = json_encode($data);
17459 amit.gupta 396
				$url = $this->apihost."storeorder/user/".$userId."?page=1&window=50&searchMap=$jsonVar";
15217 anikendra 397
				$creditedOrders[$value['batch']] = $this->make_request($url,null);
17201 naman 398
				$total_credited_amount =$total_credited_amount + $value['userAmount'];
399
				if($value['type']== 'Order')
400
				{
401
					$creditValueArray['amount'] = $value['userAmount'];
402
					$creditValueArray['type'] = $value['type'];
403
					$creditValueArray['fortbatchid'] = $value['batch'];
404
					$creditValueArray['creditedDate'] = date('Y-m-d',strtotime($value['timestamp']));
405
					// $creditKeyArray[date('Y-m-d',strtotime($value['timestamp']))] = $creditValueArray;
19552 naman 406
					$creditValueArray['description'] = '';
407
					$creditKeyArray[$value['batch'].$value['type']] = $creditValueArray;
17201 naman 408
				}
19499 naman 409
				elseif($value['type']== 'Refund'){
410
					$creditValueArray['amount'] = $value['userAmount'];
411
					$creditValueArray['type'] = $value['type'];
412
					$creditValueArray['fortbatchid'] = $value['batch'];
413
					$creditValueArray['creditedDate'] = date('Y-m-d',strtotime($value['timestamp']));
414
					$creditValueArray['description'] = $value['description'];
19552 naman 415
					$creditKeyArray[$value['batch'].$value['type']] = $creditValueArray;
19499 naman 416
 
417
				}
418
				elseif($value['type']== 'Adjustment'){
419
					$creditValueArray['amount'] = $value['userAmount'];
420
					$creditValueArray['type'] = $value['type'];
421
					$creditValueArray['fortbatchid'] = $value['batch'];
422
					$creditValueArray['creditedDate'] = date('Y-m-d',strtotime($value['timestamp']));
423
					$creditValueArray['description'] = $value['description'];
19552 naman 424
					$creditKeyArray[$value['batch'].$value['type']] = $creditValueArray;
19499 naman 425
				}
17201 naman 426
 
19785 naman 427
				elseif($value['type']== 'Offer'){
428
					$creditValueArray['amount'] = $value['userAmount'];
429
					$creditValueArray['type'] = $value['type'];
430
					$creditValueArray['fortbatchid'] = $value['batch'];
431
					$creditValueArray['creditedDate'] = date('Y-m-d',strtotime($value['timestamp']));
432
					$creditValueArray['description'] = $value['description'];
433
					$creditKeyArray[$value['batch'].$value['type']] = $creditValueArray;
434
				}
435
 
15217 anikendra 436
			}
437
		}
17201 naman 438
 
15217 anikendra 439
		$storemapping = Configure::read('storemapping');
440
		$activestores = Configure::read('activestores');
17459 amit.gupta 441
		//App related cashbacks
442
		$this->loadModel('UserAppCashback');
443
		$this->loadModel('UserAppInstall');
444
		//Compute last two fortnight ids
445
		$fortnightIds = array();
446
		if(date('d',time())<=15){
447
			$fortnightIds[] = 2*(date('m',time())-1)-1;
448
			$fortnightIds[] = 2*(date('m',time())-1);
449
		}else{
450
			$fortnightIds[] = 2*(date('m',time())-1);
451
			$fortnightIds[] = 2*(date('m',time())-1)+1;
17201 naman 452
		}
17459 amit.gupta 453
		$cashBacks = array();
17354 naman 454
 
455
		// Approved Start
456
 
457
		$url = $this->apihost.'appUserCashBack/'.$userId.'/Approved';
458
		$getapproved = $this->make_request($url,null);
459
		$fortnight = array();
460
		$fortnight_amount = array();
461
		$counter = 0;
462
		$total_approved_amount = 0;
463
		$current_date =  date("Y");
464
		foreach ($getapproved["UserAppCashBack"] as $key => $value) {
465
			 $fortnight[$counter] = $value["fortnightOfYear"];
466
			 $fortnight_amount[$counter] = $value["amount"];
467
			 $total_approved_amount += $value["amount"];
468
			 $counter++;
469
		}
470
		$approvedFortnight = array();
471
		for($i=0; $i<count($fortnight); $i++){
18172 manish.sha 472
			$url = $this->apihost.'appUserBatchDrillDown/'.$userId.'/'.$getapproved["UserAppCashBack"][$i]['fortnightOfYear'].'/'.$getapproved["UserAppCashBack"][$i]['yearVal'];
17354 naman 473
			$approvedFortnight[$i] = $this->make_request($url,null);
474
		// $url = $this->apihost.'appUserBatchDrillDown/1/16/2015';
475
		// $approvedFortnight[] = $this->make_request($url,null);
476
		}
477
		// debug($approvedFortnight);
478
		$this->set(compact('fortnight','total_approved_amount','fortnight_amount','approvedFortnight'));		
479
	// Approved End	
480
 
17459 amit.gupta 481
	// App Credit Start
482
		$creditedFortnight = array();
483
		$url = $this->apihost.'appUserCashBack/'.$userId.'/Credited';
484
		$getcredited = $this->make_request($url,null);
485
		// debug($getcredited);
486
		foreach ($getcredited['UserAppCashBack'] as $key => $value) {
487
 
488
 
489
 
490
			$url = $this->apihost.'appUserBatchDrillDown/'.$userId.'/'.$value['fortnightOfYear'].'/'.$value['yearVal'];
491
			$creditedFortnight[$value['fortnightOfYear']] = $this->make_request($url,null);
492
 
493
			$creditValueArray['amount'] = $value['amount'];
494
			// $total_credited_amount = $total_credited_amount + $value['amount'];
495
			$creditValueArray['type'] = 'App';
496
			$creditValueArray['fortbatchid'] = $value['fortnightOfYear'];
497
			$creditValueArray['creditedDate'] = $value['creditedDate'];
19552 naman 498
			$creditValueArray['description'] = '';
499
			$creditKeyArray[$value['batchCreditId'].'App'] = $creditValueArray;
17459 amit.gupta 500
			// echo "total credit",$total_credited_amount;
501
			// echo $url;
15217 anikendra 502
		}
17459 amit.gupta 503
 
504
		ksort($creditKeyArray);
17656 amit.gupta 505
		$this->set(compact('getcredited','creditedFortnight','creditKeyArray','total_credited_amount', 'creditedOrders', 'pendingAmount', 'pendingOrders','approvedAmount', 'approvedOrders'));
17459 amit.gupta 506
 
15217 anikendra 507
	}
508
 
15227 anikendra 509
/*
13532 anikendra 510
	public function index() {
13591 anikendra 511
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 512
		$this->Order->recursive = 0;
513
		$this->set('orders', $this->Paginator->paginate());
514
	}
515
 
15227 anikendra 516
 
13532 anikendra 517
	public function view($id = null) {
13591 anikendra 518
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 519
		if (!$this->Order->exists($id)) {
520
			throw new NotFoundException(__('Invalid order'));
521
		}
522
		$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
523
		$this->set('order', $this->Order->find('first', $options));
524
	}
15227 anikendra 525
*/
526
 
13532 anikendra 527
/**
528
 * add method
529
 *
530
 * @return void
531
 */
13814 anikendra 532
 
533
	public function postOrders($order=null) {
13994 anikendra 534
		// Configure::load('live');
13814 anikendra 535
		$apihost = Configure::read('pythonapihost');
536
		$url = $apihost."storeorder";
537
		if(!empty($order)) {
538
			$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']);
539
			$jsonVar = json_encode($params);
540
			return $this->make_request($url,$jsonVar);
541
		}else{
542
			$result = array('success'=>false,'message'=>'Empty order array');
543
			return $result;
544
		}
545
	}
546
 
13532 anikendra 547
	public function add() {
14933 amit.gupta 548
		$this->log(print_r($this->request->data,1),'orders');
13532 anikendra 549
		if ($this->request->is('post')) {
14886 amit.gupta 550
			if($this->request->data['zip']){
14933 amit.gupta 551
				$this->request->data['rawhtml'] = gzuncompress(base64_decode($this->request->data['rawhtml'])); 
14886 amit.gupta 552
			}
553
			$this->log(print_r($this->request->data,1),'orders');
14315 anikendra 554
			if(empty($this->request->data['id'])) {
555
				$this->Order->create();
13633 anikendra 556
			}
15093 anikendra 557
			$this->request->data['ip'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
13532 anikendra 558
			if ($this->Order->save($this->request->data)) {
14315 anikendra 559
				//$this->loadModel('PythonApi');	
560
				if(empty($this->request->data['id'])) {
561
					$id = $this->Order->getLastInsertID();
562
				}else{
563
					$id = $this->request->data['id'];
564
				}
565
				$order = $this->Order->find('first',array('conditions'=>array('id'=>$id),'recursive'=>-1));
13814 anikendra 566
				$response = $this->postOrders($order);
567
				$this->log(print_r($response,1),'orders');
568
				if(!empty($response) && $response['result']) {
14315 anikendra 569
					//if($response['result'] == 'HTML_REQUIRED' || $response['result'] == 'requireHtml') {
570
					if($response['htmlRequired'] == 1) {
13814 anikendra 571
						$this->loadModel('Rawhtml');
572
						$data = array('order_id' => $order['Order']['id'],'url' => $response['url'], 'status' => 'new');
573
						$this->Rawhtml->create();
574
						$this->Rawhtml->save($data); 
14315 anikendra 575
						//$result =array('success'=>true,'message'=>__('requireHtml'),'url' => $response['url'],'orderId' => $response['orderId']);
576
						$result =  $response;
577
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
578
					}/* elseif($response['result'] == 'IGNORED') {
13814 anikendra 579
						$result =array('success'=>true,'message'=>__('IGNORED'));
580
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
581
					} elseif($response['result'] == 'PARSE_ERROR') {
582
						$result =array('success'=>true,'message'=>__('PARSE_ERROR'));
583
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
14315 anikendra 584
					} */
585
					else {
586
						$result =array('success'=>true,'message'=> $response['result']);
587
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
13814 anikendra 588
					}
589
					$this->Order->query($sql);
590
				}
591
				//$result = array('success'=>true,'message'=>__('HTML_REQUIRED'),'url'=>'https://www.amazon.in/gp/css/summary/edit.html?orderID=404-7369214-6566739');
13633 anikendra 592
/*
593
				$options = array('conditions'=>array('status'=>'mapped'),'recursive'=>-1);
594
				$order = $this->Order->find('first',$options);
595
				if(!empty($orders)) {
596
					foreach($orders AS $order) {
597
						$response = $this->PythonApi->postOrders($order);
598
						if(!empty($response) && $response['result']) {
599
							$sql = "UPDATE orders SET status = 'processed' WHERE id = ".$order['Order']['id'];
600
							$this->Order->query($sql);
601
						}
602
					}
603
				}
604
*/
13532 anikendra 605
			} else {
14315 anikendra 606
				$this->log(print_r($this->Order->validationErrors,1),'orders');
13591 anikendra 607
				$result = array('success'=>false,'message'=>__('The order could not be saved. Please, try again.'));
13532 anikendra 608
			}
13591 anikendra 609
			$this->response->type('json');
610
			$this->layout = 'ajax';
611
			$this->set(array(
14315 anikendra 612
			    'result' => $response,
13591 anikendra 613
			    // 'callback' => $callback,
614
			    '_serialize' => array('result')
615
			));
616
			$this->render('/Elements/json');		
617
		}			
13532 anikendra 618
	}
619
 
15227 anikendra 620
/*
621
 
13532 anikendra 622
	public function edit($id = null) {
13591 anikendra 623
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 624
		if (!$this->Order->exists($id)) {
625
			throw new NotFoundException(__('Invalid order'));
626
		}
627
		if ($this->request->is(array('post', 'put'))) {
628
			if ($this->Order->save($this->request->data)) {
629
				$this->Session->setFlash(__('The order has been saved.'));
630
				return $this->redirect(array('action' => 'index'));
631
			} else {
632
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
633
			}
634
		} else {
635
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
636
			$this->request->data = $this->Order->find('first', $options);
637
		}
638
		$users = $this->Order->User->find('list');
639
		$stores = $this->Order->Store->find('list');
640
		$storeOrders = $this->Order->StoreOrder->find('list');
641
		$this->set(compact('users', 'stores', 'storeOrders'));
642
	}
643
 
15227 anikendra 644
 
13532 anikendra 645
	public function delete($id = null) {
13591 anikendra 646
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 647
		$this->Order->id = $id;
648
		if (!$this->Order->exists()) {
649
			throw new NotFoundException(__('Invalid order'));
650
		}
651
		$this->request->onlyAllow('post', 'delete');
652
		if ($this->Order->delete()) {
653
			$this->Session->setFlash(__('The order has been deleted.'));
654
		} else {
655
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
656
		}
657
		return $this->redirect(array('action' => 'index'));
658
	}
15227 anikendra 659
*/
13532 anikendra 660
 
661
/**
662
 * admin_index method
663
 *
664
 * @return void
665
 */
666
	public function admin_index() {
15227 anikendra 667
		$this->checkAcl();
13532 anikendra 668
		$this->Order->recursive = 0;
669
		$this->set('orders', $this->Paginator->paginate());
670
	}
671
 
672
/**
673
 * admin_view method
674
 *
675
 * @throws NotFoundException
676
 * @param string $id
677
 * @return void
678
 */
679
	public function admin_view($id = null) {
15227 anikendra 680
		$this->checkAcl();
13532 anikendra 681
		if (!$this->Order->exists($id)) {
682
			throw new NotFoundException(__('Invalid order'));
683
		}
684
		$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
685
		$this->set('order', $this->Order->find('first', $options));
686
	}
687
 
688
/**
689
 * admin_add method
690
 *
691
 * @return void
692
 */
693
	public function admin_add() {
15227 anikendra 694
		$this->checkAcl();
13532 anikendra 695
		if ($this->request->is('post')) {
696
			$this->Order->create();
697
			if ($this->Order->save($this->request->data)) {
698
				$this->Session->setFlash(__('The order has been saved.'));
699
				return $this->redirect(array('action' => 'index'));
700
			} else {
701
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
702
			}
703
		}
704
		$users = $this->Order->User->find('list');
705
		$stores = $this->Order->Store->find('list');
706
		$storeOrders = $this->Order->StoreOrder->find('list');
707
		$this->set(compact('users', 'stores', 'storeOrders'));
708
	}
709
 
710
/**
711
 * admin_edit method
712
 *
713
 * @throws NotFoundException
714
 * @param string $id
715
 * @return void
716
 */
717
	public function admin_edit($id = null) {
15227 anikendra 718
		$this->checkAcl();
13532 anikendra 719
		if (!$this->Order->exists($id)) {
720
			throw new NotFoundException(__('Invalid order'));
721
		}
722
		if ($this->request->is(array('post', 'put'))) {
723
			if ($this->Order->save($this->request->data)) {
724
				$this->Session->setFlash(__('The order has been saved.'));
725
				return $this->redirect(array('action' => 'index'));
726
			} else {
727
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
728
			}
729
		} else {
730
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
731
			$this->request->data = $this->Order->find('first', $options);
732
		}
733
		$users = $this->Order->User->find('list');
734
		$stores = $this->Order->Store->find('list');
735
		$storeOrders = $this->Order->StoreOrder->find('list');
736
		$this->set(compact('users', 'stores', 'storeOrders'));
737
	}
738
 
739
/**
740
 * admin_delete method
741
 *
742
 * @throws NotFoundException
743
 * @param string $id
744
 * @return void
745
 */
746
	public function admin_delete($id = null) {
15227 anikendra 747
		$this->checkAcl();
13532 anikendra 748
		$this->Order->id = $id;
749
		if (!$this->Order->exists()) {
750
			throw new NotFoundException(__('Invalid order'));
751
		}
752
		$this->request->onlyAllow('post', 'delete');
753
		if ($this->Order->delete()) {
754
			$this->Session->setFlash(__('The order has been deleted.'));
755
		} else {
756
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
757
		}
758
		return $this->redirect(array('action' => 'index'));
14354 anikendra 759
	}
760
 
761
	public function all() {
17287 amit.gupta 762
		$orderFilters = $this->getOrderFilters("monitor");
14354 anikendra 763
		$page = $this->request->query('page');
17287 amit.gupta 764
		$filter = $this->request->query('filter');
14354 anikendra 765
		$page = isset($page)?$page:1;
14509 anikendra 766
		// $userId = $this->request->query('user_id');
767
		// if(isset($userId) && !empty($userId)){
768
		// 	$this->loadModel('User');
769
		// 	$dbuser = $this->User->findById($userId);
770
		// 	$this->Auth->login($dbuser['User']);	
771
		// }
14354 anikendra 772
		$this->layout = "innerpages";
17287 amit.gupta 773
		$url = $this->apihost."orders/?page=$page&window=20";
774
		if (!empty($filter)) $url .= "&filter=".$filter."&filtertype=monitor";
14354 anikendra 775
		$response = $this->make_request($url,null);
14509 anikendra 776
		$totalPages = $response['totalPages'];		
14354 anikendra 777
		if(!empty($response['data'])){
778
			$this->set('orders',$response['data']);
779
		}
780
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
781
		$storemapping = Configure::read('storemapping');
782
		$activestores = Configure::read('activestores');
783
		$amazonorderurl = Configure::read('amazonorderurl');
784
		$allusers = $this->Order->User->find('all',array('fields'=>array('first_name','id'),'recursive'=>-1));
785
		foreach($allusers AS $user){
786
			$users[$user['User']['id']] = $user['User']['first_name'];
787
		}
15188 anikendra 788
		$this->layout = 'admin';
17287 amit.gupta 789
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl','users', 'orderFilters', 'type'));
14354 anikendra 790
	}
17290 amit.gupta 791
 
792
	public function monitor() {
793
		$orderFilters = $this->getOrderFilters("monitor");
794
		$page = $this->request->query('page');
795
		$filter = $this->request->query('filter');
17337 amit.gupta 796
		if(!empty($this->request->query('requiredetail'))){
797
				$orderId=$this->request->query('requiredetail');
798
		}
17290 amit.gupta 799
		$page = isset($page)?$page:1;
800
		$this->layout = "innerpages";
801
		$url = $this->apihost."orders/?page=$page&window=20";
17339 amit.gupta 802
		$apihost = 'http://104.200.25.40:8057/';
17290 amit.gupta 803
		if (!empty($filter)) $url .= "&filter=".$filter."&filtertype=monitor";
804
		$response = $this->make_request($url,null);
805
		$totalPages = $response['totalPages'];		
806
		if(!empty($response['data'])){
807
			$this->set('orders',$response['data']);
808
		}
809
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
810
		$storemapping = Configure::read('storemapping');
811
		$activestores = Configure::read('activestores');
812
		$amazonorderurl = Configure::read('amazonorderurl');
813
		$allusers = $this->Order->User->find('all',array('fields'=>array('first_name','id'),'recursive'=>-1));
814
		foreach($allusers AS $user){
815
			$users[$user['User']['id']] = $user['User']['first_name'];
816
		}
817
		$this->layout = 'admin';
17337 amit.gupta 818
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl','users', 'orderFilters', 'filter', 'apihost'));
17290 amit.gupta 819
	}
18446 manish.sha 820
 
821
	public function orderdetail(){
822
		$orderId = $this->request->query('order_id');
823
		$userId = $this->Auth->User('id');
824
		$next = "order/".$orderId;					
825
		$redirectUrl = $this->getAutoLoginUrl($userId,$next);
18449 manish.sha 826
		$this->layout = "innerpages";
18446 manish.sha 827
		$this->log($redirectUrl,'headers');
828
		$this->set(compact('redirectUrl','next'));
829
	}
17459 amit.gupta 830
}