Subversion Repositories SmartDukaan

Rev

Rev 14886 | Rev 15035 | 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();
14561 anikendra 20
		$this->Auth->allow('add','mine','pendingcashbacks');
13672 anikendra 21
		$this->apihost = Configure::read('pythonapihost');
13591 anikendra 22
	}
23
 
13816 anikendra 24
	public function mine() {
25
		$page = $this->request->query('page');
26
		$page = isset($page)?$page:1;
13682 anikendra 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
		}
13672 anikendra 33
		$this->layout = "innerpages";
13815 anikendra 34
		$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=$page&window=10";
13672 anikendra 35
		$response = $this->make_request($url,null);
13815 anikendra 36
		$totalPages = $response['totalPages'];
13672 anikendra 37
		if(!empty($response['data'])){
38
			$this->set('orders',$response['data']);
39
		}
13752 anikendra 40
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
13944 anikendra 41
		$storemapping = Configure::read('storemapping');
42
		$activestores = Configure::read('activestores');
14224 anikendra 43
		$amazonorderurl = Configure::read('amazonorderurl');
44
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl'));
13672 anikendra 45
	}
46
 
13762 anikendra 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";
13993 anikendra 55
		$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=1&window=50";
13762 anikendra 56
		$response = $this->make_request($url,null);
13993 anikendra 57
		// debug($response);
58
		$creditedOrders = $pendingOrders = $approvedOrders = array();
59
		$creditedAmount = $pendingAmount = $approvedAmount = 0;
13762 anikendra 60
		if(!empty($response['data'])){
13993 anikendra 61
			foreach ($response['data'] as $key => $order) {
14111 anikendra 62
				if(!empty($order['subOrders'])){
63
					foreach ($order['subOrders'] as $key => $suborder) {
64
						$suborder['storeId'] = $order['storeId'];
65
						$suborder['merchantOrderId'] = $order['merchantOrderId'];
14700 anikendra 66
						if(!empty($order['orderTrackingUrl'])){
67
							$suborder['orderSuccessUrl'] = $order['orderTrackingUrl'];
68
						}
14111 anikendra 69
						switch($suborder['cashBackStatus']){
70
							// case 'Credited to wallet'://Credited
71
							// $creditedOrders[] = $suborder;
72
							// break;
73
							case 'Approved':
74
							$approvedOrders[] = $suborder;
75
							$approvedAmount += $suborder['cashBackAmount'];
76
							break;
77
							case 'Pending':
78
							$pendingOrders[] = $suborder;
14673 anikendra 79
							// $pendingAmount += $suborder['cashBackAmount'];
14111 anikendra 80
						}
13993 anikendra 81
					}
82
				}
83
			}
13762 anikendra 84
		}
14673 anikendra 85
		$url = $this->apihost.'pending-cashbacks/user/'.$userId;
86
		$result = $this->make_request($url,null);
87
		$pendingAmount = $result['amount'];
13993 anikendra 88
		//Get pending cashbacks
89
		$url = $this->apihost.'pending-refunds/user/'.$userId;
90
		$pendingCashbacks = $this->make_request($url,null);
91
		//Get credited cashbacks
92
		$url = $this->apihost.'refund/user/'.$userId;
14068 anikendra 93
		$creditedCashbacks = $this->make_request($url,null);
94
 
14026 anikendra 95
		if(!empty($creditedCashbacks)){
13993 anikendra 96
			foreach ($creditedCashbacks['data'] as $key => $value) {
14068 anikendra 97
				$creditedAmount += $value['userAmount'];				
98
				$data = array('subOrders.batchId'=>$value['batch']);
99
				$jsonVar = json_encode($data);
100
				$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=1&window=50&searchMap=$jsonVar";
101
				$creditedOrders[$value['batch']] = $this->make_request($url,null);
102
				// debug($creditedOrders);
13993 anikendra 103
			}
104
		}
14026 anikendra 105
		$storemapping = Configure::read('storemapping');
106
		$activestores = Configure::read('activestores');
13993 anikendra 107
		if(!empty($response['data'])){
14026 anikendra 108
			$this->set(compact('storemapping','activestores','pendingOrders','approvedOrders','creditedOrders','pendingCashbacks','creditedCashbacks','pendingAmount','approvedAmount','creditedAmount'));
13993 anikendra 109
		}
13762 anikendra 110
	}
111
 
13532 anikendra 112
/**
113
 * index method
114
 *
115
 * @return void
116
 */
117
	public function index() {
13591 anikendra 118
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 119
		$this->Order->recursive = 0;
120
		$this->set('orders', $this->Paginator->paginate());
121
	}
122
 
123
/**
124
 * view method
125
 *
126
 * @throws NotFoundException
127
 * @param string $id
128
 * @return void
129
 */
130
	public function view($id = null) {
13591 anikendra 131
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 132
		if (!$this->Order->exists($id)) {
133
			throw new NotFoundException(__('Invalid order'));
134
		}
135
		$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
136
		$this->set('order', $this->Order->find('first', $options));
137
	}
138
 
139
/**
140
 * add method
141
 *
142
 * @return void
143
 */
