硕顺crm后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

297 lines
11 KiB

<?php
namespace addons\qingdong\controller;
use addons\qingdong\model\AdminConfig;
use addons\qingdong\model\AttendanceRule;
use addons\qingdong\model\AttendanceStatisc;
use addons\qingdong\model\AttendanceTime;
use addons\qingdong\model\Message;
use addons\qingdong\model\Customer;
use addons\qingdong\model\Event;
use addons\qingdong\model\Staff;
use addons\qingdong\model\Leads;
use addons\qingdong\model\Attendance;
use think\Db;
use think\Exception;
use think\Log;
/**
* 定时任务
*/
class Autotask extends \think\addons\Controller
{
protected $noNeedLogin = ["*"];
protected $layout = '';
public function _initialize()
{
parent::_initialize();
if (!$this->request->isCli()) {
$this->error('只允许在终端进行操作!');
}
}
/**
* 合同
*/
public function contract()
{
$endDate = date('Y-m-d', strtotime('+3 day'));
$contracts = \addons\qingdong\model\Contract::where(['check_status' => 2, 'contract_status' => 0, 'end_time' => ['lt', $endDate]])->select();
try {
//合同即将到期通知
foreach ($contracts as $v) {
Message::addMessage(Message::CONTRACT_EXPIRE_TYPE, $v['id'], $v['owner_staff_id'], '');
}
} catch (Exception $e) {
return $e->getMessage();
}
$this->plan();
return 'success';
}
/**
* 回款计划通知
*/
public function plan()
{
$endDate = date('Y-m-d', strtotime('+1 day'));
//进行中 -- 待回款
$plans = \addons\qingdong\model\ReceivablesPlan::where(['status' => 0, 'remind_date' => ['lt', $endDate]])->select();
try {
//回款计划即将到期通知
foreach ($plans as $v) {
Message::addMessage(Message::PLAN_EXPIRE_TYPE, $v['contract_id'], $v['owner_staff_id'], '');
}
} catch (Exception $e) {
return $e->getMessage();
}
return 'success';
}
/**
* 公海
*/
public function seas()
{
if (AdminConfig::getConfigValue('auto', AdminConfig::TYPE_SEAS)) {//自动回收客户
if (AdminConfig::getConfigValue('genjing', AdminConfig::TYPE_SEAS) == 1) {//未跟进客户回收
$day = AdminConfig::getConfigValue('genjing_day', AdminConfig::TYPE_SEAS);
$where = ['receivetime' => ['<', strtotime('-' . $day . ' day')]];
if (AdminConfig::getConfigValue('genjing_success', AdminConfig::TYPE_SEAS) == 1) {
$where['contract_status'] = 0;
}
$customers = Customer::where($where)->field('id,name,owner_staff_id')->select();
try {
foreach ($customers as $c) {
Customer::moveSeas($c['id']);
Message::addMessage(Message::SEAS_TYPE, $c['id'], $c['owner_staff_id'], 0);
}
} catch (Exception $e) {
Log::error($e->getMessage());
}
}
if (AdminConfig::getConfigValue('chengjiao', AdminConfig::TYPE_SEAS) == 1) {//未成交客户或者未上传营业执照的回收
$day = AdminConfig::getConfigValue('chengjiao_day', AdminConfig::TYPE_SEAS);
$where = [
'receivetime' => ['<', strtotime('-' . $day . ' day')],
'contract_status' => 0
];
$where2 = [
'receivetime' => ['<', strtotime('-' . $day . ' day')],
'business_img' => ''
];
$customers = Customer::whereor(function($query) use ($where){$query->where($where);})->whereor(function($query) use ($where2){$query->where($where2);})->field('id,name,owner_staff_id')->select();
try {
foreach ($customers as $c) {
Customer::moveSeas($c['id']);
Message::addMessage(Message::SEAS_TYPE, $c['id'], $c['owner_staff_id'], 0);
}
} catch (Exception $e) {
Log::error($e->getMessage());
}
}
}
return 'success';
}
/**
* 日程提醒
* @return string
*/
public function event()
{
$endDate = date('Y-m-d H:i:s');
$events = Event::where(['status' => 0, 'remind_time' => ['lt', $endDate]])->select();
$endIds = Event::where([
'status' => ['in', [0, 1]],
'auto_end' => 1,
'end_time' => ['lt', $endDate]
])->column('id');
try {
//日程自动结束
Event::where(['id' => ['in', $endIds]])->update(['status' => 2]);
//日程时间提醒
foreach ($events as $v) {
if (Message::where([
'relation_type' => Message::EVENT_TYPE,
'relation_id' => $v['id'],
'to_staff_id' => $v['staff_id']
])->count()) {
//已经提醒过了
continue;
}
Message::addMessage(Message::EVENT_TYPE, $v['id'], $v['staff_id'], 0);
}
} catch (Exception $e) {
return $e->getMessage();
}
return 'success';
}
/**
* 打卡定时任务
*/
public function attendance()
{
$w = date('w');
$date = [
1 => 'monday',
2 => 'tuesday',
3 => 'wednesday',
4 => 'thursday',
5 => 'friday',
6 => 'saturday',
0 => 'weekday',
];
$weekname = $date[$w];
$times = AttendanceTime::where([$weekname => 1])->select();
if (empty($times)) {
return '未设置规则';
}
$row = AttendanceStatisc::where(['time' => date('Y-m-d')])->find();
if (!empty($row)) {
return '规则已生成';
}
$times = collection($times)->toArray();
$rule = AttendanceRule::where([])->find();
$statiscs = AttendanceStatisc::where(['time' => date('Y-m-d', strtotime('-1 day'))])->select();
foreach ($statiscs as $v) {
$update = [];
if (empty($t['clock_in']) && !empty($t['clock_out'])) {//缺卡
$update['clock_in_status'] = 1;
}
if (empty($t['clock_out']) && !empty($t['clock_in'])) {//缺卡
$update['clock_out_status'] = 1;
}
if (empty($t['clock_in']) && empty($t['clock_out'])) {//旷工
$update['clock_in_status'] = 2;
$update['clock_out_status'] = 2;
}
if ($update) {
$statiscModel = new AttendanceStatisc();
$statiscModel->save($update, ['id' => $v['id']]);
}
}
$staff_ids = explode(',', $rule['staff_id']);
$statisc = [];
foreach ($staff_ids as $sid) {
foreach ($times as $t) {
$statisc[] = [
'staff_id' => $sid,
'number' => $t['number'],
'time' => date('Y-m-d'),
'start_time' => $t['start_time'],
'end_time' => $t['end_time'],
'ustart_time' => $t['ustart_time'],
'uend_time' => $t['uend_time'],
'dstart_time' => $t['dstart_time'],
'dend_time' => $t['dend_time'],
'start_status' => $t['start_status'],
'end_status' => $t['end_status'],
];
}
}
$statiscModel = new AttendanceStatisc();
$result = $statiscModel->allowField(true)->saveAll($statisc);
if (empty($result)) {
return '生成规则失败';
}
return '生成规则成功';
}
/*
* 线索池分配
*/
public function leadpool(){
if(AdminConfig::getConfigValue('leadauto', AdminConfig::TYPE_LEAD)){
$leadauto = AdminConfig::getConfigValue('leadauto', AdminConfig::TYPE_LEAD);
$lead_day = AdminConfig::getConfigValue('lead_day', AdminConfig::TYPE_LEAD);
$attendance = AdminConfig::getConfigValue('leadcard', AdminConfig::TYPE_LEAD);
$department = AdminConfig::getConfigValue('department', AdminConfig::TYPE_LEAD);
//开启分配
if($leadauto == 1 && $lead_day>0 && $department){
$leadpool = Leads::where('owner_staff_id is null or owner_staff_id = 0')->order('id desc')->limit(100)->column('id');
if($leadpool){
$department = explode(',',$department);
$staffinfo = array();
foreach($department as $k=>$v){
$staffids = Staff::where('', 'exp', Db::raw('FIND_IN_SET(' . $v . ',group_ids)'))->where(['status'=>1])->column('id');
$staffinfo[] = $staffids;
}
if(!$staffinfo){
return '无数据';
}
$staffinfos =[];
foreach($staffinfo as $k=>$v){
foreach($v as $ks=>$vs){
$staffinfos[] = $vs;
}
}
$staff = array_unique($staffinfos);
foreach($leadpool as $ks=>$vs){
$stafflead =[];
foreach($staff as $k=>$v){
if($attendance == 1){ //打卡
$whereC['staff_id']= $v;
$whereC['createtime'] = array(array('egt',strtotime(date('Y-m-d 00:00:00'))),array('elt',strtotime(date('Y-m-d 23:59:59'))));
$leadcard = Attendance::where($whereC)->find();
if(!$leadcard){//未打卡不分配
continue;
}
}
$wherelead['owner_staff_id'] = $v;
$wherelead['receive_time'] = array(array('egt',strtotime('-'.$lead_day.' days',time())),array('elt',time()));
$lead = Leads::where($wherelead)->count();
$stafflead[] = array(
'id'=>$v,
'num'=>$lead
);
}
if($stafflead){
$num_count = array_column($stafflead,'num');//返回数组中指定的一列
array_multisort($num_count,SORT_ASC,$stafflead);
Leads::where(array('id'=>$vs))->update(array('owner_staff_id'=>$stafflead[0]['id'],'receive_time'=>time()));
}
}
}
}
}
return 'success';
}
}