Subversion Repositories SmartDukaan

Rev

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