Subversion Repositories SmartDukaan

Rev

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