427 lines
18 KiB
PHP
427 lines
18 KiB
PHP
<?php
|
||
|
||
namespace app\adminapi\controller\approve;
|
||
|
||
use app\adminapi\controller\BaseAdminController;
|
||
use app\adminapi\lists\approve\ApproveLists;
|
||
use app\common\enum\user\AccountLogEnum;
|
||
use app\common\logic\AccountLogLogic;
|
||
use app\common\logic\task\TaskLogic;
|
||
use app\common\model\Approve;
|
||
use app\common\model\Company;
|
||
use app\common\model\company\CompanyAccountLog;
|
||
use app\common\model\task\Task;
|
||
use app\common\model\task_scheduling_plan\TaskSchedulingPlan;
|
||
use app\common\model\task_template\TaskTemplate;
|
||
use app\common\model\user\User;
|
||
use app\common\model\user\UserAccountLog;
|
||
use think\facade\Db;
|
||
|
||
class ApproveController extends BaseAdminController
|
||
{
|
||
public function lists()
|
||
{
|
||
return $this->dataLists(new ApproveLists());
|
||
}
|
||
|
||
public function lists2()
|
||
{
|
||
return $this->success('成功',(new ApproveLists())->lists2());
|
||
}
|
||
|
||
public function lists3()
|
||
{
|
||
return $this->success('成功',(new ApproveLists())->lists3());
|
||
}
|
||
public function audit()
|
||
{
|
||
try {
|
||
$params = $this->request->param(); // id check_status remark amount
|
||
$approve = Approve::find($params['id']);
|
||
if (!$approve) {
|
||
$this->fail('数据不存在');
|
||
}
|
||
|
||
Db::startTrans();
|
||
// 拒绝通过 要让用户今天可以继续做任务
|
||
if ($params['check_status'] == 3) {
|
||
$this->refuse($params, $approve);
|
||
}
|
||
// 修改任务完成状态
|
||
if ($params['check_status'] == 2) {
|
||
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) {
|
||
return $this->fail('该任务提前完成条件:销售总额必须达到30万元及以上');
|
||
} else {
|
||
// 提前完成标识
|
||
$extend = $taskTemplate['extend'];
|
||
$extend['early_finish'] = 1;
|
||
$taskTemplate->extend = json_encode($extend);
|
||
$taskTemplate->save();
|
||
$this->pass($approve, $params);
|
||
}
|
||
} else {
|
||
$this->pass($approve, $params);
|
||
}
|
||
} else {
|
||
$this->pass($approve, $params);
|
||
}
|
||
|
||
}
|
||
Db::commit();
|
||
return $this->success('审核成功');
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
return $this->fail($e->getFile().$e->getLine().$e->getMessage());
|
||
}
|
||
}
|
||
// 通过
|
||
private function pass($approve, $params=[])
|
||
{
|
||
Db::startTrans();
|
||
$approve->check_status = 2;
|
||
$approve->update_time = time();
|
||
$approve->save();
|
||
// 任务
|
||
$task = Task::find($approve['task_id']);
|
||
if ($task['status'] == 2) {
|
||
$task->status = 3;
|
||
$task->save();
|
||
}
|
||
Db::commit();
|
||
|
||
// 镇合伙人公司任务-数字农贸宣传业务、加工业务的建设和招商工作任务 结算
|
||
if ($approve->type == Approve::APPROVE_TYPE_4) {
|
||
$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', 16)
|
||
->find()
|
||
->toArray();
|
||
TaskLogic::dealTaskMarketingDirector10($taskSchedulePlan, $approve);
|
||
}
|
||
|
||
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();
|
||
TaskLogic::dealVillageTask6($taskSchedulePlan);
|
||
}
|
||
|
||
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);
|
||
}
|
||
if ($approve->type == Approve::APPROVE_TYPE_7) {
|
||
// 需要手动输入销售总额
|
||
$approve->amount = $params['amount'];
|
||
$approve->save();
|
||
}
|
||
if ($approve->type == Approve::APPROVE_TYPE_8) {
|
||
// 需要手动输入申请的政策补贴金额
|
||
$approve->amount = $params['amount'];
|
||
$approve->save();
|
||
$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', 16)
|
||
->find()
|
||
->toArray();
|
||
TaskLogic::masterTask7Settlement($taskSchedulePlan);
|
||
}
|
||
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', 16)
|
||
->find()
|
||
->toArray();
|
||
TaskLogic::masterTask8Settlement($taskSchedulePlan);
|
||
}
|
||
}
|
||
|
||
// 拒绝
|
||
private function refuse($params, $approve)
|
||
{
|
||
$approve->check_status = $params['check_status'];
|
||
$approve->remark = $params['remark'];
|
||
$approve->update_time = time();
|
||
$approve->save();
|
||
|
||
// 更新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
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 股金任务审批
|
||
*/
|
||
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();
|
||
|
||
// 审批
|
||
$approve->check_status = 2;
|
||
$approve->update_time = time();
|
||
$approve->remark = $params['remark'];
|
||
$approve->save();
|
||
|
||
// 任务
|
||
$task = Task::find($approve['task_id']);
|
||
if ($task['status'] == 2) {
|
||
$task->status = 3;
|
||
$task->save();
|
||
}
|
||
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);
|
||
|
||
|
||
// 小组服务团队-入股任务结算
|
||
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();
|
||
$company = Company::where(['id' => $taskInfo['company_id']])->find();
|
||
$this->taskSettlement($taskInfo, $company, $taskSchedulingPaln);
|
||
}
|
||
// 村联络员-督促小组服务团队入股
|
||
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', 16)
|
||
->where('is_pay',0)
|
||
->with(['template_info'])
|
||
->select()
|
||
->toArray()[0];
|
||
TaskLogic::dealTownTask6($taskSchedulingPaln, $approve);
|
||
}
|
||
// 镇合伙人负责人-促成村联络员入股甲方
|
||
if ($approve->type == Approve::APPROVE_TYPE_14) {
|
||
$taskSchedulingPaln = TaskSchedulingPlan::where(['task_id' => $task->id])
|
||
->withJoin(['scheduling'], 'left')
|
||
->where('scheduling.company_type', 16)
|
||
->where('is_pay',0)
|
||
->with(['template_info'])
|
||
->select()
|
||
->toArray()[0];
|
||
TaskLogic::masterTask6Settlement($taskSchedulingPaln, $approve);
|
||
}
|
||
return $this->success('成功');
|
||
}
|
||
|
||
private function addConpanyAccountLog($parentCompany, $amount, $task)
|
||
{
|
||
// 接收股金公司股金变更 + 公司股金增加记录
|
||
$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(['task_id' => $task->id]),
|
||
'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]);
|
||
}
|
||
} |