Commit 2540ec48 by liuyingkang

fix(律师提成计算): 修复预留结案费计算逻辑并优化相关功能

修复预留结案费计算时未过滤合同类型的问题,优化已贴票金额显示和计算逻辑
调整律师成本计算规则,增加预支款和预留结案费处理
修复导出功能中输出缓冲区处理问题
parent 445032b5
......@@ -59,18 +59,33 @@ protected function grid()
$grid->column('sign_at');
$grid->column('principal', '委托人');
$grid->column('lawyer.name', '办案律师');
$grid->column('amount')->display(function ($val) {
return number_format($val, 2);
});
$grid->column('payment_method')->display(function ($val) {
return $val == 1 ? '开票付款' : '付款开票';
});
$grid->column('reserve_fee', '预留结案费')->display(function () {
$lawyerObj = ModelLawyer::find($this->lawyer_id);
$commission_rate = $lawyerObj->commission_rate;
$result = $this->amount * $commission_rate / 100 * 0.05;
return number_format($result, 2);
});
//行政2角色无法看到amount、reserve_fee
if (Admin::user()->roles->contains('id', 4)) {
$grid->disableColumnSelector();
$grid->disableActions();
} else {
$grid->column('amount')->display(function ($val) {
return number_format($val, 2);
});
$grid->column('reserve_fee', '预留结案费')->display(function () {
// 只有特定合同类型才计算预留结案费
if (!in_array($this->ctype, [1, 2, 3, 6, 8])) {
return number_format(0, 2);
}
$lawyerObj = ModelLawyer::find($this->lawyer_id);
$commission_rate = $lawyerObj->commission_rate;
$result = $this->amount * $commission_rate / 100 * 0.05;
return number_format($result, 2);
});
}
$grid->column('is_closed', '结案状态')->display(function ($val) {
return $val ? '已结案' : '未结案';
});
......@@ -188,7 +203,7 @@ protected function detail($id)
// $form->select('lawyer_id')->options($lawyers)->required();
// $form->text('amount')->required();
// $form->select('payment_method')->options(Covenant::PAYMENT_METHOD)->default(1)->required();
// $form->text('avoid', '回避信息' );
// $form->text('remark', '备注');
......@@ -227,15 +242,15 @@ public function destroy($id)
try {
// 软删除关联的收款、开票记录
ModelCovenantReceivePayment::where('cid', $id)->update(['deleted_at' => now()]);
// 删除合同
$res = $this->form()->destroy($id);
DB::commit();
return $res;
} catch (\Exception $e) {
DB::rollBack();
Log::Add('debug', '删除合同失败:'.$e->getMessage());
Log::Add('debug', '删除合同失败:' . $e->getMessage());
return false;
}
}
......@@ -267,8 +282,8 @@ protected function form()
$form->select('lawyer_id')->options($lawyers)->required();
$form->text('amount')->required();
$form->select('payment_method')->options(Covenant::PAYMENT_METHOD)->default(1)->required();
$form->text('avoid', '回避信息' );
$form->text('avoid', '回避信息');
$form->text('remark', '备注');
$form->disableCreatingCheck();
......@@ -302,7 +317,7 @@ function (Form $form, $result) {
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
Log::Add('debug', '更新合同关联记录失败:'.$e->getMessage());
Log::Add('debug', '更新合同关联记录失败:' . $e->getMessage());
}
}
);
......
......@@ -85,7 +85,10 @@ public function lawyerCommissionExport(Request $request)
$param = json_decode($request->get('param'));
ob_end_clean();
// 检查是否有活动的输出缓冲区
if (ob_get_level() > 0) {
ob_end_clean();
}
return Excel::download(new lawyerCommissionExportExten($param), $filename . '.xlsx');
}
}
......@@ -41,7 +41,7 @@ protected function grid()
return number_format($tickets_money, 2);
});
$grid->column('commission_retention', '预留结案费')->display(function () use ($year) {
$received_money = ModelCovenantReceivePayment::getReceivedMoney($this->id, $year); //创收已收款
$received_money = ModelCovenantReceivePayment::getReceivedMoneyByPayableAmount($this->id, $year); //创收已收款
$commission_rate = $this->commission_rate; //提成比例
$result = $received_money * ($commission_rate / 100) * 0.05;
return number_format($result, 2);
......
......@@ -273,7 +273,7 @@ protected function form()
$form->text('annuity');
$form->text('assistant_fee');
$form->text('office_rental_fee');
$form->text('posting_tickets_fee_theory', '已贴票金额');
$form->text('posting_tickets_fee_the', '已贴票金额');
$form->text('noticket_cost')->readOnly();
$form->text('advance_fee');
$form->text('personal_income_tax')->required();
......
......@@ -186,6 +186,13 @@ public function getList($param)
$reserved_closing_fee = $receiveMoeny * ($commission_rate / 100) * 0.05;
$tmp[$i] = sprintf('%.2f', $reserved_closing_fee);
}
//已贴票金额
if ($val['field'] == 'posting_tickets_fee_the') {
$posting_tickets_fee_the = ModelLawyerCost::where($condition)->sum('posting_tickets_fee_the');
$tmp[$i] = $posting_tickets_fee_the;
}
//贴票金额
if ($val['field'] == 'posting_tickets_money') {
$tickets_money = ModelLawyerCost::getPostingTicketsMoney($lawyer_id, $year, $commission_rate, $ticket_ratio, $i);
......@@ -200,6 +207,12 @@ public function getList($param)
$royalty_amount = $receiveMoeny * ($commission_rate / 100);
$paid_amount = ModelLawyerCost::getPaidAmount($lawyer_id, $year, $i);
$payable_money = $royalty_amount - $paid_amount;
//还需要减去10预留结案费
$reserved_closing_fee = $receiveMoeny * ($commission_rate / 100) * 0.05;
$payable_money -= $reserved_closing_fee;
$tmp[$i] = $payable_money;
}
//个人所得税
......
......@@ -151,9 +151,18 @@ public function get(Grid\Model $model)
}
//预留结案费
if ($val['field'] == 'reserved_closing_fee') {
$reserved_closing_fee = $receiveMoeny * ($commission_rate / 100) * 0.05;
$PayableAmount = ModelCovenantReceivePayment::getReceivedMoneyByPayableAmount($lawyer_id, $year, $i);
$reserved_closing_fee = $PayableAmount * ($commission_rate / 100) * 0.05;
$tmp[$i] = sprintf('%.2f', $reserved_closing_fee);
}
//已贴票金额
if ($val['field'] == 'posting_tickets_fee_the') {
$posting_tickets_fee_the = ModelLawyerCost::where($condition)->sum('posting_tickets_fee_the');
$tmp[$i] = $posting_tickets_fee_the;
}
//贴票金额
if ($val['field'] == 'posting_tickets_money') {
$tickets_money = ModelLawyerCost::getPostingTicketsMoney($lawyer_id, $year, $commission_rate, $ticket_ratio, $i);
......@@ -161,13 +170,19 @@ public function get(Grid\Model $model)
}
//提成留底
if ($val['field'] == 'commission_retention') {
$tmp[$i] = $commission_retention;
$tmp[$i] = '4万'; // 默认显示4万
}
//可支付提成结算金额
//这段代码的核心逻辑是通过提成金额减去已支付款项,计算出律师实际可获得的提成结算金额。提成金额 - 已支付款项 = 可支付提成结算金额。
if ($val['field'] == 'payable_commission_amount') {
$royalty_amount = $receiveMoeny * ($commission_rate / 100);
$paid_amount = ModelLawyerCost::getPaidAmount($lawyer_id, $year, $i);
$payable_money = $royalty_amount - $paid_amount;
// //还需要减去10预留结案费
// $reserved_closing_fee = $receiveMoeny * ($commission_rate / 100) * 0.05;
// $payable_money -= $reserved_closing_fee;
$tmp[$i] = $payable_money;
}
//个人所得税
......@@ -188,6 +203,7 @@ public function get(Grid\Model $model)
//转成字符串
foreach ($data as $kk => &$vv) {
if ($vv['title'] == '提成比例' || $vv['title'] == '提成留底') {
$vv['total'] = $vv['title'] == '提成留底' ? '4万' : $vv['total']; // 默认显示4万
continue;
}
if (isset($vv[1])) {
......
......@@ -186,6 +186,7 @@ public function get(Grid\Model $model)
//转成字符串
foreach ($data as $kk => &$vv) {
if ($vv['title'] == '提成比例' || $vv['title'] == '提成留底') {
$vv['total'] = $vv['title'] == '提成留底' ? '4万' : $vv['total'];
continue;
}
if (isset($vv[1])) {
......
......@@ -39,7 +39,7 @@ class CovenantReceivePayment extends Model
*/
public function covenant()
{
return $this->belongsTo(Covenant::class, 'covenant_id', 'id');
return $this->belongsTo(Covenant::class, 'cid', 'id');
}
/**
......@@ -103,6 +103,33 @@ public static function getReceivedMoney($lawyer_id = 0, $year = 0, $month = 0)
->sum('received_amount');
return $money;
}
//创收已收款by预留结案费【专门用于计算预留结案费】
//合同类型中民事诉讼代理、刑事诉讼辩护及代理、行政诉讼代理、仲裁业务、其它是需要计算结案费,以外的类型是不用计算的。
//具体看:Covenant::CTYPE[$val];1、2、3、6、8
public static function getReceivedMoneyByPayableAmount($lawyer_id = 0, $year = 0, $month = 0)
{
$where = ['rtype' => 1];
if ($lawyer_id) {
$where['lawyer_id'] = $lawyer_id;
}
if ($year) {
$where['year'] = $year;
}
if ($month) {
$where['month'] = $month;
}
// 修改为通过关联合同表查询指定类别
$money = self::where($where)
->whereHas('covenant', function($query) {
$query->whereIn('ctype', [1, 2, 3, 6, 8]);
})
->sum('received_amount');
return $money;
}
/**
* 创收已收款
* number 律师编号
......
......@@ -8,6 +8,10 @@
use Illuminate\Support\Facades\DB;
use App\Command\Log;
use App\Models\CovenantReceivePayment as ModelCovenantReceivePayment;
use App\Models\LawyerCost as ModelLawyerCost;
use App\Models\Lawyer as ModelLawyer;
class LawyerCost extends Model
{
use HasDateTimeFormatter;
......@@ -64,6 +68,7 @@ class LawyerCost extends Model
['field' => 'assistant_fee', 'name' => '8.助理律师成本'],
['field' => 'advance_fee', 'name' => '9.预支款'],
['field' => 'reserved_closing_fee', 'name' => '10.预留结案费'],
['field' => 'posting_tickets_fee_the', 'name' => '11.已贴票金额'],
['field' => 'posting_tickets_money', 'name' => '贴票金额'],
['field' => 'commission_retention', 'name' => '提成留底'],
['field' => 'payable_commission_amount', 'name' => '可支付提成结算金额'],
......@@ -161,6 +166,31 @@ public static function getAdvanceFee($lawyer_id = 0, $year = 0)
//已支付款项
/**
* 获取已支付款项
*
* 计算规则:
* 1. 根据律师ID、年份和月份查询相关记录
* 2. 累加各项费用:1-11
* - 基本工资
* - 专项附加
* - 社保单位部分
* - 公积金单位部分
* - 律所年检费
* - 律所年金
* - 办公室租金
* - 助理律师成本
* - 预支款
* - 预留结案费
* - 已贴票金额
* 3. 减去已贴票金额
* 4. 返回最终计算结果
*
* @param int $lawyer_id 律师ID
* @param int $year 年份
* @param int $month 月份
* @return float 已支付款项
*/
public static function getPaidAmount($lawyer_id, $year = 0, $month = 0)
{
$paid_amount = 0;
......@@ -175,20 +205,34 @@ public static function getPaidAmount($lawyer_id, $year = 0, $month = 0)
if ($list->toArray()) {
$basic_salary = $special_additional = $social_company_fee = $accumulation_fund_company_fee = 0;
$annual_inspection_fee = $annuity = $office_rental_fee = $assistant_fee = $posting_tickets_fee_the = 0;
$advance_fee = $reserved_closing_fee = 0;
foreach ($list as $item) {
$basic_salary += $item->basic_salary;
$special_additional += $item->special_additional;
$social_company_fee += $item->social_company_fee;
$accumulation_fund_company_fee += $item->accumulation_fund_company_fee;
$annual_inspection_fee += $item->annual_inspection_fee;
$annuity += $item->annuity;
$office_rental_fee += $item->office_rental_fee;
$assistant_fee += $item->assistant_fee;
$posting_tickets_fee_the += $item->posting_tickets_fee_the; //已贴票金额[要减去已贴票金额]
// 累加各项费用
$basic_salary += $item->basic_salary; // 基本工资
$special_additional += $item->special_additional; // 专项附加
$social_company_fee += $item->social_company_fee; // 社保单位部分
$accumulation_fund_company_fee += $item->accumulation_fund_company_fee; // 公积金单位部分
$annual_inspection_fee += $item->annual_inspection_fee; // 律所年检费
$annuity += $item->annuity; // 律所年金
$office_rental_fee += $item->office_rental_fee; // 办公室租金
$assistant_fee += $item->assistant_fee; // 助理律师成本
$advance_fee += $item->advance_fee; // 预支款
$posting_tickets_fee_the += $item->posting_tickets_fee_the; // 已贴票金额[要减去已贴票金额]
}
//律师比例
$Lawyer = ModelLawyer::where(['id' => $lawyer_id])->first();
//创收收款
$receiveMoeny = ModelCovenantReceivePayment::getReceivedMoney($lawyer_id, $year, $month);
//预留结案费
$reserved_closing_fee = $receiveMoeny * ($Lawyer['commission_rate'] / 100) * 0.05;
$paid_amount = $basic_salary + $special_additional + $social_company_fee
+ $accumulation_fund_company_fee + $annual_inspection_fee + $annuity
+ $office_rental_fee + $assistant_fee - $posting_tickets_fee_the;
+ $office_rental_fee + $assistant_fee + $advance_fee + $reserved_closing_fee + $posting_tickets_fee_the;
if ($lawyer_id == '5') {
Log::add('已支付款项', [
......@@ -200,7 +244,9 @@ public static function getPaidAmount($lawyer_id, $year = 0, $month = 0)
'annuity' => $annuity,
'office_rental_fee' => $office_rental_fee,
'assistant_fee' => $assistant_fee,
'posting_tickets_fee_the' => $posting_tickets_fee_the, //已贴票金额
'advance_fee' => $advance_fee,
'reserved_closing_fee' => $reserved_closing_fee,
'posting_tickets_fee_the' => $posting_tickets_fee_the,
'paid_amount' => $paid_amount, //已贴票金额
]);
}
......@@ -220,14 +266,21 @@ public static function getPostingTicketsMoney($lawyer_id, $year, $commission_rat
$paid_amount = LawyerCost::getPaidAmount($lawyer_id, $year, $month); //已支付款项
$commission = $received_money * ($commission_rate / 100); //提成
Log::add('贴票金额', [
'received_money' => $received_money,
'paid_amount' => $paid_amount,
'commission' => $commission,
'ticket_ratio' => $ticket_ratio,
'commission_rate' => $commission_rate,
'ticket' => $ticket,
]);
if ($lawyer_id == 5) {
Log::add('贴票金额', [
'received_money' => $received_money,
'paid_amount' => $paid_amount,
'commission' => $commission,
'ticket_ratio' => $ticket_ratio,
'commission_rate' => $commission_rate,
'ticket' => $ticket,
]);
}
//预支款不参与贴票金额计算,但是paid_amount包含预支款,所以在这里加上预支款
$advance_fee = self::where(['lawyer_id' => $lawyer_id, 'year' => $year, 'month' => $month])->sum('advance_fee');
$paid_amount -= $advance_fee;
if ($commission_rate == 80) {
if ($commission > 300000) {
......@@ -244,14 +297,18 @@ public static function getPostingTicketsMoney($lawyer_id, $year, $commission_rat
}
}
if ($lawyer_id == 5) {
Log::add('贴票金额2', [
'received_money' => $received_money,
'paid_amount' => $paid_amount,
'commission' => $commission,
'ticket_ratio' => $ticket_ratio,
'ticket' => $ticket,
'advance_fee' => $advance_fee,
]);
}
Log::add('贴票金额2', [
'received_money' => $received_money,
'paid_amount' => $paid_amount,
'commission' => $commission,
'ticket_ratio' => $ticket_ratio,
'ticket' => $ticket,
]);
return $ticket;
}
......@@ -284,14 +341,14 @@ public static function getAllPostingTicketsMoney($lawyer_id, $year)
$monthArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
foreach ($monthArr as $m) {
$ticket = 0;
//创收已收款
//创收已收款
$received_money = CovenantReceivePayment::getReceivedMoney($lawyer_id, $year, $m);
//已支付款项
$paid_amount = LawyerCost::getPaidAmount($lawyer_id, $year, $m);
$paid_amount = LawyerCost::getPaidAmount($lawyer_id, $year, $m);
//提成=创收已收款*提成比例
$commission = $received_money * ($commission_rate / 100);
$commission = $received_money * ($commission_rate / 100);
if ($commission_rate == 80) {
if ($commission > 300000) {
......@@ -309,7 +366,7 @@ public static function getAllPostingTicketsMoney($lawyer_id, $year)
$total += $ticket;
if ($lawyerObj->name == '周志强'&&$m<4) {
if ($lawyerObj->name == '周志强' && $m < 4) {
//我说怎么一直计算不对,加了贴票金额但是总得不变,原来是有的月份根本不计算贴票金额,没达标
Log::add('全年贴票金额' . $m, [
......
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