Subversion Repositories SmartDukaan

Rev

Rev 14111 | Rev 14315 | 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')) {
156
			$this->Order->create();
13633 anikendra 157
			if(!empty($this->request->data['sub_tag'])){
158
				$this->request->data['status'] = 'mapped';
159
			}
13532 anikendra 160
			if ($this->Order->save($this->request->data)) {
13814 anikendra 161
				//$this->loadModel('PythonApi');
162
				$order = $this->Order->find('first',array('conditions'=>array('id'=>$this->Order->getLastInsertID()),'recursive'=>-1));
163
				$response = $this->postOrders($order);
164
				$this->log(print_r($response,1),'orders');
165
				if(!empty($response) && $response['result']) {
166
					if($response['result'] == 'HTML_REQUIRED') {
167
						$this->loadModel('Rawhtml');
168
						$data = array('order_id' => $order['Order']['id'],'url' => $response['url'], 'status' => 'new');
169
						$this->Rawhtml->create();
170
						$this->Rawhtml->save($data); 
171
						$result =array('success'=>true,'message'=>__('HTML_REQUIRED'),'url' => $response['url']);
172
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
173
					} elseif($response['result'] == 'IGNORED') {
174
						$result =array('success'=>true,'message'=>__('IGNORED'));
175
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
176
					} elseif($response['result'] == 'PARSE_ERROR') {
177
						$result =array('success'=>true,'message'=>__('PARSE_ERROR'));
178
						$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];
179
					} else {
180
						$result =array('success'=>true,'message'=>__('PROCESSED'));
181
						$sql = "UPDATE orders SET status = 'processed' WHERE id = ".$order['Order']['id'];
182
					}
183
					$this->Order->query($sql);
184
				}
185
				//$result = array('success'=>true,'message'=>__('HTML_REQUIRED'),'url'=>'https://www.amazon.in/gp/css/summary/edit.html?orderID=404-7369214-6566739');
13633 anikendra 186
/*
187
				$options = array('conditions'=>array('status'=>'mapped'),'recursive'=>-1);
188
				$order = $this->Order->find('first',$options);
189
				if(!empty($orders)) {
190
					foreach($orders AS $order) {
191
						$response = $this->PythonApi->postOrders($order);
192
						if(!empty($response) && $response['result']) {
193
							$sql = "UPDATE orders SET status = 'processed' WHERE id = ".$order['Order']['id'];
194
							$this->Order->query($sql);
195
						}
196
					}
197
				}
198
*/
13532 anikendra 199
			} else {
13591 anikendra 200
				$result = array('success'=>false,'message'=>__('The order could not be saved. Please, try again.'));
13532 anikendra 201
			}
13591 anikendra 202
			$this->response->type('json');
203
			$this->layout = 'ajax';
204
			$this->set(array(
205
			    'result' => $result,
206
			    // 'callback' => $callback,
207
			    '_serialize' => array('result')
208
			));
209
			$this->render('/Elements/json');		
210
		}			
13532 anikendra 211
	}
212
 
213
/**
214
 * edit method
215
 *
216
 * @throws NotFoundException
217
 * @param string $id
218
 * @return void
219
 */
220
	public function edit($id = null) {
13591 anikendra 221
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 222
		if (!$this->Order->exists($id)) {
223
			throw new NotFoundException(__('Invalid order'));
224
		}
225
		if ($this->request->is(array('post', 'put'))) {
226
			if ($this->Order->save($this->request->data)) {
227
				$this->Session->setFlash(__('The order has been saved.'));
228
				return $this->redirect(array('action' => 'index'));
229
			} else {
230
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
231
			}
232
		} else {
233
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
234
			$this->request->data = $this->Order->find('first', $options);
235
		}
236
		$users = $this->Order->User->find('list');
237
		$stores = $this->Order->Store->find('list');
238
		$storeOrders = $this->Order->StoreOrder->find('list');
239
		$this->set(compact('users', 'stores', 'storeOrders'));
240
	}
241
 
242
/**
243
 * delete method
244
 *
245
 * @throws NotFoundException
246
 * @param string $id
247
 * @return void
248
 */
249
	public function delete($id = null) {
13591 anikendra 250
		throw new NotFoundException(__('Access Denied'));
13532 anikendra 251
		$this->Order->id = $id;
252
		if (!$this->Order->exists()) {
253
			throw new NotFoundException(__('Invalid order'));
254
		}
255
		$this->request->onlyAllow('post', 'delete');
256
		if ($this->Order->delete()) {
257
			$this->Session->setFlash(__('The order has been deleted.'));
258
		} else {
259
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
260
		}
261
		return $this->redirect(array('action' => 'index'));
262
	}
263
 
264
/**
265
 * admin_index method
266
 *
267
 * @return void
268
 */
269
	public function admin_index() {
270
		$this->Order->recursive = 0;
271
		$this->set('orders', $this->Paginator->paginate());
272
	}
273
 
274
/**
275
 * admin_view method
276
 *
277
 * @throws NotFoundException
278
 * @param string $id
279
 * @return void
280
 */
281
	public function admin_view($id = null) {
282
		if (!$this->Order->exists($id)) {
283
			throw new NotFoundException(__('Invalid order'));
284
		}
285
		$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
286
		$this->set('order', $this->Order->find('first', $options));
287
	}
288
 
289
/**
290
 * admin_add method
291
 *
292
 * @return void
293
 */
294
	public function admin_add() {
295
		if ($this->request->is('post')) {
296
			$this->Order->create();
297
			if ($this->Order->save($this->request->data)) {
298
				$this->Session->setFlash(__('The order has been saved.'));
299
				return $this->redirect(array('action' => 'index'));
300
			} else {
301
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
302
			}
303
		}
304
		$users = $this->Order->User->find('list');
305
		$stores = $this->Order->Store->find('list');
306
		$storeOrders = $this->Order->StoreOrder->find('list');
307
		$this->set(compact('users', 'stores', 'storeOrders'));
308
	}
309
 
310
/**
311
 * admin_edit method
312
 *
313
 * @throws NotFoundException
314
 * @param string $id
315
 * @return void
316
 */
317
	public function admin_edit($id = null) {
318
		if (!$this->Order->exists($id)) {
319
			throw new NotFoundException(__('Invalid order'));
320
		}
321
		if ($this->request->is(array('post', 'put'))) {
322
			if ($this->Order->save($this->request->data)) {
323
				$this->Session->setFlash(__('The order has been saved.'));
324
				return $this->redirect(array('action' => 'index'));
325
			} else {
326
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
327
			}
328
		} else {
329
			$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
330
			$this->request->data = $this->Order->find('first', $options);
331
		}
332
		$users = $this->Order->User->find('list');
333
		$stores = $this->Order->Store->find('list');
334
		$storeOrders = $this->Order->StoreOrder->find('list');
335
		$this->set(compact('users', 'stores', 'storeOrders'));
336
	}
337
 
338
/**
339
 * admin_delete method
340
 *
341
 * @throws NotFoundException
342
 * @param string $id
343
 * @return void
344
 */
345
	public function admin_delete($id = null) {
346
		$this->Order->id = $id;
347
		if (!$this->Order->exists()) {
348
			throw new NotFoundException(__('Invalid order'));
349
		}
350
		$this->request->onlyAllow('post', 'delete');
351
		if ($this->Order->delete()) {
352
			$this->Session->setFlash(__('The order has been deleted.'));
353
		} else {
354
			$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
355
		}
356
		return $this->redirect(array('action' => 'index'));
357
	}}