13814 anikendra 144
 
145
	public function postOrders($order=null) {
13994 anikendra 146
		// Configure::load('live');
13814 anikendra 147
		$apihost = Configure::read('pythonapihost');
148
		$url = $apihost."storeorder";
149
		if(!empty($order)) {
150
			$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']);
151
			$jsonVar = json_encode($params);
152
			return $this->make_request($url,$jsonVar);
153
		}else{
154
			$result = array('success'=>false,'message'=>'Empty order array');
155
			return $result;
156
		}
157
	}
158
 
13532 anikendra 159
	public function add() {
14933 amit.gupta 160
		$this->log(print_r($this->request->data,1),'orders');
13532 anikendra 161
		if ($this->request->is('post')) {
14886 amit.gupta 162
			if($this->request->data['zip']){
14933 amit.gupta 163
				$this->request->data['rawhtml'] = gzuncompress(base64_decode($this->request->data['rawhtml'])); 
14886 amit.gupta 164
			}
165
			$this->log(print_r($this->request->data,1),'orders');
14315 anikendra 166
			if(empty($this->request->data['id'])) {
167
				$this->Order->create();
13633 anikendra 168
			}
13532 anikendra 169
			if ($this->Order->save($this->request->data)) {
14315 anikendra 170
				//$this->loadModel('PythonApi');	
171
				if(empty($this->request->data['id'])) {
172
					$id = $this->Order->getLastInsertID();
173
				}else{
174
					$id = $this->request->data['id'];
175
				}
176
				$order = $this->Order->find('first',array('conditions'=>array('id'=>$id),'recursive'=>-1));
13814 anikendra 177
				$response = $this->postOrders($order);
178
				$this->log(print_r($response,1),'orders');
179
				if(!empty($response) && $response['result']) {
14315 anikendra 180
					//if($response['result'] == 'HTML_REQUIRED' || $response['result'] == 'requireHtml') {
181
					if($response['htmlRequired'] == 1) {
13814 anikendra 182
						$this->loadModel('Rawhtml');
183
						$data = array('order_id' => $order['Order']['id'],'url' => $response['url'], 'status' => 'new');
184
						$this->Rawhtml->create();
185
						$this->Rawhtml->save($data); 
14315 anikendra 186
						//$result =array('success'=>true,'message'=>__('requireHtml'),'url' => $response['url'],'orderId' => $response['orderId']);
187
						$result =  $response;
188
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
189
					}/* elseif($response['result'] == 'IGNORED') {
13814 anikendra 190
						$result =array('success'=>true,'message'=>__('IGNORED'));
191
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
192
					} elseif($response['result'] == 'PARSE_ERROR') {
193
						$result =array('success'=>true,'message'=>__('PARSE_ERROR'));
194
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
14315 anikendra 195
					} */
196
					else {
197
						$result =array('success'=>true,'message'=> $response['result']);
198
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
13814 anikendra 199
					}
200
					$this->Order->query($sql);
201
				}
202
				//$result = array('success'=>true,'message'=>__('HTML_REQUIRED'),'url'=>'https://www.amazon.in/gp/css/summary/edit.html?orderID=404-7369214-6566739');
13633 anikendra 203
/*
204
				$options = array('conditions'=>array('status'=>'mapped'),'recursive'=>-1);
205
				$order = $this->Order->find('first',$options);
206
				if(!empty($orders)) {
207
					foreach($orders AS $order) {
208
						$response = $this->PythonApi->postOrders($order);
209
						if(!empty($response) && $response['result']) {
210
							$sql = "UPDATE orders SET status = 'processed' WHERE id = ".$order['Order']['id'];
211
							$this->Order->query($sql);
212
						}
213
					}
214
				}
215
*/
13532 anikendra 216
			} else {
14315 anikendra 217
				$this->log(print_r($this->Order->validationErrors,1),'orders');
13591 anikendra 218
				$result = array('success'=>false,'message'=>__('The order could not be saved. Please, try again.'));
13532 anikendra 219
			}
13591 anikendra 220
			$this->response->type('json');
221
			$this->layout = 'ajax';
222
			$this->set(array(
14315 anikendra 223
			    'result' => $response,
13591 anikendra 224
			    // 'callback' => $callback,
225
			    '_serialize' => array('result')
226
			));
227
			$this->render('/Elements/json');		
228
		}			
13532 anikendra 229
	}
230
 
231
/**
232
 * edit method
233
 *
234
 * @throws NotFoundException
235
 * @param string $id
236
 * @return void
237
 */
238
	public function edit($id = null) {
13591 anikendra 239
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 240
		if (!$this->Order->exists($id)) {
241
			throw new NotFoundException(__('Invalid order'));
242
		}
243
		if ($this->request->is(array('post', 'put'))) {
244
			if ($this->Order->save($this->request->data)) {
245
				$this->Session->setFlash(__('The order has been saved.'));
246
				return $this->redirect(array('action' => 'index'));
247
			} else {
248
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
249
			}
250
		} else {
251
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
252
			$this->request->data = $this->Order->find('first', $options);
253
		}
254
		$users = $this->Order->User->find('list');
255
		$stores = $this->Order->Store->find('list');
256
		$storeOrders = $this->Order->StoreOrder->find('list');
257
		$this->set(compact('users', 'stores', 'storeOrders'));
258
	}
