Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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
 
18
	public function beforeFilter() {		
19
		parent::beforeFilter();
20
		$this->Auth->allow('add','mine','pendingcashbacks','all');
21
		$this->apihost = Configure::read('pythonapihost');
22
	}
23
 
24
	public function mine() {
25
		$page = $this->request->query('page');
26
		$page = isset($page)?$page:1;
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
		}
33
		$this->layout = "innerpages";
34
		$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=$page&window=10";
35
		$response = $this->make_request($url,null);
36
		$totalPages = $response['totalPages'];
37
		if(!empty($response['data'])){
38
			$this->set('orders',$response['data']);
39
		}
40
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
41
		$storemapping = Configure::read('storemapping');
42
		$activestores = Configure::read('activestores');
43
		$amazonorderurl = Configure::read('amazonorderurl');
44
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl'));
45
	}
46
 
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";
55
		$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=1&window=50";
56
		$response = $this->make_request($url,null);
57
		// debug($response);
58
		$creditedOrders = $pendingOrders = $approvedOrders = array();
59
		$creditedAmount = $pendingAmount = $approvedAmount = 0;
60
		if(!empty($response['data'])){
61
			foreach ($response['data'] as $key => $order) {
62
				if(!empty($order['subOrders'])){
63
					foreach ($order['subOrders'] as $key => $suborder) {
64
						$suborder['storeId'] = $order['storeId'];
65
						if($order['storeId']!=4){
66
							$suborder['merchantOrderId'] = $order['merchantOrderId'];
67
						} else {
68
							$suborder['merchantOrderId'] = $suborder['merchantSubOrderId'];
69
						}
70
						if(!empty($order['orderTrackingUrl'])){
71
							$suborder['orderSuccessUrl'] = $order['orderTrackingUrl'];
72
						}
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;
83
							// $pendingAmount += $suborder['cashBackAmount'];
84
						}
85
					}
86
				}
87
			}
88
		}
89
		$url = $this->apihost.'pending-cashbacks/user/'.$userId;
90
		$result = $this->make_request($url,null);
91
		$pendingAmount = $result['amount'];
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;
97
		$creditedCashbacks = $this->make_request($url,null);
98
 
99
		if(!empty($creditedCashbacks)){
100
			foreach ($creditedCashbacks['data'] as $key => $value) {
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);
107
			}
108
		}
109
		$storemapping = Configure::read('storemapping');
110
		$activestores = Configure::read('activestores');
111
		if(!empty($response['data'])){
112
			$this->set(compact('storemapping','activestores','pendingOrders','approvedOrders','creditedOrders','pendingCashbacks','creditedCashbacks','pendingAmount','approvedAmount','creditedAmount'));
113
		}
114
	}
115
 
116
/**
117
 * index method
118
 *
119
 * @return void
120
 */
