2024-09-29 15:43:18 +08:00

496 lines
22 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
// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------
namespace app\setting\controller;
use think\admin\Controller;
use app\order\service\OrderService;
use app\setting\service\StatService;
/**
* 统计列表
* Class Order
* @package app\order\controller
*/
class Stat extends Controller
{
/**
* 绑定数据表
* @var string
*/
public $table = 'stat';
/**
* 订单统计
* @auth true
* @menu true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index()
{
$this->title = '订单统计';
$query = $this->_query($this->table);
$date = input('date');
if ($date) {
$date = str_replace('-', '', $date);
$temp = explode('~', $date);
$query->whereRaw('(replace(day, "-", "")+0) >= '. $temp[0] . ' and (replace(day, "-", "")+0) <= ' . $temp[1]);
}
$query->equal('day')->dateBetween('create_at');
$field = '*,(replace(day, "-", "")+0) as day_num';
$order = 'day_num desc,id desc';
$query->field($field);
if (input('output') === 'json') {
$result = $query->order($order)->page(true, false);
$this->success('获取数据列表成功', $result);
} else {
$query->order($order)->page();
}
}
/**
* 统计信息查看
* @login true
* @auth true
* @param integer $id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function info()
{
$this->_applyFormToken();
$id = input('id');
$this->_form($this->table, 'info', 'id', [], ['id' => $id]);
}
/**
* 表单数据处理
* @param array $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function _form_filter(&$data)
{
$this->clist = OrderService::instance()->getChannelList(false, false);
$this->mlist = OrderService::instance()->getMerchantList(false, false);
$this->plist = OrderService::instance()->getProductList(false, false);
$this->slist = OrderService::instance()->getServiceList(false, false);
$data['data'] = $this->table(json_decode($data['data'], true));
}
/**
* 数据列表处理
* @param array $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function _page_filter(&$data)
{
$this->total = StatService::instance()->total();
foreach ($data as $k => $v) {
if (isset($v['success']) && $v['success'] > 0) {
$data[$k]['chengben'] = round($v['product_cash'] / $v['success'], 4);
$data[$k]['success_lv'] = round(($v['success']/$v['total']) * 100, 2) . '%';
} else {
$data[$k]['chengben'] = 0;
}
}
}
private function stat($data)
{
$table = '<table class="layui-table margin-top-10">';
$table .= '<tr>
<td class="text-left nowrap">项目</td>
<td class="text-left nowrap">数值</td>
</tr>';
$table .= '<tr>
<td class="text-left nowrap">总金额</td>
<td class="text-left nowrap">'.$data['total'].'</td>
</tr>';
$table .= '<tr>
<td class="text-left nowrap">成功金额</td>
<td class="text-left nowrap">'.$data['success'].'</td>
</tr>';
$table .= '<tr>
<td class="text-left nowrap">失败金额</td>
<td class="text-left nowrap">'.$data['fail'].'</td>
</tr>';
if ($data['actual_cash']) {
$table .= '<tr>
<td class="text-left nowrap">商户成功金额</td>
<td class="text-left nowrap">'.$data['actual_cash'].'</td>
</tr>';
}
if ($data['product_cash']) {
$table .= '<tr>
<td class="text-left nowrap">渠道成功金额</td>
<td class="text-left nowrap">'.$data['product_cash'].'</td>
</tr>';
}
$table .= '<tr>
<td class="text-left nowrap">利润</td>
<td class="text-left nowrap">'.$data['profit'].'</td>
</tr>';
$table .= '</table>';
return $table;
}
private function table($data)
{
$table = '';
if (isset($data['merchant_total'])) {
$table .= '<fieldset><legend>商户分产品明细数据</legend><table class="layui-table margin-top-10" lay-skin="line">';
$table .= '<thead><tr>
<th class="text-left nowrap">商户</th>
<th class="text-left nowrap">产品</th>
<th class="text-left nowrap">金额</th>
<th class="text-left nowrap">实际金额</th>
<th class="text-left nowrap">渠道金额</th>
<th class="text-left nowrap">利润</th>
<th class="text-left nowrap">成本</th>
<th class="text-left nowrap">明细</th>
</tr></thead>';
$table .= '<tbody>';
foreach ($data['merchant_total'] as $ka => $va) {
foreach ($va as $k => $v) {
if (isset($this->mlist[$ka]) && $this->mlist[$ka] && isset($this->slist[$k]) && isset($v['num'])) {
$order_num = '';
if (isset($v['cash']) && $v['cash'] && is_array($v['cash'])) {
$id = 'm_' . $ka . '_' . $k;
$order_num = '<a href="javascript:show($(this), \''.$id.'\')">查看明细</a><div id="mingxi_'.$id.'" style="display:none;"><table class="layui-table margin-top-10" >';
$order_num .= '<tr>
<td class="text-left nowrap">面值</td>
<td class="text-left nowrap">金额</td>
<td class="text-left nowrap">实际金额</td>
<td class="text-left nowrap">渠道金额</td>
<td class="text-left nowrap">成本</td>
</tr>';
foreach ($v['cash'] as $key => $value) {
$key = str_replace('k_', '', $key);
$order_num .= '<tr>
<td class="text-left nowrap">'.$key.'</td>
<td class="text-left nowrap">'.$value['num'].'</td>
<td class="text-left nowrap">'.$value['actual_num'].'</td>
<td class="text-left nowrap">'.$value['product_num'].'</td>
<td class="text-left nowrap"><span style="color:red">'.round($value['product_num']/$value['num'], 4).'</span></td>
</tr>';
}
$order_num .= '</table></div>';
}
$actual_num = '0';
if (isset($v['actual_num']) && $v['actual_num']) {
$actual_num = $v['actual_num'];
}
$product_num = '0';
if (isset($v['product_num']) && $v['product_num']) {
$product_num = $v['product_num'];
}
$profit = '0';
if (isset($v['profit']) && $v['profit']) {
$profit = $v['profit'];
}
if ($v['num'] <= 0) {
$chengben = 0;
} elseif ($product_num <= 0 ) {
$chengben = 0;
} else {
$chengben = round($product_num/$v['num'], 4);
}
$table .= '<tr><td class="text-left nowrap">'.$this->mlist[$ka]['name'].'</td>
<td class="text-left nowrap">'.$this->slist[$k]['name'].'</td>
<td class="text-left nowrap">'.$v['num'].'</td>
<td class="text-left nowrap">'.$actual_num.'</td>
<td class="text-left nowrap">'.$product_num.'</td>
<td class="text-left nowrap"><span style="color:blue">'.$profit.'</span></td>
<td class="text-left nowrap"><span style="color:red">'.$chengben.'</span></td>
<td class="text-left nowrap">'.$order_num.'</td></tr>';
/*
if (isset($v1['percent_type']) && $v1['percent_type'] == 2) {
$v1['profit'] = '+' . $v1['profit'];
} else {
$v1['profit'] = '-' . $v1['profit'];
}
$table .= '<tr>
<td class="text-left nowrap">'.$this->mlist[$k]['name'].'</td>
<td class="text-left nowrap">'.$this->plist[$k1]['name'].'('.$this->clist[$this->plist[$k1]['cid']]['name'].')</td>
<td class="text-left nowrap">'.$v1['num'].'</td>
<td class="text-left nowrap">'.$v1['percent'].'</td>
<td class="text-left nowrap">'.$v1['profit'].'</td></tr>';
*/
}
}
}
$table .= '</tbody></table></fieldset>';
}
if (isset($data['merchant'])) {
$table .= '<fieldset><legend>商户分渠道明细数据</legend><table class="layui-table margin-top-10" lay-skin="line">';
$table .= '<thead><tr>
<th class="text-left nowrap">商户</th>
<th class="text-left nowrap">产品</th>
<th class="text-left nowrap">金额</th>
<th class="text-left nowrap">实际金额</th>
<th class="text-left nowrap">渠道金额</th>
<th class="text-left nowrap">利润</th>
<th class="text-left nowrap">成本</th>
<th class="text-left nowrap">明细</th>
</tr></thead>';
$table .= '<tbody>';
foreach ($data['merchant'] as $ka => $va) {
foreach ($va as $k => $v) {
if (isset($this->mlist[$ka]) && $this->mlist[$ka] && isset($this->plist[$k]) && isset($v['num'])) {
$order_num = '';
if (isset($v['percent']) && $v['percent'] && is_array($v['percent'])) {
$id = 'm_' . $ka . '_' . $k;
$order_num = '<a href="javascript:show($(this), \''.$id.'\')">查看明细</a><div id="mingxi_'.$id.'" style="display:none;"><table class="layui-table margin-top-10" >';
foreach ($v['percent'] as $key => $value) {
$key = str_replace('k_', '', $key);
$order_num .= '<tr>
<td class="text-left nowrap">面值</td>
<td class="text-left nowrap">实际</td>
<td class="text-left nowrap">订单数</td>
</tr>';
$t = 0;
foreach ($value as $k1 => $v1) {
$t += $v1['num'];
$order_num .= '<tr>
<td class="text-left nowrap">'.$k1.'</td>
<td class="text-left nowrap">'.$v1['cash'].'</td>
<td class="text-left nowrap">'.$v1['num'].'</td>
</tr>';
}
$order_num .= '<tr>
<td class="text-left nowrap"><font style="font-weight:bold">汇总</font></td>
<td class="text-left nowrap"><font style="font-weight:bold">分成:'.$key.'</font></td>
<td class="text-left nowrap"><font style="font-weight:bold">'.$t.'</font></td>
</tr>';
}
$order_num .= '</table></div>';
}
$actual_num = '0';
if (isset($v['actual_num']) && $v['actual_num']) {
$actual_num = $v['actual_num'];
}
$product_num = '0';
if (isset($v['product_num']) && $v['product_num']) {
$product_num = $v['product_num'];
}
$profit = '0';
if (isset($v['profit']) && $v['profit']) {
$profit = $v['profit'];
}
if ($v['num'] <= 0) {
$chengben = 0;
} elseif ($product_num <= 0 ) {
$chengben = 0;
} else {
$chengben = round($product_num/$v['num'], 4);
}
$table .= '<tr><td class="text-left nowrap">'.$this->mlist[$ka]['name'].'</td>
<td class="text-left nowrap">'.$this->plist[$k]['name'].'('.$this->clist[$this->plist[$k]['cid']]['name'].')</td>
<td class="text-left nowrap">'.$v['num'].'</td>
<td class="text-left nowrap">'.$actual_num.'</td>
<td class="text-left nowrap">'.$product_num.'</td>
<td class="text-left nowrap"><span style="color:blue">'.$profit.'</span></td>
<td class="text-left nowrap"><span style="color:red">'.$chengben.'</span></td>
<td class="text-left nowrap">'.$order_num.'</td></tr>';
/*
if (isset($v1['percent_type']) && $v1['percent_type'] == 2) {
$v1['profit'] = '+' . $v1['profit'];
} else {
$v1['profit'] = '-' . $v1['profit'];
}
$table .= '<tr>
<td class="text-left nowrap">'.$this->mlist[$k]['name'].'</td>
<td class="text-left nowrap">'.$this->plist[$k1]['name'].'('.$this->clist[$this->plist[$k1]['cid']]['name'].')</td>
<td class="text-left nowrap">'.$v1['num'].'</td>
<td class="text-left nowrap">'.$v1['percent'].'</td>
<td class="text-left nowrap">'.$v1['profit'].'</td></tr>';
*/
}
}
}
$table .= '</tbody></table></fieldset>';
}
if (isset($data['product'])) {
$table .= '<fieldset><legend>渠道明细数据</legend><table class="layui-table margin-top-10" lay-skin="line">';
$table .= '<thead><tr>
<th class="text-left nowrap">渠道</th>
<th class="text-left nowrap">产品</th>
<th class="text-left nowrap">金额</th>
<th class="text-left nowrap">实际金额</th>
<th class="text-left nowrap">明细</th></tr></thead>';
$table .= '<tbody>';
foreach ($data['product'] as $k => $v) {
if (isset($this->plist[$k]) && $this->plist[$k]) {
$cid = $this->plist[$k]['cid'];
$order_num = '';
if (isset($v['percent']) && $v['percent'] && is_array($v['percent'])) {
$id = 'p_' . $ka . '_' . $k;
$order_num = '<a href="javascript:show($(this), \''.$id.'\')">查看明细</a><div id="mingxi_'.$id.'" style="display:none;"><table class="layui-table margin-top-10" >';
foreach ($v['percent'] as $key => $value) {
$key = str_replace('k_', '', $key);
$order_num .= '<tr>
<td class="text-left nowrap">面值</td>
<td class="text-left nowrap">实际</td>
<td class="text-left nowrap">订单数</td>
</tr>';
$t = 0;
foreach ($value as $k1 => $v1) {
$t += $v1['num'];
$order_num .= '<tr>
<td class="text-left nowrap">'.$k1.'</td>
<td class="text-left nowrap">'.$v1['cash'].'</td>
<td class="text-left nowrap">'.$v1['num'].'</td>
</tr>';
}
$order_num .= '<tr>
<td class="text-left nowrap"><font style="font-weight:bold">汇总</font></td>
<td class="text-left nowrap"><font style="font-weight:bold">分成:'.$key.'</font></td>
<td class="text-left nowrap"><font style="font-weight:bold">'.$t.'</font></td>
</tr>';
}
$order_num .= '</table></div>';
}
$actual_num = '0';
if (isset($v['actual_num']) && $v['actual_num']) {
$actual_num = $v['actual_num'];
}
$table .= '<tr><td class="text-left nowrap">'.$this->clist[$cid]['name'].'</td>
<td class="text-left nowrap">'.$this->plist[$k]['name'].'</td>
<td class="text-left nowrap">'.$v['num'].'</td>
<td class="text-left nowrap">'.$actual_num.'</td>
<td class="text-left nowrap">'.$order_num.'</td></tr>';
}
}
$table .= '</tbody></table></fieldset>';
}
return $table;
}
/*
private function table($data)
{
$table = '<table class="layui-table margin-top-10">';
if (isset($data['product'])) {
$table .= '<tr>
<td class="text-left nowrap">渠道</td>
<td class="text-left nowrap">产品</td>
<td class="text-left nowrap">金额</td>
<td class="text-left nowrap">分成</td>
<td class="text-left nowrap">利润</td></tr>';
foreach ($data['product'] as $k => $v) {
if (isset($this->plist[$k]) && $this->plist[$k]) {
if (isset($v['percent_type']) && $v['percent_type'] == 2) {
$v['profit'] = '-' . $v['profit'];
} else {
$v['profit'] = '+' . $v['profit'];
}
$cid = $this->plist[$k]['cid'];
$table .= '<tr><td class="text-left nowrap">'.$this->clist[$cid]['name'].'</td>
<td class="text-left nowrap">'.$this->plist[$k]['name'].'</td>
<td class="text-left nowrap">'.$v['num'].'</td>
<td class="text-left nowrap">'.$v['percent'].'</td>
<td class="text-left nowrap">'.$v['profit'].'</td></tr>';
}
}
}
$table .= '</table>';
$table .= '<table class="layui-table margin-top-10">';
if (isset($data['merchant'])) {
$table .= '<tr>
<td class="text-left nowrap">商户</td>
<td class="text-left nowrap">产品</td>
<td class="text-left nowrap">金额</td>
<td class="text-left nowrap" style="display:none;">返点</td>
<td class="text-left nowrap">分成</td>
<td class="text-left nowrap">利润</td></tr>';
foreach ($data['merchant'] as $k => $v) {
foreach ($v as $k1 => $v1) {
if (isset($this->mlist[$k]) && $this->mlist[$k] && isset($this->plist[$k1]) && isset($v1['num'])) {
if (isset($v1['percent_type']) && $v1['percent_type'] == 2) {
$v1['profit'] = '+' . $v1['profit'];
} else {
$v1['profit'] = '-' . $v1['profit'];
}
$table .= '<tr>
<td class="text-left nowrap">'.$this->mlist[$k]['name'].'</td>
<td class="text-left nowrap">'.$this->plist[$k1]['name'].'('.$this->clist[$this->plist[$k1]['cid']]['name'].')</td>
<td class="text-left nowrap">'.$v1['num'].'</td>
<td class="text-left nowrap" style="display:none;">'.$v1['rebate'].'</td>
<td class="text-left nowrap">'.$v1['percent'].'</td>
<td class="text-left nowrap">'.$v1['profit'].'</td></tr>';
}
}
}
}
$table .= '</table>';
return $table;
}
*/
}