Subversion Repositories SmartDukaan

Rev

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