<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Report extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('cookie');
    }

    public function index() {
        $data = array();
        $admin_info = $this->session->userdata('adminInfo');
        if ($admin_info) {
            $data['statistics'] = $this->getStatistics();           
            $data['content'] = $this->getPaidlogs();
            $this->load->view('report/index', $data);
        } else {
            $this->load->view('login');
        }
    }
    public function getStatistics()
    {
        $data=array();
        
        $last_month['start']= strtotime(date("Y-m-d", strtotime("first day of previous month"))." 00:00:00");
        $last_month['end']= strtotime(date("Y-m-d", strtotime("last day of previous month"))." 23:59:59");
        
        $last_week['start']= strtotime(date("Y-m-d", strtotime("first day of previous week"))." 00:00:00");
        $last_week['end']= strtotime(date("Y-m-d", strtotime("last day of previous week"))." 23:59:59");
        
        $last_week['end']=strtotime('last sunday')+(24*60*60-1);
        $last_week['start']=($last_week['end']-7*24*60*60+1);
        
        $last_year['start']=  strtotime((date("Y")-1)."-01-01 00:00:00");
        $last_year['end']=  strtotime((date("Y")-1)."-12-31 23:59:59");
        
        
        
        $last_quarter=$this->getLastquarter();
        $last_quarter['start']=  $last_quarter['start'];
        $last_quarter['end']=  $last_quarter['end'];
        
        
        $this->load->model('paidlog_model');
        $paidlogs = $this->paidlog_model->getAllPaidlogs($last_year['start']);
        
        $revenue['last_week']=0;
        $revenue['last_month']=0;
        $revenue['last_quarter']=0;
        $revenue['last_year']=0;
        foreach ($paidlogs as $paidlog)
        {
            
            if (($paidlog['time'] < $last_week['end'])&&($paidlog['time'] > $last_week['start']))
            {
                $revenue['last_week']+=$paidlog['amount'];
            }
            if (($paidlog['time'] < $last_month['end'])&&($paidlog['time'] > $last_month['start']))
            {
                $revenue['last_month']+=$paidlog['amount'];
            }
            if (($paidlog['time'] < $last_quarter['end'])&&($paidlog['time'] > $last_quarter['start']))
            {
                $revenue['last_quarter']+=$paidlog['amount'];
            }
            if (($paidlog['time'] < $last_year['end'])&&($paidlog['time'] > $last_year['start']))
            {
                $revenue['last_year']+=$paidlog['amount'];
            }
        }
        $data['revenue']=$revenue;
        return $this->load->view('report/statistics', $data, true);
    }
    public function getLastquarter()
    {
        $current_month = date('m');
          $current_year = date('Y');

          if($current_month>=1 && $current_month<=3)
          {
            $start_date = strtotime('1-October-'.($current_year-1));  // timestamp or 1-October Last Year 12:00:00 AM
            $end_date = strtotime('1-Janauary-'.$current_year);  // // timestamp or 1-January  12:00:00 AM means end of 31 December Last year
          } 
          else if($current_month>=4 && $current_month<=6)
          {
            $start_date = strtotime('1-January-'.$current_year);  // timestamp or 1-Janauray 12:00:00 AM
            $end_date = strtotime('1-April-'.$current_year);  // timestamp or 1-April 12:00:00 AM means end of 31 March
          }
          else  if($current_month>=7 && $current_month<=9)
          {
            $start_date = strtotime('1-April-'.$current_year);  // timestamp or 1-April 12:00:00 AM
            $end_date = strtotime('1-July-'.$current_year);  // timestamp or 1-July 12:00:00 AM means end of 30 June
          }
          else  if($current_month>=10 && $current_month<=12)
          {
            $start_date = strtotime('1-July-'.$current_year);  // timestamp or 1-July 12:00:00 AM
            $end_date = strtotime('1-October-'.$current_year);  // timestamp or 1-October 12:00:00 AM means end of 30 September
          }
          return array("start"=>$start_date,"end"=>$end_date);
    }
    public function getPaidlogs($filters = array()) {
        $this->load->helper('pagging');
        $this->load->model('paidlog_model');
        $data['current_page'] = $this->uri->segment(4, 1);
        $data['itemsoptions'] = array(10, 25, 50, 100);
        $data['perpage'] = 10;
        $data['keyword'] = "";
        $data['sorting_order']="sorting_desc";
        $data['sorting_field']="paid_id";
        
        if ($this->input->post('sorting_order')) {
            if ($this->input->post('sorting_order') != "sorting")
            {
               $data['sorting_order']=$this->input->post('sorting_order');
               $data['sorting_field']=$this->input->post('sorting_field'); 
            }
        }
        if ($this->input->post('items')) {
            $data['perpage'] = $this->input->post('items');
        }
        if ($this->input->post('keyword')) {
            $data['search_field']="";
            $data['keyword'] = $this->input->post('keyword');
            
            if (preg_match("/[0-9]/", $data['keyword']))
            {
               $data['search_field']="cellphone"; 
            }
            
        }
        
        $data['start'] = ($data['current_page'] - 1) * $data['perpage'];    
        $data['total'] = $this->paidlog_model->countPaidlog($data);  
        $data['paidlogs'] = $this->paidlog_model->getPaidlogs($data);

        $data['paging_url'] = base_url() . "/admin/doanh_thu_tong_hop/trang/";
        $data['num_links'] = 2;
        $data['paging'] = pagging($data);

        if ($this->input->is_ajax_request()) {
            return $this->load->view('report/listview', $data);
        }
        return $this->load->view('report/listview', $data, true);
    }

    public function addPaidlog() {
        $result['success'] = 0;
        $result = array();
        $input = $this->input->post();
        $this->load->model('paidlog_model');
        if (strlen($input['sms_content'])==0)
        {
            $result['errors'][]="Nội dung tin nhắn không được để trống";
        }
        if (strlen($input['sms_reply'])==0)
        {
            $result['errors'][]="Tin nhắn trả về không được để trống";
        }
        else
        {
            /*
            if ($this->paidlog_model->isExist(array('field'=>'login_name','value'=>$input['login_name'],'id'=>$input['id'])))
            {
                $result['errors'][]="Tên đăng nhập đã được sử dụng";
            }
             * 
             */
        }
        if (strlen($input['service_id'])==0)
        {
            $result['errors'][]="Mã dịch vụ không được để trống";
        }
        else
        {
            /*
            if ($this->paidlog_model->isExist(array('field'=>'cellphone','value'=>$input['cellphone'],'id'=>$input['id'])))
            {
                $result['errors'][]="Số điện thoại đã được sử dụng";
            }
             * 
             */
        }
        if (!isset($result['errors']))
        {
            $this->load->model('paidlog_model');
            $this->paidlog_model->insert($input);
            $result['success'] = 1;
        }

        
        echo json_encode($result);
    }
    public function viewSms()
    {
        $id=$this->uri->segment(4);
        $this->load->model('paidlog_model');
        $data=$this->paidlog_model->getPaidlog($id);
        $this->load->view('report/viewSms',$data);
    }
    public function editSms()
    {
        $id=$this->uri->segment(4);
        $this->load->model('paidlog_model');
        $data=$this->paidlog_model->getPaidlog($id);
        $this->load->view('report/editSms',$data);
    }
    public function dodeleteSms()
    {
        $input=$this->input->post();
        $id=$input['sms_id'];
        $this->load->model('paidlog_model');
        $data=$this->paidlog_model->delete($id);
        $result['success']=1;
        echo json_encode($result);
    }
    public function deleteSms()
    {
        $id=$this->uri->segment(4);
        $this->load->model('paidlog_model');
        $data=$this->paidlog_model->getPaidlog($id);
        $this->load->view('report/deleteSms',$data);
    }
    public function updateSms()
    {
       
        $input=  $this->input->post();
        $this->load->model('paidlog_model');
        if (strlen($input['sms_content'])==0)
        {
            $result['errors'][]="Nội dung tin nhắn không được để trống";
        }
        if (strlen($input['sms_reply'])==0)
        {
            $result['errors'][]="Tin nhắn trả về không được để trống";
        }
        else
        {
            /*
            if ($this->paidlog_model->isExist(array('field'=>'login_name','value'=>$input['login_name'],'id'=>$input['id'])))
            {
                $result['errors'][]="Tên đăng nhập đã được sử dụng";
            }
             * 
             */
        }
        if (strlen($input['service_id'])==0)
        {
            $result['errors'][]="Mã dịch vụ không được để trống";
        }
        else
        {
            /*
            if ($this->paidlog_model->isExist(array('field'=>'cellphone','value'=>$input['cellphone'],'id'=>$input['id'])))
            {
                $result['errors'][]="Số điện thoại đã được sử dụng";
            }
             * 
             */
        }
        
        if (!isset($result['errors']))
        { 
            $this->load->model('paidlog_model');
            $this->paidlog_model->update($input['sms_id'],$input);
            $result['success'] = 1;
        }
        echo json_encode($result);
    }
    public function test() {
        
      
        for ($i = 1; $i <= 5000; $i++) {
            $input['us_id']=rand(1,100);
            $input['paid_type']=rand(1,2);
            $input['amount']=rand(3,10)*1000;
            $time=(rand((time()-(12*30*24*60*60)),time()));
            $input['paid_time']= date("Y-m-d H:i:s",$time);
            $this->load->model('paidlog_model');
            $this->paidlog_model->insert($input);
        }
        
       die(); 
      
        for ($i = 1; $i <= 100; $i++) {
            $input['us_id']=$i;
            $input['cellphone']="01234567".(100+$i);
            $input['collaborator']=rand(1,100);
            $input['acc_balanced']=rand(100000,1000000);
            $input['expire_date']=date("Y-m-d H:i:s",time());
            $input['created_time']=date("Y-m-d H:i:s",time());
            $input['updated_time']=date("Y-m-d H:i:s",time());
            $this->load->model('user_model');
            $this->user_model->insert($input);
        }
    }

}
