Subversion Repositories SmartDukaan

Rev

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