ecshop按月统计订单并输出柱状图

来源:互联网 发布:西安麻将软件 编辑:程序博客网 时间:2024/05/18 18:43

柱状图使用的是bootstrap的char.js

/*------------------------------------------------------ */
//--订单统计需要的函数
/*------------------------------------------------------ */


/**
  * 取得订单概况数据(包括订单的几种状态)
  * @param       $start_date    开始查询的日期
  * @param       $end_date      查询的结束日期
  * @return      $order_info    订单概况数据
  */
 function get_account_info($start_date, $end_date)
 {
    $res = array();
    $year_start = date("Y", $start_date);
    $year_end = date("Y", $end_date);
    $years = $year_start.",".$year_end;
    /* 线上交易 */
    $sql = "SELECT DATE_FORMAT( FROM_UNIXTIME( add_time, '%Y%m%d' ),'%Y-%m') as month,sum(money_paid) as money ". 
            " FROM " . $GLOBALS['ecs']->table("order_info").
            " where DATE_FORMAT( FROM_UNIXTIME( add_time, '%Y%m%d' ),'%Y') IN (". $years .")" . order_query_sql('finished'). 
            " AND add_time >= '$start_date'".
            " AND add_time < '" . ($end_date + 86400) . "'  " .
            " group by month order by month";
           // echo $sql;
    $online_account = $GLOBALS['db']->getAll($sql);


    /* 线上交易 */
    $sql = "SELECT DATE_FORMAT( FROM_UNIXTIME( add_time, '%Y%m%d' ),'%Y-%m') as month,sum(pay_money) as money ". 
            " FROM " . $GLOBALS['ecs']->table("wechat_auther_spay").
            " where DATE_FORMAT( FROM_UNIXTIME( add_time, '%Y%m%d' ),'%Y') IN (". $years .")".  
            " AND pay_status = 1 AND add_time >= '$start_date'".
            " AND add_time < '" . ($end_date + 86400) . "' " .
            " group by month order by month";


    $unline_account = $GLOBALS['db']->getAll($sql);
    /*提现*/
     $sql = "SELECT DATE_FORMAT( FROM_UNIXTIME( handle_time, '%Y%m%d' ),'%Y-%m') as month,sum(extract_account) as money ". 
            " FROM " . $GLOBALS['ecs']->table("seller_extract_log").
            " where DATE_FORMAT( FROM_UNIXTIME( handle_time, '%Y%m%d' ),'%Y') IN (". $years . ")" . 
            " AND handle_status = 1 AND handle_time >= '$start_date'".
            " AND handle_time < '" . ($end_date + 86400) . "' " .
            " group by month order by month";
    
    $extract_account = $GLOBALS['db']->getAll($sql);


    $month_arr = diff_months($start_date, $end_date);
    $label_str = "";
    $online_account_str = "";
    $unline_account_str = "";
    $all_account_str = "";
    $extract_account_str = "";
    $arr = array();
    foreach ($month_arr as $key => $val) {
        $arr[$key]['month'] = $val;
        $label_str .= "\"".$val."\",";
        //线上
        $arr[$key]['online_account'] = 0;
        if($online_account){
            foreach ($online_account as $on_key => $on_val) {
                if($val == $on_val['month']){
                    $arr[$key]['online_account'] = $on_val['money'];
                }
            }
        }
        $online_account_str .= $arr[$key]['online_account'].",";
        //线下
        $arr[$key]['unline_account'] = 0;
        if($unline_account){
            foreach ($unline_account as $un_key => $un_val) {
                if($val == $un_val['month']){
                    $arr[$key]['unline_account'] = $un_val['money'];
                }
            }
        }
        $unline_account_str .= $arr[$key]['unline_account'].",";
        //总
        $arr[$key]['all_account'] = $arr[$key]['online_account'] + $arr[$key]['unline_account'];
        $all_account_str .= $arr[$key]['all_account'].",";
        //提取
        $arr[$key]['extract_account'] = 0;
        if($extract_account){
            foreach ($extract_account as $ex_key => $ex_val) {
                if($val == $ex_val['month']){
                    $arr[$key]['extract_account'] = $ex_val['money'];
                }
            }
        }
        $extract_account_str .= $arr[$key]['extract_account'].",";
    }
    $res['arr'] = $arr;


    $label_str = rtrim($label_str, ",");
    $online_account_str = rtrim($online_account_str, ",");
    $unline_account_str = rtrim($unline_account_str, ",");
    $all_account_str = rtrim($all_account_str, ",");
    $extract_account_str = rtrim($extract_account_str, ",");
    $data_str = "{";
    $data_str .= "labels: [".$label_str."],";
    $data_str .= "datasets: [";
    $data_str .= "{label: '线上交易',backgroundColor: 'rgba(67,97,250,0.5)',
                data: [".$online_account_str."]}, ";
    $data_str .= "{label: '线下交易',backgroundColor: 'rgba(67,153,250,0.5)',
                data: [".$unline_account_str."]}, ";
    $data_str .= "{label: '总交易',backgroundColor: 'rgba(67,205,250,0.5)',
                data: [".$all_account_str."]}, ";
    $data_str .= "{label: '总提现',backgroundColor: 'rgba(151,187,205,0.5)',
                data: [".$extract_account_str."]}, ";
    $data_str .= "]";
    $data_str .= "}";
    $res['data_str'] = $data_str;
    return $res;
 }


 function diff_months($start_date, $end_date){


    $start_date_format = date("Y-m-d", $start_date);
    $end_date_format = date("Y-m-d", $end_date);
     
    $start_arr = explode("-", $start_date_format);
    $end_arr = explode("-", $end_date_format);
     
    $start_year = intval($start_arr[0]);
    $start_month = intval($start_arr[1]);
     
    $end_year = intval($end_arr[0]);
    $end_month = intval($end_arr[1]);
     
    $diff_year = $end_year-$start_year;
     
    $month_arr = array();
    //获取月份
    if($diff_year == 0){
        for($month = $start_month;$month<=$end_month;$month++){
            $month_arr[] = $start_year.'-'.check_month($month);
        }
    } else {
        for($year =$start_year;$year<=$end_year;$year++){
            if($year == $start_year){
                for($month = $start_month;$month<=12;$month++){
                    $month_arr[] = $year.'-'.check_month($month);
                }
            }elseif($year==$end_year){
                for($month = 1;$month<=$end_month;$month++){
                    $month_arr[] = $year.'-'.check_month($month);
                }
            }else{
                for($month = 1;$month<=12;$month++){
                    $month_arr[] = $year.'-'.check_month($month);
                }
            }
        }   
    }
    return $month_arr;
 }


 function check_month($month){
    $month = intval($month);
    if($month < 10){
        $str  = "0".$month;
    }else{
        $str = $month;
    }
    return $str;


 }




<script>
        
        
        var orderData = {$account_info.data_str};
        
        
        window.onload = function() {
            
            var ctx = document.getElementById("canvas_order").getContext("2d");
            window.myBar = new Chart(ctx, {
                type: 'bar',
                data: orderData,
                options: {
                    title:{
                        display:true,
                        text:"Chart.js Bar Chart - Stacked"
                    },
                    responsive: true,
                }
            });
            
        };


       
    </script>

0 0
原创粉丝点击