2023-09-15 17:00:52 +08:00
< ? php
namespace app\adminapi\controller\approve ;
use app\adminapi\controller\BaseAdminController ;
use app\adminapi\lists\approve\ApproveLists ;
2023-11-16 10:36:32 +08:00
use app\common\enum\user\AccountLogEnum ;
use app\common\logic\AccountLogLogic ;
2023-10-18 15:49:14 +08:00
use app\common\logic\task\TaskLogic ;
2023-09-15 19:04:45 +08:00
use app\common\model\Approve ;
2023-11-16 10:36:32 +08:00
use app\common\model\Company ;
use app\common\model\company\CompanyAccountLog ;
2023-09-15 19:04:45 +08:00
use app\common\model\task\Task ;
2023-09-16 14:43:54 +08:00
use app\common\model\task_scheduling_plan\TaskSchedulingPlan ;
2023-10-26 13:45:27 +08:00
use app\common\model\task_template\TaskTemplate ;
2023-11-16 10:36:32 +08:00
use app\common\model\user\User ;
use app\common\model\user\UserAccountLog ;
2023-09-15 19:04:45 +08:00
use think\facade\Db ;
2023-09-15 17:00:52 +08:00
class ApproveController extends BaseAdminController
{
public function lists ()
{
return $this -> dataLists ( new ApproveLists ());
}
2023-09-15 19:04:45 +08:00
2023-10-26 13:45:27 +08:00
public function lists2 ()
{
return $this -> success ( '成功' ,( new ApproveLists ()) -> lists2 ());
}
2023-11-16 10:36:32 +08:00
public function lists3 ()
{
return $this -> success ( '成功' ,( new ApproveLists ()) -> lists3 ());
}
2023-09-15 19:04:45 +08:00
public function audit ()
{
try {
$params = $this -> request -> param (); // id check_status remark
$approve = Approve :: find ( $params [ 'id' ]);
if ( ! $approve ) {
$this -> fail ( '数据不存在' );
}
2023-10-26 13:45:27 +08:00
2023-09-15 19:04:45 +08:00
Db :: startTrans ();
2023-09-16 14:43:54 +08:00
// 拒绝通过 要让用户今天可以继续做任务
2023-09-15 19:04:45 +08:00
if ( $params [ 'check_status' ] == 3 ) {
2023-09-16 14:43:54 +08:00
$this -> refuse ( $params , $approve );
2023-09-15 19:04:45 +08:00
}
// 修改任务完成状态
if ( $params [ 'check_status' ] == 2 ) {
2023-10-26 13:45:27 +08:00
if ( $approve -> type == Approve :: APPROVE_TYPE_7 ) {
$taskTemplate = TaskTemplate :: where ([ 'id' => $approve -> business_id ]) -> find ();
// 提前完成
if ( $taskTemplate [ 'day_count' ] < $taskTemplate [ 'stage_day_one' ]) {
if ( bccomp ( $params [ 'amount' ], 300000 , 2 ) == - 1 ) {
$this -> fail ( '该任务提前完成条件: 销售总额必须达到30万元及以上' );
} else {
// 提前完成标识
$extend = json_decode ( $taskTemplate [ 'extend' ], true );
$extend [ 'early_finish' ] = 1 ;
$taskTemplate -> extend = json_encode ( $extend );
$taskTemplate -> save ();
$this -> pass ( $approve , $params );
}
} else {
$this -> pass ( $approve , $params );
}
} else {
$this -> pass ( $approve );
}
2023-09-15 19:04:45 +08:00
}
Db :: commit ();
return $this -> success ( '审核成功' );
} catch ( \Exception $e ) {
Db :: rollback ();
2023-10-20 10:46:50 +08:00
return $this -> fail ( $e -> getFile () . $e -> getLine () . $e -> getMessage ());
2023-09-15 19:04:45 +08:00
}
2023-09-16 14:43:54 +08:00
}
// 通过
2023-10-26 13:45:27 +08:00
private function pass ( $approve , $params = [])
2023-09-16 14:43:54 +08:00
{
2023-10-18 15:49:14 +08:00
Db :: startTrans ();
$approve -> check_status = 2 ;
$approve -> update_time = time ();
$approve -> save ();
// 任务
2023-09-16 14:43:54 +08:00
$task = Task :: find ( $approve [ 'task_id' ]);
if ( $task [ 'status' ] == 2 ) {
$task -> status = 3 ;
$task -> save ();
}
2023-10-18 15:49:14 +08:00
Db :: commit ();
2023-10-23 17:31:01 +08:00
// 镇农科公司任务-数字农贸宣传业务、加工业务的建设和招商工作任务 结算
2023-10-18 15:49:14 +08:00
if ( $approve -> type == Approve :: APPROVE_TYPE_4 ) {
2023-10-20 10:46:50 +08:00
$taskSchedulePlan = TaskSchedulingPlan :: where ( 'la_task_scheduling_plan.id' , $task [ 'scheduling_plan_id' ])
2023-10-18 15:49:14 +08:00
-> where ( 'is_pay' , 0 )
-> with ([ 'template_info' ])
2023-10-20 10:46:50 +08:00
-> withJoin ([ 'scheduling' ], 'left' )
-> where ( 'scheduling.company_type' , 41 )
-> find ()
-> toArray ();
2023-10-18 15:49:14 +08:00
TaskLogic :: dealTaskMarketingDirector10 ( $taskSchedulePlan , $approve );
}
2023-10-23 17:31:01 +08:00
if ( $approve -> type == Approve :: APPROVE_TYPE_5 ) {
$taskSchedulePlan = TaskSchedulingPlan :: where ( 'la_task_scheduling_plan.id' , $task [ 'scheduling_plan_id' ])
-> where ( 'is_pay' , 0 )
-> with ([ 'template_info' ])
-> withJoin ([ 'scheduling' ], 'left' )
-> where ( 'scheduling.company_type' , 17 )
-> find ()
-> toArray ();
2023-10-26 13:45:27 +08:00
TaskLogic :: dealVillageTask6 ( $taskSchedulePlan );
2023-10-23 17:31:01 +08:00
}
2023-10-24 10:04:32 +08:00
if ( $approve -> type == Approve :: APPROVE_TYPE_6 ) {
$taskSchedulePlan = TaskSchedulingPlan :: where ( 'la_task_scheduling_plan.id' , $task [ 'scheduling_plan_id' ])
-> where ( 'is_pay' , 0 )
-> with ([ 'template_info' ])
-> withJoin ([ 'scheduling' ], 'left' )
-> where ( 'scheduling.company_type' , 17 )
-> find ()
-> toArray ();
TaskLogic :: dealVillageTask8 ( $taskSchedulePlan );
}
2023-10-26 13:45:27 +08:00
if ( $approve -> type == Approve :: APPROVE_TYPE_7 ) {
// 需要手动输入销售总额
$approve -> amount = $params [ 'amount' ];
$approve -> save ();
}
2023-11-06 15:58:46 +08:00
if ( $approve -> type == Approve :: APPROVE_TYPE_8 ) {
// 需要手动输入申请的政策补贴金额
$approve -> amount = $params [ 'amount' ];
$approve -> save ();
}
if ( $approve -> type == Approve :: APPROVE_TYPE_9 ) {
$taskSchedulePlan = TaskSchedulingPlan :: where ( 'la_task_scheduling_plan.id' , $task [ 'scheduling_plan_id' ])
-> where ( 'is_pay' , 0 )
-> with ([ 'template_info' ])
-> withJoin ([ 'scheduling' ], 'left' )
-> where ( 'scheduling.company_type' , 17 )
-> find ()
-> toArray ();
TaskLogic :: masterTask8Settlement ( $taskSchedulePlan );
}
2023-09-16 14:43:54 +08:00
}
2023-09-15 19:04:45 +08:00
2023-09-16 14:43:54 +08:00
// 拒绝
private function refuse ( $params , $approve )
{
$approve -> check_status = $params [ 'check_status' ];
$approve -> remark = $params [ 'remark' ];
$approve -> update_time = time ();
$approve -> save ();
2023-09-15 19:04:45 +08:00
2023-09-16 14:43:54 +08:00
// 更新schedule_plan时间和task的时间为今天依旧可提交
$schedulePlan = TaskSchedulingPlan :: find ([ 'tast_id' => $approve [ 'task_id' ]]);
if ( empty ( $schedule_plan )) {
return $this -> fail ( '数据异常,任务计划不存在' );
}
$time = strtotime ( date ( 'Y-m-d' ));
TaskSchedulingPlan :: where ([ 'id' => $schedulePlan [ 'id' ]]) -> update ([
'start_time' => $time ,
'end_time' => $time + 86399
]);
Task :: where ( 'id' , $approve [ 'task_id' ]) -> update ([
'start_time' => $time ,
'end_time' => $time + 86399
]);
2023-09-15 19:04:45 +08:00
}
2023-09-16 14:43:54 +08:00
2023-11-16 10:36:32 +08:00
/**
* 股金任务审批
*/
public function shareholderMoneyTaskAudit ()
{
// try {
$params = $this -> request -> param (); // id check_status remark
$approve = Approve :: find ( $params [ 'id' ]);
if ( ! empty ( $approve )) {
$this -> fail ( '数据不存在' );
}
// 拒绝通过 要让用户今天可以继续做任务
if ( $params [ 'check_status' ] == 3 ) {
$this -> refuse1 ( $params , $approve );
}
if ( $params [ 'check_status' ] == 2 ) {
$this -> pass1 ( $approve , $params );
}
// } catch (\Exception $e) {
// return $this->fail($e->getMessage());
// }
}
private function refuse1 ( $params , $approve )
{
$approve -> check_status = $params [ 'check_status' ];
$approve -> remark = $params [ 'remark' ];
$approve -> update_time = time ();
$approve -> save ();
}
private function pass1 ( $approve , $params )
{
Db :: startTrans ();
// 任务
$task = Task :: find ( $approve [ 'task_id' ]);
if ( $task [ 'status' ] == 2 ) {
$task -> status = 3 ;
$task -> save ();
}
2023-11-17 10:52:12 +08:00
// 小组服务公司股金上交 小组服务团队-入股任务 村联络员-督促小组服务团队入股任务 镇农科服务部长-督促小组服务团队入股村管理公司任务
if ( $approve -> type == Approve :: APPROVE_TYPE_10 || $approve -> type == Approve :: APPROVE_TYPE_11 || $approve -> type == Approve :: APPROVE_TYPE_13 ) {
// 小组服务公司
2023-11-16 10:36:32 +08:00
$subordinateCompany = Company :: where ([ 'id' => $approve -> department_id ]) -> find ();
2023-11-17 10:52:12 +08:00
// 村公司
2023-11-16 10:36:32 +08:00
$parentCompany = Company :: where ([ 'village' => $subordinateCompany [ 'village' ], 'company_type' => 17 ]) -> find ();
}
2023-11-17 10:52:12 +08:00
// 村管理公司股金上交 村联络员任务-入股甲方公司
2023-11-17 12:03:25 +08:00
if ( $approve -> type == Approve :: APPROVE_TYPE_12 || $approve -> type == Approve :: APPROVE_TYPE_14 ) {
2023-11-17 10:52:12 +08:00
// 村公司
$subordinateCompany = Company :: where ([ 'id' => $approve -> department_id ]) -> find ();
// 镇农科公司 负责区域包含有村公司的street码
2023-11-17 19:14:10 +08:00
$parentCompany = Db :: query ( " select * from la_company where company_type=:company_type and FIND_IN_SET(:street,responsible_area) " , [ 'company_type' => 41 , 'street' => $subordinateCompany [ 'street' ]], true )[ 0 ];
2023-11-17 10:52:12 +08:00
}
2023-11-16 10:36:32 +08:00
$amount = $approve -> amount ; // 上交股金金额
$subordinateCompany -> shareholder_money = $amount ;
$subordinateCompany -> save ();
// 添加股金入股记录
$sharecapitalChangeLogInsertId = $this -> addConpanyAccountLog ( $subordinateCompany , $parentCompany , $amount );
$approve -> check_status = 2 ;
$approve -> update_time = time ();
$approve -> business_id = $sharecapitalChangeLogInsertId ; // 灵活运用业务主键id
$approve -> remark = $params [ 'remark' ];
$approve -> save ();
2023-11-17 10:52:12 +08:00
// 小组服务团队-入股任务结算
2023-11-16 10:36:32 +08:00
if ( $approve -> type == Approve :: APPROVE_TYPE_10 ) {
$taskSchedulingPaln = TaskSchedulingPlan :: where ([ 'task_id' => $task -> id ])
-> withJoin ([ 'scheduling' ], 'left' )
-> where ( 'scheduling.company_type' , 18 )
-> where ( 'is_pay' , 0 )
-> with ([ 'template_info' ])
-> select ()
-> toArray ()[ 0 ];
$taskInfo = $task -> toArray ();
$this -> taskSettlement ( $taskInfo , $subordinateCompany , $taskSchedulingPaln );
}
2023-11-17 10:53:26 +08:00
Db :: commit ();
2023-11-17 10:52:12 +08:00
// 村联络员-督促小组服务团队入股
if ( $approve -> type == Approve :: APPROVE_TYPE_11 ) {
$taskSchedulingPaln = TaskSchedulingPlan :: where ([ 'task_id' => $task -> id ])
-> withJoin ([ 'scheduling' ], 'left' )
-> where ( 'scheduling.company_type' , 17 )
-> where ( 'is_pay' , 0 )
-> with ([ 'template_info' ])
-> select ()
-> toArray ()[ 0 ];
TaskLogic :: dealVillageTask4 ( $taskSchedulingPaln , $approve );
}
// 村联络员-入股甲方公司
if ( $approve -> type == Approve :: APPROVE_TYPE_12 ) {
$taskSchedulingPaln = TaskSchedulingPlan :: where ([ 'task_id' => $task -> id ])
-> withJoin ([ 'scheduling' ], 'left' )
-> where ( 'scheduling.company_type' , 17 )
-> where ( 'is_pay' , 0 )
-> with ([ 'template_info' ])
-> select ()
-> toArray ()[ 0 ];
TaskLogic :: dealVillageTask5 ( $taskSchedulingPaln , $approve );
}
// 镇农科服务部长-督促小组服务团队入股村管理公司任务
if ( $approve -> type == Approve :: APPROVE_TYPE_13 ) {
$taskSchedulingPaln = TaskSchedulingPlan :: where ([ 'task_id' => $task -> id ])
-> withJoin ([ 'scheduling' ], 'left' )
-> where ( 'scheduling.company_type' , 41 )
-> where ( 'is_pay' , 0 )
-> with ([ 'template_info' ])
-> select ()
-> toArray ()[ 0 ];
TaskLogic :: dealTownTask6 ( $taskSchedulingPaln , $approve );
}
2023-11-17 12:03:25 +08:00
// 镇农科负责人-促成村联络员入股甲方
if ( $approve -> type == Approve :: APPROVE_TYPE_14 ) {
$taskSchedulingPaln = TaskSchedulingPlan :: where ([ 'task_id' => $task -> id ])
-> withJoin ([ 'scheduling' ], 'left' )
-> where ( 'scheduling.company_type' , 41 )
-> where ( 'is_pay' , 0 )
-> with ([ 'template_info' ])
-> select ()
-> toArray ()[ 0 ];
TaskLogic :: masterTask6Settlement ( $taskSchedulingPaln , $approve );
}
2023-11-16 10:36:32 +08:00
return $this -> success ( '成功' );
}
private function addConpanyAccountLog ( $subordinateCompany , $parentCompany , $amount )
{
$sharecapitalChangeLogData = [
'subordinate_company_id' => $subordinateCompany [ 'id' ],
'parent_company_id' => $parentCompany [ 'id' ],
'amount' => $amount ,
'create_time' => time (),
'update_time' => time (),
];
$sharecapitalChangeLogInsertId = Db :: name ( 'company_sharecapital_change_log' ) -> insertGetId ( $sharecapitalChangeLogData );
// 上交股金公司股金变更 + 公司股金减少记录
$leftAmount = bcsub ( $subordinateCompany [ 'shareholder_money' ], $amount , 2 );
Company :: where ( 'id' , $subordinateCompany [ 'company_id' ]) -> save ([ 'shareholder_money' => $leftAmount ]);
$company_log1 = [
'sn' => generate_sn ( UserAccountLog :: class , 'sn' , 20 ),
'company_id' => $subordinateCompany [ 'id' ],
'change_object' => CompanyAccountLog :: SHAREHOLDER , // 变动对象 1余额 2股金
'change_type' => CompanyAccountLog :: SHAREHOLDER_DEC_DEPOSIT , //变动类型
'action' => CompanyAccountLog :: DEC , //1-增加 2-减少
'left_amount' => $leftAmount , //变动后数量
'change_amount' => $amount , //变动数量
'extend' => json_encode ([ 'company_sharecapital_change_log_id' => $sharecapitalChangeLogInsertId ]),
'status' => 1 ,
];
CompanyAccountLog :: create ( $company_log1 );
// 接收股金公司股金变更 + 公司股金增加记录
$addAmount = bcadd ( $parentCompany [ 'shareholder_money' ], $amount , 2 );
Company :: where ( 'id' , $parentCompany [ 'id' ]) -> save ([ 'shareholder_money' => $addAmount ]);
// 公司账户变动记录
$company_log2 = [
'sn' => generate_sn ( UserAccountLog :: class , 'sn' , 20 ),
'company_id' => $parentCompany [ 'id' ],
'change_object' => CompanyAccountLog :: SHAREHOLDER , // 变动对象 1余额 2股金
'change_type' => CompanyAccountLog :: TASK_INC_SHAREHOLDER_MONEY , //变动类型
'action' => CompanyAccountLog :: INC , //1-增加 2-减少
'left_amount' => $addAmount , //变动后数量
'change_amount' => $amount , //变动数量
'extend' => json_encode ([ 'company_sharecapital_change_log_id' => $sharecapitalChangeLogInsertId ]),
'status' => 1 ,
];
CompanyAccountLog :: create ( $company_log2 );
return $sharecapitalChangeLogInsertId ;
}
public function taskSettlement ( $data , $company , $datas = [])
{
$proportion = 0 ;
$remark = '来自任务【' . $datas [ 'template_info' ][ 'title' ] . '】,' ;
//总金额除以2等于不可提现账号金额和收益
$master_money = bcdiv ( $data [ 'money' ], 2 , 2 );
//收益的百分之25为负责人的收益其余为成员的平分收益
$master_money_user = bcdiv ( $master_money , 2 , 2 );
//成员数量
$userAll = User :: where ( 'company_id' , $data [ 'company_id' ]) -> where ( 'admin_id' , 0 ) -> field ( 'id,user_money' ) -> select ();
$yser_all_count = count ( $userAll );
$member_money_user = bcdiv ( $master_money_user , $yser_all_count , 2 );
//负责人
$arr = [ $company [ 'user_id' ], AccountLogEnum :: UM_INC_TASK , AccountLogEnum :: INC , $master_money_user , $datas [ 'sn' ], $remark . '获得收益' . $master_money_user . '元' , [ 'company_id' => $data [ 'company_id' ], 'proportion' => $proportion ], $data [ 'status' ]];
$this -> master ( $arr );
$arr_two = [ $company [ 'user_id' ], AccountLogEnum :: UM_INC_TASKUSER , AccountLogEnum :: INC , $master_money_user , $datas [ 'sn' ], $remark . '获得账户余额' . $master_money_user . '元' , [ 'company_id' => $data [ 'company_id' ], 'proportion' => $proportion ], $data [ 'status' ]];
$this -> Account ( $arr_two );
//成员
foreach ( $userAll as $value ) {
$arr = [ $value [ 'id' ], AccountLogEnum :: UM_INC_TASK , AccountLogEnum :: INC , $member_money_user , $datas [ 'sn' ], $remark . '获得收益' . $member_money_user . '元' , [ 'company_id' => $data [ 'company_id' ], 'proportion' => $proportion ], $data [ 'status' ]];
$this -> member ( $arr );
$arr_two = [ $value [ 'id' ], AccountLogEnum :: UM_INC_TASKUSER , AccountLogEnum :: INC , $member_money_user , $datas [ 'sn' ], $remark . '获得账户余额' . $member_money_user . '元' , [ 'company_id' => $data [ 'company_id' ], 'proportion' => $proportion ], $data [ 'status' ]];
$this -> Account ( $arr_two );
}
//公司
$deposit_count = bcadd ( $company [ 'deposit' ], $master_money , 2 );
$this -> AccountLog ( $data [ 'company_id' ], $deposit_count , $master_money );
$company_money_count = bcadd ( $company [ 'company_money' ], $master_money , 2 );
$this -> AccountLog ( $data [ 'company_id' ], $company_money_count , $master_money );
Company :: where ( 'id' , $data [ 'company_id' ]) -> update ([ 'deposit' => Db :: raw ( 'deposit+' . $master_money ), 'company_money' => Db :: raw ( 'company_money+' . $master_money )]);
}
public function AccountLog ( $companyId , $left_amount , $changeAmount , $change_object = 1 , $change_type = 1 , $action = 1 )
{
$company_log = [
'sn' => generate_sn ( UserAccountLog :: class , 'sn' , 20 ),
'company_id' => $companyId ,
'change_object' => $change_object , //变动对象
'change_type' => $change_type , //变动类型
'action' => $action , //1-增加 2-减少
'left_amount' => $left_amount , //变动后数量
'change_amount' => $changeAmount , //变动数量
'status' => 1 ,
];
CompanyAccountLog :: create ( $company_log );
}
/** 负责人的分润
* @ param $data
*/
private function master ( $data )
{
User :: where ( 'id' , $data [ 0 ]) -> update ([ 'deposit' => Db :: raw ( 'deposit+' . $data [ 3 ]), 'user_money' => Db :: raw ( 'user_money+' . $data [ 3 ])]);
return AccountLogLogic :: add ( $data [ 0 ], $data [ 1 ], $data [ 2 ], $data [ 3 ], $data [ 4 ], $data [ 5 ], $data [ 6 ], $data [ 7 ]);
}
/** 成员分润
* @ param $data
*/
private function member ( $data )
{
User :: where ( 'id' , $data [ 0 ]) -> update ([ 'deposit' => Db :: raw ( 'deposit+' . $data [ 3 ]), 'user_money' => Db :: raw ( 'user_money+' . $data [ 3 ])]);
return AccountLogLogic :: add ( $data [ 0 ], $data [ 1 ], $data [ 2 ], $data [ 3 ], $data [ 4 ], $data [ 5 ], $data [ 6 ], $data [ 7 ]);
}
private function Account ( $data )
{
return AccountLogLogic :: add ( $data [ 0 ], $data [ 1 ], $data [ 2 ], $data [ 3 ], $data [ 4 ], $data [ 5 ], $data [ 6 ], $data [ 7 ]);
}
2023-09-15 17:00:52 +08:00
}