Subversion Repositories SmartDukaan

Rev

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

<?php
App::uses('AppController', 'Controller');
/**
 * Retailers Controller
 *
 * @property Retailer $Retailer
 * @property PaginatorComponent $Paginator
 */
class RetailersController extends AppController {

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

/**
 * index method
 *
 * @return void
 */
        public function index() {
                $this->layout = 'ajax';
                $this->Retailer->recursive = 0;
                $options = array('conditions'=>array('status' =>'new'));        
                $result = $this->Retailer->find('first',$options);
                $this->set(array(
                    'result' => $result,
                    '_serialize' => array('result')
                ));
                $this->render('/Elements/json');
        }
public function notificationactive(){
                $retailerId = $this->request->query('retailerid');
                $options = array('conditions' => array('id'=> $retailerId,'status'=>'new','expiresAt >'=>date('Y-m-d H:i:s',time())));
                $count = $this->NotificationCampaign->find('count',$options);
                
                if(!$count){
                        $result = array('success'=>false);
                }else{
                        $result = array('success'=>true);
                }
                
                $this->response->type('json');
                $this->layout = 'ajax';
                $this->set(array(
                    'result' => $result,
                    '_serialize' => array('result')
                ));
                $this->render('/Elements/json');
        }

        /*public function index() {     
                $this->layout = 'ajax';
                $t = $this->request->query('t');
                $retailer = $this->Mobileappsetting->find('one',$options);
                $options = array('fields'=>array("unix_timestamp(modified) t"),'order'=>array('modified'=>'desc'));
                $lasttimestamp = $this->Mobileappsetting->find('first',$options);               
                $result = array('settings' => $settings,'t'=>$lasttimestamp[0]['t']);
                $this->set(array(
                    'result' => $result,
                    '_serialize' => array('result')
                ));
                $this->render('/Elements/json');
        }*/
/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function view($id = null) {
                if (!$this->Retailer->exists($id)) {
                        throw new NotFoundException(__('Invalid retailer'));
                }
                $options = array('conditions' => array('Retailer.' . $this->Retailer->primaryKey => $id));
                $this->set('retailer', $this->Retailer->find('first', $options));
        }

/**
 * add method
 *
 * @return void
 */
        public function add() {
                if ($this->request->is('post')) {
                        $this->Retailer->create();
                        if ($this->Retailer->save($this->request->data)) {
                                $this->Session->setFlash(__('The retailer has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The retailer could not be saved. Please, try again.'));
                        }
                }
        }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function edit($id = null) {
                if (!$this->Retailer->exists($id)) {
                        throw new NotFoundException(__('Invalid retailer'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->Retailer->save($this->request->data)) {
                                $this->Session->setFlash(__('The retailer has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The retailer could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('Retailer.' . $this->Retailer->primaryKey => $id));
                        $this->request->data = $this->Retailer->find('first', $options);
                }
        }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function delete($id = null) {
                $this->Retailer->id = $id;
                if (!$this->Retailer->exists()) {
                        throw new NotFoundException(__('Invalid retailer'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->Retailer->delete()) {
                        $this->Session->setFlash(__('The retailer has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The retailer could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }

/**
 * admin_index method
 *
 * @return void
 */
        public function admin_index() {
                $this->Retailer->recursive = 0;
                $this->set('retailers', $this->Paginator->paginate());
        }

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_view($id = null) {
                if (!$this->Retailer->exists($id)) {
                        throw new NotFoundException(__('Invalid retailer'));
                }
                $options = array('conditions' => array('Retailer.' . $this->Retailer->primaryKey => $id));
                $this->set('retailer', $this->Retailer->find('first', $options));
        }

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->Retailer->create();
                        if ($this->Retailer->save($this->request->data)) {
                                $this->Session->setFlash(__('The retailer has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The retailer could not be saved. Please, try again.'));
                        }
                }
        }

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_edit($id = null) {
                if (!$this->Retailer->exists($id)) {
                        throw new NotFoundException(__('Invalid retailer'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->Retailer->save($this->request->data)) {
                                $this->Session->setFlash(__('The retailer has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The retailer could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('Retailer.' . $this->Retailer->primaryKey => $id));
                        $this->request->data = $this->Retailer->find('first', $options);
                }
        }

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
        public function admin_delete($id = null) {
                $this->Retailer->id = $id;
                if (!$this->Retailer->exists()) {
                        throw new NotFoundException(__('Invalid retailer'));
                }
                $this->request->onlyAllow('post', 'delete');
                if ($this->Retailer->delete()) {
                        $this->Session->setFlash(__('The retailer has been deleted.'));
                } else {
                        $this->Session->setFlash(__('The retailer could not be deleted. Please, try again.'));
                }
                return $this->redirect(array('action' => 'index'));
        }

        public function admin_pendingretailer(){
//              $options['conditions'] = array('status LIKE '=>"%$search%");
                $opt = array('conditions' => array('status'=> 'pending_verification'));
                $pending = $this->Retailer->find('all',$opt);
                $this->set(compact('pending'));
        }
        
        public function admin_verifiedretailer(){
                $opt['conditions']= array('status' => 'retailer_verified');
                $verified = $this->Retailer->find('all',$opt);
                $this->set(compact('verified'));
        }
        
        public function admin_retailerverify($id,$tin){
                $agentid = Configure::read('agentid');
                if($this->request->is('post')){
                        $data = $this->request->data;
                        $retId = $data['Retailer']['retId'];
                        $retcontact = $data['Retailer']['contact'];
                        $cur_date = date('Y-m-d H:i:s', time());
                        if($data['type'] == "Verify"){
                                $retquery ="update retailers set status = 'retailer_verified' ,modified = '".$cur_date."' , isvalidated = 1  where id = ".$retId."";
                                $this->Retailer->query($retquery);
                        }elseif($data['type'] == "Cancel"){
                                $retquery ="update retailers set status = 'not_verified' ,modified = '".$cur_date."' , comments = '".$data['Retailer']['text']."' where id = ".$retId."";
                                $this->Retailer->query($retquery);
                                
                                $callhistoryquery = "insert into callhistory 
                                                (retailer_id,agent_id,mobile_number,call_type,sms_verified,call_time,duration_sec,
                                                last_fetch_time,call_disposition,disposition_description,disposition_comments,created) 
                                                values 
                                                (".$retId.",".$agentid.",'".$retcontact."','verification',0,'".$cur_date."',0,'".$cur_date."',
                                                'other','verification cancelled','".$data['Retailer']['text']."','".$cur_date."')";
                                $this->Retailer->query($callhistoryquery);
                        }elseif($data['type'] == "Pending"){
                                $retquery ="update retailers set comments = '".$data['Retailer']['text']."' ,modified = '".$cur_date."' where id = ".$retId."";
                                $this->Retailer->query($retquery);
                                $callhistoryquery = "insert into callhistory
                                                (retailer_id,agent_id,mobile_number,call_type,sms_verified,call_time,duration_sec,
                                                last_fetch_time,call_disposition,disposition_description,disposition_comments,created)
                                                values
                                                (".$retId.",".$agentid.",'".$retcontact."','verification',0,'".$cur_date."',0,'".$cur_date."',
                                                'other','verification still in process','".$data['Retailer']['text']."','".$cur_date."')";
                                $this->Retailer->query($callhistoryquery);
                        }
                        $this->redirect("pendingretailer");
                }
                $options['conditions'] = array('id'=> $id);
                $pending = $this->Retailer->find('first',$options);
                $result = $pending['Retailer'];
                $tinresult = "";
                if($tin != null){
                        $url = Configure::read('pythonapihost')."tinsearch?tin=".$tin;
                        $tinres = $this->make_request($url, null);
                        if(!$tinres['isError']){
                                $tinresult = $tinres;
                                $this->set(compact('tinresult'));
                        }
                }
                $this->set(compact('result'));
        }
        
        
        public function sendsms(){
                $this->autoRender = false;
                $this->request->onlyAllow('ajax');
                $agentid = Configure::read('agentid');
                $contact = $this->request->query('contact');
                $retid = $this->request->query('retid');
                $url = Configure::read('pythonapihost')."sendTransSms/code/".$retid."/".$agentid;
//              $url = "http://45.79.106.95:8057/sendTransSms/code/6836700/2";
                if($contact != null || !empty($contact)){
                        $url = $url."?mobile_number=".$contact;
                }
                $response = $this->make_request($url,'{}');
                
                return json_encode($response);
        }
        
        public function admin_editretailer($id){
                if($this->request->is('post') || $this->request->is('put')){
                        $retdet = $this->request->data['Retailer'];
                        $cur_date = date('Y-m-d H:i:s', time());
                        if($retdet['tinNumber'] == '' || empty($retdet['tinNumber']))
                        {
                                $upquery = "update retailers set title = '".$retdet['storeName']."' , address = '".$retdet['completeAddress']."',pin = ".$retdet['pin'].",tinnumber = NULL,city = '".$retdet['city']."',state='".$retdet['state']."' , modified='".$cur_date."' where id =".$retdet['id']."";
                        }
                        else{
                                $upquery = "update retailers set title = '".$retdet['storeName']."' , address = '".$retdet['completeAddress']."',pin = ".$retdet['pin'].",tinnumber = ".$retdet['tinNumber'].",city = '".$retdet['city']."',state='".$retdet['state']."' , modified='".$cur_date."' where id =".$retdet['id']."";
                        }
                        
                        
                        
                        $this->Retailer->query($upquery);
                        $this->redirect("retailerverify/".$retdet['id']."/".$retdet['tinNumber']);
                }
                if($id !=null){
                        $opt['conditions'] = array('id' => $id);
                        $retailer = $this->Retailer->find('first',$opt);
                        $this->set(compact('retailer'));
                        
                }
        }
        
        public function admin_filterretailer(){
                
                if($this->request->is('post')){
                        $filter = $this->request->data['Retailer']['searchReatailerBy'];
//                      debug($retdet);
                        if($filter == '0'){
                                $cnaquery ="select r.* from retailers r join profitmandi_sms p on r.id=p.identifier join retailerlinks rl on r.id= rl.retailer_id and rl.user_id is not null ";
                        }else if($filter == '1') {
                                $cnaquery ="select r.* from retailers r join profitmandi_sms p on r.id=p.identifier join retailerlinks rl on r.id= rl.retailer_id and rl.user_id is null ";
                        }else if($filter == '2') {
                                $cnaquery ="select r.* from retailers r where r.status = 'not_verified'";
                        }

                        $result = $this->Retailer->query($cnaquery);

                        $this->set(compact('result'));
                }               
                        
                
                
        }
}