Subversion Repositories SmartDukaan

Rev

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