Subversion Repositories SmartDukaan

Rev

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

<?php
App::uses('AppController', 'Controller');
/**
 * AppTransactions Controller
 *
 * @property AppTransaction $AppTransaction
 * @property PaginatorComponent $Paginator
 */
class AppTransactionsController extends AppController {

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

        public function beforeFilter()
    {
       parent::beforeFilter();
       $this->Paginator->settings=array(
              'limit'=>100
       );
    } 
/**
 * index method
 *
 * @return void
 */
        public function index() {
                $this->AppTransaction->recursive = 0;
                $this->set('appTransactions', $this->Paginator->paginate());
        }

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

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

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

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

/**
 * admin_index method
 *
 * @return void
 */
        public function admin_index() {
                $this->AppTransaction->recursive = 0;
                $this->set('appTransactions', $this->Paginator->paginate(array('AppTransaction.' . $this->AppTransaction.'payout_description'=>'Approved')));
        }

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

/**
 * admin_add method
 *
 * @return void
 */
        public function admin_add() {
                if ($this->request->is('post')) {
                        $this->AppTransaction->create();
                        if ($this->AppTransaction->save($this->request->data)) {
                                $this->Session->setFlash(__('The app transaction has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The app transaction 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->AppTransaction->exists($id)) {
                        throw new NotFoundException(__('Invalid app transaction'));
                }
                if ($this->request->is(array('post', 'put'))) {
                        if ($this->AppTransaction->save($this->request->data)) {
                                $this->Session->setFlash(__('The app transaction has been saved.'));
                                return $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The app transaction could not be saved. Please, try again.'));
                        }
                } else {
                        $options = array('conditions' => array('AppTransaction.' . $this->AppTransaction->primaryKey => $id));
                        $this->request->data = $this->AppTransaction->find('first', $options);
                }
        }

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

        public function admin_appwise(){
                $this->AppTransaction->recursive = -1;
                $type = $this->request->query('date');
                $date_from = $this->request->query('date_from');
                $date_to = $this->request->query('date_to');
                if(!isset($type) && !isset($date_from) && !isset($date_to)){
                        $date = date('Y-m-d',time());           
                        $this->Paginator->settings = array(
                'fields' => array('AppTransaction.app_id','retailer_id','app_name', 'COUNT(*) AS count'),
                'conditions' => array('Date(AppTransaction.transaction_time)'=>$date),
                'order' => 'count desc' ,
                'group' => 'app_id'
                        );
                        $approvedCounts =  $this->Paginator->paginate();
                        $sql="SELECT app_id,count(1) from app_transactions where date(transaction_time)='$date' and payout_description='Approved' group by app_id";
                        $approved = $this->AppTransaction->query($sql);
                        foreach ($approved as $key => $value1) {
                                $app_id=$value1['app_transactions']['app_id'];
                                $value22=$value1['0']['count(1)'];
                                foreach ($approvedCounts as $key2 => $value) {
                                        if($app_id==$value['AppTransaction']['app_id']){
                                                $approvedCounts[$key2]['AppTransaction']['approved'] = $value22;
                                                $approvedCounts[$key2]['AppTransaction']['conversion_percentage']=ceil(($value22/$value[0]['count'])*100);
                                        }
                                }
                        }
                }
                else if($type=='yesterday'){
                        $date = date('Y-m-d',time()-86400);             
                        $this->Paginator->settings = array(
                'fields' => array('AppTransaction.app_id','retailer_id','app_name', 'COUNT(*) AS count'),
                'conditions' => array('Date(AppTransaction.transaction_time)'=>$date),
                'order' => 'count desc' ,
                'group' => 'app_id'
                        );
                        $approvedCounts =  $this->Paginator->paginate();
                        $sql="SELECT app_id,count(1) from app_transactions where date(transaction_time)='$date' and payout_description='Approved' group by app_id";
                        $approved = $this->AppTransaction->query($sql);
                        foreach ($approved as $key => $value1) {
                                $app_id=$value1['app_transactions']['app_id'];
                                $value22=$value1['0']['count(1)'];
                                foreach ($approvedCounts as $key2 => $value) {
                                        if($app_id==$value['AppTransaction']['app_id']){
                                                $approvedCounts[$key2]['AppTransaction']['approved'] = $value22;
                                                $approvedCounts[$key2]['AppTransaction']['conversion_percentage']=ceil(($value22/$value[0]['count'])*100);
                                        }
                                }
                        }       
                }else if(isset($date_from) && isset($date_to)){
                        $this->Paginator->settings = array(
                        'fields' => array('AppTransaction.app_id','retailer_id','app_name', 'COUNT(*) AS count'),
                'conditions' => array('Date(AppTransaction.transaction_time) BETWEEN ? AND ?' =>array($date_from, $date_to)),
                'order' => 'count desc' ,
                'group' => 'app_id'
                        );
                        $approvedCounts =  $this->Paginator->paginate();
                        $sql="SELECT app_id,count(1) from app_transactions where date(transaction_time) BETWEEN '$date_from' and '$date_to' and payout_description='Approved' group by app_id";
                        $approved = $this->AppTransaction->query($sql);
                        foreach ($approved as $key => $value1) {
                                $app_id=$value1['app_transactions']['app_id'];
                                $value22=$value1['0']['count(1)'];
                                foreach ($approvedCounts as $key2 => $value) {
                                        if($app_id==$value['AppTransaction']['app_id']){
                                                $approvedCounts[$key2]['AppTransaction']['approved'] = $value22;
                                                $approvedCounts[$key2]['AppTransaction']['conversion_percentage']=ceil(($value22/$value[0]['count'])*100);
                                        }
                                }
                        }               
                }else{
                        $date = date('Y-m-d',time());           
                        $this->Paginator->settings = array(
                'fields' => array('AppTransaction.app_id','retailer_id','app_name', 'COUNT(*) AS count'),
                'conditions' => array('Date(AppTransaction.transaction_time)'=>$date),
                'order' => 'count desc' ,
                'group' => 'app_id'
                        );
                        $approvedCounts =  $this->Paginator->paginate();
                        $sql="SELECT app_id,count(1) from app_transactions where date(transaction_time)='$date' and payout_description='Approved' group by app_id";
                        $approved = $this->AppTransaction->query($sql);
                        foreach ($approved as $key => $value1) {
                                $app_id=$value1['app_transactions']['app_id'];
                                $value22=$value1['0']['count(1)'];
                                foreach ($approvedCounts as $key2 => $value) {
                                        if($app_id==$value['AppTransaction']['app_id']){
                                                $approvedCounts[$key2]['AppTransaction']['approved'] = $value22;
                                                $approvedCounts[$key2]['AppTransaction']['conversion_percentage']=ceil(($value22/$value[0]['count'])*100);
                                        }
                                }
                        }
                }
        $totalClicks=0; 
        $totalApproved=0;
        foreach ($approvedCounts as $key => $value) {
                $totalClicks+=$value[0]['count'];
                if(!(isset($value['AppTransaction']['approved']))){
                        $totalApproved+=0;
                }else{
                        $totalApproved+=$value['AppTransaction']['approved'];
                }
        }
        $this->set('appTransactions', $approvedCounts);
        $this->set('totalClicks', $totalClicks);
        $this->set('totalApproved', $totalApproved);
}

        public function admin_retailer(){
                $this->AppTransaction->recursive = -1;
                $type = $this->request->query('date');
                $date_from = $this->request->query('date_from');
                $date_to = $this->request->query('date_to');
                if(!isset($type) && !isset($date_from) && !isset($date_to)){
                        $date = date('Y-m-d',time());           
                        $this->Paginator->settings = array(
                'fields' => array('AppTransaction.app_id','retailer_id','app_name', 'COUNT(*) AS count'),
                'conditions' => array('Date(AppTransaction.transaction_time)'=>$date),
                'order' => 'count desc' ,
                'group' => 'retailer_id'
                        );
                        $approvedCounts =  $this->Paginator->paginate();
                        $sql="SELECT retailer_id,count(1) from app_transactions where date(transaction_time)='$date' and payout_description='Approved' group by retailer_id";
                        $approved = $this->AppTransaction->query($sql);
                        foreach ($approved as $key => $value1) {
                                $app_id=$value1['app_transactions']['retailer_id'];
                                $value22=$value1['0']['count(1)'];
                                foreach ($approvedCounts as $key2 => $value) {
                                        if($app_id==$value['AppTransaction']['retailer_id']){
                                                $approvedCounts[$key2]['AppTransaction']['approved'] = $value22;
                                                $approvedCounts[$key2]['AppTransaction']['conversion_percentage']=ceil(($value22/$value[0]['count'])*100);
                                        }
                                }
                        }
                }
                else if($type=='yesterday'){
                        $date = date('Y-m-d',time()-86400);             
                        $this->Paginator->settings = array(
                'fields' => array('AppTransaction.app_id','retailer_id','app_name', 'COUNT(*) AS count'),
                'conditions' => array('Date(AppTransaction.transaction_time)'=>$date),
                'order' => 'count desc' ,
                'group' => 'retailer_id'
                        );
                        $approvedCounts =  $this->Paginator->paginate();
                        $sql="SELECT retailer_id,count(1) from app_transactions where date(transaction_time)='$date' and payout_description='Approved' group by retailer_id";
                        $approved = $this->AppTransaction->query($sql);
                        foreach ($approved as $key => $value1) {
                                $app_id=$value1['app_transactions']['retailer_id'];
                                $value22=$value1['0']['count(1)'];
                                foreach ($approvedCounts as $key2 => $value) {
                                        if($app_id==$value['AppTransaction']['retailer_id']){
                                                $approvedCounts[$key2]['AppTransaction']['approved'] = $value22;
                                                $approvedCounts[$key2]['AppTransaction']['conversion_percentage']=ceil(($value22/$value[0]['count'])*100);
                                        }
                                }
                        }       
                }else if(isset($date_from) && isset($date_to)){
                        $this->Paginator->settings = array(
                        'fields' => array('AppTransaction.app_id','retailer_id','app_name', 'COUNT(*) AS count'),
                'conditions' => array('Date(AppTransaction.transaction_time) BETWEEN ? AND ?' =>array($date_from, $date_to)),
                'order' => 'count desc' ,
                'group' => 'retailer_id'
                        );
                        $approvedCounts =  $this->Paginator->paginate();
                        $sql="SELECT retailer_id,count(1) from app_transactions where date(transaction_time) BETWEEN '$date_from' and '$date_to' and payout_description='Approved' group by retailer_id";
                        $approved = $this->AppTransaction->query($sql);
                        foreach ($approved as $key => $value1) {
                                $app_id=$value1['app_transactions']['retailer_id'];
                                $value22=$value1['0']['count(1)'];
                                foreach ($approvedCounts as $key2 => $value) {
                                        if($app_id==$value['AppTransaction']['retailer_id']){
                                                $approvedCounts[$key2]['AppTransaction']['approved'] = $value22;
                                                $approvedCounts[$key2]['AppTransaction']['conversion_percentage']=ceil(($value22/$value[0]['count'])*100);
                                        }
                                }
                        }               
                }else{
                        $date = date('Y-m-d',time());           
                        $this->Paginator->settings = array(
                'fields' => array('AppTransaction.app_id','retailer_id','app_name', 'COUNT(*) AS count'),
                'conditions' => array('Date(AppTransaction.transaction_time)'=>$date),
                'order' => 'count desc' ,
                'group' => 'retailer_id'
                        );
                        $approvedCounts =  $this->Paginator->paginate();
                        $sql="SELECT retailer_id,count(1) from app_transactions where date(transaction_time)='$date' and payout_description='Approved' group by retailer_id";
                        $approved = $this->AppTransaction->query($sql);
                        foreach ($approved as $key => $value1) {
                                $app_id=$value1['app_transactions']['retailer_id'];
                                $value22=$value1['0']['count(1)'];
                                foreach ($approvedCounts as $key2 => $value) {
                                        if($app_id==$value['AppTransaction']['retailer_id']){
                                                $approvedCounts[$key2]['AppTransaction']['approved'] = $value22;
                                                $approvedCounts[$key2]['AppTransaction']['conversion_percentage']=ceil(($value22/$value[0]['count'])*100);
                                        }
                                }
                        }
                }
        $totalClicks=0; 
        $totalApproved=0;
        foreach ($approvedCounts as $key => $value) {
                $totalClicks+=$value[0]['count'];
                if(!(isset($value['AppTransaction']['approved']))){
                        $totalApproved+=0;
                }else{
                        $totalApproved+=$value['AppTransaction']['approved'];
                }
        }
        $this->set('appTransactions', $approvedCounts);
        $this->set('totalClicks', $totalClicks);
        $this->set('totalApproved', $totalApproved);
        }
}