Subversion Repositories SmartDukaan

Rev

Rev 14700 | Rev 14933 | 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() {
160
		if ($this->request->is('post')) {
14886 amit.gupta 161
			if($this->request->data['zip']){
162
				$this->request->data['rawhtml'] = gzuncompress($this->request->data['rawhtml']); 
163
			}
164
			$this->log(print_r($this->request->data,1),'orders');
14315 anikendra 165
			if(empty($this->request->data['id'])) {
166
				$this->Order->create();
13633 anikendra 167
			}
13532 anikendra 168
			if ($this->Order->save($this->request->data)) {
14315 anikendra 169
				//$this->loadModel('PythonApi');	
170
				if(empty($this->request->data['id'])) {
171
					$id = $this->Order->getLastInsertID();
172
				}else{
173
					$id = $this->request->data['id'];
174
				}
175
				$order = $this->Order->find('first',array('conditions'=>array('id'=>$id),'recursive'=>-1));
13814 anikendra 176
				$response = $this->postOrders($order);
177
				$this->log(print_r($response,1),'orders');
178
				if(!empty($response) && $response['result']) {
14315 anikendra 179
					//if($response['result'] == 'HTML_REQUIRED' || $response['result'] == 'requireHtml') {
180
					if($response['htmlRequired'] == 1) {
13814 anikendra 181
						$this->loadModel('Rawhtml');
182
						$data = array('order_id' => $order['Order']['id'],'url' => $response['url'], 'status' => 'new');
183
						$this->Rawhtml->create();
184
						$this->Rawhtml->save($data); 
14315 anikendra 185
						//$result =array('success'=>true,'message'=>__('requireHtml'),'url' => $response['url'],'orderId' => $response['orderId']);
186
						$result =  $response;
187
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
188
					}/* elseif($response['result'] == 'IGNORED') {
13814 anikendra 189
						$result =array('success'=>true,'message'=>__('IGNORED'));
190
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
191
					} elseif($response['result'] == 'PARSE_ERROR') {
192
						$result =array('success'=>true,'message'=>__('PARSE_ERROR'));
193
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
14315 anikendra 194
					} */
195
					else {
196
						$result =array('success'=>true,'message'=> $response['result']);
197
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
13814 anikendra 198
					}
199
					$this->Order->query($sql);
200
				}
201
				//$result = array('success'=>true,'message'=>__('HTML_REQUIRED'),'url'=>'https://www.amazon.in/gp/css/summary/edit.html?orderID=404-7369214-6566739');
13633 anikendra 202
/*
203
				$options = array('conditions'=>array('status'=>'mapped'),'recursive'=>-1);
204
				$order = $this->Order->find('first',$options);
205
				if(!empty($orders)) {
206
					foreach($orders AS $order) {
207
						$response = $this->PythonApi->postOrders($order);
208
						if(!empty($response) && $response['result']) {
209
							$sql = "UPDATE orders SET status = 'processed' WHERE id = ".$order['Order']['id'];
210
							$this->Order->query($sql);
211
						}
212
					}
213
				}
214
*/
13532 anikendra 215
			} else {
14315 anikendra 216
				$this->log(print_r($this->Order->validationErrors,1),'orders');
13591 anikendra 217
				$result = array('success'=>false,'message'=>__('The order could not be saved. Please, try again.'));
13532 anikendra 218
			}
13591 anikendra 219
			$this->response->type('json');
220
			$this->layout = 'ajax';
221
			$this->set(array(
14315 anikendra 222
			    'result' => $response,
13591 anikendra 223
			    // 'callback' => $callback,
224
			    '_serialize' => array('result')
225
			));
226
			$this->render('/Elements/json');		
227
		}			
13532 anikendra 228
	}
229
 
230
/**
231
 * edit method
232
 *
233
 * @throws NotFoundException
234
 * @param string $id
235
 * @return void
236
 */
237
	public function edit($id = null) {
13591 anikendra 238
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 239
		if (!$this->Order->exists($id)) {
240
			throw new NotFoundException(__('Invalid order'));
241
		}
242
		if ($this->request->is(array('post', 'put'))) {
243
			if ($this->Order->save($this->request->data)) {
244
				$this->Session->setFlash(__('The order has been saved.'));
245
				return $this->redirect(array('action' => 'index'));
246
			} else {
247
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
248
			}
249
		} else {
250
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
251
			$this->request->data = $this->Order->find('first', $options);
252
		}
253
		$users = $this->Order->User->find('list');
254
		$stores = $this->Order->Store->find('list');
255
		$storeOrders = $this->Order->StoreOrder->find('list');
256
		$this->set(compact('users', 'stores', 'storeOrders'));
257
	}
258
 