121
	public function index() {
122
		throw new NotFoundException(__('Access Denied'));
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) {
135
		throw new NotFoundException(__('Access Denied'));
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
 */
148
 
149
	public function postOrders($order=null) {
150
		// Configure::load('live');
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
 
163
	public function add() {
164
		$this->log(print_r($this->request->data,1),'orders');
165
		if ($this->request->is('post')) {
166
			if($this->request->data['zip']){
167
				$this->request->data['rawhtml'] = gzuncompress(base64_decode($this->request->data['rawhtml'])); 
168
			}
169
			$this->log(print_r($this->request->data,1),'orders');
170
			if(empty($this->request->data['id'])) {
171
				$this->Order->create();
172
			}
173
			$this->request->data['ip'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
174
			if ($this->Order->save($this->request->data)) {
175
				//$this->loadModel('PythonApi');	
176
				if(empty($this->request->data['id'])) {
177
					$id = $this->Order->getLastInsertID();
178
				}else{
179
					$id = $this->request->data['id'];
180
				}
181
				$order = $this->Order->find('first',array('conditions'=>array('id'=>$id),'recursive'=>-1));
182
				$response = $this->postOrders($order);
183
				$this->log(print_r($response,1),'orders');
184
				if(!empty($response) && $response['result']) {
185
					//if($response['result'] == 'HTML_REQUIRED' || $response['result'] == 'requireHtml') {
186
					if($response['htmlRequired'] == 1) {
187
						$this->loadModel('Rawhtml');
188
						$data = array('order_id' => $order['Order']['id'],'url' => $response['url'], 'status' => 'new');
189
						$this->Rawhtml->create();
190
						$this->Rawhtml->save($data); 
191
						//$result =array('success'=>true,'message'=>__('requireHtml'),'url' => $response['url'],'orderId' => $response['orderId']);
192
						$result =  $response;
193
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
194
					}/* elseif($response['result'] == 'IGNORED') {
195
						$result =array('success'=>true,'message'=>__('IGNORED'));
196
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
197
					} elseif($response['result'] == 'PARSE_ERROR') {
198
						$result =array('success'=>true,'message'=>__('PARSE_ERROR'));
199
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
200
					} */
201
					else {
202
						$result =array('success'=>true,'message'=> $response['result']);
203
						$sql = "UPDATE orders SET status = '".$response['result']."' WHERE id = ".$order['Order']['id'];
204
					}
205
					$this->Order->query($sql);
206
				}
207
				//$result = array('success'=>true,'message'=>__('HTML_REQUIRED'),'url'=>'https://www.amazon.in/gp/css/summary/edit.html?orderID=404-7369214-6566739');
208
/*
209
				$options = array('conditions'=>array('status'=>'mapped'),'recursive'=>-1);
210
				$order = $this->Order->find('first',$options);
211
				if(!empty($orders)) {
212
					foreach($orders AS $order) {
213
						$response = $this->PythonApi->postOrders($order);
214
						if(!empty($response) && $response['result']) {
215
							$sql = "UPDATE orders SET status = 'processed' WHERE id = ".$order['Order']['id'];
216
							$this->Order->query($sql);
217
						}
218
					}
219
				}
220
*/
221
			} else {
222
				$this->log(print_r($this->Order->validationErrors,1),'orders');
223
				$result = array('success'=>false,'message'=>__('The order could not be saved. Please, try again.'));
224
			}
225
			$this->response->type('json');
226
			$this->layout = 'ajax';
227
			$this->set(array(
228
			    'result' => $response,
229
			    // 'callback' => $callback,
230
			    '_serialize' => array('result')
231
			));
232
			$this->render('/Elements/json');		
233
		}			
234
	}
235
 
236
/**
237
 * edit method
238
 *
239
 * @throws NotFoundException
240
 * @param string $id
241
 * @return void
242
 */
243
	public function edit($id = null) {
244
		throw new NotFoundException(__('Access Denied'));
245
		if (!$this->Order->exists($id)) {
246
			throw new NotFoundException(__('Invalid order'));
247
		}
248
		if ($this->request->is(array('post', 'put'))) {
249
			if ($this->Order->save($this->request->data)) {
250
				$this->Session->setFlash(__('The order has been saved.'));
251
				return $this->redirect(array('action' => 'index'));
252
			} else {
253
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
254
			}
255
		} else {
256
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
257
			$this->request->data = $this->Order->find('first', $options);
258
		}
259
		$users = $this->Order->User->find('list');
260
		$stores = $this->Order->Store->find('list');
261
		$storeOrders = $this->Order->StoreOrder->find('list');
262
		$this->set(compact('users', 'stores', 'storeOrders'));
263
	}
264
 
265
/**
266
 * delete method
267
 *
268
 * @throws NotFoundException
269
 * @param string $id
270
 * @return void
271
 */
272
	public function delete($id = null) {
273
		throw new NotFoundException(__('Access Denied'));
274
		$this->Order->id = $id;
275
		if (!$this->Order->exists()) {
276
			throw new NotFoundException(__('Invalid order'));
277
		}
278
		$this->request->onlyAllow('post', 'delete');
279
		if ($this->Order->delete()) {
280
			$this->Session->setFlash(__('The order has been deleted.'));
281
		} else {
282
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
283
		}
284
		return $this->redirect(array('action' => 'index'));
285
	}
286
 
287
/**
288
 * admin_index method
289
 *
290
 * @return void
291
 */
292
	public function admin_index() {
293
		$this->Order->recursive = 0;
294
		$this->set('orders', $this->Paginator->paginate());
295
	}
296
 
297
/**
298
 * admin_view method
299
 *
300
 * @throws NotFoundException
301
 * @param string $id
302
 * @return void
303
 */
304
	public function admin_view($id = null) {
305
		if (!$this->Order->exists($id)) {
306
			throw new NotFoundException(__('Invalid order'));
307
		}
308
		$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
309
		$this->set('order', $this->Order->find('first', $options));
310
	}
311
 
312
/**
313
 * admin_add method
314
 *
315
 * @return void
316
 */
317
	public function admin_add() {
318
		if ($this->request->is('post')) {
319
			$this->Order->create();
320
			if ($this->Order->save($this->request->data)) {
321
				$this->Session->setFlash(__('The order has been saved.'));
322
				return $this->redirect(array('action' => 'index'));
323
			} else {
324
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
325
			}
326
		}
327
		$users = $this->Order->User->find('list');
328
		$stores = $this->Order->Store->find('list');
329
		$storeOrders = $this->Order->StoreOrder->find('list');
330
		$this->set(compact('users', 'stores', 'storeOrders'));
331
	}
332
 
333
/**
334
 * admin_edit method
335
 *
336
 * @throws NotFoundException
337
 * @param string $id
338
 * @return void
339
 */
340
	public function admin_edit($id = null) {
341
		if (!$this->Order->exists($id)) {
342
			throw new NotFoundException(__('Invalid order'));
343
		}
344
		if ($this->request->is(array('post', 'put'))) {
345
			if ($this->Order->save($this->request->data)) {
346
				$this->Session->setFlash(__('The order has been saved.'));
347
				return $this->redirect(array('action' => 'index'));
348
			} else {
349
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
350
			}
351
		} else {
352
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
353
			$this->request->data = $this->Order->find('first', $options);
354
		}
355
		$users = $this->Order->User->find('list');
356
		$stores = $this->Order->Store->find('list');
357
		$storeOrders = $this->Order->StoreOrder->find('list');
358
		$this->set(compact('users', 'stores', 'storeOrders'));
359
	}
360
 
361
/**
362
 * admin_delete method
363
 *
364
 * @throws NotFoundException
365
 * @param string $id
366
 * @return void
367
 */
368
	public function admin_delete($id = null) {
369
		$this->Order->id = $id;
370
		if (!$this->Order->exists()) {
371
			throw new NotFoundException(__('Invalid order'));
372
		}
373
		$this->request->onlyAllow('post', 'delete');
374
		if ($this->Order->delete()) {
375
			$this->Session->setFlash(__('The order has been deleted.'));
376
		} else {
377
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
378
		}
379
		return $this->redirect(array('action' => 'index'));
380
	}
381
 
382
	public function all() {
383
		$page = $this->request->query('page');
384
		$page = isset($page)?$page:1;
385
		// $userId = $this->request->query('user_id');
386
		// if(isset($userId) && !empty($userId)){
387
		// 	$this->loadModel('User');
388
		// 	$dbuser = $this->User->findById($userId);
389
		// 	$this->Auth->login($dbuser['User']);	
390
		// }
391
		$this->layout = "innerpages";
392
		$url = $this->apihost."orders/?page=$page&window=10";
393
		$response = $this->make_request($url,null);
394
		$totalPages = $response['totalPages'];		
395
		if(!empty($response['data'])){
396
			$this->set('orders',$response['data']);
397
		}
398
		$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');
399
		$storemapping = Configure::read('storemapping');
400
		$activestores = Configure::read('activestores');
401
		$amazonorderurl = Configure::read('amazonorderurl');
402
		$allusers = $this->Order->User->find('all',array('fields'=>array('first_name','id'),'recursive'=>-1));
403
		foreach($allusers AS $user){
404
			$users[$user['User']['id']] = $user['User']['first_name'];
405
		}
406
		$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping','amazonorderurl','users'));
407
	}
408
}