Files
liche/admin/controllers/stats/Order.php
T
2022-08-16 17:46:52 +08:00

335 lines
16 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class order extends HD_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('receiver/order/receiver_orders_v2_model', 'mdOrders');
$this->load->model('receiver/receiver_customers_model', 'mdCustomers');
$this->load->model('app/licheb/app_licheb_users_log_model', 'mdUsersLog');
}
//首页信息
public function index()
{
return $this->get_dashboards();
}
public function get_dashboards()
{
$params = $this->input->get();
!$params['type'] && $params['type'] = 1;
$start_date = date('Y-m', strtotime("-2 month"));//取最近3个月
$end_date = date('Y-m');
if ($params['time']) {
$time = explode(' ~ ', $params['time']);
$time[0] && $start_date = $time[0];
$time[1] && $end_date = $time[1];
} else {
$params['time'] = $start_date . ' ~ ' . $end_date;
}
$date_months = $this->dateMonths($start_date, $end_date);
rsort($date_months);
$days = [];
// $days = ['2022-07' => ['month' => 7, 'num' => 0], '2022-06' => ['month' => 6, 'num' => 0],
// '2022-05' => ['month' => 5, 'num' => 0], '2022-04' => ['month' => 4, 'num' => 0]];
foreach ($date_months as $k => $v) {
$month = date('n', strtotime($v . '-01'));
$days[$v] = ['month' => $month, 'num' => 0];
}
$brands = [1 => '东风ev', 4 => '哪吒', 5 => '零跑'];//1,4,5 1,5,4
//$brands = [1 => '东风ev', 5 => '哪吒', 4 => '零跑'];
$bizs = [20 => ['biz_name' => '厦门东风', 'rowspan' => 1], 99 => ['biz_name' => '晋江东风'], 55 => ['biz_name' => '福州东风']];//20,99,55 1,78,110
//$bizs = [1 => ['biz_name' => '厦门东风', 'rowspan' => 1], 78 => ['biz_name' => '晋江东风'], 110 => ['biz_name' => '福州东风']];
foreach ($bizs as $k => $v) {
$list = [];
foreach ($days as $k2 => $v2) {
$order_time = $this->getTime($k2);
$c_time = $this->getTime($k2, 1);
if ($params['type'] == 2) {
$num = $this->mdOrders->count(['biz_id' => $k, 'status>=' => 0, 'order_time>=' => $order_time['s_time'], 'order_time<=' => $order_time['e_time']]);
} else {
$num = $this->mdOrders->db->select('a.id')
->from('lc_receiver_orders_v2 as a')
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
->where(['a.biz_id' => $k, 'a.status>=' => 0, 'a.order_time>=' => $order_time['s_time'], 'a.order_time<=' => $order_time['e_time']
, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']])
->count_all_results();
}
$list[$k2] = $num;
$days[$k2]['num'] = $v2['num'] + $num;
}
$v['list'] = $list;
$bizs[$k] = $v;
}
$brands_stats = $customers_stats = $process = [];
foreach ($bizs as $k => $v) {
$list = [];
foreach ($brands as $k2 => $v2) {
$stats_per = $stats = [];
foreach ($days as $k3 => $v3) {
$order_time = $this->getTime($k3);
$c_time = $this->getTime($k3, 1);
if ($params['type'] == 2) {
$num = $this->mdOrders->count(['biz_id' => $k, 'brand_id' => $k2, 'status>=' => 0, 'order_time>=' => $order_time['s_time'], 'order_time<=' => $order_time['e_time']]);
} else {
$num = $this->mdOrders->db->select('a.id')
->from('lc_receiver_orders_v2 as a')
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
->where(['a.biz_id' => $k, 'a.brand_id' => $k2, 'a.status>=' => 0, 'a.order_time>=' => $order_time['s_time'], 'a.order_time<=' => $order_time['e_time']
, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']])
->count_all_results();
}
$stats_per[] = $this->getPercentage(['num' => $num, 'sum' => $v['list'][$k3]]);
$stats[] = $num;
}
$list[] = ['brand_name' => $v2, 'stats_per' => $stats_per, 'stats' => $stats];
}
$brands_stats[] = ['biz_name' => $v['biz_name'], 'list' => $list];
}
$offlineSources = $this->mdCustomers->offlineSources();
$offlineSources_count = count($offlineSources);
//线索成交情况
foreach ($bizs as $k => $v) {
$list = $cus_sum = $ord_sum = $cus_per = [];
$cus_1 = $ord_1 = [];//自然到店
foreach ($offlineSources as $k2 => $v2) {
$stats_per_cus = $stats_cus = $stats_per_ord = $stats_ord = [];
foreach ($days as $k3 => $v3) {
$c_time = $this->getTime($k3, 1);
$order_time = $this->getTime($k3);
$num_cus = $this->mdCustomers->count(['biz_id' => $k, 'of_id' => $k2, 'status>=' => 0, 'c_time>=' => $c_time['s_time'], 'c_time<=' => $c_time['e_time']]);
if ($params['type'] == 2) {
$num_ord = $this->mdOrders->db->select('a.id')
->from('lc_receiver_orders_v2 as a')
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
->where(['a.biz_id' => $k, 'a.status>=' => 0, 'a.order_time>=' => $order_time['s_time']
, 'a.order_time<=' => $order_time['e_time'], 'b.of_id' => $k2])
->count_all_results();
} else {
$num_ord = $this->mdOrders->db->select('a.id')
->from('lc_receiver_orders_v2 as a')
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
->where(['a.biz_id' => $k, 'a.status>=' => 0, 'a.order_time>=' => $order_time['s_time'], 'a.order_time<=' => $order_time['e_time']
, 'b.of_id' => $k2, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']])
->count_all_results();
}
$cus_sum[$k3] += $num_cus;
$ord_sum[$k3] += $num_ord;
$stats_cus[] = $num_cus;
$stats_ord[$k3] = $num_ord;
$stats_per_cus[] = $this->getPercentage(['num' => $num_ord, 'sum' => $num_cus]);
$stats_per_ord[$k3] = '0.00';
if ($k2 == 1) {//自然到店
$cus_1[$k3] = $num_cus;
$ord_1[$k3] = $num_ord;
}
}
$list[] = ['name' => "{$v2['name']}(转化率)", 'type' => 1, 'stats_per' => $stats_per_cus, 'stats' => $stats_cus];
$list[] = ['name' => "{$v2['name']}(订单占比)", 'type' => 2, 'stats_per' => $stats_per_ord, 'stats' => $stats_ord];
}
foreach ($days as $k3 => $v3) {
$ord = $ord_1[$k3] ? $ord_sum[$k3] - $ord_1[$k3] : $ord_sum[$k3];
$cus = $cus_1[$k3] ? $cus_sum[$k3] - $cus_1[$k3] : $cus_sum[$k3];
$cus_per[] = $this->getPercentage(['num' => $ord, 'sum' => $cus]);
}
$customers_stats[] = ['biz_name' => $v['biz_name'], 'cus_sum' => $cus_sum, 'ord_sum' => $ord_sum, 'cus_per' => $cus_per, 'list' => $list];
}
//过程数据
$process_cus = $process_into = $process_ord = [];
foreach ($bizs as $k => $v) {
$list_cus = $cus_sum = $list_into = $ord_into = $into_sum = $list_ord = $ord_sum = [];
foreach ($offlineSources as $k2 => $v2) {
$stats_per_cus = $stats_cus = $stats_per_into = $stats_into = $stats_per_ord = $stats_ord = [];
foreach ($days as $k3 => $v3) {
$order_time = $this->getTime($k3);
$c_time = $this->getTime($k3, 1);
//线索数
$num_cus = 0;
if ($k2 != 1) {//不包括自然到店
$num_cus = $this->mdCustomers->count(['biz_id' => $k, 'of_id' => $k2, 'status>=' => 0, 'c_time>=' => $c_time['s_time'], 'c_time<=' => $c_time['e_time']]);
$cus_sum[$k3] += $num_cus;
$stats_cus[] = $num_cus;
$stats_per_cus[$k3] = $num_cus;
}
//进店数
if ($params['type'] == 2) {
$num_into = $this->mdUsersLog->db->select('a.id')
->from('lc_receiver_customer_oplogs as a')
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
->where(['a.type' => 4, 'a.log' => '客户到店', 'a.c_time>=' => $c_time['s_time'], 'a.c_time<=' => $c_time['e_time'],
'b.status>=' => 0, 'b.biz_id' => $k, 'b.of_id' => $k2])
->count_all_results();
} else {
$num_into = $this->mdUsersLog->db->select('a.id')
->from('lc_receiver_customer_oplogs as a')
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
->where(['a.type' => 4, 'a.log' => '客户到店', 'a.c_time>=' => $c_time['s_time'], 'a.c_time<=' => $c_time['e_time'],
'b.status>=' => 0, 'b.biz_id' => $k, 'b.of_id' => $k2, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']])
->count_all_results();
}
$into_sum[$k3] += $num_into;
$stats_into[] = $num_into;
$num_into_per = '';
if ($k2 != 1) {//转化率=进店数/客户数
$num_into_per = $this->getPercentage(['num' => $num_into, 'sum' => $num_cus]);
}
$stats_per_into[$k3] = $num_into_per;
//成交数
if ($params['type'] == 2) {
$num_ord = $this->mdOrders->db->select('a.id')
->from('lc_receiver_orders_v2 as a')
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
->where(['a.biz_id' => $k, 'a.status>=' => 0, 'a.order_time>=' => $order_time['s_time']
, 'a.order_time<=' => $order_time['e_time'], 'b.of_id' => $k2])
->count_all_results();
} else {
$num_ord = $this->mdOrders->db->select('a.id')
->from('lc_receiver_orders_v2 as a')
->join('lc_receiver_customers as b', "b.id=a.customer_id", 'left')
->where(['a.biz_id' => $k, 'a.status>=' => 0, 'a.order_time>=' => $order_time['s_time'], 'a.order_time<=' => $order_time['e_time']
, 'b.of_id' => $k2, 'b.c_time>=' => $c_time['s_time'], 'b.c_time<=' => $c_time['e_time']])
->count_all_results();
}
$ord_sum[$k3] += $num_ord;
$stats_ord[] = $num_ord;
//转化率=订单数/进店数
$stats_per_ord[$k3] = $this->getPercentage(['num' => $num_ord, 'sum' => $num_into]);
}
if ($k2 != 1) {//不包括自然到店
$list_cus[] = ['name' => $v2['name'], 'stats_per' => $stats_per_cus, 'stats' => $stats_cus];
}
$list_into[] = ['name' => $v2['name'], 'stats_per' => $stats_per_into, 'stats' => $stats_into];
$list_ord[] = ['name' => $v2['name'], 'stats_per' => $stats_per_ord, 'stats' => $stats_ord];
}
$process_cus[] = ['biz_name' => $v['biz_name'], 'num_sum' => $cus_sum, 'list' => $list_cus];
$process_into[] = ['biz_name' => $v['biz_name'], 'num_sum' => $into_sum, 'list' => $list_into];
$process_ord[] = ['biz_name' => $v['biz_name'], 'num_sum' => $ord_sum, 'list' => $list_ord];
}
$process = ['customers' => $process_cus, 'into_shop' => $process_into, 'orders' => $process_ord];
$this->data['info'] = ['days' => $days, 'bizs' => $bizs, 'bizs_count' => count($bizs), 'days_count' => count($days)
, 'brands_count' => count($brands), 'offlineSources_count' => $offlineSources_count, 'brands_stats' => $brands_stats
, 'customers_stats' => $customers_stats, 'process' => $process, 'type_ary' => [1 => '月销售模型(当月线索)', 2 => '月销售模型']];
$this->data['params'] = $params;
$this->data['_title'] = '东风品牌店群';
return $this->show_view('stats/order/dashboards', true);
}
/**
* Notes:计算出两个日期之间的月份
* Created on: 2022/8/9 14:38
* Created by: dengbw
* @param $start_date [开始日期,如2022-03]
* @param $end_date [结束日期,如2022-12]
* @param string $explode [年份和月份之间分隔符,此例为 - ]
* @param bool $addOne [算取完之后最后是否加一月,用于算取时间戳用]
* @return array [返回是两个月份之间所有月份字符串]
*/
private function dateMonths($start_date, $end_date, $explode = '-', $addOne = false)
{
//判断两个时间是不是需要调换顺序
$start_int = strtotime($start_date);
$end_int = strtotime($end_date);
if ($start_int > $end_int) {
$tmp = $start_date;
$start_date = $end_date;
$end_date = $tmp;
}
//结束时间月份+1,如果是13则为新年的一月份
$start_arr = explode($explode, $start_date);
$start_year = intval($start_arr[0]);
$start_month = intval($start_arr[1]);
$end_arr = explode($explode, $end_date);
$end_year = intval($end_arr[0]);
$end_month = intval($end_arr[1]);
$data = array();
$data[] = $start_date;
$tmp_month = $start_month;
$tmp_year = $start_year;
//如果起止不相等,一直循环
while (!(($tmp_month == $end_month) && ($tmp_year == $end_year))) {
$tmp_month++;
//超过十二月份,到新年的一月份
if ($tmp_month > 12) {
$tmp_month = 1;
$tmp_year++;
}
$data[] = $tmp_year . $explode . str_pad($tmp_month, 2, '0', STR_PAD_LEFT);
}
if ($addOne == true) {
$tmp_month++;
//超过十二月份,到新年的一月份
if ($tmp_month > 12) {
$tmp_month = 1;
$tmp_year++;
}
$data[] = $tmp_year . $explode . str_pad($tmp_month, 2, '0', STR_PAD_LEFT);
}
return $data;
}
private function getPercentage($params)
{
return number_format_com($params['num'] / $params['sum'] * 100, 2, '') . "";
}
private function getTime($day, $type = 0)
{
$s_time = "{$day}-01 00:00:00";//本月一日
$e_time = date('Y-m-d', strtotime("$s_time +1 month -1 day")) . ' 23:59:59';//本月最后一日
if ($type == 1) {
$s_time = strtotime($s_time);
$e_time = strtotime($e_time);
} else if ($type == 2) {
$s_time = "{$day}-01";
$e_time = date('Y-m-d', strtotime("$s_time +1 month -1 day"));
}
return ['s_time' => $s_time, 'e_time' => $e_time];
}
//数据列表
public function lists()
{
}
//展示单条数据
public function get()
{
}
//添加单条数据
public function add()
{
}
//编辑单条数据
public function edit()
{
}
//删除单条数据
public function del()
{
}
//批量操作(默认修改状态)
public function batch()
{
}
//导出数据列表
public function export()
{
}
}