Subversion Repositories SmartDukaan

Rev

Rev 15403 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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
 
18
	public function beforeFilter() {		
19
		parent::beforeFilter();
20
		$this->Auth->allow('add','mine','pendingcashbacks','all');
21
		$this->apihost = Configure::read('pythonapihost');
22
	}
23
 
24
	public function mine() {
25
		$page = $this->request->query('page');
26
		$page = isset($page)?$page:1;
27
		$userId = $this->request->query('user_id');
28
		if(isset($userId) && !empty($userId)){
29
			$this->loadModel('User');
30
			$dbuser = $this->User->findById($userId);
31
			$this->Auth->login($dbuser['User']);	
32
		}
33
		$this->layout = "innerpages";
34
		$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=$page&window=10";
35
		$response = $this->make_request($url,null);
36
		$totalPages = $response['totalPages'];
37
		if(!empty($response['data'])){
38
			$this->set('orders',$response['data']);
39
		}
40
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
41
		$storemapping = Configure::read('storemapping');
42
		$activestores = Configure::read('activestores');
43
		$amazonorderurl = Configure::read('amazonorderurl');
44
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl'));
45
	}
46
 
47
	public function pendingcashbacks() {
48
		$userId = $this->request->query('user_id');
49
		if(isset($userId) && !empty($userId)){
50
			$this->loadModel('User');
51
			$dbuser = $this->User->findById($userId);
52
			$this->Auth->login($dbuser['User']);	
53
		}
54
		$this->layout = "innerpages";
55
		$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=1&window=50";
56
		$response = $this->make_request($url,null);
57
		// debug($response);
58
		$creditedOrders = $pendingOrders = $approvedOrders = array();
59
		$creditedAmount = $pendingAmount = $approvedAmount = 0;
60
		if(!empty($response['data'])){
61
			foreach ($response['data'] as $key => $order) {
62
				if(!empty($order['subOrders'])){
63
					foreach ($order['subOrders'] as $key => $suborder) {
64
						$suborder['storeId'] = $order['storeId'];
65
						if($order['storeId']!=4){
66
							$suborder['merchantOrderId'] = $order['merchantOrderId'];
67
						} else {
68
							$suborder['merchantOrderId'] = $suborder['merchantSubOrderId'];
69
						}
70
						if(!empty($order['orderTrackingUrl'])){
71
							$suborder['orderSuccessUrl'] = $order['orderTrackingUrl'];
72
						}
73
						switch($suborder['cashBackStatus']){
74
							// case 'Credited to wallet'://Credited
75
							// $creditedOrders[] = $suborder;
76
							// break;
77
							case 'Approved':
78
							$approvedOrders[] = $suborder;
79
							$approvedAmount += $suborder['cashBackAmount'];
80
							break;
81
							case 'Pending':
82
							$pendingOrders[] = $suborder;
83
							// $pendingAmount += $suborder['cashBackAmount'];
84
						}
85
					}
86
				}
87
			}
88
		}
89
		$url = $this->apihost.'pending-cashbacks/user/'.$userId;
90
		$result = $this->make_request($url,null);
91
		$pendingAmount = $result['amount'];
92
		//Get pending cashbacks
93
		$url = $this->apihost.'pending-refunds/user/'.$userId;
94
		$pendingCashbacks = $this->make_request($url,null);
95
		//Get credited cashbacks
96
		$url = $this->apihost.'refund/user/'.$userId;
97
		$creditedCashbacks = $this->make_request($url,null);
98
 
99
		if(!empty($creditedCashbacks)){
100
			foreach ($creditedCashbacks['data'] as $key => $value) {
101
				$creditedAmount += $value['userAmount'];				
102
				$data = array('subOrders.batchId'=>$value['batch']);
103
				$jsonVar = json_encode($data);
104
				$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=1&window=50&searchMap=$jsonVar";
105
				$creditedOrders[$value['batch']] = $this->make_request($url,null);
106
				// debug($creditedOrders);
107
			}
108
		}
109
		$storemapping = Configure::read('storemapping');
110
		$activestores = Configure::read('activestores');
111
		if(!empty($response['data'])){
112
			$this->set(compact('storemapping','activestores','pendingOrders','approvedOrders','creditedOrders','pendingCashbacks','creditedCashbacks','pendingAmount','approvedAmount','creditedAmount'));
113
		}
114
	}
115
 
17382 naman 116
 
