Update: update ConsultStatisticsController, FinancialStatisticsController, BaseModel, FinancialBidMargin, FinancialBidMarginRecovery, FinancialInvoice to refactor code and improve statistics functions

This commit is contained in:
mkm 2024-07-04 17:30:42 +08:00
parent 6c7bb60abe
commit 3a194a6547
8 changed files with 416 additions and 184 deletions

@ -1,74 +1,97 @@
<?php
namespace app\adminapi\controller\consult_basic;
use app\adminapi\controller\BaseAdminController;
use app\common\model\consult_basic\ConsultProject;
use app\common\model\financial\FinancialInvoice;
use app\common\model\financial\FinancialRefund;
use app\common\model\financial\FinancialSettlement;
use app\common\model\marketing\MarketingContract;
class ConsultStatisticsController extends BaseAdminController
namespace app\adminapi\controller\consult_basic;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\logic\consult_basic\ConsultStatisticsLogic;
use app\common\model\consult_basic\ConsultProject;
use app\common\model\financial\FinancialInvoice;
use app\common\model\financial\FinancialRefund;
use app\common\model\financial\FinancialSettlement;
use app\common\model\marketing\MarketingContract;
class ConsultStatisticsController extends BaseAdminController
{
public function index()
{
public function project(){
//待立项项目
$pending_project_num = MarketingContract::field('id')->where([
['review_status','=', 1],
['contract_type','=',0],
['business_nature','=',3],
['status','=',0]
])->count();
//已立项项目
$approved_project_num = ConsultProject::field('id')->count();
$result['data'] = [
[
'name' => '待立项项目',
'value' => $pending_project_num
],
[
'name' => '已立项项目',
'value' => $approved_project_num
]
];
return $this->success('success',$result);
$date = $this->request->get('date', '');
$where = [];
$time = getDay($date);
$res = ConsultStatisticsLogic::consultStatistics($where, $time);
return $this->data($res);
}
public function project()
{
//待立项项目
$pending_project_num = MarketingContract::field('id')->where([
['review_status', '=', 1],
['contract_type', '=', 0],
['business_nature', '=', 3],
['status', '=', 0]
])->count();
//已立项项目
$approved_project_num = ConsultProject::field('id')->count();
$result['data'] = [
[
'name' => '待立项项目',
'value' => $pending_project_num
],
[
'name' => '已立项项目',
'value' => $approved_project_num
]
];
return $this->success('success', $result);
}
public function invoice()
{
$year = $this->request->get('year', date('Y'));
$contract_ids = ConsultProject::column('contract');
//总开票金额
$total_invoice_amount = FinancialInvoice::where('contract_id', 'in', $contract_ids)->sum('apply_amount');
//年度开票金额
$year_invoice_amount = FinancialInvoice::where('contract_id', 'in', $contract_ids)->whereYear('create_time', $year)->sum('apply_amount');
//总到账金额
$total_refund_amount = FinancialRefund::where('contract_id', 'in', $contract_ids)->sum('amount');
$year_refund_amount = FinancialRefund::where('contract_id', 'in', $contract_ids)->whereYear('date', $year)->sum('amount');
//总结算金额
$total_settlement_amount = FinancialSettlement::where('contract_id', 'in', $contract_ids)->sum('amount');
//年度结算金额
$year_settlement_amount = FinancialSettlement::where('contract_id', 'in', $contract_ids)->whereYear('date', $year)->sum('amount');
$column = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
$invoice_series = [
'name' => '开票金额',
'data' => []
];
$refund_series = [
'name' => '到账金额',
'data' => []
];
$settlement_series = [
'name' => '结算金额',
'data' => []
];
foreach ($column as &$v) {
$invoice_series['data'][] = FinancialInvoice::where('contract_id', 'in', $contract_ids)->whereMonth('create_time', $year . '-' . $v)->sum('apply_amount');
$refund_series['data'][] = FinancialRefund::where('contract_id', 'in', $contract_ids)->whereMonth('date', $year . '-' . $v)->sum('amount');
$settlement_series['data'][] = FinancialSettlement::where('contract_id', 'in', $contract_ids)->whereMonth('date', $year . '-' . $v)->sum('amount');
$v = $v . '月';
}
public function invoice(){
$year = $this->request->get('year',date('Y'));
$contract_ids = ConsultProject::column('contract');
//总开票金额
$total_invoice_amount = FinancialInvoice::where('contract_id','in',$contract_ids)->sum('apply_amount');
//年度开票金额
$year_invoice_amount = FinancialInvoice::where('contract_id','in',$contract_ids)->whereYear('create_time',$year)->sum('apply_amount');
//总到账金额
$total_refund_amount = FinancialRefund::where('contract_id','in',$contract_ids)->sum('amount');
$year_refund_amount = FinancialRefund::where('contract_id','in',$contract_ids)->whereYear('date',$year)->sum('amount');
//总结算金额
$total_settlement_amount = FinancialSettlement::where('contract_id','in',$contract_ids)->sum('amount');
//年度结算金额
$year_settlement_amount = FinancialSettlement::where('contract_id','in',$contract_ids)->whereYear('date',$year)->sum('amount');
$column = ['1','2','3','4','5','6','7','8','9','10','11','12'];
$invoice_series = [
'name' => '开票金额',
'data' => []
];
$refund_series = [
'name' => '到账金额',
'data' => []
];
$settlement_series = [
'name' => '结算金额',
'data' => []
];
foreach($column as &$v){
$invoice_series['data'][] = FinancialInvoice::where('contract_id','in',$contract_ids)->whereMonth('create_time',$year.'-'.$v)->sum('apply_amount');
$refund_series['data'][] = FinancialRefund::where('contract_id','in',$contract_ids)->whereMonth('date',$year.'-'.$v)->sum('amount');
$settlement_series['data'][] = FinancialSettlement::where('contract_id','in',$contract_ids)->whereMonth('date',$year.'-'.$v)->sum('amount');
$v = $v.'月';
}
$result = compact('total_invoice_amount','year_invoice_amount','total_refund_amount','year_refund_amount','total_settlement_amount','year_settlement_amount',
'column','invoice_series','refund_series','settlement_series');
return $this->success('success',$result);
}
}
$result = compact(
'total_invoice_amount',
'year_invoice_amount',
'total_refund_amount',
'year_refund_amount',
'total_settlement_amount',
'year_settlement_amount',
'column',
'invoice_series',
'refund_series',
'settlement_series'
);
return $this->success('success', $result);
}
}

