Subversion Repositories SmartDukaan

Rev

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