117
	public function usercashbacks($userId) {	
118
		$this->set('byUser',$userId);
119
		$this->layout = "innerpages";
120
		$url = $this->apihost."storeorder/user/".$userId."?page=1&window=50";
121
		$response = $this->make_request($url,null);
122
		// debug($response);
123
		$creditedOrders = $pendingOrders = $approvedOrders = array();
124
		$creditedAmount = $pendingAmount = $approvedAmount = 0;
125
		if(!empty($response['data'])){
126
			foreach ($response['data'] as $key => $order) {
127
				if(!empty($order['subOrders'])){
128
					foreach ($order['subOrders'] as $key => $suborder) {
129
						$suborder['storeId'] = $order['storeId'];
130
						if($order['storeId']!=4){
131
							$suborder['merchantOrderId'] = $order['merchantOrderId'];
132
						} else {
133
							$suborder['merchantOrderId'] = $suborder['merchantSubOrderId'];
134
						}
135
						if(!empty($order['orderTrackingUrl'])){
136
							$suborder['orderSuccessUrl'] = $order['orderTrackingUrl'];
137
						}
138
						switch($suborder['cashBackStatus']){
139
							// case 'Credited to wallet'://Credited
140
							// $creditedOrders[] = $suborder;
141
							// break;
142
							case 'Approved':
143
							$approvedOrders[] = $suborder;
144
							$approvedAmount += $suborder['cashBackAmount'];
145
							break;
146
							case 'Pending':
147
							$pendingOrders[] = $suborder;
148
							// $pendingAmount += $suborder['cashBackAmount'];
149
						}
150
					}
151
				}
152
			}
153
		}
154
		$url = $this->apihost.'pending-cashbacks/user/'.$userId;
155
		$result = $this->make_request($url,null);
156
		$pendingAmount = $result['amount'];
157
		//Get pending cashbacks
158
		$url = $this->apihost.'pending-refunds/user/'.$userId;
159
		$pendingCashbacks = $this->make_request($url,null);
160
		//Get credited cashbacks
161
		$url = $this->apihost.'refund/user/'.$userId;
162
		$creditedCashbacks = $this->make_request($url,null);
163
 
164
		$creditKeyArray = array();
165
		$creditValueArray = array();
166
		$total_credited_amount = 0;
167
		if(!empty($creditedCashbacks)){
168
			foreach ($creditedCashbacks['data'] as $key => $value) {
169
				$creditedAmount += $value['userAmount'];				
170
				$data = array('subOrders.batchId'=>$value['batch']);
171
				$jsonVar = json_encode($data);
172
 
173
				$url = $this->apihost."storeorder/user/".$userId."?page=1&window=50&searchMap=$jsonVar";
174
				$creditedOrders[$value['batch']] = $this->make_request($url,null);
175
				$total_credited_amount =$total_credited_amount + $value['userAmount'];
176
				if($value['type']== 'Order')
177
				{
178
					$creditValueArray['amount'] = $value['userAmount'];
179
					$creditValueArray['type'] = $value['type'];
180
					$creditValueArray['fortbatchid'] = $value['batch'];
181
					$creditValueArray['creditedDate'] = date('Y-m-d',strtotime($value['timestamp']));
182
					// $creditKeyArray[date('Y-m-d',strtotime($value['timestamp']))] = $creditValueArray;
183
					$creditKeyArray[date('Y-m-d',strtotime($value['timestamp'])).$value['type']] = $creditValueArray;
184
				}
185
 
186
			}
187
		}
188
 
189
		$storemapping = Configure::read('storemapping');
190
		$activestores = Configure::read('activestores');
191
 
192
	// App Credit Start
193
		$creditedFortnight = array();
194
		$url = $this->apihost.'appUserCashBack/'.$userId.'/Credited';
195
		$getcredited = $this->make_request($url,null);
196
		// debug($getcredited);
197
		foreach ($getcredited['UserAppCashBack'] as $key => $value) {
198
 
199
 
200
 
201
			$url = $this->apihost.'appUserBatchDrillDown/'.$userId.'/'.$value['fortnightOfYear'].'/'.$value['yearVal'];
202
			$creditedFortnight[$value['fortnightOfYear']] = $this->make_request($url,null);
203
 
204
			$creditValueArray['amount'] = $value['amount'];
205
			// $total_credited_amount = $total_credited_amount + $value['amount'];
206
			$creditValueArray['type'] = 'App';
207
			$creditValueArray['fortbatchid'] = $value['fortnightOfYear'];
208
			$creditValueArray['creditedDate'] = $value['creditedDate'];
209
			$creditKeyArray[$value['creditedDate'].'App'] = $creditValueArray;
210
			// echo "total credit",$total_credited_amount;
211
			// echo $url;
212
		}
213
 
214
		ksort($creditKeyArray);
215
		// debug($creditKeyArray);
216
		$this->set(compact('getcredited','creditedFortnight','creditKeyArray','total_credited_amount'));
217
		// debug($creditedFortnight);
218
	// App Credit End
219
 
220
		// Approved Start
221
 
222
		$url = $this->apihost.'appUserCashBack/'.$userId.'/Approved';
223
		$getapproved = $this->make_request($url,null);
224
		$fortnight = array();
225
		$fortnight_amount = array();
226
		$counter = 0;
227
		$total_approved_amount = 0;
228
		$current_date =  date("Y");
229
		foreach ($getapproved["UserAppCashBack"] as $key => $value) {
230
			 $fortnight[$counter] = $value["fortnightOfYear"];
231
			 $fortnight_amount[$counter] = $value["amount"];
232
			 $total_approved_amount += $value["amount"];
233
			 $counter++;
234
		}
235
		$approvedFortnight = array();
236
		for($i=0; $i<count($fortnight); $i++){
237
			$url = $this->apihost.'appUserBatchDrillDown/'.$userId.'/'.$fortnight[$i].'/'.$current_date;
238
			$approvedFortnight[$i] = $this->make_request($url,null);
239
 
240
		}
241
		// debug($approvedFortnight);
242
		$this->set(compact('fortnight','total_approved_amount','fortnight_amount','approvedFortnight'));		
243
	// Approved End	
244
 
245
		if(!empty($response['data'])){
246
			$this->set(compact('storemapping','activestores','pendingOrders','approvedOrders','creditedOrders','pendingCashbacks','creditedCashbacks','pendingAmount','approvedAmount','creditedAmount'));
247
		}
248
	}