@ -3,7 +3,8 @@
namespace app\adminapi\controller\financial;
use app\adminapi\controller\BaseAdminController;
use app\common\model\financial\FinancialBidMargin;
use app\adminapi\logic\financial\FinancialStatisticsLogic;
use app\common\model\financial\FinancialBidMargin;
use app\common\model\financial\FinancialBidMarginRecovery;
use app\common\model\financial\FinancialBorrowMoney;
use app\common\model\financial\FinancialInvoice;
@ -17,6 +18,15 @@
class FinancialStatisticsController extends BaseAdminController
{
public function index(){
$date=$this->request->get('date','');
$where=[];
$time = getDay($date);
$res=FinancialStatisticsLogic::financialStatistics($where,$time);
return $this->data($res);
}
public function contracts(){
$year = $this->request->get('year',date('Y'));
//统计合同总数

@ -0,0 +1,12 @@
<?php
namespace app\adminapi\logic\consult_basic;
use app\common\logic\BaseLogic;
class ConsultStatisticsLogic extends BaseLogic
{
public static function consultStatistics($param)
{
return app('consult_basic')->consultStatistics($param);
}
}

@ -0,0 +1,140 @@
<?php
namespace app\adminapi\logic\financial;
use app\common\logic\BaseLogic;
use app\common\model\dict\DictData;
use app\common\model\financial\FinancialBidMargin;
use app\common\model\financial\FinancialBidMarginRecovery;
use app\common\model\financial\FinancialBorrowMoney;
use app\common\model\financial\FinancialInvoice;
use app\common\model\financial\FinancialPerformanceMoneyApply;
use app\common\model\financial\FinancialPerformanceMoneyRecovery;
use app\common\model\financial\FinancialRefund;
use app\common\model\financial\FinancialRepayment;
use app\common\model\financial\FinancialUsingFunds;
class FinancialStatisticsLogic extends BaseLogic
{
/**
* 统计
*/
public static function financialStatistics($where, $time)
{
$data = [];
$datearr = explode('-', $time);
$time = TimeConvert(['start_time' => $datearr[0], 'end_time' => $datearr[1]]);
$marketingContract = new FinancialInvoice();
$financialRefund = new FinancialRefund();
$financialBidMargin = new FinancialBidMargin();
$financialBidMarginRecovery = new FinancialBidMarginRecovery();
$financialPerformanceMoneyApply = new FinancialPerformanceMoneyApply();
$financialPerformanceMoneyRecovery = new FinancialPerformanceMoneyRecovery();
$financialUsingFunds = new FinancialUsingFunds();
$financialBorrowMoney = new FinancialBorrowMoney();
$financialRepayment = new FinancialRepayment();
//开票金额
$total_apply_amount = $marketingContract->ContractFinancialMoney([], $time, 'sum', '', 'apply_amount');
$apply_amount_value = $marketingContract->ContractFinancialMoney([], $time, 'group', 'create_time', 'apply_amount');
$data[] = [
'title' => '开票金额',
'desc' => '',
'total_money' => $total_apply_amount,
'value' => $apply_amount_value['y'],
'type' => 1,
];
//回款金额
$total_amount = $financialRefund->statistics([], $time, 'sum', '', 'amount');
$amount_value = $financialRefund->statistics([], $time, 'group', 'create_time', 'amount');
$data[] = [
'title' => '回款金额',
'desc' => '',
'total_money' => $total_amount,
'value' => $amount_value['y'],
'type' => 1,
];
//结算金额
// $total_capply_amount=$marketingContract->ContractFinancialMoney([],$time,'sum','','amount');
// $total_apply_amount_value=$marketingContract->ContractFinancialMoney([],$time,'group','create_time','amount');
// $data[]=[
// 'title' => '回款金额',
// 'desc' => '',
// 'total_money' => $total_capply_amount,
// 'value' => $total_apply_amount_value['y'],
// 'type' => 1,
// ];
//保证金申请
$total_bid_margin = $financialBidMargin->statistics([], $time, 'sum', '', 'bid_margin');
$bid_margin_value = $financialBidMargin->statistics([], $time, 'group', 'create_time', 'bid_margin');
$data[] = [
'title' => '保证金申请',
'desc' => '',
'total_money' => $total_bid_margin,
'value' => $bid_margin_value['y'],
'type' => 1,
];
//保证金回收
$total_pay_amount = $financialBidMarginRecovery->statistics([], $time, 'sum', '', 'pay_amount');
$pay_amount_value = $financialBidMarginRecovery->statistics([], $time, 'group', 'create_time', 'pay_amount');
$data[] = [
'title' => '保证金回收',
'desc' => '',
'total_money' => $total_pay_amount,
'value' => $pay_amount_value['y'],
'type' => 1,
];
//履约申请
$apply_amount = $financialPerformanceMoneyApply->statistics([], $time, 'sum', '', 'apply_amount');
$apply_amount_value = $financialPerformanceMoneyApply->statistics([], $time, 'group', 'create_time', 'apply_amount');
$data[] = [
'title' => '履约申请',
'desc' => '',
'total_money' => $apply_amount,
'value' => $apply_amount_value['y'],
'type' => 1,
];
//履约回收
$recovery_amount = $financialPerformanceMoneyRecovery->statistics([], $time, 'sum', '', 'recovery_amount');
$recovery_amount_value = $financialPerformanceMoneyRecovery->statistics([], $time, 'group', 'create_time', 'recovery_amount');
$data[] = [
'title' => '履约回收',
'desc' => '',
'total_money' => $recovery_amount,
'value' => $recovery_amount_value['y'],
'type' => 1,
];
//用款
$has_pay_amount = $financialUsingFunds->statistics([], $time, 'sum', '', 'has_pay_amount');
$has_pay_amount_value = $financialUsingFunds->statistics([], $time, 'group', 'create_time', 'has_pay_amount');
$data[] = [
'title' => '用款',
'desc' => '',
'total_money' => $has_pay_amount,
'value' => $has_pay_amount_value['y'],
'type' => 1,
];
//借款
$amount = $financialBorrowMoney->statistics([], $time, 'sum', '', 'amount');
$amount_value = $financialBorrowMoney->statistics([], $time, 'group', 'create_time', 'amount');
$data[] = [
'title' => '借款',
'desc' => '',
'total_money' => $amount,
'value' => $amount_value['y'],
'type' => 1,
];
//还款
$amount = $financialRepayment->statistics([], $time, 'sum', '', 'amount');
$amount_value = $financialRepayment->statistics([], $time, 'group', 'create_time', 'amount');
$data[] = [
'title' => '还款',
'desc' => '',
'total_money' => $amount,
'value' => $amount_value['y'],
'type' => 1,
];
return $data;
}
}

@ -59,7 +59,29 @@ class BaseModel extends Model
$dict = DictData::where('type_value', 'check_status')->column('name', 'value');
return !empty($data['approve_check_status']) ? $dict[$data['approve_check_status']] : '待审核';
}
/**
* 统计
*/
public function statistics($where, $time, string $selectType, string $group = "", $sum = '')
{
switch ($selectType) {
case "sum":
$totalMoney = $this->where($where)->when(isset($time), function ($query) use ($time) {
$query->whereBetweenTime('create_time', strtotime($time['start_time']), strtotime($time['end_time']));
})->sum($sum);
break;
case "group":
$totalMoney = $this->getCurveData($where, $time, "sum($sum)", $group);
break;
default:
throw new \Exception($this->name.':selectType参数错误');
}
if ($group) {
$totalMoney = $this->trendYdata((array)$totalMoney, $time);
}
return $totalMoney;
}
/**
* 曲线统计
* @param $time

@ -11,40 +11,40 @@
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\common\model\financial;
use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use think\model\concern\SoftDelete;
/**
* 财务管理--投标保证金申请模型
* Class FinancialBidMargin
* @package app\common\model\financial
*/
class FinancialBidMargin extends BaseModel
namespace app\common\model\financial;
use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use think\model\concern\SoftDelete;
/**
* 财务管理--投标保证金申请模型
* Class FinancialBidMargin
* @package app\common\model\financial
*/
class FinancialBidMargin extends BaseModel
{
use SoftDelete;
protected $name = 'financial_bid_margin';
protected $deleteTime = 'delete_time';
public function getPayTypeTextAttr($value, $data)
{
use SoftDelete;
protected $name = 'financial_bid_margin';
protected $deleteTime = 'delete_time';
public function getPayTypeTextAttr($value, $data)
{
$dict = DictData::where('type_value', 'financial_pay_type')->column('name', 'value');
return !empty($data['pay_type']) ? $dict[$data['pay_type']] : '';
}
public function getEndDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
public function getExpectedReturnDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
}
$dict = DictData::where('type_value', 'financial_pay_type')->column('name', 'value');
return !empty($data['pay_type']) ? $dict[$data['pay_type']] : '';
}
public function getEndDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
public function getExpectedReturnDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
}

