Subversion Repositories SmartDukaan

Rev

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