259
/**
260
 * delete method
261
 *
262
 * @throws NotFoundException
263
 * @param string $id
264
 * @return void
265
 */
266
	public function delete($id = null) {
13591 anikendra 267
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 268
		$this->Order->id = $id;
269
		if (!$this->Order->exists()) {
270
			throw new NotFoundException(__('Invalid order'));
271
		}
272
		$this->request->onlyAllow('post', 'delete');
273
		if ($this->Order->delete()) {
274
			$this->Session->setFlash(__('The order has been deleted.'));
275
		} else {
276
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
277
		}
278
		return $this->redirect(array('action' => 'index'));
279
	}
280
 
281
/**
282
 * admin_index method
283
 *
284
 * @return void
285
 */
286
	public function admin_index() {
287
		$this->Order->recursive = 0;
288
		$this->set('orders', $this->Paginator->paginate());
289
	}
290
 
291
/**
292
 * admin_view method
293
 *
294
 * @throws NotFoundException
295
 * @param string $id
296
 * @return void
297
 */
298
	public function admin_view($id = null) {
299
		if (!$this->Order->exists($id)) {
300
			throw new NotFoundException(__('Invalid order'));
301
		}
302
		$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
303
		$this->set('order', $this->Order->find('first', $options));
304
	}
305
 
306
/**
307
 * admin_add method
308
 *
309
 * @return void
310
 */
311
	public function admin_add() {
312
		if ($this->request->is('post')) {
313
			$this->Order->create();
314
			if ($this->Order->save($this->request->data)) {
315
				$this->Session->setFlash(__('The order has been saved.'));
316
				return $this->redirect(array('action' => 'index'));
317
			} else {
318
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
319
			}
320
		}
321
		$users = $this->Order->User->find('list');
322
		$stores = $this->Order->Store->find('list');
323
		$storeOrders = $this->Order->StoreOrder->find('list');
324
		$this->set(compact('users', 'stores', 'storeOrders'));
325
	}
326
 
327
/**
328
 * admin_edit method
329
 *
330
 * @throws NotFoundException
331
 * @param string $id
332
 * @return void
333
 */
334
	public function admin_edit($id = null) {
335
		if (!$this->Order->exists($id)) {
336
			throw new NotFoundException(__('Invalid order'));
337
		}
338
		if ($this->request->is(array('post', 'put'))) {
339
			if ($this->Order->save($this->request->data)) {
340
				$this->Session->setFlash(__('The order has been saved.'));
341
				return $this->redirect(array('action' => 'index'));
342
			} else {
343
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
344
			}
345
		} else {
346
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
347
			$this->request->data = $this->Order->find('first', $options);
348
		}
349
		$users = $this->Order->User->find('list');
350
		$stores = $this->Order->Store->find('list');
351
		$storeOrders = $this->Order->StoreOrder->find('list');
352
		$this->set(compact('users', 'stores', 'storeOrders'));
353
	}
354
 
355
/**
356
 * admin_delete method
357
 *
358
 * @throws NotFoundException
359
 * @param string $id
360
 * @return void
361
 */
362
	public function admin_delete($id = null) {
363
		$this->Order->id = $id;
364
		if (!$this->Order->exists()) {
365
			throw new NotFoundException(__('Invalid order'));
366
		}
367
		$this->request->onlyAllow('post', 'delete');
368
		if ($this->Order->delete()) {
369
			$this->Session->setFlash(__('The order has been deleted.'));
370
		} else {
371
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
372
		}
373
		return $this->redirect(array('action' => 'index'));
14354 anikendra 374
	}
375
 
376
	public function all() {
377
		$page = $this->request->query('page');
378
		$page = isset($page)?$page:1;
14509 anikendra 379
		// $userId = $this->request->query('user_id');
380
		// if(isset($userId) && !empty($userId)){
381
		// 	$this->loadModel('User');
382
		// 	$dbuser = $this->User->findById($userId);
383
		// 	$this->Auth->login($dbuser['User']);	
384
		// }
14354 anikendra 385
		$this->layout = "innerpages";
386
		$url = $this->apihost."orders/?page=$page&window=10";
387
		$response = $this->make_request($url,null);
14509 anikendra 388
		$totalPages = $response['totalPages'];		
14354 anikendra 389
		if(!empty($response['data'])){
390
			$this->set('orders',$response['data']);
391
		}
392
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
393
		$storemapping = Configure::read('storemapping');
394
		$activestores = Configure::read('activestores');
395
		$amazonorderurl = Configure::read('amazonorderurl');
396
		$allusers = $this->Order->User->find('all',array('fields'=>array('first_name','id'),'recursive'=>-1));
397
		foreach($allusers AS $user){
398
			$users[$user['User']['id']] = $user['User']['first_name'];
399
		}
400
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl','users'));
401
	}
402
}