@ -11,55 +11,56 @@
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\common\model\financial;
use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use think\model\concern\SoftDelete;
/**
* 财务管理--投标保证金回收模型
* Class FinancialBidMarginRecovery
* @package app\common\model\financial
*/
class FinancialBidMarginRecovery extends BaseModel
namespace app\common\model\financial;
use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use think\model\concern\SoftDelete;
/**
* 财务管理--投标保证金回收模型
* Class FinancialBidMarginRecovery
* @package app\common\model\financial
*/
class FinancialBidMarginRecovery extends BaseModel
{
use SoftDelete;
protected $name = 'financial_bid_margin_recovery';
protected $deleteTime = 'delete_time';
public function getPayTypeTextAttr($value, $data)
{
use SoftDelete;
protected $name = 'financial_bid_margin_recovery';
protected $deleteTime = 'delete_time';
public function getPayTypeTextAttr($value, $data)
{
$dict = DictData::where('type_value', 'financial_pay_type')->column('name', 'value');
return !empty($data['pay_type']) ? $dict[$data['pay_type']] : '';
}
public function getEndDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
public function getExpectedReturnDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
public function getPayDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
public function getOperationDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
public function getRecoveryDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
}
$dict = DictData::where('type_value', 'financial_pay_type')->column('name', 'value');
return !empty($data['pay_type']) ? $dict[$data['pay_type']] : '';
}
public function getEndDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
public function getExpectedReturnDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
public function getPayDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
public function getOperationDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
public function getRecoveryDateAttr($value): string
{
return !empty($value) ? date('Y-m-d', $value) : '';
}
}

