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 {
2023-11-18 17:33:09 +08:00
$params = $this -> request -> param (); // id check_status remark amount
2023-09-15 19:04:45 +08:00
$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 {
// 提前完成标识
2023-11-18 17:33:09 +08:00
$extend = $taskTemplate [ 'extend' ];
2023-10-26 13:45:27 +08:00
$extend [ 'early_finish' ] = 1 ;
$taskTemplate -> extend = json_encode ( $extend );
$taskTemplate -> save ();
$this -> pass ( $approve , $params );
}
} else {
$this -> pass ( $approve , $params );
}
} else {
2023-11-18 20:16:44 +08:00
$this -> pass ( $approve , $params );
2023-10-26 13:45:27 +08:00
}
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 ();
2023-11-18 20:16:44 +08:00
$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' , 41 )
-> find ()
-> toArray ();
TaskLogic :: masterTask7Settlement ( $taskSchedulePlan );
2023-11-06 15:58:46 +08:00
}
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' )
2023-11-18 20:16:44 +08:00
-> where ( 'scheduling.company_type' , 41 )
2023-11-06 15:58:46 +08:00
-> 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 ();
2023-11-18 09:52:43 +08:00
// 审批
$approve -> check_status = 2 ;
$approve -> update_time = time ();
$approve -> remark = $params [ 'remark' ];
$approve -> save ();
2023-11-16 10:36:32 +08:00
// 任务
$task = Task :: find ( $approve [ 'task_id' ]);
if ( $task [ 'status' ] == 2 ) {
$task -> status = 3 ;
$task -> save ();
}
2023-11-18 09:52:43 +08:00
Db :: commit ();
// 小组服务公司股金上交 小组服务团队-入股任务
// if ($approve->type == Approve::APPROVE_TYPE_10) {
// // 小组服务公司
// $subordinateCompany = Company::where(['id'=>$approve->department_id])->find();
// // 村公司
// $parentCompany = Company::where(['village'=>$subordinateCompany['village'], 'company_type'=>17])->find();
// }
//
// // 村联络员-督促小组服务团队入股任务
// if ( $approve->type == Approve::APPROVE_TYPE_11) {
// $parentCompany = Company::where(['id'=>$approve->department_id])->find();
// }
//
// // 村管理公司股金上交 村联络员任务-入股甲方公司
// if ($approve->type == Approve::APPROVE_TYPE_12) {
// // 村公司
// $subordinateCompany = Company::where(['id'=>$approve->department_id])->find();
// // 镇农科公司 负责区域包含有村公司的street码
// $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];
// }
//
// // 镇农科服务部长-督促小组服务团队入股村管理公司任务
// if ($approve->type == Approve::APPROVE_TYPE_13) {
// $parentCompany = Company::where(['id'=>$approve->department_id])->find(); //无法确定那个小组服务公司入股到哪个村管理公司
// }
//
// // 镇农科负责人任务-促成村联络员入股甲方
// if ($approve->type == Approve::APPROVE_TYPE_14) {
// // 镇农科公司
// $parentCompany = Company::where(['id'=>$approve->department_id])->find();
// }
//
// $amount = $approve->amount; // 上交股金金额
//
// $subordinateCompany->shareholder_money = $amount;
// $subordinateCompany->save();
//
// // 添加入股记录
// $this->addConpanyAccountLog($parentCompany, $amount, $task);
2023-11-16 10:36:32 +08:00
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 ();
2023-11-18 09:52:43 +08:00
$company = Company :: where ([ 'id' => $taskInfo [ 'company_id' ]]) -> find ();
$this -> taskSettlement ( $taskInfo , $company , $taskSchedulingPaln );
2023-11-16 10:36:32 +08:00
}
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 ( '成功' );
}
2023-11-18 09:52:43 +08:00
private function addConpanyAccountLog ( $parentCompany , $amount , $task )
2023-11-16 10:36:32 +08:00
{
// 接收股金公司股金变更 + 公司股金增加记录
$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 , //变动数量
2023-11-18 09:52:43 +08:00
'extend' => json_encode ([ 'task_id' => $task -> id ]),
2023-11-16 10:36:32 +08:00
'status' => 1 ,
];
CompanyAccountLog :: create ( $company_log2 );
}
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
}