Subversion Repositories SmartDukaan

Rev

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