Subversion Repositories SmartDukaan

Rev

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