Subversion Repositories SmartDukaan

Rev

Rev 17354 | Rev 17402 | 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";
13993 anikendra 92
		$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=1&window=50";
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";
261
		$url = $this->apihost."storeorder/user/".$userId."?page=1&window=50";
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);
17376 naman 324
				// $url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=1&window=50&searchMap=$jsonVar";
325
				$url = $this->apihost."storeorder/user/".$userId."?page=1&window=50&searchMap=$jsonVar";
15217 anikendra 326
				$creditedOrders[$value['batch']] = $this->make_request($url,null);
17201 naman 327
				$total_credited_amount =$total_credited_amount + $value['userAmount'];
328
				if($value['type']== 'Order')
329
				{
330
					$creditValueArray['amount'] = $value['userAmount'];
331
					$creditValueArray['type'] = $value['type'];
332
					$creditValueArray['fortbatchid'] = $value['batch'];
333
					$creditValueArray['creditedDate'] = date('Y-m-d',strtotime($value['timestamp']));
334
					// $creditKeyArray[date('Y-m-d',strtotime($value['timestamp']))] = $creditValueArray;
335
					$creditKeyArray[date('Y-m-d',strtotime($value['timestamp'])).$value['type']] = $creditValueArray;
336
				}
337
 
15217 anikendra 338
			}
339
		}
17201 naman 340
 
15217 anikendra 341
		$storemapping = Configure::read('storemapping');
342
		$activestores = Configure::read('activestores');
17201 naman 343
 
344
	// App Credit Start
345
		$creditedFortnight = array();
346
		$url = $this->apihost.'appUserCashBack/'.$userId.'/Credited';
347
		$getcredited = $this->make_request($url,null);
348
		// debug($getcredited);
349
		foreach ($getcredited['UserAppCashBack'] as $key => $value) {
350
 
351
 
352
 
353
			$url = $this->apihost.'appUserBatchDrillDown/'.$userId.'/'.$value['fortnightOfYear'].'/'.$value['yearVal'];
354
			$creditedFortnight[$value['fortnightOfYear']] = $this->make_request($url,null);
355
 
356
			$creditValueArray['amount'] = $value['amount'];
357
			// $total_credited_amount = $total_credited_amount + $value['amount'];
358
			$creditValueArray['type'] = 'App';
359
			$creditValueArray['fortbatchid'] = $value['fortnightOfYear'];
360
			$creditValueArray['creditedDate'] = $value['creditedDate'];
361
			$creditKeyArray[$value['creditedDate'].'App'] = $creditValueArray;
362
			// echo "total credit",$total_credited_amount;
363
			// echo $url;
364
		}
365
 
366
		ksort($creditKeyArray);
367
		// debug($creditKeyArray);
368
		$this->set(compact('getcredited','creditedFortnight','creditKeyArray','total_credited_amount'));
369
		// debug($creditedFortnight);
370
	// App Credit End
17354 naman 371
 
372
		// Approved Start
373
 
374
		$url = $this->apihost.'appUserCashBack/'.$userId.'/Approved';
375
		$getapproved = $this->make_request($url,null);
376
		$fortnight = array();
377
		$fortnight_amount = array();
378
		$counter = 0;
379
		$total_approved_amount = 0;
380
		$current_date =  date("Y");
381
		foreach ($getapproved["UserAppCashBack"] as $key => $value) {
382
			 $fortnight[$counter] = $value["fortnightOfYear"];
383
			 $fortnight_amount[$counter] = $value["amount"];
384
			 $total_approved_amount += $value["amount"];
385
			 $counter++;
386
		}
387
		$approvedFortnight = array();
388
		for($i=0; $i<count($fortnight); $i++){
389
			$url = $this->apihost.'appUserBatchDrillDown/'.$userId.'/'.$fortnight[$i].'/'.$current_date;
390
			$approvedFortnight[$i] = $this->make_request($url,null);
391
		// $url = $this->apihost.'appUserBatchDrillDown/1/16/2015';
392
		// $approvedFortnight[] = $this->make_request($url,null);
393
		}
394
		// debug($approvedFortnight);
395
		$this->set(compact('fortnight','total_approved_amount','fortnight_amount','approvedFortnight'));		
396
	// Approved End	
397
 
15217 anikendra 398
		if(!empty($response['data'])){
399
			$this->set(compact('storemapping','activestores','pendingOrders','approvedOrders','creditedOrders','pendingCashbacks','creditedCashbacks','pendingAmount','approvedAmount','creditedAmount'));
400
		}
