Subversion Repositories SmartDukaan

Rev

Rev 15077 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * Exceptionalnlcs Controller
 *
 * @property Exceptionalnlc $Exceptionalnlc
 * @property PaginatorComponent $Paginator
 */
class MasterDatasController extends AppController {

/**
 * Components
 *
 * @var array
 */
        public $components = array('Paginator');

        public function beforeFilter() {
                parent::beforeFilter();
//              Configure::load('live');
                $this->apihost = Configure::read('pythonapihost');
        }

        public function admin_index() {
                /*$page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }
                $limit = Configure::read('admindashboardlimit');
                $params = array(
                        'fields' => array('_id','brand','source_product_name', 'available_price', 'thumbnail','category','category_id','in_stock','marketPlaceUrl','mrp','rank','source_id','source_product_name','url'),
                        'conditions' => array('source_id' => array('$ne' => 0)),
                        'order' => array('_id' => -1),
                        'limit' => $limit,
                        'page' => $page,
                );
                $masterdata = $this->MasterData->find('all', $params);
                $this->loadModel('Category');
                $categories = $this->Category->find('list');
                $storemapping = Configure::read('storemapping');
                $this->set(compact('masterdata','categories','page','storemapping'));*/
                $page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }
                $limit = Configure::read('admindashboardlimit');
                $offset = ($page - 1)*$limit;
                $search = '';
                $url = $this->apihost."Catalog/searchMaster/?search=$search&limit=$limit&offset=$offset";
                $response = $this->make_request($url,null);
                $storemapping = Configure::read('storemapping');
                $this->set('masterdata', $response);
                $this->set(compact('page','search','storemapping'));
        }

        public function admin_show($id=null){
                $limit = 10;
                $page = 1;
                $params = array(
                        'fields' => array('_id','brand','source_product_name', 'available_price', 'thumbnail','category','category_id','in_stock','marketPlaceUrl','mrp','rank','source_id','source_product_name','url'),
                        'conditions' => array('source_id' => array('$ne' => 0)),
                        'order' => array('_id' => -1),
                        'limit' => $limit,
                        'page' => $page,
                );
                $masterdata = $this->MasterData->find('all', $params);
                $this->set(compact('masterdata'));
        }
        public function admin_edit($id = null) {
                $id = (integer) $id;
                if (!$id && empty($this->data)) {
                        $this->flash(__('Invalid Post', true), array('action' => 'index'));
                }
                if (!empty($this->data)) {
                        if ($this->MasterData->save($this->data)) {
                                $this->flash(__('The data has been saved.', true), array('action' => 'index'));
                        } else {
                        }
                }
                if (empty($this->data)) {
                        $this->data = $this->MasterData->read(null, $id);
                        debug($this->data);
                }
        }

        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->log(print_r($this->request->data,1),'masterdatas');
                        $url = $this->apihost."masterData/addOrUpdateMaster/?addNew=1";
                        $data = $this->request->data['MasterData'];
                        $url = $this->generateMultiUrl($url,$data);
                        $data['identifier'] = (string)$data['identifier'];
                        $data['secondaryIdentifier'] = (string)$data['secondaryIdentifier'];
                        $jsonVar = json_encode($data, JSON_NUMERIC_CHECK );
                        $response = $this->make_request($url,$jsonVar);
                        if (key($response)) {
                                $this->Session->setFlash(current($response));
                                return $this->redirect(array('action' => 'search'));
                        } else {
                                $this->Session->setFlash(current($response));
                        }       
                }
                $sources = Configure::read('storemapping');
                $sources[0] = 'Other';
                // $this->set('sources',$sources);
                // $this->set('source_ids',Configure::read('storemapping'));
                $this->set('categories',Configure::read('Categories'));
                $stock_statuses = array(0=>'OOS',1=>'In Stock');
                $statuses = array(2=>'EOL',1=>'Active',3=>'In Process',4=>'Exclusive');
                $shortage_status = array(0=>'No',1=>'Yes');
                $buyBoxFlag = array(1 => 'Yes', 0 => 'No');
                $this->set(compact('stock_statuses','statuses','sources','shortage_status','buyBoxFlag'));
        }

        public function admin_addtoexisting() {
                if ($this->request->is('post')) {
                        $this->log(print_r($this->request->data,1),'masterdatas');
                        $url = $this->apihost."masterData/addOrUpdateMaster/?addToExisting=1";
                        $data = $this->request->data['MasterData'];
                        $url = $this->generateMultiUrl($url,$data);
                        $data['identifier'] = (string)$data['identifier'];
                        $data['secondaryIdentifier'] = (string)$data['secondaryIdentifier'];
                        $jsonVar = json_encode($data, JSON_NUMERIC_CHECK );
                        $response = $this->make_request($url,$jsonVar);
                        if (key($response)) {
                                $this->Session->setFlash(current($response));
                                return $this->redirect(array('action' => 'search'));
                        } else {
                                $this->Session->setFlash(current($response));
                        }       
                }
                $sources = Configure::read('storemapping');
                $sources[0] = 'Other';
                // $this->set('sources',$sources);
                // $this->set('source_ids',Configure::read('storemapping'));
                $this->set('categories',Configure::read('Categories'));
                $stock_statuses = array(0=>'OOS',1=>'In Stock');
                $statuses = array(2=>'EOL',1=>'Active',3=>'In Process',4=>'Exclusive');
                $shortage_status = array(0=>'No',1=>'Yes');
                $buyBoxFlag = array(1 => 'Yes', 0 => 'No');
                $this->set(compact('stock_statuses','statuses','sources','shortage_status','buyBoxFlag'));
        }

        public function admin_update($id) {
                if ($this->request->is('post')) {
                        $this->log(print_r($this->request->data,1),'masterdatas');
                        $url = $this->apihost."masterData/addOrUpdateMaster/?update=1";                 
                        $data = $this->request->data['MasterData'];
                        if(isset($data['multi']) && !empty($data['multi'])){
                                $url .= '&multi=1';
                                unset($data['multi']);
                        }
                        $url = $this->generateMultiUrl($url,$data);
                        $data['identifier'] = (string)$data['identifier'];
                        $data['secondaryIdentifier'] = (string)$data['secondaryIdentifier'];
                        $jsonVar = json_encode($data, JSON_NUMERIC_CHECK );
                        $response = $this->make_request($url,$jsonVar);
                        if (key($response)) {
                                $this->Session->setFlash(current($response));
                                return $this->redirect(array('action' => 'search'));
                        } else {
                                $this->Session->setFlash(current($response));
                        }       
                }
                $url = $this->apihost."masterData/getSkuById/$id";
                $temp = $this->make_request($url,null);
                $data = json_decode($temp[0],1);
                $sources = Configure::read('storemapping');
                $sources[0] = 'Other';
                // $this->set('sources',$sources);
                // $this->set('source_ids',Configure::read('storemapping'));
                $this->set('categories',Configure::read('Categories'));
                $stock_statuses = array(0=>'OOS',1=>'In Stock');
                $statuses = array(2=>'EOL',1=>'Active',3=>'In Process',4=>'Exclusive');
                $shortage_status = array(0=>'No',1=>'Yes');
                $buyBoxFlag = array(1 => 'Yes', 0 => 'No');
                $this->set(compact('stock_statuses','statuses','sources','shortage_status','data','buyBoxFlag'));
        }

        public function admin_search(){
                $page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }
                $limit = Configure::read('admindashboardlimit');
                $offset = ($page - 1)*$limit;
                $search = urlencode($this->request->query('search'));
                $url = $this->apihost."Catalog/searchMaster/?search=$search&limit=$limit&offset=$offset";
                $response = $this->make_request($url,null);
                $storemapping = Configure::read('storemapping');
                $this->set('masterdata', $response);
                $this->set(compact('page','search','storemapping'));
        }

        public function admin_autoupdates(){            
                $page = $this->request->query('page');
                if(!isset($page)){
                        $page = 1;
                }
                $limit = Configure::read('admindashboardlimit');
                $offset = ($page - 1)*$limit;
                $search = urlencode($this->request->query('search'));
                if(!empty($search)){
                        $type = $this->request->query('type');
                        $url = $this->apihost."Catalog/searchProducts/?class=Notifications&$type=$search";
                }else{
                        $url = $this->apihost."Catalog/notification/?limit=$limit&offset=$offset";
                }               
                $autoupdates = $this->make_request($url,null);
                $this->set(compact('page','autoupdates'));      
        }

        public function admin_scheduleupdate(){                         
                if ($this->request->is('post')) {                       
                        $data = $this->request->data['MasterData'];
                        $this->log(print_r($data,1),'scheduleupdates');
                        $data['startDate'] = mktime($data['startDate']['hour'],$data['startDate']['min'],0,$data['startDate']['month'],$data['startDate']['day'],$data['startDate']['year'])*1000;
                        $data['endDate'] = mktime($data['endDate']['hour'],$data['endDate']['min'],0,$data['endDate']['month'],$data['endDate']['day'],$data['endDate']['year'])*1000;
                        $url = $this->apihost."Catalog/notification";                   
                        $url = $this->generateMultiUrl($url,$data);
                        $jsonVar = json_encode($data, JSON_NUMERIC_CHECK );
                        $response = $this->make_request($url,$jsonVar);
                        if (key($response)) {
                                $this->Session->setFlash(current($response));
                                return $this->redirect(array('action' => 'autoupdates'));
                        } else {
                                $this->Session->setFlash(current($response));
                        }       
                }
        }

        public function admin_editscheduledupdate($id,$skuBundleId,$model,$brand,$startDate,$endDate){                          
                if ($this->request->is('post')) {                       
                        $data = $this->request->data['MasterData'];                     
                        $this->log(print_r($data,1),'scheduleupdates');
                        $data['startDate'] = mktime($data['startDate']['hour'],$data['startDate']['min'],0,$data['startDate']['month'],$data['startDate']['day'],$data['startDate']['year'])*1000;
                        $data['endDate'] = mktime($data['endDate']['hour'],$data['endDate']['min'],0,$data['endDate']['month'],$data['endDate']['day'],$data['endDate']['year'])*1000;
                        // debug($data);die;
                        $url = $this->apihost."Catalog/updateCollection";
                        $data1 = $data2 = $data;
                        $data1['class'] = 'Notifications';
                        unset($data1['endDate']);
                        $data2['class'] = 'Notifications';
                        unset($data2['startDate']);
                        $jsonVar = json_encode($data1, JSON_NUMERIC_CHECK );
                        $response = $this->make_request($url,$jsonVar);
                        $jsonVar = json_encode($data2, JSON_NUMERIC_CHECK );
                        $response = $this->make_request($url,$jsonVar);
                        if (key($response)) {
                                // $this->Session->setFlash(current($response));
                                return $this->redirect(array('action' => 'autoupdates'));
                        } else {
                                $this->Session->setFlash(current($response));
                        }       
                }
                $this->set(compact('id','skuBundleId','brand','model','startDate','endDate'));
        }

        public function admin_deletescheduledupdate($id){
                if ($this->remove($id,'Notifications')) {
                        $this->Session->setFlash(__('The sku scheme deal has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The sku scheme could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'autoupdates'));
        }
}