Subversion Repositories SmartDukaan

Rev

Rev 19499 | Rev 19667 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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