Subversion Repositories SmartDukaan

Rev

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