Dashboard.php
14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
namespace app\admin\controller\facrm;
use app\admin\model\AuthGroup;
use app\admin\model\facrm\Contract;
use app\admin\model\facrm\contract\Receivables;
use app\common\controller\Backend;
use fast\Date;
use fast\Tree;
use think\Config;
/**
* 控制台
*
* @icon fa fa-dashboard
* @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
*/
class Dashboard extends Backend
{
protected $groupList=[];
protected $groupdata=[];
public function _initialize()
{
parent::_initialize();
$this->model = model('Admin');
$this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
$this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
$this->groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
Tree::instance()->init( $this->groupList);
if ($this->auth->isSuperAdmin()) {
$result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
foreach ($result as $k => $v) {
$this->groupdata[$v['id']] = $v['name'];
}
} else {
$result = [];
$groups = $this->auth->getGroups();
foreach ($groups as $m => $n) {
$childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
$temp = [];
foreach ($childlist as $k => $v) {
$temp[$v['id']] = $v['name'];
}
$result[__($n['name'])] = $temp;
}
$this->groupdata = $result;
}
$this->view->assign('groupdata', $this->groupdata);
$this->assignconfig("admin", ['id' => $this->auth->id]);
}
/**
* 查看
*/
public function index()
{
if ($this->request->isAjax()){
$ajax_type=$this->request->param('ajax_type');
$temp_type=$this->request->param('type');
switch ($ajax_type){
case 'achievement':
$achievement=$this->achievement(
$this->request->param('row_config'),
$this->request->param('row_year'),
$this->request->param('row_month',0,'intval'),
3,$this->auth->id,$this->auth->id
);
$this->success('','',$achievement);
case 'team_achievement':
$achievement=$this->achievement(
$this->request->param('row_config'),
$this->request->param('row_year'),
$this->request->param('row_month',0,'intval'),
2,$this->childrenGroupIds,$this->childrenAdminIds
);
$this->success('','',$achievement);
case 'getAchievementEchart':
//$temp_type是区分团队的还是个人的
$this->success('', '', [
$temp_type => $this->getAchievementEchart(false,0,($temp_type=='team_achievement'?$this->childrenAdminIds:$this->auth->id))
]);
case 'getRecordEchartData':
//$temp_type是区分团队的还是个人的
$this->success('', '', [
$temp_type => $this->getRecordEchartData(false,0,($temp_type=='team_record'?$this->childrenAdminIds:$this->auth->id))
]);
}
$this->error("访问出错");
}
//自己的start
//当天开始时间
$start_time=strtotime(date("Y-m-d",time()));
//当天结束之间
$end_time=$start_time+60*60*24;
$filter_w['owner_user_id'] = $this->auth->id;
$customerModel = model('\app\admin\model\facrm\Customer');
$cluesModel = model('\app\admin\model\facrm\Clues');
//需要联系客户
$communicate=$customerModel->where('next_time','between',[1,$end_time])->where($filter_w)->count();
//需要联系的线索
$clues_num=$cluesModel->where('next_time','between',[1,$end_time])->where($filter_w)->count();
//将要过期的用户
$lose_num=count($customerModel->getLose($this->auth->id));
$businessModel = model('\app\admin\model\facrm\Business');
$business=$businessModel->where('next_time','between',[1,$end_time])->where($filter_w)->count();
//是否有合同权限
$contract_return=$contract_expire=0;
if($this->auth->check('facrm/contract/index')){
$contractModel = model('\app\admin\model\facrm\Contract');
$filter_w['check_status']=2;
//快过期和过期合同
$contract_expire=$contractModel->where('end_time','between',[1,$end_time+(30*86400)])->where('expire_handle',0)->where($filter_w)->count();
//待回款合同
$contract_return=$contractModel->where("money","exp",">return_money")->where($filter_w)->count();
}
$year=date("Y",time());
$achievement=$this->achievement(1,$year,0,3,$this->auth->id,$this->auth->id);
$this->view->assign([
'communicate'=>$communicate,
'lose_num'=>$lose_num,
'business'=>$business,
'contract_expire'=>$contract_expire,
'contract_return'=>$contract_return,
'clues_num'=>$clues_num,
'achievement'=>$achievement,
]);
$erchart = [
'achievement' => $this->getAchievementEchart(false,0,$this->auth->id),
'record' => $this->getRecordEchartData(false,0,$this->auth->id),
];
$this->view->assign([
'erchart' => $erchart,
]);
$this->assignconfig(['achievement'=>$achievement]);
//自己的end
//团队的
$filter_w=array();
$filter_w['owner_user_id'] =['in',$this->childrenAdminIds];
//需要联系客户
$team_communicate=$customerModel->where('next_time','between',[1,$end_time])->where($filter_w)->count();
//需要联系的线索
$team_clues_num=$cluesModel->where('next_time','between',[1,$end_time])->where($filter_w)->count();
//需要联系的联系人
//$team_communicate=$customerModel->where('next_time','between',[1,$end_time])->where($filter_w)->count();
//将要过期的用户
$team_lose_num=count($customerModel->getLose($this->childrenAdminIds));
$team_business=$businessModel->where('next_time','between',[1,$end_time])->where($filter_w)->count();
//是否有合同权限
$team_contract_return=$team_contract_expire=0;
if($this->auth->check('facrm/contract/index')){
$contractModel = model('\app\admin\model\facrm\Contract');
$filter_w['check_status']=2;
//快过期和过期合同
$team_contract_expire=$contractModel->where('end_time','between',[1,$end_time+(30*86400)])->where('expire_handle',0)->where($filter_w)->count();
//待回款合同
$team_contract_return=$contractModel->where("money","exp",">return_money")->where($filter_w)->count();
}
$team_achievement=$this->achievement(1,$year,0,2,$this->childrenGroupIds,$this->childrenAdminIds);
$this->view->assign([
'team_communicate'=>$team_communicate,
'team_lose_num'=>$team_lose_num,
'team_business'=>$team_business,
'team_contract_expire'=>$team_contract_expire,
'team_contract_return'=>$team_contract_return,
'team_clues_num'=>$team_clues_num,
'team_achievement'=>$team_achievement,
]);
$team_erchart = [
'team_achievement' => $this->getAchievementEchart(false,0,$this->childrenAdminIds),
'team_record' => $this->getRecordEchartData(false,0,$this->childrenAdminIds),
];
$this->view->assign([
'team_erchart' => $team_erchart,
]);
$this->assignconfig(['team_achievement'=>$team_achievement]);
//团队的end
return $this->view->fetch();
}
/**
* 业绩指标
* @param $config 1=>'合同金额',2=>'回款金额'
* @param $year 年份
* @param $month 1~12月份 0代表年份
* @param $type 2部门3员工
* @param $type_id 对应type对象ID
* @return array
*/
private function achievement($config,$year,$month,$type,$type_id,$admin_id){
$config_arr=[1=>'合同金额',2=>'回款金额'];
$month_arr=[0=>'yeartarget',1=>"january",2=>"february",3=>"march",4=>"april",5=>"may",6=>"june"
,7=>"july",8=>"august",9=>"september",10=>"october",11=>"november",12=>"december"
];
if (!isset($month_arr[$month])||!isset($config_arr[$config])){
$this->error("参数有误");
}
if (!is_numeric($year)){
$this->error("年份有误");
}
$achievementModel=new \app\admin\model\facrm\Achievement();
//获取已完成情况
//当天开始时间
list($start_time,$end_time)=$this->mFristAndLast($year,$month);
//获取业绩目标
if ( $type==3){
//员工业绩
$yeartarget=$achievementModel->where('type',$type)->where('type_id',$type_id)
->where('config',$config)
->where('year',$year)->value($month_arr[$month]);
$contract_money=Contract::where('check_status',2)->where('order_admin_id',$admin_id)
->where('order_time','between',[$start_time,$end_time])
->sum('money');
$receivables_money=Receivables::where('check_status',2)->where('order_admin_id',$admin_id)
->where('return_time','between',[$start_time,$end_time])
->sum('money');
}else{
//部门业绩
$yeartarget=$achievementModel->where('type',$type)->where('type_id','in',$type_id)
->where('config',$config)
->where('year',$year)->sum($month_arr[$month]);
$contract_money=Contract::where('check_status',2)->where('order_admin_id','in',$admin_id)
->where('order_time','between',[$start_time,$end_time])
->sum('money');
$receivables_money=Receivables::where('check_status',2)->where('order_admin_id','in',$admin_id)
->where('return_time','between',[$start_time,$end_time])
->sum('money');
}
$achievement=[
'name'=>$config_arr[$config],
'year'=>$year,
'yeartarget'=>$yeartarget?$yeartarget:0,
'contract_money'=>$contract_money,
'receivables_money'=>$receivables_money,
'complete_percent'=>$yeartarget>0?round(($config==1?$contract_money:$receivables_money)/$yeartarget*100,2):0,
];
return $achievement;
}
/**
* 获取时间的开始和结束时间
* @param int $y
* @param int $m
* @return array
*/
private function mFristAndLast($y=0,$m=0){
$y = $y ? $y : date('Y');
$e_m=$m ? $m :12;//如果月份不存在说明想获取一整年,取结束月份为12月
$m = $m ? $m :1;//如果月份不存在说明想获取一整年,取开始月份为1月
$d = date('t', strtotime($y.'-'.$m));
return array(strtotime($y.'-'.$m),mktime(23,59,59,$e_m,$d,$y));
}
/**
* ECHART业绩情况统计
* @param bool $is_ajax
* @return array|bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
private function getAchievementEchart($is_ajax = true,$group_id=0,$owner_user_id=0){
$startDate = $this->request->param('start_date', null);
$endDate = $this->request->param('end_date', null);
$owner_user_ids='';
if ($owner_user_id > 0) {
$owner_user_ids=$owner_user_id;
}elseif($group_id&&$group_id>0){
Tree::instance()->init($this->groupList);
$groupIds=Tree::instance()->getChildrenIds($group_id,true);
$authGroupList = \app\admin\model\AuthGroupAccess::
field('uid,group_id')
->where('group_id', 'in', $groupIds)
->select();
foreach ($authGroupList as $k => $v) {
if (in_array($v['uid'],$owner_user_ids)) continue;
$owner_user_ids[] = $v['uid'];
}
if (!$owner_user_ids){
$this->error("没有数据");
}
}
$contractModel = new \app\admin\model\facrm\Contract();
$data=$contractModel->getAchievementEchart($startDate,$endDate,$owner_user_ids);
if ($is_ajax) {
$this->success('', '', [
'achievement' => $data
]);
} else {
return $data;
}
}
/**
* Echart跟进趋势
* @return bool
*/
private function getRecordEchartData($is_ajax = true,$group_id=0,$owner_user_id=0)
{
$startDate = $this->request->param('start_date', null);
$endDate = $this->request->param('end_date', null);
$owner_user_ids='';
if ($owner_user_id > 0) {
$owner_user_ids=$owner_user_id;
}elseif($group_id&&$group_id>0){
Tree::instance()->init($this->groupList);
$groupIds=Tree::instance()->getChildrenIds($group_id,true);
$authGroupList = \app\admin\model\AuthGroupAccess::
field('uid,group_id')
->where('group_id', 'in', $groupIds)
->select();
foreach ($authGroupList as $k => $v) {
if (in_array($v['uid'],$owner_user_ids)) continue;
$owner_user_ids[] = $v['uid'];
}
if (!$owner_user_ids){
$this->error("没有数据");
}
}
$recordModel = new \app\admin\model\facrm\Record();
$data=$recordModel->getRecordEchartData($startDate,$endDate,$owner_user_ids);
if ($is_ajax) {
$this->success('', '', [
'record' => $data
]);
} else {
return $data;
}
}
}