Subversion Repositories SmartDukaan

Rev

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