Subversion Repositories SmartDukaan

Rev

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