@ -11,30 +11,54 @@
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\common\model\financial;
use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use think\model\concern\SoftDelete;
/**
* 财务管理--开票台账模型
* Class FinancialInvoice
* @package app\common\model\financial
*/
class FinancialInvoice extends BaseModel
namespace app\common\model\financial;
use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use think\model\concern\SoftDelete;
/**
* 财务管理--开票台账模型
* Class FinancialInvoice
* @package app\common\model\financial
*/
class FinancialInvoice extends BaseModel
{
use SoftDelete;
protected $name = 'financial_invoice';
protected $deleteTime = 'delete_time';
public function getInvoiceTypeTextAttr($value, $data)
{
use SoftDelete;
protected $name = 'financial_invoice';
protected $deleteTime = 'delete_time';
public function getInvoiceTypeTextAttr($value, $data)
{
$dict = DictData::where('type_value', 'zjzx_invoice_type')->column('name', 'value');
return !empty($data['invoice_type']) ? $dict[$data['invoice_type']] : '';
$dict = DictData::where('type_value', 'zjzx_invoice_type')->column('name', 'value');
return !empty($data['invoice_type']) ? $dict[$data['invoice_type']] : '';
}
/**
* 统计合同金额
*/
public function ContractFinancialMoney($where, $time, string $selectType, string $group = "", $sum = '')
{
switch ($selectType) {
case "sum":
$totalMoney = $this->where($where)->when(isset($time), function ($query) use ($time) {
$query->whereBetweenTime('create_time', strtotime($time['start_time']), strtotime($time['end_time']));
})->sum($sum);
break;
case "group":
$totalMoney = $this->getCurveData($where, $time, "sum($sum)", $group);
break;
default:
throw new \Exception('ContractFinancialMoney:selectType参数错误');
}
}
if ($group) {
$totalMoney = $this->trendYdata((array)$totalMoney, $time);
}
return $totalMoney;
}
}