Commit e9c74026 by liuyingkang

refactor: 优化合同和律师相关查询逻辑

- 在合同查询中添加软删除条件,确保查询结果不包含已删除记录
- 优化分页逻辑,使用SQL查询的总记录数代替手动计算
- 在律师汇总查询中添加日志记录,便于调试和追踪
- 删除不必要的代码注释,提高代码可读性
parent 29ff5831
...@@ -26,6 +26,8 @@ class CovenantReceivePaymentController extends AdminController ...@@ -26,6 +26,8 @@ class CovenantReceivePaymentController extends AdminController
protected function grid() protected function grid()
{ {
return Grid::make(new CovenantReceivePaymentCollect(), function (Grid $grid) { return Grid::make(new CovenantReceivePaymentCollect(), function (Grid $grid) {
// 设置每页显示100条数据
// 获取筛选条件 // 获取筛选条件
$params = []; $params = [];
$params['lnum'] = $_GET['lnum'] ?? ''; $params['lnum'] = $_GET['lnum'] ?? '';
......
...@@ -23,11 +23,13 @@ class LawyerCovenantIncomeController extends AdminController ...@@ -23,11 +23,13 @@ class LawyerCovenantIncomeController extends AdminController
protected function grid() protected function grid()
{ {
return Grid::make(new Covenant(), function (Grid $grid) { return Grid::make(new Covenant(), function (Grid $grid) {
$grid->model()->whereNull('deleted_at'); // 新增这行
$lawyerID = $grid->model()->filter()->input('no') ?? ''; //律师ID $lawyerID = $grid->model()->filter()->input('no') ?? ''; //律师ID
if ($lawyerID) { if ($lawyerID) {
$grid->model()->where('lawyer_id', $lawyerID); $grid->model()->where('lawyer_id', $lawyerID);
} }
//$grid->column('cid')->sortable(); $grid->column('id');
$grid->column('number', '合同编号'); $grid->column('number', '合同编号');
$grid->column('cname', '合同名称'); $grid->column('cname', '合同名称');
$grid->column('ctype', '合同类型')->display(function ($val) { $grid->column('ctype', '合同类型')->display(function ($val) {
...@@ -45,7 +47,6 @@ protected function grid() ...@@ -45,7 +47,6 @@ protected function grid()
$grid->disableViewButton(); $grid->disableViewButton();
$grid->disableCreateButton(); $grid->disableCreateButton();
$grid->disableEditButton(); $grid->disableEditButton();
$grid->disableDeleteButton();
$grid->disableRowSelector(); $grid->disableRowSelector();
$grid->filter(function (Grid\Filter $filter) { $grid->filter(function (Grid\Filter $filter) {
......
...@@ -108,7 +108,9 @@ public function getList(array $param) ...@@ -108,7 +108,9 @@ public function getList(array $param)
$note_monitor = DB::table('covenant as c') $note_monitor = DB::table('covenant as c')
->select(['rp.*', 'c.id as cid', 'c.cname', 'c.ctype', 'c.number', 'c.lawyer_id', 'c.principal']) ->select(['rp.*', 'c.id as cid', 'c.cname', 'c.ctype', 'c.number', 'c.lawyer_id', 'c.principal'])
->leftJoin('covenant_receive_payment AS rp', 'rp.cid', '=', 'c.id') ->leftJoin('covenant_receive_payment AS rp', 'rp.cid', '=', 'c.id')
->where($condition); ->where($condition)
->whereNull('c.deleted_at') // 添加软删除条件
->whereNull('rp.deleted_at'); // 添加软删除条件
if ($invoiced_at['start'] && $received_at['start']) { if ($invoiced_at['start'] && $received_at['start']) {
$in_start = $invoiced_at['start'] ?? ''; $in_start = $invoiced_at['start'] ?? '';
$in_end = $invoiced_at['end'] ?? ''; $in_end = $invoiced_at['end'] ?? '';
......
...@@ -62,9 +62,9 @@ public function get(Grid\Model $model) ...@@ -62,9 +62,9 @@ public function get(Grid\Model $model)
$data = $this->getList($param); $data = $this->getList($param);
// 确保total和数据都是有效的 // 使用getList返回的total作为总数,而不是count($items)
$items = $data['subjects'] ?? []; $items = $data['subjects'] ?? [];
$total = count($items); // 直接使用实际的数据数量作为总 $total = $data['total'] ?? 0; // 使用SQL查询的总记录
Log::add('Pagination data:', [ Log::add('Pagination data:', [
'total' => $total, 'total' => $total,
...@@ -186,6 +186,15 @@ public function getList(array $param) ...@@ -186,6 +186,15 @@ public function getList(array $param)
}) })
->get(); ->get();
// 获取并打印最后一个查询
$queries = DB::getQueryLog();
$lastQuery = end($queries);
Log::add('Last SQL Query:', [
'sql' => $lastQuery['query'],
'bindings' => $lastQuery['bindings'],
'time' => $lastQuery['time']
]);
foreach ($list as $key => $item) { foreach ($list as $key => $item) {
$tmp = []; $tmp = [];
$tmp['lnum'] = $item->lnum; $tmp['lnum'] = $item->lnum;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use App\Command\Log;
class Lawyer extends Model class Lawyer extends Model
{ {
...@@ -97,29 +98,45 @@ public static function getTotalBasiSalary($params, $received_at, $invoiced_at) ...@@ -97,29 +98,45 @@ public static function getTotalBasiSalary($params, $received_at, $invoiced_at)
} }
//律师汇总表 律师基本信息统计 //律师汇总表 律师基本信息统计
/**
* 获取律师汇总信息
* @param array $params 查询参数
* @param array $received_at 收款日期范围
* @param array $invoiced_at 开票日期范围
* @return array 返回包含各项统计数据的数组
*/
public static function getTotalInfo($params, $received_at, $invoiced_at) public static function getTotalInfo($params, $received_at, $invoiced_at)
{ {
// 获取筛选条件 // 获取筛选条件
$lnum = $params['number'] ?? ''; $lnum = $params['number'] ?? ''; // 律师编号
$lname = $params['name'] ?? ''; $lname = $params['name'] ?? ''; // 律师姓名
$year = $params['year'] ?? date('Y'); $year = $params['year'] ?? date('Y'); // 年份,默认为当前年
DB::enableQueryLog(); DB::enableQueryLog(); // 开启SQL日志记录
$list = []; $list = [];
// 构建基础查询:关联律师表和合同收款表
$note_monitor = DB::table('lawyer as l') $note_monitor = DB::table('lawyer as l')
->select(DB::raw('rp.lawyer_id,rp.year,l.number as lnumber,l.name as lname,l.commission_rate,sum(invoiced_money) as invoiced_money,sum(received_amount) as received_amount')) ->select(DB::raw('rp.lawyer_id,rp.year,l.number as lnumber,l.name as lname,l.commission_rate,sum(invoiced_money) as invoiced_money,sum(received_amount) as received_amount'))
//->select(['rp.*', 'l.name as lname', 'l.number as lnumber', 'l.commission_rate'])
->leftJoin('covenant_receive_payment AS rp', 'rp.lawyer_id', '=', 'l.id') ->leftJoin('covenant_receive_payment AS rp', 'rp.lawyer_id', '=', 'l.id')
->whereNull('rp.deleted_at'); ->whereNull('rp.deleted_at'); // 只查询未删除的记录
// 打印SQL日志
// 添加筛选条件
if ($lnum) { if ($lnum) {
$note_monitor = $note_monitor->where("l.number", $lnum); $note_monitor = $note_monitor->where("l.number", $lnum); // 按律师编号筛选
} }
if ($lname) { if ($lname) {
$note_monitor = $note_monitor->where("l.name", $lname); $note_monitor = $note_monitor->where("l.name", $lname); // 按律师姓名筛选
} }
if ($year) { if ($year) {
$note_monitor = $note_monitor->where("rp.year", $year); $note_monitor = $note_monitor->where("rp.year", $year); // 按年份筛选
} }
// 处理日期范围筛选条件
// 同时有开票日期和收款日期筛选
if ($invoiced_at['start'] && $received_at['start']) { if ($invoiced_at['start'] && $received_at['start']) {
$in_start = $invoiced_at['start'] ?? ''; $in_start = $invoiced_at['start'] ?? '';
$in_end = $invoiced_at['end'] ?? ''; $in_end = $invoiced_at['end'] ?? '';
...@@ -128,9 +145,11 @@ public static function getTotalInfo($params, $received_at, $invoiced_at) ...@@ -128,9 +145,11 @@ public static function getTotalInfo($params, $received_at, $invoiced_at)
if ($in_start && $re_start) { if ($in_start && $re_start) {
if ($in_end && $re_end) { if ($in_end && $re_end) {
// 完整的日期范围筛选
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$re_start, $re_end]) $note_monitor = $note_monitor->whereBetween('rp.received_at', [$re_start, $re_end])
->whereBetween('rp.invoiced_at', [$in_start, $in_end]); ->whereBetween('rp.invoiced_at', [$in_start, $in_end]);
} else { } else {
// 部分日期范围筛选
if ($in_end) { if ($in_end) {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $re_start) $note_monitor = $note_monitor->whereDate('rp.received_at', '>', $re_start)
->whereBetween('rp.invoiced_at', [$in_start, $in_end]); ->whereBetween('rp.invoiced_at', [$in_start, $in_end]);
...@@ -143,7 +162,9 @@ public static function getTotalInfo($params, $received_at, $invoiced_at) ...@@ -143,7 +162,9 @@ public static function getTotalInfo($params, $received_at, $invoiced_at)
} }
} }
} }
} elseif ($invoiced_at['start'] && !$received_at['start']) { //开票日期 }
// 只有开票日期筛选
elseif ($invoiced_at['start'] && !$received_at['start']) {
$start = $invoiced_at['start'] ?? ''; $start = $invoiced_at['start'] ?? '';
$end = $invoiced_at['end'] ?? ''; $end = $invoiced_at['end'] ?? '';
...@@ -154,8 +175,9 @@ public static function getTotalInfo($params, $received_at, $invoiced_at) ...@@ -154,8 +175,9 @@ public static function getTotalInfo($params, $received_at, $invoiced_at)
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $start); $note_monitor = $note_monitor->whereDate('rp.received_at', '>', $start);
} }
} }
// }
} elseif ($received_at['start'] && !$invoiced_at['start']) { //收款日期 // 只有收款日期筛选
elseif ($received_at['start'] && !$invoiced_at['start']) {
$start = $received_at['start'] ?? ''; $start = $received_at['start'] ?? '';
$end = $received_at['end'] ?? ''; $end = $received_at['end'] ?? '';
...@@ -168,37 +190,54 @@ public static function getTotalInfo($params, $received_at, $invoiced_at) ...@@ -168,37 +190,54 @@ public static function getTotalInfo($params, $received_at, $invoiced_at)
} }
} }
// 获取列表 // 初始化返回结果数组
$total = [ $total = [
'receipt_money' => 0, 'receipt_money' => 0, // 已收款总额
'invoiced_money' => 0, 'invoiced_money' => 0, // 已开票总额
'commission_amount' => 0, 'commission_amount' => 0, // 提成金额总额
'cost' => 0, 'cost' => 0, // 成本合计
'salary' => 0, 'salary' => 0, // 基本工资总额
'special_additional' => 0, 'special_additional' => 0, // 专项附加总额
'social' => 0, 'social' => 0, // 社保总额
'accumulation_fund' => 0, 'accumulation_fund' => 0, // 公积金总额
'advance_fee' => 0, 'advance_fee' => 0, // 预支款总额
'tickets_money' => 0, 'tickets_money' => 0, // 贴票金额总额
'payable_commission' => 0, 'payable_commission' => 0, // 可结算提成总额
]; ];
// 执行查询并获取结果
$list = $note_monitor->limit(1000)->groupBy('rp.lawyer_id')->get()->toArray(); $list = $note_monitor->limit(1000)->groupBy('rp.lawyer_id')->get()->toArray();
//$queries = DB::getQueryLog();
$sql = $note_monitor->toSql();
$bindings = $note_monitor->getBindings();
Log::add('debug', 'SQL查询: '.$sql.' 参数: '.json_encode($bindings));
// 处理查询结果
foreach ($list as $item) { foreach ($list as $item) {
// 获取律师信息
$lawyerObj = Lawyer::find($item->lawyer_id); $lawyerObj = Lawyer::find($item->lawyer_id);
//已收款 //Log::add('debug', $item); // 记录调试日志
$receipt_money = $item->received_amount;
$total['receipt_money'] += $receipt_money; // 检查律师是否存在
//已开票 if (!$lawyerObj) {
$invoiced_money = $item->invoiced_money; Log::add('error', '未找到律师ID: ' . $item->lawyer_id);
continue;
}
// 计算各项金额并累加
$receipt_money = $item->received_amount; // 已收款金额
$total['receipt_money'] = round($total['receipt_money'] + $receipt_money, 2);
$invoiced_money = $item->invoiced_money; // 已开票金额
$total['invoiced_money'] += $invoiced_money; $total['invoiced_money'] += $invoiced_money;
//提成金额
$commission_amount = self::getCommissionAmount($receipt_money, $lawyerObj->commission_rate); $commission_amount = self::getCommissionAmount($receipt_money, $lawyerObj->commission_rate); // 提成金额
$total['commission_amount'] += $commission_amount; $total['commission_amount'] += $commission_amount;
//成本合计
$cost = LawyerCost::getTotalCost($item->lawyer_id, $year); $cost = LawyerCost::getTotalCost($item->lawyer_id, $year); // 总成本
$total['cost'] += $cost; $total['cost'] += $cost;
//基本工资 //基本工资
$salary = LawyerCost::getBasiSalary($item->lawyer_id, $year); $salary = LawyerCost::getBasiSalary($item->lawyer_id, $year);
$total['salary'] += $salary; $total['salary'] += $salary;
...@@ -215,13 +254,15 @@ public static function getTotalInfo($params, $received_at, $invoiced_at) ...@@ -215,13 +254,15 @@ public static function getTotalInfo($params, $received_at, $invoiced_at)
$advance_fee = LawyerCost::getAdvanceFee($item->lawyer_id, $year); $advance_fee = LawyerCost::getAdvanceFee($item->lawyer_id, $year);
$total['advance_fee'] += $advance_fee; $total['advance_fee'] += $advance_fee;
//贴票金额 //贴票金额
$lawyerObj = Lawyer::find($item->lawyer_id); $lawyerObj = Lawyer::find($item->lawyer_id); // 第2次
// 应该缓存查询结果
$tickets_money = LawyerCost::getPostingTicketsMoney($item->lawyer_id, $year, $lawyerObj->commission_rate, $lawyerObj->ticket_ratio); $tickets_money = LawyerCost::getPostingTicketsMoney($item->lawyer_id, $year, $lawyerObj->commission_rate, $lawyerObj->ticket_ratio);
$total['tickets_money'] += $tickets_money; $total['tickets_money'] += $tickets_money;
//可结算提成 //可结算提成
$payable_commission = self::getPayableAmount($item->lawyer_id, $year, $lawyerObj->commission_rate, $receipt_money); $payable_commission = self::getPayableAmount($item->lawyer_id, $year, $lawyerObj->commission_rate, $receipt_money);
$total['payable_commission'] += $payable_commission; $total['payable_commission'] += $payable_commission;
} }
return $total; return $total;
} }
...@@ -245,7 +286,7 @@ public static function getIdByName($title = '') ...@@ -245,7 +286,7 @@ public static function getIdByName($title = '')
$row = self::where("name", $title)->first(); $row = self::where("name", $title)->first();
if ($row) { if ($row) {
$id = $row->id; $id = $row->id;
}else{ } else {
$id = self::insertGetId([ $id = self::insertGetId([
'name' => $title, 'name' => $title,
'wtype' => 2, 'wtype' => 2,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment