Subversion Repositories SmartDukaan

Rev

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