REAPI/app/setting/controller/Account.php

292 lines
8.8 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?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\setting\service\AccountLogService;
use app\setting\service\AccountService;
/**
* 总账统计列表
* Class Order
* @package app\order\controller
*/
class Account extends Controller
{
/**
* 绑定数据表
* @var string
*/
public $table = 'stat_account';
/**
* 账单统计
* @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();
}
}
/**
* 数据列表处理
* @param array $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function _page_filter(&$data)
{
foreach ($data as &$vo) {
$log['channel'] = AccountLogService::instance()->db()->where(array('day' => $vo['day'], 'type' => 1))->order('id asc')->select();
$log['merchant'] = AccountLogService::instance()->db()->where(array('day' => $vo['day'], 'type' => 2))->order('id asc')->select();
$log['account'] = AccountLogService::instance()->db()->where(array('day' => $vo['day'], 'type' => 3))->order('id asc')->select();
$vo['channel'] = $this->getTable('渠道', $log['channel']);
$vo['merchant'] = $this->getTable('商户', $log['merchant']);
$vo['account'] = $this->getTable('公户', $log['account']);
$vo['merchant_total'] = round($vo['merchant_total'], 2);
$vo['channel_total'] = round($vo['channel_total'], 2);
$vo['account_total'] = round($vo['account_total'], 2);
$vo['total'] = $vo['merchant_total'] + $vo['channel_total'] + $vo['account_total'];
$ydata = AccountService::instance()->getDay($vo['day']);
$vo['profit'] = '0.00';
if ($ydata) {
$ytotal = $ydata['merchant_total'] + $ydata['channel_total'] + $ydata['account_total'];
$vo['profit'] = round($vo['total']- $ytotal, 2);
}
}
}
private function getTotal($data)
{
$table = '<table class="layui-table margin-top-10">';
$table .= '<tr>
<td class="text-left nowrap">类型</td>
<td class="text-left nowrap">金额</td>';
$table .= '</tr>';
$merchant = round($data['merchant_total'], 2);
$channel = round($data['channel_total'], 2);
$account = round($data['account_total'], 2);
$total = $merchant + $channel + $account;
$table .= '<tr>
<td class="text-left nowrap">商户</td>
<td class="text-left nowrap">'.$merchant.'</td>';
$table .= '</tr>';
$table .= '<tr>
<td class="text-left nowrap">渠道</td>
<td class="text-left nowrap">'.$channel.'</td>';
$table .= '</tr>';
$table .= '<tr>
<td class="text-left nowrap">公户</td>
<td class="text-left nowrap">'.$account.'</td>';
$table .= '</tr>';
$table .= '<tr>
<td class="text-left nowrap">合计</td>
<td class="text-left nowrap">'.$total.'</td>';
$table .= '</tr>';
$ydata = AccountService::instance()->getDay($data['day']);
if ($ydata) {
$ytotal = $ydata['merchant_total'] + $ydata['channel_total'] + $ydata['account_total'];
$yue = round($total - $ytotal, 2);
$table .= '<tr>
<td class="text-left nowrap">利润</td>
<td class="text-left nowrap">'.$yue.'</td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
private function getTable($name, $data)
{
$info = array();
$thead = array();
foreach ($data as $k => $v) {
if (!$v['name']) {
if ($v['type'] == -1) {
$v['name'] = '余额';
} elseif ($v['type'] == 1) {
$v['name'] = '收入';
} elseif ($v['type'] == 2) {
$v['name'] = '支出';
}
}
if ($v['type'] == -1) {
$v['type'] = 1;
}
$v['num'] = str_replace(',','',$v['num']);
$v['num'] = (float) $v['num'];
if (isset($info[$v['type_name']][$v['name']]) && $info[$v['type_name']][$v['name']]) {
$info[$v['type_name']][$v['name']]['num'] += $v['num'];
} else {
$info[$v['type_name']][$v['name']] = $v;
}
$thead[$v['name']] = $v['name'];
}
$table = '<table class="layui-table margin-top-10">';
$table .= '<tr>
<td class="text-left nowrap">'.$name.'</td>';
foreach ($thead as $k => $v) {
$table .= '<td class="text-left nowrap">'.$v.'</td>';
}
$table .= '</tr>';
$total = array();
foreach ($info as $k => $v) {
$table .= '<tr>';
$table .= '<td class="text-left nowrap">'.$k.'</td>';
foreach ($thead as $k1 => $v1) {
if (isset($v[$k1]) && $v[$k1]) {
$total[$k1]['oper'] = $v[$k1]['oper'];
if (!isset($total[$k1]['num'])) {
$total[$k1]['num'] = 0;
}
$v[$k1]['num'] = (float) $v[$k1]['num'];
$total[$k1]['num'] += $v[$k1]['num'];
/*
if ($v[$k1]['oper'] == 1) {
$v[$k1]['num'] = '+' . $v[$k1]['num'];
} elseif ($v[$k1]['oper'] == 2 && $v[$k1]['num'] > 0) {
$v[$k1]['num'] = '-' . $v[$k1]['num'];
}
if ($v[$k1]['num'] == 0) {
$v[$k1]['num'] = '0.00';
}
*/
if ($k1 == '对账') {
if ($v[$k1]['num'] != $v['当日余额']['num']) {
$v[$k1]['num'] = '<span style="color:red">'.$v[$k1]['num'].'</span>';
}
}
$table .= '<td class="text-left nowrap">'.$v[$k1]['num'].'</td>';
}
}
$table .= '</tr>';
}
$table .= '<tr><td class="text-left nowrap">合计</td>';
foreach ($total as $k2 => $v2) {
// $v2['num'] = str_replace(',', '', number_format($v2['num'], 2));
/*
if ($v2['oper'] == 1) {
$v2['num'] = '+' . $v2['num'];
} elseif ($v2['oper'] == 2 && $v2['num'] > 0) {
$v2['num'] = '-' . $v2['num'];
}
*/
$table .= '<td class="text-left nowrap">'.round($v2['num'], 2).'</td>';
}
$table .= '</tr>';
$table .= '</table>';
return $table;
}
/**
* 重新统计
* @auth true
* @throws \think\db\exception\DbException
*/
public function reset()
{
$day = input('day');
\app\setting\service\AccountLogService::instance()->handle($day);
$this->success('操作成功');
}
}