您的位置:首页 > 其它

销售报表

2015-12-09 15:32 615 查看
//销售报表
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: