Subversion Repositories SmartDukaan

Rev

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