Demo entry 3540670

Heo Bự

   

Submitted by anonymous on Jan 11, 2016 at 11:58
Language: PHP. Code size: 12.6 kB.

<?php
//set_time_limit(0);
//ini_set('memory_limit', '-1');
/**
 * Author: Longnd@appota.com
 * Time: 24/12/2015
 */
class Report extends CI_Controller {
    var $info = array();
    function __construct() {
        parent::__construct();
        $this->load->helper('cookie');
        $this->load->library('session');
        
        $this->load->library('session');
         
        //load model
        $this->load->model('report_m', '', TRUE);
        //info
        $this->info['user_id'] = $this->session->userdata('user_id');
        $this->info['username'] = $this->session->userdata('username');
        $this->info['email'] = $this->session->userdata('email');
        $check_login = $this->session->userdata('logined');
        
        //check login
        if(!isset($check_login) || $check_login != 1){
            redirect(base_url());
        }
        
        //notification
//        $this->info['notice_all'] = $this->report_m->getNoticeAll();
//        $this->info['notice_private'] = $this->report_m->getNoticePrivate($this->info['user_id']);
//        $this->info['total_notice_private'] = $this->report_m->countNoticePrivate($this->info['user_id']);
        
        $set_lang = get_cookie('lang');
        if ($set_lang) {
            $this->info['lang'] = $set_lang;
            $this->set_lang = $set_lang;
        } else {
            $this->info['lang'] = 'vi';
            $this->set_lang = 'vi';
        }
    }
    
    function index(){
        $this->overview();
    }
    