249
 
250
 
251
	public function by($userId) {
252
		$page = $this->request->query('page');
253
		$page = isset($page)?$page:1;		
254
		$this->layout = "innerpages";
255
		$url = $this->apihost."storeorder/user/".$userId."?page=$page&window=10";
256
		$response = $this->make_request($url,null);
257
		$totalPages = $response['totalPages'];
258
		if(!empty($response['data'])){
259
			$this->set('orders',$response['data']);
260
		}
261
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
262
		$storemapping = Configure::read('storemapping');
263
		$activestores = Configure::read('activestores');
264
		$amazonorderurl = Configure::read('amazonorderurl');
265
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl'));
266
	}
267
 
15403 manish.sha 268
/**
269
 * index method
270
 *
271
 * @return void
272
 */
273
	public function index() {
274
		throw new NotFoundException(__('Access Denied'));
275
		$this->Order->recursive = 0;
276
		$this->set('orders', $this->Paginator->paginate());
277
	}
278
 
279
/**
280
 * view method
281
 *
282
 * @throws NotFoundException
283
 * @param string $id
284
 * @return void
285
 */
286
	public function view($id = null) {
287
		throw new NotFoundException(__('Access Denied'));
288
		if (!$this->Order->exists($id)) {
289
			throw new NotFoundException(__('Invalid order'));
290
		}
291
		$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
292
		$this->set('order', $this->Order->find('first', $options));
293
	}
294
 
295
/**
296
 * add method
297
 *
298
 * @return void
299
 */
300
 
301
	public function postOrders($order=null) {
302
		// Configure::load('live');
303
		$apihost = Configure::read('pythonapihost');
304
		$url = $apihost."storeorder";
305
		if(!empty($order)) {
306
			$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']);
307
			$jsonVar = json_encode($params);
308
			return $this->make_request($url,$jsonVar);
309
		}else{
310
			$result = array('success'=>false,'message'=>'Empty order array');
311
			return $result;
312
		}
313
	}
314
 
