Subversion Repositories SmartDukaan

Rev

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