259
 
260
/**
261
 * delete method
262
 *
263
 * @throws NotFoundException
264
 * @param string $id
265
 * @return void
266
 */
267
	public function delete($id = null) {
13591 anikendra 268
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 269
		$this->Order->id = $id;
270
		if (!$this->Order->exists()) {
271
			throw new NotFoundException(__('Invalid order'));
272
		}
273
		$this->request->onlyAllow('post', 'delete');
274
		if ($this->Order->delete()) {
275
			$this->Session->setFlash(__('The order has been deleted.'));
276
		} else {
277
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
278
		}
279
		return $this->redirect(array('action' => 'index'));
280
	}
281
 
282
/**
283
 * admin_index method
284
 *
285
 * @return void
286
 */
287
	public function admin_index() {
288
		$this->Order->recursive = 0;
289
		$this->set('orders', $this->Paginator->paginate());
290
	}
291
 
292
/**
293
 * admin_view method
294
 *
295
 * @throws NotFoundException
296
 * @param string $id
297
 * @return void
298
 */
299
	public function admin_view($id = null) {
300
		if (!$this->Order->exists($id)) {
301
			throw new NotFoundException(__('Invalid order'));
302
		}
303
		$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
304
		$this->set('order', $this->Order->find('first', $options));
305
	}
306
 
307
/**
308
 * admin_add method
309
 *
310
 * @return void
311
 */
312
	public function admin_add() {
313
		if ($this->request->is('post')) {
314
			$this->Order->create();
315
			if ($this->Order->save($this->request->data)) {
316
				$this->Session->setFlash(__('The order has been saved.'));
317
				return $this->redirect(array('action' => 'index'));
318
			} else {
319
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
320
			}
321
		}
322
		$users = $this->Order->User->find('list');
323
		$stores = $this->Order->Store->find('list');
324
		$storeOrders = $this->Order->StoreOrder->find('list');
325
		$this->set(compact('users', 'stores', 'storeOrders'));
326
	}
327
 
328
/**
329
 * admin_edit method
330
 *
331
 * @throws NotFoundException
332
 * @param string $id
333
 * @return void
334
 */
335
	public function admin_edit($id = null) {
336
		if (!$this->Order->exists($id)) {
337
			throw new NotFoundException(__('Invalid order'));
338
		}
339
		if ($this->request->is(array('post', 'put'))) {
340
			if ($this->Order->save($this->request->data)) {
341
				$this->Session->setFlash(__('The order has been saved.'));
342
				return $this->redirect(array('action' => 'index'));
343
			} else {
344
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
345
			}
346
		} else {
347
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
348
			$this->request->data = $this->Order->find('first', $options);
349
		}
350
		$users = $this->Order->User->find('list');
351
		$stores = $this->Order->Store->find('list');
352
		$storeOrders = $this->Order->StoreOrder->find('list');
353
		$this->set(compact('users', 'stores', 'storeOrders'));
354
	}
355
 
356
/**
357
 * admin_delete method
358
 *
359
 * @throws NotFoundException
360
 * @param string $id
361
 * @return void
362
 */
363
	public function admin_delete($id = null) {
364
		$this->Order->id = $id;
365
		if (!$this->Order->exists()) {
366
			throw new NotFoundException(__('Invalid order'));
367
		}
368
		$this->request->onlyAllow('post', 'delete');
369
		if ($this->Order->delete()) {
370
			$this->Session->setFlash(__('The order has been deleted.'));
371
		} else {
372
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
373
		}
374
		return $this->redirect(array('action' => 'index'));
14354 anikendra 375
	}
376
 
377
	public function all() {
378
		$page = $this->request->query('page');
379
		$page = isset($page)?$page:1;
14509 anikendra 380
		// $userId = $this->request->query('user_id');
381
		// if(isset($userId) && !empty($userId)){
382
		// 	$this->loadModel('User');
383
		// 	$dbuser = $this->User->findById($userId);
384
		// 	$this->Auth->login($dbuser['User']);	
385
		// }
14354 anikendra 386
		$this->layout = "innerpages";
387
		$url = $this->apihost."orders/?page=$page&window=10";
388
		$response = $this->make_request($url,null);
14509 anikendra 389
		$totalPages = $response['totalPages'];		
14354 anikendra 390
		if(!empty($response['data'])){
391
			$this->set('orders',$response['data']);
392
		}
393
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
394
		$storemapping = Configure::read('storemapping');
395
		$activestores = Configure::read('activestores');
396
		$amazonorderurl = Configure::read('amazonorderurl');
397
		$allusers = $this->Order->User->find('all',array('fields'=>array('first_name','id'),'recursive'=>-1));
398
		foreach($allusers AS $user){
399
			$users[$user['User']['id']] = $user['User']['first_name'];
400
		}
401
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl','users'));
402
	}
403
}