315
	public function add() {
316
		$this->log(print_r($this->request->data,1),'orders');
317
		if ($this->request->is('post')) {
318
			if($this->request->data['zip']){
319
				$this->request->data['rawhtml'] = gzuncompress(base64_decode($this->request->data['rawhtml'])); 
320
			}
321
			$this->log(print_r($this->request->data,1),'orders');
322
			if(empty($this->request->data['id'])) {
323
				$this->Order->create();
324
			}
325
			$this->request->data['ip'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
326
			if ($this->Order->save($this->request->data)) {
327
				//$this->loadModel('PythonApi');	
328
				if(empty($this->request->data['id'])) {
329
					$id = $this->Order->getLastInsertID();
330
				}else{
331
					$id = $this->request->data['id'];
332
				}
333
				$order = $this->Order->find('first',array('conditions'=>array('id'=>$id),'recursive'=>-1));
334
				$response = $this->postOrders($order);
335
				$this->log(print_r($response,1),'orders');
336
				if(!empty($response) && $response['result']) {
337
					//if($response['result'] == 'HTML_REQUIRED' || $response['result'] == 'requireHtml') {
338
					if($response['htmlRequired'] == 1) {
339
						$this->loadModel('Rawhtml');
340
						$data = array('order_id' => $order['Order']['id'],'url' => $response['url'], 'status' => 'new');
341
						$this->Rawhtml->create();
342
						$this->Rawhtml->save($data); 
343
						//$result =array('success'=>true,'message'=>__('requireHtml'),'url' => $response['url'],'orderId' => $response['orderId']);
344
						$result =  $response;
345
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
346
					}/* elseif($response['result'] == 'IGNORED') {
347
						$result =array('success'=>true,'message'=>__('IGNORED'));
348
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
349
					} elseif($response['result'] == 'PARSE_ERROR') {
350
						$result =array('success'=>true,'message'=>__('PARSE_ERROR'));
351
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
352
					} */
353
					else {
354
						$result =array('success'=>true,'message'=> $response['result']);
355
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
356
					}
357
					$this->Order->query($sql);
358
				}
359
				//$result = array('success'=>true,'message'=>__('HTML_REQUIRED'),'url'=>'https://www.amazon.in/gp/css/summary/edit.html?orderID=404-7369214-6566739');
360
/*
361
				$options = array('conditions'=>array('status'=>'mapped'),'recursive'=>-1);
362
				$order = $this->Order->find('first',$options);
363
				if(!empty($orders)) {
364
					foreach($orders AS $order) {
365
						$response = $this->PythonApi->postOrders($order);
366
						if(!empty($response) && $response['result']) {
367
							$sql = "UPDATE orders SET status = 'processed' WHERE id = ".$order['Order']['id'];
368
							$this->Order->query($sql);
369
						}
370
					}
371
				}
372
*/
373
			} else {
374
				$this->log(print_r($this->Order->validationErrors,1),'orders');
375
				$result = array('success'=>false,'message'=>__('The order could not be saved. Please, try again.'));
376
			}
377
			$this->response->type('json');
378
			$this->layout = 'ajax';
379
			$this->set(array(
380
			    'result' => $response,
381
			    // 'callback' => $callback,
382
			    '_serialize' => array('result')
383
			));
384
			$this->render('/Elements/json');		
385
		}			
386
	}
387
 
388
/**
389
 * edit method
390
 *
391
 * @throws NotFoundException
392
 * @param string $id
393
 * @return void
394
 */
395
	public function edit($id = null) {
396
		throw new NotFoundException(__('Access Denied'));
397
		if (!$this->Order->exists($id)) {
398
			throw new NotFoundException(__('Invalid order'));
399
		}
400
		if ($this->request->is(array('post', 'put'))) {
401
			if ($this->Order->save($this->request->data)) {
402
				$this->Session->setFlash(__('The order has been saved.'));
403
				return $this->redirect(array('action' => 'index'));
404
			} else {
405
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
406
			}
407
		} else {
408
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
409
			$this->request->data = $this->Order->find('first', $options);
410
		}
411
		$users = $this->Order->User->find('list');
412
		$stores = $this->Order->Store->find('list');
413
		$storeOrders = $this->Order->StoreOrder->find('list');
414
		$this->set(compact('users', 'stores', 'storeOrders'));
415
	}
416
 
417
/**
418
 * delete method
419
 *
420
 * @throws NotFoundException
421
 * @param string $id
422
 * @return void
423
 */
424
	public function delete($id = null) {
425
		throw new NotFoundException(__('Access Denied'));
426
		$this->Order->id = $id;
427
		if (!$this->Order->exists()) {
428
			throw new NotFoundException(__('Invalid order'));
429
		}
430
		$this->request->onlyAllow('post', 'delete');
431
		if ($this->Order->delete()) {
432
			$this->Session->setFlash(__('The order has been deleted.'));
433
		} else {
434
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
435
		}
436
		return $this->redirect(array('action' => 'index'));
437
	}
438
 
439
/**
440
 * admin_index method
441
 *
442
 * @return void
443
 */
444
	public function admin_index() {
445
		$this->Order->recursive = 0;
446
		$this->set('orders', $this->Paginator->paginate());
447
	}
448
 
449
/**
450
 * admin_view method
451
 *
452
 * @throws NotFoundException
453
 * @param string $id
454
 * @return void
455
 */
456
	public function admin_view($id = null) {
457
		if (!$this->Order->exists($id)) {
458
			throw new NotFoundException(__('Invalid order'));
459
		}
460
		$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
461
		$this->set('order', $this->Order->find('first', $options));
462
	}
463
 
464
/**
465
 * admin_add method
466
 *
467
 * @return void
468
 */
469
	public function admin_add() {
470
		if ($this->request->is('post')) {
471
			$this->Order->create();
472
			if ($this->Order->save($this->request->data)) {
473
				$this->Session->setFlash(__('The order has been saved.'));
474
				return $this->redirect(array('action' => 'index'));
475
			} else {
476
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
477
			}
478
		}
479
		$users = $this->Order->User->find('list');
480
		$stores = $this->Order->Store->find('list');
481
		$storeOrders = $this->Order->StoreOrder->find('list');
482
		$this->set(compact('users', 'stores', 'storeOrders'));
483
	}
484
 
485
/**
486
 * admin_edit method
487
 *
488
 * @throws NotFoundException
489
 * @param string $id
490
 * @return void
491
 */
492
	public function admin_edit($id = null) {
493
		if (!$this->Order->exists($id)) {
494
			throw new NotFoundException(__('Invalid order'));
495
		}
496
		if ($this->request->is(array('post', 'put'))) {
497
			if ($this->Order->save($this->request->data)) {
498
				$this->Session->setFlash(__('The order has been saved.'));
499
				return $this->redirect(array('action' => 'index'));
500
			} else {
501
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
502
			}
503
		} else {
504
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
505
			$this->request->data = $this->Order->find('first', $options);
506
		}
507
		$users = $this->Order->User->find('list');
508
		$stores = $this->Order->Store->find('list');
509
		$storeOrders = $this->Order->StoreOrder->find('list');
510
		$this->set(compact('users', 'stores', 'storeOrders'));
511
	}
512
 
513
/**
514
 * admin_delete method
515
 *
516
 * @throws NotFoundException
517
 * @param string $id
518
 * @return void
519
 */
520
	public function admin_delete($id = null) {
521
		$this->Order->id = $id;
522
		if (!$this->Order->exists()) {
523
			throw new NotFoundException(__('Invalid order'));
524
		}
525
		$this->request->onlyAllow('post', 'delete');
526
		if ($this->Order->delete()) {
527
			$this->Session->setFlash(__('The order has been deleted.'));
528
		} else {
529
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
530
		}
531
		return $this->redirect(array('action' => 'index'));
532
	}
533
 
534
	public function all() {
535
		$page = $this->request->query('page');
536
		$page = isset($page)?$page:1;
537
		// $userId = $this->request->query('user_id');
538
		// if(isset($userId) && !empty($userId)){
539
		// 	$this->loadModel('User');
540
		// 	$dbuser = $this->User->findById($userId);
541
		// 	$this->Auth->login($dbuser['User']);	
542
		// }
543
		$this->layout = "innerpages";
544
		$url = $this->apihost."orders/?page=$page&window=10";
545
		$response = $this->make_request($url,null);
546
		$totalPages = $response['totalPages'];		
547
		if(!empty($response['data'])){
548
			$this->set('orders',$response['data']);
549
		}
550
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
551
		$storemapping = Configure::read('storemapping');
552
		$activestores = Configure::read('activestores');
553
		$amazonorderurl = Configure::read('amazonorderurl');
554
		$allusers = $this->Order->User->find('all',array('fields'=>array('first_name','id'),'recursive'=>-1));
555
		foreach($allusers AS $user){
556
			$users[$user['User']['id']] = $user['User']['first_name'];
557
		}
558
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl','users'));
559
	}
560
}