Subversion Repositories SmartDukaan

Rev

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