销售报表

//销售报表
function report_forms()
{
    //用户ID
    $user_info_id = $_POST['user_info_id'];
    //获取当月时间
    $data = date('Y-m-d', time());
    $order = M('product_order');

    //一个月
    //获取上一个月的今天日期
    $data2 = date('Y-m-d', strtotime('-2 month'));
    //获取总销售额条件
    $month['time'] = array('between', array($data2, $data));
    $month['user_info_id'] = $user_info_id;
    //销售额
    $list['month_res'] = $order->where($month)->field('sum(total_price)')->find();

    //一个季度
    $data3 = date('Y-m-d', strtotime('-3 month'));
    //获取总销售额条件
    $quarter['time'] = array('between', array($data3, $data));
    $quarter['user_info_id'] = $user_info_id;
    $list['quarter_res'] = $order->where($quarter)->field('sum(total_price)')->find();

    //半年
    $data4 = date('Y-m-d', strtotime('-5 month'));
    //获取总销售额条件
    $half_year['time'] = array('between', array($data4, $data));
    $half_year['user_info_id'] = $user_info_id;
    $list['half_year_res'] = $order->where($half_year)->field('sum(total_price)')->find();

    //一年
    $data5 = date('Y-1-1', time());
    //获取总销售额条件
    $year['time'] = array('between', array($data5, $data));
    $year['user_info_id'] = $user_info_id;
    $list['year_res'] = $order->where($year)->field('sum(total_price)')->find();


    if ($list) {
        $code = array('code' => 1, 'message' => '请求成功');
        $this->ajaxReturn(array_merge($code, $list));
    } else {
        $code = array('code' => 2, 'message' => '请求失败');
        $this->ajaxReturn($code);
    }
}

//历史明细
function history_record()
{
    //用户ID
    $user_info_id = $_POST['user_info_id'];
    $condition['user_info_id'] = $user_info_id;
    $order = M('product_order');
    //取出各月订单数据合计列表
    $amount = $order->field('DATE_FORMAT(time, "%Y%m") as hmonth, SUM(total_price) as amount')->where($condition)
        ->group('hmonth')
        ->order('hmonth desc')
        ->select();
    //如果取出有数据
    if (sizeof($amount) > 0) {
        //从最初月份开始逐月累加的计数器
        $month_index = 0;
        //数据合计列表数组中索引计数器
        $array_index = 0;
        //取出最初月份
        $current_month = $amount[0]['hmonth'];
        $sum_amount = array();
        do {
            //判断当前月份是否等于数据合计列表数组中的月份
            if ($current_month == $amount[$array_index]['hmonth']) {
                //当月合计计入结果数组
                $sum_amount[$month_index]['month_amount'] = $amount[$array_index]['amount'];
                //该月累计数据为之前累计数据加上当月数据
                if ($month_index > 0) {
                    $sum_amount[$month_index]['amount'] = $amount[$array_index]['amount'] + $sum_amount[$month_index - 1]['amount'];
                } else {
                    $sum_amount[$month_index]['amount'] = $amount[$array_index]['amount'];
                }
                //数据合计列表数组中索引计数器增一
                $array_index++;
            } else {
                //当月合计结果为0
                $sum_amount[$month_index]['month_amount'] = 0;
                //该月累计数据为之前累计数据加上当月数据
                $sum_amount[$month_index]['amount'] += $sum_amount[$month_index - 1]['amount'];
            }
            //把月份计入结果数组
            $sum_amount[$month_index]['month'] = $current_month;
            //增长率归零
            $rate = 0;
            //如果上月合计大于零
            if ($sum_amount[$month_index - 1]['month_amount'] > 0) {
                //计算当月增长率
                $rate = round(($sum_amount[$month_index]['month_amount'] - $sum_amount[$month_index - 1]['month_amount']) /
                    $sum_amount[$month_index - 1]['month_amount'], 2);
            }
            //把当月增长率写入结果数组
            $sum_amount[$month_index]['rate'] = $rate;
            //从最初月份开始逐月累加的计数器增一
            $month_index++;
            if (substr($current_month, -2) == '01') {
                $current_month = $current_month - 89;
            } else {
                $current_month--;
            }
        } while ($current_month >= $amount[sizeof($amount) - 1]['hmonth']);
        $list['list'] = $sum_amount;
        $code = array('code' => 1, 'message' => '请求成功');
        $this->ajaxReturn(array_merge($code, $list));
    } else {
        $code = array('code' => 1, 'message' => '请求成功');
        $this->ajaxReturn($code);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值