Subversion Repositories SmartDukaan

Rev

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