@ -38,7 +38,7 @@ class Achievement extends ApiCommon
exit(json_encode(['code' => 102, 'error' => '无权操作']));
}
}
/**
* 业绩目标完成情况
*
@ -49,21 +49,20 @@ class Achievement extends ApiCommon
*/
public function statistics($param = '')
{
if($param['excel_type']!=1){
if ($param['excel_type'] != 1) {
$param = $this->param;
}
// $achievementModel = new \app\crm\model\Achievement();
// $list = $achievementModel->getList($param) ? : [];
$list = $this->getAchievementStatistics($param) ?: [];
//导出使用
if (!empty($param['excel_type'])) {
$list = $this->excelStatistics($param) ?: [];
return $list;
}
return resultArray(['data' => $list]);
}
/**
* 业绩目标完成情况列表
*
@ -77,26 +76,26 @@ class Achievement extends ApiCommon
{
# 结果数据
$result = [];
# 参数
$status = !empty($param['status']) ? $param['status'] : 1; # 类型: 1合同目标; 2回款目标;
$year = !empty($param['year']) ? $param['year'] : 0; # 年份
$status = !empty($param['status']) ? $param['status'] : 1; # 类型: 1合同目标; 2回款目标;
$year = !empty($param['year']) ? $param['year'] : 0; # 年份
$structureId = !empty($param['structure_id']) ? $param['structure_id'] : 0; # 部门
$userId = !empty($param['user_id']) ? $param['user_id'] : 0; # 员工
$type = !empty($param['type']) ? $param['type'] : 1; # 类型: 1部门; 2员工
$userId = !empty($param['user_id']) ? $param['user_id'] : 0; # 员工
$type = !empty($param['type']) ? $param['type'] : 1; # 类型: 1部门; 2员工
# 设置业绩目标条件
$achievementWhere['year'] = $year;
$achievementWhere['status'] = $status;
$achievementWhere['type'] = !empty($type) & & $type == 1 ? 2 : 3;
if (!empty($userId)) $achievementWhere['obj_id'] = $userId;
if (!empty($structureId)) $achievementWhere['obj_id'] = $structureId;
# 查询业绩目标数据
$achievementList = Db::name('crm_achievement')->where($achievementWhere)->select();
if (empty($achievementList)) return [];
# 部门
if ($type == 1) {
foreach ($achievementList as $key => $value) {
@ -118,50 +117,50 @@ class Achievement extends ApiCommon
'12' => ['achievement' => (int)$value['december'], 'money' => 0, 'rate' => 0, 'month' => '十二月']
]
];
# 获取部门下的员工ID
$userIds = Db::name('admin_user')->where('structure_id', $value['obj_id'])->column('id');
# 业绩完成字段
$finishField = ["DATE_FORMAT(FROM_UNIXTIME(`create_time`,'%Y-%m-%d'),'%m') AS time", 'sum(money) AS money'];
# 业绩完成条件
$finishWhere['check_status'] = 2;
$finishWhere['owner_user_id'] = ['in', $userIds];
# 合同
if ($status == 1) {
$finishArray = Db::name('crm_contract')->field($finishField)->where($finishWhere)->group('time')->select();
}
# 回款
if ($status == 2) {
$finishArray = Db::name('crm_receivables')->field($finishField)->where($finishWhere)->group('time')->select();
}
# 计算完成情况
foreach ($finishArray as $k => $v) {
if (!empty($result[$value['obj_id']]['list'][$v['time']])) {
$achievement = $result[$value['obj_id']]['list'][$v['time']]['achievement'];
$result[$value['obj_id']]['list'][$v['time']]['money'] = (int)$v['money'];
$result[$value['obj_id']]['list'][$v['time']]['rate'] = (int)(($v['money'] / $achievement) * 100);
}
}
$result[$value['obj_id']]['list'] = array_values($result[$value['obj_id']]['list']);
}
}
# 员工
if ($type == 2) {
$userData = [];
$userList = db('admin_user')->field(['id', 'realname'])->select();
foreach ($userList AS $key => $value) {
foreach ($userList as $key => $value) {
$userData[$value['id']] = $value['realname'];
}
foreach ($achievementList as $key => $value) {
# 组装结果数据
$result[$value['obj_id']] = [
@ -181,225 +180,221 @@ class Achievement extends ApiCommon
'12' => ['achievement' => (int)$value['december'], 'money' => 0, 'rate' => 0, 'month' => '十二月']
]
];
# 业绩完成字段
$finishField = ["DATE_FORMAT(FROM_UNIXTIME(`create_time`,'%Y-%m-%d'),'%m') AS time", 'sum(money) AS money'];
# 业绩完成条件
$finishWhere = ['check_status' => 2, 'owner_user_id' => $value['obj_id']];
# 合同
if ($status == 1) {
$finishArray = Db::name('crm_contract')->field($finishField)->where($finishWhere)->group('time')->select();
}
# 回款
if ($status == 2) {
$finishArray = Db::name('crm_receivables')->field($finishField)->where($finishWhere)->group('time')->select();
}
# 计算完成情况
foreach ($finishArray as $k => $v) {
if (!empty($result[$value['obj_id']]['list'][$v['time']])) {
$achievement = $result[$value['obj_id']]['list'][$v['time']]['achievement'];
$result[$value['obj_id']]['list'][$v['time']]['money'] = (int)$v['money'];
$result[$value['obj_id']]['list'][$v['time']]['rate'] = (int)(($v['money'] / $achievement) * 100);
}
}
$result[$value['obj_id']]['list'] = array_values($result[$value['obj_id']]['list']);
}
}
return array_values($result);
}
public function excelStatistics($param)
{
# 结果数据
$result = [];
# 参数
$status = !empty($param['status']) ? $param['status'] : 1; # 类型: 1合同目标; 2回款目标;
$year = !empty($param['year']) ? $param['year'] : 0; # 年份
$structureId = !empty($param['structure_id']) ? $param['structure_id'] : 0; # 部门
$userId = !empty($param['user_id']) ? $param['user_id'] : 0; # 员工
$type = !empty($param['type']) ? $param['type'] : 1; # 类型: 1部门; 2员工
# 设置业绩目标条件
$achievementWhere['year'] = $year;
$achievementWhere['status'] = $status;
$achievementWhere['type'] = !empty($type) & & $type == 1 ? 2 : 3;
if (!empty($userId)) $achievementWhere['obj_id'] = $userId;
if (!empty($structureId)) $achievementWhere['obj_id'] = $structureId;
# 查询业绩目标数据
$achievementList = Db::name('crm_achievement')->where($achievementWhere)->select();
if (empty($achievementList)) return [];
# 部门
if ($type == 1) {
foreach ($achievementList as $key => $value) {
# 组装结果数据
$result[] = [
['name' => $value['name'], 'achievement' => (int)$value['january'], 'money' => 0, 'rate' => 0, 'month' => '一月'],
['name' => $value['name'], 'achievement' => (int)$value['february'], 'money' => 0, 'rate' => 0, 'month' => '二月'],
['name' => $value['name'], 'achievement' => (int)$value['march'], 'money' => 0, 'rate' => 0, 'month' => '三月'],
['name' => '', 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第一季度'],
['name' => $value['name'], 'achievement' => (int)$value['april'], 'money' => 0, 'rate' => 0, 'month' => '四月'],
['name' => $value['name'], 'achievement' => (int)$value['may'], 'money' => 0, 'rate' => 0, 'month' => '五月'],
['name' => $value['name'], 'achievement' => (int)$value['june'], 'money' => 0, 'rate' => 0, 'month' => '六月'],
['name' => $value['name'], 'achievement' => (int)$value['april'], 'money' => 0, 'rate' => 0, 'month' => '第二季度'],
['name' => $value['name'], 'achievement' => (int)$value['july'], 'money' => 0, 'rate' => 0, 'month' => '七月'],
['name' => $value['name'], 'achievement' => (int)$value['august'], 'money' => 0, 'rate' => 0, 'month' => '八月'],
['name' => $value['name'], 'achievement' => (int)$value['september'], 'money' => 0, 'rate' => 0, 'month' => '九月'],
['name' => $value['name'], 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第三季度'],
['name' => $value['name'], 'achievement' => (int)$value['october'], 'money' => 0, 'rate' => 0, 'month' => '十月'],
['name' => $value['name'], 'achievement' => (int)$value['november'], 'money' => 0, 'rate' => 0, 'month' => '十一月'],
['name' => $value['name'], 'achievement' => (int)$value['december'], 'money' => 0, 'rate' => 0, 'month' => '十二月'],
['name' => $value['name'], 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第四季度'],
['name' => $value['name'], 'achievement' => (int)$value['yeartarget'], 'money' => 0, 'rate' => 0, 'month' => '全年'],
$result[$value['obj_id']] = [
'list' => [
'01' => ['name' => $value['name'] ,'achievement' => (int)$value['january'], 'money' => 0, 'rate' => 0, 'month' => '一月'],
'02' => ['name' => $value['name'] ,'achievement' => (int)$value['february'], 'money' => 0, 'rate' => 0, 'month' => '二月'],
'03' => ['name' => $value['name'] ,'achievement' => (int)$value['march'], 'money' => 0, 'rate' => 0, 'month' => '三月'],
'04' => ['name' => '', 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第一季度'],
'05' => ['name' => $value['name'] ,'achievement' => (int)$value['april'], 'money' => 0, 'rate' => 0, 'month' => '四月'],
'06' => ['name' => $value['name'] ,'achievement' => (int)$value['may'], 'money' => 0, 'rate' => 0, 'month' => '五月'],
'07' => ['name' => $value['name'] ,'achievement' => (int)$value['june'], 'money' => 0, 'rate' => 0, 'month' => '六月'],
'08' => ['name' => '', 'achievement' => (int)$value['april'], 'money' => 0, 'rate' => 0, 'month' => '第二季度'],
'09' => ['name' => $value['name'] ,'achievement' => (int)$value['july'], 'money' => 0, 'rate' => 0, 'month' => '七月'],
'10' => ['name' => $value['name'] ,'achievement' => (int)$value['august'], 'money' => 0, 'rate' => 0, 'month' => '八月'],
'11' => ['name' => $value['name'] ,'achievement' => (int)$value['september'], 'money' => 0, 'rate' => 0, 'month' => '九月'],
'12' => ['name' => '', 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第三季度'],
'13' => ['name' => $value['name'] ,'achievement' => (int)$value['october'], 'money' => 0, 'rate' => 0, 'month' => '十月'],
'14' => ['name' => $value['name'] ,'achievement' => (int)$value['november'], 'money' => 0, 'rate' => 0, 'month' => '十一月'],
'15' => ['name' => $value['name'] ,'achievement' => (int)$value['december'], 'money' => 0, 'rate' => 0, 'month' => '十二月'],
'16' => ['name' => '', 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第四季度'],
'17' => ['name' => '', 'achievement' => (int)$value['yeartarget'], 'money' => 0, 'rate' => 0, 'month' => '全年'],
]
];
# 获取部门下的员工ID
$userIds = Db::name('admin_user')->where('structure_id', $value['obj_id'])->column('id');
# 业绩完成字段
$finishField = ["DATE_FORMAT(FROM_UNIXTIME(`create_time`,'%Y-%m-%d'),'%m') AS time", 'sum(money) AS money'];
# 业绩完成条件
$finishWhere['check_status'] = 2;
$finishWhere['owner_user_id'] = ['in', $userIds];
# 合同
if ($status == 1) {
$finishArray = Db::name('crm_contract')->field($finishField)->where($finishWhere)->group('time')->select();
}
# 回款
if ($status == 2) {
$finishArray = Db::name('crm_receivables')->field($finishField)->where($finishWhere)->group('time')->select();
}
# 计算完成情况
foreach ($finishArray as $k => $v) {
if (!empty($result[$v['time']])) {
$achievement = $result[$v['time']]['achievement'];
$result[$v['time']]['money'] = (int)$v['money'];
$result[$v['time']]['rate'] = (int)(($v['money'] / $achievement) * 100);
if (!empty($result[$value['obj_id']]['list'][$v['time']])) {
$achievement = $result[$value['obj_id']]['list'][$v['time']]['achievement'];
$result[$value['obj_id']]['list'][$v['time']]['money'] = (int)$v['money'];
$result[$value['obj_id']]['list'][$v['time']]['rate'] = (int)(($v['money'] / $achievement) * 100);
}
}
foreach ($result as & $val){
$val[3]['money']=$val[0]['money']+$val[1]['money']+$val[2]['money'];
$val[3]['rate']=$val[0]['rate']+$val[1]['rate']+$val[2]['rate'];
$val[7]['money']=$val[4]['money']+$val[5]['money']+$val[6]['money'];
$val[7]['rate']=$val[4]['rate']+$val[5]['rate']+$val[6]['rate'];
$val[11]['money']=$val[7]['money']+$val[9]['money']+$val[10]['money'];
$val[11]['rate']=$val[8]['rate']+$val[9]['rate']+$val[10]['rate'];
$val[15]['money']=$val[12]['money']+$val[13]['money']+$val[14]['money'];
$val[15]['rate']=$val[12]['rate']+$val[13]['rate']+$val[14]['rate'];
$val[15]['money']=$val[12]['money']+$val[13]['money']+$val[14]['money'];
$val[15]['rate']=$val[12]['rate']+$val[13]['rate']+$val[14]['rate'];
$val[16]['money']=$val[3]['money']+$val[7]['money']+$val[11]['money']+$val[15]['money'];
$val[16]['rate']=$val[3]['rate']+$val[7]['rate']+$val[11]['rate']+$val[15]['rate'];
}
$result = array_values($result);
$result[$value['obj_id']]['list'] = array_values($result[$value['obj_id']]['list']);
$item[]=$result[$value['obj_id']]['list'];
}
}
# 员工
if ($type == 2) {
foreach ($achievementList AS $key => $value) {
$userData = [];
$userList = db('admin_user')->field(['id', 'realname'])->select();
foreach ($userList as $key => $value) {
$userData[$value['id']] = $value['realname'];
}
foreach ($achievementList as $key => $value) {
# 组装结果数据
$result[] = [
['name' => $value['name'], 'achievement' => (int)$value['january'], 'money' => 0, 'rate' => 0, 'month' => '一月'],
['name' => $value['name'], 'achievement' => (int)$value['february'], 'money' => 0, 'rate' => 0, 'month' => '二月'],
['name' => $value['name'], 'achievement' => (int)$value['march'], 'money' => 0, 'rate' => 0, 'month' => '三月'],
['name' => '', 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第一季度'],
['name' => $value['name'], 'achievement' => (int)$value['april'], 'money' => 0, 'rate' => 0, 'month' => '四 月'],
['name' => $value['name'], 'achievement' => (int)$value['may'], 'money' => 0, 'rate' => 0, 'month' => '五 月'],
['name' => $value['name'], 'achievement' => (int)$value['june'], 'money' => 0, 'rate' => 0, 'month' => '六月'],
['name' => $value['name'], 'achievement' => (int)$value['april'], 'money' => 0, 'rate' => 0, 'month' => '第二季度'],
['name' => $value['name'], 'achievement' => (int)$value['july'], 'money' => 0, 'rate' => 0, 'month' => '七 月'],
['name' => $value['name'], 'achievement' => (int)$value['augu st'], 'money' => 0, 'rate' => 0, 'month' => '八 月'],
['name' => $value['name'], 'achievement' => (int)$value['september'], 'money' => 0, 'rate' => 0, 'month' => '九月'],
['name' => $value['name'], 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第三季度'],
['name' => $value['name'], 'achievement' => (int)$value['octo ber'], 'money' => 0, 'rate' => 0, 'month' => '十月'],
['name' => $value['name'], 'achievement' => (int)$value['november'], 'money' => 0, 'rate' => 0, 'month' => '十一 月'],
['name' => $value['name'], 'achievement' => (int)$value['december'], 'money' => 0, 'rate' => 0, 'month' => '十二月'],
['name' => $value['name'], 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第四季度 '],
['name' => $value['name' ], 'achievement' => (int)$value['yeartarget'], 'money' => 0, 'rate' => 0, 'month' => '全年'],
$result[$value['obj_id']] = [
'list' => [
'01' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']],'achievement' => (int)$value['january'], 'money' => 0, 'rate' => 0, 'month' => '一月'],
'02' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']],'achievement' => (int)$value['february'], 'money' => 0, 'rate' => 0, 'month' => '二 月'],
'03' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']],'achievement' => (int)$value['march'], 'money' => 0, 'rate' => 0, 'month' => '三 月'],
'04' => ['name' => '', 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第一季度'],
'05' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']],'achievement' => (int)$value['april'], 'money' => 0, 'rate' => 0, 'month' => '四月'],
'06' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']],'achievement' => (int)$value['may'], 'money' => 0, 'rate' => 0, 'month' => '五 月'],
'07' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']],'achievement' => (int)$value['june'], 'money' => 0, 'rate' => 0, 'month' => '六 月'],
'08' => ['name' => '', 'achievement' => (int)$value['april'], 'money' => 0, 'rate' => 0, 'month' => '第二季度'],
'09' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']],'achievement' => (int)$value['july'], 'money' => 0, 'rate' => 0, 'month' => '七月'],
'10' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']],'achievement' => (int)$value['august'], 'money' => 0, 'rate' => 0, 'month' => '八 月'],
'11' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']], 'achievement' => (int)$value['sep tember '], 'money' => 0, 'rate' => 0, 'month' => '九 月'],
'12' => ['name' => '', 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第三季度'],
'13' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']],'achievement' => (int)$value['october'], 'money' => 0, 'rate' => 0, 'month' => '十月'],
'14' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']],'achievement' => (int)$value['novem ber'], 'money' => 0, 'rate' => 0, 'month' => '十一 月'],
'15' => ['name' =>!empty($value['name']) ? $value['name'] : $userData[$value['obj_id']],'achievement' => (int)$value['december'], 'money' => 0, 'rate' => 0, 'month' => '十二 月'],
'16' => ['name' => '', 'achievement' => 0, 'money' => 0, 'rate' => 0, 'month' => '第四季度'],
'17' => ['name' => '', 'achievement' => (int)$value['yeartarget'], 'money' => 0, 'rate' => 0, 'month' => '全年 '],
]
];
# 业绩完成字段
$finishField = ["DATE_FORMAT(FROM_UNIXTIME(`create_time`,'%Y-%m-%d'),'%m') AS time", 'sum(money) AS money'];
# 业绩完成条件
$finishWhere = ['check_status' => 2, 'owner_user_id' => $value['obj_id']];
# 合同
if ($status == 1) {
$finishArray = Db::name('crm_contract')->field($finishField)->where($finishWhere)->group('time')->select();
}
# 回款
if ($status == 2) {
$finishArray = Db::name('crm_receivables')->field($finishField)->where($finishWhere)->group('time')->select();
}
# 计算完成情况
foreach ($finishArray AS $k => $v) {
if (!empty($result[$v['time']])) {
$achievement = $result[$v['time']]['achievement'];
$result[$v['time']]['money'] = (int)$v['money'];
$result[$v['time']]['rate'] = (int)(($v['money'] / $achievement) * 100);
foreach ($finishArray as $k => $v) {
if (!empty($result[$value['obj_id']]['list'][$v ['time']])) {
$achievement = $result[$value['obj_id']]['list'][$v ['time']]['achievement'];
$result[$value['obj_id']]['list'][$v ['time']]['money'] = (int)$v['money'];
$result[$value['obj_id']]['list'][$v ['time']]['rate'] = (int)(($v['money'] / $achievement) * 100);
}
}
foreach ($result as & $val){
$val[3]['money']=$val[0]['money']+$val[1]['money']+$val[2]['money'];
$val[3]['rate']=$val[0]['rate']+$val[1]['rate']+$val[2]['rate'];
$val[7]['money']=$val[4]['money']+$val[5]['money']+$val[6]['money'];
$val[7]['rate']=$val[4]['rate']+$val[5]['rate']+$val[6]['rate'];
$val[11]['money']=$val[7]['money']+$val[9]['money']+$val[10]['money'];
$val[11]['rate']=$val[8]['rate']+$val[9]['rate']+$val[10]['rate'];
$val[15]['money']=$val[12]['money']+$val[13]['money']+$val[14]['money'];
$val[15]['rate']=$val[12]['rate']+$val[13]['rate']+$val[14]['rate'];
$val[15]['money']=$val[12]['money']+$val[13]['money']+$val[14]['money'];
$val[15]['rate']=$val[12]['rate']+$val[13]['rate']+$val[14]['rate'];
$val[16]['money']=$val[3]['money']+$val[7]['money']+$val[11]['money']+$val[15]['money'];
$val[16]['rate']=$val[3]['rate']+$val[7]['rate']+$val[11]['rate']+$val[15]['rate'];
}
$result = array_values($result);
$result[$value['obj_id']]['list'] = array_values($result[$value['obj_id']]['list']);
$item[]=$result[$value['obj_id']]['list'];
}
}
return array_values($result);
foreach ($item as & $val){
$val[3]['money']=$val[0]['money']+$val[1]['money']+$val[2]['money'];
$val[3]['rate']=$val[0]['rate']+$val[1]['rate']+$val[2]['rate'];
$val[3]['achievement']=$val[0]['achievement']+$val[1]['achievement']+$val[2]['achievement'];
$val[7]['money']=$val[4]['money']+$val[5]['money']+$val[6]['money'];
$val[7]['rate']=$val[4]['rate']+$val[5]['rate']+$val[6]['rate'];
$val[7]['achievement']=$val[4]['achievement']+$val[5]['achievement']+$val[6]['achievement'];
$val[11]['money']=$val[7]['money']+$val[9]['money']+$val[10]['money'];
$val[11]['rate']=$val[8]['rate']+$val[9]['rate']+$val[10]['rate'];
$val[11]['achievement']=$val[8]['achievement']+$val[9]['achievement']+$val[10]['achievement'];
$val[15]['money']=$val[12]['money']+$val[13]['money']+$val[14]['money'];
$val[15]['rate']=$val[12]['rate']+$val[13]['rate']+$val[14]['rate'];
$val[15]['achievement']=$val[12]['achievement']+$val[13]['achievement']+$val[14]['achievement'];
$val[16]['money']=$val[3]['money']+$val[7]['money']+$val[11]['money']+$val[15]['money'];
$val[16]['rate']=$val[3]['rate']+$val[7]['rate']+$val[11]['rate']+$val[15]['rate'];
}
return $item;
}
/**
* 导出
* @param $type
@ -409,11 +404,11 @@ class Achievement extends ApiCommon
{
$param = $this->param;
$list = $this->statistics($param);
if(empty($list)){
return resultArray(['data'=>'数据不存在']);
if (empty($list)) {
return resultArray(['data' => '数据不存在']);
}
$excelLogic = new ExcelLogic();
$data = $excelLogic->achienementExcel($param, $ list);
$data = $excelLogic->achienementExcel($list);
return $data;
}
}