Files
liche/admin/controllers/stats/Order.php
T
2022-08-05 16:37:36 +08:00

195 lines
9.0 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();
$this->data['params'] = $params;
$brands = [1 => '东风ev', 4 => '哪吒', 5 => '零跑'];//1,4,5 1,5,4
$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]];
$bizs = [20 => ['biz_name' => '厦门东风', 'rowspan' => 1], 99 => ['biz_name' => '晋江东风'], 55 => ['biz_name' => '福州东风']];//20,99,55 1,78,110
foreach ($bizs as $k => $v) {
$list = [];
foreach ($days as $k2 => $v2) {
$order_time = $this->getTime($k2);
$num = $this->mdOrders->count(['biz_id' => $k, 'status>=' => 0, 'order_time>=' => $order_time['s_time'], 'order_time<=' => $order_time['e_time']]);
$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);
$num = $this->mdOrders->count(['biz_id' => $k, 'brand_id' => $k2, 'status>=' => 0, 'order_time>=' => $order_time['s_time'], 'order_time<=' => $order_time['e_time']]);
$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']]);
$num_ord = $this->mdOrders->count([
"customer_id in(select id from lc_receiver_customers where biz_id={$k} and of_id={$k2} and status>=0)" => null,
'biz_id' => $k, 'status>=' => 0, 'order_time>=' => $order_time['s_time'], 'order_time<=' => $order_time['e_time']]);
$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) {
//线索数
if ($k2 != 1) {//不包括自然到店
$c_time = $this->getTime($k3, 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']]);
$stats_cus[] = $num_cus;
$cus_sum[$k3] += $num_cus;
$stats_per_cus[$k3] = $num_cus;
}
//进店数
$log_date = $this->getTime($k3, 2);
$sum = $this->mdUsersLog->sum("customer_{$k2}", ['biz_id' => $k, 'log_date>=' => $log_date['s_time'], 'log_date<=' => $log_date['e_time']]);
$num_into = intval($sum["customer_{$k2}"]);
$stats_into[] = $num_into;
$into_sum[$k3] += $num_into;
$stats_per_into[$k3] = $num_into;
//成交数
$order_time = $this->getTime($k3);
$num_ord = $this->mdOrders->count([
"customer_id in(select id from lc_receiver_customers where biz_id={$k} and of_id={$k2} and status>=0)" => null,
'biz_id' => $k, 'status>=' => 0, 'order_time>=' => $order_time['s_time'], 'order_time<=' => $order_time['e_time']]);
$stats_ord[] = $num_ord;
$ord_sum[$k3] += $num_ord;
$stats_per_ord[$k3] = $num_ord;
}
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];
$this->data['_title'] = '东风品牌店群';
return $this->show_view('stats/order/dashboards', true);
}
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()
{
}
}