Subversion Repositories SmartDukaan

Rev

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