Subversion Repositories SmartDukaan

Rev

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