empcrm->root_id; $empid = $request->empcrm->id; $team_orgs = orgSubIds($request->empcrm->org_id); $param = $this->request->only(['cid', 'date']); if (!$request->isAjax()) { $data['company_name'] = Company::where('root_id', $root_id)->value('company_name'); View::assign('data', $data); return View::fetch(); } $year = $param['date']; $xin = []; $weekarray = array("日", "一", "二", "三", "四", "五", "六"); //先定义一个数组 for ($i = 1; $i < 13; $i++) { $month = $i; $m = $i < 10 ? '0' . $i : $i; $start = $year . '-' . $m . '-01 00:00:00'; $end = date('Y-m-d', strtotime("$start +1 month -1 day")); $a = ['month' => $month, 'start' => $start, 'end' => $end . ' 23:59:59']; $xin[] = $a; } $param_sub_org = $team_orgs; $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id'); //$condition[] = ['org_id', 'in', $param_sub_org]; $condition[] = ['employee_id', 'in', $org_employee]; //$condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')]; $condition[] = ['sign_time', 'between', [$param['date'] . '-01-01 00:00:00', $param['date'] . '-12-31 23:59:59']]; //$customer_list = Customer::where($condition)->field('id,addtime,state,died')->select()->toArray();//->count(); $customer_list = Customer::where($condition)->where('state', 'not in', Customer::changeState('待确认', 'chaos'))->column('id,sign_time,state,died'); //->count(); //查询无效的客户 $invalidcus = CustomerInvalidLog::where([['root_id', '=', $root_id], ['employee_id', 'in', $org_employee], ['cus_addtime', 'between', [$param['date'] . '-01-01 00:00:00', $param['date'] . '-12-31 23:59:59']]])->column('cus_addtime,customer_id,status'); $state1 = Customer::changeState('待确认', 'chaos'); $state2 = Customer::changeState('无效', 'chaos'); $state3 = CustomerVisitLog::changeState('已交定', 'chaos'); $state4 = CustomerVisitLog::changeState('已签单', 'chaos'); $state5 = CustomerVisitLog::changeState('确认到店', 'chaos'); $state6 = CustomerVisitLog::changeState('无效', 'chaos'); $state7 = CustomerVisitLog::changeState('已量房', 'chaos'); $state8 = CustomerVisitLog::changeState('已到场', 'chaos'); $where[] = ['customer_employee_id', 'in', $org_employee]; $where[] = ['confirm_date', 'between', [$param['date'] . '-01-01 00:00:00', $param['date'] . '-12-31 23:59:59']]; $where[] = ['state', 'in', array_merge($state3, $state4, $state5, $state7, $state8)]; $visitlog_list = CustomerVisitLog::where($where) ->group('state,customer_id') ->column('customer_id,state,confirm_date,money'); //var_dump($visitlog_list);exit; //->select()->toArray(); $empmod = new Empcrm($this->app); $metids = []; foreach ($visitlog_list as $key => $val) { if (in_array($val['state'], array_merge($state5, $state7, $state8))) $metids[] = $val['customer_id']; } $yesids = !empty(array_unique($metids)) ? $empmod->selBeforelog(array_unique($metids), $param['date'] . '-01-01 00:00:00') : []; $exten_where[] = ['root_id', '=', $root_id]; $exten_where[] = ['addtime', 'between', [$param['date'] . '-01-01 00:00:00', $param['date'] . '-12-31 23:59:59']]; $exten_list = CustomerExtension::with(['source' => function ($query) { $query->field('id,source'); }])->where($exten_where)->select()->toArray(); $t = ['month' => '求和', 'deposit_percen' => 0, 'exten_money' => 0, 'exten_show' => 0, 'exten_click' => 0, 'click_percen' => 0, 'clue_conversion' => 0, 'clue_money' => 0, 'valid_money' => 0, 'meet_money' => 0, 'sign_agv_money' => 0, 'ROI' => 0, 'week' => '', 'data' => [], 'clue_cont' => 0, 'valid_cont' => 0, 'valid_percen' => 0, 'meet_cont' => 0, 'meet_percen' => 0, 'deposit_cont' => 0, 'sign_cont' => 0, 'basic_money' => 0, 'other_money' => 0, 'all_money' => 0, 'invalid_no' => 0, 'invalid_yes' => 0, 'invalid_meet' => 0, 'invalid_deposit' => 0, 'invalid_sign' => 0]; $meetids = []; foreach ($xin as $key => $val) { $valids = $deposids = $cud = $daystate = $wuxiao = []; $exten_money = $exten_show = $exten_click = $clue_cont = $valid = $meet = $deposit = $sign = $basic_money = $other_money = $invalid_no = $invalid_yes = $invalid_meet = $invalid_deposit = 0; foreach ($customer_list as $k => $v) { if ($v['sign_time'] >= $val['start'] && $v['sign_time'] <= $val['end']) { //if($v['died'] != 2 && !in_array($v['state'],array_merge($state1,$state2),true)) $valid++; if ($v['died'] != 2) { $valid++; $valids[] = $v['id']; } } } $clue_ids = Customer::where([['employee_id', 'in', $org_employee], ['sign_time', '>=', $val['start']], ['sign_time', '<=', $val['end']]])->column('id'); $clue_cont = count($clue_ids); //添加无效的客户到有效上 foreach ($invalidcus as $k => $v) { if ($v['cus_addtime'] >= $val['start'] && $v['cus_addtime'] <= $val['end'] && !in_array($v['customer_id'], $clue_ids)) { $clue_cont++; $clue_ids[] = $v['customer_id']; } if ($v['cus_addtime'] >= $val['start'] && $v['cus_addtime'] <= $val['end'] && !in_array($v['customer_id'], $valids)) { if ($v['status'] == 1) $valid++; $valids[] = $v['customer_id']; } } unset($clue_ids, $valids); foreach ($visitlog_list as $k => $v) { if ($v['confirm_date'] >= $val['start'] && $v['confirm_date'] <= $val['end']) { if (!in_array($v['customer_id'], $meetids) && in_array($v['state'], array_merge($state5, $state7, $state8), true) && in_array($v['customer_id'], $yesids)) { $meet++; $meetids[] = $v['customer_id']; } if (in_array($v['state'], $state3, true)) { $deposit++; $deposids[] = $v['customer_id']; } if (in_array($v['state'], $state4, true)) { $basic_money += $v['money']; $sign++; } if (in_array($v['state'], $state6, true)) { $invalid_no++; $wuxiao[] = $v['customer_id']; } } } foreach ($wuxiao as $k => $v) { if (in_array($v, $meetids, true)) { $invalid_meet++; } if (in_array($v, $deposids, true)) { $invalid_deposit++; } } //$valid = CustomerVisitLog::where([['customer_id', 'in', $cud], ['state', 'in', $now_status],['confirm_date','between',[$val['start'],$val['end']]]])->group('customer_id')->count(); foreach ($exten_list as $k => $v) { if ($v['addtime'] >= $val['start'] && $v['addtime'] <= $val['end']) { $exten_money += $v['money']; $exten_show += $v['show']; $exten_click += $v['click']; } } $xin[$key]['exten_money'] = $exten_money; $xin[$key]['exten_show'] = $exten_show; $xin[$key]['exten_click'] = $exten_click; $xin[$key]['click_percen'] = !empty($exten_click) && !empty($exten_show) ? round($exten_click / $exten_show, 2) : 0; $xin[$key]['clue_conversion'] = !empty($exten_click) && !empty($clue_cont) ? round($clue_cont / $exten_click * 100, 2) : 0; $xin[$key]['clue_cont'] = $clue_cont; $xin[$key]['clue_money'] = !empty($clue_cont) && !empty($exten_money) ? round($exten_money / $clue_cont, 2) : 0; $xin[$key]['valid_cont'] = $valid; $xin[$key]['valid_money'] = !empty($valid_cont) && !empty($exten_money) ? round($exten_money / $valid_cont, 2) : 0; $xin[$key]['valid_percen'] = $valid && $clue_cont ? round($valid / $clue_cont * 100, 2) : 0; $xin[$key]['meet_cont'] = $meet; $xin[$key]['meet_percen'] = !empty($meet) && !empty($clue_cont) ? round($meet / $clue_cont * 100, 2) : 0; $xin[$key]['meet_money'] = !empty($meet) && !empty($exten_money) ? round($exten_money / $meet, 2) : 0; $xin[$key]['deposit_cont'] = $deposit; $xin[$key]['deposit_percen'] = !empty($deposit) && !empty($meet) ? round($deposit / $meet * 100, 2) : 0; $xin[$key]['sign_cont'] = $sign; $xin[$key]['basic_money'] = $basic_money; $xin[$key]['sign_agv_money'] = !empty($sign_cont) && !empty($basic_money) ? floor($basic_money / $sign_cont) : 0; $xin[$key]['other_money'] = $other_money; $xin[$key]['all_money'] = $basic_money + $other_money; //$xin[$key]['invalid_no'] = $invalid_no; //$xin[$key]['invalid_yes'] = $invalid_yes; $xin[$key]['invalid_meet'] = $invalid_meet; $xin[$key]['invalid_deposit'] = $invalid_deposit; //$xin[$key]['invalid_sign'] = $invalid_sign; $xin[$key]['ROI'] = !empty($basic_money) && !empty($exten_money) ? round($exten_money / $basic_money * 100, 2) : 0; //总数 $t['exten_money'] += $exten_money; $t['exten_show'] += $exten_show; $t['exten_click'] += $exten_click; $t['click_percen'] += !empty($exten_click) && !empty($exten_show) ? round($exten_click / $exten_show, 2) : 0; $t['clue_conversion'] = !empty($exten_click) && !empty($clue_cont) ? round($clue_cont / $exten_click * 100, 2) : 0; $t['clue_cont'] += $clue_cont; //$t['clue_money'] = !empty($clue_cont) && !empty($exten_money) ? round($exten_money/$clue_cont,2) : 0; $t['valid_cont'] += $valid; //$t['valid_money'] = !empty($valid_cont) && !empty($exten_money) ? round($exten_money/$valid_cont,2) : 0; //$t['valid_percen'] += $clue_cont && $valid ? round($valid/$clue_cont*100,2) : 0; $t['meet_cont'] += $meet; //$t['meet_money'] = !empty($meet) && !empty($exten_money) ? round($exten_money/$meet,2) : 0; //$t['meet_percen'] += !empty($meet) && !empty($clue_cont) ? round($meet/$clue_cont*100,2) : 0; $t['deposit_cont'] += $deposit; $t['sign_cont'] += $sign; $t['basic_money'] += $basic_money; //$t['sign_agv_money'] = !empty($sign_cont) && !empty($basic_money) ? floor($basic_money/$sign_cont) : 0; $t['other_money'] += $other_money; $t['all_money'] += $other_money + $basic_money; // $t['invalid_no'] += $invalid_no; // $t['invalid_yes'] += $invalid_yes; $t['invalid_meet'] += $invalid_meet; $t['invalid_deposit'] += $invalid_deposit; //$t['invalid_sign'] += $invalid_sign; $t['ROI'] = !empty($basic_money) && !empty($exten_money) ? round($exten_money / $basic_money * 100, 2) : 0; } $t['clue_money'] = !empty($t['clue_cont']) && !empty($t['exten_money']) ? round($t['exten_money'] / $t['clue_cont'], 2) : 0; $t['valid_money'] = !empty($t['valid_cont']) && !empty($t['exten_money']) ? round($t['exten_money'] / $t['valid_cont'], 2) : 0; $t['valid_percen'] = !empty($t['clue_cont']) && !empty($t['valid_cont']) ? round($t['valid_cont'] / $t['clue_cont'] * 100, 2) : 0; $t['meet_percen'] += !empty($t['meet_cont']) && !empty($t['clue_cont']) ? round($t['meet_cont'] / $t['clue_cont'] * 100, 2) : 0; $t['meet_money'] = !empty($t['meet_cont']) && !empty($t['exten_money']) ? round($t['exten_money'] / $t['meet_cont'], 2) : 0; $t['deposit_percen'] = !empty($t['deposit_cont']) && !empty($t['meet_cont']) ? round($t['deposit_cont'] / $t['meet_cont'] * 100, 2) : 0; $t['sign_agv_money'] = !empty($t['sign_cont']) && !empty($t['basic_money']) ? floor($t['basic_money'] / $t['sign_cont']) : 0; array_push($xin, $t); return json(['code' => 0, 'data' => $xin, 'count' => 0, 'msg' => '获取成功']); } //推广明细 public function extension_report() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $team_orgs = orgSubIds($request->empcrm->root_id); $param = $this->request->only(['source_id', 'date', 'page' => 1, 'limit' => 17]); $order = isset($param['order']) ? $param['order'] : 'addtime desc'; $page = intval($param['page']); $limit = intval($param['limit']); if (!$request->isAjax()) { $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray(); View::assign('soudata', $soudata); //客户等级 隶属计划 $fields = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', 'in', ['level']]])->select()->toArray(); View::assign('fields', $fields); return View::fetch(); } $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id'); $where[] = ['root_id','=',$root_id]; if(!empty($param['source_id'])) { is_numeric($param['source_id'])?$where[] = ['source_id','=',$param['source_id']]:$where[] = ['source_id','in',$param['source_id']]; } if(!empty($param['date'])) $where[] = ['addtime','between',[$param['date'].' 00:00:00',$param['date'].' 23:59:59']]; $list = CustomerExtension::with(['source'=>function($query){ $query->field('id,source'); }])->where($where)->page($page, $limit)->order($order)->select()->toArray(); $company_name = Company::where('root_id', $root_id)->value('company_name'); $weekarray = array("日", "一", "二", "三", "四", "五", "六"); //先定义一个数组 foreach ($list as $key => $val) { $clue_cont = $valid = 0; $clue_list = Customer::where([['employee_id', 'in', $org_employee], ['sign_time', 'between', [$val['addtime'] . ' 00:00:00', $val['addtime'] . ' 23:59:59']], ['org_id', 'in', $team_orgs], ['source_id', '=', $val['source_id']]])->column('id,state,died'); $clue_cont = count($clue_list); foreach ($clue_list as $k => $v) { if ($v['died'] != 2 && !in_array($v['state'], array_merge(Customer::changeState('待确认', 'chaos'), Customer::changeState('无效', 'chaos')), true)) $valid++; } //查询无效的客户 $invalidcus = CustomerInvalidLog::where([['employee_id', 'in', $org_employee], ['source_id', '=', $val['source_id']], ['root_id', '=', $root_id], ['cus_addtime', 'between', [$val['addtime'] . ' 00:00:00', $val['addtime'] . ' 23:59:59']]])->column('cus_addtime,employee_id,status'); $clue_cont += count($invalidcus); foreach ($invalidcus as $k => $v) { if ($v['status'] == 1) $valid++; } //$valid+= count($invalidcus); $list[$key]['week'] = $weekarray[date("w", strtotime($val['addtime']))]; $list[$key]['company_name'] = $company_name; $list[$key]['clue_cont'] = $clue_cont; $list[$key]['valid'] = $valid; $list[$key]['danshow_money'] = !empty($val['money']) && !empty($val['show']) ? round($val['money'] / $val['show'], 2) : 0; //展现费用=费用/展现次数 $list[$key]['click_percen'] = !empty($val['click']) && !empty($val['show']) ? round($val['click'] / $val['show'] * 100, 2) : 0; //点击率=点击次数/展现次数 $list[$key]['click_cost'] = !empty($val['money']) && !empty($val['click']) ? round($val['money'] / $val['click'], 2) : 0; //点击成本=费用/点击次数 $list[$key]['clue_conversion'] = !empty($val['click']) && !empty($clue_cont) ? round($clue_cont / $val['click'] * 100, 2) : 0; //转化线索率=线索/点击次数 $list[$key]['valid_conversion'] = !empty($val['money']) && !empty($clue_cont) ? round($val['money'] / $clue_cont, 2) : 0; //有效线索转化率=有效线索/点击次数 $list[$key]['valid_percen'] = !empty($valid) && !empty($clue_cont) ? round($valid / $clue_cont, 2) : 0; //有效率=有效线索/线索数 $list[$key]['valid_money'] = !empty($valid) && !empty($val['money']) ? round($val['money'] / $valid, 2) : 0; //有效单价=费用/有效索数 $list[$key]['clue_money'] = !empty($clue_cont) && !empty($val['money']) ? round($val['money'] / $clue_cont, 2) : 0; //线索单价=费用/线索量 } $count = CustomerExtension::where($where)->count(); return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']); } //添加推广明细数据 public function add_extension() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $param = $this->request->only(['source_id','launch_time','money','show','click']); if (!$request->isAjax()) { $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray(); View::assign('soudata', $soudata); $company_name = Company::where('root_id', $root_id)->value('company_name'); View::assign('company_name', $company_name); View::assign('empname', $request->empcrm->name); return View::fetch(); } $add['source_id'] = $param['source_id']; $add['addtime'] = $param['launch_time']; $add['money'] = $param['money']; $add['show'] = $param['show']; $add['click'] = $param['click']; $add['root_id'] = $root_id; $add['employee_id'] = $empid; $add['org_id'] = $request->empcrm->org_id; $ms = CustomerExtension::insertGetId($add); if ($ms) { return json(['code' => 0, 'data' => $ms, 'msg' => '添加成功']); } else { return json(['code' => 1, 'data' => '', 'msg' => '添加失败']); } } //修改推广明细 public function edit_extension() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $param = $this->request->only(['id','source_id','launch_time','money','show','click']); if (!$request->isAjax()) { $data = CustomerExtension::where('id',$param['id'])->find(); if(empty($data)) return json(['code' => 1, 'data' =>'', 'msg'=>'id为空']); View::assign('data',$data); $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray(); View::assign('soudata', $soudata); $company_name = Company::where('root_id', $root_id)->value('company_name'); View::assign('company_name', $company_name); View::assign('empname', $request->empcrm->name); return View::fetch(); } if (empty($param['id'])) return json(['code' => 1, 'data' => '', 'msg' => 'id为空']); $add['source_id'] = $param['source_id']; $add['addtime'] = $param['launch_time']; $add['money'] = $param['money']; $add['show'] = $param['show']; $add['click'] = $param['click']; $ms = CustomerExtension::where([['id', '=', $param['id']], ['root_id', '=', $root_id]])->update($add); if ($ms) { return json(['code' => 0, 'data' => $ms, 'msg' => '修改成功']); } else { return json(['code' => 1, 'data' => '', 'msg' => '修改失败']); } } /** * 查询部门下的员工列表 */ public function seL_emplist() { $request = request(); $root_id = $request->empcrm->root_id; $param = $this->request->only(['org_id']); $team_orgs = orgSubIds($request->empcrm->org_id); if (!empty($param['org_id'])) $team_orgs = orgSubIds($param['org_id']); $emplist = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', $team_orgs], ['uid', '>', 0], ['state', '=', '在职']])->column('id,name'); return json(['code' => 0, 'data' => $emplist, 'msg' => '获取成功']); } //账户明细 public function account_report() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $team_orgs = orgSubIds($request->empcrm->org_id); $param = $this->request->only(['date', 'employee_id', 'source_id', 'org_id']); if (!$request->isAjax()) { $emplist = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职']])->field('id,name')->select()->toArray(); View::assign('emplist', $emplist); $org_list = Org::where([['id', 'in', $team_orgs]])->select()->toArray(); View::assign('org_list', $org_list); $data['company_name'] = Company::where('root_id', $root_id)->value('company_name'); $data['empname'] = $request->empcrm->name; View::assign('data', $data); $source_list = CustomerSource::where('root_id', '=', $root_id)->select()->toArray(); View::assign('source_list', $source_list); return View::fetch(); } if (!empty($param['date'])) { $newtime = explode('到', $param['date']); } else { return json(['code' => 1, 'msg' => '日期为空']); } $soud_where[] = ['root_id', '=', $root_id]; $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id'); if (!empty($param['source_id'])) $soud_where[] = ['id', 'in', explode(',', $param['source_id'])]; $soud_list = CustomerSource::field('id,source as name')->where($soud_where)->select()->toArray(); $where[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]; //$where[] = ['org_id','in',$team_orgs]; if (!empty($param['employee_id'])) $org_employee = [$param['employee_id']]; if (!empty($param['org_id']) && empty($param['employee_id'])) { $org_employee = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', orgSubIds($param['org_id'])]])->column('id'); } $where[] = ['employee_id', 'in', $org_employee]; $exten_where[] = ['root_id', '=', $root_id]; $exten_where[] = ['addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]; if (!empty($param['employee_id'])) $exten_where[] = ['id', '=', 0]; $exten_list = CustomerExtension::with(['source' => function ($query) { $query->field('id,source'); }])->where($exten_where)->select()->toArray(); $t = ['dd' => 0, 'dc' => 0, 'lf' => 0, 'name' => '求和', 'deposit_percen' => 0, 'exten_money' => 0, 'exten_show' => 0, 'exten_click' => 0, 'click_percen' => 0, 'clue_conversion' => 0, 'clue_money' => 0, 'valid_money' => 0, 'meet_money' => 0, 'sign_agv_money' => 0, 'ROI' => 0, 'week' => '', 'data' => [], 'clue_cont' => 0, 'valid_cont' => 0, 'valid_percen' => 0, 'meet_cont' => 0, 'meet_percen' => 0, 'deposit_cont' => 0, 'sign_cont' => 0, 'basic_money' => 0, 'other_money' => 0, 'all_money' => 0, 'invalid_no' => 0, 'invalid_yes' => 0, 'invalid_meet' => 0, 'invalid_deposit' => 0, 'invalid_sign' => 0]; $state1 = CustomerVisitLog::changeState('确认到店', 'chaos'); $state2 = CustomerVisitLog::changeState('已交定', 'chaos'); $state3 = CustomerVisitLog::changeState('已签单', 'chaos'); $state4 = CustomerVisitLog::changeState('无效', 'chaos'); $state5 = Customer::changeState('无效', 'chaos'); $state6 = Customer::changeState('待确认', 'chaos'); $state7 = CustomerVisitLog::changeState('已量房', 'chaos'); $state8 = CustomerVisitLog::changeState('已到场', 'chaos'); $ismeet = []; foreach ($soud_list as $key => $val) { $soud_list[$key]['employee_id'] = !empty($param['employee_id']) ? $param['employee_id'] : ''; $soud_list[$key]['org_id'] = !empty($param['org_id']) ? $param['org_id'] : ''; $dd = $lf = $dc = $invalid_sign = $invalid_deposit = $exten_money = $exten_show = $exten_click = $clue_cont = $valid = $meet = $deposit = $sign = $basic_money = $other_money = $invalid_no = $invalid_yes = $invalid_meet = 0; $validids = $cusids = $vs_where = []; $customer_list = Customer::where($where)->where([['source_id', '=', $val['id']], ['state', 'not in', $state6]])->column('state', 'id'); //$clue_cont = Customer::where($where)->where([['source_id','=',$val['id']]])->count(); $clue_ids = Customer::where($where)->where([['source_id', '=', $val['id']]])->column('id'); $del_clue_ids = Customer::where([['source_id', '=', $val['id']]])->onlyTrashed()->column('id'); $clue_cont = count($clue_ids); foreach ($customer_list as $k => $v) { if (!in_array($v, $state5)) { $cusids[] = $k; $valid++; } } unset($customer_list); $validids = $cusids; //查询无效的客户 $invalidcus = CustomerInvalidLog::hasWhere('customer',['source_id'=>$val['id']])->where([['CustomerInvalidLog.employee_id', 'in', $org_employee], ['CustomerInvalidLog.root_id', '=', $root_id], ['CustomerInvalidLog.source_id', '=', $val['id']], ['CustomerInvalidLog.cus_addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]])->column('CustomerInvalidLog.customer_id,CustomerInvalidLog.employee_id,CustomerInvalidLog.status'); foreach ($invalidcus as $k => $v) { if (!in_array($v['customer_id'], $validids)) { if ($v['status'] == 1) { $valid++; $validids[] = $v['customer_id']; } } if (!in_array($v['customer_id'], $clue_ids) && !in_array($v['customer_id'],$del_clue_ids)) { $clue_cont++; $clue_ids[] = $v['customer_id']; } } unset($clue_ids, $validids, $cusids); foreach ($exten_list as $k => $v) { if ($v['source_id'] == $val['id']) { $exten_money += $v['money']; $exten_show += $v['show']; $exten_click += $v['click']; } } $vs_where[] = ['customer_employee_id', 'in', $org_employee]; $vs_where[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]; $w = Customer::where('source_id', $val['id'])->withTrashed(); $b_start = date('Y-m-d H:i:s', (strtotime($newtime[0])-1)); $b_end = date('Y-m-d H:i:s', (strtotime($newtime[1]) + 86399)); $visitlog_list = CustomerVisitLog::hasWhere('customer', $w) ->where([['CustomerVisitLog.customer_employee_id', 'in', $org_employee], ['CustomerVisitLog.confirm_date', 'between', [$b_start, $b_end]]]) ->group('state,customer_id')->column('CustomerVisitLog.id,CustomerVisitLog.customer_id,CustomerVisitLog.state,CustomerVisitLog.money'); $metids = []; foreach ($visitlog_list as $k => $v) { if (in_array($v['state'], array_merge($state1, $state2, $state3, $state7, $state8))) $metids[] = $v['customer_id']; } $empmod = new Empcrm($this->app); $yesids = !empty(array_unique($metids)) ? $empmod->selBeforelog(array_unique($metids), $newtime[0] . ' 00:00:00') : []; $isdep = $ising = $wuxiao = []; foreach ($visitlog_list as $k => $v) { if (in_array($v['state'], array_merge($state1, $state2, $state3, $state7, $state8)) && !in_array($v['customer_id'], $ismeet) && in_array($v['customer_id'], $yesids)) { $meet++; $ismeet[] = $v['customer_id']; } if (in_array($v['state'], $state1)) { $dd++; } if (in_array($v['state'], $state7)) { $lf++; } if (in_array($v['state'], $state8)) { $dc++; } if (in_array($v['state'], $state2)) { $deposit++; $isdep[] = $v['customer_id']; } if (in_array($v['state'], $state3)) { $sign++; $basic_money += $v['money']; $ising[] = $v['customer_id']; } if (in_array($v['state'], $state4)) $wuxiao[] = $v['customer_id']; } if (!empty($wuxiao)) { foreach ($wuxiao as $v) { if (in_array($v, $ismeet)) $invalid_meet++; if (in_array($v, $isdep)) $invalid_deposit++; if (in_array($v, $ising)) $invalid_sign++; } } unset($visitlog_list); $soud_list[$key]['dd'] = $dd; $soud_list[$key]['dc'] = $dc; $soud_list[$key]['lf'] = $lf; $soud_list[$key]['exten_money'] = !empty($exten_money) ? round($exten_money, 2) : 0; $soud_list[$key]['exten_show'] = $exten_show; $soud_list[$key]['exten_click'] = $exten_click; $soud_list[$key]['click_percen'] = !empty($exten_click) && !empty($exten_show) ? round($exten_click / $exten_show, 2) : 0; $soud_list[$key]['clue_conversion'] = !empty($exten_click) && !empty($clue_cont) ? round($clue_cont / $exten_click * 100, 2) : 0; $soud_list[$key]['clue_cont'] = $clue_cont; $soud_list[$key]['clue_money'] = !empty($clue_cont) && !empty($exten_money) ? round($exten_money / $clue_cont, 2) : 0; $soud_list[$key]['valid_cont'] = $valid; $soud_list[$key]['valid_money'] = !empty($valid) && !empty($exten_money) ? round($exten_money / $valid, 2) : 0; $soud_list[$key]['valid_percen'] = $valid && $clue_cont ? round($valid / $clue_cont * 100, 2) : 0; $soud_list[$key]['meet_cont'] = $meet; $soud_list[$key]['meet_percen'] = !empty($meet) && !empty($clue_cont) ? round($meet / $clue_cont * 100, 2) : 0; $soud_list[$key]['meet_money'] = !empty($meet) && !empty($exten_money) ? round($exten_money / $meet, 2) : 0; $soud_list[$key]['deposit_cont'] = $deposit; $soud_list[$key]['deposit_percen'] = !empty($deposit) && !empty($meet) ? round($deposit / $meet * 100, 2) : 0; $soud_list[$key]['sign_cont'] = $sign; $soud_list[$key]['basic_money'] = $basic_money; $soud_list[$key]['sign_agv_money'] = !empty($sign) && !empty($basic_money) ? floor($basic_money / $sign) : 0; $soud_list[$key]['other_money'] = $other_money; $soud_list[$key]['all_money'] = $basic_money + $other_money; $soud_list[$key]['invalid_no'] = $invalid_no; $soud_list[$key]['invalid_meet'] = $invalid_meet; $soud_list[$key]['invalid_deposit'] = $invalid_deposit; $soud_list[$key]['invalid_sign'] = $invalid_sign; $soud_list[$key]['ROI'] = !empty($exten_money) && !empty($basic_money) ? round($exten_money / $basic_money * 100, 2) : 0; //总数 $t['dd'] += $dd; $t['dc'] += $dc; $t['lf'] += $lf; $t['exten_money'] += !empty($exten_money) ? round($exten_money, 2) : 0; $t['exten_show'] += $exten_show; $t['exten_click'] += $exten_click; $t['clue_conversion'] += !empty($exten_click) && !empty($clue_cont) ? round($clue_cont / $exten_click * 100, 2) : 0; $t['clue_cont'] += $clue_cont; $t['valid_cont'] += $valid; $t['meet_cont'] += $meet; $t['deposit_cont'] += $deposit; $t['sign_cont'] += $sign; $t['basic_money'] += $basic_money; $t['sign_agv_money'] += !empty($sign) && !empty($basic_money) ? floor($basic_money / $sign) : 0; $t['other_money'] += $other_money; $t['all_money'] += $other_money + $basic_money; $t['invalid_no'] += $invalid_no; $t['invalid_meet'] += $invalid_meet; $t['invalid_deposit'] += $invalid_deposit; $t['invalid_sign'] += $invalid_sign; } $t['exten_money'] = round($t['exten_money'], 2); $t['clue_money'] = $t['exten_money'] == 0 || $t['clue_cont'] == 0 ? 0 : round($t['exten_money'] / $t['clue_cont'], 2); $t['valid_money'] = $t['exten_money'] == 0 || $t['valid_cont'] == 0 ? 0 : round($t['exten_money'] / $t['valid_cont'], 2); $t['click_percen'] = $t['exten_show'] == 0 || $t['exten_click'] == 0 ? 0 : round($t['exten_click'] / $t['exten_show'] * 100, 2); $t['valid_percen'] = $t['clue_cont'] == 0 || $t['valid_cont'] == 0 ? 0 : round($t['valid_cont'] / $t['clue_cont'] * 100, 2); $t['meet_percen'] = $t['clue_cont'] == 0 || $t['meet_cont'] == 0 ? 0 : round($t['meet_cont'] / $t['clue_cont'] * 100, 2); $t['deposit_percen'] = $t['meet_cont'] == 0 || $t['deposit_cont'] == 0 ? 0 : round($t['deposit_cont'] / $t['meet_cont'] * 100, 2); $t['meet_money'] = $t['exten_money'] == 0 || $t['meet_cont'] == 0 ? 0 : round($t['exten_money'] / $t['meet_cont'], 2); $t['ROI'] = $t['exten_money'] == 0 || $t['basic_money'] == 0 ? 0 : round($t['exten_money'] / $t['basic_money'] * 100, 2); array_push($soud_list, $t); return json(['code' => 0, 'data' => $soud_list, 'count' => 0, 'msg' => '获取成功']); } public function account_detail() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $team_orgs = orgSubIds($request->empcrm->org_id); $param = $this->request->only(['date', 'source_id', 'org_id', 'employee_id', 'state', 'page' => 1, 'limit' => 17]); if (!$request->isAjax()) { View::assign('date', $param['date']); View::assign('state', $param['state']); View::assign('source_id', !empty($param['source_id']) ? $param['source_id'] : ''); View::assign('employee_id', !empty($param['employee_id']) ? $param['employee_id'] : ''); View::assign('org_id', !empty($param['org_id']) ? $param['org_id'] : ''); return View::fetch(); } $order = isset($param['order']) ? $param['order'] : 'id desc'; $page = intval($param['page']); $limit = intval($param['limit']); //$org_employee = [$empid]; $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id'); //$condition[] = ['org_id', 'in', $team_orgs]; if (!empty($param['employee_id'])) $org_employee = [$param['employee_id']]; if (!empty($param['org_id']) && empty($param['employee_id'])) { $org_employee = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', orgSubIds($param['org_id'])]])->column('id'); } $condition[] = ['employee_id', 'in', $org_employee]; if (!empty($param['source_id'])) $condition[] = ['source_id', '=', $param['source_id']]; //if($param['state']!='线索') $condition[] = ['state', 'not in',Customer::changeState('无效', 'chaos')]; $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')]; if (empty($param['date'])) return json(['code' => 1, 'msg' => '参数错误']); if (!empty($param['date'])) { $newtime = explode('到', $param['date']); } $condition[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]; //$customer_list = Customer::where($condition)->column('id'); $vlst = '确认到店'; if ($param['state'] == '到访') { $vlst = '确认到店'; } elseif ($param['state'] == '交定') { $vlst = '交定'; } elseif ($param['state'] == '签单') { $vlst = '签单'; } elseif ($param['state'] == '线索') { //$condition[] = ['addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]; $clue_contids = Customer::where($condition)->column('id'); $delCondition = [ ['org_id', 'in', orgSubIds($root_id)] ]; if (!empty($param['source_id'])) $delCondition[] = ['source_id', '=', $param['source_id']]; $del_clue_contids = Customer::where($delCondition)->onlyTrashed()->column('id'); $wuxiaoids = CustomerInvalidLog::hasWhere('customer',['source_id'=>$param['source_id']])->where([['CustomerInvalidLog.employee_id', 'in', $org_employee], ['CustomerInvalidLog.source_id', '=', $param['source_id']], ['CustomerInvalidLog.root_id', '=', $root_id], ['CustomerInvalidLog.cus_addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]])->column('CustomerInvalidLog.customer_id'); if (!empty($wuxiaoids)) $clue_contids = array_merge($clue_contids, $wuxiaoids); $clue_contids = array_diff($clue_contids, $del_clue_contids); unset($condition); if (empty($clue_contids)) { return json(['code' => 0, 'data' => [], 'count' => 0]); } $condition = implode(',', $clue_contids); $page = ($page - 1) * 17; $empmod = new Empcrm($this->app); $data = $empmod->cus_recycle($condition, $page, $limit, $order, $root_id); return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]); } elseif ($param['state'] == '有效') { $condition[] = ['state', 'not in', Customer::changeState('待确认', 'chaos')]; //$condition[] = ['died', '<>', 2]; $validids = Customer::where($condition)->column('id'); $wuxiaoids = CustomerInvalidLog::hasWhere('customer',['source_id'=>$param['source_id']])->where([['CustomerInvalidLog.employee_id', 'in', $org_employee], ['CustomerInvalidLog.status', '=', 1], ['CustomerInvalidLog.source_id', '=', $param['source_id']], ['CustomerInvalidLog.root_id', '=', $root_id], ['CustomerInvalidLog.cus_addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]])->column('CustomerInvalidLog.customer_id'); if (!empty($wuxiaoids)) $validids = array_merge($validids, $wuxiaoids); unset($condition); $condition = implode(',', $validids); $page = ($page - 1) * 17; $empmod = new Empcrm($this->app); $data = $empmod->cus_recycle($condition, $page, $limit, $order, $root_id); return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]); } $where[] = ['customer_employee_id', 'in', $org_employee]; if ($vlst == '确认到店') { $where[] = ['state', 'in', array_merge(CustomerVisitLog::changeState($vlst, 'chaos'), CustomerVisitLog::changeState('已到场', 'chaos'), CustomerVisitLog::changeState('已量房', 'chaos'), CustomerVisitLog::changeState('已交定', 'chaos'), CustomerVisitLog::changeState('已签单', 'chaos'))]; } else { $where[] = ['state', 'in', CustomerVisitLog::changeState($vlst, 'chaos')]; } if (!empty($param['date'])) { $b_start = date('Y-m-d H:i:s', (strtotime($newtime[0])-1)); $b_end = date('Y-m-d H:i:s', (strtotime($newtime[1])+86399)); $where[] = ['confirm_date', 'between', [$b_start, $b_end]]; } $vslogid = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id'); $vslogid = !empty($vslogid) ? array_unique($vslogid) : []; if (!in_array($param['state'], ['线索', '有效'])) { unset($condition); $empmod = new Empcrm($this->app); if ($vlst == '确认到店') { $vslogid = !empty(array_unique($vslogid)) ? $empmod->selBeforelog($vslogid, $newtime[0] . ' 00:00:00') : []; } $condition[] = ['id', 'in', $vslogid]; if (!empty($param['source_id'])) $condition[] = ['source_id', '=', $param['source_id']]; $source_cusids = Customer::where($condition)->withTrashed()->column('id'); $wx_where[] = ['customer_id', 'in', $vslogid]; $wx_where[] = ['employee_id', 'in', $org_employee]; $wx_where[] = ['root_id', '=', $root_id]; if (!empty($param['source_id'])) $wx_where[] = ['source_id', '=', $param['source_id']]; $wuxiaoids = CustomerInvalidLog::where($wx_where)->column('customer_id'); if (!empty($wuxiaoids)) $source_cusids = array_unique(array_merge($source_cusids, $wuxiaoids)); unset($condition); $condition = implode(',', $source_cusids); $page = ($page - 1) * 17; //$empmod = new Empcrm($this->app); $data = $empmod->cus_recycle($condition, $page, $limit, $order, $root_id); return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]); } $data = $this->selCustomer($condition, $page, $limit, $order, $root_id); $count = Customer::where($condition)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } public function account_detail_fish() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $team_orgs = orgSubIds($request->empcrm->org_id); $param = $this->request->only(['date', 'source_id', 'org_id', 'employee_id', 'state', 'page' => 1, 'limit' => 17]); if (!$request->isAjax()) { View::assign('date', $param['date']); View::assign('state', $param['state']); View::assign('source_id', !empty($param['source_id']) ? $param['source_id'] : ''); View::assign('employee_id', !empty($param['employee_id']) ? $param['employee_id'] : ''); View::assign('org_id', !empty($param['org_id']) ? $param['org_id'] : ''); return View::fetch(); } if (empty($param['source_id'])) $param['source_id'] = 0; $order = isset($param['order']) ? $param['order'] : 'id desc'; $page = intval($param['page']); $limit = intval($param['limit']); //$org_employee = [$empid]; $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id'); //$condition[] = ['org_id', 'in', $team_orgs]; if (!empty($param['employee_id'])) $org_employee = [$param['employee_id']]; if (!empty($param['org_id']) && empty($param['employee_id'])) { $org_employee = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', orgSubIds($param['org_id'])]])->column('id'); } $condition[] = ['employee_id', 'in', $org_employee]; $tmp_sourceids = CustomerSource::where([['root_id', '=', $root_id], ['source', 'in', ['飞鱼线索','腾讯线索']]])->column('id'); $condition[] = ['source_id', 'in', $tmp_sourceids]; //if($param['state']!='线索') $condition[] = ['state', 'not in',Customer::changeState('无效', 'chaos')]; $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')]; if (empty($param['date'])) return json(['code' => 1, 'msg' => '参数错误']); if (!empty($param['date'])) { $newtime = explode('到', $param['date']); } $condition[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]; //$customer_list = Customer::where($condition)->column('id'); $vlst = '飞鱼确认到店'; if ($param['state'] == '飞鱼到访') { $vlst = '飞鱼确认到店'; } elseif ($param['state'] == '飞鱼交定') { $vlst = '飞鱼交定'; } elseif ($param['state'] == '飞鱼签单') { $vlst = '飞鱼签单'; } if ($param['state'] == '飞鱼有效') { $tmp_validids = FishData::where([['is_allocation', '=', 1], ['type', 'in', [1, 2, 4]], ['root_id', '=', $root_id], ['advertiser_id', '=', $param['source_id']], ['employee_id', 'in', $org_employee]])->column('customer_id'); $condition[] = ['id', 'in', $tmp_validids]; $validids = Customer::where($condition)->column('id'); $wuxiaoids = CustomerInvalidLog::where([['employee_id', 'in', $org_employee], ['status', '=', 1], ['root_id', '=', $root_id], ['cus_addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]])->column('customer_id'); if (!empty($wuxiaoids)) $validids = array_merge($validids, $wuxiaoids); unset($condition); $condition = implode(',', $validids); $page = ($page - 1) * 17; $empmod = new Empcrm($this->app); $data = $empmod->cus_recycle($condition, $page, $limit, $order, $root_id); return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]); } $where[] = ['customer_employee_id', 'in', $org_employee]; if ($vlst == '飞鱼交定') { $state3 = CustomerVisitLog::changeState('已交定', 'chaos'); $validids = FishData::where([['is_allocation', '=', 1], ['type', 'in', [1, 2, 4]], ['root_id', '=', $root_id], ['advertiser_id', '=', $param['source_id']], ['employee_id', 'in', $org_employee]])->column('customer_id'); $where[] = ['customer_id', 'in', $validids]; $where[] = ['state', 'in', $state3]; } elseif ($vlst == '飞鱼签单') { $state3 = CustomerVisitLog::changeState('已签单', 'chaos'); $validids = FishData::where([['is_allocation', '=', 1], ['type', 'in', [1, 2, 4]], ['root_id', '=', $root_id], ['advertiser_id', '=', $param['source_id']], ['employee_id', 'in', $org_employee]])->column('customer_id'); $where[] = ['customer_id', 'in', $validids]; $where[] = ['state', 'in', $state3]; } elseif ($vlst == '飞鱼确认到店') { $state3 = CustomerVisitLog::changeState('已交定', 'chaos'); $state4 = CustomerVisitLog::changeState('已签单', 'chaos'); $state5 = CustomerVisitLog::changeState('确认到店', 'chaos'); $state6 = CustomerVisitLog::changeState('已到场', 'chaos'); $state7 = CustomerVisitLog::changeState('已量房', 'chaos'); $validids = FishData::where([['is_allocation', '=', 1], ['type', 'in', [1, 2, 4]], ['root_id', '=', $root_id], ['advertiser_id', '=', $param['source_id']], ['employee_id', 'in', $org_employee]])->column('customer_id'); $where[] = ['customer_id', 'in', $validids]; $where[] = ['state', 'in', array_merge($state3, $state4, $state5, $state6, $state7)]; } else { $where[] = ['state', 'in', CustomerVisitLog::changeState($vlst, 'chaos')]; } if (!empty($param['date'])) { $where[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]; } $vslogid = CustomerVisitLog::where($where)->column('customer_id'); $vslogid = !empty($vslogid) ? array_unique($vslogid) : []; if (!in_array($param['state'], ['线索', '有效'])) { unset($condition); $empmod = new Empcrm($this->app); if ($vlst == '确认到店') { $vslogid = !empty(array_unique($vslogid)) ? $empmod->selBeforelog($vslogid, $newtime[0] . ' 00:00:00') : []; } $condition[] = ['id', 'in', $vslogid]; if (!empty($param['source_id']) && $vlst != '飞鱼确认到店' && $vlst != '飞鱼交定' && $vlst != '飞鱼签单') { $condition[] = ['source_id', '=', $param['source_id']]; $wx_where[] = ['source_id', '=', $param['source_id']]; } if ($vlst == '飞鱼确认到店' || $vlst == '飞鱼交定' || $vlst == '飞鱼签单') { $condition[] = ['source_id', 'in', $tmp_sourceids]; $wx_where[] = ['source_id', 'in', $tmp_sourceids]; } $source_cusids = Customer::where($condition)->withTrashed()->column('id'); $wx_where[] = ['customer_id', 'in', $vslogid]; $wx_where[] = ['employee_id', 'in', $org_employee]; $wx_where[] = ['root_id', '=', $root_id]; // if (!empty($param['source_id'])) $wx_where[] = ['source_id', '=', $param['source_id']]; $wuxiaoids = CustomerInvalidLog::where($wx_where)->column('customer_id'); if (!empty($wuxiaoids)) $source_cusids = array_unique(array_merge($source_cusids, $wuxiaoids)); unset($condition); $condition = implode(',', $source_cusids); $page = ($page - 1) * 17; //$empmod = new Empcrm($this->app); $data = $empmod->cus_recycle($condition, $page, $limit, $order, $root_id); return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]); } $data = $this->selCustomer($condition, $page, $limit, $order, $root_id); $count = Customer::where($condition)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } //公用查询列表 public function selCustomer($condition, $page, $limit, $order, $root_id) { $data = Customer::with(['employee', 'org', 'designer', 'source']) ->withCount(['visitLog' => function ($query) { $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]); }]) ->where($condition) ->page($page, $limit)->order($order)->select(); $data = $data->visible(['id', 'employee_id', 'name', 'introduce', 'community_name', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'remark', 'crm_res_id', 'agents_id', 'sign_time', 'house_delivery_time', 'first_visit_date', 'ext', 'transfer_info', 'deposit_money', 'signed_money', 'package_id'])->toArray(); $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find(); if (!empty($setting)) { $setting_content = json_decode($setting['content'], true); } else { $setting_content = []; } foreach ($data as &$item) { $item['un_protected'] = false; if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) { $state_n = Customer::changeState($item['state'], 'n'); if (isset($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) { $item['un_protected'] = true; } } if ($item['state'] == '已签单') $item['state'] = '已转单'; if ($item['state'] == '已交定') $item['state'] = '已签单'; $item['square'] = floatval($item['square']); $item['phone'] = substr_replace($item['phone'], '******', 3, 6); $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : ''; $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : ''; $item['jiaofang'] = !empty($item['house_delivery_time']) ? (str_replace('/', '-', $item['house_delivery_time']) <= date('Y-m-d', time()) ? '现房' : $item['house_delivery_time']) : ''; $vlotime = CustomerVisitLog::where([['customer_id', '=', $item['id']], [CustomerVisitLog::changeState(['state', '=', '未到访'])]])->order('addtime desc')->value('addtime'); //$fisttime = CustomerVisitLog::where([['customer_id','=',$item['id']],['customer_employee_id','=',$item['employee_id']],[CustomerVisitLog::changeState(['state', '=', '确认到店'])]])->order('addtime asc')->value('addtime'); $fisttime = CustomerVisitLog::where([['customer_id', '=', $item['id']], [CustomerVisitLog::changeState(['state', '=', '确认到店'])]])->order('addtime asc')->value('addtime'); //$jiaodingtime = CustomerVisitLog::where([['customer_id','=',$item['id']],['customer_employee_id','=',$item['employee_id']],[CustomerVisitLog::changeState(['state', '=', '已交定'])]])->order('addtime desc')->value('addtime'); $jiaodingtime = CustomerVisitLog::where([['customer_id', '=', $item['id']], [CustomerVisitLog::changeState(['state', '=', '已交定'])]])->order('addtime desc')->value('addtime'); $item['sign'] = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['customer_employee_id', '=', $item['employee_id']], [CustomerVisitLog::changeState(['state', '=', '已签单'])]])->find(); $item['fisttime'] = $fisttime ? $fisttime : ''; $item['jiaoding_time'] = $jiaodingtime ? $jiaodingtime : ''; } return $data; } //设计师报表 public function designer_report() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $team_orgs = orgSubIds($request->empcrm->org_id); $param = $this->request->only(['date', 'source_id', 'org_id']); if (!$request->isAjax()) { $org_data = Org::where([['id', 'in', $team_orgs]])->field('id,name')->select(); //->toArray(); View::assign('org_data', $org_data); $data['company_name'] = Company::where('root_id', $root_id)->value('company_name'); $data['empname'] = $request->empcrm->name; View::assign('data', $data); $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray(); View::assign('soudata', $soudata); View::assign('is_manager', $is_manager); return View::fetch(); } // 获取部门列表 $param_sub_org = $team_orgs; if (!empty($param['org_id'])) $param_sub_org = [$param['org_id']]; if (!empty($param['date'])) { $newtime = explode('到', $param['date']); } else { return json(['code' => 1, 'msg' => '日期为空']); } //层级以下所有客户指派的设计师 $designer_where = [ ['org_id', 'in', $param_sub_org], ['designer_id', '>', 0], ['addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']] ]; if (!empty($param['source_id'])) $designer_where[] = ['source_id', '=', $param['source_id']]; $designer_id = Customer::where($designer_where)->group('designer_id')->column('designer_id'); //查询无效后的设计师 $wxdesignerids = CustomerInvalidLog::where([['root_id', '=', $root_id], ['status', '=', 1], ['org_id', 'in', $param_sub_org], ['cus_addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']], ['designer_id', '>', 0]])->group('designer_id')->column('designer_id'); if (!empty($wxdesignerids)) $designer_id = array_unique(array_merge($designer_id, $wxdesignerids)); $designer_list = Employee::where([['id', 'in', $designer_id], ['root_id', '=', $root_id], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray(); // 客户 //$designer_ids = !empty($designer_list) ? array_column($designer_list, 'id') : []; $customer_id = Customer::where([['org_id', 'in', $param_sub_org], ['designer_id', '>', 0]])->column('id'); $where = [ ['addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']], ['id', 'in', $customer_id] ]; if (!empty($param['source_id'])) $where[] = ['source_id', '=', $param['source_id']]; $customer_list = Customer::where($where)->field('id,name,state,source_id,employee_id,designer_id,died')->select()->toArray(); $invalidcus = CustomerInvalidLog::where([ ['cus_addtime', 'between', [$newtime[0].' 00:00:00',$newtime[1].' 23:59:59']], ['designer_id','in',$designer_id], ['root_id','=',$root_id] ])->column('designer_id,employee_id,status'); $t = ['name' => '求和', 'deposit_percen' => 0, 'dai_confirmed' => 0, 'exten_money' => 0, 'exten_show' => 0, 'exten_click' => 0, 'click_percen' => 0, 'clue_conversion' => 0, 'clue_money' => 0, 'valid_money' => 0, 'meet_money' => 0, 'sign_agv_money' => 0, 'ROI' => 0, 'week' => '', 'data' => [], 'clue_cont' => 0, 'valid_cont' => 0, 'valid_percen' => 0, 'meet_cont' => 0, 'meet_percen' => 0, 'deposit_cont' => 0, 'sign_cont' => 0, 'basic_money' => 0, 'other_money' => 0, 'all_money' => 0, 'invalid_no' => 0, 'invalid_yes' => 0, 'invalid_meet' => 0, 'invalid_deposit' => 0, 'invalid_sign' => 0]; foreach ($designer_list as $key => $val) { $dai_confirmed = $exten_money = $exten_show = $exten_click = $clue_cont = $valid = $meet = $deposit = $sig· = $basic_money = $other_money = $invalid_no = $invalid_yes = $invalid_meet = 0; $cusids = []; $vs_where = []; foreach ($customer_list as $k => $v) { if (in_array($val['id'], [$v['employee_id'], $v['designer_id']])) { $cusids[] = $v['id']; $clue_cont++; if ($v['died'] != 2 && !in_array($v['state'], array_merge(Customer::changeState('无效', 'chaos'), Customer::changeState('待确认', 'chaos')), true)) $valid++; if (in_array($v['state'], Customer::changeState('待确认', 'chaos'))) $dai_confirmed++; } } //处理无效的客户还原到员工身上为有效 foreach($invalidcus as $k=>$v){ if(in_array($val['id'],[$v['employee_id'],$v['designer_id']])){ $clue_cont++; if($v['status'] == 1) $valid++; } } // 见面量只统计一次(到场、到店、量房) if (empty($param['source_id'])) { $visilog = CustomerVisitLog::where([ ['employee_id', '=', $val['id']], ['customer_id', 'in', $customer_id], ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']], ['state', 'in', array_merge(CustomerVisitLog::changeState('确认到店', 'chaos'), CustomerVisitLog::changeState('已到场', 'chaos'), CustomerVisitLog::changeState('已量房', 'chaos'))] ])->column('customer_id'); } else { $visilog = CustomerVisitLog::hasWhere('customer',['source_id'=>$param['source_id']]) ->where([ ['CustomerVisitLog.employee_id', '=', $val['id']], ['CustomerVisitLog.customer_id', 'in', $customer_id], ['CustomerVisitLog.confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']], ['CustomerVisitLog.state','in',array_merge(CustomerVisitLog::changeState('确认到店','chaos'), CustomerVisitLog::changeState('已到场','chaos'), CustomerVisitLog::changeState('已量房','chaos'))] ])->column('CustomerVisitLog.customer_id'); } $meet = 0; if (!empty(array_unique($visilog))) { $new = new Empcrm($this->app); $meet = $new->selBeforelog(array_unique($visilog), $newtime[0] . ' 00:00:00'); $meet = count($meet); } $vs_where[] = ['customer_id', 'in', $cusids]; $vs_where[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]; //$valid = CustomerVisitLog::where($vs_where)->where('state', 'in', $now_status)->group('customer_id')->count(); $deposit = CustomerVisitLog::where($vs_where)->where('state', 'in', CustomerVisitLog::changeState('已交定', 'chaos'))->group('customer_id')->count(); $sign = CustomerVisitLog::where($vs_where)->where('state', 'in', CustomerVisitLog::changeState('已签单', 'chaos'))->group('customer_id')->count(); //$meet = CustomerVisitLog::where($vs_where)->where('state', 'in', CustomerVisitLog::changeState('确认到店', 'chaos'))->group('customer_id')->count(); //$basic_money = CustomerVisitLog::where($vs_where)->where('state','in',array_merge(CustomerVisitLog::changeState('已交定','chaos'),CustomerVisitLog::changeState('已签单','chaos')))->sum('money'); $basic_money = CustomerVisitLog::where($vs_where)->where('state', 'in', CustomerVisitLog::changeState('已签单', 'chaos'))->sum('money'); $wuxiao = CustomerVisitLog::where($vs_where)->where('state', 'in', CustomerVisitLog::changeState('无效', 'chaos'))->group('customer_id')->column('customer_id'); $invalid_meet = CustomerVisitLog::where('state', 'in', CustomerVisitLog::changeState('确认到店', 'chaos'))->where([['customer_id', 'in', $wuxiao]])->group('customer_id')->count(); $invalid_deposit = CustomerVisitLog::where('state', 'in', CustomerVisitLog::changeState('已交定', 'chaos'))->where([['customer_id', 'in', $wuxiao]])->group('customer_id')->count(); $invalid_sign = CustomerVisitLog::where('state', 'in', CustomerVisitLog::changeState('已签单', 'chaos'))->where([['customer_id', 'in', $wuxiao]])->group('customer_id')->count(); $designer_list[$key]['clue_cont'] = $clue_cont; $designer_list[$key]['valid_cont'] = $valid; $designer_list[$key]['valid_percen'] = $valid && $clue_cont ? round($valid / $clue_cont * 100, 2) : 0; $designer_list[$key]['dai_confirmed'] = $dai_confirmed; $designer_list[$key]['meet_cont'] = $meet; $designer_list[$key]['meet_percen'] = !empty($meet) && !empty($clue_cont) ? round($meet / $clue_cont * 100, 2) : 0; $designer_list[$key]['deposit_cont'] = $deposit; $designer_list[$key]['deposit_percen'] = !empty($deposit) && !empty($meet) ? round($deposit / $meet * 100, 2) : 0; $designer_list[$key]['sign_cont'] = $sign; $designer_list[$key]['basic_money'] = $basic_money; $designer_list[$key]['sign_agv_money'] = !empty($sign_cont) && !empty($basic_money) ? floor($basic_money / $sign_cont) : 0; $designer_list[$key]['other_money'] = 0; $designer_list[$key]['all_money'] = $basic_money; //$xin[$key]['invalid_no'] = $invalid_no; //$xin[$key]['invalid_yes'] = $invalid_yes; $designer_list[$key]['invalid_meet'] = $invalid_meet; $designer_list[$key]['invalid_deposit'] = $invalid_deposit; $designer_list[$key]['invalid_sign'] = $invalid_sign; //总数 $t['clue_cont'] += $clue_cont; $t['valid_cont'] += $valid; // $t['valid_percen'] += !empty($valid) && !empty($clue_cont) ? round($valid/$clue_cont*100,2) : 0; //$t['valid_percen'] += $designer_list[$key]['valid_percen']; $t['dai_confirmed'] += $dai_confirmed; $t['meet_cont'] += $meet; // $t['meet_percen'] += !empty($meet) && !empty($clue_cont) ? round($meet/$clue_cont*100,2) : 0; $t['deposit_cont'] += $deposit; // $t['deposit_percen'] += !empty($deposit) && !empty($meet) ? round($deposit/$meet*100,2) : 0; $t['sign_cont'] += $sign; $t['basic_money'] += $basic_money; $t['sign_agv_money'] += !empty($sign_cont) && !empty($basic_money) ? floor($basic_money / $sign_cont) : 0; $t['other_money'] += 0; $t['all_money'] += $basic_money; // $t['invalid_no'] += $invalid_no; // $t['invalid_yes'] += $invalid_yes; $t['invalid_meet'] += $invalid_meet; $t['invalid_deposit'] += $invalid_deposit; $t['invalid_sign'] += $invalid_sign; } $t['valid_percen'] = $t['clue_cont'] == 0 ? 0 : round($t['valid_cont'] / $t['clue_cont'] * 100, 2); $t['meet_percen'] = $t['clue_cont'] == 0 ? 0 : round($t['meet_cont'] / $t['clue_cont'] * 100, 2); $t['deposit_percen'] = $t['meet_cont'] == 0 ? 0 : round($t['deposit_cont'] / $t['meet_cont'] * 100, 2); array_push($designer_list, $t); return json(['code' => 0, 'data' => $designer_list, 'count' => 0, 'msg' => '获取成功']); } //校对 public function proofread() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $team_orgs = orgSubIds($request->empcrm->org_id); $param = $this->request->only(['phone', 'org_id', 'source_id', 'channel_id']); $is_manager = request()->empcrm->is_manager; if (!$request->isAjax()) { $data['company_name'] = Company::where('root_id', $root_id)->value('company_name'); $data['empname'] = $request->empcrm->name; View::assign('data', $data); $all_orgs = orgSubIds($request->empcrm->root_id); $org_list = Org::where('id', 'in', $all_orgs)->field('id,name')->select()->toArray(); View::assign('org_list', $org_list); $source_list = CustomerSource::where('root_id', $root_id)->column('id,source'); View::assign('source_list', $source_list); View::assign('root_id', $root_id); return View::fetch(); } if (empty($param['phone'])) return json(['code' => 1, 'data' => '', 'msg' => '手机号为空']); $pharr = explode(',', $param['phone']); foreach ($pharr as $key => $val) { $xin[] = cypherphone(trim($val)); } if (!empty($param['org_id'])) { $team_orgs = orgSubIds($param['org_id']); } if (empty($param['org_id'])) { $team_orgs = orgSubIds($request->empcrm->root_id); } //$where[] = ['phone|phone1|phone2', 'in', $xin]; $where[] = ['phone', 'in', $xin]; $where[] = ['org_id', 'in', $team_orgs]; $fish_where[] = ['telephone', 'in', $pharr]; $fish_where[] = ['org_id', 'in', $team_orgs]; if (empty($param['channel_id'])) { if (!empty($param['source_id'])) $where[] = ['source_id', '=', $param['source_id']]; $customer_list = Customer::with(['source' => function ($query) { $query->field('id,source'); }, 'employee' => function ($query) { $query->field('id,name'); }, 'org'])->where($where)->field('id,name,phone,phone1,phone2,community_name,square,ext,house_status,source_id,state,employee_id,is_resource,org_id,sign_time')->select()->toArray(); $visit_state = [7=>'已到店', 8=>'已量房', 9=>'已到场', 2=>'已签单', 3=>'已转单', 15=>'已卖卡']; foreach ($customer_list as $key => $val) { if ($val['state'] == '无效') { $customer_employee_id = CustomerVisitLog::where([['customer_id', '=', $val['id']], ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')]])->order('addtime desc')->value('customer_employee_id'); $customer_list[$key]['employee'] = $customer_employee_id ? Employee::where([['root_id', '=', $root_id], ['id', '=', $customer_employee_id]])->field('id,name')->find() : ''; } //不是管理员则无法查看详情 if ($is_manager == 0) { $customer_list[$key]['id'] = 0; if ($val['employee_id'] == $empid) { $customer_list[$key]['id'] = $val; } } // 查询客户状态 $customerState = CustomerVisitLog::where(['customer_id'=>$val['id']])->group('state')->column('state'); $customer_list[$key]['state'] = ''; $stateForCustomer = []; foreach($customerState as $s) { if(isset($visit_state[$s])) $stateForCustomer[] = $visit_state[$s]; } $customer_list[$key]['state'] = !empty($stateForCustomer) ? implode(',', $stateForCustomer) : Customer::changeState($val['state'], 'state'); } $xin = $nopr = $pr = []; foreach ($customer_list as $k => $v) { $v['phone1'] = !empty($v['phone1']) ? substr_replace($v['phone1'], '****', 3, 4) : ''; $v['phone2'] = !empty($v['phone2']) ? substr_replace($v['phone2'], '****', 3, 4) : ''; $xin[] = ['data' => $v, 'phone' => $v['phone']]; $pr[] = $v['phone']; // if(!empty($v['phone1'])){ // $pr[] = $v['phone1']; // } // if(!empty($v['phone2'])){ // $pr[] = $v['phone2']; // } } foreach ($pharr as $key => $val) { if (!in_array($val, $pr)) { $nopr[] = ['data' => null, 'phone' => $val]; } } //查询回收站是否存在 foreach ($nopr as $key => $val) { $nopr[$key]['data'] = CustomerRecycle::with(['source' => function ($query) { $query->field('id,source'); }, 'org'])->where([['org_id', 'in', $team_orgs], ['phone', '=', cypherphone($val['phone'])]]) ->field('id,name,phone,phone1,phone2,community_name,square,ext,house_status,source_id,state,employee_id,is_resource,org_id')->find(); } $new = array_merge($xin, $nopr); // 按照校验手机号排序 $new_list = []; $pharr = array_unique($pharr); foreach ($pharr as $k => $v) { foreach ($new as $kk => $vv) { if ($vv['phone'] == $v) { $new_list[] = $vv; } } } } else { //,phone1,phone2,community_name,square,ext,house_status,source_id,state,employee_id,is_resource,org_id,sign_time $new_list = []; $fish_list = FishData::with(['org'])->where($fish_where)->field('name,telephone,location,clue_state,clue_owner_name,advertiser_name,create_time_detail,is_allocation,org_id')->select()->toArray(); foreach ($pharr as $k => $v) { $new_list[$k]['phone'] = $v; $new_list[$k]['data'] = ''; foreach ($fish_list as $ky => $vy) { if ($vy['telephone'] == $v) { $tmp_val = [ 'community_name' => $vy['location'], 'square' => '未知', 'house_status' => '未知', 'employee' => ['name' => $vy['clue_owner_name']], 'source' => ['source' => $vy['advertiser_name']], 'sign_time' => $vy['create_time_detail'], 'state' => $vy['is_allocation'] == 1 ? '已转化' : '未转化', 'org_name' => $vy['org_name'], ]; $new_list[$k]['data'] = $tmp_val; } } } } return json(['code' => 0, 'data' => $new_list, 'msg' => '获取成功']); } /* * 人员树 */ public function get_person() { $root_id = request()->empcrm->root_id; //树形 $where = [ ['path', 'like', $root_id . '-%'], ['status', '=', 1] ]; $allnodes = Org::where($where)->field('id value,id,pid,name')->order('level asc, id asc')->select()->toArray(); $tree = $this->treeV2($allnodes, 0); return json($tree); } private function treeV2($data, $pid = 0) { $new_arr = []; foreach ($data as $k => $v) { if ($v['pid'] == $pid) { $children = $this->treeV2($data, $v['id']); $v['children'] = $children; $new_arr[] = $v; } } return $new_arr; } /** * 获取org ids array * @param integer $orgid 目标节点id * @return array $orgids 子节点ids数组 */ function designer_orgid($orgid) { $theorg = Org::find($orgid); if ($theorg) { $orgids = Org::where([['path', 'like', $theorg->path . '%'], ['org_type', '=', 2]])->column('id'); return $orgids; } else { return []; } } //资源库管理 public function resource() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $org_id = $request->empcrm->org_id; $is_manager = $request->empcrm->is_manager; if (!$request->isAjax()) { //公海 $pool = PoolLogic::struc(); View::assign('pool', $pool); // 部门 $org = OrgLogic::struc($root_id); View::assign('org', $org); $orgids = orgSubIds($root_id); View::assign('orgids', json_encode($orgids)); View::assign('empid', $empid); // View::assign('manager', $request->employee->is_manager); $data = CustomerSource::field('id,source')->where(['root_id' => $root_id])->select(); View::assign('source', $data); return View::fetch(); } $param = $request->only(['page', 'limit', 'org_pool', 'state' => '', 'source' => '', 'person' => '', 'start_date' => '', 'end_date' => '']); if (isset($param['org_pool']) && $param['org_pool']) { $org = $param['org_pool']; } else { $org = $root_id; } // $where[] = ['id','>',0]; $where[] = ['org_id', '=', $org_id]; // $where1[] = ['employee_id','=',$empid]; // $where1[] = ['upload_source','=',1]; // $whereOr[] = ['org_id', '=', $org_id]; // $whereOr[] = ['employee_id', 'NULL',null]; //分配状态 if ($param['state'] !== '') $where[] = ['state', '=', $param['state']]; //来源渠道 if ($param['source'] !== '') $where[] = ['source', '=', $param['source']]; // 导入人员 if ($param['person'] !== '') $where[] = ['employee_id', '=', $param['person']]; //开始时间 if ($param['start_date'] !== '') $where[] = ['addtime', '>=', $param['start_date']]; //结束时间 if ($param['end_date'] !== '') $where[] = ['addtime', '<=', $param['end_date'] . ' 23:59:59']; //$data = CrmImportLog::where($where)->whereOr($whereOr)->with(['employee', 'org', 'optname'])->page($param['page'])->limit($param['limit'])->order('addtime', 'desc')->select(); $data = CrmImportLog::where($where) // ->where( // function($query) use ($whereOr,$where1){ // $query->whereOr([$whereOr,$where1]); // } // ) ->with(['employee', 'org', 'optname'])->page($param['page'])->limit($param['limit'])->order('addtime', 'desc')->select(); //获取所有资源id $listids = CrmImportLog::where('id', '>', 0)->column('pid,id'); foreach ($data as &$item) { if ($item->pid == 0) { $from = '数据导入'; } else { $cil = CrmImportLog::with('org')->find($item->pid); $from = $cil->org_info . ' 资源库'; } $item['from'] = $from; $item['belong'] = $item->org_info; if (empty($item['pid'])) $item['employee_name'] = $item['employee_opt_name'] ?: $item['employee_name']; if ($item['pid']) { $pidarr = $this->getInfo($item['id'], $listids, 0); $ppid = !empty($pidarr[count($pidarr) - 1]['id']) ? $pidarr[count($pidarr) - 1]['id'] : 0; $pid_data = CrmImportLog::with(['employee', 'optname'])->where('id', '=', $ppid)->find(); $item['employee_name'] = !empty($pid_data['employee_name']) ? $pid_data['employee_name'] : $pid_data['employee_opt_name']; } $item['source_name'] = ''; if ($item['source']) { $source_name = CustomerSource::where('id', intval($item['source']))->value('source'); if ($source_name) { $item['source_name'] = $source_name; } else { $item['source_name'] = '其他'; } } $where1[] = ['employee_id', '=', NULL]; $where1[] = ['crm_res_id', '=', $item['id']]; $ispid = !empty($item['pid']) ? 1 : 0; $where1[] = ['is_distribution', '=', $ispid]; $where1[] = ['state', 'not in', Customer::changeState('无效', 'chaos')]; $left_num = Customer::where($where1)->count(); unset($where1); $item['left_num'] = $left_num; if ($item['pid']) $item['y_count'] = $item['count'] - $item['left_num']; if (empty($item['pid'])) $item['y_count'] = $item['avaliable_count'] - $item['left_num']; $item['state_name'] = $item['state'] == 0 ? '未分配' : '已分配'; } $count = CrmImportLog::where($where) // ->where( // function($query) use ($whereOr,$where1){ // $query->whereOr([$whereOr,$where1]); // } // ) ->count(); //查询资源库分组是否是最高层 $org_pid = Org::where('id', $org)->value('pid'); return json(['code' => 0, 'data' => $data, 'count' => $count, 'org' => $org_pid]); } //添加导入资源 public function importing() { $data = CustomerSource::field('id,source')->where(['root_id' => request()->empcrm->root_id])->select(); View::assign('source', $data); $old_source = CrmImportLog::where('employee_id', request()->empcrm->id)->order('id desc')->value('source'); View::assign('old_source', $old_source); return View::fetch(); } private function checkphone($value) { if (preg_match("/^1[356789]\d{9}$/", $value)) { return true; } else { return false; } } /** * 导入前日志存储 */ public function importLog() { $request = request(); $param = $request->only(['file_name', 'source', 'file']); $empid = $request->empcrm->id; $org_id = $request->empcrm->org_id; $root_id = $request->empcrm->root_id; $logData = [ 'name' => $param['file_name'], 'employee_id' => $empid, 'org_id' => $org_id, 'count' => 0, 'avaliable_count' => 0, 'bad_phone_num' => 0, 'left_num' => 0, 'price' => '', 'source' => $param['source'], 'repeat_phone_number' => 0, 'path'=> $param['file'], 'root_id'=> $root_id, 'upload_source' => 1 ]; $data = CrmImportLog::create($logData); if ($data == false) { return json(['code' => 1, 'msg' => '添加失败']); } $data->status = 1; $data->save(); $jobHandlerClassName = 'app\jobs\CrmImport'; $jobQueueName = 'crm_import'; $orderData = ['id'=> $data->id]; //这个是需要传到消费者的数据 Queue::later(0, $jobHandlerClassName, $orderData, $jobQueueName); return json(['code' => 0, 'msg' => '正在导入中,请刷新列表查看']); } /** * 获取导入失败的客户 */ public function get_error_customers(){ $param = request()->only(['page' => 1, 'limit' => 10, 'keyword' => '', 'id' => 0]); $list = CrmImportRecord::where([['log_id', '=', $param['id']], ['content', 'not null', null]])->order('id asc')->column('content'); $new_list = []; foreach($list as $k => $v){ if (!empty($v)) { $one = json_decode($v); $new_list = array_merge($new_list, $one); } } $count = count($new_list); $data = array_slice($new_list, ($param['page'] - 1) * $param['limit'], $param['limit']); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 客户excel添加 */ public function add() { $request = request(); $orgIds = orgSubIds($request->empcrm->root_id); $logId = $request->param('log_id'); $sourceId = $request->param('source_id'); $log = CrmImportLog::where('id', $logId)->find(); if (empty($log) || !in_array($log->org_id, $orgIds)) return json(['code' => 1, 'msg' => '请求错误,记录不存在']); $orgIds = orgSubIds($request->empcrm->root_id); $datas = $request->param('customers'); if (count($datas) == 0) return json(['code' => 1, 'msg' => '请求错误,未传输数据']); //客户来源扩展字段 $ext_where = [ ['root_id', '=', $request->empcrm->root_id], ['keyname', '=', 'source_id'] ]; $ext = []; $source_field_id = CustomerPortraitField::where($ext_where)->value('id'); if ($source_field_id) $ext[] = ['id' => (string)$source_field_id, 'keyname' => 'source_id', 'value' => $sourceId]; $unit_number_id = CustomerPortraitField::where([['root_id', '=', $request->empcrm->root_id], ['keyname', '=', 'unit_number']])->find(); $house_location = CustomerPortraitField::where([['root_id', '=', $request->empcrm->root_id], ['keyname', '=', 'house_location']])->find(); //error 手机号错误数量 repeat 手机号重复数量 $rs = ['ok' => 0, 'error' => 0, 'repeat' => 0]; $phones = []; $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); foreach ($datas as $k => &$data) { $data['phone1'] = $data['phone2'] = ''; ////2023-02-15 修改手机号可以输入多个 $s = true; $ls_phone = explode('、', $data['phone']); if (!empty($ls_phone) && is_array($ls_phone)) { $p = 0; foreach ($ls_phone as $k2 => $v2) { if ($this->checkphone($v2)) { $field = $p ? 'phone' . $p : 'phone'; $data[$field] = $aec->encrypt($v2); $p += 1; } if ($p > 2) break; //只取三个有效手机号 } if ($p > 0) $s = false; } if ($s) { unset($datas[$k]); $rs['error']++; continue; } // 数据处理 $data['house_type'] = mb_strlen($data['house_type']) > 20 ? mb_substr($data['house_type'], 0, 20) : trim($data['house_type']); $data['age_range'] = mb_strlen($data['age_range']) > 5 ? mb_substr($data['age_range'], 0, 5) : trim($data['age_range']); $data['name'] = mb_strlen($data['name']) > 20 ? mb_substr($data['name'], 0, 20) : trim($data['name']); $data['crm_res_id'] = $log->id; $data['state'] = 0; $data['sex'] = ($data['sex'] == '男') ? 1 : 2; $data['org_id'] = $request->empcrm->org_id; $data['is_resource'] = 1; $data['bad_phone'] = 0; $data['source_id'] = $sourceId; $ext1 = $ext; if ($data['unit_number'] && !$unit_number_id->isEmpty()) $ext1[] = ['id' => (string)$unit_number_id->id, 'keyname' => 'unit_number', 'value' => $data['unit_number']]; if ($data['house_location'] && !$house_location->isEmpty()) $ext1[] = ['id' => (string)$house_location->id, 'keyname' => 'house_location', 'value' => $data['house_location']]; $data['ext'] = $ext1 ? json_encode($ext1) : NULL; unset($data['unit_number']); unset($data['house_location']); $data['level'] = in_array($data['level'], ['A', 'B', 'C', 'D']) ? trim($data['level']) : null; $rs['ok']++; } Db::startTrans(); try { Customer::insertAll($datas); $log->bad_phone_num -= $rs['ok']; $log->avaliable_count += $rs['ok']; $log->left_num += $rs['ok']; $log->save(); Db::commit(); } catch (\Exception $e) { trace($e->getMessage(), 'error'); // 回滚事务 Db::rollback(); return json(['code' => 1, 'msg' => $e->getMessage()]); } return json(['code' => 0, 'msg' => '保存成功', 'data' => $rs]); } /** * 获取公司人员 * 分配弹框页面接口 */ public function get_persons() { $request = request(); $root_id = $request->empcrm->root_id; $org_id = $request->empcrm->org_id; $param = $request->only(['page' => 1, 'limit' => 10000, 'keyword' => '', 'type' => 1, 'ids' => '']); $orgids = orgSubIds($org_id); $where = [ ['root_id', '=', $root_id], ['org_id', 'in', $orgids], ['uid', '>', 0], ['state', '=', '在职'] ]; if ($param['type'] == 1 && $param['keyword']) { $benorg = Org::where('id', $org_id)->value('path'); $org_ids = Org::where([['path', 'like', $benorg . '%'], ['id', '<>', $org_id], ['name', 'like', '%' . $param['keyword'] . '%']])->column('id'); $where[] = ['org_id', 'in', $org_ids]; } elseif ($param['type'] == 2 && $param['keyword']) { $where[] = ['name', 'like', '%' . $param['keyword'] . '%']; } $list = Employee::with(['org' => function ($query) { $query->field(['id', 'name'])->bind(['org_name' => 'name']); }])->where($where)->field('id,org_id,name')->page($param['page'], $param['limit'])->order('id asc')->select(); $count = Employee::where($where)->count(); if ($param['ids']) { $arr = explode(',', $param['ids']); foreach ($list as $k => $v) { $list[$k]['checked'] = in_array($v['id'], $arr); } } else { foreach ($list as $k => $v) { $list[$k]['checked'] = false; } } return json(['code' => 0, 'data' => $list, 'count' => $count]); } /** * 删除资源 */ public function delete_new() { $id = input('id', 0); $root_id = request()->empcrm->root_id; $empid = request()->empcrm->id; $orgids = orgSubIds($root_id); if ($id > 0) { $where[] = ['id', '=', $id]; $where[] = ['org_id', 'in', $orgids]; $info = CrmImportLog::where($where)->findOrEmpty(); $listids = CrmImportLog::where([['id', '>', $info['id']], ['org_id', 'in', $orgids]])->column('id,pid'); $ids = $this->getAllLowerLevelId($listids, $info['id']); $info->delete(); CrmImportLog::where([['id', 'in', $ids]])->useSoftDelete('delete_time', time())->delete(); //记录操作删除资源库信息 $msg = '操作回收客户员工id:' . $empid . '要删除资源库的id' . $id; trace($msg,'资源删除'); return json(['code' => 0, 'data' => '删除成功', 'msg' => '删除成功']); // if ($info->state == 0) { // $info->delete(); // CrmImportLog::where([['id', 'in', $ids]])->useSoftDelete('delete_time', time())->delete(); // //CrmImportLog::where($where)->delete(); // if ($info->count > 0) Customer::where([['crm_res_id', '=', $id]])->useSoftDelete('delete_time', time())->delete(); // //记录操作删除资源库信息 // $msg = '操作回收客户员工id:' . $empid . '要删除资源库的id' . $id; // Log::write($msg); // return json(['code' => 0, 'data' => '删除成功', 'msg' => '删除成功']); // } else { // $iscus = Customer::where([['crm_res_id', 'in', $ids], ['org_id', 'in', $orgids], ['state', 'in', Customer::changeState('待确认', 'chaos')]])->count(); // $bencus = Customer::where([['crm_res_id', '=', $info['id']], ['org_id', 'in', $orgids], ['employee_id', 'NOTNULL', null], ['state', 'in', Customer::changeState('待确认', 'chaos')]])->count(); // if ($iscus && $bencus) return json(['code' => 1, 'msg' => '请先回收未转化为客户的资源']); // //删除已经回收的客户 // //$isdelcus = Customer::where([['crm_res_id','=',$info['id']],['state','in',Customer::changeState('待确认','chaos')]])->delete(); // $info->delete(); // CrmImportLog::where([['id', 'in', $ids]])->useSoftDelete('delete_time', time())->delete(); // return json(['code' => 0, 'data' => '删除成功', 'msg' => '删除成功']); // } } return json(['code' => 1, 'data' => '删除失败', 'msg' => '删除失败']); } /** * 获取部门树 * 分配弹框页面接口 */ public function get_orgs() { $root_id = request()->empcrm->root_id; $keyword = input('keyword', ''); $pids = input('pid', 0); //获取本人部门及子部门 $org_id = request()->empcrm->org_id; $org_data = Org::find($org_id); $where = [ ['path', 'like', $org_data->path . '%'], ['status', '=', 1] ]; if ($pids) $where[] = ['id', '<>', $org_id]; $count = Employee::where([['root_id', '=', $root_id], ['state', 'like', '%在职%'], ['uid', '>', 0]])->group('org_id')->column('count(*) count', 'org_id'); $allnodes = Org::where($where)->field('id,pid,name title,level,org_type,info,path')->order('level asc, id asc')->select()->toArray(); foreach ($allnodes as $k => $v) { $allnodes[$k]['count'] = isset($count[$v['id']]) ? $count[$v['id']] : 0; //本部门人数 $allnodes[$k]['all_count'] = 0; //包含子部门总数 $allnodes[$k]['org_class'] = 'org_' . $v['id']; $allnodes[$k]['per_class'] = 'per_org_' . $v['id']; $allnodes[$k]['type'] = 'org'; foreach ($allnodes as $k2 => $v2) { if (strpos($v2['path'], $v['path']) !== false) { $allnodes[$k]['all_count'] = isset($count[$v2['id']]) ? $count[$v2['id']] + $allnodes[$k]['all_count'] : $allnodes[$k]['all_count']; } } $allnodes[$k]['title'] = $allnodes[$k]['title'] . ' (' . $allnodes[$k]['all_count'] . ')'; } // var_dump($allnodes); // exit; //$newpid = $pids ? $org_data->id : $org_data->pid; $tree = $this->tree($allnodes, $org_data->id); return json(['code' => 0, 'data' => $tree, 'checkOrg' => '']); } public function tree($data, $pid = 0) { $new_arr = []; foreach ($data as $k => $v) { if ($v['pid'] == $pid) { $children = $this->tree($data, $v['id']); $v['children'] = $children; if (empty($v['children'])) $v['disabled'] = true; $new_arr[] = $v; } } return $new_arr; } //资源库迭代新增接口 /** * */ public function distribution() { $root_id = request()->empcrm->root_id; $orgids = orgSubIds($root_id); $id = input('id', 0); //资源id $count = input('count', 0); //可分配资源数量 $customer_id = input('customer_id', ''); $data = CrmImportLog::where([['id', '=', $id], ['org_id', 'in', $orgids]])->find(); $fen = $this->sel_ywassign($id, $orgids, $data); if (empty($customer_id)) $count = $fen['left_num']; View::assign('id', $id); View::assign('pid', $data['pid']); View::assign('count', $count); View::assign('customer_id', $customer_id); return View::fetch(); } /** * 获取子部门 */ public function get_child_orgs($id) { $info = Org::where('id', $id)->findOrEmpty(); $path = $info->path; $ids = Org::where([['path', 'like', $path . '%']])->column('id,name'); return json(['code' => 0, 'data' => array_column($ids, 'id'), 'param' => $ids]); } /** * 分配资源 * 分配弹框页面接口 * type=org分配给部门,person分配给员工, * resources_id批量分配时资源id ,必传 * content 被分配部门id/员工id和数量 [{con_id:1,count:2},{con_id:89,count:4}] * customer_id 手动分配时选择的客户id */ public function distribution_resources() { $request = request(); $root_id = $request->empcrm->root_id; $orgids = orgSubIds($root_id); $param = $request->only(['type' => 'org', 'resources_id' => 0, 'content' => [], 'customer_id' => '']); if (empty($param['content']) || !is_array($param['content'])) return json(['code' => 1, 'data' => '请选择分配对象', 'msg' => '请选择分配对象']); $customer = $param['customer_id'] ? explode(',', $param['customer_id']) : Customer::where([['crm_res_id', '=', $param['resources_id']], ['employee_id', '=', null], ['bad_phone', '=', 0], ['is_resource', '=', 1]])->column('id'); shuffle($customer); //随机分配 $count = count($customer); $all_count = array_sum(array_column($param['content'], 'count')); if ($all_count > $count) return json(['code' => 1, 'data' => '分配数量不能大于资源总数', 'msg' => '分配数量不能大于资源总数']); // 查询老记录 $metaLog = CrmImportLog::find($param['resources_id']); $repeat = []; //指派类型 if ($param['type'] == 'org') { foreach ($param['content'] as $k => $v) { $con = array_slice($customer, 0, $v['count']); $param['content'][$k]['customer'] = $con; $customer = array_diff($customer, $con); } //指派到部门 foreach ($param['content'] as $key => $val) { if ($val['count'] > 0) { $log = CrmImportLog::create([ 'pid' => $param['resources_id'], 'employee_id' => NULL, 'org_id' => $val['con_id'], 'name' => $metaLog->name, 'count' => $val['count'], 'state' => 0, 'left_num' => $val['count'], 'source' => $metaLog->source ]); $where[] = ['id', 'in', $val['customer']]; Customer::where($where)->update([ 'employee_id' => NULL, 'org_id' => $val['con_id'], 'is_resource' => 1, 'crm_res_id' => $log->id, 'is_distribution' => 1 ]); unset($where); } } } elseif ($param['type'] == 'person') { //分配到人去重 $eids = array_column($param['content'], 'con_id'); $phones = Customer::where([['employee_id', 'in', $eids]])->column('employee_id,name,phone,phone1,phone2'); $phone_emp = []; //待分配的业务员 foreach ($phones as $k => $v) { $phone_emp[$v['employee_id']] = isset($phone_emp[$v['employee_id']]) ? $phone_emp[$v['employee_id']] : []; array_push($phone_emp[$v['employee_id']], $v['phone'], $v['phone1'], $v['phone2']); } foreach ($phone_emp as $k2 => $v2) { $phone_emp[$k2] = array_unique(array_filter($v2)); } foreach ($param['content'] as $k3 => $v3) { //原有手机号 $param['content'][$k3]['phones'] = isset($phone_emp[$v3['con_id']]) ? $phone_emp[$v3['con_id']] : []; //分配的客户 $param['content'][$k3]['cids'] = []; } //分配 $customer = Customer::where([['id', 'in', $customer], ['employee_id', '=', null]])->column('id,name,phone,phone1,phone2'); foreach ($customer as $k4 => $v4) { $ls_phones = array_filter([$v4['phone'], $v4['phone1'], $v4['phone2']]); $i = 0; foreach ($param['content'] as $k5 => $v5) { //没有分配完成 if ($v5['count'] > count($v5['cids'])) { //并且手机号不重复 if ($v5['phones'] == []) { $param['content'][$k5]['cids'][] = $v4['id']; $i = 1; break; } else { $phone_diff = array_intersect($v5['phones'], $ls_phones); if ($phone_diff) { continue; //手机号重复 分配给下一个客户 } else { $param['content'][$k5]['cids'][] = $v4['id']; $i = 1; break; } } } } //无法分配 if ($i == 0) { $repeat[$v4['id']] = $v4['name']; } } //分配 $visitlog = []; $date = date('Y-m-d'); foreach ($param['content'] as $k6 => $v6) { if ($v6['cids']) { $emp = Employee::where('id', $v6['con_id'])->findOrEmpty(); Customer::where([['id', 'in', $v6['cids']]])->update([ 'employee_id' => $v6['con_id'], 'org_id' => $emp->org_id, 'is_resource' => 0, 'is_distribution' => 1, 'employee_time' => date('Y-m-d H:i:s'), 'remark' => '', 'valid_time' => null, 'state' => 0 ]); CustomerVisitLog::where([['customer_id', 'in', $v6['cids']], ['data_type', '=', 'out_call']])->delete(); OutCallLog::where([['customer_id', 'in', $v6['cids']]])->update(['is_redistribution' => 1]); OutCallMbLog::where([['customer_id', 'in', $v6['cids']]])->update(['is_redistribution' => 1]); //添加跟进记录 foreach ($v6['cids'] as $v7) { $visitlog[] = [ 'customer_id' => $v7, 'type' => 1, 'remark' => '资源库分配', 'employee_id' => $v6['con_id'], 'user_id' => $emp->uid, 'state' => '待确认', 'next_contact_date' => $date ]; } } } if ($visitlog) CustomerVisitLog::insertAll($visitlog); } else { return json(['code' => 1, 'data' => '数据错误', 'msg' => '数据错误']); } //查询未分配数量 $left_num = Customer::where([['crm_res_id', '=', $param['resources_id']], ['employee_id', '=', null]])->count(); CrmImportLog::where('id', $param['resources_id'])->update(['state' => 1, 'left_num' => $left_num]); $j = 0; if ($param['type'] == 'person') { foreach ($param['content'] as $v7) { if ($v7['count'] > count($v7['cids'])) $j = 1; } } if ($repeat && $j == 1) { $name = implode(',', array_values($repeat)); return json(['code' => 1, 'data' => '客户:' . $name . '。手机号重复无法分配', 'msg' => '客户:' . $name . '。手机号重复无法分配']); } else { return json(['code' => 0, 'data' => '分配完成', 'msg' => '分配完成']); } } /** * 已分配客户筛选多种状态处理 */ public function cus_arrstatus($state, $where) { $arr_state = explode(',', $state); $allids = []; $newids = []; $cids = Customer::where($where)->column('id'); foreach ($arr_state as $key => $val) { $arr = ['已到店', '已量房', '已到场', '已交定', '已签单']; $v_where[] = ['customer_id', 'in', $cids]; if (in_array($val, $arr)) { $v_where[] = ['state', 'in', CustomerVisitLog::changeState($val, 'chaos')]; $ids = CustomerVisitLog::where($v_where)->group('customer_id')->column('customer_id'); unset($v_where); $allids[] = $ids; } elseif ($val == '待确认') { //排除有效就是待确认 $ids = Customer::where($where)->where('state', 'in', Customer::changeState('待确认', 'chaos'))->column('id'); $allids[] = $ids; } elseif ($val == '有效') { //2023-02-02修改 有效状态 客户表state字段不为待确认和无效,就是有效 $state1 = Customer::changeState('待确认', 'chaos'); $state2 = Customer::changeState('无效', 'chaos'); $allids[] = Customer::where($where)->where('state', 'not in', array_merge($state1, $state2))->column('id'); } elseif ($val == '无效') { $state = Customer::changeState('无效', 'chaos'); $allids[] = Customer::where($where)->where('state', 'in', $state)->column('id'); } } foreach ($allids as $key => $val) { foreach ($val as $k => $v) { $newids[] = $v; } } $newids = array_unique($newids); return $newids; } /** * 查询所有资源的下级 */ function getAllLowerLevelId($list, $p_id = '0', $isFirstTime = true) { static $arr = []; if ($isFirstTime) { $arr = []; } foreach ($list as $key => $val) { if ($val['pid'] == $p_id) { $arr[] = $val['id']; $this->getAllLowerLevelId($list, $val['id'], false); } } return $arr; } /** * 查询所有资源的上级 */ function getParentIds($id, $list, $parents = []) { //改变数组的key值 $list = array_column($list, NULL, 'id'); if (!isset($list[$id])) { return []; } $parents[] = $id; $info = $list[$id]; if ($info['pid'] == 0) { return $parents; } return $this->getParentIds($info['pid'], $list, $parents); } /** * 已分配客户列表 * 分配弹框页面接口 */ public function get_customers() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $param = $request->only(['page' => 1, 'limit' => 10, 'keyword' => '', 'type' => 1, 'id' => 0, 'keyword3' => '', 'state' => '', 'recovery' => 0, 'ids' => '', 'call_state', 'pid' => 0]); $orgids = orgSubIds($root_id); //分配记录 //$ids = CrmImportLog::where('pid|id', $param['id'])->column('id'); $listid = CrmImportLog::where([['id', '>', 0], ['org_id', 'in', $orgids]])->field('id,pid')->select()->toArray(); $ids = $this->getAllLowerLevelId($listid, $param['id'], $is_first_time = true); if (in_array($param['id'], array_column($listid, 'id'))) { $ids[] = $param['id']; } else { return json(['code' => 1, 'msg' => '参数错误']); } if ($param['pid']) { $ids = array_diff($ids, [$param['id']]); //查询分配到本部门员工上的客户 $benids = Customer::where([['crm_res_id', '=', $param['id']], ['org_id', 'in', $orgids]]) ->where(function ($query) { $query->whereOr([['employee_id', 'NOTNULL', NULL], ['state', 'in', Customer::changeState('无效', 'chaos')]]); })->column('id'); } //已分配的数量包括,已经分配 和 曾经分配过当前处于无效状态的客户 和 包括分配到部门资源库的 $customer_id = Customer::where([['crm_res_id', 'in', $ids], ['org_id','in',$orgids],['is_distribution', '=', 1]])->withTrashed()->column('id'); //分配到人 if(!empty($benids)) $customer_id = array_merge($customer_id,$benids); // var_dump($customer_id); // exit; $where[] = ['id', 'in', $customer_id]; if ($param['type'] == 1 && $param['keyword']) { $org_ids = Org::where([['path', 'like', request()->empcrm->root_id . '-%'], ['name', 'like', '%' . $param['keyword'] . '%']])->column('id'); $where[] = ['org_id', 'in', $org_ids]; } elseif ($param['type'] == 2 && $param['keyword']) { $emp_ids = Employee::where([['name', 'like', '%' . $param['keyword'] . '%'], ['uid', '>', 0], ['root_id', '=', request()->empcrm->root_id], ['state', '=', '在职']])->column('id'); // $where[] = ['name', 'like', '%' . $param['keyword'] . '%']; $where[] = ['employee_id', 'in', $emp_ids]; } //小区,手机号搜索 if ($param['keyword3']) { $ids = []; $keyword = Customer::where($where)->field('id,community_name,phone,phone1,phone2')->select(); foreach ($keyword as $key => $val) { $str = $val['community_name'] . $val['phone'] . $val['phone1'] . $val['phone2']; if (strpos($str, $param['keyword3']) !== false) $ids[] = $val['id']; } $where[] = ['id', 'in', $ids]; } //状态搜索 if ($param['state']) { $state_ids = $this->cus_arrstatus($param['state'], $where); unset($where); $where[] = ['id', 'in', $state_ids]; } //打电话状态筛选 if (!empty($param['call_state'])) { //电话未联系和未接通 $call_list = OutCallLog::where([['customer_id', 'in', $customer_id]])->order('addtime desc')->field('id,status,customer_id')->select()->toArray(); $call_ids = []; $call_status = []; $no_connect_ids = []; $call_all = []; foreach ($call_list as $key => $val) { if (!in_array($val['customer_id'], $call_ids)) { $call_ids[] = $val['customer_id']; $call_status[$val['customer_id']] = $val['status']; } $call_all[$val['customer_id']][] = $val; } foreach ($call_status as $key => $val) { if (in_array($val, [-1, -2])) $no_connect_ids[] = $key; } $no_call_ids = array_diff($customer_id, $call_ids) ? array_diff($customer_id, $call_ids) : []; unset($where); if ($param['call_state'] == '已联系') { //2023-02-02 已联系包括打电话和remark不为空的客户 $id1 = array_column($call_list, 'customer_id'); $id2 = Customer::whereOr([[['id', 'in', $customer_id], ['remark', '<>', '']], [['id', 'in', $id1]]])->column('id'); $where[] = ['id', 'in', $id2]; // if (!empty($state_ids) && !empty($call_ids)) { // $call_ids = array_intersect($state_ids, $call_ids); // } // $where[] = ['id', 'in', $call_ids]; } if ($param['call_state'] == '未联系') { //2023-02-02 未打电话并且remark为空 $id1 = array_column($call_list, 'customer_id'); $id2 = Customer::whereOr([[['id', 'in', $customer_id], ['remark', '<>', '']], [['id', 'in', $id1]]])->column('id'); $where[] = ['id', 'not in', $id2]; // if (!empty($state_ids) && !empty($no_call_ids)) { // $no_call_ids = array_intersect($state_ids, $no_call_ids); // } $where[] = ['id', 'in', $no_call_ids]; } if ($param['call_state'] == '未接通') { if (!empty($state_ids) && !empty($no_connect_ids)) { $no_connect_ids = array_intersect($state_ids, $no_connect_ids); } $where[] = ['id', 'in', $no_connect_ids]; } } //回收客户 if ($param['recovery'] == 1) { //记录操作回收信息 $msg = '操作回收客户员工id:'.$empid.'回收到资源库的id'.$param['id'].'要回收的客户id'.$param['ids']; //Log::write($msg); if (empty($param['ids'])) return json(['code' => 1, 'data' => '请选择要回收的客户', 'msg' => '请选择要回收的客户']); unset($where); $where[] = ['id', 'in', explode(',', $param['ids'])]; $where[] = ['org_id', 'in', $orgids]; //$list = Customer::where($where)->field('id,name,employee_id,org_id,designer_id,crm_res_id,state')->select()->toArray(); $list = Customer::where($where)->column('id,name,employee_id,crm_res_id,state'); if (empty($list)) return json(['code' => 1, 'msg' => '客户不存在']); $repeat_name = []; foreach ($list as $item_key => $item_val) { if (!in_array($item_val['state'], Customer::changeState('待确认', 'chaos'), true)) { unset($list[$item_key]); $repeat_name[] = $item_val['name']; } } if (empty($list)) { if ($repeat_name) { $name = implode(',', $repeat_name); return json(['code' => 1, 'data' => '客户“' . $name . '”已经标记为有效无法回收', 'msg' => '客户“' . $name . '”已经标记为有效无法回收']); } else { return json(['code' => 0, 'data' => '回收完成', 'msg' => '回收完成']); } } //---end 2023-01-04--------//已经交定签单不能回收 $info = CrmImportLog::where([['id', '=', $param['id']], ['org_id', 'in', $orgids]])->find(); if (empty($info)) return json(['code' => 1, 'msg' => '数据不存在']); $listids = CrmImportLog::where([['id', '>', $info['id']], ['org_id', 'in', $orgids]])->column('pid,id'); foreach ($list as $key => $val) { $groups[$val['crm_res_id']][] = $val; } foreach ($groups as $key => $val) { $notemp = 0; $ids = []; foreach ($val as $k => $v) { if (empty($v['employee_id'])) $notemp++; $ids[] = $v['id']; } $newlist[] = ['crm_res_id' => $key, 'num' => count($val), 'notemp' => $notemp, 'ids' => $ids]; } Db::startTrans(); try { foreach ($newlist as $k => $v) { $this->recovery_crms($v, $param['id'], $listids); } Db::commit(); } catch (\Exception $e) { Db::rollback(); //var_dump($e->getMessage()); return json(['code' => 1, 'msg' => '回收数据失败.']); } if ($repeat_name) { $name = implode(',', $repeat_name); return json(['code' => 1, 'data' => '客户“' . $name . '”已经标记为有效无法回收', 'msg' => '客户“' . $name . '”已经标记为有效无法回收']); } else { return json(['code' => 0, 'data' => '回收完成', 'msg' => '回收完成']); } } $list = Customer::with(['orgs' => function ($query) { $query->field(['info', 'id', 'name'])->bind(['org_name' => 'name']); }, 'designer' => function ($query) { $query->field(['id', 'name'])->bind(['designer_name' => 'name']); }, 'employee' => function ($query) { $query->field(['id', 'name'])->bind(['sale_name' => 'name']); }])->withCount(['outcall'])->where($where)->page($param['page'], $param['limit'])->withTrashed()->field('id,employee_id,name,community_name,org_id,designer_id,crm_res_id,state,employee_time')->order('employee_id asc,id asc')->select(); $allids = Customer::where($where)->withTrashed()->column('id'); $count = count($allids); $allids = !empty($allids) ? implode(',', $allids) : ''; //分配时间 $times_arr = CrmImportLog::where([['id', 'in', array_column($list->toArray(), 'crm_res_id')]])->column('addtime', 'id'); foreach ($list as $key => $val) { $list[$key]['allocate_time'] = $times_arr[$val['crm_res_id']]; } // 状态统计获取 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($list->toArray(), 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0, 'ydf' => 0, 'wdf' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; elseif ($s['state'] == '已到访') $customersState[$s['customer_id']]['ydf'] += $s['num']; elseif ($s['state'] == '未到访') $customersState[$s['customer_id']]['wdf'] += $s['num']; } $list = $list->toArray(); $state1 = Customer::changeState('待确认', 'chaos'); $state2 = Customer::changeState('无效', 'chaos'); foreach ($list as $key => &$value) { $value['stateNum'] = $customersState[$value['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0, 'ydf' => 0, 'wdf' => 0]; //2023-02-02 修改逻辑 分配后为待确认状态,不为待确认和无效就是有效状态 $value['state'] = in_array($value['state'], $state1, true) ? '待确认' : (in_array($value['state'], $state2) ? '无效' : '有效'); } return json(['code' => 0, 'data' => $list, 'count' => $count, 'allids' => $allids]); } /** * 循环调用回收客户 */ public function recovery_crm($crm, $crm_res_id, $listids) { $crm_log = CrmImportLog::where('id', $crm['crm_res_id'])->find(); if ($crm_res_id != $crm_log['id']) { $upids = $this->getInfo($crm_log['id'], $listids, $crm_res_id); foreach ($upids as $k => $v) { $res_data = CrmImportLog::find($v['id']); $count = $res_data->count - 1; $left_num = $res_data->left_num - 1; $res_data->count = $count ? $count : 0; if ($crm_log->id == $res_data->id && empty($crm['employee_id'])) $res_data->left_num = $left_num ? $left_num : 0; $res_data->save(); } } $ben_log = CrmImportLog::where('id', $crm_res_id)->find(); $ben_left_num = $ben_log->left_num + 1; $ben_log->left_num = $ben_left_num ?: 0; $ben_log->save(); $is_distribution = $ben_log->pid ? 1 : 0; Customer::where([['id', '=', $crm['id']]])->update(['crm_res_id' => $ben_log->id, 'org_id' => $ben_log->org_id, 'employee_id' => null, 'employee_time' => null, 'is_resource' => 1, 'fresh' => 1, 'is_distribution' => $is_distribution, 'state' => 0]); } /** * 优化回收方法 */ public function recovery_crms($crm, $crm_res_id, $listids) { $crm_log = CrmImportLog::where('id', $crm['crm_res_id'])->find(); if ($crm_res_id != $crm_log['id']) { $upids = $this->getInfo($crm_log['id'], $listids, $crm_res_id); foreach ($upids as $k => $v) { $res_data = CrmImportLog::find($v['id']); $count = $res_data->count - $crm['num']; $left_num = $res_data->left_num - $crm['notemp']; $res_data->count = $count ? $count : 0; if ($crm_log->id == $res_data->id && !empty($crm['notemp'])) $res_data->left_num = $left_num ? $left_num : 0; $res_data->save(); } } $ben_log = CrmImportLog::where('id', $crm_res_id)->find(); $ben_left_num = $ben_log->left_num + $crm['num']; $ben_log->left_num = $ben_left_num ?: 0; $ben_log->save(); $is_distribution = $ben_log->pid ? 1 : 0; Customer::where([['id', 'in', $crm['ids']]])->update(['crm_res_id' => $ben_log->id, 'org_id' => $ben_log->org_id, 'employee_id' => null, 'employee_time' => null, 'is_resource' => 1, 'fresh' => 1, 'is_distribution' => $is_distribution, 'state' => 0]); } /** * 新详情页 * 详情页 */ public function read_new() { $request = request(); $id = input('id', 0); $type = input('type', 0); //handdistribution //基本信息 $data = $this->read($id); View::assign('data', $data); $counts = $this->get_customer_data($id); View::assign('counts', $counts); View::assign('id', $id); $org = Org::where([['path', 'like', request()->empcrm->root_id . '-%']])->field('id,name')->select()->toArray(); View::assign('org', $org); View::assign('type', $type); $root_id = $request->empcrm->root_id; View::assign('root_id', $root_id); return View::fetch(); } /** * 详情页获取基本信息 * 详情页 */ public function read() { $request = request(); $root_id = $request->empcrm->root_id; $allOrg = Org::where([['path', 'like', $root_id . '-%']])->column('id'); $param = $request->only(['id' => 0, 'empname' => null]); $data = CrmImportLog::with(['employee', 'source' => function ($query) { $query->field('id,source')->bind(['source_name' => 'source']); }])->where([['org_id', 'in', $allOrg], ['id', '=', $param['id']]])->findOrEmpty(); //if($data->isEmpty()) echo json_encode(['code' => 1, 'msg' =>'数据不存在']); exit; $fen = $this->sel_ywassign($param['id'],$allOrg,$data); // if(empty($data->pid)) $data->y_count = (int)$data->avaliable_count - (int)$data->left_num; // if(!empty($data->pid)){ // $data->y_count = (int)$data->count - (int)$data->left_num; // $data->employee_name = $param['empname']; // } $data->new_left_num = $fen['left_num']; if (empty($data->pid)) $data->y_count = $fen['y_count']; if (!empty($data->pid)) { $data->y_count = $fen['y_count']; $data->employee_name = $param['empname']; } if ($data->left_num != $fen['left_num']) { $data->left_num = $fen['left_num']; $data->save(); } return $data; } /** * 查询已分配未分配 */ public function sel_ywassign($id, $allOrg, $data) { $request = request(); $root_id = $request->empcrm->root_id; $orgids = orgSubIds($root_id); $where[] = ['employee_id', '=', NULL]; $where[] = ['crm_res_id', '=', $id]; $ispid = !empty($data['pid']) ? 1 : 0; $where[] = ['is_distribution', '=', $ispid]; $where[] = ['org_id', 'in', $allOrg]; $where[] = ['state', 'not in', Customer::changeState('无效', 'chaos')]; $left_num = Customer::where($where)->count(); $listid = CrmImportLog::where([['id', '>', 0], ['org_id', 'in', $orgids]])->field('id,pid')->select()->toArray(); $ids = $this->getAllLowerLevelId($listid, $id, $is_first_time = true); if (in_array($id, array_column($listid, 'id'))) { $ids[] = $id; } else { return json(['code' => 1, 'msg' => '参数错误']); } if ($data['pid']) { $ids = array_diff($ids, [$id]); //查询分配到本部门员工上的客户 $benids = Customer::where([['crm_res_id', '=', $id], ['org_id', 'in', $orgids]]) ->where(function ($query) { $query->whereOr([['employee_id', 'NOTNULL', NULL], ['state', 'in', Customer::changeState('无效', 'chaos')]]); })->column('id'); } //已分配的数量包括,已经分配 和 曾经分配过当前处于无效状态的客户 和 包括分配到部门资源库的 $customer_id = Customer::where([['crm_res_id', 'in', $ids], ['org_id', 'in', $orgids], ['is_distribution', '=', 1]])->withTrashed()->column('id'); //分配到人 if (!empty($benids)) $customer_id = array_merge($customer_id, $benids); $data = ['left_num' => $left_num, 'y_count' => count($customer_id)]; return $data; } /** * 已分配页面获取客户跟进数据 * 详情页 */ public function get_customer_data() { $request = request(); $root_id = $request->empcrm->root_id; $allOrg = Org::where([['path', 'like', $root_id . '-%']])->column('id'); $param = $request->only(['id' => 0]); //分配记录 $ids = CrmImportLog::where([['pid|id', '=', $param['id']], ['org_id', 'in', $allOrg]])->column('id'); //已分配的数量包括,已经分配 和 曾经分配过当前处于无效状态的客户 $customer_id1 = Customer::where([['crm_res_id', 'in', $ids], ['employee_id', '>', 0], ['is_distribution', '=', 1]])->column('id'); $customer_id2 = Customer::where([['crm_res_id', 'in', $ids], ['employee_id', '=', null], ['is_distribution', '=', 1]])->column('id'); $customer_id3 = CustomerVisitLog::where([['customer_id', 'in', $customer_id2]])->group('customer_id')->column('customer_id'); $customer_id = array_merge($customer_id1, $customer_id3); //有效状态 未到访,已到访,确定到场,已量房,交定,签单 // $state1 = CustomerVisitLog::changeState('待确认', 'chaos'); $state2 = CustomerVisitLog::changeState('未到访', 'chaos'); $state3 = CustomerVisitLog::changeState('已到访', 'chaos'); $state4 = CustomerVisitLog::changeState('已到场', 'chaos'); $state5 = CustomerVisitLog::changeState('已量房', 'chaos'); $state6 = CustomerVisitLog::changeState('已交定', 'chaos'); $state7 = CustomerVisitLog::changeState('已签单', 'chaos'); $state = array_merge($state2, $state3, $state4, $state5, $state6, $state7); //有效客户 排除待回访,和无效 //2023-02-02 逻辑修改 新分配的客户为待确认状态,确认标记有效后到我的客户,状态为有效 $query1 = ['id', 'in', $customer_id]; $query2 = ['is_distribution', '=', 1]; $query3 = ['is_distribution', '=', 0]; $query4 = ['died', '<>', 2]; //排除死单 $query5 = ['customer_id', 'in', $customer_id]; $query6 = ['state', 'in', Customer::changeState('待确认', 'chaos')]; $query7 = ['state', 'not in', Customer::changeState('待确认', 'chaos')]; $query8 = ['state', 'not in', Customer::changeState('无效', 'chaos')]; $query9 = ['state', 'in', Customer::changeState('无效', 'chaos')]; //有效 $data['valid'] = Customer::where([$query1, $query2, $query7, $query8])->count(); //无效 $data['invalid'] = Customer::where([$query1, $query2, $query9])->count(); //待确认 $data['confirmed'] = Customer::where([$query1, $query2, $query6])->count(); //已到店,,曾经已到店 $data['arrived_at_the_store'] = CustomerVisitLog::where([ $query5, ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')] ])->group('customer_id')->count(); //已量房 $data['measured_room'] = CustomerVisitLog::where([ $query5, ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')] ])->group('customer_id')->count(); //已到场 $data['already_present'] = CustomerVisitLog::where([ $query5, ['state', 'in', CustomerVisitLog::changeState('已到场', 'chaos')] ])->group('customer_id')->count(); //已交定 $data['settled'] = CustomerVisitLog::where([ $query5, ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')] ])->group('customer_id')->count(); //已签单 $data['sign_the_bill'] = CustomerVisitLog::where([ $query5, ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')] ])->group('customer_id')->count(); //2023-02-02 已联系包括’分配资源‘中的客户已经调用外呼或者标记时remark有值的客户 $visit1 = OutCallLog::where([$query5])->group('customer_id')->column('customer_id'); $data['visit'] = Customer::whereOr([[$query1, ['remark', '<>', '']], [['id', 'in', $visit1]]])->count(); //未联系 $data['unvisit'] = count($customer_id) - $data['visit']; //电话未联系和未接通 $call_list = OutCallLog::where([['customer_id', 'in', $customer_id]])->order('addtime desc')->field('id,status,customer_id')->select()->toArray(); $call_ids = []; $call_status = []; $no_connect = 0; foreach ($call_list as $key => $val) { if (!in_array($val['customer_id'], $call_ids)) { $call_ids[] = $val['customer_id']; $call_status[$val['customer_id']] = $val['status']; } } foreach ($call_status as $key => $val) { if (in_array($val, [-1, -2])) $no_connect++; } $yfp = Customer::where([['crm_res_id', 'in', $ids], ['is_distribution', '=', 1]])->count(); $data['no_connect'] = $no_connect; $data['no_call'] = $yfp - count($call_ids); return $data; } /** * 未分配分配客户列表 * 分配弹框页面接口 */ public function get_uncustomers() { $request = request(); $root_id = $request->empcrm->root_id; $allOrg = Org::where([['path', 'like', $root_id . '-%']])->column('id'); $param = $request->only(['page' => 1, 'limit' => 10, 'keyword' => '', 'org_id' => 0, 'id' => 0, 'pid' => 0]); $where[] = ['employee_id', '=', NULL]; $where[] = ['crm_res_id', '=', $param['id']]; $ispid = !empty($param['pid']) ? 1 : 0; $where[] = ['is_distribution', '=', $ispid]; $where[] = ['org_id', 'in', $allOrg]; $where[] = ['state', 'not in', Customer::changeState('无效', 'chaos')]; if ($param['keyword']) { $ids = []; $keyword = Customer::where($where)->field('id,community_name,phone,phone1,phone2')->select(); foreach ($keyword as $key => $val) { $str = $val['community_name'] . $val['phone'] . $val['phone1'] . $val['phone2']; if (strpos($str, $param['keyword']) !== false) $ids[] = $val['id']; } $where[] = ['id', 'in', $ids]; } // $customer_ids = Customer::where($where)->column('id'); //排除已经分配后又置为无效的数据 // $query[] = ['customer_id', 'in', $customer_ids]; // $customer_ids1 = CustomerVisitLog::where($query)->column('customer_id'); // $where[] = ['id', 'in', array_diff($customer_ids, $customer_ids1)]; $list = Customer::where($where)->field('id,name,community_name,square,phone,phone1,phone2')->page($param['page'], $param['limit'])->select(); foreach ($list as &$item) { $item['phone'] = substr_replace($item['phone'], '******', 3, 6); $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : ''; $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : ''; } $count = Customer::where($where)->count(); return json(['code' => 0, 'data' => $list, 'count' => $count]); } /** * 电话联系次数详情 */ public function call_cuslist() { $request = request(); $param = $request->only(['customer_id' => 0, 'page' => 1, 'limit' => 10]); if (!request()->isAjax()) { View::assign('customer_id', $param['customer_id']); return View::fetch(); } $root_id = $request->empcrm->root_id; $allOrg = Org::where([['path', 'like', $root_id . '-%']])->column('id'); $info = Customer::where([['id', '=', $param['customer_id']], ['org_id', 'in', $allOrg]])->find(); if (empty($info)) return json(['code' => 1, 'msg' => '客户不存在']); $list = OutCallLog::where([['customer_id', '=', $param['customer_id']], ['root_id', '=', $root_id]])->field('id,fwd_start_time,status')->page($param['page'], $param['limit'])->select()->toArray(); foreach ($list as $key => $val) { $list[$key]['status_name'] = '无'; if ($val['status'] == 1) $list[$key]['status_name'] = '接通'; if ($val['status'] == -1) $list[$key]['status_name'] = '未接通'; if ($val['status'] == -2) $list[$key]['status_name'] = '未接听'; } $count = OutCallLog::where([['customer_id', '=', $param['customer_id']], ['root_id', '=', $root_id]])->count(); return json(['code' => 0, 'data' => $list, 'msg' => '获取成功', 'count' => $count]); } function getInfo($id, $all, $pid) { $result = []; if ($id != $pid) { //将id等于0作为递归终点,如果要将无法查找到作为递归终点,也要在下面的循环遍历之后判断 foreach ($all as $item) { if ($item['id'] == $id) { $result[] = $item; break; } } $parents = $this->getInfo($result[0]['pid'], $all, $pid); if (!empty($parents)) { $result = array_merge($result, $parents); } } return $result; } /** * 已分配客户列表 * 分配弹框页面接口 */ public function recovery_customers() { $request = request(); $param = $request->only(['customer_id' => 0, 'crm_res_id']); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $allOrg = Org::where([['path', 'like', $root_id . '-%']])->column('id'); $info = Customer::where([['id', '=', $param['customer_id']], ['org_id', 'in', $allOrg]])->findOrEmpty(); //记录操作回收信息 $msg = '操作回收客户员工id:'.$empid.'回收到资源库的id'.$param['crm_res_id'].'要回收的客户id'.$param['customer_id']; //Log::write($msg); if (!$info->isEmpty() && $info->crm_res_id) { if (!in_array($info->state, [0, '待确认'], true)) { return json(['code' => 1, 'data' => '客户“' . $info->name . '”已标记为有效', 'msg' => '客户“' . $info->name . '”已标记为有效无法回收']); } Db::startTrans(); try { $crm_log = CrmImportLog::where('id', $info->crm_res_id)->find(); if ($param['crm_res_id'] != $crm_log['id']) { $listids = CrmImportLog::where('id', '>', 0)->column('pid,id'); $upids = $this->getInfo($crm_log['id'], $listids, $param['crm_res_id']); foreach ($upids as $k => $v) { $res_data = CrmImportLog::find($v['id']); $count = $res_data->count - 1; $left_num = $res_data->left_num - 1; $res_data->count = $count ? $count : 0; if ($info->crm_res_id == $res_data->id && empty($info->employee_id)) $res_data->left_num = $left_num ? $left_num : 0; $res_data->save(); } } $ben_log = CrmImportLog::where([['id', '=', $param['crm_res_id']], ['org_id', 'in', $allOrg]])->find(); $ben_left_num = $ben_log->left_num + 1; $ben_log->left_num = $ben_left_num ?: 0; $ben_log->save(); $is_distribution = $ben_log->pid ? 1 : 0; Customer::where([['id', '=', $param['customer_id']]])->update(['crm_res_id' => $ben_log->id, 'org_id' => $ben_log->org_id, 'employee_id' => null, 'employee_time' => null, 'is_resource' => 1, 'fresh' => 1, 'is_distribution' => $is_distribution, 'state' => 0]); Db::commit(); } catch (\Exception $e) { Db::rollback(); var_dump($e->getMessage()); return json(['code' => 1, 'msg' => '回收失败.']); } return json(['code' => 0, 'data' => '回收成功', 'msg' => '回收成功']); } else { return json(['code' => 0, 'data' => '回收成功', 'msg' => '回收成功']); } } /** * 管理层数据统计 */ public function manageStatistics() { $request = request(); if (!request()->isAjax()) { //房屋状态是否开启 $house_status_where = [ ['root_id', '=', $request->empcrm->root_id], ['keyname', '=', 'house_status'], ['status', '=', 0] ]; $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty(); $field = []; if (!$house_status_per->isEmpty()) { $field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id'); $fields = []; foreach ($field as $v) { $fields[] = [ 'field' => 'house_status_' . $v['id'], 'title' => $v['name'], 'align' => 'center', 'width' => 100, 'rowspan' => 2 ]; } $field = $fields; } View::assign('field', json_encode($field)); //返回组织列表 $path = Org::where('id', $request->empcrm->org_id)->value('path'); $org = Org::where([['path', 'like', $path . '%']])->select()->toArray(); View::assign('org', $org); return View::fetch(); } $param = $request->only(['page' => 1, 'limit' => 10, 'start_date' => '', 'end_date' => '', 'keyword' => '', 'org_id' => '']); //日期搜索//2023-02-15页面逻辑修改 传参方式改为 start_date=2023/2/15 - 2023/2/15 if ($param['start_date']) { $count_dates = explode(' - ', $param['start_date']); $param['start_date'] = date('Y-m-d 00:00:00', strtotime($count_dates[0])); $param['end_date'] = date('Y-m-d 23:59:59', strtotime($count_dates[1])); } else { $param['start_date'] = $param['end_date'] = ''; } $query_date = ($param['start_date'] && $param['end_date']) ? [['addtime', 'between', [$param['start_date'], $param['end_date']]]] : []; $root_id = request()->empcrm->root_id; $orgids = orgSubIds($request->empcrm->org_id); $where = [ ['root_id', '=', $root_id], ['uid', '>', 0], ['state', '=', '在职'] // ['is_manager', '=', 1] ]; if ($param['start_date'] && $param['end_date']) { $sdate = strtotime($param['start_date']); $edate = strtotime($param['end_date']); } else { $edate = time(); $sdate = 972473642; } if ($param['keyword']) { $where[] = ['name', 'like', '%' . $param['keyword'] . '%']; } if ($param['org_id']) { $where[] = ['org_id', '=', $param['org_id']]; } else { $where[] = ['org_id', 'in', $orgids]; } $employee_date = [$param['start_date'], $param['end_date']]; $list = Employee::with(['customer' => function ($query) use ($employee_date) { //手动报备查addtime,分配查询employee_time if ($employee_date[0] && $employee_date[1]) { $e_where1 = [ ['addtime', 'between', [$employee_date[0], $employee_date[1]]], ['crm_res_id', 'NULL', null] //['crm_res_id','=',0] ]; $e_where2 = [ ['crm_res_id', '>', 0], ['employee_time', 'between', [$employee_date[0], $employee_date[1]]] ]; $query->whereOr([$e_where1, $e_where2])->field('id,employee_id,ext,is_resource,addtime,signed_money,house_type,square,house_status,return_visit,assigned_personnel'); } else { $query->field('id,employee_id,ext,is_resource,addtime,signed_money,house_type,square,house_status,return_visit,assigned_personnel'); } }])->where($where)->field('id,org_id,name')->page($param['page'], $param['limit'])->select()->toArray(); $org = Org::where([['id', 'in', array_column($list, 'org_id')]])->column('name', 'id'); // return json(['code' => 0, 'data' => $list, 'msg' => '获取成功']); $count = Employee::where($where)->count(); //客户ID $customer_ids = []; foreach ($list as $key => $val) { $cid = array_column($val['customer'], 'id'); $customer_ids = array_merge($customer_ids, $cid); } //最近跟进时间 $max_addtime = CustomerVisitLog::where([['is_merge', '=', 0], ['customer_id', 'in', $customer_ids]])->group('customer_id')->column('max(addtime) addtime', 'customer_id'); //查询有效数量 //有效性判断 ,, //有效状态 未到访,已到访,确定到场,已量房,交定,签单 $state1 = CustomerVisitLog::changeState('未到访', 'chaos'); $state2 = CustomerVisitLog::changeState('已到访', 'chaos'); $state3 = CustomerVisitLog::changeState('已量房', 'chaos'); $state4 = CustomerVisitLog::changeState('已到店', 'chaos'); $state5 = CustomerVisitLog::changeState('已交定', 'chaos'); $state6 = CustomerVisitLog::changeState('已签单', 'chaos'); $state7 = CustomerVisitLog::changeState('已到场', 'chaos'); $vis_where[] = ['customer_id', 'in', $customer_ids]; $vis_where[] = ['state', 'in', array_merge($state1, $state2, $state3, $state4, $state5, $state6, $state7)]; if ($query_date) $vis_where[] = $query_date[0]; $vislog_list = CustomerVisitLog::where($vis_where)->order('addtime asc')->column('customer_id,state,addtime,next_contact_date'); $valid_vislog = array_unique(array_column($vislog_list, 'customer_id')); $rooms = $store = $dep = $sign = []; foreach ($vislog_list as $vislog_list_item) { if (in_array($vislog_list_item['state'], $state3)) { $rooms[] = $vislog_list_item['customer_id']; } elseif (in_array($vislog_list_item['state'], $state4)) { @$store[$vislog_list_item['customer_id']][] = $vislog_list_item['addtime']; } elseif (in_array($vislog_list_item['state'], $state5)) { @$dep[$vislog_list_item['customer_id']][] = $vislog_list_item['addtime']; } elseif (in_array($vislog_list_item['state'], $state6)) { $sign[] = $vislog_list_item['customer_id']; } } $rooms = array_unique($rooms); //量房客户 //到店客户 $store1 = $store2 = $store3 = []; foreach ($store as $store_key => $store_val) { if (count($store_val) == 1) { $store1[] = $store_key; } elseif (count($store_val) == 2) { $store2[] = $store_key; } else { $store3[] = $store_key; } } //交定客户 $dep1 = $dep2 = $dep3 = []; foreach ($dep as $dep_key => $dep_item) { if (count($dep_item) == 1) { $dep1[] = $dep_key; } elseif (count($dep_item) == 2) { $dep2[] = $dep_key; } else { $dep3[] = $dep_key; } } //房屋状态是否开启 $house_status_where = [ ['root_id', '=', $root_id], ['keyname', '=', 'house_status'], ['status', '=', 0] ]; $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty(); $field = []; if (!$house_status_per->isEmpty()) { $field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id'); } // 电话统计 $outCallLog = OutCallLog::where([['root_id', '=', $root_id]])->where($query_date)->field("count(id) as phone_count, sum(if(`status`>0,1,0)) as on_phone_count, sum(if(`status`<0,1,0)) as off_phone_count,sum(billsec) as phone_time,employee_id")->group('employee_id')->select()->toArray(); $outCallLog = array_combine(array_column($outCallLog, 'employee_id'), $outCallLog); foreach ($list as $k => $v) { //客户ID $cids = array_column($v['customer'], 'id'); //客户数量 $list[$k]['customer_count'] = count($cids); //待回访 $list[$k]['return_visit_count'] = count(array_filter(array_column($v['customer'], 'return_visit'))); //待指派 $assigned_personnel = count(array_filter(array_column($v['customer'], 'assigned_personnel'))); $list[$k]['no_assigned_personnel'] = $list[$k]['customer_count'] - $assigned_personnel; //三天,七天,十五天,三十天,三十天以上未跟进 $list[$k]['no_visit_3'] = $list[$k]['no_visit_7'] = $list[$k]['no_visit_15'] = $list[$k]['no_visit_30'] = $list[$k]['no_visit_31'] = 0; foreach ($v['customer'] as $customer_item) { //最近跟进时间 $visit_time = isset($max_addtime[$customer_item['id']]) ? $max_addtime[$customer_item['id']] : $customer_item['addtime']; $day = ceil((time() - strtotime($visit_time)) / 86400); if ($day <= 3 && $day > 1) { $list[$k]['no_visit_3'] += 1; } elseif ($day <= 7 && $day > 3) { $list[$k]['no_visit_7'] += 1; } elseif ($day < 15 && $day > 7) { $list[$k]['no_visit_15'] += 1; } elseif ($day <= 30 && $day > 15) { $list[$k]['no_visit_30'] += 1; } elseif ($day > 30) { $list[$k]['no_visit_31'] += 1; } } //有效客户 $valid = array_intersect($cids, $valid_vislog); //有效客户数量 $list[$k]['valid_count'] = count($valid); //有效率 $list[$k]['valid_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['valid_count']); if (isset($outCallLog[$v['id']])) { //电话量 $list[$k]['phone_count'] = $outCallLog[$v['id']]['phone_count']; //接通量 $list[$k]['on_phone_count'] = $outCallLog[$v['id']]['on_phone_count']; //接通率 $list[$k]['on_phone_grawth'] = $this->grawth($list[$k]['phone_count'], $list[$k]['on_phone_count']); //未接通 $list[$k]['off_phone_count'] = $outCallLog[$v['id']]['off_phone_count']; //平均通话时长 $list[$k]['avg_phone_time'] = $list[$k]['on_phone_count'] == 0 ? 0 : round($outCallLog[$v['id']]['phone_time'] / $list[$k]['on_phone_count'], 2); } else { //电话量 $list[$k]['phone_count'] = 0; //接通量 $list[$k]['on_phone_count'] = 0; //接通率 $list[$k]['on_phone_grawth'] = 0; //未接通 $list[$k]['off_phone_count'] = 0; //平均通话时长 $list[$k]['avg_phone_time'] = 0; } $customer = $this->addWechatCount($v['customer'], $sdate, $edate); //加微数量 $list[$k]['add_wechat_count'] = count($customer['wechat_count']); //出方案客户数 $list[$k]['drawing_date'] = count($customer['plan_issuing_date']); //加微率 $list[$k]['add_wechat_grawth'] = $this->grawth($list[$k]['valid_count'], $list[$k]['add_wechat_count']); //量房客户数 $list[$k]['room'] = count(array_intersect($rooms, $cids)); //量房率 $list[$k]['room_grawth'] = $this->grawth($list[$k]['valid_count'], $list[$k]['room']); //到店客户数 $store1 = array_intersect($store1, $cids); $store2 = array_intersect($store2, $cids); $store3 = array_intersect($store3, $cids); $list[$k]['store1'] = count($store1); $list[$k]['store2'] = count($store2); $list[$k]['store3'] = count($store3); $list[$k]['store1_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store1']); $list[$k]['store2_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store2']); $list[$k]['store3_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store3']); //交定客户数 $z_dep = array_intersect(array_keys($dep), $cids); $dep1 = array_intersect($dep1, $cids); $dep2 = array_intersect($dep2, $cids); $dep3 = array_intersect($dep3, $cids); $list[$k]['dep1'] = count($dep1); $list[$k]['dep2'] = count($dep2); $list[$k]['dep3'] = count($dep3); $list[$k]['dep1_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep1']); $list[$k]['dep2_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep2']); $list[$k]['dep3_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep3']); $list[$k]['dep'] = count($z_dep); $list[$k]['dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep']); //一次到店签单客户数 $list[$k]['store1_dep'] = count(array_intersect($z_dep, $store1)); $list[$k]['store1_dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store1_dep']); $list[$k]['store2_dep'] = count(array_intersect($z_dep, $store2)); $list[$k]['store2_dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store2_dep']); $list[$k]['store3_dep'] = count(array_intersect($z_dep, $store3)); $list[$k]['store3_dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store3_dep']); //签单 $list[$k]['sign_count'] = count(array_intersect($cids, $sign)); $list[$k]['sign_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['sign_count']); //面积 $list[$k]['square_0_80'] = $customer['square_0_80']; // $list[$k]['square_80_100'] = $customer['square_80_100']; // $list[$k]['square_100_120'] = $customer['square_100_120']; // $list[$k]['square_120_200'] = $customer['square_120_200']; // $list[$k]['square_200_500'] = $customer['square_200_500']; // $list[$k]['square_500'] = $customer['square_500']; // //房屋类型 $list[$k]['existing_homes_count'] = $customer['existing_homes_count']; //现房数量 $list[$k]['forward_housing_count'] = $customer['forward_housing_count']; //期房数量 //房屋状态 if ($field) { $house_status = $this->getHouseStatus($customer['house_status'], $field); $list[$k] = array_merge($list[$k], $house_status); } unset($list[$k]['customer']); } return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功', 'page' => ceil($count / $param['limit'])]); } /** * 率 */ private function grawth($zcount, $count) { $res = $zcount == 0 ? '0%' : round($count / $zcount * 100, 2) . '%'; return $res; } /** * 房屋状态 */ private function getHouseStatus($house_status, $field) { $data = []; $name = array_column($field, 'name'); $house = []; foreach ($house_status as $v) { @$house[$v] += 1; } foreach ($field as $k2 => $v2) { $data['house_status_' . $v2['id']] = isset($house[$v2['name']]) ? $house[$v2['name']] : 0; } return $data; } /** * 加微数量 */ private function addWechatCount($wechat, $sdate, $edate) { $data['wechat_count'] = []; $data['plan_issuing_date'] = []; //出方案时间 $square = $house_type = []; $data['house_status'] = []; foreach ($wechat as $v) { $data['house_status'][] = $v['house_status']; $square[] = $v['square']; $house_type[] = $v['house_type']; //客户添加时间 $addtime = strtotime($v['addtime']); //加微客户,有户型图客户 $v2 = $v['ext']; $ext = json_decode($v2, true); if (!empty($ext) && is_array($ext)) { $ext = array_filter($ext); foreach ($ext as $e) { if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value']) && strtotime($e['value']) >= $sdate && strtotime($e['value']) <= $edate) { $data['wechat_count'][] = $v['id']; } elseif (isset($e['keyname']) && $e['keyname'] == 'drawing_date' && !empty($e['value']) && strtotime($e['value']) >= $sdate && strtotime($e['value']) <= $edate) { $data['drawing_date'][] = $v['id']; } } } } //面积 //房屋面积 $data['square_0_80'] = $data['square_80_100'] = $data['square_100_120'] = $data['square_120_200'] = $data['square_200_500'] = $data['square_500'] = 0; foreach ($square as $square_item) { if (!$square_item) continue; //不填不计算 if ($square_item <= 80) { $data['square_0_80'] += 1; } elseif ($square_item <= 100) { $data['square_80_100'] += 1; } elseif ($square_item <= 120) { $data['square_100_120'] += 1; } elseif ($square_item <= 200) { $data['square_120_200'] += 1; } elseif ($square_item <= 500) { $data['square_200_500'] += 1; } else { $data['square_500'] += 1; } } //房屋类型 $data['existing_homes_count'] = 0; //现房数量 $data['forward_housing_count'] = 0; //期房数量 foreach ($house_type as $house_type_item) { if (strpos($house_type_item, '现房') !== false) { $data['existing_homes_count'] += 1; } elseif (strpos($house_type_item, '期房') !== false) { $data['forward_housing_count'] += 1; } } return $data; } /** * 客户统计表 */ public function customer_statistics_list() { $request = request(); $root_id = $request->empcrm->root_id; $orgids = orgSubIds($request->empcrm->org_id); if (!request()->isAjax()) { $org = Org::where([['id', 'in', $orgids], ['org_type', '<>', 2]])->field('id,name')->select()->toArray(); View::assign('org', $org); //渠道来源下拉 $source = CustomerSource::where('root_id', $root_id)->column('id,source'); View::assign('source', $source); $customer_type = [['id' => 1, 'name' => '资源库'], ['id' => 2, 'name' => '活动报名'], ['id' => 3, 'name' => '装修推荐官'], ['id' => 4, 'name' => '自建'], ['id' => 5, 'name' => '公海获取']]; View::assign('customer_type', $customer_type); return View::fetch(); } $page = input('page', 1, 'intval'); $limit = input('limit', 20, 'intval'); $source_id = input('source_id', 0); $customer_type = input('customer_type', '', 'intval'); switch ($customer_type) { case 1: $where[] = ['crm_res_id', '>', 0]; $where[] = ['remark', '<>', '公海获取']; break; case 2: $where[] = ['remark', '=', '活动报名建档']; break; case 3: $where[] = ['agents_id', '>', 0]; break; case 4: $where[] = ['crm_res_id', 'NULL', null]; $where[] = ['remark', '<>', '活动报名建档']; $where[] = ['remark', '<>', '公海获取']; $where[] = ['agents_id', 'NULL', null]; break; case 5: $where[] = ['remark', '=', '公海获取']; break; default: break; } $employee_name = input('keyword', '', 'trim'); if ($employee_name) { $employee_ids = Employee::where([['org_id', 'in', $orgids], ['name', 'like', '%' . $employee_name . '%'], ['root_id', '=', $root_id]])->column('id'); $where[] = ['employee_id', 'in', $employee_ids]; } $start_date = input('start_date', '', 'trim'); $end_date = input('end_date', '', 'trim'); if ($start_date) { $count_dates = explode(' - ', $start_date); $start_date = $count_dates[0]; $end_date = $count_dates[1]; } else { $start_date = $end_date = ''; } if ($start_date && $end_date) { $where[] = ['addtime', 'between', [$start_date . ' 00:00:00', $end_date . ' 23:59:59']]; } $org_id = input('org_id', '', 'trim'); $org_ids = orgSubIds($root_id); if ($org_id) { $org_id = explode(',', $org_id); $sel_org_ids = []; foreach ($org_id as $k => $v) { if (in_array($v, $org_ids)) { $sel_org_ids[] = $v; } } $where[] = ['org_id', 'in', $sel_org_ids]; } else { $where[] = ['org_id', 'in', $orgids]; } if ($source_id) $where[] = ['source_id', '=', $source_id]; $model = Customer::where($where)->where(function ($query) { $state11 = Customer::changeState('待确认', 'chaos'); $state22 = Customer::changeState('无效', 'chaos'); $or1[] = ['crm_res_id', '>', 0]; $or1[] = ['state', 'not in', array_merge($state11, $state22)]; $or2[] = ['crm_res_id', 'null', null]; $or2[] = ['state', 'not in', $state22]; $query->whereOr([$or1, $or2]); }); $count = $model->count(); $list = $model->with(['employee', 'org', 'designer', 'source'])->page($page, $limit)->order('addtime desc')->select()->each(function ($item) { if ($item['remark'] == '公海获取') { $customer_type = '公海获取'; } elseif ($item['agents_id']) { $customer_type = '装修推荐官'; } elseif ($item['remark'] == '活动报名建档') { $customer_type = '活动报名'; } elseif ($item['crm_res_id']) { $customer_type = '资源库'; } else { $customer_type = '自建'; } $item['customer_type'] = $customer_type; if (empty($item['name'])) { $item['name'] = '未知'; } if (empty($item['community_name'])) { $item['community_name'] = '未知'; } if (empty($item['house_type'])) { $item['house_type'] = '未知'; } if (empty($item['square'])) { $item['square'] = '未知'; } $item['phone'] = substr_replace($item['phone'], '******', 3, 6); $item['signed_money_data'] = $item->getData('signed_money'); // 指派客户经理及设计师 $assigned_personal_manager_ids = []; if ($item['assigned_personnel']) { $assigned_personal = explode(',', $item['assigned_personnel']); $assigned_personal_list = Employee::with('org')->where('id', 'in', $assigned_personal)->select()->toArray(); $assigned_personal_designer = []; $assigned_personal_manager = []; $assigned_personal_designer_org = []; foreach ($assigned_personal_list as $k => $v) { if ($v['org']['org_type'] == 2) { $assigned_personal_designer[] = $v['name']; $assigned_personal_designer_org[] = $v['org']['name']; } else { $assigned_personal_manager[] = $v['name']; $assigned_personal_manager_ids[] = $v['id']; } } $item['assigned_personal_manager'] = $assigned_personal_manager ? implode('、', $assigned_personal_manager) : '无'; $item['assigned_personal_designer'] = $assigned_personal_designer ? implode('、', $assigned_personal_designer) : '无'; $item['assigned_personal_designer_org'] = $assigned_personal_designer_org ? implode('、', $assigned_personal_designer_org) : '无'; } else { $item['assigned_personal_manager'] = '无'; $item['assigned_personal_designer'] = '无'; $item['assigned_personal_designer_org'] = '无'; } // 未跟踪天数 $last_visit_log = CustomerVisitLog::where('customer_id', '=', $item['id'])->findOrEmpty(); if (!$last_visit_log->isEmpty()) { $time_long = time() - strtotime($last_visit_log['addtime']); } else { $time_long = time() - strtotime($item['addtime']); } $no_visit_day = ceil($time_long / (24 * 3600)); $item['no_visit_day'] = $no_visit_day; // 是否加微 $ext = json_decode($item['ext'], true); $item['wechat'] = '否'; // 是否加微 $item['add_wechat_type'] = '无'; // 加微类型 $item['add_wechat_time'] = '无'; // 加微时间 $item['plan_issuing_date'] = '无'; //出方案时间 $item['drawing_date'] = '无'; // 出图时间 $item['group_building_date'] = '无'; // 建群时间 if (!empty($ext)) { foreach ($ext as $v) { if (isset($v['keyname']) && $v['keyname'] == 'wechat' && !empty($v['value'])) { $item['wechat'] = CustomerPortraitFieldSelect::where('id', $v['value'])->value('name'); } elseif (isset($v['keyname']) && $v['keyname'] == 'add_wechat_time' && !empty($v['value'])) { $item['wechat'] = '是'; } elseif (isset($v['keyname']) && $v['keyname'] == 'add_wechat_type' && !empty($v['value'])) { $item['wechat'] = '是'; } if (isset($v['keyname']) && $v['keyname'] == 'add_wechat_time' && !empty($v['value'])) { $item['add_wechat_time'] = $v['value']; } if (isset($v['keyname']) && $v['keyname'] == 'add_wechat_type' && !empty($v['value'])) { $item['add_wechat_type'] = CustomerPortraitFieldSelect::where('id', $v['value'])->value('name'); } if (isset($v['keyname']) && $v['keyname'] == 'plan_issuing_date' && !empty($v['value'])) { $item['plan_issuing_date'] = $v['value']; } if (isset($v['keyname']) && $v['keyname'] == 'drawing_date' && !empty($v['value'])) { $item['drawing_date'] = $v['value']; } if (isset($v['keyname']) && $v['keyname'] == 'group_building_date' && !empty($v['value'])) { $item['group_building_date'] = $v['value']; } } } $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); $lf_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $lf_state]])->order('addtime asc')->select()->toArray(); if (!empty($lf_list)) { $item['is_liangfang'] = '是'; $item['liangfang_date'] = $lf_list[0]['addtime']; $item['once_liangfang_days'] = ceil((strtotime($lf_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600)); } else { $item['is_liangfang'] = '否'; $item['liangfang_date'] = '无'; $item['once_liangfang_days'] = '无'; } // 到店 $dd_state = CustomerVisitLog::changeState('已到店', 'chaos'); $dd_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $dd_state]])->order('addtime asc')->select()->toArray(); $item['once_daodian'] = '否'; $item['daodian_date'] = '无'; $item['once_daodian_days'] = '无'; $item['once_daodian_shop'] = '无'; $item['twice_daodian_date'] = '无'; //二次到店时间 $item['twice_daodian_days'] = '无'; // 二次到店周期 二次到店时间 - 一次到店时间 $item['twice_daodian_shop'] = '无'; if (!empty($dd_list)) { $item['once_daodian'] = '是'; $item['daodian_date'] = $dd_list[0]['addtime']; $item['once_daodian_days'] = ceil((strtotime($dd_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600)); if (!empty($dd_list[0]['shop_id'])) { $shop_name = Shop::where('id', $dd_list[0]['shop_id'])->findOrEmpty(); if ($shop_name->isEmpty()) { $item['once_daodian_shop'] = '未知'; } else { $item['once_daodian_shop'] = $shop_name['name']; } } // 二次到店 if (count($dd_list) > 1) { $item['twice_daodian_date'] = $dd_list[1]['addtime']; $item['twice_daodian_days'] = ceil((strtotime($dd_list[1]['addtime']) - strtotime($dd_list[0]['addtime'])) / (24 * 3600)); $shop_name = Shop::where('id', $dd_list[1]['shop_id'])->findOrEmpty(); if ($shop_name->isEmpty()) { $item['twice_daodian_shop'] = '未知'; } else { $item['twice_daodian_shop'] = $shop_name['name']; } } } // 量房 - 到店 周期 if (!empty($lf_list) && !empty($dd_list)) { if ($lf_list[0]['addtime'] > $dd_list[0]['addtime']) { $item['liangfang_daodian_days'] = ceil((strtotime($lf_list[0]['addtime']) - strtotime($dd_list[0]['addtime'])) / (24 * 3600)); } else { $item['liangfang_daodian_days'] = ceil((strtotime($dd_list[0]['addtime']) - strtotime($lf_list[0]['addtime'])) / (24 * 3600)); } } else { $item['liangfang_daodian_days'] = '无'; } // 交定(签单) $jd_state = CustomerVisitLog::changeState('已交定', 'chaos'); $jd_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $jd_state]])->order('addtime asc')->select()->toArray(); if (!empty($jd_list)) { $item['is_deposit'] = '是'; $item['deposit_date'] = $jd_list[0]['addtime']; $item['deposit_days'] = ceil((strtotime($jd_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600)); } else { $item['is_deposit'] = '否'; $item['deposit_date'] = '无'; $item['deposit_days'] = '无'; } // 签单(转单) $qd_state = CustomerVisitLog::changeState('已签单', 'chaos'); $qd_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $qd_state]])->order('addtime asc')->select()->toArray(); if (!empty($qd_list)) { $item['sign_date'] = $qd_list[0]['addtime']; $item['sign_days'] = ceil((strtotime($qd_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600)); } else { $item['sign_date'] = '无'; $item['sign_days'] = '无'; } // 交定转单周期 if (!empty($jd_list) && !empty($qd_list)) { if ($jd_list[0]['addtime'] > $qd_list[0]['addtime']) { $item['deposit_sign_days'] = ceil((strtotime($jd_list[0]['addtime']) - strtotime($qd_list[0]['addtime'])) / (24 * 3600)); } else { $item['deposit_sign_days'] = ceil((strtotime($qd_list[0]['addtime']) - strtotime($jd_list[0]['addtime'])) / (24 * 3600)); } } else { $item['deposit_sign_days'] = '无'; } // 一次到店未签单时间 $first_visit = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $dd_state]])->order('addtime asc')->findOrEmpty(); $item['first_visit_no_sign'] = '无'; if (!$first_visit->isEmpty()) { $first_visit_no_sign = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['addtime', '>', $first_visit['addtime']]])->order('addtime asc')->findOrEmpty(); if (!$first_visit_no_sign->isEmpty()) { if (!in_array($first_visit_no_sign['state'], $qd_state)) { $item['first_visit_no_sign'] = $first_visit['addtime']; } } } // 客户经理回访 if (!empty($assigned_personal_manager_ids)) { $manager_next_visit = CustomerVisitLog::where([['employee_id', 'in', $assigned_personal_manager_ids], ['next_contact_date', 'not null', ''], ['customer_id', '=', $item['id']]])->field('customer_id,next_contact_date,addtime,employee_id')->select()->toArray(); $manager_visit_times = 0; $manager_visit_days = 0; foreach ($manager_next_visit as $k => $v) { unset($visit_where); $visit_where[] = ['addtime', 'between', [$v['next_contact_date'] . ' 00:00:00', $v['next_contact_date'] . ' 23:59:59']]; $visit_where[] = ['customer_id', '=', $v['customer_id']]; $visit_where[] = ['employee_id', '=', $v['employee_id']]; $visit_next_find = CustomerVisitLog::where($visit_where)->findOrEmpty(); if (!$visit_next_find->isEmpty()) { $manager_visit_times++; $next_days = ceil((strtotime($visit_next_find['addtime']) - strtotime($v['next_contact_date'])) / (24 * 3600)); $manager_visit_days += $next_days; } } $item['manager_visit_times'] = $manager_visit_times; $item['manager_visit_cycle'] = $manager_visit_times ? round($manager_visit_days / $manager_visit_times, 1) : '无'; } else { $item['manager_visit_times'] = 0; $item['manager_visit_cycle'] = '无'; } // 经理回访 $employee_next_visit = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['next_contact_date', 'not null', ''], ['employee_id', '=', $item['employee_id']]])->field('customer_id,next_contact_date,employee_id')->select()->toArray(); $employee_visit_times = 0; $employee_visit_days = 0; foreach ($employee_next_visit as $k => $v) { unset($visit_where); $visit_where[] = ['addtime', 'between', [$v['next_contact_date'] . ' 00:00:00', $v['next_contact_date'] . ' 23:59:59']]; $visit_where[] = ['customer_id', '=', $v['customer_id']]; $visit_where[] = ['employee_id', '=', $v['employee_id']]; $visit_next_find = CustomerVisitLog::where($visit_where)->findOrEmpty(); if (!$visit_next_find->isEmpty()) { $employee_visit_times++; $next_days = ceil((strtotime($visit_next_find['addtime']) - strtotime($v['next_contact_date'])) / (24 * 3600)); $employee_visit_days += $next_days; } } $item['employee_visit_times'] = $employee_visit_times; $item['employee_visit_cycle'] = $employee_visit_times ? round($employee_visit_days / $employee_visit_times, 1) : '无'; $wx_state = Customer::changeState('无效', 'chaos'); if (in_array($item['state'], $wx_state)) { $item['invalid'] = '是'; } else { $item['invalid'] = '否'; } if ($item['died'] == 2) { $item['died'] = '是'; } else { $item['died'] = '否'; } })->toArray(); $clue_where[] = ['name', '=', 'clueTag']; $clue_where[] = ['root_id', '=', request()->empcrm->root_id]; $find = Setting::where($clue_where)->findOrEmpty(); if ($find->isEmpty()) { $clue_tag = ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话']; } else { $clue_tag = array_filter(explode(',', $find['content'])); } foreach ($list as $k => $v) { $list[$k]['house_delivery_time'] = $v['house_delivery_time'] ? $v['house_delivery_time'] : '未知'; $list[$k]['employee_name'] = $v['employee']['name'] ?? '无'; $list[$k]['source_name'] = $v['source']['source'] ?? '未知'; if ($v['invalid'] == '否') { $list[$k]['invalid_remark'] = ''; } else { $wx_state = CustomerVisitLog::changeState('无效', 'chaos'); $invalid_log = CustomerVisitLog::where([['customer_id', '=', $v['id']], ['state', 'in', $wx_state]])->order('addtime desc')->findOrEmpty(); if (!$invalid_log->isEmpty() && !empty($invalid_log['remark']) && in_array($invalid_log['remark'], $clue_tag)) { $list[$k]['invalid_remark'] = $invalid_log['remark']; } else { $list[$k]['invalid_remark'] = '其他'; } } } return json(['code' => 0, 'data' => $list, 'count' => $count, 'page' => ceil($count / $limit)]); } //获取设计师列表 private function get_designer() { //设计师获取 $w[] = ['path', 'like', request()->empcrm->root_id . '-%']; $w[] = ['org_type', '=', 2]; $orgs = Org::where($w)->column('id'); $list = Employee::where([['org_id', 'in', $orgs], ['state', '=', '在职'], ['show', '=', 0]])->field('id,name,initials s')->order('s asc')->select()->toArray(); return $list; } /** * 共有客户 */ public function shareList() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $orgids = orgSubIds($request->empcrm->org_id); $is_manager = $request->empcrm->is_manager; $path = Org::where([['pid', '=', $request->empcrm->org_id]])->count(); $param = $request->only(['page' => 1, 'limit' => 17, 'last_contact_date', 'assign_org', 'assign_list', 'community_name', 'no_visit_day', 'liangfang_time', 'jiaoding_time', 'qiandan_time', 'jiaoding_money', 'qiandan_money', 'daodian_time' => '', 'square', 'level', 'source_name', 'name' => '', 'state', 'assign_time', 'phone', 'employee_name', 'assign_type_name', 'designer_name']); if (!$request->isAjax()) { $employee_list = Employee::where([['org_id', 'in', $orgids], ['state', '=', '在职'], ['uid', '>', 0]])->column('id,name'); View::assign('employee_list', $employee_list); //来源 $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray(); View::assign('soudata', $soudata); //设计师 $list = $this->get_designer(); if (!empty($list)) { $sort = array_column($list, 's'); sort($sort); array_multisort($sort, SORT_ASC, $list); $sort = array_unique($sort); $list = ['sort' => $sort, 'arr' => $list]; } else { $list = ['sort' => [], 'arr' => []]; } View::assign('designers', $list); //客户等级 $level = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', '=', 'level']])->find()->toArray(); View::assign('level', $level['select']); //指派经理 $assignemp_list = Employee::where([['org_id', 'in', orgSubIds($root_id)], ['id', 'not in', array_column($list['arr'], 'id')], ['state', '=', '在职'], ['uid', '>', 0]])->column('id,name'); View::assign('assignemp_list', $assignemp_list); //指派部门 $assignorg_list = Org::where([['path', 'like', $root_id . '-%']])->column('id,name'); View::assign('assignorg_list', $assignorg_list); $assign_type = [['id' => 1, 'name' => '意向指派'], ['id' => 2, 'name' => '量房指派'], ['id' => 3, 'name' => '到店指派']]; View::assign('assign_type', $assign_type); return View::fetch(); } //$org_employee = [$empid]; if ($is_manager == 1) { $team_orgs = orgSubIds($request->empcrm->org_id); $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id'); } else { $org_employee = [$empid]; } $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')]; // $condition[] = ['employee_id|designer_id|assigned_personnel', 'find in set', $org_employee]; $condition1[] = ['assigned_personnel', 'not null', ""]; $condition1[] = ['designer_id', '>', 0]; $condition[] = ['employee_id', '>', 0]; $condition[] = ['died', '<>', 2]; if (!empty($param['assign_type_name'])) { $all_assign_type = [1 => 'yixiang', 2 => 'liangfang', 3 => 'daodian']; foreach (explode(',', $param['assign_type_name']) as $v) { if (!empty($all_assign_type[$v])) $now_assign_type[] = $all_assign_type[$v]; } //$param_assign_type = !empty($all_assign_type[$param['assign_type']]) ? $all_assign_type[$param['assign_type']] : ''; $condition[] = ['assign_type', 'in', $now_assign_type]; } //客户姓名 if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%']; //小区名称 if (!empty($param['community_name'])) $condition[] = ['community_name', 'like', '%' . $param['community_name'] . '%']; //等级 if (!empty($param['level'])) $condition[] = ['level', 'in', explode(',', $param['level'])]; //来源 if (!empty($param['source_name'])) $condition[] = ['source_id', 'in', explode(',', $param['source_name'])]; //所属员工 if (!empty($param['employee_name'])) $condition[] = ['employee_id', 'in', explode(',', $param['employee_name'])]; //所属设计师 if (!empty($param['designer_name'])) $condition[] = ['designer_id', 'in', explode(',', $param['designer_name'])]; //客户的跟进时间 if (!empty($param['last_contact_date'])) { $last_contact = explode(' - ', $param['last_contact_date']); $start = str_replace('/', '-', $last_contact[0]); $end = str_replace('/', '-', $last_contact[1]); $condition[] = ['last_contact_date', '>=', $start . ' 00:00:00']; $condition[] = ['last_contact_date', '<=', $end . ' 23:59:59']; } //指派时间查询 if (!empty($param['assign_time'])) { $assign_time = explode('-', $param['assign_time']); $start = $assign_time[0] . ' 00:00:00'; $end = $assign_time[1] . ' 23:59:59'; $condition[] = ['assign_time', '>', $start]; $condition[] = ['assign_time', '<', $end]; } $customerIdList = Customer::where($condition)->where(function ($query) use ($org_employee) { foreach ($org_employee as $v) { $query->whereOr([['employee_id|designer_id|assigned_personnel', 'find in set', $v]]); } })->where(function ($query) use ($condition1) { $query->whereOr($condition1); })->column('id'); unset($condition); $condition[] = ['id', 'in', $customerIdList]; //手机号 if (!empty($param['phone'])) { $list = Customer::where($condition)->order('addtime desc')->field('id,name,community_name,phone')->select()->toArray(); $customerIdList = []; foreach ($list as $v) { if (strpos($v['phone'], trim($param['phone'])) !== false) $customerIdList[] = $v['id']; } unset($condition); $condition[] = ['id', 'in', $customerIdList]; } //客户经理 if (!empty($param['assign_list'])) { $assign_employee = explode(',', $param['assign_list']); $customerIdList = Customer::where($condition)->where(function ($query) use ($assign_employee) { foreach ($assign_employee as $v) { $query->whereOr([['assigned_personnel', 'find in set', $v]]); } })->column('id'); unset($condition); $condition[] = ['id', 'in', $customerIdList]; } //指派部门 if (!empty($param['assign_org'])) { $assign_org = explode(',', $param['assign_org']); $all_assigids = []; $assigned_personnelList = Customer::where($condition)->column('assigned_personnel,id,designer_id'); foreach ($assigned_personnelList as $k => $v) { $assigned_personal = !empty($v['assigned_personnel']) ? explode(',', $v['assigned_personnel']) : []; if (!empty($v['designer_id'])) $assigned_personal[] = $v['designer_id']; $assign_orgid = Employee::where([['id', 'in', $assigned_personal], ['org_id', 'in', $assign_org], ['root_id', '=', $root_id]])->column('org_id'); if (!empty($assign_orgid)) $all_assigids[] = $v['id']; } unset($condition); $condition[] = ['id', 'in', $all_assigids]; } //到店时间 if (!empty($param['daodian_time'])) { $daodian_time = explode(' - ', $param['daodian_time']); if (empty($daodian_time)) return json(['code' => 1, 'msg' => '时间参数错误']); $start = str_replace('/', '-', $daodian_time[0]); $end = str_replace('/', '-', $daodian_time[1]); $customerIdList = Customer::where($condition)->column('id'); $logwhere[] = ['addtime', '>=', $start . ' 00:00:00']; $logwhere[] = ['addtime', '<=', $end . ' 23:59:59']; $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]; $logwhere[] = ['customer_id', 'in', $customerIdList]; $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id'); unset($condition); $condition[] = ['id', 'in', $customerIdList]; } //量房时间 if (!empty($param['liangfang_time'])) { $liangfang_time = explode(' - ', $param['liangfang_time']); if (empty($liangfang_time)) return json(['code' => 1, 'msg' => '时间参数错误']); $start = str_replace('/', '-', $liangfang_time[0]); $end = str_replace('/', '-', $liangfang_time[1]); $customerIdList = Customer::where($condition)->column('id'); $logwhere[] = ['addtime', '>=', $start . ' 00:00:00']; $logwhere[] = ['addtime', '<=', $end . ' 23:59:59']; $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')]; $logwhere[] = ['customer_id', 'in', $customerIdList]; $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id'); unset($condition); $condition[] = ['id', 'in', $customerIdList]; } //交定时间 if (!empty($param['jiaoding_time'])) { $jiaoding_time = explode(' - ', $param['jiaoding_time']); if (empty($jiaoding_time)) return json(['code' => 1, 'msg' => '时间参数错误']); $start = str_replace('/', '-', $jiaoding_time[0]); $end = str_replace('/', '-', $jiaoding_time[1]); $customerIdList = Customer::where($condition)->column('id'); $logwhere[] = ['addtime', '>=', $start . ' 00:00:00']; $logwhere[] = ['addtime', '<=', $end . ' 23:59:59']; $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]; $logwhere[] = ['customer_id', 'in', $customerIdList]; $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id'); unset($condition); $condition[] = ['id', 'in', $customerIdList]; } //签单时间 if (!empty($param['qiandan_time'])) { $qiandan_time = explode(' - ', $param['qiandan_time']); if (empty($qiandan_time)) return json(['code' => 1, 'msg' => '时间参数错误']); $start = str_replace('/', '-', $qiandan_time[0]); $end = str_replace('/', '-', $qiandan_time[1]); $customerIdList = Customer::where($condition)->column('id'); $logwhere[] = ['addtime', '>=', $start . ' 00:00:00']; $logwhere[] = ['addtime', '<=', $end . ' 23:59:59']; $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')]; $logwhere[] = ['customer_id', 'in', $customerIdList]; $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id'); unset($condition); $condition[] = ['id', 'in', $customerIdList]; } //交定金额 if (!empty($param['jiaoding_money'])) { $jiaoding_money = explode('-', $param['jiaoding_money']); if (empty($jiaoding_money)) return json(['code' => 1, 'msg' => '参数错误']); $customerIdList = Customer::where($condition)->column('id'); $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]; $logwhere[] = ['money', 'between', $jiaoding_money]; $logwhere[] = ['customer_id', 'in', $customerIdList]; $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id'); unset($condition); $condition[] = ['id', 'in', $customerIdList]; } //签单金额 if (!empty($param['qiandan_money'])) { $sign_money = explode('-', $param['qiandan_money']); if (empty($sign_money)) return json(['code' => 1, 'msg' => '参数错误']); $customerIdList = Customer::where($condition)->column('id'); $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')]; $logwhere[] = ['money', 'between', $sign_money]; $logwhere[] = ['customer_id', 'in', $customerIdList]; $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id'); unset($condition); $condition[] = ['id', 'in', $customerIdList]; } // 房屋面积 if (!empty($param['square'])) { $square = explode(',', $param['square']); $square_arr = [ 1 => '(square > 1 and square <= 80)', 2 => '(square > 81 and square <= 100)', 3 => '(square > 101 and square <= 120)', 4 => '(square > 121 and square <= 200)', 5 => '(square > 201 and square <= 500)', 6 => '(square > 501 and square <= 10000)' ]; $squ_whereOr = null; foreach ($square as $k => $v) { if (!empty($square_arr[$v])) $squ_whereOr .= $square_arr[$v] . ' OR'; } $customersList = Customer::where($condition)->where(function ($query) use ($squ_whereOr) { $query->whereRaw(trim($squ_whereOr, ' OR')); })->order('addtime desc')->column('id'); unset($condition); $condition[] = ['id', 'in', $customersList]; } //未跟进天数 if (!empty($param['no_visit_day'])) { $no_visit_day = explode(',', $param['no_visit_day']); $all_no_visit = [ 1 => [3, 7], 2 => [8, 14], 3 => [15, 30], 4 => [31, 10000] ]; $customersList = Customer::where($condition)->order('addtime desc')->column('id,addtime'); $wg_where[] = ['customer_id', 'in', array_column($customersList, 'id')]; $latest_visit_log = CustomerVisitLog::where($wg_where)->group('customer_id')->column('max(addtime) as addtime', 'customer_id'); //->select()->toArray(); $no_visit_ids = []; foreach ($customersList as $k => $v) { if (empty($latest_visit_log[$v['id']])) { $no_visit_long = ceil((time() - strtotime($v['addtime'])) / 86400); } else { $no_visit_long = ceil((time() - strtotime($latest_visit_log[$v['id']])) / 86400); } foreach ($no_visit_day as $m => $p) { if (empty($all_no_visit[$p])) continue; if ($no_visit_long >= $all_no_visit[$p][0] && $no_visit_long <= $all_no_visit[$p][1]) $no_visit_ids[] = $v['id']; } } unset($condition); $condition[] = ['id', 'in', $no_visit_ids]; } if (!empty($param['state'])) { $customerIdList = Customer::where($condition)->column('id'); $stateall = explode(',', $param['state']); $allsta = []; foreach ($stateall as $k => $v) { if ($v == '已签单') $v = '已交定'; if ($v == '已转单') $v = '已签单'; if (in_array($v, ['待确认', '有效'])) continue; $onestate = CustomerVisitLog::changeState($v, 'chaos'); $allsta = array_merge($onestate, $allsta); } $visit_where[] = ['customer_id', 'in', $customerIdList]; $visit_where[] = ['state', 'in', $allsta]; // $visit_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)]; // $visit_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)]; $logCustomersIdList = CustomerVisitLog::where($visit_where)->group('customer_id')->column('customer_id'); $c_ids = $logCustomersIdList; foreach ($stateall as $k => $v) { if ($v == '待确认') { $confirmedids = Customer::where($condition)->where([['state', 'in', Customer::changeState('待确认', 'chaos')], ['crm_res_id', 'NULL', null]])->column('id'); } if ($v == '有效') { $validids = Customer::where($condition)->where('state', 'not in', Customer::changeState('待确认', 'chaos'))->column('id'); } } unset($condition); if (!empty($confirmedids)) $c_ids = array_merge($confirmedids, $c_ids); if (!empty($validids)) $c_ids = array_merge($validids, $c_ids); unset($condition); $condition[] = ['id', 'in', $c_ids]; } $customers = Customer::with(['source' => function ($query) { $query->field('id,source'); }, 'visitLog' => function ($query) { $query->field('id,customer_id,state,addtime,confirm_date,money')->order('addtime asc'); }])->where($condition)->order('assign_time desc')->page($param['page'], $param['limit'])->select(); $count = Customer::where($condition)->count(); $data = $customers->toArray(); $pageIdList = array_column($data, 'id'); $employeeList = array_column($data, 'employee_id'); $designerList = array_column($data, 'designer_id'); $empIdList = array_merge($employeeList, $designerList, [$empid]); $empList = Employee::where([['id', 'in', $empIdList]])->column('name,org_id', 'id'); $orgList = Org::where([['id', 'in', array_column($empList, 'org_id')]])->column('name', 'id'); // $customers = $customers->visible(['id', 'name', 'sex', 'level', 'community_name', 'state'])->toArray(); // 查询跟踪记录 //$depositVisitLog = CustomerVisitLog::where([CustomerVisitLog::changeState(['state', '=', '已交定']), ['customer_id', 'in', $pageIdList]])->group('customer_id')->column('count(id) as num', 'customer_id'); //$signVisitLog = CustomerVisitLog::where([CustomerVisitLog::changeState(['state', '=', '已签单']), ['customer_id', 'in', $pageIdList]])->group('customer_id')->column('count(id) as num', 'customer_id'); // 状态统计获取 $customerIdList = !empty($data) ? array_column($data, 'id') : []; $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $customerIdList]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; $allstate = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0, 'allstate' => []]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已签单') { $s['state'] = '已转单'; $customersState[$s['customer_id']]['signed'] += $s['num']; } elseif ($s['state'] == '已交定') { $s['state'] = '已签单'; $customersState[$s['customer_id']]['deposit'] += $s['num']; } elseif ($s['state'] == '回访') $s['state'] = '待确认'; if (in_array($s['state'], ['回访', '待确认', '未到访', '已到店', '已量房', '已到场', '已交定', '已签单']) && !in_array($s['state'], $customersState[$s['customer_id']]['allstate'])) $customersState[$s['customer_id']]['allstate'][] = $s['state']; } $data = []; // 指派类型 $assign_type_default['yixiang'] = '意向客戶指派'; $assign_type_default['liangfang'] = '量房客戶指派'; $assign_type_default['daodian'] = '到店客戶指派'; foreach ($customers->toArray() as $item) { $daodian_time = $jiaoding_time = $jiaoding_money = $qiandan_time = $qiandan_money = $liangfang_time = $last_contact_date = ''; $state = []; if (!empty($item['visitLog'])) { $last_contact_date = !empty($item['visitLog'][count($item['visitLog']) - 1]['addtime']) ? $item['visitLog'][count($item['visitLog']) - 1]['addtime'] : ''; foreach ($item['visitLog'] as $k => $v) { $state[$v['state']][] = $v; } foreach ($state as $k => $v) { if ($k == '已交定') { $jiaoding_time = date('Y-m-d', strtotime($v[0]['addtime'])); $jiaoding_money = $v[0]['money']; } if ($k == '已签单') { $qiandan_time = date('Y-m-d', strtotime($v[0]['addtime'])); $qiandan_money = $v[0]['money']; } if ($k == '已量房') { $liangfang_time = date('Y-m-d', strtotime($v[0]['addtime'])); } if ($k == '已到店') $daodian_time = date('Y-m-d', strtotime($v[0]['addtime'])); } } $assignIdList = !empty($item['assigned_personnel']) ? explode(',', $item['assigned_personnel']) : []; //$assignIdList[] = $item['employee_id']; // 所属销售 //$assignIdList[] = $item['designer_id']; // 指派的设计师 if (!empty($item['designer_id']) && !empty($assignIdList)) $assignIdList = array_diff($assignIdList, [$item['designer_id']]); $assign_data = Employee::where('id', 'in', $assignIdList)->column('name,org_id'); $assign_list = !empty($assign_data) ? array_column($assign_data, 'name') : []; $assign_org = Org::where([['id', 'in', array_column($assign_data, 'org_id')]])->group('id')->column('name'); $assign_type_name = ''; if ($item['assign_type']) { if (isset($assign_type_default[$item['assign_type']])) { $assign_type_name = $assign_type_default[$item['assign_type']]; } } $allstate = !empty($customersState[$item['id']]['allstate']) ? $customersState[$item['id']]['allstate'] : []; if ($item['state'] != '待确认' && !empty($customersState[$item['id']]['allstate']) && count($customersState[$item['id']]['allstate']) > 1 && in_array('待确认', $customersState[$item['id']]['allstate'])) { $allstate = array_diff($customersState[$item['id']]['allstate'], ['待确认']); } $no_visit_state = ['未到访', '待确认']; $visit_state = ['已到店', '已量房', '已到场', '已签单', '已转单', '已卖卡']; // 有'已到店', '已量房', '已到场'状态的话不显示未到访 $no_jiaoji = array_intersect($no_visit_state, $allstate); if (count($no_jiaoji) == 2) { // 有未到访、待确认两个状态 $allstate = ['未到访']; } $jiaoji = array_intersect($visit_state, $allstate); if (count($state) > 1 && count($jiaoji) > 0) { $allstate = array_diff($allstate, $no_visit_state); } //if($item['state'] == '待确认') $allstate = [$item['state']]; $item['is_followup'] = 0; if ($is_manager == 1 && empty($path)) { $item['is_followup'] = 1; } if ($empid == $item['employee_id']) { $item['is_followup'] = 1; } $data[] = [ 'id' => $item['id'], 'name' => $item['name'], 'sex' => $item['sex'], 'source_name' => !empty($item['source']) ? $item['source']['source'] : '', 'no_visit_day' => ceil((strtotime(date('Y-m-d')) - strtotime($last_contact_date)) / 86400) > 0 ? ceil((strtotime(date('Y-m-d')) - strtotime($last_contact_date)) / 86400) : 0, 'org_id' => $item['org_id'], 'level' => $item['level'], 'phone' => substr_replace($item['phone'], '******', 3, 6), 'community_name' => $item['community_name'], 'state' => !empty($allstate) ? implode(',', $allstate) : '', 'employee_name' => !empty($empList[$item['employee_id']]['name']) ? $empList[$item['employee_id']]['name'] : '', 'employeeOrg' => [ 'name' => $orgList[$empList[$item['employee_id']]['org_id']] ], 'designer_name' => !empty($empList[$item['designer_id']]['name']) ? $empList[$item['designer_id']]['name'] : '', 'designerOrg' => empty($item['designer_id']) ? null : [ 'name' => $orgList[$empList[$item['designer_id']]['org_id']] ], 'btn_type' => !empty($arr[$item['id']]) ? $arr[$item['id']] : 0, ////1到店,3量房 'square' => !empty($item['square']) ? $item['square'] : '', //房屋面积 'agents' => !empty($item['agents_id']) ? $item['agents_id'] : '', //是否是经纪人推荐的 'stateNum' => !empty($customersState[$item['id']]) ? $customersState[$item['id']] : ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0], 'assign_time' => !empty($item['assign_time']) ? $item['assign_time'] : '', 'assign_list' => !empty($assign_list) ? implode(',', $assign_list) : '', 'assign_org' => !empty($assign_org) ? implode(',', $assign_org) : '', 'assign_type' => $item['assign_type'], 'assigned_personnel' => $item['assigned_personnel'], 'assign_type_name' => $assign_type_name, 'allstate' => !empty($allstate) ? implode(',', $allstate) : '', 'jiaoding_time' => !empty($jiaoding_time) ? $jiaoding_time : '', 'jiaoding_money' => !empty($jiaoding_money) ? $jiaoding_money : '', 'qiandan_time' => !empty($qiandan_time) ? $qiandan_time : '', 'qiandan_money' => !empty($qiandan_money) ? $qiandan_money : '', 'liangfang_time' => !empty($liangfang_time) ? $liangfang_time : '', 'last_contact_date' => $last_contact_date, 'is_followup' => $item['is_followup'], 'daodian_time' => !empty($daodian_time) ? $daodian_time : '', 'jiange_day' => ceil((strtotime(date('Y-m-d')) - strtotime($last_contact_date)) / 86400) ]; } return json(['code' => 0, 'data' => $data, 'count' => $count]); } }