401
	}
402
 
15227 anikendra 403
/*
13532 anikendra 404
	public function index() {
13591 anikendra 405
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 406
		$this->Order->recursive = 0;
407
		$this->set('orders', $this->Paginator->paginate());
408
	}
409
 
15227 anikendra 410
 
13532 anikendra 411
	public function view($id = null) {
13591 anikendra 412
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 413
		if (!$this->Order->exists($id)) {
414
			throw new NotFoundException(__('Invalid order'));
415
		}
416
		$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
417
		$this->set('order', $this->Order->find('first', $options));
418
	}
15227 anikendra 419
*/
420
 
13532 anikendra 421
/**
422
 * add method
423
 *
424
 * @return void
425
 */
13814 anikendra 426
 
427
	public function postOrders($order=null) {
13994 anikendra 428
		// Configure::load('live');
13814 anikendra 429
		$apihost = Configure::read('pythonapihost');
430
		$url = $apihost."storeorder";
431
		if(!empty($order)) {
432
			$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']);
433
			$jsonVar = json_encode($params);
434
			return $this->make_request($url,$jsonVar);
435
		}else{
436
			$result = array('success'=>false,'message'=>'Empty order array');
437
			return $result;
438
		}
439
	}
440
 
13532 anikendra 441
	public function add() {
14933 amit.gupta 442
		$this->log(print_r($this->request->data,1),'orders');
13532 anikendra 443
		if ($this->request->is('post')) {
14886 amit.gupta 444
			if($this->request->data['zip']){
14933 amit.gupta 445
				$this->request->data['rawhtml'] = gzuncompress(base64_decode($this->request->data['rawhtml'])); 
14886 amit.gupta 446
			}
447
			$this->log(print_r($this->request->data,1),'orders');
14315 anikendra 448
			if(empty($this->request->data['id'])) {
449
				$this->Order->create();
13633 anikendra 450
			}
15093 anikendra 451
			$this->request->data['ip'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
13532 anikendra 452
			if ($this->Order->save($this->request->data)) {
14315 anikendra 453
				//$this->loadModel('PythonApi');	
454
				if(empty($this->request->data['id'])) {
455
					$id = $this->Order->getLastInsertID();
456
				}else{
457
					$id = $this->request->data['id'];
458
				}
459
				$order = $this->Order->find('first',array('conditions'=>array('id'=>$id),'recursive'=>-1));
13814 anikendra 460
				$response = $this->postOrders($order);
461
				$this->log(print_r($response,1),'orders');
462
				if(!empty($response) && $response['result']) {
14315 anikendra 463
					//if($response['result'] == 'HTML_REQUIRED' || $response['result'] == 'requireHtml') {
464
					if($response['htmlRequired'] == 1) {
13814 anikendra 465
						$this->loadModel('Rawhtml');
466
						$data = array('order_id' => $order['Order']['id'],'url' => $response['url'], 'status' => 'new');
467
						$this->Rawhtml->create();
468
						$this->Rawhtml->save($data); 
14315 anikendra 469
						//$result =array('success'=>true,'message'=>__('requireHtml'),'url' => $response['url'],'orderId' => $response['orderId']);
470
						$result =  $response;
471
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
472
					}/* elseif($response['result'] == 'IGNORED') {
13814 anikendra 473
						$result =array('success'=>true,'message'=>__('IGNORED'));
474
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
475
					} elseif($response['result'] == 'PARSE_ERROR') {
476
						$result =array('success'=>true,'message'=>__('PARSE_ERROR'));
477
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
14315 anikendra 478
					} */
479
					else {
480
						$result =array('success'=>true,'message'=> $response['result']);
481
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
13814 anikendra 482
					}
483
					$this->Order->query($sql);
484
				}
485
				//$result = array('success'=>true,'message'=>__('HTML_REQUIRED'),'url'=>'https://www.amazon.in/gp/css/summary/edit.html?orderID=404-7369214-6566739');
13633 anikendra 486
/*
487
				$options = array('conditions'=>array('status'=>'mapped'),'recursive'=>-1);
488
				$order = $this->Order->find('first',$options);
489
				if(!empty($orders)) {
490
					foreach($orders AS $order) {
491
						$response = $this->PythonApi->postOrders($order);
492
						if(!empty($response) && $response['result']) {
493
							$sql = "UPDATE orders SET status = 'processed' WHERE id = ".$order['Order']['id'];
494
							$this->Order->query($sql);
495
						}
496
					}
497
				}
498
*/
13532 anikendra 499
			} else {
14315 anikendra 500
				$this->log(print_r($this->Order->validationErrors,1),'orders');
13591 anikendra 501
				$result = array('success'=>false,'message'=>__('The order could not be saved. Please, try again.'));
13532 anikendra 502
			}
13591 anikendra 503
			$this->response->type('json');
504
			$this->layout = 'ajax';
505
			$this->set(array(
14315 anikendra 506
			    'result' => $response,
13591 anikendra 507
			    // 'callback' => $callback,
508
			    '_serialize' => array('result')
509
			));
510
			$this->render('/Elements/json');		
511
		}			
13532 anikendra 512
	}
513
 
15227 anikendra 514
/*
515
 
13532 anikendra 516
	public function edit($id = null) {
13591 anikendra 517
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 518
		if (!$this->Order->exists($id)) {
519
			throw new NotFoundException(__('Invalid order'));
520
		}
521
		if ($this->request->is(array('post', 'put'))) {
522
			if ($this->Order->save($this->request->data)) {
523
				$this->Session->setFlash(__('The order has been saved.'));
524
				return $this->redirect(array('action' => 'index'));
525
			} else {
526
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
527
			}
528
		} else {
529
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
530
			$this->request->data = $this->Order->find('first', $options);
531
		}
532
		$users = $this->Order->User->find('list');
533
		$stores = $this->Order->Store->find('list');
534
		$storeOrders = $this->Order->StoreOrder->find('list');
535
		$this->set(compact('users', 'stores', 'storeOrders'));
536
	}
537
 
15227 anikendra 538
 
13532 anikendra 539
	public function delete($id = null) {
13591 anikendra 540
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 541
		$this->Order->id = $id;
542
		if (!$this->Order->exists()) {
543
			throw new NotFoundException(__('Invalid order'));
544
		}
545
		$this->request->onlyAllow('post', 'delete');
546
		if ($this->Order->delete()) {
547
			$this->Session->setFlash(__('The order has been deleted.'));
548
		} else {
549
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
550
		}
551
		return $this->redirect(array('action' => 'index'));
552
	}
15227 anikendra 553
*/
13532 anikendra 554
 
555
/**
556
 * admin_index method
557
 *
558
 * @return void
559
 */
560
	public function admin_index() {
15227 anikendra 561
		$this->checkAcl();
13532 anikendra 562
		$this->Order->recursive = 0;
563
		$this->set('orders', $this->Paginator->paginate());
564
	}
565
 
566
/**
567
 * admin_view method
568
 *
569
 * @throws NotFoundException
570
 * @param string $id
571
 * @return void
572
 */
573
	public function admin_view($id = null) {
15227 anikendra 574
		$this->checkAcl();
13532 anikendra 575
		if (!$this->Order->exists($id)) {
576
			throw new NotFoundException(__('Invalid order'));
577
		}
578
		$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
579
		$this->set('order', $this->Order->find('first', $options));
580
	}
581
 
582
/**
583
 * admin_add method
584
 *
585
 * @return void
586
 */
587
	public function admin_add() {
15227 anikendra 588
		$this->checkAcl();
13532 anikendra 589
		if ($this->request->is('post')) {
590
			$this->Order->create();
591
			if ($this->Order->save($this->request->data)) {
592
				$this->Session->setFlash(__('The order has been saved.'));
593
				return $this->redirect(array('action' => 'index'));
594
			} else {
595
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
596
			}
597
		}
598
		$users = $this->Order->User->find('list');
599
		$stores = $this->Order->Store->find('list');
600
		$storeOrders = $this->Order->StoreOrder->find('list');
601
		$this->set(compact('users', 'stores', 'storeOrders'));
602
	}
603
 
604
/**
605
 * admin_edit method
606
 *
607
 * @throws NotFoundException
608
 * @param string $id
609
 * @return void
610
 */
611
	public function admin_edit($id = null) {
15227 anikendra 612
		$this->checkAcl();
13532 anikendra 613
		if (!$this->Order->exists($id)) {
614
			throw new NotFoundException(__('Invalid order'));
615
		}
616
		if ($this->request->is(array('post', 'put'))) {
617
			if ($this->Order->save($this->request->data)) {
618
				$this->Session->setFlash(__('The order has been saved.'));
619
				return $this->redirect(array('action' => 'index'));
620
			} else {
621
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
622
			}
623
		} else {
624
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
625
			$this->request->data = $this->Order->find('first', $options);
626
		}
627
		$users = $this->Order->User->find('list');
628
		$stores = $this->Order->Store->find('list');
629
		$storeOrders = $this->Order->StoreOrder->find('list');
630
		$this->set(compact('users', 'stores', 'storeOrders'));
631
	}
632
 
633
/**
634
 * admin_delete method
635
 *
636
 * @throws NotFoundException
637
 * @param string $id
638
 * @return void
639
 */
640
	public function admin_delete($id = null) {
15227 anikendra 641
		$this->checkAcl();
13532 anikendra 642
		$this->Order->id = $id;
643
		if (!$this->Order->exists()) {
644
			throw new NotFoundException(__('Invalid order'));
645
		}
646
		$this->request->onlyAllow('post', 'delete');
647
		if ($this->Order->delete()) {
648
			$this->Session->setFlash(__('The order has been deleted.'));
649
		} else {
650
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
651
		}
652
		return $this->redirect(array('action' => 'index'));
14354 anikendra 653
	}
654
 
655
	public function all() {
17287 amit.gupta 656
		$orderFilters = $this->getOrderFilters("monitor");
14354 anikendra 657
		$page = $this->request->query('page');
17287 amit.gupta 658
		$filter = $this->request->query('filter');
14354 anikendra 659
		$page = isset($page)?$page:1;
14509 anikendra 660
		// $userId = $this->request->query('user_id');
661
		// if(isset($userId) && !empty($userId)){
662
		// 	$this->loadModel('User');
663
		// 	$dbuser = $this->User->findById($userId);
664
		// 	$this->Auth->login($dbuser['User']);	
665
		// }
14354 anikendra 666
		$this->layout = "innerpages";
17287 amit.gupta 667
		$url = $this->apihost."orders/?page=$page&window=20";
668
		if (!empty($filter)) $url .= "&filter=".$filter."&filtertype=monitor";
14354 anikendra 669
		$response = $this->make_request($url,null);
14509 anikendra 670
		$totalPages = $response['totalPages'];		
14354 anikendra 671
		if(!empty($response['data'])){
672
			$this->set('orders',$response['data']);
673
		}
674
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
675
		$storemapping = Configure::read('storemapping');
676
		$activestores = Configure::read('activestores');
677
		$amazonorderurl = Configure::read('amazonorderurl');
678
		$allusers = $this->Order->User->find('all',array('fields'=>array('first_name','id'),'recursive'=>-1));
679
		foreach($allusers AS $user){
680
			$users[$user['User']['id']] = $user['User']['first_name'];
681
		}
15188 anikendra 682
		$this->layout = 'admin';
17287 amit.gupta 683
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl','users', 'orderFilters', 'type'));
14354 anikendra 684
	}
17290 amit.gupta 685
 
686
	public function monitor() {
687
		$orderFilters = $this->getOrderFilters("monitor");
688
		$page = $this->request->query('page');
689
		$filter = $this->request->query('filter');
17337 amit.gupta 690
		if(!empty($this->request->query('requiredetail'))){
691
				$orderId=$this->request->query('requiredetail');
692
		}
17290 amit.gupta 693
		$page = isset($page)?$page:1;
694
		$this->layout = "innerpages";
695
		$url = $this->apihost."orders/?page=$page&window=20";
17339 amit.gupta 696
		$apihost = 'http://104.200.25.40:8057/';
17290 amit.gupta 697
		if (!empty($filter)) $url .= "&filter=".$filter."&filtertype=monitor";
698
		$response = $this->make_request($url,null);
699
		$totalPages = $response['totalPages'];		
700
		if(!empty($response['data'])){
701
			$this->set('orders',$response['data']);
702
		}
703
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
704
		$storemapping = Configure::read('storemapping');
705
		$activestores = Configure::read('activestores');
706
		$amazonorderurl = Configure::read('amazonorderurl');
707
		$allusers = $this->Order->User->find('all',array('fields'=>array('first_name','id'),'recursive'=>-1));
708
		foreach($allusers AS $user){
709
			$users[$user['User']['id']] = $user['User']['first_name'];
710
		}
711
		$this->layout = 'admin';
17337 amit.gupta 712
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl','users', 'orderFilters', 'filter', 'apihost'));
17290 amit.gupta 713
	}
14354 anikendra 714
}