    function overview($product = "all", $startdate = "0", $enddate = "0"){
        $dev_id = $this->session->userdata('user_id');
        if(!$dev_id){
            redirect(base_url());
        }
        //load driver memcache
        $this->load->driver('cache', array('adapter' => 'memcached'));

        //xử lý chọn product
        if($product != "all"){
            $data['product'] = $this->report_m->getProductInfo($product,$dev_id);
        }else{
            $data['product'] = array();
        }
        $start_date = $startdate;
        $end_date = $enddate;
        //khai báo giá trị mặc dịnh cho tỷ lệ phần trăm
        $percent_transaction_success = 0;
        $percent_transaction_error = 0;
        $percent_revenue = 0;
        $percent_transaction_sms = 0;
        $percent_transaction_card = 0;
        $percent_transaction_bank = 0;
        //xử lý date time
        if($startdate != "0" && $enddate != "0"){
            $time_start = strtotime(str_replace('_', '/', $start_date) . '00:00:00');
            $time_end = strtotime(str_replace('_', '/', $end_date) . '23:59:59');
            $data['filter_date'] = date("m/d/Y", $time_start)." - ". date("m/d/Y", $time_end);
            $data['chart_start_date'] = date("m/d/Y", $time_start);
            $data['chart_end_date'] = date("m/d/Y", $time_end);
        }else{
            //set time mặc định là ngày hôm nay
//            $time_start = strtotime(date("m/d/Y", time()).'00:00:00');
//            $time_end = strtotime(date("m/d/Y H:i:s", time()));

            //Mặc định từ ngày đầu tháng tới ngày hiện tại
            $time_start = strtotime('00:00:00 ' . "01-" . date('m-Y', time()));
            $time_end = time();
            
            $data['filter_date'] = date("m/d/Y", $time_start)." - ". date("m/d/Y", $time_end);
            $data['chart_start_date'] = date("m/d/Y", $time_start);
            $data['chart_end_date'] = date("m/d/Y", $time_end);
        }
        $filter = $filter_compare = $filter_chart = array(
            'dev_id' => $dev_id,
            'product_id' => $product
        );

        $filter['startdate'] = $time_start;
        $filter['enddate'] = $time_end;

        //tính ra số ngày người dùng đã chọn
        $number_start = $time_start;
        $number_end = $time_end;

        $number_day = ceil(($number_end - $number_start + 1) /60/60/24);
        $data['number_day'] = $number_day;

        $day_end = strtotime(str_replace('_', '/', $startdate) . '23:59:59');

        //trừ đi 1 ngày. vd : select 10->15 thì ngày so sánh là : 4->9
        $number_day_end = $day_end - 86400;
        $number_day_start = strtotime('-' . $number_day . 'day', $time_start);
        $filter_compare['startdate'] = $number_day_start;
        $filter_compare['enddate'] = $number_day_end;

        //TÍNH TỔNG SỐ LƯỢT GIAO DỊCH SUCCESS VÀ TỶ LỆ %
        $total_transaction_success_today = $this->report_m->countTotalTransactionStatus($filter, "success");
        $total_transaction_success_compare = $this->report_m->countTotalTransactionStatus($filter_compare, "success");
        if($total_transaction_success_compare > 0){
            $percent_transaction_success = (($total_transaction_success_today - $total_transaction_success_compare) / $total_transaction_success_compare) * 100;
        }else{
            $percent_transaction_success = $total_transaction_success_today;
        }
        //TÍNH TỔNG SỐ LƯỢT GIAO DỊCH ERROR VÀ TỶ LỆ %
        $total_transaction_error_today = $this->report_m->countTotalTransactionStatus($filter, "error");
        $total_transaction_error_compare = $this->report_m->countTotalTransactionStatus($filter_compare, "error");
        if($total_transaction_error_compare > 0){
            $percent_transaction_error = (($total_transaction_error_today - $total_transaction_error_compare) / $total_transaction_error_compare) * 100;
        }else{
            $percent_transaction_error = $total_transaction_error_today;
        }
        //TÍNH TỔNG TIỀN THU DC VÀ TỶ LỆ %
        $total_revenue_today = $this->report_m->countTotalRevenue($filter);
        $total_revenue_compare = $this->report_m->countTotalRevenue($filter_compare);
        if($total_revenue_compare > 0){
            $percent_revenue = (($total_revenue_today - $total_revenue_compare) / $total_revenue_compare) * 100;
        }else{
            $percent_revenue = $total_revenue_today;
        }
        //TÍNH TỔNG SỐ LƯỢT GIAO DỊCH SMS VÀ TỶ LỆ %
        $total_transaction_sms_today = $this->report_m->countTotalTransactionType($filter, "success", "sms_transaction");
        $total_transaction_sms_compare = $this->report_m->countTotalTransactionType($filter_compare, "success", "sms_transaction");
        if($total_transaction_sms_compare > 0){
            $percent_transaction_sms = (($total_transaction_sms_today - $total_transaction_sms_compare) / $total_transaction_sms_compare) * 100;
        }else{
            $percent_transaction_sms = $total_transaction_sms_today;
        }
        //TÍNH TỔNG SỐ LƯỢT GIAO DỊCH CARD VÀ TỶ LỆ %
        $total_transaction_card_today = $this->report_m->countTotalTransactionType($filter, "success", "card_transaction");
        $total_transaction_card_compare = $this->report_m->countTotalTransactionType($filter_compare, "success", "card_transaction");
        if($total_transaction_card_compare > 0){
            $percent_transaction_card = (($total_transaction_card_today - $total_transaction_card_compare) / $total_transaction_card_compare) * 100;
        }else{
            $percent_transaction_card = $total_transaction_card_today;
        }
        //TÍNH TỔNG SỐ LƯỢT GIAO DỊCH BANK VÀ TỶ LỆ %
        $total_transaction_bank_today = $this->report_m->countTotalTransactionType($filter, "success", "bank_transaction");
        $total_transaction_bank_compare = $this->report_m->countTotalTransactionType($filter_compare, "success", "bank_transaction");
        if($total_transaction_bank_compare > 0){
            $percent_transaction_bank = (($total_transaction_bank_today - $total_transaction_bank_compare) / $total_transaction_bank_compare) * 100;
        }else{
            $percent_transaction_bank = $total_transaction_bank_today;
        }
        //TÍNH TỔNG DOANH THU CHO BIỂU ĐỒ - tạm thời dùng chung ngày tháng bên trên
//        $chart_startdate = strtotime('00:00:00 ' . "01-" . date('m-Y', time()));
//        $chart_enddate = time();
        
        $chart_startdate = $time_start;
        $chart_enddate = $time_end;
        
        $arr_date = array();
        $arr_total_revenue = array();
        $arr_total_transaction_success = array();
        $arr_total_transaction_error = array();
        $total_revenue_current_month = 0;
        $check_time = strtotime(date("m/d/Y",  time())." 00:00:00");
        while ($chart_startdate < $chart_enddate) {
            $t_start = $chart_startdate;
            $t_end = strtotime(date('d-m-Y', $t_start).'23:59:59 ');

            $filter_chart['startdate'] =$t_start;
            $filter_chart['enddate'] =$t_end;
            $arr_date[] = date("d/m", $t_start);
            //cached revenue, check ko cache ngày hiện tại
            $key_revenue_by_day = "appotapay_chart_revenue_by_day_".$dev_id."_" . $t_start . "_" . $t_end;
            $data_cache = $this->cache->memcached->get($key_revenue_by_day);
            if ($data_cache) {
                $total_revenue_current_month += $data_cache['total_revenue'];
                $arr_total_revenue[] = $data_cache['total_revenue'];
                $arr_total_transaction_success[] = $data_cache['total_transaction_success'];
                $arr_total_transaction_error[] = $data_cache['total_transaction_error'];
            } else {
                $data_info = $this->report_m->getTransactionInfoByDay($filter_chart);
                $total_revenue_current_month += $data_info['total_revenue'];
                $arr_total_revenue[] = $data_info['total_revenue'];
                $arr_total_transaction_success[] = $data_info['total_transaction_success'];
                $arr_total_transaction_error[] = $data_info['total_transaction_error'];
                //set cache, ko cache ngày hiện tại
                $data_set_cache['total_revenue'] = $data_info['total_revenue'];
                $data_set_cache['total_transaction_success'] = $data_info['total_transaction_success'];
                $data_set_cache['total_transaction_error'] = $data_info['total_transaction_error'];
                if ($t_start < $check_time) {
                    $this->cache->memcached->save($key_revenue_by_day, $data_set_cache, 43200);
                }
            }
            $chart_startdate += 86400;
        }
        
        //data today
        $data['total_transaction_success_today'] = $total_transaction_success_today;
        $data['total_transaction_error_today'] = $total_transaction_error_today;
        $data['total_revenue_today'] = $total_revenue_today;
        $data['total_transaction_sms_today'] = $total_transaction_sms_today;
        $data['total_transaction_card_today'] = $total_transaction_card_today;
        $data['total_transaction_bank_today'] = $total_transaction_bank_today;
        //data compare
        $data['total_transaction_success_compare'] = $total_transaction_success_compare;
        $data['total_transaction_error_compare'] = $total_transaction_error_compare;
        $data['total_revenue_compare'] = $total_revenue_compare;
        $data['total_transaction_sms_compare'] = $total_transaction_sms_compare;
        $data['total_transaction_card_compare'] = $total_transaction_card_compare;
        $data['total_transaction_bank_compare'] = $total_transaction_bank_compare;
        //data percent
        $data['percent_transaction_success'] = $percent_transaction_success;
        $data['percent_transaction_error'] = $percent_transaction_error;
        $data['percent_revenue'] = $percent_revenue;
        $data['percent_transaction_sms'] = $percent_transaction_sms;
        $data['percent_transaction_card'] = $percent_transaction_card;
        $data['percent_transaction_bank'] = $percent_transaction_bank;
        //data Chart
        $data['arr_date'] = json_encode($arr_date);
        $data['arr_total_revenue'] = json_encode($arr_total_revenue);
        $data['arr_total_transaction_success'] = json_encode($arr_total_transaction_success);
        $data['arr_total_transaction_error'] = json_encode($arr_total_transaction_error);
        $data['total_revenue_current_month'] = $total_revenue_current_month;
        
        $data['AllProduct'] = $this->report_m->getAllProduct($this->session->userdata('user_id'));
        
        $this->info['menu'] = "report";
        $this->load->view("common/header", $this->info);
        $this->load->view("report_overview", $data);
        $this->load->view("common/footer");
    }
}
    ?>

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).