Subversion Repositories SmartDukaan

Rev

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