empcrm->root_id; $today = date('Y-m-d H:i:s',time()); $firstday = $today; $lastday = date('Y-m-d 00:00:00', strtotime("$firstday -2 day")); $org_employee = Employee::where([['root_id','=',$root_id],['org_id','=',$request->empcrm->org_id],['community_disable','=',0]])->column('id'); $where[] = ['customer_employee_id','in',$org_employee]; $where[] = ['addtime', 'between', [$lastday,$firstday]]; $where[] = ['state', 'in', array_merge(CustomerVisitLog::changeState('已交定','chaos'),CustomerVisitLog::changeState('已签单','chaos'))]; $list = CustomerVisitLog::with(['employee'=>function($query){ $query->field('id,name'); }])->where($where)->order('addtime desc')->select()->toArray(); foreach($list as $key=>$val){ $list[$key]['package_name'] = !empty($val['package_id']) ? CustomerPackage::where('id',$val['package_id'])->value('name') :''; $cusdata = Customer::with(['designer'])->where('id',$val['customer_id'])->field('name,community_name,deposit_money,signed_money,addtime,designer_id')->find(); $cusdata['deposit_moneys'] = number_format($cusdata->getData('deposit_money'), 2, '.', ','); $cusdata['signed_moneys'] = number_format($cusdata->getData('signed_money'), 2, '.', ','); $list[$key]['cusdata'] = $cusdata; } $company_name = Company::where('root_id',$root_id)->value('company_name'); View::assign('company_name',$company_name); View::assign('list',$list); return View::fetch(); } /** * 登录记录 */ public function login_record(){ $subOrg = orgSubIds(request()->empcrm->org_id); $emp_list = Employee::where([['org_id', 'in', $subOrg], ['community_disable', '=', 0], ['state', '=', '在职'], ['uid', '>', 0]])->column('id,name,community_login_time'); $today = date('Y-m-d'); foreach ($emp_list as $k => $v) { $days = 0; if ($v['community_login_time']) { $login = date('Y-m-d', strtotime($v['community_login_time'])); if ($login == $today) { $days = '今'; } else { $str_days = strtotime($today) - strtotime($login); $days = $str_days / 86400; } } else { $days = '-'; } $emp_list[$k]['days'] = $days; } View::assign('emp_list', $emp_list); return View::fetch(); } /** * 运营报表 */ public function operate_report(){ $root_id = request()->empcrm->root_id; if (!request()->isAjax()) { $list = CommunityModel::where('root_id', '=', $root_id)->column('id,name'); View::assign('community', $list); return View::fetch(); } else { $community_id = input('community_id', '', 'intval'); $page = input('page', 1, 'intval'); $limit = input('limit', 10, 'intval'); $date = input('date', '', 'trim'); if ($date) { $dates = explode(' - ', $date); $start_date = date('Y-m-d 00:00:00', strtotime($dates[1])); $end_date = date('Y-m-d 23:59:59', strtotime($dates[0])); } else { // 默认近一个月 $start_date = date('Y-m-d 00:00:00'); $end_date = date('Y-m-d 23:59:59', time()- 30*86400); } $community = CommunityModel::where([['root_id', '=', $root_id], ['id', '=', $community_id]])->findOrEmpty(); if ($community->isEmpty()) { return json(['code'=> 0, 'data'=> [], 'msg'=> 'success']); } $days = []; $days_start = strtotime($start_date); $days_end = strtotime(date('Y-m-d', strtotime($end_date))); $i = 0; while (($days_start - $i * 86400) > $days_end){ $days[] = date('Y-m-d', $days_start - $i * 86400); $i++; } $count = count($days); $new_days = $page_days = array_slice($days, ($page - 1) * $limit, $limit); $page_start_date = array_pop($new_days) . '00:00:00'; $page_end_date = array_shift($new_days) . '23:59:59'; $subOrg = orgSubIds(request()->empcrm->org_id); $emp_list = Employee::where([['org_id', 'in', $subOrg], ['community_disable', '=', 0], ['state', '=', '在职']])->column('id'); $where[] = ['community_name', '=', $community['name']]; $where[] = ['employee_id', 'in', $emp_list]; $where[] = ['add_wechat_time|into_owner_group|reputation_gather|last_contact_date', '>=', $page_start_date]; $where[] = ['add_wechat_time|into_owner_group|reputation_gather|last_contact_date', '<=', $page_end_date]; $list = Customer::where($where)->field('id,add_wechat_time,into_owner_group,reputation_gather,last_contact_date')->select()->toArray(); $add_wechat_time = []; // 加微的 $into_owner_group = []; //加群的 $reputation_gather = []; //口碑采集时间 $last_contact_date = []; // 跟进的 $str_start_time = strtotime($page_start_date); $str_end_time = strtotime($page_end_date); foreach ($list as $k => $v) { // 加微处理 $v_time = !empty($v['add_wechat_time']) ? strtotime($v['add_wechat_time']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $add_wechat_time[date('Y-m-d', $v_time)][] = $v['id']; } // 加群处理 $v_time = !empty($v['into_owner_group']) ? strtotime($v['into_owner_group']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $into_owner_group[date('Y-m-d', $v_time)][] = $v['id']; } // 口碑采集处理 $v_time = !empty($v['reputation_gather']) ? strtotime($v['reputation_gather']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $reputation_gather[date('Y-m-d', $v_time)][] = $v['id']; } // 跟进的处理 $v_time = !empty($v['last_contact_date']) ? strtotime($v['last_contact_date']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $last_contact_date[date('Y-m-d', $v_time)][] = $v['id']; } } // 运营纪录查询 $b_where[] = ['root_id', '=', $root_id]; $b_where[] = ['community_id', '=', $community_id]; $b_where[] = ['record_date', '>=', $str_start_time]; $b_where[] = ['record_date', '<=', $str_end_time]; $b_list = CommunityBusiness::where($b_where)->select()->toArray(); $b_employee_ids = array_column($b_list, 'employee_id'); $b_employee_list = Employee::where('id', 'in', $b_employee_ids)->column('name', 'id'); $business_list = []; foreach ($b_list as $k => $v) { $v['employee_name'] = $b_employee_list[$v['employee_id']] ?? ''; $business_list[$v['record_date']] = $v; } $data = []; $emp_data = [ 'chat_group'=> 0, 'work_log'=> 0, 'group_script'=> 0, 'customer_worship'=> 0, 'building_output'=> 0, 'talk_drill'=> 0, 'material_production'=> 0, 'wei_opinions'=> 0, 'community_broadcast'=> 0, 'housetype_broadcast'=> 0, 'house_inspection_broadcast'=> 0, 'start_work_broadcast'=> 0, 'sample_room_broadcast'=> 0, 'anchoring_traffic'=> 0, 'sample_room_collect'=> 0, 'owner_buy'=> 0, 'owner_meeting'=> 0, 'construction_direct'=> 0, 'house_inspection'=> 0, 'home_dinner'=> 0, 'customer_thanks'=> 0, 'housetype_share'=> 0, 'decoration_saves_money'=> 0, 'decoration_closed_pit'=> 0, 'top_ten_spatial_planning'=> 0, 'decoration_acceptance_standards'=> 0, 'weekly_meeting'=> 0, 'month_meeting'=> 0, 'single_meeting'=> 0, 'train_meeting'=> 0, ]; foreach ($page_days as $k => $v) { $one['date'] = $v; $one['jiav'] = isset($add_wechat_time[$v]) ? count($add_wechat_time[$v]) : 0; $one['into_owner_group'] = isset($into_owner_group[$v]) ? count($into_owner_group[$v]) : 0; $one['reputation_gather'] = isset($reputation_gather[$v]) ? count($reputation_gather[$v]) : 0; $one['last_contact_date'] = isset($last_contact_date[$v]) ? count($last_contact_date[$v]) : 0; if (isset($business_list[$v])) { $one = array_merge($one, $business_list[$v]); } else { $one = array_merge($one, $emp_data); } $data[] = $one; } return json(['code'=> 0, 'data'=> $data, 'msg'=> 'success', 'count'=> $count]); } } /** * 运营纪录列表 */ public function operate_list() { $root_id = request()->empcrm->root_id; if (!request()->isAjax()) { $list = CommunityModel::where('root_id', '=', $root_id)->column('id,name'); View::assign('community', $list); return View::fetch(); } else { $community_id = input('community_id', '', 'intval'); $date = input('date', '', 'trim'); $page = input('page', 1, 'intval'); $limit = input('limit', 10, 'intval'); if ($community_id) { $where[] = ['community_id', '=', $community_id]; } if ($date) { $w_date = strtotime(date('Y-m-d', strtotime($date))); $where[] = ['record_date', '=', $w_date]; } $where[] = ['root_id', '=', $root_id]; $list = CommunityBusiness::with('communityName')->where($where)->page($page, $limit)->select()->toArray(); $count = CommunityBusiness::where($where)->count(); return json(['code'=> 0, 'data'=> $list, 'msg'=> 'success', 'count'=> $count]); } } /** * 添加运营纪录 */ public function operate_add() { $root_id = request()->empcrm->root_id; if (!request()->isAjax()) { $list = CommunityModel::where('root_id', '=', $root_id)->column('id,name'); View::assign('community', $list); return View::fetch(); } else { $param = $this->request->only(['community_id', 'record_date', 'chat_group', 'work_log', 'group_script', 'customer_worship', 'building_output', 'talk_drill', 'material_production', 'wei_opinions', 'community_broadcast', 'housetype_broadcast', 'house_inspection_broadcast', 'start_work_broadcast', 'sample_room_broadcast', 'anchoring_traffic', 'sample_room_collect', 'owner_buy', 'owner_meeting', 'construction_direct', 'house_inspection', 'home_dinner', 'customer_thanks', 'housetype_share', 'decoration_saves_money', 'decoration_closed_pit', 'top_ten_spatial_planning', 'decoration_acceptance_standards', 'weekly_meeting', 'month_meeting', 'single_meeting', 'train_meeting']); $param['employee_id'] = $this->request->empcrm->id; $param['root_id'] = $root_id; // 判断当天的记录是否存在 $f_where[] = ['root_id', '=', $root_id]; $f_where[] = ['record_date', '=', strtotime($param['record_date'])]; $f_where[] = ['community_id', '=', $param['community_id']]; $find = CommunityBusiness::where($f_where)->findOrEmpty(); if (!$find->isEmpty()) { return json(['code'=> 1, 'msg'=> '当天记录已存在', 'data'=> []]); } $result = CommunityBusiness::create($param); if ($result !== false) { return json(['code'=> 0, 'data'=> [], 'msg'=> '添加成功']); } else { return json(['code'=> 1, 'data'=> [], 'msg'=> '添加失败']); } } } /** * 修改运营纪录 */ public function operate_edit() { $root_id = request()->empcrm->root_id; if (!request()->isAjax()) { $list = CommunityModel::where('root_id', '=', $root_id)->column('id,name'); View::assign('community', $list); $id = input('id', '', 'intval'); $business = CommunityBusiness::where([['id', '=', $id], ['root_id', '=', $root_id]])->findOrEmpty(); View::assign('data', $business); return View::fetch(); } else { $param = $this->request->only(['id', 'community_id', 'record_date', 'chat_group', 'work_log', 'group_script', 'customer_worship', 'building_output', 'talk_drill', 'material_production', 'wei_opinions', 'community_broadcast', 'housetype_broadcast', 'house_inspection_broadcast', 'start_work_broadcast', 'sample_room_broadcast', 'anchoring_traffic', 'sample_room_collect', 'owner_buy', 'owner_meeting', 'construction_direct', 'house_inspection', 'home_dinner', 'customer_thanks', 'housetype_share', 'decoration_saves_money', 'decoration_closed_pit', 'top_ten_spatial_planning', 'decoration_acceptance_standards', 'weekly_meeting', 'month_meeting', 'single_meeting', 'train_meeting']); $find = CommunityBusiness::where([['id', '=', $param['id']], ['root_id', '=', request()->empcrm->root_id]])->findOrEmpty(); if ($find->isEmpty()) { return json(['code'=> 1, 'msg'=> '数据不存在', 'data'=> []]); } // 判断当天的记录是否存在 $f_where[] = ['root_id', '=', $root_id]; $f_where[] = ['record_date', '=', strtotime($param['record_date'])]; $f_where[] = ['community_id', '=', $param['community_id']]; $f_where[] = ['id', '<>', $param['id']]; $have_find = CommunityBusiness::where($f_where)->findOrEmpty(); if (!$have_find->isEmpty()) { return json(['code'=> 1, 'msg'=> '当天记录已存在', 'data'=> []]); } $result = $find->save($param); if ($result !== false) { return json(['code'=> 0, 'data'=> [], 'msg'=> '编辑成功']); } else { return json(['code'=> 1, 'data'=> [], 'msg'=> '编辑失败']); } } } /** * 汇总报表 */ public function summary_report(){ if (!request()->isAjax()) { return View::fetch(); } else { $invalid = input('invalid', '', 'intval'); $page = input('page', 1, 'intval'); $limit = input('limit', 10, 'intval'); $root_id = $this->request->empcrm->root_id; $community_ids = Building::where([['root_id', '=', $root_id], ['del', '=', 0]])->group('community_id')->column('community_id'); $community_list = CommunityModel::where([['root_id', '=', $root_id], ['id', 'in', $community_ids]])->page($page, $limit)->column('id,name,households'); $count = CommunityModel::where([['root_id', '=', $root_id], ['id', 'in', $community_ids]])->count(); // 社群员工 $subOrg = orgSubIds(request()->empcrm->org_id); $emp_list = Employee::where([['org_id', 'in', $subOrg], ['community_disable', '=', 0], ['state', '=', '在职'], ['uid', '>', 0]])->column('id'); // 所有小区名称 $community_names = array_column($community_list, 'name'); // 见面状态 $state1 = CustomerVisitLog::changeState('已到店', 'chaos'); $state2 = CustomerVisitLog::changeState('已量房', 'chaos'); $state3 = CustomerVisitLog::changeState('已到场', 'chaos'); $state4 = CustomerVisitLog::changeState('已交定', 'chaos'); $state5 = CustomerVisitLog::changeState('已签单', 'chaos'); $visit_state = array_merge($state1, $state2, $state3, $state4, $state5); $where[] = ['Customer.employee_id', 'in', $emp_list]; $where[] = ['Customer.community_name', 'in', $community_names]; $where[] = ['Customer.died', '<>', 2]; $where[] = ['Customer.state', 'not in', Customer::changeState('无效', 'chaos')]; // $where[] = ['CustomerVisitLog.state', 'in', array_merge($state1, $state2, $state3, $state4, $state5)]; $customer = Db::view('Customer') ->view('CustomerVisitLog', 'id', 'Customer.id=CustomerVisitLog.customer_id') ->where($where) ->where(function($query){ $not_sure = Customer::changeState('待确认', 'chaos'); $or1[] = ['Customer.crm_res_id', 'null', null]; $or2[] = ['Customer.state', 'not in', $not_sure]; $or2[] = ['Customer.crm_res_id', 'not null', null]; $or3[] = ['Customer.valid_time', 'not null', null]; $query->whereOr([$or1, $or2, $or3]); }) ->column('Customer.id,Customer.community_name,Customer.add_wechat_time,Customer.into_owner_group,Customer.deposit_money,Customer.signed_money,CustomerVisitLog.state'); $customer_data = []; // 数据 $customer_count = []; // 小区客户数组 $customer_state4 = []; // 收定客户数组 $customer_state5 = []; // 签单客户数组 $customer_wechat = []; // 加微客户数组 $customer_visit = []; // 见面客户数组 $customer_group = []; // 进群客户数组 foreach ($customer as $k => $v) { if (empty($customer_data[$v['community_name']])) { $customer_data[$v['community_name']] = []; $customer_data[$v['community_name']]['count'] = 0; $customer_data[$v['community_name']]['deposit'] = 0; $customer_data[$v['community_name']]['signed'] = 0; $customer_data[$v['community_name']]['visit'] = 0; $customer_data[$v['community_name']]['wechat'] = 0; $customer_data[$v['community_name']]['into_group'] = 0; $customer_data[$v['community_name']]['signed_money'] = 0; } // 小区客户 if (!in_array($v['id'], $customer_count)) { $customer_count[] = $v['id']; $customer_data[$v['community_name']]['count'] ++; } // 见面 if (!in_array($v['id'], $customer_visit) && in_array($v['state'], $visit_state)) { $customer_visit[] = $v['id']; $customer_data[$v['community_name']]['visit'] ++; } // 定金 if (in_array($v['state'], $state4, true) && !in_array($v['id'], $customer_state4)) { $customer_state4[] = $v['id']; $customer_data[$v['community_name']]['deposit'] ++; } // 签单 if (in_array($v['state'], $state5, true) && !in_array($v['id'], $customer_state5)) { $customer_state5[] = $v['id']; $customer_data[$v['community_name']]['signed'] ++; $customer_data[$v['community_name']]['signed_money'] += $v['signed_money']; } // 加微 if (!empty($v['add_wechat_time']) && !in_array($v['id'], $customer_wechat)) { $customer_wechat[] = $v['id']; $customer_data[$v['community_name']]['wechat'] ++; } // 进群 if (!empty($v['into_owner_group']) && !in_array($v['id'], $customer_group)) { $customer_group[] = $v['id']; $customer_data[$v['community_name']]['into_group'] ++; } } // 包含无效 $subOrg = orgSubIds(request()->empcrm->org_id); $i_where[] = ['Customer.org_id', 'in', $subOrg]; $i_where[] = ['Customer.community_name', 'in', $community_names]; $i_where[] = ['Customer.state', 'in', Customer::changeState('无效', 'chaos')]; $i_where[] = ['CustomerVisitLog.state', 'in', array_merge($state1, $state2, $state3, $state4, $state5)]; $i_customer = Db::view('Customer') ->view('CustomerVisitLog', 'id', 'Customer.id=CustomerVisitLog.customer_id') ->where($i_where) ->column('Customer.id,Customer.community_name,Customer.add_wechat_time,Customer.into_owner_group,Customer.deposit_money,Customer.signed_money,CustomerVisitLog.state'); $customer_invalid_data = []; // 无效数据 $customer_invalid_count = []; // 无效的客户数量 $customer_invalid_visit = []; // 无效的客户见面 $customer_invalid_deposit = []; // 无效的客户交定 foreach ($i_customer as $k => $v) { if (empty($customer_invalid_data[$v['community_name']])) { $customer_invalid_data[$v['community_name']] = []; $customer_invalid_data[$v['community_name']]['invalid_count'] = 0; $customer_invalid_data[$v['community_name']]['invalid_visit'] = 0; $customer_invalid_data[$v['community_name']]['invalid_deposit'] = 0; } // 无效的客户数量 if (!in_array($v['id'], $customer_invalid_count)) { $customer_invalid_count[] = $v['id']; $customer_invalid_data[$v['community_name']]['invalid_count'] ++; } // 无效的客户见面 if (!in_array($v['id'], $customer_invalid_visit)) { $customer_invalid_visit[] = $v['id']; $customer_invalid_data[$v['community_name']]['invalid_visit'] ++; } // 无效的客户交定 if (in_array($v['state'], $state4, true) && !in_array($v['id'], $customer_invalid_deposit)) { $customer_invalid_deposit[] = $v['id']; $customer_invalid_data[$v['community_name']]['invalid_deposit'] ++; } } $empty_data = [ 'count'=> 0, 'deposit' => 0, 'signed' => 0, 'visit' => 0, 'wechat' => 0, 'into_group' => 0, 'signed_money' => 0 ]; $empty_invalid_data = [ 'invalid_count'=> 0, 'invalid_visit'=> 0, 'invalid_deposit'=> 0 ]; foreach ($community_list as $k => $v) { if (isset($customer_data[$v['name']])) { $v = array_merge($v, $customer_data[$v['name']]); } else { $v = array_merge($v, $empty_data); } if (isset($customer_invalid_data[$v['name']])) { $v = array_merge($v, $customer_invalid_data[$v['name']]); } else { $v = array_merge($v, $empty_invalid_data); } $community_list[$k] = $v; $community_list[$k]['wechat_lv'] = $v['count'] > 0 ? round($v['wechat'] / $v['count'] * 100, 2) : $v['wechat'] * 100; $community_list[$k]['into_group_lv'] = $v['wechat'] > 0 ? round($v['into_group'] / $v['wechat'] * 100, 2) : $v['into_group'] * 100; $community_list[$k]['visit_lv'] = $v['wechat'] > 0 ? round($v['visit'] / $v['wechat'] * 100, 2) : $v['visit'] * 100; $community_list[$k]['deposit_lv'] = $v['visit'] > 0 ? round($v['deposit'] / $v['visit'] * 100, 2) : $v['deposit'] * 100; } return json(['code'=> 0, 'data'=> $community_list, 'msg'=> 'success', 'count'=> $count]); } } /** * 业务报表 */ public function employee_report(){ $root_id = request()->empcrm->root_id; if (!request()->isAjax()) { $list = CommunityModel::where('root_id', '=', $root_id)->column('id,name'); View::assign('community', $list); $theorg = Org::find(request()->empcrm->org_id); $org = Org::where([['path', 'like', $theorg->path . '%']])->column('id,name'); View::assign('org', $org); return View::fetch(); } else { $community_id = input('community_id', '', 'intval'); $search_date = input('date', '', 'trim'); $invalid = input('invalid', '', 'trim'); $org_id = input('org_id', request()->empcrm->org_id, 'intval'); // 小区 if ($community_id) { $community = CommunityModel::where([['id', '=', $community_id], ['root_id', '=', $root_id]])->findOrEmpty(); // 小区不存在直接返回 if ($community->isEmpty()) { return json(['code'=> 0, 'data'=> [], 'msg'=> 'success', 'count'=> 0]); } } if ($search_date) { $date = explode(' - ', $search_date); $start_date = date('Y-m-d 00:00:00', strtotime($date[0])); $end_date = date('Y-m-d 23:59:59', strtotime($date[1])); } else { // 默认本月 $start_date = date('Y-m-01 00:00:00'); $end_date = date('Y-m-d 23:59:59'); } $subOrg = orgSubIds($org_id); // 社群员工 $emp_list = Employee::where([['org_id', 'in', $subOrg], ['community_disable', '=', 0], ['state', '=', '在职'], ['uid', '>', 0]])->column('id,name'); // 有效客户总量 $valid_where[] = ['Employee.org_id', 'in', $subOrg]; $valid_where[] = ['Employee.community_disable', '=', 0]; $valid_where[] = ['Employee.state', '=', '在职']; $valid_where[] = ['Employee.uid', '>', 0]; $valid_where[] = ['Customer.sign_time', 'between', [$start_date, $end_date]]; if ($community_id) { $valid_where[] = ['Customer.community_name', '=', $community['name']]; } $customer_valid = Db::view('Employee') ->view('Customer', 'id cid', 'Customer.employee_id=Employee.id') ->where($valid_where) ->column('Customer.id,Customer.employee_id'); $customer_count = []; foreach ($customer_valid as $k => $v){ $customer_count[$v['employee_id']][] = $v['id']; } // 加微、进群 $m_where[] = ['Employee.org_id', 'in', $subOrg]; $m_where[] = ['Employee.community_disable', '=', 0]; $m_where[] = ['Employee.state', '=', '在职']; $m_where[] = ['Employee.uid', '>', 0]; $m_where[] = ['Customer.add_wechat_time|Customer.into_owner_group', '>=', $start_date]; $m_where[] = ['Customer.add_wechat_time|Customer.into_owner_group', '<=', $end_date]; if ($community_id) { $m_where[] = ['Customer.community_name', '=', $community['name']]; } $emp_customer = Db::view('Employee') ->view('Customer', 'id cid', 'Customer.employee_id=Employee.id') ->where($m_where) ->column('Customer.id,Customer.employee_id,Customer.add_wechat_time,Customer.into_owner_group,Customer.reputation_gather'); $add_wechat_time = []; // 加微的 $into_owner_group = []; //加群的 $str_start_time = strtotime($start_date); $str_end_time = strtotime($end_date); foreach ($emp_customer as $k => $v) { // 加微处理 $v_time = !empty($v['add_wechat_time']) ? strtotime($v['add_wechat_time']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $add_wechat_time[$v['employee_id']][] = $v['id']; } // 加群处理 $v_time = !empty($v['into_owner_group']) ? strtotime($v['into_owner_group']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $into_owner_group[$v['employee_id']][] = $v['id']; } } // 见面状态 $state1 = CustomerVisitLog::changeState('已到店', 'chaos'); $state2 = CustomerVisitLog::changeState('已量房', 'chaos'); $state3 = CustomerVisitLog::changeState('已到场', 'chaos'); $state4 = CustomerVisitLog::changeState('已交定', 'chaos'); $state5 = CustomerVisitLog::changeState('已签单', 'chaos'); $db_where[] = ['Employee.org_id', 'in', $subOrg]; $db_where[] = ['Employee.community_disable', '=', 0]; $db_where[] = ['Employee.state', '=', '在职']; $db_where[] = ['Employee.uid', '>', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]]; $db_where[] = ['CustomerVisitLog.state', 'in', array_merge($state1, $state2, $state3, $state4, $state5)]; if ($community_id) { $db_where[] = ['Customer.community_name', '=', $community['name']]; } $customer = Db::view('Customer') ->view('CustomerVisitLog', 'id', 'Customer.id=CustomerVisitLog.customer_id') ->view('Employee', 'id', 'CustomerVisitLog.customer_employee_id=Employee.id') ->where($db_where) ->column('Customer.id,CustomerVisitLog.customer_employee_id employee_id,CustomerVisitLog.state,Customer.deposit_money,Customer.signed_money'); $customer_data = []; // 数据 未做去重 foreach ($customer as $k => $v){ if (empty($customer_data[$v['employee_id']])) { $customer_data[$v['employee_id']] = []; $customer_data[$v['employee_id']]['deposit'] = []; $customer_data[$v['employee_id']]['signed'] = []; $customer_data[$v['employee_id']]['visit'] = []; $customer_data[$v['employee_id']]['signed_money'] = 0; } // 见面 $customer_data[$v['employee_id']]['visit'][] = $v['id']; // 定金 if (in_array($v['state'], $state4, true)) { $customer_data[$v['employee_id']]['deposit'][] = $v['id']; } // 签单 if (in_array($v['state'], $state5, true)) { $customer_data[$v['employee_id']]['signed'][] = $v['id']; } } // 处理业绩 交定+签单 签单的客户不计算交定金额 $customer_deposit = []; foreach ($customer as $k => $v){ // 用于判断这个客户是否记录过数据 if (empty($customer_deposit[$v['employee_id']])) { $customer_deposit[$v['employee_id']] = []; $customer_deposit[$v['employee_id']]['deposit'] = []; //收定客户数组 $customer_deposit[$v['employee_id']]['signed'] = []; //签单客户数组 } // 定金 if (in_array($v['state'], $state4, true) && !in_array($v['id'], $customer_deposit[$v['employee_id']]['deposit'])) { $customer_deposit[$v['employee_id']]['deposit'][] = $v['id']; // 不是转单过的,才加定金 if (!in_array($v['id'], $customer_data[$v['employee_id']]['signed'])) { $customer_data[$v['employee_id']]['signed_money'] += $v['deposit_money']; } } // 签单 if (in_array($v['state'], $state5, true) && !in_array($v['id'], $customer_deposit[$v['employee_id']]['signed'])) { $customer_deposit[$v['employee_id']]['signed'][] = $v['id']; $customer_data[$v['employee_id']]['signed_money'] += $v['signed_money']; } } // 包含无效 $i_where[] = ['Employee.org_id', 'in', $subOrg]; $i_where[] = ['Employee.community_disable', '=', 0]; $i_where[] = ['Employee.state', '=', '在职']; $i_where[] = ['Employee.uid', '>', 0]; $i_where[] = ['CustomerInvalidLog.cus_addtime|CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]]; if ($community_id) { $i_where[] = ['Customer.community_name', '=', $community['name']]; } $i_customer = Db::view('CustomerInvalidLog') ->view('Employee', 'id eid', 'Employee.id=CustomerInvalidLog.employee_id') ->view('Customer', 'id cid', 'Customer.id=CustomerInvalidLog.customer_id') ->view('CustomerVisitLog', 'id log_id', 'Customer.id=CustomerVisitLog.customer_id') ->where($i_where) ->column('CustomerInvalidLog.id,CustomerInvalidLog.status,CustomerInvalidLog.cus_addtime,Customer.id customer_id,Customer.add_wechat_time,Customer.into_owner_group,CustomerVisitLog.id log_id,CustomerVisitLog.customer_employee_id employee_id,CustomerVisitLog.state,CustomerVisitLog.confirm_date'); $customer_invalid_data = []; // 无效数据 数据没有去重 foreach ($i_customer as $k => $v) { if (empty($customer_invalid_data[$v['employee_id']])) { $customer_invalid_data[$v['employee_id']] = []; $customer_invalid_data[$v['employee_id']]['count'] = []; // 某个员工下的置为无效的 $customer_invalid_data[$v['employee_id']]['valid'] = []; // 某个员工下置为无效的时候是有效的 $customer_invalid_data[$v['employee_id']]['visit'] = []; // 某个员工下置为无效的时候有见面 $customer_invalid_data[$v['employee_id']]['deposit'] = []; // 某个员工下置为无效的时候有交定 $customer_invalid_data[$v['employee_id']]['wechat'] = []; // 某个员工下置为无效的时候有加微 $customer_invalid_data[$v['employee_id']]['into_group'] = []; // 某个员工下置为无效的时候有进群 } // 无效的客户 if ($str_start_time <= strtotime($v['cus_addtime']) && strtotime($v['cus_addtime']) <= $str_end_time) { $customer_invalid_data[$v['employee_id']]['count'][] = $v['customer_id']; // 无效的客户曾经有效 if ($v['status'] == 1) { $customer_invalid_data[$v['employee_id']]['valid'][] = $v['customer_id']; } } if ($str_start_time <= strtotime($v['confirm_date']) && strtotime($v['confirm_date']) <= $str_end_time) { // 无效的客户见面 $visit_state = array_merge($state1, $state2, $state3, $state4, $state5); if (in_array($v['state'], $visit_state, true)) { $customer_invalid_data[$v['employee_id']]['visit'][] = $v['customer_id']; } // 无效的客户交定 if (in_array($v['state'], $state4, true)) { $customer_invalid_data[$v['employee_id']]['deposit'][] = $v['customer_id']; } } // 加微处理 $v_time = !empty($v['add_wechat_time']) ? strtotime($v['add_wechat_time']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $customer_invalid_data[$v['employee_id']]['wechat'][] = $v['customer_id']; } // 加群处理 $v_time = !empty($v['into_owner_group']) ? strtotime($v['into_owner_group']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $customer_invalid_data[$v['employee_id']]['into_group'][] = $v['customer_id']; } } $empty_invalid_data = [ 'invalid_valid'=> 0, 'invalid_visit'=> 0, 'invalid_deposit'=> 0 ]; // 每个员工下的信息量 foreach ($emp_list as $k => $v) { // 客户量 $v_count = $customer_count[$v['id']] ?? []; // 现有客户id数组 $v_count_valid = $customer_invalid_data[$v['id']]['count'] ?? []; // 无效客户id数组 $v_count_merge = count(array_unique(array_merge($v_count, $v_count_valid))); $emp_list[$k]['count'] = $v_count_merge; // 加微量 加微率 $v_wechat = $add_wechat_time[$v['id']] ?? []; $v_wechat_valid = $customer_invalid_data[$v['id']]['wechat'] ?? []; $v_wechat_merge = count(array_unique(array_merge($v_wechat, $v_wechat_valid))); $emp_list[$k]['wechat'] = $v_wechat_merge; $emp_list[$k]['wechat_lv'] = ($v_count_merge > 0 ? round($v_wechat_merge / $v_count_merge * 100, 2) : $v_wechat_merge * 100) . '%'; // 进群量 进群率 $into_group = $into_owner_group[$v['id']] ?? []; $into_group_valid = $customer_invalid_data[$v['id']]['into_group'] ?? []; $v_into_group_merge = count(array_unique(array_merge($into_group, $into_group_valid))); $emp_list[$k]['into_group'] = $v_into_group_merge; $emp_list[$k]['into_group_lv'] = ($v_wechat_merge > 0 ? round($v_into_group_merge / $v_wechat_merge * 100, 2) : $v_into_group_merge * 100) . '%'; // 见面 见面率 $v_visit = $customer_data[$v['id']]['visit'] ?? []; $v_visit_valid = $customer_invalid_data[$v['id']]['visit'] ?? []; $v_visit_merge = count(array_unique(array_merge($v_visit, $v_visit_valid))); $emp_list[$k]['visit'] = $v_visit_merge; $emp_list[$k]['visit_lv'] = ($v_wechat_merge > 0 ? round($v_visit_merge / $v_wechat_merge * 100, 2) : $v_visit_merge * 100) . '%'; // 定金 成单率 $v_deposit = $customer_data[$v['id']]['deposit'] ?? []; $v_deposit_valid = $customer_invalid_data[$v['id']]['deposit'] ?? []; $v_deposit_merge = count(array_unique(array_merge($v_deposit, $v_deposit_valid))); $emp_list[$k]['deposit'] = $v_deposit_merge; $emp_list[$k]['deposit_lv'] = ($v_visit_merge > 0 ? round($v_deposit_merge / $v_visit_merge * 100, 2) : $v_deposit_merge * 100) . '%'; // 签单 $v_signed = $customer_data[$v['id']]['signed'] ?? []; $v_signed_merge = count(array_unique($v_signed)); $emp_list[$k]['signed'] = $v_signed_merge; // 总业绩 $emp_list[$k]['signed_money'] = $customer_data[$v['id']]['signed_money'] ?? 0; if (isset($customer_invalid_data[$v['id']])) { $invalid_data = [ 'invalid_valid'=> count($customer_invalid_data[$v['id']]['valid']), 'invalid_visit'=> count($customer_invalid_data[$v['id']]['visit']), 'invalid_deposit'=> count($customer_invalid_data[$v['id']]['deposit']) ]; $emp_list[$k] = array_merge($emp_list[$k], $invalid_data); } else { $emp_list[$k] = array_merge($emp_list[$k], $empty_invalid_data); } } // 求和 $total['name'] = '求和'; $total['count'] = array_sum(array_column($emp_list, 'count')); $total['wechat'] = array_sum(array_column($emp_list, 'wechat')); $total['wechat_lv'] = ($total['count'] > 0 ? round($total['wechat'] / $total['count'] * 100, 2) : $total['wechat'] * 100) . '%'; $total['into_group'] = array_sum(array_column($emp_list, 'into_group')); $total['into_group_lv'] = ($total['wechat'] > 0 ? round($total['into_group'] / $total['wechat'] * 100, 2) : $total['into_group'] * 100) . '%'; $total['visit'] = array_sum(array_column($emp_list, 'visit')); $total['visit_lv'] = ($total['wechat'] > 0 ? round($total['visit'] / $total['wechat'] * 100, 2) : $total['visit'] * 100) . '%'; $total['deposit'] = array_sum(array_column($emp_list, 'deposit')); $total['deposit_lv'] = ($total['visit'] > 0 ? round($total['deposit'] / $total['visit'] * 100, 2) : $total['deposit'] * 100) . '%'; $total['signed'] = array_sum(array_column($emp_list, 'signed')); $total['signed_money'] = array_sum(array_column($emp_list, 'signed_money')); $total['invalid_valid'] = array_sum(array_column($emp_list, 'invalid_valid')); $total['invalid_visit'] = array_sum(array_column($emp_list, 'invalid_visit')); $total['invalid_deposit'] = array_sum(array_column($emp_list, 'invalid_deposit')); $emp_list[] = $total; return json(['code'=> 0, 'data'=> $emp_list, 'msg'=> 'success']); } } /** * 小区报表 */ public function community_report(){ if (!request()->isAjax()) { // 社群员工 $subOrg = orgSubIds(request()->empcrm->org_id); $emp_list = Employee::where([['org_id', 'in', $subOrg], ['community_disable', '=', 0], ['state', '=', '在职'], ['uid', '>', 0]])->column('id,name'); View::assign('employee', $emp_list); return View::fetch(); } else { $invalid = input('invalid', '', 'intval'); $employee_id = input('employee_id', '', 'intval'); $search_date = input('date', '', 'trim'); if ($search_date) { $date = explode(' - ', $search_date); $start_date = date('Y-m-d 00:00:00', strtotime($date[0])); $end_date = date('Y-m-d 23:59:59', strtotime($date[1])); } else { // 默认本月 $start_date = date('Y-m-01 00:00:00'); $end_date = date('Y-m-d 23:59:59'); } $root_id = request()->empcrm->root_id; $community_ids = Building::where([['root_id', '=', $root_id], ['del', '=', 0]])->group('community_id')->column('community_id'); $community_list = CommunityModel::where([['id', 'in', $community_ids]])->column('id,name,households'); // 社群员工 $subOrg = orgSubIds(request()->empcrm->org_id); $emp_list = Employee::where([['org_id', 'in', $subOrg], ['community_disable', '=', 0], ['state', '=', '在职'], ['uid', '>', 0]])->column('id'); // 所有小区名称 $community_names = array_column($community_list, 'name'); if ($employee_id) { $valid_where[] = ['Employee.id', '=', $employee_id]; } else { $valid_where[] = ['Employee.org_id', 'in', $subOrg]; $valid_where[] = ['Employee.community_disable', '=', 0]; $valid_where[] = ['Employee.state', '=', '在职']; $valid_where[] = ['Employee.uid', '>', 0]; } $valid_where[] = ['Customer.sign_time', 'between', [$start_date, $end_date]]; $valid_where[] = ['Customer.community_name', 'in', $community_names]; // 客户数量 $customer_valid = Db::view('Employee') ->view('Customer', 'id cid', 'Customer.employee_id=Employee.id') ->where($valid_where) ->column('Customer.id,Customer.community_name'); $customer_count = []; foreach ($customer_valid as $k => $v){ $customer_count[$v['community_name']][] = $v['id']; } // 加微、进群 $m_where[] = ['Employee.org_id', 'in', $subOrg]; $m_where[] = ['Employee.community_disable', '=', 0]; $m_where[] = ['Employee.state', '=', '在职']; $m_where[] = ['Employee.uid', '>', 0]; $m_where[] = ['Customer.add_wechat_time|Customer.into_owner_group', '>=', $start_date]; $m_where[] = ['Customer.add_wechat_time|Customer.into_owner_group', '<=', $end_date]; $m_where[] = ['Customer.community_name', 'in', $community_names]; $emp_customer = Db::view('Employee') ->view('Customer', 'id cid', 'Customer.employee_id=Employee.id') ->where($m_where) ->column('Customer.id,Customer.community_name,Customer.add_wechat_time,Customer.into_owner_group,Customer.reputation_gather'); $add_wechat_time = []; // 加微的 $into_owner_group = []; //加群的 $str_start_time = strtotime($start_date); $str_end_time = strtotime($end_date); foreach ($emp_customer as $k => $v) { // 加微处理 $v_time = !empty($v['add_wechat_time']) ? strtotime($v['add_wechat_time']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $add_wechat_time[$v['community_name']][] = $v['id']; } // 加群处理 $v_time = !empty($v['into_owner_group']) ? strtotime($v['into_owner_group']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $into_owner_group[$v['community_name']][] = $v['id']; } } // 见面状态 $state1 = CustomerVisitLog::changeState('已到店', 'chaos'); $state2 = CustomerVisitLog::changeState('已量房', 'chaos'); $state3 = CustomerVisitLog::changeState('已到场', 'chaos'); $state4 = CustomerVisitLog::changeState('已交定', 'chaos'); $state5 = CustomerVisitLog::changeState('已签单', 'chaos'); $db_where[] = ['Employee.org_id', 'in', $subOrg]; $db_where[] = ['Employee.community_disable', '=', 0]; $db_where[] = ['Employee.state', '=', '在职']; $db_where[] = ['Employee.uid', '>', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]]; $db_where[] = ['Customer.community_name', 'in', $community_names]; $db_where[] = ['CustomerVisitLog.state', 'in', array_merge($state1, $state2, $state3, $state4, $state5)]; $customer = Db::view('Customer') ->view('CustomerVisitLog', 'id', 'Customer.id=CustomerVisitLog.customer_id') ->view('Employee', 'id', 'CustomerVisitLog.customer_employee_id=Employee.id') ->where($db_where) ->column('Customer.id,Customer.community_name,CustomerVisitLog.customer_employee_id employee_id,CustomerVisitLog.state,Customer.deposit_money,Customer.signed_money'); $customer_data = []; // 数据 未做去重 foreach ($customer as $k => $v){ if (empty($customer_data[$v['community_name']])) { $customer_data[$v['community_name']] = []; $customer_data[$v['community_name']]['deposit'] = []; $customer_data[$v['community_name']]['signed'] = []; $customer_data[$v['community_name']]['visit'] = []; $customer_data[$v['community_name']]['signed_money'] = 0; } // 见面 $customer_data[$v['community_name']]['visit'][] = $v['id']; // 定金 if (in_array($v['state'], $state4, true)) { $customer_data[$v['community_name']]['deposit'][] = $v['id']; } // 签单 if (in_array($v['state'], $state5, true)) { $customer_data[$v['community_name']]['signed'][] = $v['id']; } } // 处理业绩 交定+签单 签单的客户不计算交定金额 $customer_deposit = []; foreach ($customer as $k => $v){ // 用于判断这个客户是否记录过数据 if (empty($customer_deposit[$v['community_name']])) { $customer_deposit[$v['community_name']] = []; $customer_deposit[$v['community_name']]['deposit'] = []; //收定客户数组 $customer_deposit[$v['community_name']]['signed'] = []; //签单客户数组 } // 定金 if (in_array($v['state'], $state4, true) && !in_array($v['id'], $customer_deposit[$v['community_name']]['deposit'])) { $customer_deposit[$v['community_name']]['deposit'][] = $v['id']; // 不是转单过的,才加定金 if (!in_array($v['id'], $customer_data[$v['community_name']]['signed'])) { $customer_data[$v['community_name']]['signed_money'] += $v['deposit_money']; } } // 签单 if (in_array($v['state'], $state5, true) && !in_array($v['id'], $customer_deposit[$v['community_name']]['signed'])) { $customer_deposit[$v['community_name']]['signed'][] = $v['id']; $customer_data[$v['community_name']]['signed_money'] += $v['signed_money']; } } // 包含无效 $i_where[] = ['Employee.org_id', 'in', $subOrg]; $i_where[] = ['Employee.community_disable', '=', 0]; $i_where[] = ['Employee.state', '=', '在职']; $i_where[] = ['Employee.uid', '>', 0]; $i_where[] = ['Customer.community_name', 'in', $community_names]; $i_where[] = ['CustomerVisitLog.confirm_date|CustomerInvalidLog.cus_addtime', 'between', [$start_date, $end_date]]; $i_customer = Db::view('CustomerInvalidLog') ->view('Employee', 'id eid', 'Employee.id=CustomerInvalidLog.employee_id') ->view('Customer', 'id cid', 'Customer.id=CustomerInvalidLog.customer_id') ->view('CustomerVisitLog', 'id log_id', 'Customer.id=CustomerVisitLog.customer_id') ->where($i_where) ->column('CustomerInvalidLog.id,CustomerInvalidLog.status,CustomerInvalidLog.cus_addtime,Customer.id customer_id,Customer.community_name,Customer.add_wechat_time,Customer.into_owner_group,CustomerVisitLog.id log_id,CustomerVisitLog.customer_employee_id employee_id,CustomerVisitLog.state,CustomerVisitLog.confirm_date'); $customer_invalid_data = []; // 无效数据 数据没有去重 foreach ($i_customer as $k => $v) { if (empty($v['community_name'])) { continue; } if (empty($customer_invalid_data[$v['community_name']])) { $customer_invalid_data[$v['community_name']] = []; $customer_invalid_data[$v['community_name']]['count'] = []; // 某个员工下的置为无效的 $customer_invalid_data[$v['community_name']]['valid'] = []; // 某个员工下置为无效的时候是有效的 $customer_invalid_data[$v['community_name']]['visit'] = []; // 某个员工下置为无效的时候有见面 $customer_invalid_data[$v['community_name']]['deposit'] = []; // 某个员工下置为无效的时候有交定 $customer_invalid_data[$v['community_name']]['wechat'] = []; // 某个员工下置为无效的时候有加微 $customer_invalid_data[$v['community_name']]['into_group'] = []; // 某个员工下置为无效的时候有进群 } // 无效的客户 if ($str_start_time <= strtotime($v['cus_addtime']) && strtotime($v['cus_addtime']) <= $str_end_time) { $customer_invalid_data[$v['community_name']]['count'][] = $v['customer_id']; // 无效的客户曾经有效 if ($v['status'] == 1) { $customer_invalid_data[$v['community_name']]['valid'][] = $v['customer_id']; } } // 无效的客户 if ($str_start_time <= $v['confirm_date'] && $v['confirm_date'] <= $str_end_time) { // 无效的客户见面 $visit_state = array_merge($state1, $state2, $state3, $state4, $state5); if (in_array($v['state'], $visit_state, true)) { $customer_invalid_data[$v['community_name']]['visit'][] = $v['customer_id']; } // 无效的客户交定 if (in_array($v['state'], $state4, true)) { $customer_invalid_data[$v['community_name']]['deposit'][] = $v['customer_id']; } } // 加微处理 $v_time = !empty($v['add_wechat_time']) ? strtotime($v['add_wechat_time']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $customer_invalid_data[$v['community_name']]['wechat'][] = $v['customer_id']; } // 加群处理 $v_time = !empty($v['into_owner_group']) ? strtotime($v['into_owner_group']) : 0; if ($v_time && $str_start_time <= $v_time && $v_time <= $str_end_time) { $customer_invalid_data[$v['community_name']]['into_group'][] = $v['customer_id']; } } $empty_invalid_data = [ 'invalid_valid'=> 0, 'invalid_visit'=> 0, 'invalid_deposit'=> 0 ]; // 每个小区下的信息量 foreach ($community_list as $k => $v) { // 客户量 $v_count = $customer_count[$v['name']] ?? []; // 现有客户id数组 $v_count_valid = $customer_invalid_data[$v['name']]['count'] ?? []; // 无效客户id数组 $v_count_merge = count(array_unique(array_merge($v_count, $v_count_valid))); $community_list[$k]['count'] = $v_count_merge; // 加微量 加微率 $v_wechat = $add_wechat_time[$v['name']] ?? []; $v_wechat_valid = $customer_invalid_data[$v['name']]['wechat'] ?? []; $v_wechat_merge = count(array_unique(array_merge($v_wechat, $v_wechat_valid))); $community_list[$k]['wechat'] = $v_wechat_merge; $community_list[$k]['wechat_lv'] = ($v_count_merge > 0 ? round($v_wechat_merge / $v_count_merge * 100, 2) : $v_wechat_merge * 100) . '%'; // 进群量 进群率 $into_group = $into_owner_group[$v['name']] ?? []; $into_group_valid = $customer_invalid_data[$v['name']]['into_group'] ?? []; $v_into_group_merge = count(array_unique(array_merge($into_group, $into_group_valid))); $community_list[$k]['into_group'] = $v_into_group_merge; $community_list[$k]['into_group_lv'] = ($v_wechat_merge > 0 ? round($v_into_group_merge / $v_wechat_merge * 100, 2) : $v_into_group_merge * 100) . '%'; // 见面 见面率 $v_visit = $customer_data[$v['name']]['visit'] ?? []; $v_visit_valid = $customer_invalid_data[$v['name']]['visit'] ?? []; $v_visit_merge = count(array_unique(array_merge($v_visit, $v_visit_valid))); $community_list[$k]['visit'] = $v_visit_merge; $community_list[$k]['visit_lv'] = ($v_wechat_merge > 0 ? round($v_visit_merge / $v_wechat_merge * 100, 2) : $v_visit_merge * 100) . '%'; // 定金 成单率 $v_deposit = $customer_data[$v['name']]['deposit'] ?? []; //$v_deposit_valid = $customer_invalid_data[$v['name']]['deposit'] ?? []; $v_deposit_valid = []; // 置为无效的不加交定 $v_deposit_merge = count(array_unique(array_merge($v_deposit, $v_deposit_valid))); $community_list[$k]['deposit'] = $v_deposit_merge; $community_list[$k]['deposit_lv'] = ($v_visit_merge > 0 ? round($v_deposit_merge / $v_visit_merge * 100, 2) : $v_deposit_merge * 100) . '%'; // 签单 $v_signed = $customer_data[$v['name']]['signed'] ?? []; $v_signed_merge = count(array_unique($v_signed)); $community_list[$k]['signed'] = $v_signed_merge; // 总业绩 $community_list[$k]['signed_money'] = $customer_data[$v['name']]['signed_money'] ?? 0; if (isset($customer_invalid_data[$v['name']])) { $invalid_data = [ 'invalid_valid'=> count($customer_invalid_data[$v['name']]['valid']), 'invalid_visit'=> count($customer_invalid_data[$v['name']]['visit']), 'invalid_deposit'=> count($customer_invalid_data[$v['name']]['deposit']) ]; $community_list[$k] = array_merge($community_list[$k], $invalid_data); } else { $community_list[$k] = array_merge($community_list[$k], $empty_invalid_data); } } // 求和 $total['name'] = '求和'; $total['households'] = array_sum(array_column($community_list, 'households')); $total['count'] = array_sum(array_column($community_list, 'count')); $total['wechat'] = array_sum(array_column($community_list, 'wechat')); $total['wechat_lv'] = ($total['count'] > 0 ? round($total['wechat'] / $total['count'] * 100, 2) : $total['wechat'] * 100) . '%'; $total['into_group'] = array_sum(array_column($community_list, 'into_group')); $total['into_group_lv'] = ($total['wechat'] > 0 ? round($total['into_group'] / $total['wechat'] * 100, 2) : $total['into_group'] * 100) . '%'; $total['visit'] = array_sum(array_column($community_list, 'visit')); $total['visit_lv'] = ($total['wechat'] > 0 ? round($total['visit'] / $total['wechat'] * 100, 2) : $total['visit'] * 100) . '%'; $total['deposit'] = array_sum(array_column($community_list, 'deposit')); $total['deposit_lv'] = ($total['visit'] > 0 ? round($total['deposit'] / $total['visit'] * 100, 2) : $total['deposit'] * 100) . '%'; $total['signed'] = array_sum(array_column($community_list, 'signed')); $total['signed_money'] = array_sum(array_column($community_list, 'signed_money')); $total['invalid_valid'] = array_sum(array_column($community_list, 'invalid_valid')); $total['invalid_visit'] = array_sum(array_column($community_list, 'invalid_visit')); $total['invalid_deposit'] = array_sum(array_column($community_list, 'invalid_deposit')); $community_list[] = $total; return json(['code'=> 0, 'data'=> $community_list, 'msg'=> 'success']); } } /** * 弹框页面 */ public function open() { $employee_id = input('employee_id', '', 'intval'); //员工id $community_id = input('community_id', '', 'intval'); // 小区id $type = input('type', ''); // 数据类型 $module = input('module', '', 'trim'); //数据模块 $date = input('date', ''); $field = [ ['field' => 'name', 'title' => '客户姓名'], ['field' => 'community_name', 'title' => '小区名称'], ['field' => 'square', 'title' => '面积'], ['field' => 'employee_name', 'title' => '客服专员'], ['field' => 'designer_name', 'title' => '设计师'], ['field' => 'first_visit_time', 'title' => '首次会面'], ['field' => 'deposit_time', 'title' => '定金时间'], ['field' => 'sign_time', 'title' => '报名时间'], ['field' => 'state', 'title' => '状态'] ]; $url = url("community/openData", ['employee_id' => $employee_id, 'community_id' => $community_id, 'type' => $type, 'date' => $date, 'module'=> $module]); View::assign('fields', json_encode($field)); View::assign('url', $url); return View::fetch(); } /** * 弹框列表 */ public function openData() { $request = request(); $param = $request->only(['page' => 1, 'limit' => 10, 'employee_id' => 0, 'community_id' => 0, 'type' => '', 'date' => '', 'module'=> '']); if($param['date']){ $search_date = explode(' - ', $param['date']); $start_date = date('Y-m-d H:i:s', strtotime($search_date[0])); $end_date = date('Y-m-d 23:59:59', strtotime($search_date[1])); } else { $search_date = []; } $where = []; $root_id = request()->empcrm->root_id; $org_id = request()->empcrm->org_id; if ($param['module'] == 'community') { // 以小区为主线 $community = CommunityModel::where([['root_id', '=', $root_id], ['id', '=', $param['community_id']]])->findOrEmpty(); if ($community->isEmpty()) { return json(['code'=> 0, 'data'=> [], 'msg'=> 'success']); } if ($param['type'] == 'customer') { // 信息量 // 有效客户总量 $subOrg = orgSubIds($org_id); $valid_where[] = ['Employee.org_id', 'in', $subOrg]; $valid_where[] = ['Employee.community_disable', '=', 0]; $valid_where[] = ['Employee.state', '=', '在职']; $valid_where[] = ['Employee.uid', '>', 0]; $valid_where[] = ['Customer.community_name', '=', $community['name']]; if ($search_date) { $valid_where[] = ['Customer.sign_time', 'between', [$start_date, $end_date]]; } if ($param['employee_id']) { $valid_where[] = ['Customer.employee_id', '=', $param['employee_id']]; } // 有效客户id $customer_ids = Db::view('Employee') ->view('Customer', 'id', 'Customer.employee_id=Employee.id') ->where($valid_where) ->order('Customer.sign_time desc') ->column('Customer.id'); // 无效的客户id $invalid_where[] = ['Employee.org_id', 'in', $subOrg]; $invalid_where[] = ['Employee.community_disable', '=', 0]; $invalid_where[] = ['Employee.state', '=', '在职']; $invalid_where[] = ['Employee.uid', '>', 0]; $invalid_where[] = ['Customer.community_name', '=', $community['name']]; if ($search_date) { $invalid_where[] = ['CustomerInvalidLog.cus_addtime', 'between', [$start_date, $end_date]]; } if ($param['employee_id']) { $invalid_where[] = ['CustomerInvalidLog.employee_id', '=', $param['employee_id']]; } $i_customer_ids = Db::view('Employee') ->view('CustomerInvalidLog', 'id iid', 'CustomerInvalidLog.employee_id=Employee.id') ->view('Customer', 'id', 'Customer.id=CustomerInvalidLog.customer_id') ->where($invalid_where) ->order('Customer.sign_time desc') ->column('Customer.id'); $all_ids = array_values(array_unique(array_merge($customer_ids, $i_customer_ids))); $count = count($all_ids); $page_customer_id = array_slice($all_ids, ($param['page'] - 1) * $param['limit'], $param['limit']); // 见面状态 $state1 = CustomerVisitLog::changeState('已到店', 'chaos'); $state2 = CustomerVisitLog::changeState('已量房', 'chaos'); $state3 = CustomerVisitLog::changeState('已到场', 'chaos'); $state4 = CustomerVisitLog::changeState('已交定', 'chaos'); $state5 = CustomerVisitLog::changeState('已签单', 'chaos'); $visit_state = array_merge($state1, $state2, $state3, $state4, $state5); $list = Customer::with(['employee', 'designer', 'visitLog'=> function($query) use ($visit_state) { $query->where([['state', 'in', $visit_state]])->order('confirm_date asc')->field('id,customer_id,confirm_date,state'); }])->where('id', 'in', $page_customer_id)->select()->toArray(); $new_list = []; foreach ($list as $k => $v) { $one = [ 'id'=> $v['id'], 'name'=> $v['name'], 'community_name'=> $v['community_name'], 'area'=> $v['square'], 'employee_name'=> !empty($v['employee']) ? $v['employee']['name'] : '', 'designer_name'=> !empty($v['designer']) ? $v['designer']['name'] : '', 'sign_time'=> $v['sign_time'], 'state'=> $v['state'], 'first_visit_time'=> '', 'deposit_time'=> '' ]; if (!empty($v['visitLog'])) { $first_visit_time = ''; $deposit_time = ''; foreach ($v['visitLog'] as $kk =>$vv) { if (empty($first_visit_time)) { $first_visit_time = $vv['confirm_date']; } if (empty($deposit_time) && in_array($vv['state'], $state4)) { $deposit_time = $vv['confirm_date']; } } $one['first_visit_time'] = $first_visit_time; $one['deposit_time'] = $deposit_time; } $new_list[] = $one; } return json(['code'=> 0, 'data'=> $new_list, 'msg'=> 'success', 'count'=> $count]); } elseif (in_array($param['type'], ['visit', 'deposit', 'signed'])) { //见面 // 搜索状态 $state1 = CustomerVisitLog::changeState('已到店', 'chaos'); $state2 = CustomerVisitLog::changeState('已量房', 'chaos'); $state3 = CustomerVisitLog::changeState('已到场', 'chaos'); $state4 = CustomerVisitLog::changeState('已交定', 'chaos'); $state5 = CustomerVisitLog::changeState('已签单', 'chaos'); switch ($param['type']) { case 'visit': $search_state = array_merge($state1, $state2, $state3, $state4, $state5); break; case 'deposit': $search_state = $state4; break; case 'signed': $search_state = $state5; break; default: $search_state = ''; break; } $subOrg = orgSubIds($org_id); $s_where[] = ['Customer.community_name', '=', $community['name']]; if ($search_date) { $s_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]]; } if ($param['employee_id']) { $s_where[] = ['CustomerVisitLog.customer_employee_id', '=', $community['employee_id']]; } $s_where[] = ['CustomerVisitLog.org_id', 'in', $subOrg]; $s_where[] = ['CustomerVisitLog.state', 'in', $search_state]; $ids = Db::view('Customer') ->view('CustomerVisitLog', 'id lid', 'CustomerVisitLog.customer_id=Customer.id') ->where($s_where) ->order('Customer.sign_time desc') ->column('Customer.id'); $count = count($ids); $page_customer_id = array_slice($ids, ($param['page'] - 1) * $param['limit'], $param['limit']); $visit_state = array_merge($state1, $state2, $state3, $state4, $state5); $list = Customer::with(['employee', 'designer', 'visitLog'=> function($query) use ($visit_state) { $query->where([['state', 'in', $visit_state]])->order('confirm_date asc')->field('id,customer_id,confirm_date,state'); }])->where('id', 'in', $page_customer_id)->select()->toArray(); $new_list = []; foreach ($list as $k => $v) { $one = [ 'id'=> $v['id'], 'name'=> $v['name'], 'community_name'=> $v['community_name'], 'area'=> $v['square'], 'employee_name'=> !empty($v['employee']) ? $v['employee']['name'] : '', 'designer_name'=> !empty($v['designer']) ? $v['designer']['name'] : '', 'sign_time'=> $v['sign_time'], 'state'=> $v['state'], 'first_visit_time'=> '', 'deposit_time'=> '' ]; if (!empty($v['visitLog'])) { $first_visit_time = ''; $deposit_time = ''; foreach ($v['visitLog'] as $kk =>$vv) { if (empty($first_visit_time)) { $first_visit_time = $vv['confirm_date']; } if (empty($deposit_time) && in_array($vv['state'], $state4)) { $deposit_time = $vv['confirm_date']; } } $one['first_visit_time'] = $first_visit_time; $one['deposit_time'] = $deposit_time; } $new_list[] = $one; } return json(['code'=> 0, 'data'=> $new_list, 'msg'=> 'success', 'count'=> $count]); } } elseif ($param['module'] == 'employee') { // 以员工为主线 $subOrg = orgSubIds($org_id); $employee = Employee::where([['org_id', 'in', $subOrg], ['id', '=', $param['employee_id']]])->findOrEmpty(); if ($employee->isEmpty()) { return json(['code'=> 0, 'data'=> [], 'msg'=> 'success', 'count'=> 0]); } if ($param['community_id']) { $community = CommunityModel::where([['root_id', '=', $root_id], ['id', '=', $param['community_id']]])->findOrEmpty(); if ($community->isEmpty()) { return json(['code'=> 0, 'data'=> [], 'msg'=> 'success', 'count'=> 0]); } } if ($param['type'] == 'customer') { // 信息量 // 有效客户总量 $valid_where[] = ['employee_id', '=', $param['employee_id']]; if ($search_date) { $valid_where[] = ['sign_time', 'between', [$start_date, $end_date]]; } if ($param['community_id']) { $valid_where[] = ['community_name', '=', $community['name']]; } // 有效客户id $customer_ids = Customer::where($valid_where)->column('id'); // 无效的客户id $invalid_where[] = ['CustomerInvalidLog.employee_id', '=', $param['employee_id']]; if ($search_date) { $invalid_where[] = ['CustomerInvalidLog.cus_addtime', 'between', [$start_date, $end_date]]; } if ($param['community_id']) { $invalid_where[] = ['Customer.community_name', '=', $community['name']]; } $i_customer_ids = Db::view('Customer') ->view('CustomerInvalidLog', 'id iid', 'CustomerInvalidLog.customer_id=Customer.id') ->where($invalid_where) ->order('Customer.sign_time desc') ->column('Customer.id'); $all_ids = array_values(array_unique(array_merge($customer_ids, $i_customer_ids))); $count = count($all_ids); $page_customer_id = array_slice($all_ids, ($param['page'] - 1) * $param['limit'], $param['limit']); // 见面状态 $state1 = CustomerVisitLog::changeState('已到店', 'chaos'); $state2 = CustomerVisitLog::changeState('已量房', 'chaos'); $state3 = CustomerVisitLog::changeState('已到场', 'chaos'); $state4 = CustomerVisitLog::changeState('已交定', 'chaos'); $state5 = CustomerVisitLog::changeState('已签单', 'chaos'); $visit_state = array_merge($state1, $state2, $state3, $state4, $state5); $list = Customer::with(['employee', 'designer', 'visitLog'=> function($query) use ($visit_state) { $query->where([['state', 'in', $visit_state]])->order('confirm_date asc')->field('id,customer_id,confirm_date,state'); }])->where('id', 'in', $page_customer_id)->select()->toArray(); $new_list = []; foreach ($list as $k => $v) { $one = [ 'id'=> $v['id'], 'name'=> $v['name'], 'community_name'=> $v['community_name'], 'area'=> $v['square'], 'employee_name'=> !empty($v['employee']) ? $v['employee']['name'] : '', 'designer_name'=> !empty($v['designer']) ? $v['designer']['name'] : '', 'sign_time'=> $v['sign_time'], 'state'=> $v['state'], 'first_visit_time'=> '', 'deposit_time'=> '' ]; if (!empty($v['visitLog'])) { $first_visit_time = ''; $deposit_time = ''; foreach ($v['visitLog'] as $kk =>$vv) { if (empty($first_visit_time)) { $first_visit_time = $vv['confirm_date']; } if (empty($deposit_time) && in_array($vv['state'], $state4)) { $deposit_time = $vv['confirm_date']; } } $one['first_visit_time'] = $first_visit_time; $one['deposit_time'] = $deposit_time; } $new_list[] = $one; } return json(['code'=> 0, 'data'=> $new_list, 'msg'=> 'success', 'count'=> $count]); } elseif (in_array($param['type'], ['visit', 'deposit', 'signed'])) { //见面 // 搜索状态 $state1 = CustomerVisitLog::changeState('已到店', 'chaos'); $state2 = CustomerVisitLog::changeState('已量房', 'chaos'); $state3 = CustomerVisitLog::changeState('已到场', 'chaos'); $state4 = CustomerVisitLog::changeState('已交定', 'chaos'); $state5 = CustomerVisitLog::changeState('已签单', 'chaos'); switch ($param['type']) { case 'visit': $search_state = array_merge($state1, $state2, $state3, $state4, $state5); break; case 'deposit': $search_state = $state4; break; case 'signed': $search_state = $state5; break; default: $search_state = ''; break; } $s_where[] = ['CustomerVisitLog.customer_employee_id', '=', $param['employee_id']]; if ($search_date) { $s_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]]; } if ($param['community_id']) { $s_where[] = ['Customer.community_name', '=', $community['name']]; } $s_where[] = ['CustomerVisitLog.state', 'in', $search_state]; $ids = Db::view('Customer') ->view('CustomerVisitLog', 'id lid', 'CustomerVisitLog.customer_id=Customer.id') ->where($s_where) ->group('Customer.id') ->order('Customer.sign_time desc') ->column('Customer.id'); $count = count($ids); $page_customer_id = array_slice($ids, ($param['page'] - 1) * $param['limit'], $param['limit']); $visit_state = array_merge($state1, $state2, $state3, $state4, $state5); $list = Customer::with(['employee', 'designer', 'visitLog'=> function($query) use ($visit_state) { $query->where([['state', 'in', $visit_state]])->order('confirm_date asc')->field('id,customer_id,confirm_date,state'); }])->where('id', 'in', $page_customer_id)->select()->toArray(); $new_list = []; foreach ($list as $k => $v) { $one = [ 'id'=> $v['id'], 'name'=> $v['name'], 'community_name'=> $v['community_name'], 'area'=> $v['square'], 'employee_name'=> !empty($v['employee']) ? $v['employee']['name'] : '', 'designer_name'=> !empty($v['designer']) ? $v['designer']['name'] : '', 'sign_time'=> $v['sign_time'], 'state'=> $v['state'], 'first_visit_time'=> '', 'deposit_time'=> '' ]; if (!empty($v['visitLog'])) { $first_visit_time = ''; $deposit_time = ''; foreach ($v['visitLog'] as $kk =>$vv) { if (empty($first_visit_time)) { $first_visit_time = $vv['confirm_date']; } if (empty($deposit_time) && in_array($vv['state'], $state4)) { $deposit_time = $vv['confirm_date']; } } $one['first_visit_time'] = $first_visit_time; $one['deposit_time'] = $deposit_time; } $new_list[] = $one; } return json(['code'=> 0, 'data'=> $new_list, 'msg'=> 'success', 'count'=> $count]); } } else { return json(['code'=> 0, 'data'=> [], 'count'=> 0, 'msg'=> 'success']); } } //获取设计师列表 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 screen() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $team_orgs = [$request->empcrm->org_id]; 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'); } $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)) { // $list = hanziInitsort($list, 'name'); $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']); //部门所有员工 $employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0],['community_disable','=',0]])->field('id,name')->select()->toArray(); View::assign('employee_list', $employee); View::assign('empid', $empid); $customer_type = [['id'=>1,'name'=>'资源库'],['id'=>2,'name'=>'自建'],['id'=>3,'name'=>'活动'],['id'=>4,'name'=>'装修推荐官']]; View::assign('customer_type',$customer_type); } //已交定列表 public function jiaoding_list() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $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],['community_disable','=',0]])->column('id'); } if (!$request->isAjax()) { $this->screen(); return View::fetch(); } $param = $request->only(['page'=>1, 'limit'=>10, 'employee_id', 'level','source_id','designer_id','keyname','date','times'=> 0,'customer_type']); if(!empty($param['employee_id']) && $is_manager ==1 && in_array($param['employee_id'],$org_employee)) $org_employee = [$param['employee_id']]; $condition[] = ['employee_id', 'in', $org_employee]; $condition[] = ['state', 'not in',Customer::changeState('无效', 'chaos')]; if(!empty($param['level'])) $condition[] = ['level','=',$param['level']]; if(!empty($param['source_id'])) $condition[] = ['source_id','=',$param['source_id']]; if(!empty($param['designer_id'])) $condition[] = ['designer_id','=',$param['designer_id']]; if(!empty($param['keyname'])) $condition[] = ['name|community_name','like','%'.$param['keyname'].'%']; $order = isset($param['order']) ? $param['order'] : 'id desc'; if($param['times'] == 1) $order = 'sign_time asc'; if($param['times'] == 2) $order = 'sign_time desc'; $page = intval($param['page']); $limit = intval($param['limit']); $vslogid = CustomerVisitLog::where([['customer_employee_id','in',$org_employee],['state','in',CustomerVisitLog::changeState('已交定','chaos')]])->column('customer_id'); if(!empty($param['date'])){ $time = explode(' - ',$param['date']); $vslogid = CustomerVisitLog::where([['customer_employee_id','in',$org_employee],['state','in',CustomerVisitLog::changeState('已交定','chaos')],['confirm_date','between',[$time[0].' 00:00:00',$time[1].' 23:59:59']]])->column('customer_id'); } $condition[] = ['id', 'in', $vslogid]; //客户种类 资源库,活动,经纪人,自建 if (!empty($param['customer_type'])) { $gather = [ 1=>[['crm_res_id','not null',null]],//'资源库' 2=>[['crm_res_id','null',null],['remark','not like','%活动报名建档%'],['agents_id','null',null]],//'自建' 3=>[['remark','like','%活动报名建档%']],//'活动' 4=>[['agents_id','>',0]]//'经纪人' ]; $arr = explode(',',$param['customer_type']); $whereOr = []; foreach ($arr as $val) { if (isset($gather[$val])) { //$whereOr[] = array_merge($condition,$gather[$val]); $whereOr[] = $gather[$val]; } } if (!empty($whereOr)) { $new_cids = Customer::where($condition)->where(function($query) use ($whereOr){ $query->whereOr($whereOr); })->column('id'); unset($condition); $condition[] = ['id','in',$new_cids]; }else{ //无效条件 return json(['code' => 0, 'data' => [], 'count' => 0]); } } $data = $this->selCustomer($condition,$page,$limit,$order,$root_id); $no_visit_state = ['未到访', '待确认']; $visit_state = ['已到店', '已量房', '已到场', '已签单', '已转单', '已卖卡']; foreach($data as $key=>$val){ $data[$key]['customer_type'] = $val['crm_res_id'] ? '资源库' : ($val['remark'] == '活动报名建档' ? '活动' : ($val['agents_id'] ? '装修推荐官' : '自建')); $data[$key]['jiaoding_day'] = !empty($val['fisttime']) && strtotime($val['jiaoding_time']) > strtotime($val['fisttime']) ? ceil((strtotime($val['jiaoding_time']) - strtotime($val['fisttime']))/86400) : ceil((strtotime($val['jiaoding_time']) - strtotime($val['addtime']))/86400); $data[$key]['xinjushang'] = $request->empcrm['xinjushang']; // 有'已到店', '已量房', '已到场'状态的话不显示未到访 $state = explode(',', $val['state']); $jiaoji = array_intersect($visit_state, $state); $no_jiaoji = array_intersect($no_visit_state, $state); if (count($no_jiaoji) == 2) { // 有未到访、待确认两个状态 $state = ['未到访']; } if (count($state) > 1 && count($jiaoji) > 0) { $state = array_diff($state, $no_visit_state); } $data[$key]['state'] = implode(',', $state); } $count = Customer::where($condition)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } //业绩页面(已签单) public function achment_list() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $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],['community_disable','=',0]])->column('id'); } if (!$request->isAjax()) { $this->screen(); return View::fetch(); } $param = $request->only(['page'=>1, 'limit'=>10, 'employee_id', 'level','source_id','designer_id','keyname','date','customer_type']); if(!empty($param['employee_id']) && $is_manager ==1 && in_array($param['employee_id'],$org_employee)) $org_employee = [$param['employee_id']]; $condition[] = ['employee_id', 'in', $org_employee]; $condition[] = ['signed_money', '<>', 0]; $condition[] = ['state', 'not in',Customer::changeState('无效', 'chaos')]; if(!empty($param['level'])) $condition[] = ['level','=',$param['level']]; if(!empty($param['source_id'])) $condition[] = ['source_id','=',$param['source_id']]; if(!empty($param['designer_id'])) $condition[] = ['designer_id','=',$param['designer_id']]; if(!empty($param['keyname'])) $condition[] = ['name|community_name','like','%'.$param['keyname'].'%']; $order = isset($param['order']) ? $param['order'] : 'id desc'; $page = intval($param['page']); $limit = intval($param['limit']); $vslogid = CustomerVisitLog::where([['customer_employee_id','in',$org_employee],['state','in',CustomerVisitLog::changeState('已签单','chaos')]])->column('customer_id'); if(!empty($param['date'])){ $time = explode(' - ',$param['date']); $vslogid = CustomerVisitLog::where([['customer_employee_id','in',$org_employee],['state','in',CustomerVisitLog::changeState('已签单','chaos')],['confirm_date','between',[$time[0].' 00:00:00',$time[1].' 23:59:59']]])->column('customer_id'); } $condition[] = ['id', 'in', $vslogid]; //客户种类 资源库,活动,经纪人,自建 if (!empty($param['customer_type'])) { $gather = [ 1=>[['crm_res_id','not null',null]],//'资源库' 2=>[['crm_res_id','null',null],['remark','not like','%活动报名建档%'],['agents_id','null',null]],//'自建' 3=>[['remark','like','%活动报名建档%']],//'活动' 4=>[['agents_id','>',0]]//'经纪人' ]; $arr = explode(',',$param['customer_type']); $whereOr = []; foreach ($arr as $val) { if (isset($gather[$val])) { //$whereOr[] = array_merge($condition,$gather[$val]); $whereOr[] = $gather[$val]; } } if (!empty($whereOr)) { $new_cids = Customer::where($condition)->where(function($query) use ($whereOr){ $query->whereOr($whereOr); })->column('id'); unset($condition); $condition[] = ['id','in',$new_cids]; }else{ //无效条件 return json(['code' => 0, 'data' => [], 'count' => 0]); } } $data = $this->selCustomer($condition,$page,$limit,$order,$root_id); foreach($data as $key=>$val){ $sign=[]; if(!empty($val['sign'])) $sign = $val['sign']; $data[$key]['dingqian_day'] = !empty($sign) && !empty($val['jiaoding_time']) ? round((strtotime($sign['confirm_date']) - strtotime($val['jiaoding_time']))/86400) : 0; $data[$key]['daoqian_day'] = !empty($sign) && !empty($val['fisttime'])? round((strtotime($sign['confirm_date']) - strtotime($val['fisttime']))/86400) : 0; $data[$key]['all_money'] = !empty($sign) ? $sign['money'] : 0; $data[$key]['pf_money'] = !empty($val['square']) && !empty($sign) ? round(($sign['money'])/$val['square'],2) : 0; $data[$key]['customer_type'] = $val['crm_res_id'] ? '资源库' : ($val['remark'] == '活动报名建档' ? '活动' : ($val['agents_id'] ? '装修推荐官' : '自建')); } $count = Customer::where($condition)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } //搜索 public function search_list() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $org_employee = [$empid]; if (!$request->isAjax()) { //$this->screen(); return View::fetch(); } $param = $request->only(['page'=>1, 'limit'=>10, 'keyname']); if($is_manager == 1){ $team_orgs = orgSubIds($request->empcrm->org_id); $empids = Employee::where([['root_id','=',$root_id],['org_id','in',$team_orgs],['community_disable','=',0]])->column('id'); $condition[] = ['employee_id', 'in', $empids]; }else{ $condition[] = ['employee_id', '=', $empid]; $condition[] = ['community_disable', '=', 0]; } $order = isset($param['order']) ? $param['order'] : 'id desc'; $list = Customer::where($condition)->field('id,name,community_name,phone,employee_id,designer_id')->select()->toArray(); $customersIdList = []; if(!empty($param['keyname'])){ foreach($list as $key=>$val){ $str = $val['name'] . $val['community_name'] . $val['phone']; if (strpos($str, trim($param['keyname'])) !== false) $customersIdList[] = $val['id']; } } $new = array_slice($customersIdList, ($param['page'] - 1) * $param['limit'], $param['limit']); $cond[] = ['id','in',$new]; $data = $this->search_selone($cond,$order); $count = count($customersIdList); return json(['code' => 0, 'data' => $data, 'count' => $count]); } //已签单或已交定客户详情 public function deposit_sign_detail() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $param = $this->request->only(['cid']); $ms = $this->seLemp($param['cid'],$empid); $data = $ms->toArray(); $visitlog = $data['visitLog']; if(!empty($visitlog)){ foreach($visitlog as $key=>$val){ $visitlog[$key]['empname'] = Employee::where('id',$val['employee_id'])->value('name'); $parr[] = $val['addtime']; } array_multisort($parr, SORT_ASC, $visitlog); foreach($visitlog as $key=>$val){ if((count($visitlog)-1) > $key){ $visitlog[$key]['xt'] = round((strtotime($visitlog[$key+1]['addtime']) - strtotime($val['addtime']))/86400); } if((count($visitlog)-1) == $key){ $visitlog[$key]['xt'] = 0; } } } $data['visitLog'] = $visitlog; View::assign('data', $data); return View::fetch(); } //搜索的单独查询 public function search_selone($condition,$order) { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $data = Customer::with(['employee', 'org', 'designer','source']) ->withCount(['visitLog' => function ($query) { $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]); }]) ->where($condition) ->order($order)->select(); $data = $data->visible(['id', 'employee_id', 'name','designer_id', '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','house_delivery_time','first_visit_date','ext','transfer_info'])->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; } } $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'])?($item['house_delivery_time'] <= date('Y/m/d',time())?'现房':$item['house_delivery_time']):''; $vlotime = CustomerVisitLog::where('customer_id',$item['id'])->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'); $jiaoding_time = CustomerVisitLog::where([['customer_id','=',$item['id']],['customer_employee_id','=',$item['employee_id']],[CustomerVisitLog::changeState(['state', '=', '已交定'])]])->order('addtime asc')->value('addtime'); $signorder_time = CustomerVisitLog::where([['customer_id','=',$item['id']],['customer_employee_id','=',$item['employee_id']],[CustomerVisitLog::changeState(['state', '=', '已签单'])]])->order('addtime asc')->value('addtime'); $item['jiaoding_time'] = $jiaoding_time ? $jiaoding_time :''; $item['signorder_time'] = $signorder_time ? $signorder_time :''; $item['fisttime'] = $fisttime ? $fisttime :''; $item['time_status'] = $vlotime ? $this->uc_time_ago(date('Y-m-d',strtotime($vlotime))) :''; $item['add_wechat'] = '否'; $item['sign_time'] = null; if(!empty($item['ext'])){ $extdata=json_decode($item['ext'],true); foreach($extdata as $key=>$val){ if(isset($val['keyname']) && $val['keyname']=='wechat' && !empty($val['value'])){ $add_wechat = CustomerPortraitFieldSelect::where('id',$val['value'])->value('name'); if($add_wechat == '有'){ $add_wechat = '是'; } $item['add_wechat'] = $add_wechat; } if(isset($val['keyname']) && $val['keyname']=='sign_time' && !empty($val['value'])){ $item['sign_time'] = $val['value']; } } } } return $data; } //公用查询列表 public function selCustomer($condition,$page,$limit,$order,$root_id) { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $path = Org::where([['pid','=',$request->empcrm->org_id]])->count(); if($page && $limit){ $data = Customer::with(['employee', 'org', 'designer','source','visitLog'=>function($query){ $query->field('id,customer_id,state,addtime,confirm_date,money')->order('addtime asc'); }])->withCount(['visitLog' => function ($query) { $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]); }])->where($condition)->page($page, $limit)->order($order)->select(); }else{ $data = Customer::with(['employee', 'org', 'designer','source','visitLog'=>function($query){ $query->field('id,customer_id,state,addtime,confirm_date,money')->order('addtime asc'); }])->withCount(['visitLog' => function ($query) { $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]); }])->where($condition)->order($order)->select(); } $data = $data->visible(['designer_id','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','fresh','house_type','remark','employee_time', 'add_wechat_time','assigned_personnel','sign_time','reputation_gather','into_owner_group'])->toArray(); $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find(); if (!empty($setting)){ $setting_content = json_decode($setting['content'], true); } else { $setting_content = []; } $empids = !empty($data) ? array_column($data,'employee_id') : []; $emporg = Employee::with(['org'=>function($query){ $query->field('id,name'); }])->where([['root_id','=',$root_id],['id','in',$empids]])->field('id,org_id')->select()->toArray(); $state1 = CustomerVisitLog::changeState('已到店','chaos'); $state2 = CustomerVisitLog::changeState('已交定','chaos'); $state3 = CustomerVisitLog::changeState('已签单','chaos'); $state4 = CustomerVisitLog::changeState('已量房','chaos'); $state5 = CustomerVisitLog::changeState('无效','chaos'); foreach ($data as &$item) { $item['emporg'] = $item['source_name'] = $item['designer_name'] = $item['employee_name'] = ''; if (!empty($item['source'])){ $item['source_name'] = $item['source']['source']; } if (!empty($item['designer'])){ $item['designer_name'] = $item['designer']['name']; } if (!empty($item['employee'])){ $item['employee_name'] = $item['employee']['name']; foreach($emporg as $p){ if($p['id'] == $item['employee_id']) $item['emporg'] = $p['org']['name']; } } $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; } } $item['last_invalid_time'] = $item['first_liangfang'] = $item['jiaoding_time'] = $item['sign'] = $item['fisttime'] = ''; if(!empty($item['visitLog'])){ //取最新的一条为客户的跟进时间 $item['last_contact_date'] = $item['visitLog'][count($item['visitLog'])-1]['addtime']; $state = []; foreach($item['visitLog'] as $k=>$v){ $state[$v['state']][] = $v; } foreach($state as $k=>$v){ if(in_array($k,$state1)) $item['fisttime'] = date('Y-m-d',strtotime($v[0]['confirm_date'])); if(in_array($k,$state4)) $item['first_liangfang'] = date('Y-m-d',strtotime($v[0]['addtime'])); if(in_array($k,$state2)){ $item['jiaoding_time'] = $v[0]['addtime']; $item['jiaoding_money'] = $v[0]['money']; } if(in_array($k,$state3)){ $item['qiandan_time'] = $v[0]['addtime']; $item['qiandan_money'] = $v[0]['money']; $item['sign'] = $v[0]; } if(in_array($k,$state5)) $item['last_invalid_time'] = $v[0]['addtime']; } } $item['square'] = floatval($item['square']); //$item['visitLog'] = CustomerVisitLog::where([['customer_employee_id','=',$item['employee_id']],['customer_id','=',$item['id']],['state','in',array_merge(CustomerVisitLog::changeState('待确认','chaos'),CustomerVisitLog::changeState('已到店','chaos'),CustomerVisitLog::changeState('已到场','chaos'),CustomerVisitLog::changeState('已交定','chaos'),CustomerVisitLog::changeState('已签单','chaos'))]])->group('state,customer_id')->field('state,id,customer_id')->order('addtime asc')->select()->toArray(); $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) : ''; $houses = $item['house_type']; if(!empty($item['house_type']) && $item['house_type'] == '期房') $houses = !empty($item['house_delivery_time'])?(str_replace('/','-',$item['house_delivery_time']) <= date('Y-m-d',time())?'现房':$item['house_delivery_time']):'期房'; $item['jiaofang'] = $houses; $item['time_status'] = $item['last_contact_date'] ? $this->uc_time_ago($item['last_contact_date']) :''; $item['allstate'] = null; $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id',$item['package_id'])->value('name') :''; $item['is_followup'] = 0; $item['lognum'] = count($item['visitLog']); if($is_manager==1 && empty($path)){ $item['is_followup'] = 1; } if($empid == $item['employee_id']){ $item['is_followup'] = 1; } $now_status = $item['state']; if($now_status == '已签单' && $request->empcrm['xinjushang'] == 0) $now_status = '已转单'; if($now_status == '已交定' && $request->empcrm['xinjushang'] == 0) $now_status = '已签单'; $item['now_status'] = $now_status; $item['state'] = ''; $allstate=''; if(!empty($item['visitLog'])){ $stain=[$now_status]; foreach($item['visitLog'] as $k=>$v){ if(!in_array($v['state'],['已交定','已签单','已到店','已量房','已到场','未到访','回访'])) continue; if($v['state']=='回访') $v['state'] = '待确认'; if($v['state']=='已签单' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已转单'; if($v['state']=='已交定' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已签单'; if(!in_array($v['state'],$stain)){ $stain[] = $v['state']; //$allstate.= $v['state'].','; } } if(count($stain) > 1 && in_array('待确认',$stain)) $stain = array_diff($stain,['待确认']); //$item['state'] = trim($allstate,','); $item['state'] = implode(',',$stain); if($now_status == '待确认') $item['state'] = '待确认'; } $item['unit_number'] = ''; if(!empty($item['ext'])){ foreach(json_decode($item['ext'],true) as $k=>$v){ if(!empty($v['keyname']) && $v['keyname'] == 'unit_number' && !empty($v['value'])) $item['unit_number'] = $v['value']; } } // 未跟进时间 $latest_visit_log = $item['last_contact_date']; if (empty($latest_visit_log)) { $no_visit_long = time() - strtotime($item['addtime']); } else { $no_visit_long = time() - strtotime($latest_visit_log); } $no_visit_day = ceil($no_visit_long / 24 / 3600); $item['no_visit_day'] = $no_visit_day; // 预计见面时间 $subscribe = CustomersSubscribe::where([['customer_id', '=', $item['id']], ['employee_id', '=', $item['employee_id']], ['state', '=', 0]])->order('subscribe_date desc')->find(); if (!empty($subscribe)) { $item['subscribe_date'] = $subscribe['subscribe_date']; } else { $item['subscribe_date'] = ''; } $assignIdList = !empty($item['assigned_personnel']) ? explode(',',$item['assigned_personnel']) : []; if(!empty($item['designer_id']) && !empty($assignIdList)) $assignIdList = array_diff($assignIdList,[$item['designer_id']]); $assign_list = Employee::where('id', 'in', $assignIdList)->column('name'); $item['assign_list'] = !empty($assign_list) ? implode(',',$assign_list) : ''; } return $data; } public function uc_time_ago($ptime) { $ptime = strtotime($ptime); $etime = strtotime(date('Y-m-d')) - $ptime; switch ($etime) { case $etime > 60 * 60 && $etime <= 24 * 60 * 60: $msg = date('Ymd', $ptime) == date('Ymd', time()) ? '今天 ' : '昨天 '; break; case $etime > 24 * 60 * 60 && $etime <= 2 * 24 * 60 * 60: $msg = date('Ymd', $ptime) + 1 == date('Ymd', time()) ? '昨天 ' : '前天 '; break; case $etime > 2 * 24 * 60 * 60 : $msg = floor($etime / 86400).'天+'; break; default: $msg = date('Y-m-d H:i', $ptime); } return $msg; } /** * 客户迁移 */ public function customerTransfer() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $orgids = orgSubIds($root_id); $param = $this->request->only(['customer_ids' => '', 'employeeid']); $arr = explode(',', $param['customer_ids']); //查询手机号 $phone = Customer::where([['employee_id','=', $param['employeeid']],['org_id','in',$orgids]])->column('phone,phone1,phone2'); $phones = []; foreach ($phone as $v) { $phones = array_merge($phones, array_values($v)); } $phones = array_filter($phones); $customers = Customer::where([['id', 'in', $arr],['org_id','in',$orgids]])->column('id,name,phone,phone1,phone2'); $repeat = $no_repeat = []; foreach ($customers as $v2) { $l_phone = array_filter([$v2['phone'], $v2['phone1'], $v2['phone2']]); empty(array_intersect($l_phone, $phones)) ? $no_repeat[] = $v2['id'] : $repeat[] = $v2['name']; } if ($no_repeat) { $org_id = Employee::where('id', $param['employeeid'])->value('org_id'); Customer::where([['id', 'in', $no_repeat]])->update(['employee_id' => $param['employeeid'], 'org_id' => $org_id]); Customer::where([['id', 'in', $no_repeat]])->update(['transfer_info' => json_encode(['transfer_empid'=>$empid,'transfer_time'=>date('Y-m-d H:i:s'),'transfer_empname'=>$request->empcrm->name])]); //修改转移的客户的跟进记录的customer_employee_id为新员工的id CustomerVisitLog::where([['customer_id','in',$no_repeat],['org_id','in',$orgids]])->update(['customer_employee_id'=>$param['employeeid'],'customer_org_id'=>$org_id]); } if ($repeat) return json(['code' => 1, 'msg' => '分派失败:员工名下已存在客户[' . implode(',', $repeat) . ']的联系方式']); return json(['code' => 0, 'msg' => '分派完成']); } //分派 public function assignment_list() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $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],['community_disable','=',0]])->column('id'); } if (!$request->isAjax()) { $this->screen(); $newteam_orgs = orgSubIds($request->empcrm->org_id); $assignemp_list = Employee::where([['org_id', 'in', $newteam_orgs], ['state', '=', '在职'], ['uid', '>', 0],['community_disable','=',0]])->field('id,name')->select()->toArray(); View::assign('assignemp_list',$assignemp_list); View::assign('is_manager',$is_manager); return View::fetch(); } $param = $request->only(['page'=>1, 'limit'=>10, 'employee_id', 'level','source_id','designer_id','keyname']); if(!empty($param['employee_id']) && $is_manager ==1) $org_employee = [$param['employee_id']]; $condition[] = ['employee_id', 'in', $org_employee]; $condition[] = ['transfer_info', '<>','']; $order = isset($param['order']) ? $param['order'] : 'updatetime desc'; $page = intval($param['page']); $limit = intval($param['limit']); if(!empty($param['level'])) $condition[] = ['level','=',$param['level']]; if(!empty($param['source_id'])) $condition[] = ['source_id','=',$param['source_id']]; if(!empty($param['designer_id'])) $condition[] = ['designer_id','=',$param['designer_id']]; if(!empty($param['keyname'])) $condition[] = ['name|community_name','like','%'.$param['keyname'].'%']; $data = $this->selCustomer($condition,$page,$limit,$order,$root_id); foreach($data as $key=>$val){ $transfer = json_decode($val['transfer_info'],true); $data[$key]['transfer_time'] = $transfer['transfer_time']; $data[$key]['transfer_name'] = $transfer['transfer_empname']; } $count = Customer::where($condition)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 公海获取客户 * customer_id来源:业务员获取(int)、报备录入时查询公海存在自动获取(逗号分隔字符串) */ public function fetchpoolitem() { $request = request(); $root_id = $request->empcrm->root_id; $employee_id = $request->empcrm->id; $orgids = orgSubIds($root_id); $param = $this->request->only(['cid','designer_id']); if (!$request->isAjax()) { $this->screen(); $data = $this->cusdet_noemp($param['cid']); View::assign('data',$data); return View::fetch(); } // 设计师能否报备、获取客户 $de_where[] = ['root_id', '=', $root_id]; $de_where[] = ['name', '=', 'designer_get_customer']; $info_de_where = Setting::where($de_where)->findOrEmpty(); if (!$info_de_where->isEmpty() && request()->empcrm->org_type == 2) { if ((int)$info_de_where['content'] != 1) { return json(['code' => 1, 'msg' => '您的身份为设计师,无法获取客户']); } } //$customer_id = explode(',', $param['customer_id']); $customer_id = $param['cid']; $otheremp = Customer::where([['org_id','in',$orgids],['id', 'in', $customer_id], ['employee_id', '=', NULL]])->field('employee_id,phone,phone1,phone2')->select(); if (empty($otheremp)) { return json(['code' => 1, 'msg' => '客户已被人抢先获取。记得下次抓住机会。']); } //判断名下是否存在该客户手机号,存在则不可获取 foreach ($otheremp as $item) { $phone_arr = []; !empty($item['phone']) ? $phone_arr[] = cypherphone($item['phone']) : ''; !empty($item['phone1']) ? $phone_arr[] = cypherphone($item['phone1']) : ''; !empty($item['phone2']) ? $phone_arr[] = cypherphone($item['phone2']) : ''; $phone_arr = array_filter($phone_arr); $haveCrm = Customer::where([['employee_id', '=', $employee_id], ['phone|phone1|phone2', 'in', $phone_arr]])->count(); if ($haveCrm) { return json(['code' => 1, 'msg' => '您名下已存在该客户信息。']); } } if (Customer::where([['id', 'in', $customer_id]])->update(['employee_id' => $employee_id, 'org_id' => $this->request->empcrm->org_id, 'state' => 0, 'remark' => '公海获取', 'fresh' => 1, 'designer_id' => null,'employee_time'=>date('Y-m-d H:i:s')])) { $saveAll = []; $saveAll[] = [ 'customer_id' => $customer_id, 'type' => 1, 'remark' => '公海获取', 'employee_id' => $employee_id, 'user_id' => $this->request->empcrm->uid, 'state' => 1 ]; if (CustomerVisitLog::insertAll($saveAll)) { return json(['code' => 0, 'msg' => '获取成功']); } else { return json(['code' => 1, 'msg' => '获取失败']); } } else { return json(['code' => 1, 'msg' => '获取失败']); } } //更新已签单客户页面 public function signcus_detail() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $org_employee = [$empid]; $param = $request->only(['id']); $this->screen(); //客户等级 隶属计划 $fields=CustomerPortraitField::with(['select'])->where([['root_id','=',$root_id],['keyname','in',['level','subjection_plan','housetype_arrow']]])->select()->toArray(); View::assign('fields', $fields); //意向风格 $decostyle_list = Decostyle::field('id,name,root_id as pid')->where(['root_id' =>$root_id])->where([['type', '=', 0]])->order('id desc')->select()->toArray(); View::assign('decostyle_list', $decostyle_list); //意向产品 $product = CustomerPackage::field('id, name, total_price')->where(['root_id' =>$root_id])->select(); View::assign('product', $product); $data = $this->seLemp($param['id'],$empid); View::assign('data',$data); return View::fetch(); } //已签单客户修改客户信息 public function sign_savecust() { $request = request(); $param = $request->only(['id','designer_id','name'=>'','community_name'=>'','square'=>0,'housetype_arrow'=>'','package_id'=>'','deco_style'=>'']); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $org_employee = [$empid]; $orgids = orgSubIds($root_id); $data['name'] = $param['name']; $data['community_name'] = $param['community_name']; $data['square'] = $param['square']; $data['package_id'] = $param['package_id']; $data['deco_style'] = $param['deco_style']; //$data['designer_id'] = $param['designer_id']; //处理客户的扩展字段 $et=[]; $kz = CustomerPortraitField::where([['root_id','=',$root_id],['keyname','in',['housetype_arrow']]])->select(); foreach($kz as $key=>$val){ $et[]=['id'=>$val['id'],'keyname'=>$val['keyname'],'value'=>$param[$val['keyname']]]; } $data['ext'] = !empty($et)?json_encode($et):NULL; $ms = Customer::where([['id','=',$param['id']],['org_id','in',$orgids]])->update($data); return json(['code' => 0, 'data' => $ms, 'msg' => '修改成功']); } //公海点击展示客户详情 public function pollcus_detail() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $param = $this->request->only(['cid']); $data = $this->cusdet_noemp($param['cid']); View::assign('data',$data); return View::fetch(); } //公共查询客户详情(没有所属员工id时) public function cusdet_noemp($id) { $request = request(); $root_id = $request->empcrm->root_id; $orgids = orgSubIds($root_id); $data = Customer::with(['visitLog'])->where([['id','=',$id],['org_id','in',$orgids]])->find(); $data = $data->toArray(); // echo json_encode($data['visitLog']); // exit; $data['package_name'] = $data['package_id'] ? CustomerPackage::where('id',$data['package_id'])->value('name') :''; $data['deco_style_name'] = $data['deco_style'] ? Decostyle::where('id',$data['deco_style'])->value('name') : ''; $data['company_name'] = Company::where('root_id',$root_id)->value('company_name'); $data['add_wechat'] = '否'; $data['housetype'] = $data['unit_number'] = $data['subjection_plan'] = null; $data['jiaofang'] = !empty($data['house_delivery_time'])?($data['house_delivery_time'] <= date('Y/m/d',time())?'现房':'期房'):''; $data['company_name'] = Company::where('root_id',$root_id)->value('company_name'); //$vslog = CustomerVisitLog::where('customer_id',$id)->order('addtime desc')->find(); $data['lrname'] = NULL; if(!empty($data['visitLog']) && !empty($data['visitLog'][0]['customer_employee_id'])){ $data['lrname'] = Employee::where('id',$data['visitLog'][0]['customer_employee_id'])->value('name'); } if(!empty($data['ext'])){ $field = $this->get_portrait_field($data); foreach($field as $key=>$val){ if($val['keyname'] == 'wechat' && !empty($val['value'])){ $data['add_wechat'] = CustomerPortraitFieldSelect::where('id',$val['value'])->value('name'); } if($val['keyname']=='housetype_arrow' && !empty($val['value'])){ $data['housetype'] = CustomerPortraitFieldSelect::where('id',$val['value'])->value('name'); } if($val['keyname']=='subjection_plan' && !empty($val['value'])){ $data['subjection_plan'] = CustomerPortraitFieldSelect::where('id',$val['value'])->value('name'); } if($val['keyname']=='unit_number' && !empty($val['value'])){ $data['unit_number'] = $val['value']; } if($val['keyname']=='sign_name' && !empty($val['value'])){ $data['sign_time'] = $val['value']; } } } if(!empty($data['visitLog'])){ foreach($data['visitLog'] as $key=>$val){ if($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已转单'; if($val['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已签单'; if($val['state'] == '已转单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已签单','已转单',$val['remark']); if($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已交定','已签单',$val['remark']); $data['visitLog'][$key]['state'] = $val['state']; $data['visitLog'][$key]['empname'] = Employee::where('id',$val['employee_id'])->value('name'); if(!empty($val['save_portrait_field'])){ $save_portrait_field = json_decode($val['save_portrait_field'],true); foreach($save_portrait_field as $k=>$v){ if($v['type'] == 6){ $save_portrait_field[$k]['valname'] = explode(',',$v['value']); } } }else{ $save_portrait_field = ''; } $data['visitLog'][$key]['save_portrait_field'] = $save_portrait_field; $parr[] = $val['addtime']; if($val['next_contact_date'] > date('Y-m-d H:i:s')){ $data['visitLog'][$key]['isyes'] = 1; }else{ $data['visitLog'][$key]['isyes'] = 0; } $data['visitLog'][$key]['wisdom_url'] = ''; $data['visitLog'][$key]['is_wisdom'] = 0; if ($val['remark'] && strpos($val['remark'],'讲解智慧屏##')!==false) { //remark = 接待了客户:张三,讲解时长21分钟@讲解智慧屏## url; $data['visitLog'][$key]['is_wisdom'] = 1; $arr = explode('##',$val['remark']); if(count($arr)==2){ $ali_oss_bindurl = config('app.ali_oss_bindurl'); $data['visitLog'][$key]['wisdom_url'] = 'https://' . $ali_oss_bindurl . '/' . $arr[1]; $title = explode('@',$arr[0]); $data['visitLog'][$key]['remark'] = $title[0]; } } // 检测是否有录音 if($val['data_type'] == 'out_call'){ $data['visitLog'][$key]['is_wisdom'] = 2; $data['visitLog'][$key]['wisdom_url'] = OutCallLog::where(['id'=>$val['data_id']])->value('url'); } } array_multisort($parr, SORT_DESC, $data['visitLog']); } return $data; } //新增客户后跟进页面 public function followup() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $param = $this->request->only(['cid']); $data = $this->seLemp($param['cid'],$empid); View::assign('data',$data); return View::fetch(); } //单独确认客户有效 public function validation() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $orgids = orgSubIds($root_id); $param = $this->request->only(['customer_id']); $ms = Customer::where([['org_id','in',$orgids],['id','=',$param['customer_id']],['state','in',Customer::changeState('待确认', 'chaos')]])->update(['state'=>1,'valid_time'=>date('Y-m-d H:i:s')]); if($ms){ return json(['code' => 0, 'msg' => '确认完成']); }else{ return json(['code' => 1, 'msg' => '确认失败']); } } //待回访 public function repay_list() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $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],['community_disable','=',0]])->column('id'); } if (!$request->isAjax()) { $this->screen(); return View::fetch(); } $param = $request->only(['page'=>1, 'limit'=>10, 'jiange_day','employee_name', 'level','source_name','designer_name','name','date','last_contact_date','community_name','phone']); // 客户表revisit_time字段存在无法识别日期格式,查询需指定开始日期 $condition[] = empty($param['date']) ? ['revisit_time', '=', date('Y-m-d') . ' 00:00:00'] : ['revisit_time', '=', $param['date'] . ' 00:00:00']; if(!empty($param['employee_id']) && $is_manager ==1 && in_array($param['employee_id'],$org_employee)) $org_employee = [$param['employee_id']]; $condition[] = ['employee_id', 'in', $org_employee]; //$condition[] = ['state', 'not in',Customer::changeState('已签单', 'chaos')]; //客户姓名 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'])]; $order = isset($param['order']) ? $param['order'] : 'id desc'; $page = intval($param['page']); $limit = intval($param['limit']); if (!empty($param['phone'])) { $list = Customer::where($condition)->column('id','phone'); $keyCustomersId = []; foreach ($list as $k => $v) { if(cypherphone($param['phone']) == $k) $keyCustomersId[] = $v; // if (strpos($k, trim($new_params['phone'])) !== false) $keyCustomersId[] = $v; } unset($condition); $condition[] = ['id', 'in', $keyCustomersId]; } //客户的跟进时间 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]; $condition[] = ['last_contact_date','<=',$end]; } //未跟进天数 if (!empty($param['jiange_day'])) { $no_visit_day = explode(',',$param['jiange_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]; } $data = $this->selCustomer($condition,$page,$limit,$order,$root_id); foreach($data as $key=>$val){ $data[$key]['jiange_day'] = ceil((strtotime(date('Y-m-d')) - strtotime($val['last_contact_date']))/86400); } $count = Customer::where($condition)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 设置客户池展示字段 */ public function poolFieldsSet(){ $type = input('type', '', 'intval'); $content = input('content', '', 'trim'); $content = json_decode($content, true); $root_id = request()->empcrm->root_id; $employee_id = request()->empcrm->id; $find = StatisticsSetting::where([['type', '=', 'sq_'.$type], ['employee_id', '=', $employee_id], ['root_id', '=', $root_id]])->findOrEmpty(); $default_field = $this->poolFields($type); foreach ($content as $k => $v){ if(isset($v['name']) && isset($v['show']) && isset($v['sort']) && isset($default_field[$v['name']])){ $default_field[$v['name']]['show'] = $v['show']; $default_field[$v['name']]['sort'] = $v['sort']; } } if ($find->isEmpty()) { $save['employee_id'] = $employee_id; $save['root_id'] = $root_id; $save['content'] = json_encode($default_field); $save['type'] = 'sq_'.$type; $result = StatisticsSetting::insert($save); } else { $find->content = json_encode($default_field); $find->save(); } return json(['code'=> 0, 'msg'=> 'success']); } /** * 客户池展示字段 */ public function poolFields($type = 6){ // 客户池字段 $field1 = [ 'name'=> ['name'=> '客户姓名', 'show'=> 1, 'width'=> 120,'sort'=>1], 'phone'=> ['name'=> '手机号', 'show'=> 1, 'width'=> 100,'sort'=>2], 'employee_name'=> ['name'=> '所属员工', 'show'=> 1, 'width'=> 120,'sort'=>3], 'designer_name'=> ['name'=> '设计师', 'show'=> 1, 'width'=> 120,'sort'=>4], 'source_name'=> ['name'=> '来源渠道', 'show'=> 1, 'width'=> 120,'sort'=>5], 'add_wechat_time'=> ['name'=> '加微时间', 'show'=> 1, 'width'=> 120, 'sort'=> 6], 'customer_type'=> ['name'=> '客户种类', 'show'=> 1, 'width'=> 120,'sort'=> 7], 'community_name'=> ['name'=> '小区名称', 'show'=> 1, 'width'=> 160,'sort'=> 8], 'level'=> ['name'=> '重要', 'show'=> 1, 'width'=> 160,'sort'=> 9], 'sign_time'=> ['name'=> '报名时间', 'show'=> 1, 'width'=> 160,'sort'=> 10], 'house_delivery_time'=> ['name'=> '交房时间', 'show'=> 1, 'width'=> 120,'sort'=> 11], 'state'=> ['name'=> '状态', 'show'=> 1, 'width'=> 120,'sort'=> 12], 'square'=> ['name'=> '面积', 'show'=> 1, 'width'=> 100,'sort'=> 13], 'last_contact_date'=> ['name'=> '跟进时间', 'show'=> 1, 'width'=> 160,'sort'=> 14], 'revisit_time'=> ['name'=> '下次回访时间', 'show'=> 1, 'width'=> 160,'sort'=> 15], 'addtime'=> ['name'=> '添加时间', 'show'=> 1, 'width'=> 160,'sort'=> 16], 'protected_to'=> ['name'=> '保护至', 'show'=> 1, 'width'=> 160,'sort'=> 17], 'visit_log_count'=> ['name'=> '预约次数', 'show'=> 1, 'width'=> 160,'sort'=> 18], 'activity_frequency_count'=> ['name'=> '参加活动次数', 'show'=> 1, 'width'=> 160,'sort'=> 19], 'lognum'=> ['name'=> '跟进次数', 'show'=> 1, 'width'=> 150,'sort'=> 20], 'no_visit_day'=> ['name'=> '未跟进天数', 'show'=> 1, 'width'=> 150,'sort'=> 21], 'subscribe_date'=> ['name'=> '预计见面时间', 'show'=> 1, 'width'=> 150,'sort'=> 22], 'first_liangfang'=> ['name'=> '首次量房时间', 'show'=> 1, 'width'=> 150,'sort'=> 23], 'fisttime'=> ['name'=> '首次到店时间', 'show'=> 1, 'width'=> 150,'sort'=> 24], 'jiaoding_time'=> ['name'=> '签单时间', 'show'=> 1, 'width'=> 120, 'sort'=> 25], 'jiaoding_money'=> ['name'=> '签单金额', 'show'=> 1, 'width'=> 120, 'sort'=> 26], 'qiandan_time'=> ['name'=> '转单时间', 'show'=> 1, 'width'=> 120, 'sort'=> 27], 'qiandan_money'=> ['name'=> '转单金额', 'show'=> 1, 'width'=> 120, 'sort'=> 28], 'assign_list' => ['name'=>'指派客户经理','show'=>1,'width'=>160,'sort'=>29], 'emporg' => ['name'=>'业务员部门','show'=>1,'width'=>160,'sort'=>30], 'unit_number' => ['name'=>'楼栋','show'=>1,'width'=>160,'sort'=>31] //'editbox'=> ['name'=>'编辑','show'=>1,'width'=>150,'sort'=>24] ]; // 到店客户字段 $field2 = [ 'name'=> ['name'=> '客户姓名', 'show'=> 1, 'width'=> 120,'sort'=>1], 'phone'=> ['name'=> '手机号', 'show'=> 1, 'width'=> 100,'sort'=>2], 'employee_name'=> ['name'=> '所属员工', 'show'=> 1, 'width'=> 120,'sort'=>3], 'designer_name'=> ['name'=> '设计师', 'show'=> 1, 'width'=> 120,'sort'=>4], 'source_name'=> ['name'=> '来源渠道', 'show'=> 1, 'width'=> 120,'sort'=>5], 'community_name'=> ['name'=> '小区名称', 'show'=> 1, 'width'=> 160,'sort'=> 6], 'level'=> ['name'=> '重要', 'show'=> 1, 'width'=> 160,'sort'=> 7], 'square'=> ['name'=> '面积', 'show'=> 1, 'width'=> 100,'sort'=> 8], 'state'=> ['name'=> '状态', 'show'=> 1, 'width'=> 120,'sort'=> 9], 'assign_list' => ['name'=>'指派客户经理','show'=>1,'width'=>160,'sort'=>10], 'assign_time' => ['name'=>'指派时间','show'=>1,'width'=>160,'sort'=>11], 'assign_org' => ['name'=>'指派部门','show'=>1,'width'=>160,'sort'=>12], 'assign_type_name' => ['name'=>'指派类型','show'=>1,'width'=>160,'sort'=>13], 'last_contact_date'=> ['name'=> '跟进时间', 'show'=> 1, 'width'=> 160,'sort'=> 14], 'no_visit_day'=> ['name'=> '未跟进天数', 'show'=> 1, 'width'=> 150,'sort'=> 15], 'liangfang_time'=> ['name'=> '量房时间', 'show'=> 1, 'width'=> 150,'sort'=> 16], 'daodian_time'=> ['name'=> '到店时间', 'show'=> 1, 'width'=> 150,'sort'=> 17], 'jiaoding_time'=> ['name'=> '签单时间', 'show'=> 1, 'width'=> 120, 'sort'=> 18], 'jiaoding_money'=> ['name'=> '签单金额', 'show'=> 1, 'width'=> 120, 'sort'=> 19], 'qiandan_time'=> ['name'=> '转单时间', 'show'=> 1, 'width'=> 120, 'sort'=> 20], 'qiandan_money'=> ['name'=> '转单金额', 'show'=> 1, 'width'=> 120, 'sort'=> 21], 'customer_type'=> ['name'=> '客户种类', 'show'=> 1, 'width'=> 120,'sort'=> 22], 'unit_number' => ['name'=>'楼栋','show'=>1,'width'=>160,'sort'=>23] ]; // 待回访客户字段 $field3 = [ 'name'=> ['name'=> '客户姓名', 'show'=> 1, 'width'=> 120,'sort'=>1], 'phone'=> ['name'=> '手机号', 'show'=> 1, 'width'=> 100,'sort'=>2], 'employee_name'=> ['name'=> '所属员工', 'show'=> 1, 'width'=> 120,'sort'=>3], 'designer_name'=> ['name'=> '设计师', 'show'=> 1, 'width'=> 120,'sort'=>4], 'source_name'=> ['name'=> '来源渠道', 'show'=> 1, 'width'=> 120,'sort'=>5], 'community_name'=> ['name'=> '小区名称', 'show'=> 1, 'width'=> 160,'sort'=> 6], 'level'=> ['name'=> '重要', 'show'=> 1, 'width'=> 160,'sort'=> 7], 'square'=> ['name'=> '面积', 'show'=> 1, 'width'=> 100,'sort'=> 8], 'last_contact_date'=> ['name'=> '上次回访', 'show'=> 1, 'width'=> 100,'sort'=> 9], 'jiange_day'=> ['name'=> '间隔', 'show'=> 1, 'width'=> 100,'sort'=> 10] ]; $root_id = request()->empcrm->root_id; switch ($type) { case 1: // 客户池 return $field1; break; case 2: // 到店 return $field2; break; case 3: //待回访 return $field3; break; default: break; } } /** * 客户池展示字段 */ public function poolFieldsList(){ $type = input('type', '', 'intval'); $is_org = input('is_org', '', 'intval'); // 是否是部门统计 目前只有总统计 $root_id = request()->empcrm->root_id; $employee_id = request()->empcrm->id; $find = StatisticsSetting::where([['type', '=', 'sq_'.$type], ['employee_id', '=', $employee_id], ['root_id', '=', $root_id]])->findOrEmpty(); $default_fields = $this->poolFields($type); if (!$find->isEmpty()) { $set = json_decode($find['content'], true); if (!empty($set)) { foreach ($set as $k => $v){ if (isset($default_fields[$k])){ $default_fields[$k]['show'] = $v['show']; $default_fields[$k]['sort'] = $v['sort']; } } array_multisort(array_column($default_fields, 'sort'), SORT_ASC, $default_fields); } } if ($type == 6) { $field_6 = []; foreach ($default_fields as $k => $v) { if ($k == 'name') { unset($default_fields[$k]); $field_6[$k] = $v; } } $field_6 = array_merge($field_6, $default_fields); $default_fields = $field_6; } //处理馨居尚交定签单显示文字 if(request()->empcrm['xinjushang'] == 1){ foreach($default_fields as $key=>$val){ if(strpos($val['name'], '签单') !== false) $default_fields[$key]['name'] = str_replace('签单','交定',$val['name']); if(strpos($val['name'], '转单') !== false) $default_fields[$key]['name'] = str_replace('转单','签单',$val['name']); } } return json(['code'=> 0, 'msg'=> 'success', 'data'=> $default_fields]); } //到访(确认到场)列表 包含 到店、量房、到场、签单、转单 public function confirm_visit_list() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $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],['community_disable','=',0]])->column('id'); } if (!$request->isAjax()) { $this->screen(); //指派经理 $assignemp_list = Employee::where([['org_id','in',orgSubIds($root_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(); } $param = $request->only(['page'=>1, 'limit'=>10, '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','times'=>0,'customer_type']); if(!empty($param['employee_id']) && $is_manager ==1 && in_array($param['employee_id'],$org_employee)) $org_employee = [$param['employee_id']]; $condition[] = ['employee_id', 'in', $org_employee]; $condition[] = ['state', 'not in',Customer::changeState('无效','chaos')]; $tmp_condition = ""; //客户姓名 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'])]; $order = isset($param['order']) ? $param['order'] : 'id desc'; if($param['times'] == 1) $order = 'assign_time asc'; if($param['times'] == 2) $order = 'assign_time desc'; if($param['times'] == 3) $order = 'last_contact_date asc'; if($param['times'] == 4) $order = 'last_contact_date desc'; $page = intval($param['page']); $limit = intval($param['limit']); //客户的跟进时间 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['customer_type'])) { $gather = [ 1=>[['crm_res_id','not null',null]],//'资源库' 2=>[['crm_res_id','null',null],['remark','not like','%活动报名建档%'],['agents_id','null',null]],//'自建' 3=>[['remark','like','%活动报名建档%']],//'活动' 4=>[['agents_id','>',0]]//'经纪人' ]; $arr = explode(',',$param['customer_type']); $whereOr = []; foreach ($arr as $val) { if (isset($gather[$val])) { $whereOr[] = $gather[$val]; } } if (!empty($whereOr)) { $new_cids = Customer::where($condition)->where(function($query) use ($whereOr){ $query->whereOr($whereOr); })->column('id'); unset($condition); $condition[] = ['id','in',$new_cids]; }else{ //无效条件 return json(['code' => 0, 'data' => [], 'count' => 0]); } } //指派时间查询 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]; } //$cusids = Customer::where($condition)->column('id'); //$vs_where[] = ['customer_id','in',$cusids]; $vs_where[] = ['customer_employee_id','in',$org_employee]; $vs_where[] = ['state', 'in', array_merge(CustomerVisitLog::changeState('确认到店','chaos'), CustomerVisitLog::changeState('已量房','chaos'),CustomerVisitLog::changeState('已到场','chaos'), CustomerVisitLog::changeState('已交定','chaos'), CustomerVisitLog::changeState('已签单','chaos'))]; if(!empty($param['date'])) $vs_where[] = ['confirm_date','between',[$param['date'].' 00:00:00',$param['date'].' 23:59:59']]; $vslogid = CustomerVisitLog::where($vs_where)->group('customer_id')->column('customer_id'); $condition[] = ['id', 'in', $vslogid]; //手机号 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[] = ['confirm_date','>=',$start.' 00:00:00']; $logwhere[] = ['confirm_date','<=',$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[] = ['confirm_date','>=',$start.' 00:00:00']; $logwhere[] = ['confirm_date','<=',$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[] = ['confirm_date','>=',$start.' 00:00:00']; $logwhere[] = ['confirm_date','<=',$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[] = ['confirm_date','>=',$start.' 00:00:00']; $logwhere[] = ['confirm_date','<=',$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]; } $vis_where = "if(CustomerVisitLog.state in (7,'已到店'), 1, 0) desc,confirm_date asc"; $tmp_vis_where = 'confirm_date asc'; if($param['times'] > 4){ if($param['times'] == 6) { $vis_where = "if(CustomerVisitLog.state in (7,'已到店'), 1, 0) desc,confirm_date desc"; $tmp_vis_where = 'confirm_date desc'; } $page = ($page - 1) * 17; $tmp_sql = CustomerVisitLog::hasWhere('customer', $condition)->field("if(CustomerVisitLog.state in (7,'已到店'),1,0) as tmp_state")->group('CustomerVisitLog.state,CustomerVisitLog.customer_id')->orderRaw($vis_where)->buildSql(); $sql = "select customer_id from " . $tmp_sql . " a group by a.customer_id ORDER BY a.tmp_state desc," . $tmp_vis_where . " LIMIT " . $page . "," . $limit; $customer_id = Db::query($sql); $data = []; foreach($customer_id as $k => $v){ $one = Customer::with(['employee', 'org', 'designer','source','visitLog' => function ($query) {$query->field('id,customer_id,addtime,state,confirm_date,money');}])->where($condition)->where(['id'=>$v['customer_id']])->find(); $data[$k] = $one->visible(['designer_id','assigned_personnel','assign_type','assign_time','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','house_delivery_time','first_visit_date','ext','transfer_info','deposit_money','signed_money','package_id','add_wechat_time','reputation_gather','into_owner_group'])->toArray(); } }else{ $data = Customer::with(['employee', 'org', 'designer','source','visitLog' => function ($query) { $query->field('id,customer_id,addtime,state,confirm_date,money'); }]) ->where($condition) ->page($page, $limit)->order($order)->select(); $data = $data->visible(['designer_id','assigned_personnel','assign_type','assign_time','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','house_delivery_time','first_visit_date','ext','transfer_info','deposit_money','signed_money','package_id','add_wechat_time','reputation_gather','into_owner_group'])->toArray(); } $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find(); if (!empty($setting)){ $setting_content = json_decode($setting['content'], true); } else { $setting_content = []; } // 指派类型 $assign_type_default = ['yixiang' => '意向客戶指派','liangfang'=>'量房客戶指派','daodian'=>'到店客戶指派']; 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; } } $item['customer_type'] = $item['crm_res_id'] ? '资源库' : ($item['remark'] == '活动报名建档' ? '活动' : ($item['agents_id'] ? '装修推荐官' : '自建')); $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']):''; $item['time_status'] = $item['last_contact_date'] ? $this->uc_time_ago($item['last_contact_date']) :''; $item['add_wechat'] = '否'; $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id',$item['package_id'])->value('name') :''; $item['employee_name'] = !empty($item['employee']) ? $item['employee']['name'] : ''; $item['designer_name'] = !empty($item['designer']) ? $item['designer']['name'] : ''; $item['source_name'] = !empty($item['source']) ? $item['source']['source'] : ''; $item['unit_number'] = ''; if(!empty($item['ext'])){ $extdata=json_decode($item['ext'],true); foreach($extdata as $key=>$val){ if(isset($val['keyname']) && $val['keyname']=='unit_number' && !empty($val['value'])){ $item['unit_number'] = $val['value']; } } } $now_status = $item['state']; if($now_status == '已签单') $now_status = '已转单'; if($now_status == '已交定') $now_status = '已签单'; $assignIdList = !empty($item['assigned_personnel']) ? explode(',',$item['assigned_personnel']) : []; //$assignIdList[] = $item['employee_id']; // 所属销售 if(!empty($item['designer_id'])) $assignIdList[] = $item['designer_id']; // 指派的设计师 $assign_data = Employee::where([['id', 'in', $assignIdList],['root_id','=',$root_id]])->column('name,org_id'); $item['assign_list'] = !empty($assign_data) ? implode(',',array_column($assign_data,'name')) : ''; $assign_org = Org::where([['id', 'in', array_column($assign_data, 'org_id')]])->group('id')->column('name'); $item['assign_org'] = !empty($assign_org) ? implode(',',$assign_org) : ''; $item['assign_type_name'] = $item['state'] = $item['daodian_time'] = $item['liangfang_time'] = $item['jiaoding_time'] = $item['qiandan_time'] = ''; if(!empty($item['assign_type'])){ if(isset($assign_type_default[$item['assign_type']])){ $item['assign_type_name'] = $assign_type_default[$item['assign_type']]; } } if(!empty($item['visitLog'])){ //取最新的一条为客户的跟进时间 $item['last_contact_date'] = $item['visitLog'][count($item['visitLog'])-1]['addtime']; //处理合并多个状态 $stain=[$now_status]; foreach($item['visitLog'] as $k=>$v){ if(!in_array($v['state'],['已交定','已签单','已到店','已量房','已到场','未到访','回访'])) continue; if($v['state']=='回访') $v['state'] = '待确认'; if($v['state']=='已签单') $v['state'] = '已转单'; if($v['state']=='已交定') $v['state'] = '已签单'; if(!in_array($v['state'],$stain)){ $stain[] = $v['state']; } } if(count($stain) > 1 && in_array('待确认',$stain)) $stain = array_diff($stain,['待确认']); $visit_state = ['已到店', '已量房', '已到场', '已签单', '已转单', '已卖卡']; $no_visit_state = ['未到访', '待确认']; $jiaoji = array_intersect($visit_state, $stain); $no_jiaoji = array_intersect($no_visit_state, $stain); if (count($no_jiaoji) == 2) { // 有未到访、待确认两个状态 $stain = ['未到访']; } if (count($stain) > 1 && count($jiaoji) > 0) { $stain = array_diff($stain, $no_visit_state); } $item['state'] = implode(',', $stain); if($now_status == '待确认') $item['state'] = '待确认'; //处理时间 $state = []; foreach($item['visitLog'] as $k=>$v){ $state[$v['state']][] = $v; } foreach($state as $k=>$v){ if($k=='已到店') $item['daodian_time'] = date('Y-m-d',strtotime($v[0]['confirm_date'])); if($k=='已量房') $item['liangfang_time'] = date('Y-m-d',strtotime($v[0]['confirm_date'])); if($k=='已交定'){ $item['jiaoding_time'] = date('Y-m-d',strtotime($v[0]['confirm_date'])); $item['jiaoding_money'] = $v[0]['money']; } if($k=='已签单'){ $item['qiandan_time'] = date('Y-m-d',strtotime($v[0]['confirm_date'])); $item['qiandan_money'] = $v[0]['money']; //$item['sign'] = $v[0]; } } } $item['now_state'] = $now_status; $item['no_visit_day'] = ceil((strtotime(date('Y-m-d')) - strtotime($item['last_contact_date']))/86400) > 0 ? ceil((strtotime(date('Y-m-d')) - strtotime($item['last_contact_date']))/86400) : 0; } $count = Customer::where($condition)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 客户池 */ public function list() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $sub_orgs = orgSubIds($request->empcrm->org_id); $org_id = input('org_id', $request->empcrm->org_id, 'intval'); if(!in_array($org_id, $sub_orgs)){ $org_id = $request->empcrm->org_id; } if ($is_manager) { $team_orgs = orgSubIds($org_id); } else { $team_orgs = [$org_id]; } if (!$request->isAjax()) { //来源 $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']); //部门所有员工 $employee_list = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0],['community_disable','=',0]])->field('id,name')->select()->toArray(); View::assign('employee_list', $employee_list); View::assign('empid', $empid); View::assign('is_manager',$is_manager); //指派经理 $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); // 馨居上 //View::assign('xinjushang', $request->empcrm['xinjushang']); //员工所属部门 $emporg_list = Org::where([['id','in',$team_orgs]])->column('id,name'); View::assign('emporg_list',$emporg_list); // 统计模块的控制 $statistics_setting = Setting::where([['name', '=', 'empcrm_pool_statistics'], ['root_id', '=', $root_id]])->findOrEmpty(); $statistics_setting_content = !$statistics_setting->isEmpty() ? $statistics_setting['content'] : 1; View::assign('statistics_setting', $statistics_setting_content); //统计字段展示控制权限 $type_arr = ['customer_num' => 1, 'visit_num' => 1, 'valid_count' => 1, 'deposit_num' => 1, 'signed_num' => 1, 'yylf_num' => 1, 'lf_num' => 1, 'yydd_num' => 1, 'ydd_num' => 1, 'yyhd_num' => 1, 'ydc_num' => 1]; $setting = Setting::where([['name', '=', 'teamStatisticsJurisdiction'], ['root_id', '=', $root_id]])->value('content'); $setting = $setting ? explode(',', $setting) : []; foreach ($type_arr as $k => $v) { $type_arr[$k] = in_array($k, $setting) ? 1 : 0; } $sum = array_sum(array_values($type_arr)); if ($sum < 5) $type_arr = ['customer_num' => 1, 'visit_num' => 1, 'valid_count' => 1, 'deposit_num' => 1, 'signed_num' => 1, 'yylf_num' => 1, 'lf_num' => 1, 'yydd_num' => 1, 'ydd_num' => 1, 'yyhd_num' => 1, 'ydc_num' => 1]; View::assign('setting', $type_arr); return View::fetch(); } $param = $request->only(['page' => 1, 'limit' => 10, 'addtime'=> '', 'level'=> '', 'source_name'=> '', 'designer_name'=> '', 'keyname'=> '', 'followtime'=> '','state'=> '','employee_name'=> '', 'type'=> '','is_assign']); $search_type = input('search_type', 1, 'intval'); //数据查询方式 1,以客户为主导 2,以满足的搜索状态为主导,合并出所有客户 $startTime = strtotime('2015-01-01'); $endTime = strtotime(date('Y-m-d')) + 86400; $array_intersect = false; $array_intersect_ids = []; if ($search_type == 1) { $org_type = Org::where('id', '=', $org_id)->value('org_type'); // 设计师部门管理员,需要查询指派给下属设计师的客户信息 if ($org_type == 2) { if($is_manager == 1){ $designer_employee = Employee::where([['org_id', 'in', $team_orgs], ['uid', '<>', 0], ['state', '=', '在职'],['community_disable','=',0]])->column('id'); $condition[] = ['designer_id', 'in', $designer_employee]; // 是哪个员工 if($param['employee_name']) $condition[] = ['employee_id','in',explode(',',$param['employee_name'])]; } else { $condition[] = ['designer_id', '=', $empid]; } } else { if($is_manager == 1){ $empids = Employee::where([['root_id','=',$root_id],['org_id', 'in', $team_orgs], ['uid', '<>', 0], ['state', '=', '在职'],['community_disable','=',0]])->column('id'); $condition[] = ['employee_id', 'in', $empids]; // 是哪个员工 if($param['employee_name']) $condition[] = ['employee_id','in',explode(',',$param['employee_name'])]; } else { $condition[] = ['employee_id', '=', $empid]; } } $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')]; $condition[] = ['died', '<>', 2]; if (!empty($param['addtime'])) { //录入时间 $addtimeArr = explode(' - ', $param['addtime']); $start = $addtimeArr[0]; $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400); $startTime = strtotime($start) - 1; $endTime = strtotime($end); } $condition[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)]; $condition[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)]; } else { $array_intersect_ids = $this->logCondition(); $array_intersect = true; $org_type = Org::where('id', '=', $org_id)->value('org_type'); // 设计师部门管理员,需要查询指派给下属设计师的客户信息 if ($org_type == 2) { if($is_manager == 1){ $designer_employee = Employee::where([['org_id', 'in', $team_orgs], ['uid', '<>', 0], ['state', '=', '在职']])->column('id'); $condition[] = ['designer_id', 'in', $designer_employee]; // 是哪个员工 setCondition($param['employee_name'], 'designer_id', '=', $condition); } else { $condition[] = ['designer_id', '=', $empid]; } } else { if($is_manager == 1){ $condition[] = ['org_id', 'in', $team_orgs]; $condition[] = ['employee_id', '>', 0]; // 是哪个员工 setCondition($param['employee_name'], 'employee_id', '=', $condition); } else { $condition[] = ['employee_id', '=', $empid]; } } $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')]; $condition[] = ['died', '<>', 2]; } 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['designer_name'])) $condition[] = ['designer_id', 'in', explode(',',$param['designer_name'])]; if(!empty($param['is_assign']) && $param['is_assign'] == 1) $condition[] = ['assigned_personnel','NOTNULL',null]; if(!empty($param['is_assign']) && $param['is_assign'] == 2) $condition[] = ['assigned_personnel','NULL',null]; //2023-02-22 搜索条件修改 $new_params = $request->only(['emporg','assign_list','revisit_time'=>'','phone'=>'','name'=>'','community_name'=>'','customer_type'=>'','first_liangfang'=>'','last_contact_date'=>'','fisttime'=>'','house_delivery_time'=>'','subscribe_date'=>'','square'=>'','no_visit_day'=>'','sign_time'=>'', 'add_wechat_time'=> '','jiaoding_time'=>'','qiandan_time'=>'']); if (!empty($new_params['phone'])) { $list = Customer::where($condition)->column('id,phone'); $keyCustomersId = []; $cypherphone = cypherphone($new_params['phone']); foreach ($list as $k => $v) { if($cypherphone == $v['phone']) $keyCustomersId[] = $v['id']; } unset($condition); $condition[] = ['id', 'in', $keyCustomersId]; } //客户姓名 if ($new_params['name']) $condition[] = ['name', 'like', '%' . $new_params['name'] . '%']; //小区名称 if ($new_params['community_name']) $condition[] = ['community_name', 'like', '%' . $new_params['community_name'] . '%']; //客户种类 资源库,活动,经纪人,自建 if ($new_params['customer_type']) { $gather = [ 1=>[['crm_res_id','not null',null]],//'资源库' 2=>[['crm_res_id','null',null],['remark','not like','%活动报名建档%'],['agents_id','null',null]],//'自建' 3=>[['remark','like','%活动报名建档%']],//'活动' 4=>[['agents_id','>',0]]//'经纪人' ]; $arr = explode(',',$new_params['customer_type']); $whereOr = []; foreach ($arr as $val) { if (isset($gather[$val])) { $whereOr[] = $gather[$val]; } } if (!empty($whereOr)) { $new_cids = Customer::where($condition)->where(function($query) use ($whereOr){ $query->whereOr($whereOr); })->column('id'); unset($condition); $condition[] = ['id','in',$new_cids]; }else{ //无效条件 return json(['code' => 0, 'data' => [], 'count' => 0]); } } //加微时间 if (!empty($new_params['add_wechat_time'])) { $add_wechat_time = explode(' - ', $new_params['add_wechat_time']); $start = str_replace('/','-',$add_wechat_time[0]); $end = str_replace('/','-',$add_wechat_time[1] . ' 23:59:59'); $condition[] = ['add_wechat_time', '>=', $start]; $condition[] = ['add_wechat_time', '<', $end]; } //下次回访时间 if (!empty($new_params['revisit_time'])) { $revisit_time = explode(' - ', $new_params['revisit_time']); $start = str_replace('/','-',$revisit_time[0]. ' 00:00:00'); $end = str_replace('/','-',$revisit_time[1] . ' 23:59:59'); $condition[] = ['revisit_time', '>', $start]; $condition[] = ['revisit_time', '<', $end]; } //交房时间 if (!empty($new_params['house_delivery_time'])) { $house_delivery_time = explode(' - ', $new_params['house_delivery_time']); $start = $house_delivery_time[0]; $end = $house_delivery_time[1] . ' 23:59:59'; $condition[] = ['house_delivery_time', '>=', $start]; $condition[] = ['house_delivery_time', '<=', $end]; } //报名时间 if (!empty($new_params['sign_time'])) { $sign_time = explode(' - ', $new_params['sign_time']); $start = str_replace('/','-',$sign_time[0]. ' 00:00:00'); $end = str_replace('/','-',$sign_time[1]. ' 23:59:59'); $condition[] = ['sign_time', '>=', $start]; $condition[] = ['sign_time', '<=', $end]; } //客户的跟进时间 if(!empty($new_params['last_contact_date'])){ $last_contact = explode(' - ', $new_params['last_contact_date']); $start = str_replace('/','-',$last_contact[0]); $end = str_replace('/','-',$last_contact[1]); $condition[] = ['last_contact_date','>=',$start]; $condition[] = ['last_contact_date','<=',$end]; } //首次量房时间 if(!empty($new_params['first_liangfang'])){ $first_liangfang = explode(' - ', $new_params['first_liangfang']); $start = str_replace('/','-',$first_liangfang[0]); $end = date('Y-m-d', strtotime($first_liangfang[1]) + 86400); $customersIdList = Customer::where($condition)->order('addtime desc')->column('id'); $lf_where[] = ['customer_id', 'in', $customersIdList]; $lf_where[] = ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')]; $lf_where[] = ['addtime','>=',$start]; $lf_where[] = ['addtime','<=',$end]; $lflog = CustomerVisitLog::where($lf_where)->group('customer_id')->order('addtime asc')->column('customer_id'); unset($condition); $condition[] = ['id','in',$lflog]; } //首次到店时间 if(!empty($new_params['fisttime'])){ $fisttime = explode(' - ', $new_params['fisttime']); $start = str_replace('/','-',$fisttime[0]); $end = date('Y-m-d', strtotime($fisttime[1]) + 86400); $customersIdList = Customer::where($condition)->order('addtime desc')->column('id'); $dd_where[] = ['customer_id', 'in', $customersIdList]; $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]; $dd_where[] = ['confirm_date','>=',$start]; $dd_where[] = ['confirm_date','<=',$end]; $ddlog = CustomerVisitLog::where($dd_where)->group('customer_id')->order('addtime asc')->column('customer_id'); unset($condition); $condition[] = ['id','in',$ddlog]; } //预计见面时间 if (!empty($new_params['subscribe_date'])) { $subscribe_date = explode(' - ', $new_params['subscribe_date']); $start = $subscribe_date[0]; $end = date('Y-m-d', strtotime($subscribe_date[1]) + 86400); $customersIdList = Customer::where($condition)->order('addtime desc')->column('id'); $yj_where[] = ['customer_id', 'in', $customersIdList]; $yj_where[] = ['state', '=', 0]; $yj_where[] = ['subscribe_date', '>=', $start]; $yj_where[] = ['subscribe_date', '<=', $end]; $sublog = CustomersSubscribe::where($yj_where)->group('customer_id')->order('subscribe_date desc')->column('customer_id'); unset($condition); $condition[] = ['id','in',$sublog]; } //签单时间 if(!empty($new_params['jiaoding_time'])){ $fisttime = explode(' - ', $new_params['jiaoding_time']); $start = str_replace('/','-',$fisttime[0]. ' 00:00:00'); $end = str_replace('/','-',$fisttime[1] . ' 23:59:59'); $customersIdList = Customer::where($condition)->order('addtime desc')->column('id'); $dd_where[] = ['customer_id', 'in', $customersIdList]; $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]; $ddlog = CustomerVisitLog::where($dd_where)->whereBetween('addtime',[$start,$end])->group('customer_id')->order('addtime asc')->column('customer_id'); unset($condition); $condition[] = ['id','in',$ddlog]; } //签单时间 if(!empty($new_params['qiandan_time'])){ $fisttime = explode(' - ', $new_params['qiandan_time']); $start = str_replace('/','-',$fisttime[0]. ' 00:00:00'); $end = str_replace('/','-',$fisttime[1] . ' 23:59:59'); $customersIdList = Customer::where($condition)->order('addtime desc')->column('id'); $dd_where[] = ['customer_id', 'in', $customersIdList]; $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')]; $ddlog = CustomerVisitLog::where($dd_where)->whereBetween('addtime',[$start,$end])->group('customer_id')->order('addtime asc')->column('customer_id'); unset($condition); $condition[] = ['id','in',$ddlog]; } //客户经理 if(!empty($new_params['assign_list'])){ $assign_employee = explode(',', $new_params['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($new_params['no_visit_day'])) { $no_visit_day = explode(',',$new_params['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($new_params['square'])){ $square = explode(',',$new_params['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($new_params['emporg'])){ $orgids = explode(',',$new_params['emporg']); $empids = Employee::where([['root_id','=',$root_id],['org_id','in',$orgids]])->column('id'); $customersList = Customer::where($condition)->where('employee_id','in',$empids)->column('id'); unset($condition); $condition[] = ['id','in',$customersList]; } if ($search_type == 1) { if (!empty($param['addtime'])) { //录入时间 $addtimeArr = explode(' - ', $param['addtime']); $start = $addtimeArr[0]; $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400); $startTime = strtotime($start) - 1; $endTime = strtotime($end); } } else { if (!empty($param['followtime'])) { //录入时间 $addtimeArr = explode(' - ', $param['followtime']); $start = $addtimeArr[0]; $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400); $startTime = strtotime($start) - 1; $endTime = strtotime($end); } } // 状态筛选 $c_ids = []; if (!empty($param['state'])) { $condition1[] = ['state','in',Customer::changeState('待确认','chaos')]; $condition1[] = ['crm_res_id','NULL',null]; $condition2[] = ['state','not in',Customer::changeState('待确认','chaos')]; //从公海获取的客户是资源库的要显示在客户池 $condition3[] = ['state','in',Customer::changeState('待确认','chaos')]; $condition3[] = ['crm_res_id','NOTNULL',null]; $condition3[] = ['valid_time','NOTNULL',null]; $customersIdList = Customer::where($condition)->where(function($query) use ($condition1,$condition2,$condition3){ $query->whereOr([$condition1,$condition2,$condition3]); })->order('addtime desc')->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', $customersIdList]; $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(function($query) use ($condition1,$condition3){ $query->whereOr([$condition1,$condition3]); })->column('id'); } if($v=='有效'){ $validids = Customer::where($condition)->where('state','not in',Customer::changeState('待确认','chaos'))->column('id'); } if($v=='未到访'){ $novisitids = Customer::where($condition)->where('state','in',Customer::changeState('未到访','chaos'))->column('id'); } } unset($condition,$condition1,$condition2,$condition3); if(!empty($confirmedids)) $c_ids = array_merge($confirmedids,$c_ids); if(!empty($validids)) $c_ids = array_merge($validids,$c_ids); if(!empty($novisitids)) $c_ids = array_merge($novisitids,$c_ids); }else{ //2023-02-10 客户有效状态 为排除待确认和无效, 或者 待确认并且不是资源库过来的 $state1 = Customer::changeState('待确认', 'chaos'); $state2 = Customer::changeState('无效', 'chaos'); $query1 = array_merge($condition,[['state', 'not in', array_merge($state1,$state2)]]); $query2 = array_merge($condition,[['state', 'in',$state1],['crm_res_id','NULL',NULL]]); $query3 = array_merge($condition,[['state', 'in',$state1],['crm_res_id','NOTNULL',NULL],['valid_time','NOTNULL',null]]); $ids = Customer::whereOr([$query1,$query2,$query3])->order('id desc')->column('id'); unset($condition); $c_ids = $ids; } if ($array_intersect) { $c_ids = array_intersect($c_ids, $array_intersect_ids); $count = count($c_ids); $c_ids = array_slice($c_ids, ($param['page'] - 1) * $param['limit'], $param['limit']); $condition[] = ['id', 'in', $c_ids]; } else { $count = count($c_ids); $c_ids = array_slice($c_ids, ($param['page'] - 1) * $param['limit'], $param['limit']); $condition[] = ['id', 'in', $c_ids]; } $order = isset($param['order']) ? $param['order'] : 'id desc'; $page = intval($param['page']); $limit = intval($param['limit']); //$data = $this->selCustomer($condition, $page, $limit, $order, $root_id); $data = $this->selCustomer($condition, 0, 0, $order, $root_id); $no_visit_state = ['未到访', '待确认']; if(!empty($stateall) && in_array('未到访',$stateall)) $no_visit_state = ['待确认']; $visit_state = ['已到店', '已量房', '已到场', '已签单', '已转单', '已卖卡']; foreach($data as &$item) { //客户种类 $item['customer_type'] = $item['crm_res_id'] ? '资源库' : ($item['remark'] == '活动报名建档' ? '活动' : ($item['agents_id'] ? '经纪人' : '自建')); // 有'已到店', '已量房', '已到场', '已签单', '已转单'状态的话不显示未到访、待确认 // 有未到访状态不显示待确认 $state = explode(',', $item['state']); $jiaoji = array_intersect($visit_state, $state); $no_jiaoji = array_intersect($no_visit_state, $state); if (count($no_jiaoji) == 2) { // 有未到访、待确认两个状态 $state = ['未到访']; } if (count($state) > 1 && count($jiaoji) > 0) { $state = array_diff($state, $no_visit_state); } $item['state'] = implode(',', $state); } //$count = Customer::where($condition)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 以客户跟进为主的查询条件 * */ public function logCondition(){ $param = $this->request->only(['followtime' => '', 'source' => '', 'employee_id' => '', 'org_id' => '', 'type' => '']); $is_manager = request()->empcrm->is_manager; $employee_id = request()->empcrm->id; $org_id = request()->empcrm->org_id; $startTime = strtotime('2015-01-01'); $endTime = strtotime(date('Y-m-d')) + 86400; if (!empty($param['followtime'])) { //录入时间 $addtimeArr = explode(' - ', $param['followtime']); $start = $addtimeArr[0]; $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400); $startTime = strtotime($start) - 1; $endTime = strtotime($end); } // 查询条件 // 设计师部门管理员,需要查询指派给下属设计师的客户信息 $org_type = Org::where('id', '=', $org_id)->value('org_type'); if ($param['org_id']) { $param_sub_org = orgSubIds($param['org_id']); } else { $param_sub_org = orgSubIds($org_id); } if ($org_type == 2) { if($is_manager == 1){ $designer_employee = Employee::where([['org_id', 'in', $param_sub_org], ['uid', '<>', 0], ['state', '=', '在职']])->column('id'); $condition[] = ['designer_id', 'in', $designer_employee]; $condition_log[] = ['Customer.designer_id', 'in', $designer_employee]; $s_where[] = ['employee_id', 'in', $designer_employee]; // 是哪个员工 setCondition($param['employee_id'], 'designer_id', '=', $condition); setCondition($param['employee_id'], 'Customer.designer_id', '=', $condition_log); setCondition($param['employee_id'], 'employee_id', '=', $s_where); } else { $condition[] = ['designer_id', '=', $employee_id]; $condition_log[] = ['Customer.designer_id', '=', $employee_id]; $s_where[] = ['employee_id', '=', $employee_id]; } } else { if($is_manager == 1){ $condition[] = ['org_id', 'in', $param_sub_org]; $condition[] = ['employee_id', '>', 0]; $condition_log[] = ['Customer.org_id', 'in', $param_sub_org]; $condition_log[] = ['Customer.employee_id', '>', 0]; $s_where[] = ['org_id', 'in', $param_sub_org]; $s_where[] = ['employee_id', '>', 0]; // 是哪个员工 setCondition($param['employee_id'], 'employee_id', '=', $condition); setCondition($param['employee_id'], 'Customer.employee_id', '=', $condition_log); setCondition($param['employee_id'], 'employee_id', '=', $s_where); } else { $condition[] = ['employee_id', '=', $employee_id]; $condition_log[] = ['Customer.employee_id', '=', $employee_id]; $s_where[] = ['employee_id', '=', $employee_id]; } } $condition[] = ['died', '<>', 2]; $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')]; $condition_log[] = ['Customer.died', '<>', 2]; $condition_log[] = ['Customer.state', 'not in', Customer::changeState('无效', 'chaos')]; $state_n = Customer::changeState('待确认', 'chaos'); $state_w = Customer::changeState('无效', 'chaos'); $query1 = array_merge($condition,[['state', 'not in', array_merge($state_n,$state_w)]]); $query2 = array_merge($condition,[['state', 'in',$state_n],['crm_res_id','NULL',NULL]]); $ids = Customer::whereOr([$query1,$query2])->column('id'); // 模块 // 加v $condition_jiav = $condition; $condition_jiav[] = ['add_wechat_time', '>', date('Y-m-d H:i:s', $startTime)]; $condition_jiav[] = ['add_wechat_time', '<', date('Y-m-d H:i:s', $endTime)]; $jiav_ids_find = Customer::where($condition_jiav)->column('id'); $jiav_ids = array_intersect($jiav_ids_find, $ids); // 交房 $condition_jiaofang = $condition; $condition_jiaofang[] = ['house_delivery_time', '>', date('Y/m/d H:i:s', $startTime)]; $condition_jiaofang[] = ['house_delivery_time', '<', date('Y/m/d H:i:s', $endTime)]; $jiaofang_ids_find = Customer::where($condition_jiaofang)->column('id'); $jiaofang_ids = array_intersect($jiaofang_ids_find, $ids); //预计量房 预计到店 if ($is_manager) { $s_where[] = ['org_id', 'in', $param_sub_org]; } else { $s_where[] = ['employee_id', '=', $employee_id]; } $s_where[] = ['state', '=', 0]; $s_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)]; $s_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)]; $s_where[] = ['type', 'in', [1,3]]; $yuji_ids_find = CustomersSubscribe::where($s_where)->group('customer_id')->column('customer_id'); $yuji_ids = array_intersect($yuji_ids_find, $ids); $customer_ids_1 = array_values(array_unique(array_merge($jiav_ids, $jiaofang_ids, $yuji_ids))); // 时间段内有跟踪 $condition_log[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)]; $condition_log[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)]; $customer_ids_2_find = Db::view('Customer') ->view('Org', '', 'Org.id=Customer.org_id') ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id') ->where($condition_log) ->group('Customer.id') ->column('Customer.id'); $customer_ids_2 = array_intersect($customer_ids_2_find, $ids); $customer_ids = array_values(array_unique(array_merge($customer_ids_1, $customer_ids_2))); return $customer_ids; } /** * 判断能否查看客户详情 */ public function checkCustomer() { $id = input('id', '', 'intval'); $request = request(); $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $where = [ ['id', '=', $id], ['state', '<>', '无效'] ]; $data = Customer::where($where)->find(); if (empty($data)){ return json(['code'=> 1, 'msg'=> '无法查看此客户,请刷新后重试']); } $empIdList = explode(',', $data->employee_id . ',' . $data->employee_id . ',' . $data->assigned_personnel); $empIdList = array_unique(array_filter($empIdList)); if(!empty($data) && !in_array($empid, $empIdList)){ if ($is_manager == 1) { $team_orgs = orgSubIds($request->empcrm->org_id); $yuangong = Employee::where([['org_id', 'in', $team_orgs], ['uid', '>', 0], ['id', 'in', $empIdList]])->count(); if ($yuangong == 0) $data = null; } else { $data = null; } } if (empty($data)){ return json(['code'=> 1, 'msg'=> '无法查看此客户,请刷新后重试']); } return json(['code'=> 0, 'msg'=> 'success']); } //添加回访记录 public function addlog() { $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(['cid','org_id'=>'']); $orgids = orgSubIds($root_id); //if(!empty($param['org_id']) && !in_array($param['org_id'],$team_orgs)){ //管理层跟员工校验时 if(!empty($param['org_id']) && in_array($param['org_id'],$orgids)){ $ms = $this->other_seLemp($param['cid'],$param['org_id']); $is_sharecus = 1; }else{ $ms = $this->seLemp($param['cid'],$empid); } $data = $ms->toArray(); $stain = []; if(!empty($data['visitLog'])){ $state1 = CustomerVisitLog::changeState('已到店', 'chaos'); $state2 = CustomerVisitLog::changeState('已到场', 'chaos'); foreach($data['visitLog'] as $key=>$val){ if($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已转单'; if($val['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已签单'; if($val['state'] == '已转单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已签单','已转单',$val['remark']); if($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已交定','已签单',$val['remark']); $data['visitLog'][$key]['state'] = $val['state']; $data['visitLog'][$key]['empname'] = Employee::where('id',$val['employee_id'])->value('name'); if(!in_array($val['state'],$stain)){ $stain[] = $val['state']; } if(!empty($val['save_portrait_field'])){ $save_portrait_field = json_decode($val['save_portrait_field'],true); foreach($save_portrait_field as $k=>$v){ if($v['type'] == 6){ $save_portrait_field[$k]['valname'] = explode(',',$v['value']); } } }else{ $save_portrait_field = ''; } $data['visitLog'][$key]['save_portrait_field'] = $save_portrait_field; $parr[] = $val['addtime']; if($val['next_contact_date'] > date('Y-m-d H:i:s')){ $data['visitLog'][$key]['isyes'] = 1; }else{ $data['visitLog'][$key]['isyes'] = 0; } $data['visitLog'][$key]['wisdom_url'] = ''; $data['visitLog'][$key]['is_wisdom'] = 0; if ($val['remark'] && strpos($val['remark'],'讲解智慧屏##')!==false) { //remark = 接待了客户:张三,讲解时长21分钟@讲解智慧屏## url; $data['visitLog'][$key]['is_wisdom'] = 1; $arr = explode('##',$val['remark']); if(count($arr)==2){ $ali_oss_bindurl = config('app.ali_oss_bindurl'); $data['visitLog'][$key]['wisdom_url'] = 'https://' . $ali_oss_bindurl . '/' . $arr[1]; $title = explode('@',$arr[0]); $data['visitLog'][$key]['remark'] = $title[0]; } } // 检测是否有录音 if($val['data_type'] == 'out_call'){ $data['visitLog'][$key]['is_wisdom'] = 2; $data['visitLog'][$key]['wisdom_url'] = OutCallLog::where(['id'=>$val['data_id']])->value('url'); } if ($val['number_of_visitors'] > 0 && in_array($val['state'], $state1)) { $val['remark'] = $val['remark'] . ' 到店人数:' . $val['number_of_visitors'] . '人;'; $data['visitLog'][$key]['remark'] = $val['remark']; } if ($val['talking_order_time'] && in_array($val['state'], $state1)) { $val['remark'] = $val['remark'] . ' 谈单时长:' . $val['talking_order_time'] . '分钟;'; $data['visitLog'][$key]['remark'] = $val['remark']; } if ($val['number_of_visitors'] > 0 && in_array($val['state'], $state2)) { $val['remark'] = $val['remark'] . ' 到场人数:' . $val['number_of_visitors'] . '人;'; $data['visitLog'][$key]['remark'] = $val['remark']; } if ($val['stay_length'] && in_array($val['state'], $state2)) { $val['remark'] = $val['remark'] . ' 停留时间:' . $val['stay_length'] . '分钟;'; $data['visitLog'][$key]['remark'] = $val['remark']; } //检测是否有下载文件 if(!empty($val['document_field']) && !empty($val['document_name'])){ $str = $root_id.'#'.$empid.'#'.$val['id']; $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->encrypt($str); $data['visitLog'][$key]['document_field'] = $request->domain().'/downdocument/index.html?token='.$value; } } array_multisort($parr, SORT_DESC, $data['visitLog']); } $data['allstate'] = $stain; //房屋状态 $house_status=CustomerPortraitField::with(['select'])->where([['root_id','=',$root_id],['keyname','=','house_status']])->find()->toArray(); View::assign('house_status', $house_status['select']); View::assign('empname', $request->empcrm->name); //指派类型 $assign_count = Setting::where([['root_id','=',$root_id],['name','=','assign_type']])->findOrEmpty(); $assign_name = !$assign_count->isEmpty() ? json_decode($assign_count->content,true) : []; $assign_list = []; foreach($assign_name as $k=>$v){ if($v=='yixiang') $assign_list[] =['keyname'=>$v,'value'=>'意向客户指派']; if($v=='liangfang') $assign_list[] =['keyname'=>$v,'value'=>'量房客户指派']; if($v=='daodian') $assign_list[] =['keyname'=>$v,'value'=>'到店客户指派']; } View::assign('assign_list', $assign_list); $isadd_log = 0; $path = Org::where([['pid','=',$request->empcrm->org_id]])->count(); if(empty($path) && $is_manager && in_array($data['org_id'],$team_orgs)){ $isadd_log = 1; } if($data['employee_id'] == $empid){ $isadd_log = 1; } if(!empty($is_sharecus) && $is_manager) $isadd_log = 1; View::assign('data', $data); View::assign('isadd_log', $isadd_log); View::assign('is_manager', $is_manager); // 判断是否有外呼系统 $outcallSetting = OutCallSetting::where(['root_id' => $root_id, 'type' => 'yhuatong'])->find(); if ($outcallSetting !== null) { // 如果有外呼系统则获取登陆人拨打的电话列表 View::assign('outcall', 1); $phones = empty($request->empcrm['out_call_phone']) ? [] : explode(',', $request->empcrm['out_call_phone']); array_unshift($phones, $request->empcrm['phone']); View::assign('outcall_phone', $phones); } else { View::assign('outcall', 0); } //判断下次回访时间是否必传 $next_where[] = ['root_id', '=', $root_id]; $next_where[] = ['name', '=', 'nextVisitTimeIsOk']; $nextVisitTimeIsOk = Setting::where($next_where)->value('content'); View::assign('nextVisitTimeIsOk', $nextVisitTimeIsOk); return View::fetch(); } //公用查询客户详情 public function other_seLemp($id,$org_id) { $request = request(); $root_id = $request->empcrm->root_id; $orgids = orgSubIds($root_id); if(!in_array($org_id,$orgids)) echo json(['code'=>1,'msg'=>'数据不存在']); $condition = [ ['org_id', '=', $org_id], ['id', '=', $id] ]; $data = Customer::with(['source', 'designer' => function ($query) { $query->field('id,name,headimgurl,org_id,uid'); }, 'employee' => function ($query) { $query->withField(['id','name']); },'visitLog' => function ($query){ $query->field('id,customer_id,type,next_contact_date,remark,addtime,employee_id,state,customer_employee_id,img,img1,sign_img,delivery_img,save_portrait_field,data_type,data_id,number_of_visitors,talking_order_time,stay_length,document_field,document_name'); }])->where($condition)->find(); if (empty($data)) echo json(['code' => 1, 'msg' => '数据不存在']); //if (empty($data)) return false; $data['daochang'] = CustomerVisitLog::where([['customer_id', '=', $id],['employee_id','=',$data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已到访'])])->order('addtime desc')->find(); $data['deposit'] = CustomerVisitLog::where([['customer_id', '=', $id],['employee_id','=',$data['employee_id']], CustomerVisitLog::changeState(['state', '=', '交定'])])->order('addtime desc')->field("id,money,DATE_FORMAT(confirm_date, '%Y-%m-%d') as confirm_date")->find(); $data['sign'] = CustomerVisitLog::where([['customer_id', '=', $id],['employee_id','=',$data['employee_id']], CustomerVisitLog::changeState(['state', '=', '签单'])])->order('addtime desc')->field("id,money,DATE_FORMAT(confirm_date, '%Y-%m-%d') as confirm_date")->find(); $data['dd'] = CustomerVisitLog::where([['customer_id', '=', $id],['customer_employee_id','=',$data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已到店'])])->order('addtime desc')->value("DATE_FORMAT(confirm_date, '%Y-%m-%d')"); $data['lf'] = CustomerVisitLog::where([['customer_id', '=', $id],['customer_employee_id','=',$data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已量房'])])->order('addtime desc')->value("DATE_FORMAT(confirm_date, '%Y-%m-%d')"); $data['package_name'] = $data['package_id'] ? CustomerPackage::where('id',$data['package_id'])->value('name') :''; $data['deco_style_name'] = $data['deco_style'] ? Decostyle::where('id',$data['deco_style'])->value('name') : ''; //$data['company_name'] = Company::where('root_id',$root_id)->value('company_name'); $data['add_wechat'] = '否'; $data['housetype'] = $data['unit_number'] = $data['subjection_plan'] = null; if(!empty($data['ext'])){ $field = $this->get_portrait_field($data); foreach($field as $key=>$val){ if($val['keyname'] == 'wechat' && !empty($val['value'])){ $data['add_wechat'] = CustomerPortraitFieldSelect::where('id',$val['value'])->value('name'); } if($val['keyname']=='housetype_arrow' && !empty($val['value'])){ $data['housetype'] = CustomerPortraitFieldSelect::where('id',$val['value'])->value('name'); } if($val['keyname']=='subjection_plan' && !empty($val['value'])){ $data['subjection_plan'] = CustomerPortraitFieldSelect::where('id',$val['value'])->value('name'); } if($val['keyname']=='unit_number' && !empty($val['value'])){ $data['unit_number'] = $val['value']; } } } $data['daochang'] = !empty($data['daochang']) ? json_decode($data['daochang'],true) : null; $data['deposit'] = !empty($data['deposit']) ? json_decode($data['deposit'],true) : null; $data['sign'] = !empty($data['sign']) ? json_decode($data['sign'],true) : null; return $data; } //公用查询客户详情 public function seLemp($id,$empid) { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $orgids = orgSubIds($root_id); $is_manager = $request->empcrm->is_manager; $where = [ ['id', '=', $id], ['state', '<>', '无效'] ]; //增加查询是否要显示修改客户扩展字段信息功能 $showEmpVistlog = Setting::where([['root_id', '=', $root_id], ['name', '=', 'showEmpVistlog']])->findOrEmpty(); $visit = !$showEmpVistlog->isEmpty() ? (int)$showEmpVistlog->content : 0; $vlg_where[] = $visit ? ['save_portrait_field','NULL',null] : ['id','>',0]; $data = Customer::with(['source', 'designer' => function ($query) { $query->field('id,name,headimgurl,org_id,uid'); }, 'employee' => function ($query) { $query->withField(['id','name']); },'visitLog' => function ($query) use ($vlg_where){ $query->where($vlg_where)->field('id,customer_id,type,next_contact_date,remark,addtime,employee_id,state,customer_employee_id,img,img1,sign_img,delivery_img,save_portrait_field,data_type,data_id,number_of_visitors,talking_order_time,stay_length,document_field,document_name'); }])->where($where)->find(); $empIdList = explode(',', $data->employee_id . ',' . $data->employee_id . ',' . $data->assigned_personnel); $empIdList = array_unique(array_filter($empIdList)); if(!empty($data) && !in_array($empid, $empIdList)){ if ($is_manager == 1) { $team_orgs = orgSubIds($request->empcrm->org_id); $yuangong = Employee::where([['org_id', 'in', $team_orgs], ['uid', '>', 0], ['id', 'in', $empIdList]])->count(); if ($yuangong == 0) $data = null; } else { $data = null; } } if (empty($data)){ echo json_encode(['code' => 1, 'msg' => '数据不存在']); exit; } $data['daochang'] = CustomerVisitLog::where([['customer_id', '=', $id],['customer_employee_id','=',$data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已到访'])])->order('addtime desc')->find(); $data['deposit'] = CustomerVisitLog::where([['customer_id', '=', $id],['customer_employee_id','=',$data['employee_id']], CustomerVisitLog::changeState(['state', '=', '交定'])])->order('addtime desc')->field("id,money,DATE_FORMAT(confirm_date, '%Y-%m-%d') as confirm_date,addtime")->find(); $data['sign'] = CustomerVisitLog::where([['customer_id', '=', $id],['customer_employee_id','=',$data['employee_id']], CustomerVisitLog::changeState(['state', '=', '签单'])])->order('addtime desc')->field("id,money,DATE_FORMAT(confirm_date, '%Y-%m-%d') as confirm_date,addtime")->find(); $data['dd'] = CustomerVisitLog::where([['customer_id', '=', $id],['customer_employee_id','=',$data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已到店'])])->order('addtime desc')->value("DATE_FORMAT(confirm_date, '%Y-%m-%d')"); $data['lf'] = CustomerVisitLog::where([['customer_id', '=', $id],['customer_employee_id','=',$data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已量房'])])->order('addtime desc')->value("DATE_FORMAT(confirm_date, '%Y-%m-%d')"); $data['package_name'] = $data['package_id'] ? CustomerPackage::where('id',$data['package_id'])->value('name') :''; $data['deco_style_name'] = $data['deco_style'] ? Decostyle::where('id',$data['deco_style'])->value('name') : ''; $data['company_name'] = Company::where('root_id',$root_id)->value('company_name'); $data['add_wechat'] = '否'; $data['housetype'] = $data['unit_number'] = $data['subjection_plan'] = null; $data['sign_time'] = ''; if(!empty($data['ext'])){ $field = $this->get_portrait_field($data); // var_dump($field); // exit; foreach($field as $key=>$val){ if($val['keyname'] == 'wechat' && !empty($val['value'])){ $data['add_wechat'] = CustomerPortraitFieldSelect::where('id',$val['value'])->value('name'); } if($val['keyname']=='housetype_arrow' && !empty($val['value'])){ $data['housetype'] = CustomerPortraitFieldSelect::where('id',$val['value'])->value('name'); } if($val['keyname']=='subjection_plan' && !empty($val['value'])){ $data['subjection_plan'] = CustomerPortraitFieldSelect::where('id',$val['value'])->value('name'); } if($val['keyname']=='unit_number' && !empty($val['value'])){ $data['unit_number'] = $val['value']; } if($val['keyname']=='sign_name' && !empty($val['value'])){ $data['sign_time'] = $val['value']; } } } $data['daochang'] = !empty($data['daochang']) ? json_decode($data['daochang'],true) : null; $data['deposit'] = !empty($data['deposit']) ? json_decode($data['deposit'],true) : null; $data['sign'] = !empty($data['sign']) ? json_decode($data['sign'],true) : null; $data['assigned_personnel'] = !empty($data['designer_id']) ? $data['assigned_personnel'].','.$data['designer_id'] : $data['assigned_personnel']; return $data; } //新获取客户扩展字段 public function get_portrait_field($cusdata) { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $w[] = ['root_id', '=', $root_id]; $list=CustomerPortraitField::with(['select'])->where([['root_id','=',$root_id],['pid','<>',0]])->select()->toArray(); $cuslog=new CustomerLogic; $new = $list; if(!empty($cusdata['ext']) && $cusdata['ext'] != 'null'){ $extdata=json_decode($cusdata['ext'],true); if(isset($extdata['ext1'])){ $new=$cuslog->old_data_saves($cusdata,$list); }else{ $new=$cuslog->new_data_saves($cusdata,$extdata,$list); } } return $new; //return json(['code' => 0, 'data' => $new]); } //添加客户的回访记录 public function addVisitlog() { //$token = $this->request->token; $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; //防止后台切换部门 $token['org_id'] = Employee::where('id', $empid)->value('org_id'); $param = request()->only(['customer_id', 'name'=>'','type'=>'', 'next_contact_date', 'remark'=>'', 'img' => '', 'state', 'money' => 0, 'aid' => 0, 'addtime' => date('Y-m-d H:i:s'), 'confirm_date' => '', 'weixin_media' => '', 'media_id' => '', 'starts' => 0, 'weixin_media1' => '', 'media_id1' => '', 'img1' => '', 'number_of_visitors' => 0, 'measure_room_img_type' => '', 'package' => '', 'deposit_mode' => 0,'house_status'=>'','other_money'=>0,'house_delivery_time'=>'','deco_style'=>'','designer_id'=>'','vlogid'=>'','document_field' =>'','document_name' => '']); $param['confirm_date'] = $param['confirm_date'] ? $param['confirm_date'] . ' ' . date('H:i:s') : date('Y-m-d H:i:s'); $employeeTypeName = $this->request->empcrm['org_type'] == 2 ? '设计师' : '销售'; $employeeType = $this->request->empcrm['org_type'] == 2 ? 'designer_id' : 'employee_id'; if($is_manager ==1) $employeeTypeName = '部门领导'; //2022-10-04 逻辑修改,无效时无效原因必传remark //if ($param['state'] == '无效' && empty($param['remark'])) return json(['code' => 1, 'msg' => '请填写无效原因']); //2022-10-27 逻辑修改,跟进保护优化 如果后台设置了重复客户跟进保护规则 跟进到保护状态后其他业务员的此重复客户就不能在跟进了,但是可以置为无效 $forbiddenState = $this->forbiddenState($param['customer_id'],$param['state']); if($forbiddenState != false) return json(['code' => 1, 'msg' =>$forbiddenState]); 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]; } $where = [ ['id', '=', $param['customer_id']], ['state', '<>', '无效'] ]; $had = Customer::where($where)->where(function($query) use ($org_employee){ foreach($org_employee as $k=>$v){ $query->whereOr([['employee_id|designer_id|assigned_personnel', 'find in set', $v]]); } })->find(); if (!$had) return json(['code' => 1, 'msg' => '添加失败,数据不存在']); //签单之后不能置为无效 if ($param['state'] == '无效') { $is_orders = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count(); if ($is_orders) return json(['code' => 1, 'msg' => '该客户已签单,不能置为无效']); } //改为有效之后不能再改为待确认 if ($param['state'] == '待确认') { if ($had->state != '待确认') return json(['code' => 1, 'msg' => '该客户已标记为有效,不能修改为待确认']); } // 撞单机制 if ($param['state'] == '未到访') { // 同部门能否重复录入开关设置 $empcrm_repeat[] = ['root_id', '=', $root_id]; $empcrm_repeat[] = ['name', '=', 'empcrm_customer_repeat']; $repeat_setting = Setting::where($empcrm_repeat)->findOrEmpty(); //判断同部门 if (!$repeat_setting->isEmpty()) { $repeat_org = explode(',', $repeat_setting['content']); if (in_array($this->request->empcrm['org_id'], $repeat_org)) { // 需要验证 $not_sure = Customer::changeState('待确认', 'chaos'); $wuxiao = Customer::changeState('无效', 'chaos'); $phones_arr = array_filter([$had['phone'], $had['phone1'], $had['phone2']]); $repeat_where[] = ['phone|phone1|phone2', 'in', $phones_arr]; $repeat_where[] = ['org_id', 'in', $repeat_org]; $repeat_where[] = ['state', 'not in', array_merge($not_sure, $wuxiao)]; $customerExist = Customer::where($repeat_where)->field('id,employee_id'); if (!empty($customerExist)) { foreach ($customerExist as $ex) { if ($ex['employee_id'] != $empid && $ex['id'] != $param['customer_id']) { $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name'); return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法标记有效']); } } } } } } // 如果是预约添加预约记录 $stateType = [ 1 => '到店', 2 => '到场', 3 => '量房' ]; $yylf_state = CustomerVisitLog::changeState('预约量房', 'chaos'); $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); $yydc_state = CustomerVisitLog::changeState('预约活动', 'chaos'); $dc_state = CustomerVisitLog::changeState('已到场', 'chaos'); $yydd_state = CustomerVisitLog::changeState('预约到店', 'chaos'); $dd_state = CustomerVisitLog::changeState('已到店', 'chaos'); $check_state = array_merge($yylf_state, $lf_state, $yydc_state, $dc_state, $yydd_state, $dd_state); if (in_array($param['state'], $check_state)) { // 保护设置 $sub_setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'subscribe_protected']])->findOrEmpty(); if (!$sub_setting->isEmpty()) { // 有设置 $setting_content = json_decode($sub_setting['content'], true); $org_ids = orgSubIds($root_id); foreach ($setting_content as $k_s => $v_s){ if (!empty($v_s['state']) && !empty($v_s['day']) && $v_s['state'] == 1){ $continue = false; $sub_state = 0; $errmsg = '该客户在保护期内,无法提交'; $check_log = false; $check_where = []; switch ($k_s){ case 'liangfang': $sub_state = 3; $visit_state = 1; $check_log = true; $check_where[] = ['org_id', 'in', $org_ids]; $check_where[] = ['addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$v_s['day'])]; $check_where[] = ['employee_id', '<>', $empid]; $check_where[] = ['state', 'in', $lf_state]; break; case 'yliangfang': //预约量房 $sub_state = 3; $visit_state = 0; break; case 'daodian': $sub_state = 1; $visit_state = 1; $check_log = true; $check_where[] = ['org_id', 'in', $org_ids]; $check_where[] = ['addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$v_s['day'])]; $check_where[] = ['employee_id', '<>', $empid]; $check_where[] = ['state', 'in', $dd_state]; break; case 'ydaodian': $sub_state = 1; $visit_state = 0; break; case 'daochang': $sub_state = 2; $visit_state = 1; $check_log = true; $check_where[] = ['org_id', 'in', $org_ids]; $check_where[] = ['addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$v_s['day'])]; $check_where[] = ['employee_id', '<>', $empid]; $check_where[] = ['state', 'in', $dc_state]; break; case 'ydaochang': $sub_state = 2; $visit_state = 0; break; default: $continue = true; break; } if ($continue) continue; // “确认” 不受预约的限制 if (in_array($k_s, ['yliangfang', 'ydaodian', 'ydaochang']) && in_array($param['state'], array_merge($lf_state, $dd_state, $dc_state))) { continue; } if (in_array($param['state'], array_merge($lf_state, $dd_state, $dc_state))){ $errmsg = '该客户在保护期内,无法提交确认'; } elseif (in_array($param['state'], array_merge($yylf_state, $yydd_state, $yydc_state))){ $errmsg = '该客户在保护期内,无法预约'; } // 查询保护时间段内添加的预约量房记录 unset($v_where); $v_where[] = ['org_id', 'in', $org_ids]; $v_where[] = ['addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$v_s['day'])]; $v_where[] = ['employee_id', '<>', $empid]; $v_where[] = ['type', '=', $sub_state]; $v_where[] = ['state', '=', $visit_state]; $sub_list = CustomersSubscribe::where($v_where)->select()->toArray(); if (!empty($sub_list)){ $sub_list_ids = array_column($sub_list,'customer_id'); $sub_employee_ids = array_column($sub_list, 'employee_id'); $sub_customers = Customer::where([['id', 'in', $sub_list_ids], ['employee_id', 'in', $sub_employee_ids]])->select()->toArray(); foreach ($sub_customers as $v){ if (in_array($had->phone, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']){ return json(['code' => 1, 'msg' => $errmsg]); } if (!empty($had->phone1) && in_array($had->phone1, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']){ return json(['code' => 1, 'msg' => $errmsg]); } if (!empty($had->phone2) && in_array($had->phone2, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']){ return json(['code' => 1, 'msg' => $errmsg]); } } } if ($check_log) { $log_list = CustomerVisitLog::where($check_where)->select()->toArray(); $log_list_ids = array_column($log_list,'customer_id'); $log_employee_ids = array_column($log_list, 'employee_id'); $log_customers = Customer::where([['id', 'in', $log_list_ids], ['employee_id', 'in', $log_employee_ids]])->select()->toArray(); foreach ($log_customers as $v){ if (in_array($had->phone, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']){ return json(['code' => 1, 'msg' => $errmsg]); } if (!empty($had->phone1) && in_array($had->phone1, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']){ return json(['code' => 1, 'msg' => $errmsg]); } if (!empty($had->phone2) && in_array($had->phone2, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']){ return json(['code' => 1, 'msg' => $errmsg]); } } } } } } } // 更改客户信息 $state = Customer::changeState($param['state']); $orders = false; //签单之后不能签单交定,可以跟进 if ($state == '已签单') { $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count(); if ($is) { return $this->updateMoney($param); // Db::rollback(); // return json(['code' => 1, 'msg' => '该客户' . $state]); } $orders = true; } elseif ($state == '已交定') { $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count(); if ($is) { Db::rollback(); return json(['code' => 1, 'msg' => '该客户已签单']); } $orders = true; } Db::startTrans(); //处理房屋状态 if(!empty($param['house_status'])){ $had->house_status = $param['house_status']; } //如果是分派的客户第一次跟进直接修改分派信息为空 if(!empty($had->transfer_info)){ $had->transfer_info = NULL; } //如果是第一次有效修改有效时间 if($state == '未到访' && $had->state == '待确认'){ $had->valid_time = !empty($param['confirm_date']) ? $param['confirm_date'] : null; } if (!empty($state)) { //if(!in_array($had->state,Customer::changeState('待确认','chaos'),true)){ $had->state = $state; //} } //客户名称 if (!empty($param['name'])) { $had->name = $param['name']; } //是否设置客户转介绍新客户 // if($param['introduce']){ // $had->introduce = 1; // } //设计师 if(!empty($param['designer_id']) && empty($had->designer_id)){ $limitNum = Setting::where(['root_id' => $root_id, 'name' => 'designerNum'])->value('content'); if ($limitNum != 0) { //获取当月时间跨度 $start_time = date('Y-m').'-01 00:00:00'; $end_time = date('Y-m-d',strtotime("$start_time +1 month -1 day")).' 23:59:59'; $whered[] = ['addtime', 'between', [strtotime($start_time),strtotime($end_time)]]; $whered[] = ['employee_id', '=', $param['designer_id']]; $hads = CustomerSharing::field('employee_id,count(customer_id) as num')->group('employee_id')->where($whered)->having("num >= $limitNum")->select(); if (!$hads->isEmpty()){ Db::rollback(); return json(['code' => 1, 'msg' => '指派的设计师已无指派名额,请刷新后再试']); } } $had->designer_id = $param['designer_id']; } $had->revisit_time = !empty($param['next_contact_date']) ? $param['next_contact_date'] . ' 00:00:00' : date('Y-m-d').' 00:00:00'; $had->last_contact_date = date('Y-m-d', time()); $had->updatetime = date('Y-m-d H:i:s', time()); if ($param['aid'] && $param['state'] == '预约活动') $had->aid = $param['aid']; $had->fresh = 0; // 交定产品 $dingMsg = ''; if ($had->state == '已交定') { empty($param['money']) ?: $had->deposit_money = $param['money']; if (!empty($param['package'])) { $package = CustomerPackage::where([['root_id', '=', $root_id], ['id', 'in', $param['package']]])->column('name', 'id'); if (!$package) return json(['code' => 1, 'msg' => '客户产品不存在']); $had->package_id = implode(',', array_keys($package)); $dingMsg = '交定金额:' . $param['money'] . '元,选择产品:' . implode(',', $package); } } if ($had->state == '已签单' && !empty($param['money'])) $had->signed_money = $param['money']; $had->save(); // 更新客户保护期 if ($param['state'] !== '无效' && !empty($state)) { $log_state = CustomerVisitLog::changeState($state, 'chaos'); $v_where[] = ['employee_id', '=', $empid]; $v_where[] = ['state', 'in', $log_state]; $v_where[] = ['customer_id', '=', $param['customer_id']]; if (!empty($had['employee_time'])) { $v_where[] = ['addtime', '>', $had['employee_time']]; } $v_count = CustomerVisitLog::where($v_where)->count(); //查看此状态的跟踪记录,如果有数据,说明此客户在此员工这里已经经历过此状态,不再更新保护期 if (!$v_count) { Customer::changeProtectedTo($had->id, $root_id); } } // 添加确定消息内容 $confirmMsg = ''; if (!empty($param['confirm_date'])) { $confirmMsg = ', 时间:' . $param['confirm_date']; } // 备注 $employeeName = Employee::where(['id' => $empid])->value('name'); $remark = $param['remark']; if (empty($remark)) { if (empty($state)) $remark = $employeeTypeName . $employeeName . '对客户进行了回访'; else $remark = $employeeTypeName . $employeeName . '更新客户状态为' . $state . $confirmMsg . ',' . $dingMsg; } $valid_status = NULL; //2022-10-04 逻辑修改,无效时增加无效原因 if ($param['state'] == '无效' && !empty($param['remark'])) { // $remark = $employeeTypeName . $employeeName . '更新客户状态为' . $state . $confirmMsg . ',原因是:' . $param['remark'] . $dingMsg; $valid_status = !empty($had->valid_time) ? 2 : 1; } //如果置为无效时,查询客户的有效状态 // 添加追踪记录 $visitLog = [ 'customer_id' => $had->id, 'type' => $param['type'], 'next_contact_date' => !empty($param['next_contact_date']) ? $param['next_contact_date'] : null, 'employee_id' => $empid, 'user_id' => $this->request->empcrm['uid'], 'remark' => $remark, 'state' => $param['state'], 'weixin_media' => $param['weixin_media'], 'media_id' => $param['media_id'], 'starts' => $param['starts'], 'money' => $param['money'], 'confirm_date' => $param['confirm_date'], 'org_id' => $this->request->empcrm['org_id'], 'aid' => $param['aid'] ?: $had->aid, 'weixin_media1' => $param['weixin_media1'], 'media_id1' => $param['media_id1'], 'customer_employee_id' => $had->employee_id, 'customer_org_id' => Employee::where('id', $had->employee_id)->value('org_id'), 'img' => $param['img'], 'img1' => $param['img1'], 'number_of_visitors' => $param['number_of_visitors'], 'measure_room_img_type' => $param['measure_room_img_type'], 'deposit_mode' => $param['deposit_mode'], 'document_field' => $param['document_field'], 'document_name' => $param['document_name'] //'other_money' => $param['other_money'], //'valid_status' => $valid_status ]; $returnvisit = ['customer_org_id'=>$visitLog['customer_org_id']]; //2022-10-28 增加交定和签单凭证 凭证为图片 $deliverySignVoucher = request()->only(['delivery_media_id'=>'','delivery_weixin_media'=>'','sign_media_id'=>'','sign_weixin_media'=>'','delivery_img'=>'','sign_img'=>'']); $visitLog = $visitLog + $deliverySignVoucher; empty($param['img']) ?: $visitLog['img'] = $param['img']; if (!empty($param['aid'])) { // 检测活动是否存在 $existActivity = Activity::where([['id', '=', $param['aid']], ['root_id', '=', $root_id]])->count(); if ($existActivity == 0) { Db::rollback(); return json(['code' => 1, 'msg' => '活动不存在']); } $visitLog['aid'] = $param['aid']; } $visitLog = CustomerVisitLog::create($visitLog); if ($orders) $this->preformance_tasks($state, $param['customer_id'], $param['money'], $visitLog->id); //计算业绩 if (strpos($visitLog->state, '预约') !== false) { $state = str_replace('预约', '', $visitLog->state); // 查找是否已经预约 if($is_manager ==1){ $hadSubscribe = CustomersSubscribe::where([ ['customer_id', '=', $had['id']], //['employee_id|designer_id', '=', $empid], ['subscribe_date', '=', date('Y-m-d', strtotime($param['confirm_date']))], ['state', '=', 0] ])->count(); $typeSubscribe = CustomersSubscribe::where([ ['customer_id', '=', $had['id']], //['employee_id|designer_id', '=', $empid], ['type', '=', array_search($state, $stateType)], ['state', '=', 0] ])->find(); }else{ $hadSubscribe = CustomersSubscribe::where([ ['customer_id', '=', $had['id']], ['employee_id|designer_id', '=', $empid], ['subscribe_date', '=', date('Y-m-d', strtotime($param['confirm_date']))], ['state', '=', 0] ])->count(); $typeSubscribe = CustomersSubscribe::where([ ['customer_id', '=', $had['id']], ['employee_id|designer_id', '=', $empid], ['type', '=', array_search($state, $stateType)], ['state', '=', 0] ])->find(); } $typeSubscribe = false; // 不是同一天的预约,同类型还可以预约 if ($hadSubscribe) { Db::rollback(); return json(['code' => 1, 'msg' => '该客户在当前日期已有预约']); } elseif ($typeSubscribe) { Db::rollback(); return json(['code' => 1, 'msg' => '该客户已有' . $state . '预约, 预约日期' . $typeSubscribe->subscribe_date]); } else { $subscribe = [ 'customer_id' => $had['id'], 'subscribe_date' => $param['confirm_date'], 'employee_id' => $had->employee_id, 'designer_id' => $had->designer_id, 'type' => $state, 'aid' => $param['aid'] ?: $had->aid, 'org_id' => $had->org_id ]; CustomersSubscribe::create($subscribe); } } //当设为无效时,记录当前操作人跟进的最后一次状态,此处在公海列表处展示 if ($param['state'] == '无效') { $last_cvl = CustomerVisitLog::where('customer_id', $param['customer_id'])->with('employee')->order('addtime desc')->find(); //获取"无效"并进入公海之前的那次跟进记录的信息 if ($last_cvl) { $before_pool = [ 'name' => $last_cvl->employee->name, 'state' => $last_cvl->state, 'addtime' => $last_cvl->addtime ]; } else { $before_pool = [ 'name' => Employee::find($had['employee_id'])->value('name'), 'state' => '待确认', 'addtime' => $had->addtime ]; } // 该客户预约中的状态置为无效 CustomersSubscribe::where([['customer_id', '=', $param['customer_id']], ['state', '=', 0]])->save(['state'=> -1]); //公海内是否存在该客户信息,存在则合并客户信息(phone、phone1、phone2) $orgids = orgSubIds($root_id); $phone_arr = []; !empty(trim($had['phone'])) ? $phone_arr[] = cypherphone(trim($had['phone'])) : '' ; !empty(trim($had['phone1'])) ? $phone_arr[] = cypherphone(trim($had['phone1'])) : '' ; !empty(trim($had['phone2'])) ? $phone_arr[] = cypherphone(trim($had['phone2'])) : '' ; $phone_arr = array_filter($phone_arr); $pool_condition = [ ['employee_id', '=', NULL], ['is_resource', '=', 0], ['org_id', 'in', $orgids], ['phone|phone1|phone2','in',$phone_arr] ]; $pool_crm = Customer::where($pool_condition)->column('id'); if(!empty($pool_crm)){ //公海存在则删除 Customer::where([['id', 'in', $pool_crm]])->update(['delete_time'=>time()]); //关联回访记录修改 CustomerVisitLog::where([['customer_id', 'in', $pool_crm]])->update(['is_merge'=>1, 'customer_id'=>$had['id']]); CustomersSubscribe::where([['customer_id', 'in', $pool_crm]])->update(['is_merge'=>1, 'customer_id'=>$had['id']]); } //增加无效记录 $vlglist = CustomerVisitLog::where([['customer_id','=',$param['customer_id']],['customer_employee_id','=',$empid],['state','not in',array_merge(CustomerVisitLog::changeState('待确认','chaos'),CustomerVisitLog::changeState('无效','chaos'))],['save_portrait_field','NULL',null]])->count(); $invalidlog = [ 'customer_id'=>$param['customer_id'], 'employee_id'=>$had->employee_id, 'designer_id'=>$had->designer_id ? $had->designer_id : 0, 'org_id' => $had->org_id, 'root_id' => $request->empcrm->root_id, 'cus_addtime' =>$had->addtime, 'visitlog_id' =>$visitLog->id, 'assigned_personnel' => $had->assigned_personnel ? $had->assigned_personnel : '', 'source_id' => $had->source_id ? $had->source_id : 0, 'status' => !empty($vlglist) ? 1 : 2, 'cus_employee_time'=> $had->employee_time, 'is_resource'=> $had->is_resource ]; CustomerInvalidLog::create($invalidlog); Customer::where('id', $param['customer_id'])->update(['state' => 7, 'employee_id' => NULL, 'org_id' => $request->empcrm->org_id, 'is_resource' => 0, 'before_pool' => json_encode($before_pool), 'designer_id' => NULL, 'employee_time' => NULL]); // -------------------- 企业微信逻辑相关 start ----------------- // 判断是否绑定有企业微信外部联系人关系,并做解绑操作 $company_id = Company::where('root_id', '=', $root_id)->value('id'); $company_setting = WeworksingleCompanySetting::where('company_id', '=', $company_id)->find(); if (!empty($company_setting)) { $w_where['company_id'] = $company_id; $w_where['employee_id'] = $empid; $w_where['customer_id'] = $param['customer_id']; WeworksingleCustomer::where($w_where)->save(['customer_id' => 0]); } // -------------------- 企业微信逻辑相关 end ----------------- } // //如果签单,默认交定 if (strpos($param['state'], "签单") !== false) { trace(strpos($param['state'], "签单")); $lw[] = CustomerVisitLog::changeState(['state', '=', '交定']); $lw[] = ['customer_id', '=', $param['customer_id']]; $check = CustomerVisitLog::where($lw)->find(); if (!$check) { $l_visitlog = $visitLog->toArray(); unset($l_visitlog['id']); $l_visitlog['state'] = '交定'; $l_visitlog['remark'] = !empty($param['remark']) ? $param['remark'] : $employeeTypeName . $employeeName . '变更客户状态为交定'; $new_visitlog = array_merge($param, $l_visitlog); $new_visitlog['money'] = 0; //直接签单默认交定金额为0 $mr = CustomerVisitLog::create($new_visitlog); $this->preformance_tasks('已交定', $param['customer_id'], $param['money'], $mr->id); //计算业绩 } } if (in_array($param['state'], ['已到店', '已量房', '已到场'])) { $state = str_replace('已', '', $param['state']); $subscribe = CustomersSubscribe::where([['customer_id', '=', $had['id']], ['type', '=', array_search($state, $stateType)], ['state', '=', 0]])->find(); if (!empty($subscribe)) { $subscribe->state = 1; $subscribe->save(); } elseif ($param['aid'] && in_array($param['state'], ['已到场'])) { //没有预约可以直接确认到场 $this->activityState($param, $had); } } if (in_array($param['state'], ['未到店', '未量房', '未到场'])) { $state = str_replace('未', '', $param['state']); $subscribe = CustomersSubscribe::where([['customer_id', '=', $had['id']], ['type', '=', array_search($state, $stateType)], ['state', '=', 0]])->find(); if (!empty($subscribe)) { $subscribe->state = -1; $subscribe->save(); } $data = [ 'eid' => $had->employee_id, 'org_id' => $returnvisit['customer_org_id'], 'designer_id' => $had->designer_id, 'confirm_date' => $param['confirm_date'], 'customer_id' => $had->id, 'return_visit' => $had->return_visit, 'name' => $had->name, 'state' => $param['state'] ]; $this->cancelReservation($data); //取消预约后,待回访状态也要取消 $had->return_visit = 0; $had->save(); } //增加经纪人送积分功能 if ($had->agents_id && in_array($param['state'], ['已到店', '已交定', '已签单'])) { $this->agents_integral($had->agents_id, $param['state'], $had->id); } // 微爆活动 if (in_array($param['state'], ['已到店', '已量房', '已到场', '已交定', '已签单'])) { $activity_data['customer_id'] = $param['customer_id']; $activity_data['employee_id'] = $had->employee_id; $activity_data['org_id'] = $had->org_id; switch ($param['state']) { case '已到店': $activity_data['type'] = 2; break; case '已量房': $activity_data['type'] = 3; break; case '已交定': $activity_data['type'] = 4; break; case '已签单': $activity_data['type'] = 5; break; case '已到场': $activity_data['type'] = 7; break; default: break; } WechatActivityIntegral::addIntegral($activity_data, $request->empcrm->root_id); } Db::commit(); $from_wework = input('from_wework', '', 'trim'); if ($from_wework) { Console::call('download', ['customer', 'wework', (string)$root_id]); Console::call('download', ['customer1', 'wework', (string)$root_id]); } else { Console::call('download', ['customer']); Console::call('download', ['customer1']); } //2022-10-28 更新交定/签单凭证 if(in_array($param['state'],['已交定','已签单'])){ $deliveryType = $param['state']=='已交定' ? 'delivery' : 'sign'; Console::call('download', [$deliveryType]); } Console::call('medal', ['visit_log', (string)$empid, (string)$root_id]); //勋章 if($is_manager == 1){ //经理回访后修改客户的待回访的状态 Customer::where('id', $had->id)->update(['return_visit' => 0]); }else{ //给业务经理和设计是发送提醒消息回访 $data = ['phone' => $had->phone, 'org_id' => $returnvisit['customer_org_id'], 'state' => $param['state'], 'confirm_date' => $param['confirm_date'], 'name' => $had->name, 'designer_id' => $had->designer_id, 'eid' => $had->employee_id, 'customer_id' => $had->id]; } //$this->returnVisit($data); $data['name'] = !empty($data['name']) ? $data['name'] : ''; if (in_array($param['state'], $dd_state)) { // 发送管理层模板消息 $msg = ['first'=>'行为通知', 'keyword1'=>'客户确认到店', 'keyword2'=>"客户".$data['name']."(".$had['phone'].")确认到店", 'keyword3'=>date('Y/m/d'), 'remark'=>'请悉知']; //TmpMsg::sendMsgToLeader($msg, $token['org_id']); } if ($state == '已签单') { // 发送管理层模板消息 $msg = ['first'=>'行为通知', 'keyword1'=>'客户签单', 'keyword2'=>"客户".$data['name']."(".$had['phone'].")签单", 'keyword3'=>date('Y/m/d'), 'remark'=>'请悉知']; //TmpMsg::sendMsgToLeader($msg, $token['org_id']); } return json(['code' => 0, 'msg' => '跟进成功']); } /** * 2022-10-27 逻辑修改,跟进保护优化 如果后台设置了重复客户跟进保护规则 跟进到保护状态后其他客户的此重复客户就不能在跟进了,但是可以置为无效 */ private function forbiddenState($id,$status){ if($status == '无效') return false; //$token = $this->request->token; $root_id = $this->request->empcrm->root_id; //保护规则 $provite = Setting::where([['name','=','forbiddenState'],['root_id','=',$root_id]])->value('content'); if(!$provite) return false; $info = Customer::where('id',$id)->column('phone,phone1,phone2'); //查询是否是重复客户 $orgs = Org::where([['path','like',$root_id.'-%']])->column('id'); $query[] = ['employee_id','>',0]; $query[] = ['org_id','in',$orgs]; $query[] = ['phone|phone1|phone2','in',array_filter($info[0])]; // $query[] = ['employee_id','<>',$token['employee_id']]; $check = Customer::where($query)->column('id,employee_id'); if(count($check) <= 1) return false; $provite = explode(',',$provite); $state = []; foreach ($provite as $v) { $state = array_merge($state,CustomerVisitLog::changeState($v, 'chaos')); } //查询该客户是否存在受保护的状态 只有第一个跟进的业务员可以继续跟进 $where[] = ['state','in',$state]; $where[] = ['customer_id','in',array_column($check,'id')]; $where[] = ['employee_id|customer_employee_id','in',array_column($check,'employee_id')]; $repeat = CustomerVisitLog::where($where)->order('id asc')->field('customer_id,state')->findOrEmpty(); if($repeat->isEmpty() || (int)$repeat->customer_id==(int)$id) return false; //该客户已被其他员工 确认到店/确认量房/确认到场/交定/签单 无法更近 $state = str_replace('已','',$repeat->state); $state = in_array(trim($state),['交定','签单']) ? trim($state) : '确认'.trim($state); return '该客户已被其他员工“'.$state.'”无法跟进'; } /** * 经纪人送积分功能 */ public function agents_integral($agentid, $state, $customer_id) { //$token = $this->request->token; $root_id = $this->request->empcrm->root_id; $agent_data = AgentUser::where('id', $agentid)->field('agent_name,type')->find(); if ($state == '已到店') { $code = 'daodian_integral'; $sta_type = 1; } if ($state == '已交定') { $code = 'jiaoding_integral'; $sta_type = 2; } if ($state == '已签单') { $code = 'qiandan_integral'; $sta_type = 3; } //添加与经纪人相关记录 $you = AgentCustomerLog::where([['agent_id', '=', $agentid], ['type', '=', $sta_type], ['customer_id', '=', $customer_id]])->count(); if (empty($you)) { $add = array( 'agent_id' => $agentid, 'type' => $sta_type, 'typename' => $state, 'addtime' => time(), 'customer_id' => $customer_id, 'agent_type' => $agent_data['type'], 'status' => 1 ); AgentCustomerLog::insert($add); } if ($agent_data['type'] == 2) return; //如果是网红经纪人直接返回 $jifen = CreditsSetting::where(['code' => $code, 'root_id' => $root_id])->value('value'); $ye = AgentIntegral::where(['customer_id' => $customer_id, 'agent_id' => $agentid])->column('state'); if (!in_array($state, $ye) && $agent_data['type'] == 1) { $add = array( 'agent_id' => $agentid, 'type' => 1, 'integral' => $jifen ? $jifen : 1, 'addtime' => time(), 'state' => $state, 'customer_id' => $customer_id ); AgentIntegral::insert($add); } if ($state == '已交定' && !in_array('已到店', $ye)) { $jifen = CreditsSetting::where(['code' => 'daodian_integral', 'root_id' => $root_id])->value('value'); $add = array( 'agent_id' => $agentid, 'type' => 1, 'integral' => $jifen ? $jifen : 1, 'addtime' => time(), 'state' => '已到店', 'customer_id' => $customer_id ); AgentIntegral::insert($add); } if ($state == '已签单' && !in_array('已到店', $ye)) { $jifen = CreditsSetting::where(['code' => 'daodian_integral', 'root_id' => $root_id])->value('value'); $add = array( 'agent_id' => $agentid, 'type' => 1, 'integral' => $jifen ? $jifen : 1, 'addtime' => time(), 'state' => '已到店', 'customer_id' => $customer_id ); AgentIntegral::insert($add); } if ($state == '已签单' && !in_array('已交定', $ye)) { $jifen = CreditsSetting::where(['code' => 'jiaoding_integral', 'root_id' => $root_id])->value('value'); $add = array( 'agent_id' => $agentid, 'type' => 1, 'integral' => $jifen ? $jifen : 1, 'addtime' => time(), 'state' => '已交定', 'customer_id' => $customer_id ); AgentIntegral::insert($add); } } /** * 取消预约 给部门管理员和设计师发送消息提醒 * 张三取消了客户李四2020年8月1日的量房/到店/到场预约 */ public function cancelReservation($data) { $token = $this->get_token(); if ($data['return_visit'] == 1) { $token['name'] = Employee::where([['root_id', '=', $token['root_org']],['id', '=', $data['eid']]])->value('name'); $eids = Employee::where([['root_id', '=', $token['root_org']], ['org_id', '=', $data['org_id']], ['state', '=', '在职'], ['is_manager', '=', 1], ['uid', '>', 0]])->column('id'); if ($data['designer_id']) array_push($eids, $data['designer_id']); $date = date('Y-m-d', strtotime($data['confirm_date'])); $date = explode('-', $date); $state = str_replace('未', '', $data['state']); $msg = $token['name'] . '取消了客户' . $data['name'] . $date[0] . '年' . $date[1] . '月' . $date[2] . '日的' . $state; $eids = array_unique($eids); foreach ($eids as $key => $val) { event(new Msg($val, $msg, 'returnVisit', $data['customer_id'])); } return true; } return false; } /** * 没有预约活动,跟进时传了aid和state='预约活动' * 没有预约可以直接确认到场 * 补全数据添加预约和的追踪记录 */ public function activityState($param, $had) { $token = $this->get_token(); $employeeTypeName = $token['org_type'] == 2 ? '设计师' : '销售'; // 备注 $employeeName = Employee::where(['id' => $token['employee_id']])->value('name'); $remark = $param['remark']; if (empty($remark)) { $remark = $employeeTypeName . $employeeName . '对客户进行了回访'; } //预约记录 $visitLog = [ 'customer_id' => $had->id, 'type' => $param['type'], 'next_contact_date' => !empty($param['next_contact_date']) ? $param['next_contact_date'] : date('Y-m-d'), 'employee_id' => $token['employee_id'], 'user_id' => $token['uid'], 'remark' => $remark, 'state' => 6, 'weixin_media' => '', 'media_id' => '', 'starts' => $param['starts'], 'money' => $param['money'], 'confirm_date' => $param['confirm_date'], 'org_id' => $token['org_id'], 'aid' => $param['aid'], 'weixin_media1' => '', 'media_id1' => '', 'customer_employee_id' => $had->employee_id, 'customer_org_id' => Employee::where('id', $had->employee_id)->value('org_id') ]; CustomerVisitLog::create($visitLog); // $subscribe = [ 'customer_id' => $had['id'], 'subscribe_date' => $param['confirm_date'], 'employee_id' => $had->employee_id, 'designer_id' => $had->designer_id, 'type' => 2, 'aid' => $param['aid'], 'org_id' => $visitLog['customer_org_id'], 'state' => 1 ]; CustomersSubscribe::create($subscribe); return 1; } //转换token public function get_token() { $token = [ 'uid'=>$this->request->empcrm->uid, 'employee_id'=>$this->request->empcrm->id, 'org_id'=>$this->request->empcrm->org_id, 'root_org'=>$this->request->empcrm->root_id, 'name'=>$this->request->empcrm->name, ]; return $token; } /** * 计算业绩任务 */ public function preformance_tasks($state, $customer_id, $money = 0, $vislog_id) { $token = $this->get_token(); //防止后台切换部门 $token['org_id'] = Employee::where('id', $token['employee_id'])->value('org_id'); if ($state == '已交定') { $w2[] = ['customer_id', '=', $customer_id]; $w2[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]; $w2[] = ['customer_employee_id', '=', $token['employee_id']]; $w2[] = ['customer_org_id', '=', $token['org_id']]; //切换部门重复交定仍然算业绩 $check = CustomerVisitLog::where($w2)->count(); //没有交定过计算业绩 if ($check == 1) { $time = date('Y-m-d H:i:s'); //查询进行中的指派到所属部门的所有进度 // $w[] = ['org_id','=',$token['org_id']]; $w[] = ['root_id', '=', $token['root_org']]; $w[] = ['start_date', '<=', $time]; $w[] = ['end_date', '>=', $time]; $w[] = ['is_deposit', '=', 1]; $preformance_tasks = PreformanceTasksModel::where($w)->column('id'); $w1[] = ['root_id', '=', $token['root_org']]; $w1[] = ['org_id', '=', $token['org_id']]; $w1[] = ['performance_tasks_id', 'in', $preformance_tasks]; $w1[] = ['is_deposit', '=', 1]; //指派到自己部门 $pid = PreformanceTasksOrg::where($w1)->group('performance_tasks_id')->column('*'); $w4[] = ['id', 'in', array_column($pid, 'performance_tasks_id')]; $model = PreformanceTasksModel::where($w4)->column('*'); foreach ($model as $k => $v) { $u = []; $u['ok_deposit'] = $v['ok_deposit'] + 1; $u['customer_visit_log_id'] = $v['customer_visit_log_id'] ? $v['customer_visit_log_id'] . ',' . $vislog_id : $vislog_id; PreformanceTasksModel::where('id', $v['id'])->update($u); unset($u); } PreformanceTasksOrg::where($w1)->inc('ok_deposit')->update(); } } elseif ($state == '已签单') { $w2[] = ['customer_id', '=', $customer_id]; $w2[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')]; $w2[] = ['customer_employee_id', '=', $token['employee_id']]; $check = CustomerVisitLog::where($w2)->count(); //没有交定过计算业绩 if ($check == 1) { $time = date('Y-m-d H:i:s'); //查询进行中的指派到所属部门的所有进度 // $w[] = ['org_id','=',$token['org_id']]; $w[] = ['root_id', '=', $token['root_org']]; $w[] = ['start_date', '<=', $time]; $w[] = ['end_date', '>=', $time]; $w[] = ['is_money', '=', 1]; $preformance_tasks = PreformanceTasksModel::where($w)->column('id'); $w1[] = ['root_id', '=', $token['root_org']]; $w1[] = ['org_id', '=', $token['org_id']]; $w1[] = ['performance_tasks_id', 'in', $preformance_tasks]; $w1[] = ['is_money', '=', 1]; //指派到自己部门 $pid = PreformanceTasksOrg::where($w1)->group('performance_tasks_id')->column('*'); $w4[] = ['id', 'in', array_column($pid, 'performance_tasks_id')]; $model = PreformanceTasksModel::where($w4)->column('*'); foreach ($model as $k => $v) { $u = []; $u['ok_money'] = $v['ok_money'] + $money; $u['customer_visit_log_id'] = $v['customer_visit_log_id'] ? $v['customer_visit_log_id'] . ',' . $vislog_id : $vislog_id; PreformanceTasksModel::where('id', $v['id'])->update($u); unset($u); } PreformanceTasksOrg::where($w1)->inc('ok_money', (int)$money)->update(); } } return 1; } /** * 2022-11-11 业务修改 签单之后可以编辑签单金额,跟进记录增加 业务员修改签单金额为3000元 */ private function updateMoney($param) { $request = request(); $token = $this->get_token(); $money = $param['money'] ?: 0; $orgids = orgSubIds($token['root_org']); $where[] = ['id', '=', $param['customer_id']]; $where[] = ['org_id','in',$orgids]; $cusdata = Customer::where($where)->find(); $cusdata->signed_money = $money; $cusdata->save(); //Customer::where($where)->update(['signed_money' => $money]); if(isset($param['vlogid'])&&$param['vlogid'])CustomerVisitLog::where('id',$param['vlogid'])->update(['money'=>$money]); $token['name'] = Employee::where([['id', '=', $token['employee_id']]])->value('name'); $time = date('Y-m-d H:i:s'); //增加跟进记录 $save = [ 'customer_id' => $param['customer_id'], 'type' => '', 'next_contact_date' => date('Y-m-d'), 'remark' => '业务员' . $token['name'] . '修改已签单金额为' . $money . '元', 'addtime' => $time, 'employee_id' => $token['employee_id'], 'user_id' => $token['uid'], // 'state' => 3, 'state' => 1, 'org_id' => $token['org_id'], 'customer_employee_id' => $cusdata['employee_id'], 'customer_org_id' => $token['org_id'], 'money' => $money ]; //2022-10-28 增加交定和签单凭证 凭证为图片 //$deliverySignVoucher = request()->only(['sign_media_id' => '', 'sign_weixin_media' => '', 'sign_img' => '']); //$save = $save + $deliverySignVoucher; CustomerVisitLog::insertGetId($save); //if ($deliverySignVoucher['sign_media_id']) Console::call('download', ['sign']); $msg = '转单金额修改成功.'; if($request->empcrm['xinjushang'] == 1) $msg = '签单金额修改成功.'; return json(['code' => 0, 'msg' => $msg, 'data' => $msg]); } /** * 快速状态跟进 */ public function kb(){ $request = request(); $empcrm = $request->empcrm; $employeeTypeName = $empcrm->is_manager ==1 ? '部门领导' : ($empcrm->org_type == 2 ? '设计师' : '销售'); // 请求参数设置 $stateParam = [ 3 => ['money', 'confirm_date'], 2 => ['money', 'confirm_date'], 7 => ['confirm_date'], 8 => ['confirm_date'] ]; $param = $request->only(['customer_id', 'state', 'money','add_wechat_time','vlogid']); //2022-10-27 逻辑修改,跟进保护优化 如果后台设置了重复客户跟进保护规则 跟进到保护状态后其他业务员的此重复客户就不能在跟进了,但是可以置为无效 $forbiddenState = $this->forbiddenState($param['customer_id'],null); if($forbiddenState != false) return json(['code' => 1, 'msg' =>$forbiddenState]); // 数据查找 $where = [ ['id', '=', $param['customer_id']], ['state', '<>', '无效'] ]; if($empcrm->is_manager ==1){ $team_orgs = orgSubIds($request->empcrm->org_id); $where[] = ['org_id', 'in', $team_orgs]; }else{ $where[] = ['employee_id|designer_id|assigned_personnel', 'find in set', $empcrm->id]; } $had = Customer::where($where)->find(); if ($had === null) return json(['code' => 1, 'msg' => '添加失败,数据不存在']); if(!empty($param['add_wechat_time'])) { $ext = $had->ext; $ext = json_decode($ext,true); $ei = false; foreach($ext as &$e){ if($e['keyname'] == 'add_wechat_time') { $e['value'] = date('Y-m-d', strtotime($param['add_wechat_time'])); $ei = true; } } if ($ei === false) { $fid = CustomerPortraitField::where(['root_id' => $empcrm->root_id, 'keyname' => 'add_wechat_time'])->value('id'); if($fid) $ext[] = ['id' => $fid, 'keyname' => 'add_wechat_time', 'value' => date('Y-m-d', strtotime($param['add_wechat_time']))]; } $had->ext = json_encode($ext); } if(empty($param['state']) || !in_array($param['state'], [2,3,7,8])) { $had->save(); return json(['code'=>0, 'msg'=>'保存成功']); } else { $customerVisitLogState = CustomerVisitLog::changeState($param['state'], 'n'); $stateStr = CustomerVisitLog::changeState($param['state']); $customerState = Customer::changeState($stateStr, 'n'); $stateParamValue = $request->only($stateParam[$customerVisitLogState]); } // 量房到店保护机制 $protectedDay = 0; if (7 == $customerVisitLogState || 8 == $customerVisitLogState) { // 保护设置 $sub_setting = Setting::where([['root_id', '=', $empcrm->root_id], ['name', '=', 'subscribe_protected']])->findOrEmpty(); if (!$sub_setting->isEmpty()) { // 有设置 $setting_content = json_decode($sub_setting['content'], true); // 判断保护是否开启 $bh = []; foreach ($setting_content as $k_s => $v_s) { if (!empty($v_s['state']) && !empty($v_s['day']) && $v_s['state'] == 1) { $bh[$k_s] = $v_s['day']; if (($k_s == 'ydaodian' && 7 == $customerVisitLogState) || ($k_s == 'yliangfang' && 8 == $customerVisitLogState)) $protectedDay = $v_s['day']; } } $phones = [$had->phone, $had->phone1, $had->phone2]; $phones = array_filter($phones); // 如果设置了保护 if (!empty($bh)) { foreach($bh as $k=>$d){ $condition = $this->protectCondition($k, $d); if(!empty($condition['log'])){ $e = CustomerVisitLog::withJoin('customer')->where([['Customer.id', '<>', $param['customer_id']]])->where($condition['log']) ->where(function($query) use ($phones){ foreach($phones as $p){ $c = [ [['customer.phone', '=', $p]], [['customer.phone1', '=', $p]], [['customer.phone2', '=', $p]] ]; $query->whereOr($c); } })->find(); if($e) return json(['code'=>1, 'msg'=>'该客户在保护期内,无法提交']); } if(!empty($condition['subscribe'])){ $e = CustomersSubscribe::withJoin('customer')->where([['Customer.id', '<>', $param['customer_id']]])->where($condition['subscribe']) ->where(function($query) use ($phones){ foreach($phones as $p){ $c = [ [['customer.phone', '=', $p]], [['customer.phone1', '=', $p]], [['customer.phone2', '=', $p]] ]; $query->whereOr($c); } }) ->count(); if($e>0) return json(['code'=>1, 'msg'=>'该客户在保护期内,无法提交']); } } } } } // 更改客户信息 $orders = false; //签单之后不能签单交定,可以跟进 if (3 == $customerVisitLogState) { $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count(); if ($is) return $this->updateMoney($param); $orders = true; $had->signed_money = $stateParamValue['money']; } if (2 == $customerVisitLogState) { $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count(); if ($is) return json(['code' => 1, 'msg' => '该客户已签单']); $orders = true; $had->deposit_money = $stateParamValue['money']; } Db::startTrans(); //如果是分派的客户第一次跟进直接修改分派信息为空 if (!empty($had->transfer_info)) $had->transfer_info = NULL; //如果是第一次有效修改有效时间 if(in_array($param['state'], [2,3,7,8]) && $had->state == '待确认'){ $had->valid_time = !empty($stateParamValue['confirm_date']) ? $stateParamValue['confirm_date'] : date('Y-m-d H:i:s'); } $had->state = $customerState; $had->last_contact_date = date('Y-m-d', time()); $had->updatetime = date('Y-m-d H:i:s', time()); $had->fresh = 0; // 更新客户保护期限 if($protectedDay) $had->protected_to = date('Y-m-d', time()+$protectedDay * 86400); $had->save(); // 添加确定消息内容 $confirmMsg = ''; if (!empty($stateParamValue['confirm_date'])) { $confirmMsg = ', 时间:' . $stateParamValue['confirm_date']; } // 备注 $employeeName = Employee::where(['id' => $empcrm->id])->value('name'); $remark = ''; $remark = $employeeTypeName . $employeeName . '更新客户状态为' . $stateStr . $confirmMsg; $is_set = Setting::where([['root_id','=',$empcrm->root_id],['name','=','nextVisitTimeIsOk']])->value('content'); // 添加追踪记录 $visitLog = [ 'customer_id' => $had->id, 'type' => '', 'next_contact_date' => $is_set ? date('Y-m-d') : null, 'employee_id' => $empcrm->id, 'user_id' => $empcrm->uid, 'remark' => $remark, 'state' => $customerVisitLogState, 'money' => $stateParamValue['money'] ?? 0, 'confirm_date' => $stateParamValue['confirm_date'] ?? date('Y-m-d H:i:s'), 'org_id' => $empcrm['org_id'], 'customer_employee_id' => $had->employee_id, 'customer_org_id' => Employee::where('id', $had->employee_id)->value('org_id'), ]; $visitLog = CustomerVisitLog::create($visitLog); if ($orders) $this->preformance_tasks($stateStr, $param['customer_id'], $stateParamValue['money'], $visitLog->id); //计算业绩 // //如果签单,默认交定 // if ($customerVisitLogState == 3) { // $lw[] = CustomerVisitLog::changeState(['state', '=', '交定']); // $lw[] = ['customer_id', '=', $param['customer_id']]; // $check = CustomerVisitLog::where($lw)->find(); // if (!$check) { // $l_visitlog = $visitLog->toArray(); // unset($l_visitlog['id']); // $l_visitlog['state'] = '交定'; // $l_visitlog['remark'] = $employeeTypeName . $employeeName . '变更客户状态为交定'; // $new_visitlog = array_merge($param, $l_visitlog); // $new_visitlog['money'] = 0; //直接签单默认交定金额为0 // $mr = CustomerVisitLog::create($new_visitlog); // $this->preformance_tasks('已交定', $param['customer_id'], $stateParamValue['money'], $mr->id); //计算业绩 // } // } if ($customerVisitLogState == 7 || $customerVisitLogState == 8) { $type = $customerVisitLogState == 7 ? 1 : 3; $subscribe = CustomersSubscribe::where([['customer_id', '=', $had['id']], ['type', '=', $type], ['state', '=', 0]])->find(); if (!empty($subscribe)) { $subscribe->state = 1; $subscribe->save(); } } //增加经纪人送积分功能 if ($had->agents_id && in_array($customerVisitLogState, [2,3,7])) { $this->agents_integral($had->agents_id, $customerVisitLogState, $had->id); } // 微爆活动 if (in_array($param['state'], [2, 3, 7, 8])) { $activity_data['customer_id'] = $param['customer_id']; $activity_data['employee_id'] = $had->employee_id; $activity_data['org_id'] = $had->org_id; switch ($param['state']) { case 7: // 已到店 $activity_data['type'] = 2; break; case 8: // 已量房 $activity_data['type'] = 3; break; case 2: // 已交定 $activity_data['type'] = 4; break; case 3: // 已签单 $activity_data['type'] = 5; break; case 9: // 已到场 $activity_data['type'] = 7; break; default: break; } WechatActivityIntegral::addIntegral($activity_data, $empcrm['root_id']); } Db::commit(); Console::call('medal', ['visit_log', (string)$empcrm->id, (string)$empcrm->root_id]); //勋章 if($empcrm->is_manager == 1){ // 经理回访后修改客户的待回访的状态 Customer::where('id', $had->id)->update(['return_visit' => 0]); } // if ($param['state'] == 2) { // // 发送管理层模板消息 // $msg = ['first'=>'行为通知', 'keyword1'=>'客户确认到店', 'keyword2'=>"客户".$had['name']."(".$had['phone'].")确认到店", 'keyword3'=>date('Y/m/d'), 'remark'=>'请悉知']; // TmpMsg::sendMsgToLeader($msg, $empcrm['org_id']); // } $had['name'] = !empty($had['name']) ? $had['name'] : ''; if ($param['state'] == 3) { // 发送管理层模板消息 $msg = ['first'=>'行为通知', 'keyword1'=>'客户签单', 'keyword2'=>"客户".$had['name']."(".$had['phone'].")签单", 'keyword3'=>date('Y/m/d'), 'remark'=>'请悉知']; //TmpMsg::sendMsgToLeader($msg, $empcrm['org_id']); } if ($param['state'] == 7) { // 发送管理层模板消息 $msg = ['first'=>'行为通知', 'keyword1'=>'客户确认到店', 'keyword2'=>"客户".$had['name']."(".$had['phone'].")确认到店", 'keyword3'=>date('Y/m/d'), 'remark'=>'请悉知']; //TmpMsg::sendMsgToLeader($msg, $empcrm['org_id']); } return json(['code' => 0, 'msg' => '跟进成功']); } private function protectCondition($k_s, $day) { $empcrm = request()->empcrm; $org_ids = orgSubIds($empcrm->root_id); $check_where = []; $v_where = []; switch ($k_s){ case 'liangfang': $v_where[] = [ ['customers_subscribe.org_id', 'in', $org_ids], ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$day)], ['customers_subscribe.employee_id', '<>', $empcrm->id], ['customers_subscribe.type', '=', 3], ['customers_subscribe.state', '=', 1] ]; $check_where = [ ['customer_visit_log.org_id', 'in', $org_ids], ['customer_visit_log.addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$day)], ['customer_visit_log.employee_id', '<>', $empcrm->id], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')] ]; break; case 'yliangfang': //预约量房 $v_where[] = [ ['customers_subscribe.org_id', 'in', $org_ids], ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$day)], ['customers_subscribe.employee_id', '<>', $empcrm->id], ['customers_subscribe.type', '=', 3], ['customers_subscribe.state', '=', 0] ]; break; case 'daodian': $v_where[] = [ ['customers_subscribe.org_id', 'in', $org_ids], ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$day)], ['customers_subscribe.employee_id', '<>', $empcrm->id], ['customers_subscribe.type', '=', 1], ['customers_subscribe.state', '=', 1] ]; $check_where = [ ['customer_visit_log.org_id', 'in', $org_ids], ['customer_visit_log.addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$day)], ['customer_visit_log.employee_id', '<>', $empcrm->id], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')] ]; break; case 'ydaodian': $v_where[] = [ ['customers_subscribe.org_id', 'in', $org_ids], ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$day)], ['customers_subscribe.employee_id', '<>', $empcrm->id], ['customers_subscribe.type', '=', 1], ['customers_subscribe.state', '=', 0] ]; break; case 'daochang': $v_where[] = [ ['customers_subscribe.org_id', 'in', $org_ids], ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$day)], ['customers_subscribe.employee_id', '<>', $empcrm->id], ['customers_subscribe.type', '=', 2], ['customers_subscribe.state', '=', 1] ]; $check_where = [ ['customer_visit_log.org_id', 'in', $org_ids], ['customer_visit_log.addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$day)], ['customer_visit_log.employee_id', '<>', $empcrm->id], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到场', 'chaos')] ]; break; case 'ydaochang': $v_where[] = [ ['customers_subscribe.org_id', 'in', $org_ids], ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s',time() - 24*3600*$day)], ['customers_subscribe.employee_id', '<>', $empcrm->id], ['customers_subscribe.type', '=', 2], ['customers_subscribe.state', '=', 0] ]; break; } return ['log' => $check_where, 'subscribe' => $v_where]; } //获取客户所有扩展字段 public function get_all_portrait_field() { $root_id = request()->empcrm->root_id; $w[] = ['root_id', '=', $root_id]; $param = $this->request->only(['id']); $id = $param['id']; $AllOrgId = orgSubIds($root_id); $cusdata = Customer::where([['org_id', 'in', $AllOrgId], ['id', '=', $id]])->find(); if(empty($cusdata)) return json(['code' => 1,'msg'=>'客户不存在']); $list = CustomerPortraitField::where([['root_id', '=', $root_id], ['pid', '=', 0], ['status', '=', 0]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray(); $city = Company::where('root_id', $root_id)->value('city'); foreach ($list as $key => $val) { $child = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['pid', '<>', 0], ['pid', '=', $val['id']], ['status', '=', 0]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray(); foreach ($child as $k => $v) { if ($v['keyname'] == 'current_region') { $child[$k]['select'][] = ['id' => $root_id, 'name' => $city, 'pid' => $v['id']]; } if ($v['keyname'] == 'source_id') { $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray(); $child[$k]['select'] = $soudata; } if ($v['keyname'] == 'deco_style') { $decostyles = Decostyle::field('id,name')->where([['root_id', '=', $root_id], ['type', '=', 0]])->select()->toArray(); $child[$k]['select'] = $decostyles; } $child[$k]['value'] = ''; $child[$k]['valname'] = ''; } $list[$key]['child'] = $child; } $new = $list; $cuslog = new CustomerLogic; if (!empty($cusdata['ext']) && $cusdata['ext'] != 'null') { $extdata = json_decode($cusdata['ext'], true); if (isset($extdata['ext1'])) { $new = $cuslog->old_data_save($cusdata, $list); } else { $new = $cuslog->new_data_save($cusdata, $extdata, $list); } } return json(['code' => 0, 'data' => $new,'msg'=>'获取成功']); } /** * 设计师指定 * 2022-11-08 业务修改, 指派设计师改为指派人员 并且可以指派多人 * $id 客户id * $designerId 指派的人员 1,2,3,4 //为空取消指派人员 */ public function reserveDesigner() { $request = request(); $param = $this->request->only(['id','designerId','assign_type']); $id = $param['id']; $designerId = $param['designerId']; $root_id = $request->empcrm->root_id; if (isset($request->empcrm->is_manager) && $request->empcrm->is_manager) { $customer = Customer::where([['id', '=', $id], ['org_id', 'in', orgSubIds($request->empcrm['root_id'])]])->find(); } else { $customer = Customer::where(['id' => $id, 'employee_id' => $request->empcrm['id']])->find(); } if (empty($customer)) return json(['code' => 1, 'msg' => '客户不存在']); if (empty($designerId)) { $customer->designer_id = NULL; $customer->assigned_personnel = NULL; $customer->save(); return json(['code' => 0, 'msg' => '取消指派成功!']); } $designerId = explode(',', $designerId); $designer = Employee::where([ ['id', 'in', $designerId], ['root_id', '=', $root_id], ['state', '=', '在职'] ])->column('org_id', 'id'); if (empty($designer)) return json(['code' => 1, 'msg' => '指派人员不存在']); $orgs = Org::where([['id', 'in', array_values($designer)]])->column('org_type', 'id'); //第一个选择的设计师 $selectDesigner = []; foreach ($designerId as $k => $v) { if (empty($designer[$v]) || $orgs[$designer[$v]] !== 2) continue; $selectDesigner[] = $v; } $designer_id = isset($selectDesigner[0]) ? $selectDesigner[0] : null; // 检测是否有设计师已指派满了 $limitNum = Setting::where(['root_id' => $request->empcrm['root_org'], 'name' => 'designerNum'])->value('content'); if ($limitNum != 0) { //获取当月时间跨度 $start_time = date('Y-m').'-01 00:00:00'; $end_time = date('Y-m-d',strtotime("$start_time +1 month -1 day")).' 23:59:59'; $where[] = ['addtime', 'between', [strtotime($start_time),strtotime($end_time)]]; $where[] = ['employee_id', 'in', $selectDesigner]; $had = CustomerSharing::field('employee_id,count(customer_id) as num')->group('employee_id')->where($where)->having("num >= $limitNum")->select(); // var_dump($had->toArray()); // exit; if (!$had->isEmpty()) return json(['code' => 1, 'msg' => '指派的设计师已无指派名额,请刷新后再试']); } // 检测部门是否是设计师 // $orgType = Org::where(['id' => $designer->org_id])->value('org_type'); // if ($orgType != 2) return json(['code' => 1, 'msg' => '设计师不存在']); $customer->designer_id = $designer_id; $customer->assigned_personnel = implode(',', array_keys($designer)); $customer->assign_time = date('Y-m-d H:i:s'); if(!empty($param['assign_type']) && empty($customer->assign_type)) $customer->assign_type = $param['assign_type']; $customer->save(); // 更新预约未确定的客户所属设计师id CustomersSubscribe::where(['customer_id' => $customer->id, 'state' => 0])->update(['designer_id' => $designer_id]); $all_assign_type = ['yixiang'=>'意向客户指派','liangfang'=>'量房客户指派','daodian'=>'到店客户指派']; if(!empty($all_assign_type[$customer->assign_type])){ $empname_arr = Employee::where([['id','in',implode(',', array_keys($designer))],['root_id','=',$root_id]])->column('name'); $remark = $all_assign_type[$customer->assign_type] . '指派至员工' . implode(',', $empname_arr); $visit_Log = [ 'customer_id' => $id, 'type' => '', 'remark' => $remark, 'employee_id' => $request->empcrm->id, 'user_id' => $request->empcrm->uid, 'state' => 1, 'next_contact_date'=>date('Y-m-d'), 'customer_employee_id'=>$customer->employee_id, 'customer_org_id'=>$customer->org_id, 'org_id'=>$customer->org_id ]; CustomerVisitLog::create($visit_Log); } return json(['code' => 0, 'msg' => '指派成功!']); } /** * 客户报备 */ public function add_customer() { if (!request()->isAjax()) { return View::fetch(); } $root_id = request()->empcrm->root_id; $employee_id = request()->empcrm->id; $is_manager = request()->empcrm->is_manager; $org_id = request()->empcrm->org_id; $orgids = orgSubIds($root_id); //编辑客户id $id = $this->request->param('id'); if (empty($id)) { // 设计师能否报备、获取客户 $de_where[] = ['root_id', '=', $root_id]; $de_where[] = ['name', '=', 'designer_get_customer']; $info_de_where = Setting::where($de_where)->findOrEmpty(); if (!$info_de_where->isEmpty() && request()->empcrm->org_type == 2) { if ((int)$info_de_where['content'] != 1) { return json(['code' => 1, 'msg' => '您的身份为设计师,无法报备客户']); } } } //管理员进行编辑客户信息 if($is_manager ==1 && $id){ $employee_id = Customer::where([['id','=', $id],['org_id','in',$orgids]])->value('employee_id'); } $data = $this->request->only(['name', 'phone', 'sex', 'level', 'state' => '待确认', 'last_contact_date' => date('Y-m-d'), 'phone1', 'phone2', 'agents_id']); //扩展字段 $ext = $this->request->param('ext'); //去掉添加的备用手机号字段 foreach($ext as $key=>$val){ if(in_array($val['keyname'],['phone1','phone2'])){ unset($ext[$key]); } } $ext = array_values($ext); $orgids = orgSubIds($root_id); $pharr = [ $data['phone'], !empty($data['phone1']) ? $data['phone1'] : '', !empty($data['phone2']) ? $data['phone2'] : '' ]; $repeat=[]; foreach($pharr as $key=>$val){ if(empty($val)) continue; if(!in_array($val,$repeat)){ $repeat[] = $val; }else{ return json(['code' => 1, 'msg' => '手机号不能重复']); } } //判断手机号是否在公海,资源库,已建档,回收站 // 增加同部门能否重复录入判断 if (!$id) { $tips = $this->checkPepeat($pharr, $orgids, $employee_id); if (!empty($tips)) return $tips; } else { $tips = $this->checkPepeat($pharr, $orgids, $employee_id, $id); if (!empty($tips)) return $tips; } //验证接收的自定义字段是否开启必填 $get_field = [ isset($data['name']) ? 'name' : '', isset($data['phone']) ? 'phone' : '', isset($data['sex']) ? 'sex' : '', isset($data['level']) ? 'level' : '', ]; $get_field = array_filter($get_field); $get_field = array_merge($get_field, array_column($ext, 'keyname')); $all_field = CustomerPortraitField::where([['root_id', '=', $root_id], ['status', '=', 0], ['keyname', 'in', $get_field]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray(); $validate_data = $data; $house_type_value = ''; $live_broadcast_value = []; foreach($ext as $t) { $validate_data[$t['keyname']] = !empty($t['value']) ? $t['value'] : ''; if($t['keyname'] == 'house_type' && !empty($t['value'])){ $house_type_value = CustomerPortraitFieldSelect::where([['pid', '=', $t['id']], ['id', '=', $t['value']]])->value('name'); } if($t['keyname'] == 'live_broadcast' && !empty($t['value'])){ $live_broadcast_value = CustomerPortraitFieldSelect::where([['pid', '=', $t['id']], ['id', 'in', explode(',',$t['value'])]])->column('name'); } } foreach($all_field as $item) { if($house_type_value != '期房' && $item['keyname'] == 'house_delivery_time' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue; if(!in_array('1对1业务直播',$live_broadcast_value) && $item['keyname'] == 'live_broadcast_business' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue; if(!in_array('1对1设计直播',$live_broadcast_value) && $item['keyname'] == 'live_broadcast_design' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue; if(!in_array('一对多直播',$live_broadcast_value) && $item['keyname'] == 'live_broadcast_personnel' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue; if ($item['is_must'] == 0 && empty($validate_data[$item['keyname']])) { return json(['code' => 1, 'msg' => $item['name'].'不能为空']); } } //扩展字段保存 $add_customer_extfield = $this->add_customer_extfield($ext); $data = array_merge($data, $add_customer_extfield); $data['sex'] = isset($data['sex']) && !empty($data['sex']) ? $data['sex'] : 1;//数据库性别必须有默认值 $customer = new Customer(); if ($id) { unset($data['state']); unset($data['last_contact_date']); $customer = Customer::where(['id' => $id, 'employee_id|designer_id' => $employee_id])->find(); $old_customer = $customer; if (empty($customer)) return json(['code' => 1, 'msg' => '数据不存在']); //增加检测修改的扩展字段信息 if (!empty($customer['ext']) && $customer['ext'] != 'null') { $extdata = json_decode($customer['ext'], true); if (isset($extdata['ext1'])) { $new_ext = CustomerPortraitField::where([['root_id', '=', $root_id], ['pid', '<>', 0]])->select()->toArray(); $this->check_old_updatefield($customer, $ext,$new_ext); } else { $this->check_new_updatefield($customer, $ext); } } } else { $data['employee_id'] = $employee_id; $data['org_id'] = $org_id; } Db::startTrans(); try { $customer->save($data); // 更新客户保护期 if (!$id) { $customer->save(['employee_time' => date('Y-m-d H:i:s')]); //保存员工获取客户时间 Customer::changeProtectedTo($customer->id, $root_id); $this->check_not_updatefield($customer,$ext); } //建档后添加一条跟进记录 if (empty($id)) $this->addFirstCustoemrVisitLog($customer->id); Db::commit(); } catch (\Exception $e) { Db::rollback(); return json(['code' => 1, 'msg' => $e->getMessage()]); } // 添加微爆积分 if ($id) { // 老客户新加微 $new_customer = Customer::find($id); if (!empty($new_customer['add_wechat_time']) && empty($old_customer['add_wechat_time'])) { $i_data['add_wechat_time'] = $new_customer['add_wechat_time']; $i_data['customer_id'] = $id; $i_data['employee_id'] = $new_customer['employee_id']; $i_data['type'] = 1; $i_data['org_id'] = $new_customer['org_id']; WechatActivityIntegral::addIntegral($i_data, $root_id); } } else { $new_customer = Customer::find($customer->id); if (!empty($new_customer['add_wechat_time'])) { $i_data['add_wechat_time'] = $new_customer['add_wechat_time']; $i_data['customer_id'] = $customer->id; $i_data['employee_id'] = $new_customer['employee_id']; $i_data['type'] = 1; $i_data['org_id'] = $new_customer['org_id']; WechatActivityIntegral::addIntegral($i_data, $root_id); } } $customer['name'] = !empty($customer['name']) ? $customer['name'] : ''; // 发送管理层模板消息 $msg = ['first'=>'行为通知', 'keyword1'=>'客户信息编辑', 'keyword2'=>"员工". request()->empcrm->name . "对客户".$customer['name']."(".$customer['phone'].")信息编辑", 'keyword3'=>date('Y/m/d'), 'remark'=>'请悉知']; //TmpMsg::sendMsgToLeader($msg, request()->empcrm->org_id); return json(['code' => 0, 'data' => $customer->id, 'msg' => '添加成功']); } /** * 新建客户时添加一条跟进记录 * $id 客户id */ public function addFirstCustoemrVisitLog($id) { $token = request()->empcrm; $name = Employee::where('id',$token['id'])->value('name'); $cname = Customer::where('id',$id)->value('name'); // $type = $source ? '建档' : '报备'; $type = '报备'; $remark = '业务员'.$name.'对客户'.$cname.'进行'.$type; $save = [ 'customer_id' => $id, 'type' => '', 'next_contact_date' => date('Y-m-d'), 'remark' => $remark, 'addtime' => date('Y-m-d H:i:s'), 'employee_id' => $token['id'], 'user_id' => $token['uid'], 'state' => 1, 'org_id' => $token['org_id'], 'customer_employee_id' => $token['id'], 'customer_org_id' => $token['org_id'] ]; CustomerVisitLog::insertGetId($save); return true; } //新添加字段时的处理 public function check_not_updatefield($cusdata,$save_ext) { $yes_save = []; foreach($save_ext as $key=>$val){ if(!empty($val['value'])){ $yes_save[] = $val; } } if(!empty($yes_save)) $this->save_field_log($yes_save,$cusdata); } //处理修改字段的记录 public function save_field_log($yes_data,$cusdata) { $token = $this->get_token(); $save_ids = array_column($yes_data,'id'); $is_down = 0; $new_ext = CustomerPortraitField::with(['select'])->where([['root_id', '=', $token['root_org']], ['pid', '<>', 0],['id','in',$save_ids]])->select()->toArray(); $city = Company::where('root_id', $token['root_org'])->value('city'); foreach($new_ext as $key=>$val) { if ($val['keyname'] == 'current_region') { $new_ext[$key]['select'][] = ['id' => $token['root_org'], 'name' => $city, 'pid' => $val['id']]; } if ($val['keyname'] == 'source_id') { $new_ext[$key]['select'] = CustomerSource::field('id,source as name')->where('root_id', $token['root_org'])->select()->toArray(); } if ($val['keyname'] == 'deco_style') { $new_ext[$key]['select'] = Decostyle::field('id,name')->where([['root_id', '=', $token['root_org']], ['type', '=', 0]])->select()->toArray(); } } foreach($yes_data as $key=>$val){ $selname1 = null; $yes_data[$key]['valname'] = $val['value']; foreach($new_ext as $k=>$v){ if($v['id']==$val['id']){ if($v['type']==6) $is_down = 1; $yes_data[$key]['name'] = $v['name']; $yes_data[$key]['type'] = $v['type']; if (in_array($v['type'], [3, 4]) && !empty($val['value'])) { $sp = explode(',', $val['value']); foreach ($v['select'] as $w => $q) { if (in_array($q['id'], $sp)) { $selname1 .= $q['name'] . ','; } } $yes_data[$key]['valname'] = trim($selname1, ','); } } } } // 添加追踪记录 $visitLog = [ 'customer_id' => $cusdata->id, 'type' => '', 'next_contact_date' => null, 'employee_id' => $token['employee_id'], 'user_id' => $token['uid'], 'remark' => '修改客户扩展信息', 'state' => 1, 'org_id' => $token['org_id'], 'customer_employee_id' => $cusdata->employee_id, 'customer_org_id' => Employee::where('id', $cusdata->employee_id)->value('org_id'), 'save_portrait_field' =>json_encode($yes_data), 'down_portrait_field_status' => 0 ]; // var_dump($visitLog); // exit; CustomerVisitLog::create($visitLog); } //查询修改值的字段 public function check_new_updatefield($cusdata,$save_ext) { $old_data = json_decode($cusdata['ext'],true); $yes_save = []; foreach ($old_data as $k => $v) { foreach ($save_ext as $kk => $vv) { if ($v['id'] == $vv['id']) { //判断是否修改了数据 if ($v['value'] != $vv['value']) { $yes_save[] = $vv; } unset($save_ext[$kk]); } } } //去除为空值的新字段 foreach($save_ext as $key=>$val){ if(empty($val['value'])){ unset($save_ext[$key]); } } $save_ext = array_values($save_ext); $yes_data = $yes_save; if (!empty($save_ext)){ $yes_data = array_merge($yes_save,$save_ext); } // var_dump($yes_data); // exit; if(!empty($yes_data)) $this->save_field_log($yes_data,$cusdata); } //老数据查询修改值的字段 public function check_old_updatefield($cusdata,$save_ext,$new_ext) { $old_ext = $this->get_old_ext(); $ext = json_decode($cusdata['ext'],true); // 数据的旧ext处理 $xin = []; foreach ($ext as $key => $val) { foreach ($old_ext as $k => $v) { if ($k == $key) { $a['name'] = $v; $a['value'] = $val; } } $xin[] = $a; } //旧ext处理成新的ext结构 $old_data = []; foreach ($xin as $k => $v){ foreach ($new_ext as $kk => $vv){ if ($v['name'] == $vv['name'] && !empty($v['value'])) { $old_data['id'] = $vv['id']; $old_data['keyname'] = $vv['keyname']; $old_data['value'] = $v['value']; } } } //$old_data = json_decode($cusdata['ext'],true); $yes_save = []; foreach ($old_data as $k => $v) { foreach ($save_ext as $kk => $vv) { if ($v['id'] == $vv['id']) { //判断是否修改了数据 if ($v['value'] != $vv['value']) { $yes_save[] = $vv; } unset($save_ext[$kk]); } } } //去除为空值的新字段 foreach($save_ext as $key=>$val){ if(empty($val['value'])){ unset($save_ext[$key]); } } $save_ext = array_values($save_ext); $yes_data = $yes_save; if (!empty($save_ext)){ $yes_data = array_merge($yes_save,$save_ext); } if(!empty($yes_data)) $this->save_field_log($yes_data,$cusdata); } /** * 客户报备扩展字段 */ private function add_customer_extfield($ext) { $root_id = request()->empcrm->root_id; $seldata = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['status', '=', 0], ['type', 'in', [3, 4]]])->select()->toArray(); $allselid = []; foreach ($seldata as $key => $val) { foreach ($val['select'] as $k => $v) { $allselid[] = ['id' => $v['id'], 'name' => $v['name']]; } } $customer_data = []; //Log::record($ext)->save(); foreach ($ext as $key => $val) { $selval = ''; if ($val['keyname'] == 'community_name') { $customer_data['community_name'] = $val['value']; } if ($val['keyname'] == 'square') { $customer_data['square'] = $val['value']; } if ($val['keyname'] == 'house_type') { foreach ($allselid as $k => $v) { if ($val['value'] == $v['id']) { $selval = $v['name']; } } $customer_data['house_type'] = $selval; } if ($val['keyname'] == 'house_status') { foreach ($allselid as $k => $v) { if ($val['value'] == $v['id']) { $selval = $v['name']; } } $customer_data['house_status'] = $selval; } if ($val['keyname'] == 'level') { foreach ($allselid as $k => $v) { if ($val['value'] == $v['id']) { $selval = $v['name']; } } $customer_data['level'] = $selval; } if ($val['keyname'] == 'house_delivery_time') { $customer_data['house_delivery_time'] = $val['value']; } if ($val['keyname'] == 'plan_deco_time') { $customer_data['plan_deco_time'] = $val['value']; } if ($val['keyname'] == 'budget') { $customer_data['budget'] = $val['value']; } if ($val['keyname'] == 'deco_style') { $customer_data['deco_style'] = $val['value']; } if ($val['keyname'] == 'follow') { if (!empty($val['value'])) { $gza = explode(',', $val['value']); foreach ($allselid as $k => $v) { if (in_array($v['id'], $gza)) { $selval .= $v['name'] . ','; } } } $customer_data['product'] = trim($selval, ','); } if ($val['keyname'] == 'source_id') { $customer_data['source_id'] = $val['value']; } } $customer_data['ext'] = json_encode($ext); return $customer_data; } public function get_old_ext(){ $extAttr = [ 'ext1' => '年龄', 'ext2' => '是否首次装修', 'ext3' => '消费水平', 'ext4' => '预计装修时间', 'ext5' => '客户爱好', 'ext6' => '是否添加微信', 'ext7' => '客户空闲时间段', 'ext8' => '家庭结构', 'ext9' => '家庭成员意见', 'ext10' => '请选择房屋户型', 'ext11' => '决策人', 'ext12' => '客户喜欢的色调', 'ext13' => '房屋结构', 'ext14' => '装修方式', 'ext15' => '客户其它需求', 'ext16' => '客户痛点', 'ext17' => '重视环保', 'ext18' => '重视设计', 'ext19' => '关注的工艺', 'ext20' => '关注房屋的空间', 'ext21' => '汽车价格', 'ext22' => '房屋价格', 'ext23' => '买小区的原因', 'ext24' => '周边配置', 'ext25' => '设计师服务', 'ext26' => '方案满意度', 'ext27' => '报价满意度', 'ext28' => '装修意向', 'ext29' => '是否到店', 'ext30' => '预计到店时间', 'ext31' => '是否交定', 'ext32' => '客户其他需求', 'ext33' => '当前所在区域', 'ext34' => '加微类型', 'ext35' => '建群情况', 'ext36' => '直播情况', 'ext37' => '直播人员', 'ext38' => '房屋用途', 'ext39' => '房屋位置', 'ext40' => '其他房屋信息', 'ext41' => '谈单时长', 'ext42' => '门牌单元号', 'ext43' => '业务直播人员', 'ext44' => '设计直播人员' ]; return $extAttr; } /** * 添加客户检测手机号的重复 * 1.客户录入不和资源库和回收站排重 (创艺) * 2.客户录入需查询是否和公海重复,如重复,提示可以获取该客户 (创艺) 工单id6077 */ public function checkPepeat($pharr, $org_id, $employee_id, $id = null) { // 同部门能否重复录入开关设置 $root_id = request()->empcrm['root_id']; $empcrm_repeat[] = ['root_id', '=', $root_id]; $empcrm_repeat[] = ['name', '=', 'empcrm_customer_repeat']; $repeat_setting = Setting::where($empcrm_repeat)->findOrEmpty(); $emp_org = request()->empcrm['org_id']; foreach ($pharr as $val) { if (empty($val)) continue; // 先判断公海 只有添加时候判断 if (empty($id)) { $poolExist = Customer::where([ ['phone|phone1|phone2', '=', cypherphone(trim($val))], ['org_id', '=', $emp_org], ['employee_id', 'null', null], ['is_resource', '=', 0] ])->column('id'); if (!empty($poolExist)) { $poolCrmIdStr = implode(',', $poolExist); return json(['code' => 3, 'data' => $poolCrmIdStr, 'msg' => '录入失败:客户信息已在公海中。']); } } // 新建档,检测手机号是否重复 $customerExist = Customer::where([ ['phone|phone1|phone2', '=', cypherphone(trim($val))], ['org_id', 'in', $org_id], ['employee_id', '>', 0] // 员工客户 ])->field('id,employee_id,is_resource,org_id')->select()->toArray(); $id_arr = array_column($customerExist, 'id'); $employee_id_arr = array_column($customerExist, 'employee_id'); if (empty($id)) { // 对非当前业务员名下重复客户不在判断 if (in_array($employee_id, $employee_id_arr)) { // 是否已存在为自己的客户 return json(['code' => 2, 'msg' => '录入失败。' . $val . '已经是您的客户。']); } } else { if (!in_array($id, $id_arr) && in_array($employee_id, $employee_id_arr)) { // 是否已存在为自己的客户 return json(['code' => 2, 'msg' => '录入失败。' . $val . '已经是您的客户。']); } } //判断同部门 if (!$repeat_setting->isEmpty()) { $repeat_org = explode(',', $repeat_setting['content']); if (in_array($emp_org, $repeat_org)) { // 需要验证 if (!empty($customerExist)) { foreach ($customerExist as $ex) { if (in_array($ex['org_id'], $repeat_org) && $ex['employee_id'] != $employee_id) { // 为了避免开启撞单之前都已经重复的情况,所以要查一下这个id的现数据 if ($id) { $this_customer = Customer::find($id); if ($this_customer) { $this_customer_phone = array_filter([$this_customer['phone'], $this_customer['phone1'], $this_customer['phone2']]); if (!in_array($val, $this_customer_phone)) { // 不是原来的客户手机号,编辑时候判断撞单 $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name'); return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法录入']); } } } else { $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name'); return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法录入']); } } } } } } } } //编辑客户 public function edit_customer() { $request = request(); $param = $request->only(['id','empid', 'level','source_id','designer_id','name','phone','sex','source_position','subjection_plan'=>'','community_name','square','house_delivery_time','housetype_arrow'=>'','budget','unit_number'=>'','package_id','deco_style','sign_time','employee_id','phone1'=>'']); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $org_employee = [$empid]; if (!$request->isAjax()) { $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)) { // $list = hanziInitsort($list, 'name'); $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); //客户等级 隶属计划 $fields=CustomerPortraitField::with(['select'])->where([['root_id','=',$root_id],['keyname','in',['level','subjection_plan','housetype_arrow']]])->select()->toArray(); View::assign('fields', $fields); // var_dump($fields); // exit; //部门所有员工 $employee_list = Employee::where([['root_id','=',$root_id],['org_id','=',$request->empcrm->org_id],['state', 'in', ['在职']]])->field('id,name')->select()->toArray(); View::assign('employee_list', $employee_list); //意向风格 $decostyle_list = Decostyle::field('id,name,root_id as pid')->where(['root_id' =>$root_id])->where([['type', '=', 0]])->order('id desc')->select()->toArray(); View::assign('decostyle_list', $decostyle_list); //意向产品 $product = CustomerPackage::field('id, name, total_price')->where(['root_id' =>$root_id])->select(); View::assign('product', $product); View::assign('empid', $empid); //客户详情 $customer = $this->seLemp($param['id'],$empid); // var_dump($customer->toArray()); // exit; View::assign('customer',$customer); return View::fetch(); } } //客户状态详情 public function customer_detail() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $org_employee = [$empid]; // if (!$request->isAjax()) { // $this->screen(); // return View::fetch(); // } $this->screen(); $param = $request->only(['id']); $data=$this->seLemp($param['id'],$empid); //意向风格 $decostyle_list = Decostyle::field('id,name,root_id as pid')->where(['root_id' =>$root_id])->where([['type', '=', 0]])->order('id desc')->select()->toArray(); View::assign('decostyle_list', $decostyle_list); //意向产品 $product = CustomerPackage::field('id, name, total_price')->where(['root_id' =>$root_id])->select(); View::assign('product', $product); View::assign('data', $data); View::assign('empname', $request->empcrm->name); //View::assign('xinjushang',$request->empcrm['xinjushang']); // var_dump($data->toArray()); // exit; return View::fetch(); } //公海列表 public function pool_list() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $orgids = orgSubIds($root_id); if (!$request->isAjax()) { $this->screen(); $week = date('w'); View::assign('week',$week); View::assign('is_manager',$is_manager); return View::fetch(); } $param = $request->only(['page' => 1, 'limit' => 20, 'order', 'org', 'keyname', 'state', 'date' => '', 'level', 'source_id', 'designer_id', 'addtime', 'name', 'phone', 'community_name', 'employee_name', 'fistDate']); $condition = []; $order = isset($param['order']) ? $param['order'] : 'id desc'; $condition[] = ['employee_id', '=', NULL]; $condition[] = ['is_resource', '=', 0]; $condition[] = ['state','in',Customer::changeState('无效','chaos')]; $condition[] = ['org_id','in',$orgids]; if (isset($param['org']) && $param['org']) { $condition[] = ['org_id', '=', $param['org']]; } else { $orgids = orgSubIds($request->empcrm->org_id); $condition[] = ['org_id', 'in', $orgids]; } // if (isset($param['keyname']) && $param['keyname']) { // $condition[] = ['name|community_name', 'like', '%' . trim($param['keyname']) . '%']; // } if (isset($param['level']) && $param['level']) { $condition[] = ['level', 'in', explode(',',$param['level'])]; } if (isset($param['source_id']) && $param['source_id']) { $condition[] = ['source_id', 'in', explode(',',$param['source_id'])]; } if (isset($param['designer_id']) && $param['designer_id']) { $condition[] = ['designer_id', 'in', explode(',',$param['designer_id'])]; } // 姓名筛选 if (isset($param['name']) && $param['name']) { $condition[] = ['name', 'like', '%' . trim($param['name']) . '%']; } // 小区筛选 if (isset($param['community_name']) && $param['community_name']) { $condition[] = ['community_name', 'like', '%' . trim($param['community_name']) . '%']; } // 手机号筛选 if (!empty($param['phone'])) { $list = Customer::where($condition)->column('id', 'phone'); $keyCustomersId = []; $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); foreach ($list as $k => $v) { $v_phone = $aec->decrypt($k); if (strpos($v_phone, $param['phone']) !== false) $keyCustomersId[] = $v; } unset($condition); $condition[] = ['id', 'in', $keyCustomersId]; } // 交房时间筛选 if (!empty($param['house_delivery_time'])) { $house_delivery_time = explode(' - ', $param['house_delivery_time']); $start = $house_delivery_time[0]; $end = $house_delivery_time[1] . ' 23:59:59'; $condition[] = ['house_delivery_time', '>=', $start]; $condition[] = ['house_delivery_time', '<=', $end]; } // 面积筛选 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['fistDate'])) { $fistDate = explode(' - ', $param['fistDate']); $start = str_replace('/', '-', $fistDate[0]); $end = date('Y-m-d', strtotime($fistDate[1]) + 86400); $customersIdList = Customer::where($condition)->order('addtime desc')->column('id'); $dd_where[] = ['customer_id', 'in', $customersIdList]; $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]; $dd_where[] = ['confirm_date', '>=', $start]; $dd_where[] = ['confirm_date', '<=', $end]; $ddlog = CustomerVisitLog::where($dd_where)->group('customer_id')->order('addtime asc')->column('customer_id'); unset($condition); $condition[] = ['id', 'in', $ddlog]; } // 最后一次置为公海的业务员筛选 if (!empty($param['employee_name'])) { $customersIdList = Customer::where($condition)->column('id'); $pool_employee_id = CustomerVisitLog::where([['employee_id', 'in', explode(',', $param['employee_name'])], ['customer_id', 'in', $customersIdList], ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')]])->group('customer_id')->column('customer_id'); unset($condition); $condition[] = ['id', 'in', $pool_employee_id]; } // 报备时间 if (!empty($param['addtime'])) { //筛选区间范围 $date_time = explode(' - ', $param['addtime']); $start = str_replace('/','-',$date_time[0]); $end = str_replace('/','-',$date_time[1]); $condition[] = [['addtime', 'between', [$start . ' 00:00:00', $end . ' 23:59:59']]]; } // 查询id列表 $customerIdList = Customer::where($condition)->column('id'); if (isset($param['state']) && $param['state']) { // 筛查状态 $customerIdList = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState($param['state'])], ['customer_id', 'in', $customerIdList]])->group('customer_id')->column('customer_id'); } //回收时间 if ($param['date']) { //筛选区间范围 $date_time = explode(' - ', $param['date']); $start = str_replace('/','-',$date_time[0]); $end = str_replace('/','-',$date_time[1]); //$date = explode(' - ', $param['date']); //$date = $param['date']; $customerIdList = CustomerVisitLog::where([['addtime', 'between', [$start . ' 00:00:00', $end . ' 23:59:59']], ['customer_id', 'in', $customerIdList], CustomerVisitLog::changeState(['state', '=', '无效'])])->group('customer_id')->column('customer_id'); } //$pageIdList = array_slice($customerIdList, ($param['page'] - 1) * $param['limit'], $param['limit']); $pageIdList = $customerIdList; $condition[] = ['id', 'in', $pageIdList]; $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $pageIdList]])->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]; $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']; } // 上方id已分页,此处分页参数传固定1 $data = $this->poollist($condition, 1, $param['limit'], $order); $customer_id = array_column($data, 'id'); $visitLogNum = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '无效'])])->group('customer_id')->column('count(customer_id) as num', 'customer_id'); $visitLogDate = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '无效'])])->order('addtime')->column('addtime', 'customer_id'); $visitLogMeet = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '确认到店'])])->order('addtime')->column('addtime', 'customer_id'); foreach ($data as &$value) { //回收次数 $value['recoverNum'] = isset($visitLogNum[$value['id']]) ? $visitLogNum[$value['id']] : 0; //最近的回收时间 $value['recoverLatelyDate'] = isset($visitLogDate[$value['id']]) ? $visitLogDate[$value['id']] : null; //最近的确认到店时间 $value['fistDate'] = isset($visitLogMeet[$value['id']]) ? $visitLogMeet[$value['id']] : null; //上次跟进人信息 $value['before_pool'] = !empty($value['before_pool']) ? json_decode($value['before_pool'], true) : null; $value[$value['id']]['stateNum'] = $customersState[$value['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; // 客户最后一次置为公海的关联员工 $last_pool_employee = CustomerVisitLog::where([['customer_id', '=', $value['id']], ['state','in',CustomerVisitLog::changeState('无效','chaos')]])->with('employee')->order('addtime desc')->find(); $value['employee'] = $last_pool_employee ? $last_pool_employee->employee->name : ''; } $sort = array_column($data,'recoverLatelyDate'); array_multisort($sort, SORT_DESC, $data); foreach ($condition as $k => $v) { if ($v[0] == 'id') { if (isset($v[2])) { $condition[$k][2] = $customerIdList; } } } $data = array_slice($data, ($param['page'] - 1) * $param['limit'], $param['limit']); $count = CustomerLogic::count($condition); return json(['code' => 0, 'data' => $data, 'count' => $count]); } //查询公海 public function poollist($condition = [], $page, $limit, $order) { $page = intval($page); $limit = intval($limit); $data = Customer::with(['source'=>function($query){ $query->field('id,source'); }])->field('source_id,id, name, community_name, phone, level, designer_id,state, square, addtime, org_id, employee_id, is_resource, before_pool,first_visit_date,house_delivery_time') ->where($condition) ->select()->toArray(); foreach ($data as &$item) { $item['pool_name'] = Org::where('id', $item['org_id'])->column('name'); if (!$item['employee_id']) { $item['phone'] = substr_replace($item['phone'], '******', 3, 6); } } return $data; } /** * 快速修改客户的扩展信息 */ public function fast_up_portrait() { $request = request(); $root_id = $request->empcrm->root_id; $orgids = orgSubIds($root_id); $param = $request->only(['type','cid','value']); //var_dump($param);exit; $cusdata = Customer::where([['org_id','in',$orgids],['id','=',$param['cid']]])->find(); if(empty($cusdata)) return json(['code'=>'1','msg'=>'客户不存在']); if($param['type'] == 'level'){ if(!in_array($param['value'],['A','B','C','D'])) return json(['code'=>'1','msg'=>'客户等级不存在']); $cusdata->level = $param['value']; $cusdata->save(); return json(['code'=>'0','msg'=>'修改成功']); } if($param['type'] == 'field'){ if(!in_array($param['value'],['reputation_gather','into_owner_group','add_wechat_time'])) return json(['code'=>'1','msg'=>'修改字段不存在']); $fieldid = CustomerPortraitField::where([['root_id','=',$root_id],['keyname','=',$param['value']]])->value('id'); $isin = false; if(!empty($cusdata['ext'])){ foreach(json_decode($cusdata['ext'],true) as $key=>$val){ if($val['keyname'] == $param['value'] && empty($val['value'])){ $val['value'] = date('Y-m-d'); $isin = true; } $all[] = ['id'=>$val['id'],'value'=>$val['value'],'keyname'=>$val['keyname']]; } if(!$isin) $all[] = ['id'=>$fieldid,'value'=>date('Y-m-d'),'keyname'=>$param['value']]; $cusdata->ext = json_encode($all); $cusdata->save(); return json(['code'=>'0','msg'=>'修改成功']); } } } /* * 审核人员 */ public function get_person() { $ids = input('id', ''); $root_id = request()->empcrm->root_id; $type = input('type', 0); //树形 if ($type == 1) { //所有员工 $w1[] = ['root_id', '=', $root_id]; $w1[] = ['state', '=', '在职']; $w1[] = ['uid', '<>', 0]; $w1[] = ['org_id', '>', 0]; $w1[] = ['org_id', '<>', $root_id]; $w1[] = ['id','<>',request()->empcrm->id]; $employee_list = Employee::where($w1)->with(['org'=>function($query){ $query->field('id,org_type'); }])->select()->toArray(); $employee = []; foreach ($employee_list as $k => $v) { $employee[] = [ 'value'=> $v['id'], 'name'=> $v['name'], 'org_id'=> $v['org_id'], 'org_type'=> $v['org']['org_type'] ]; } if ($ids) { $ids = explode(',', $ids); foreach ($employee as $k => &$v) { $v['selected'] = in_array($v['value'], $ids); } } else { foreach ($employee as $k => &$v) { $v['selected'] = false; } } $persons = []; foreach ($employee as $k => $v1) { $persons[$v1['org_id']][] = $v1; } $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->tree($allnodes, 0, $persons); return json($tree); } $w[] = ['root_id', '=', $root_id]; $w[] = ['state', '=', '在职']; $w[] = ['role', '=', '领导']; $w[] = ['uid', '<>', 0]; $employee = Employee::where($w)->field('id value,name')->select()->toArray(); if ($ids) { $ids = explode(',', $ids); foreach ($employee as $k => &$v) { $v['selected'] = in_array($v['value'], $ids); } } else { foreach ($employee as $k => &$v) { $v['selected'] = false; } } return json($employee); } public function tree($data, $pid = 0, $persons) { $new_arr = []; foreach ($data as $k => $v) { if ($v['pid'] == $pid) { $persions = isset($persons[$v['id']]) ? $persons[$v['id']] : []; $children = $this->tree($data, $v['id'], $persons); $v['children'] = array_merge_recursive($children, $persions); //if (empty($v['children'])) $v['disabled'] = true; if (empty($v['children'])) continue; $new_arr[] = $v; } } return $new_arr; } /** * 日报 */ public function daily() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $org_employee = [$empid]; $team_orgs = [$request->empcrm->org_id]; if($is_manager ==1){ $team_orgs = orgSubIds($request->empcrm->org_id); $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0],['community_disable','=',0]])->column('id'); } $param = $request->only(['date','employee_id','org_id']); if (!$request->isAjax()) { $data['company_name'] = Company::where('root_id',$root_id)->value('company_name'); $data['empname'] = $request->empcrm->name; $data['date'] = !empty($param['date']) ? $param['date'] : date('Y-m'); $data['org_id'] = !empty($param['org_id']) ? $param['org_id'] :''; $data['org_name'] = !empty($param['org_id']) ? Org::where([['id','=',$param['org_id']],['root_id','=',$root_id]])->value('name') : ''; View::assign('data',$data); View::assign('is_manager',$is_manager); //部门所有员工 $employee_list = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0],['community_disable','=',0]])->field('id,name')->select()->toArray(); View::assign('employee_list', $employee_list); //下级所有部门 $org_list = Org::where([['id','in',$team_orgs],['id','<>',$request->empcrm->org_id]])->select()->toArray(); View::assign('org_list',$org_list); return View::fetch(); } $today = date('Y-m',time()).'-01 00:00:00'; $todaynum = date('d',time()); $firstday = $param['date'].'-'.'01 00:00:00'; $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day")); $lastnum = date('d', strtotime("$firstday +1 month -1 day")); $nowempid = $empid; $noworgid = $request->empcrm->org_id; if($is_manager ==1) $nowempid = $noworgid = ''; if(!empty($param['employee_id']) && $is_manager ==1 && in_array($param['employee_id'],$org_employee)) $nowempid = $param['employee_id']; $where[] = ['addtime', 'between', [$firstday,$lastday]]; if(!empty($param['employee_id']) && $is_manager ==1 && in_array($param['employee_id'],$org_employee)) $org_employee = [$param['employee_id']]; if(!empty($param['org_id']) && $is_manager ==1){ $noworgid = $param['org_id']; $org_employee = Employee::where([['org_id','in',$param['org_id']],['root_id','=',$root_id],['state', '=', '在职'], ['uid', '>', 0],['community_disable','=',0]])->column('id'); } $where[] = ['customer_employee_id','in',$org_employee]; //$list = Customer::where($where)->field('id,name,addtime,state,valid_time')->select()->toArray(); //电话记录 $hu_where = []; $hu_where[] = ['employee_id','in',$org_employee]; $hu_where[] = ['root_id','=',$root_id]; $hu_where[] = ['addtime','>=',$firstday]; $hu_where[] = ['addtime','<=',$lastday]; //新增加客户数 $new_cuslist = Customer::where([['sign_time','>=',$firstday],['sign_time','<=',$lastday],['employee_id','in',$org_employee],['state','not in',Customer::changeState('无效','chaos')]])->column('id,sign_time,state');//->select()->toArray(); //加微客户数 $addwechat_cuslist = Customer::where([['add_wechat_time','>=',$firstday],['add_wechat_time','<=',$lastday],['employee_id','in',$org_employee]])->column('id,add_wechat_time'); //进群客户数 $into_group_cuslist = Customer::where([['into_owner_group','>=',$firstday],['into_owner_group','<=',$lastday],['employee_id','in',$org_employee]])->column('id,into_owner_group'); //待确认的客户 //$treat_cuslist = Customer::where([['state','in',[0,'待确认']],['sign_time','>=',$firstday],['sign_time','<=',$lastday],['employee_id','in',$org_employee]])->column('sign_time','id'); //置为无效的客户 $invalidcus = CustomerInvalidLog::where([['root_id','=',$root_id],['employee_id', 'in', $org_employee],['cus_addtime','between', [$firstday,$lastday]]])->column('cus_addtime,employee_id,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'); $state9 = CustomerVisitLog::changeState('已量房','chaos'); //记录 $visitlog_list = CustomerVisitLog::where($where) ->where([['state','in',array_merge($state3,$state4,$state5,$state6,$state7,$state8,$state9)]]) //->group('state,customer_id') ->order('addtime asc') ->column('customer_id,state,money,confirm_date'); $metids = []; foreach($visitlog_list as $key=>$val){ if(in_array($val['state'],array_merge($state5,$state8,$state9))) $metids[] = $val['customer_id']; } $yesids = !empty($metids) ? $this->selBeforelog(array_unique($metids),$firstday) : []; $xin=[]; $weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组 if($today == $firstday){ for($i=0;$i<$todaynum;$i++){ $day = date('Y-m-d', strtotime("$firstday +$i days")); $a['day'] = $day; $a['week'] = $weekarray[date("w",strtotime("$firstday +$i days"))]; $a['logdata'] = []; foreach($visitlog_list as $key=>$val){ if($val['confirm_date'] >= $day.' 00:00:00' && $val['confirm_date'] <= $day.' 23:59:59'){ $a['logdata'][] = $val; } } $xin[] = $a; } }elseif($today > $firstday){ for($i=0;$i<$lastnum;$i++){ $day = date('Y-m-d', strtotime("$firstday +$i days")); $a['day'] = $day; $a['week'] = $weekarray[date("w",strtotime("$firstday +$i days"))]; $a['logdata'] = []; foreach($visitlog_list as $key=>$val){ if($val['confirm_date'] >= $day.' 00:00:00' && $val['confirm_date'] <= $day.' 23:59:59'){ $a['logdata'][] = $val; } } $xin[] = $a; } } unset($visitlog_list); $meetids = []; $t=['day'=>'求和','week'=>'','data'=>[],'new_cus'=>0,'jv'=>0,'jv_percen'=>0,'jq'=>0,'jq_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]; foreach($xin as $key=>$val){ //$xin[$key]['clue_cont'] = count($new_cuslist); $xin[$key]['nowempid'] = $nowempid; $xin[$key]['noworgid'] = $noworgid; $jq = $jv = $new_cus = $meet = $deposit = $sign = $basic_money = $invalid_no = $invalid_yes = $invalid_meet = 0; $deposids = $invalids = $daystate = $cusids = $validids = []; // 外呼系统统计 $start = $val['day'].' 00:00:00'; $end = $val['day'].' 23:59:59'; //线索量 foreach($new_cuslist as $k=>$v){ if($v['sign_time'] >= $start && $v['sign_time'] <= $end){ $new_cus++; $cusids[] = $v['id']; } } //加微客户数 foreach($addwechat_cuslist as $k=>$v){ if($v['add_wechat_time'] >= $val['day'] && $v['add_wechat_time'] <= $val['day']){ $jv++; } } //进群客户数 foreach($into_group_cuslist as $k=>$v){ if($v['into_owner_group'] >= $val['day'] && $v['into_owner_group'] <= $val['day']){ $jq++; } } //无效客户数增加 foreach($invalidcus as $v){ if($v['cus_addtime'] >= $start && $v['cus_addtime'] <= $end && !in_array($v['customer_id'],$cusids)){ $new_cus++; $cusids[] = $v['customer_id']; } } unset($cusids); foreach($val['logdata'] as $ky=>$v){ if(in_array($v['state'],$state3,true) && !in_array($v['customer_id'],$deposids)){ $deposit++; $deposids[] = $v['customer_id']; } if(in_array($v['state'],$state4,true)){ $sign++; $basic_money+= $v['money']; } if(in_array($v['state'],array_merge($state5,$state8,$state9)) && !in_array($v['customer_id'],$meetids) && in_array($v['customer_id'],$yesids)){ $meet++; $meetids[] = $v['customer_id']; } if(in_array($v['state'],$state6)){ $invalid_yes++; $invalids[] = $v['customer_id']; } } foreach($invalids as $k=>$v){ if(in_array($v,$meetids)) $invalid_meet++; } $xin[$key]['meet_cont'] = $meet; $xin[$key]['deposit_cont'] = $deposit; $xin[$key]['sign_cont'] = $sign; $xin[$key]['basic_money'] = $basic_money; $xin[$key]['invalid_no'] = $invalid_no; $xin[$key]['invalid_yes'] = $invalid_yes; $xin[$key]['invalid_meet'] = $invalid_meet; $xin[$key]['new_cus'] = $new_cus; $xin[$key]['jv'] = $jv; $xin[$key]['jv_percen'] = !empty($jv) && !empty($new_cus) ? round($jv/$new_cus*100,2) : 0; $xin[$key]['jq'] = $jq; $xin[$key]['jq_percen'] = !empty($jq) && !empty($jv) ? round($jq/$jv*100,2) : 0; //总数 $t['meet_cont'] += $meet; $t['deposit_cont'] += $deposit; $t['sign_cont'] += $sign; $t['basic_money'] += $basic_money; $t['invalid_no'] += $invalid_no; $t['invalid_yes'] += $invalid_yes; $t['invalid_meet'] += $invalid_meet; $t['new_cus'] += $new_cus; $t['jv'] += $jv; $t['jq'] += $jq; } $t['jv_percen'] = !empty($t['jv']) && !empty($t['new_cus']) ? round($t['jv']/$t['new_cus']*100,2) : 0; $t['jq_percen'] = !empty($t['jq']) && !empty($t['jv']) ? round($t['jq']/$t['jv']*100,2) : 0; $xin = array_reverse($xin); array_push($xin,$t); return json(['code' => 0, 'data' => $xin, 'count' =>0,'msg'=>'获取成功']); } //处理查询客户之前的见面记录 public function selBeforelog($metids,$firstday) { $statday = '2021-12-01 00:00:00'; $state1 = CustomerVisitLog::changeState('确认到店','chaos'); $state2 = CustomerVisitLog::changeState('已到场','chaos'); $state3 = CustomerVisitLog::changeState('已量房','chaos'); $q_where[] = ['customer_id','in',$metids]; $q_where[] = ['state','in',array_merge($state1,$state2,$state3)]; $q_where[] = ['addtime', 'between', [$statday,$firstday]]; $qianvslog = CustomerVisitLog::where($q_where)->group('customer_id')->column('customer_id'); $yesids = array_diff($metids,$qianvslog); return $yesids; } //日报点击弹出详情页面 public function new_daily_detail() { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $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],['community_disable','=',0]])->column('id'); } $param = $this->request->only(['date','state','empid'=>'','page'=>1,'limit'=>17,'orgid'=>'']); if (!$request->isAjax()) { //$this->screen(); View::assign('date',$param['date']); View::assign('state',$param['state']); View::assign('empid',$param['empid']); View::assign('orgid',$param['orgid']); return View::fetch(); } $order = isset($param['order']) ? $param['order'] : 'id desc'; $page = intval($param['page']); $limit = intval($param['limit']); if($is_manager ==1 && !empty($param['empid']) && $param['empid'] != 'undefined' && in_array($param['empid'],$org_employee)) $org_employee = [$param['empid']]; if($is_manager ==1 && !empty($param['orgid']) && $param['orgid'] != 'undefined'){ $org_employee = Employee::where([['org_id','=',$param['orgid']],['root_id','=',$root_id],['community_disable','=',0]])->column('id'); } if(empty($param['date'])) return json(['code' => 1, 'msg'=>'参数错误']); if (!empty($param['date'])){ $newtime=explode('到', $param['date']); } $condition[] = ['employee_id', 'in', $org_employee]; if($param['state'] != 'addnew') $condition[] = ['died', '<>', 2]; $condition[] = ['state','not in',Customer::changeState('无效','chaos')]; $vlst = '确认到店'; if($param['state']=='到访'){ $vlst = '确认到店'; }elseif($param['state']=='交定'){ $vlst = '交定'; }elseif($param['state']=='签单'){ $vlst = '签单'; }elseif($param['state']=='clue' || $param['state']=='addnew'){ $condition[] = ['sign_time', 'between', [$newtime[0].' 00:00:00',$newtime[1].' 23:59:59']]; $addnewids = Customer::where($condition)->column('id'); //置为无效的客户 $wuxiaoids = CustomerInvalidLog::where([['employee_id', 'in', $org_employee],['root_id','=',$root_id],['cus_addtime','between', [$newtime[0].' 00:00:00',$newtime[1].' 23:59:59']]])->column('customer_id'); if(!empty($wuxiaoids)) $addnewids = array_merge($addnewids,$wuxiaoids); unset($condition); //$condition[] = ['id','in',$addnewids]; $condition = implode(',',$addnewids); $page = ($page - 1) * 17; $data = $this->cus_recycle($condition,$page,$limit,$order,$root_id); return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]); }elseif($param['state']=='valid'){ $condition[] = ['sign_time', 'between', [$newtime[0].' 00:00:00',$newtime[1].' 23:59:59']]; $condition[] = ['state', 'not in', Customer::changeState('待确认','chaos')]; $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[] = ['id','in',$validids]; $condition = implode(',',$validids); $page = ($page - 1) * 17; $data = $this->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'))]; }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']]; } //$where[] =['customer_id','in',$customer_list]; $vslogid = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id'); if($vlst == '确认到店'){ $vslogid = !empty(array_unique($vslogid)) ? $this->selBeforelog($vslogid,$newtime[0].' 00:00:00') : []; } if(!in_array($param['state'],['clue','addnew','yixiang','liangfang','daodian','valid'])){ unset($condition); $condition[] = ['id', 'in', $vslogid]; } $data = $this->selCustomer($condition,$page,$limit,$order,$root_id); $count = Customer::where($condition)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 客户表加回收站查询 */ public function cus_recycle($condition,$page,$limit,$order,$root_id) { $request = request(); $root_id = $request->empcrm->root_id; $empid = $request->empcrm->id; $is_manager = $request->empcrm->is_manager; $path = Org::where([['pid','=',$request->empcrm->org_id]])->count(); $sql = "( SELECT id,name,community_name,square,phone,addtime,employee_id,designer_id,source_id,package_id,assigned_personnel,house_delivery_time,state,ext,last_contact_date FROM `fl_customer` WHERE `id` in (".$condition.")) UNION ( SELECT customer_id as id,name,community_name,phone,square,addtime,employee_id,designer_id,source_id,package_id,assigned_personnel,house_delivery_time,state,ext,last_contact_date FROM `fl_customer_recycle` WHERE `customer_id` in (".$condition.")) LIMIT ".$page.",".$limit; $data = Db::query($sql); $sqlcont = "( SELECT id FROM `fl_customer` WHERE `id` in (".$condition.")) UNION ( SELECT customer_id as id FROM `fl_customer_recycle` WHERE `customer_id` in (".$condition."))"; $count = count(Db::query($sqlcont)); $state1 = CustomerVisitLog::changeState('已到店','chaos'); $state2 = CustomerVisitLog::changeState('已交定','chaos'); $state3 = CustomerVisitLog::changeState('已签单','chaos'); $state4 = CustomerVisitLog::changeState('已量房','chaos'); $state5 = CustomerVisitLog::changeState('无效','chaos'); foreach ($data as &$item) { $item['visitLog'] = $item['source_name'] = $item['designer_name'] = $item['employee_name'] = ''; $item['source'] = !empty($item['source_id']) ? CustomerSource::where([['root_id','=',$root_id],['id','=',$item['source_id']]])->field('id,source')->find() : ''; $item['designer'] = !empty($item['designer_id']) ? Employee::where([['root_id','=',$root_id],['id','=',$item['designer_id']]])->field('id,name')->find() : ''; $item['employee'] = !empty($item['employee_id']) ? Employee::where([['root_id','=',$root_id],['id','=',$item['employee_id']]])->field('id,name')->find() : ''; $item['last_invalid_time'] = $item['first_liangfang'] = $item['jiaoding_time'] = $item['sign'] = $item['fisttime'] = ''; $visitLog = CustomerVisitLog::where([['customer_id','=',$item['id']],['org_id','in',orgSubIds($root_id)]])->field('id,customer_id,state,addtime,confirm_date,money')->select()->toArray(); $item['visitLog'] = !empty($visitLog) ? $visitLog : ''; if(!empty($item['visitLog'])){ //取最新的一条为客户的跟进时间 $item['last_contact_date'] = $item['visitLog'][count($item['visitLog'])-1]['addtime']; $state = []; foreach($item['visitLog'] as $k=>$v){ $state[$v['state']][] = $v; } foreach($state as $k=>$v){ if(in_array($k,$state1)) $item['fisttime'] = date('Y-m-d',strtotime($v[0]['confirm_date'])); if(in_array($k,$state4)) $item['first_liangfang'] = date('Y-m-d',strtotime($v[0]['addtime'])); if(in_array($k,$state2)){ $item['jiaoding_time'] = $v[0]['addtime']; $item['jiaoding_money'] = $v[0]['money']; } if(in_array($k,$state3)){ $item['qiandan_time'] = $v[0]['addtime']; $item['qiandan_money'] = $v[0]['money']; $item['sign'] = $v[0]; } if(in_array($k,$state5)) $item['last_invalid_time'] = $v[0]['addtime']; } } $item['square'] = floatval($item['square']); //$item['visitLog'] = CustomerVisitLog::where([['customer_employee_id','=',$item['employee_id']],['customer_id','=',$item['id']],['state','in',array_merge(CustomerVisitLog::changeState('待确认','chaos'),CustomerVisitLog::changeState('已到店','chaos'),CustomerVisitLog::changeState('已到场','chaos'),CustomerVisitLog::changeState('已交定','chaos'),CustomerVisitLog::changeState('已签单','chaos'))]])->group('state,customer_id')->field('state,id,customer_id')->order('addtime asc')->select()->toArray(); $item['phone'] = substr_replace($this->getPhone($item['phone']), '******', 3, 6); $item['time_status'] = $item['last_contact_date'] ? $this->uc_time_ago($item['last_contact_date']) :''; $item['add_wechat'] = '否'; $item['sign_time'] = $item['addtime']; $item['allstate'] = null; $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id',$item['package_id'])->value('name') :''; if(!empty($item['ext'])){ $extdata=json_decode($item['ext'],true); foreach($extdata as $key=>$val){ if(isset($val['keyname']) && $val['keyname']=='wechat' && !empty($val['value'])){ $add_wechat = CustomerPortraitFieldSelect::where('id',$val['value'])->value('name'); if($add_wechat == '有'){ $add_wechat = '是'; } $item['add_wechat'] = $add_wechat; } if(isset($val['keyname']) && $val['keyname']=='sign_time' && !empty($val['value'])){ $item['sign_time'] = str_replace('/','-',$val['value']); } } } $item['is_followup'] = 0; if($is_manager==1 && empty($path)){ $item['is_followup'] = 1; } if($empid == $item['employee_id']){ $item['is_followup'] = 1; } $now_status = $this->getState($item['state']); if($now_status == '已签单' && $request->empcrm['xinjushang'] == 0) $now_status = '已转单'; if($now_status == '已交定' && $request->empcrm['xinjushang'] == 0) $now_status = '已签单'; $item['now_status'] = $now_status; $item['state'] = ''; $allstate=''; if(!empty($item['visitLog'])){ $stain=[$now_status]; foreach($item['visitLog'] as $k=>$v){ if(!in_array($v['state'],['已交定','已签单','已到店','已量房','已到场','未到访','回访'])) continue; if($v['state']=='回访') $v['state'] = '待确认'; if($v['state']=='已签单' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已转单'; if($v['state']=='已交定' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已签单'; if(!in_array($v['state'],$stain)){ $stain[] = $v['state']; //$allstate.= $v['state'].','; } } if(count($stain) > 1 && in_array('待确认',$stain)) $stain = array_diff($stain,['待确认']); //$item['state'] = trim($allstate,','); $item['state'] = implode(',',$stain); } if($now_status == '待确认') $item['state'] = '待确认'; // 未跟进时间 //$latest_visit_log = CustomerVisitLog::where([['customer_id', '=', $item['id']]])->order('addtime desc')->find(); $latest_visit_log = $item['last_contact_date']; if (empty($latest_visit_log)) { $no_visit_long = time() - strtotime($item['addtime']); } else { $no_visit_long = time() - strtotime($latest_visit_log); } $no_visit_day = ceil($no_visit_long / 24 / 3600); $item['no_visit_day'] = $no_visit_day; // 预计见面时间 $subscribe = CustomersSubscribe::where([['customer_id', '=', $item['id']], ['employee_id', '=', $item['employee_id']], ['state', '=', 0]])->order('subscribe_date desc')->find(); if (!empty($subscribe)) { $item['subscribe_date'] = $subscribe['subscribe_date']; } else { $item['subscribe_date'] = ''; } $assignIdList = !empty($item['assigned_personnel']) ? explode(',',$item['assigned_personnel']) : []; if(!empty($item['designer_id']) && !empty($assignIdList)) $assignIdList = array_diff($assignIdList,[$item['designer_id']]); $assign_list = Employee::where('id', 'in', $assignIdList)->column('name'); $item['assign_list'] = !empty($assign_list) ? implode(',',$assign_list) : ''; } return ['data'=>$data,'count'=>$count]; } /** * 手机号解密 */ public function getPhone($value) { if (preg_match('/^1[\d]{10}$/', $value)) return $value; $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->decrypt($value); return $value; } public function getState($value) { $states = [ ['n' => 0, 'chaos' => [0, '待确认', '0'], 'state' => '待确认'], ['n' => 1, 'chaos' => [1, '未到访', '1'], 'state' => '未到访'], ['n' => 2, 'chaos' => [2, '已到访', '确认到店', '确定到店', '已到店', '2'], 'state' => '已到店'], ['n' => 3, 'chaos' => [3, '确定到场', '确认到场', '已到场', '3'], 'state' => '已到场'], ['n' => 4, 'chaos' => [4, '已量房', '确定量房', '确认量房', '4'], 'state' => '已量房'], ['n' => 5, 'chaos' => [5, '定金', '交定', '已交定', '5'], 'state' => '已交定'], ['n' => 6, 'chaos' => [6, '签单', '已签单', '6'], 'state' => '已签单'], ['n' => 7, 'chaos' => [7, '无效', '7'], 'state' => '无效'], ['n' => 8, 'chaos' => [8, '已卖卡', '8'], 'state' => '已卖卡'], ]; if (is_numeric($value)) $value = intval($value); foreach ($states as $s) { if ($value === $s['n'] || in_array($value, $s['chaos'], true)) return $s['state']; } return ''; } //设计师报表 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] ]; 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 = [ ['sign_time', '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']], ['customer_id','in',$customer_id], ['root_id','=',$root_id] ])->column('customer_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['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 $item => $vv){ if($vv['customer_id'] == $v['id']){ $clue_cont++; if($vv['status'] == 1) $valid++; } } } // 见面量只统计一次(到场、到店、量房、签单、转单) $meet_state = array_merge(CustomerVisitLog::changeState('确认到店','chaos'), CustomerVisitLog::changeState('已到场','chaos'), CustomerVisitLog::changeState('已量房','chaos'), CustomerVisitLog::changeState('已交定','chaos'), CustomerVisitLog::changeState('已签单','chaos')); if(empty($param['source_id'])) { $visilog = CustomerVisitLog::where([ ['customer_id', 'in', $cusids], ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']], ['state','in', $meet_state] ])->column('customer_id'); } else { $visilog = CustomerVisitLog::hasWhere('customer',['source_id'=>$param['source_id']]) ->where([ ['CustomerVisitLog.customer_id', 'in', $cusids], ['CustomerVisitLog.confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']], ['CustomerVisitLog.state','in', $meet_state] ])->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' => '获取成功']); } } ?>