Rev 13993 | Rev 14026 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?phpApp::uses('AppController', 'Controller');/*** Orders Controller** @property Order $Order* @property PaginatorComponent $Paginator*/class OrdersController extends AppController {/*** Components** @var array*/public $components = array('Paginator');public function beforeFilter() {parent::beforeFilter();$this->Auth->allow('add','mine','pendingcashbacks');$this->apihost = Configure::read('pythonapihost');}public function mine() {$page = $this->request->query('page');$page = isset($page)?$page:1;$userId = $this->request->query('user_id');if(isset($userId) && !empty($userId)){$this->loadModel('User');$dbuser = $this->User->findById($userId);$this->Auth->login($dbuser['User']);}$this->layout = "innerpages";$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=$page&window=10";$response = $this->make_request($url,null);$totalPages = $response['totalPages'];if(!empty($response['data'])){$this->set('orders',$response['data']);}$ignoredFields = array('imgUrl','status','productTitle','estimatedDeliveryDate','productCode','merchantSubOrderId','productUrl','closed','tracingkUrl','detailedStatus');$storemapping = Configure::read('storemapping');$activestores = Configure::read('activestores');$this->set(compact('ignoredFields','page','totalPages','userId','activestores','storemapping'));}public function pendingcashbacks() {$userId = $this->request->query('user_id');if(isset($userId) && !empty($userId)){$this->loadModel('User');$dbuser = $this->User->findById($userId);$this->Auth->login($dbuser['User']);}$this->layout = "innerpages";$url = $this->apihost."storeorder/user/".$this->Auth->User('id')."?page=1&window=50";$response = $this->make_request($url,null);// debug($response);$creditedOrders = $pendingOrders = $approvedOrders = array();$creditedAmount = $pendingAmount = $approvedAmount = 0;if(!empty($response['data'])){foreach ($response['data'] as $key => $order) {foreach ($order['subOrders'] as $key => $suborder) {$suborder['storeId'] = $order['storeId'];switch($suborder['cashBackStatus']){case 'Credited to wallet'://Credited$creditedOrders[] = $suborder;break;case 'Approved':$approvedOrders[] = $suborder;$approvedAmount += $suborder['cashBackAmount'];break;case 'Pending':$pendingOrders[] = $suborder;$pendingAmount += $suborder['cashBackAmount'];}}}}//Get pending cashbacks$url = $this->apihost.'pending-refunds/user/'.$userId;$pendingCashbacks = $this->make_request($url,null);//Get credited cashbacks$url = $this->apihost.'refund/user/'.$userId;$creditedCashbacks = $this->make_request($url,null);if(!empty($response)){foreach ($creditedCashbacks['data'] as $key => $value) {$creditedAmount += $value['userAmount'];}}// debug($response);if(!empty($response['data'])){$this->set(compact('pendingOrders','approvedOrders','creditedOrders','pendingCashbacks','creditedCashbacks','pendingAmount','approvedAmount','creditedAmount'));}}/*** index method** @return void*/public function index() {throw new NotFoundException(__('Access Denied'));$this->Order->recursive = 0;$this->set('orders', $this->Paginator->paginate());}/*** view method** @throws NotFoundException* @param string $id* @return void*/public function view($id = null) {throw new NotFoundException(__('Access Denied'));if (!$this->Order->exists($id)) {throw new NotFoundException(__('Invalid order'));}$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));$this->set('order', $this->Order->find('first', $options));}/*** add method** @return void*/public function postOrders($order=null) {// Configure::load('live');$apihost = Configure::read('pythonapihost');$url = $apihost."storeorder";if(!empty($order)) {$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']);$jsonVar = json_encode($params);return $this->make_request($url,$jsonVar);}else{$result = array('success'=>false,'message'=>'Empty order array');return $result;}}public function add() {$this->log(print_r($this->request->data,1),'orders');if ($this->request->is('post')) {$this->Order->create();if(!empty($this->request->data['sub_tag'])){$this->request->data['status'] = 'mapped';}if ($this->Order->save($this->request->data)) {//$this->loadModel('PythonApi');$order = $this->Order->find('first',array('conditions'=>array('id'=>$this->Order->getLastInsertID()),'recursive'=>-1));$response = $this->postOrders($order);$this->log(print_r($response,1),'orders');if(!empty($response) && $response['result']) {if($response['result'] == 'HTML_REQUIRED') {$this->loadModel('Rawhtml');$data = array('order_id' => $order['Order']['id'],'url' => $response['url'], 'status' => 'new');$this->Rawhtml->create();$this->Rawhtml->save($data);$result =array('success'=>true,'message'=>__('HTML_REQUIRED'),'url' => $response['url']);$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];} elseif($response['result'] == 'IGNORED') {$result =array('success'=>true,'message'=>__('IGNORED'));$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];} elseif($response['result'] == 'PARSE_ERROR') {$result =array('success'=>true,'message'=>__('PARSE_ERROR'));$sql = "UPDATE orders SET status = 'deleted' WHERE id = ".$order['Order']['id'];} else {$result =array('success'=>true,'message'=>__('PROCESSED'));$sql = "UPDATE orders SET status = 'processed' WHERE id = ".$order['Order']['id'];}$this->Order->query($sql);}//$result = array('success'=>true,'message'=>__('HTML_REQUIRED'),'url'=>'https://www.amazon.in/gp/css/summary/edit.html?orderID=404-7369214-6566739');/*$options = array('conditions'=>array('status'=>'mapped'),'recursive'=>-1);$order = $this->Order->find('first',$options);if(!empty($orders)) {foreach($orders AS $order) {$response = $this->PythonApi->postOrders($order);if(!empty($response) && $response['result']) {$sql = "UPDATE orders SET status = 'processed' WHERE id = ".$order['Order']['id'];$this->Order->query($sql);}}}*/} else {$result = array('success'=>false,'message'=>__('The order could not be saved. Please, try again.'));}$this->response->type('json');$this->layout = 'ajax';$this->set(array('result' => $result,// 'callback' => $callback,'_serialize' => array('result')));$this->render('/Elements/json');}}/*** edit method** @throws NotFoundException* @param string $id* @return void*/public function edit($id = null) {throw new NotFoundException(__('Access Denied'));if (!$this->Order->exists($id)) {throw new NotFoundException(__('Invalid order'));}if ($this->request->is(array('post', 'put'))) {if ($this->Order->save($this->request->data)) {$this->Session->setFlash(__('The order has been saved.'));return $this->redirect(array('action' => 'index'));} else {$this->Session->setFlash(__('The order could not be saved. Please, try again.'));}} else {$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));$this->request->data = $this->Order->find('first', $options);}$users = $this->Order->User->find('list');$stores = $this->Order->Store->find('list');$storeOrders = $this->Order->StoreOrder->find('list');$this->set(compact('users', 'stores', 'storeOrders'));}/*** delete method** @throws NotFoundException* @param string $id* @return void*/public function delete($id = null) {throw new NotFoundException(__('Access Denied'));$this->Order->id = $id;if (!$this->Order->exists()) {throw new NotFoundException(__('Invalid order'));}$this->request->onlyAllow('post', 'delete');if ($this->Order->delete()) {$this->Session->setFlash(__('The order has been deleted.'));} else {$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));}return $this->redirect(array('action' => 'index'));}/*** admin_index method** @return void*/public function admin_index() {$this->Order->recursive = 0;$this->set('orders', $this->Paginator->paginate());}/*** admin_view method** @throws NotFoundException* @param string $id* @return void*/public function admin_view($id = null) {if (!$this->Order->exists($id)) {throw new NotFoundException(__('Invalid order'));}$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));$this->set('order', $this->Order->find('first', $options));}/*** admin_add method** @return void*/public function admin_add() {if ($this->request->is('post')) {$this->Order->create();if ($this->Order->save($this->request->data)) {$this->Session->setFlash(__('The order has been saved.'));return $this->redirect(array('action' => 'index'));} else {$this->Session->setFlash(__('The order could not be saved. Please, try again.'));}}$users = $this->Order->User->find('list');$stores = $this->Order->Store->find('list');$storeOrders = $this->Order->StoreOrder->find('list');$this->set(compact('users', 'stores', 'storeOrders'));}/*** admin_edit method** @throws NotFoundException* @param string $id* @return void*/public function admin_edit($id = null) {if (!$this->Order->exists($id)) {throw new NotFoundException(__('Invalid order'));}if ($this->request->is(array('post', 'put'))) {if ($this->Order->save($this->request->data)) {$this->Session->setFlash(__('The order has been saved.'));return $this->redirect(array('action' => 'index'));} else {$this->Session->setFlash(__('The order could not be saved. Please, try again.'));}} else {$options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));$this->request->data = $this->Order->find('first', $options);}$users = $this->Order->User->find('list');$stores = $this->Order->Store->find('list');$storeOrders = $this->Order->StoreOrder->find('list');$this->set(compact('users', 'stores', 'storeOrders'));}/*** admin_delete method** @throws NotFoundException* @param string $id* @return void*/public function admin_delete($id = null) {$this->Order->id = $id;if (!$this->Order->exists()) {throw new NotFoundException(__('Invalid order'));}$this->request->onlyAllow('post', 'delete');if ($this->Order->delete()) {$this->Session->setFlash(__('The order has been deleted.'));} else {$this->Session->setFlash(__('The order could not be deleted. Please, try again.'));}return $this->redirect(array('action' => 'index'));}}