public function analytics()
 {
     $m_leads = M('leads');
     if ($_GET['role']) {
         $role_id = intval($_GET['role']);
     } else {
         $role_id = 'all';
     }
     if ($_GET['department'] && $_GET['department'] != 'all') {
         $department_id = intval($_GET['department']);
     } else {
         $department_id = D('RoleView')->where('role.role_id = %d', session('role_id'))->getField('department_id');
     }
     if ($_GET['start_time']) {
         $start_time = strtotime(date('Y-m-d', strtotime($_GET['start_time'])));
     }
     $end_time = $_GET['end_time'] ? strtotime(date('Y-m-d 23:59:59', strtotime($_GET['end_time']))) : strtotime(date('Y-m-d 23:59:59', time()));
     if ($role_id == "all") {
         $roleList = getRoleByDepartmentId($department_id);
         $role_id_array = array();
         foreach ($roleList as $v2) {
             $role_id_array[] = $v2['role_id'];
         }
         $where_source['creator_role_id'] = array('in', implode(',', $role_id_array));
         $where_status['owner_role_id'] = array('in', implode(',', $role_id_array));
     } else {
         $where_source['creator_role_id'] = $role_id;
         $where_status['owner_role_id'] = $role_id;
     }
     if ($start_time) {
         $where_source['create_time'] = array(array('elt', $end_time), array('egt', $start_time), 'and');
         $where_status['create_time'] = array(array('elt', $end_time), array('egt', $start_time), 'and');
     } else {
         $where_source['create_time'] = array('elt', $end_time);
         $where_status['create_time'] = array('elt', $end_time);
     }
     //线索来源统计
     $setting = M('Fields')->where("model = 'leads' and field = 'source'")->getField('setting');
     $setting_str = '$revenueList=' . $setting . ';';
     //eval($setting_str);
     $source_count_array = array();
     $sourceList = M('leads')->field('count(1) as num , source')->group('source')->where($where_source)->select();
     foreach ($sourceList as $v) {
         $source = $v['source'] ? $v['source'] : L('OTHER');
         $source_count[$source] = $v['num'];
     }
     foreach ($revenueList['data'] as $v) {
         if ($source_count[$v]) {
             $source_count_array[] = '["' . $v . '",' . $source_count[$v] . ']';
         } else {
             $source_count_array[] = '["' . $v . '",0]';
         }
     }
     $this->source_count = implode(',', $source_count_array);
     //Statistics Content
     $role_id_array = array();
     if ($role_id == "all") {
         if ($department_id != "all") {
             if (session('?admin')) {
                 $roleList = M('role')->where('user_id <> 0')->getField('role_id', true);
             } else {
                 $roleList = getRoleByDepartmentId($department_id);
             }
             //$roleList = getRoleByDepartmentId($department_id);
             foreach ($roleList as $v) {
                 $role_id_array[] = $v;
             }
         } else {
             $role_id_array = getSubRoleId();
         }
     } else {
         $role_id_array[] = $role_id;
     }
     if ($start_time) {
         $create_time = array(array('elt', $end_time), array('egt', $start_time), 'and');
     } else {
         $create_time = array('elt', $end_time);
     }
     $add_count_total = 0;
     $own_count_total = 0;
     $success_count_total = 0;
     $deal_count_total = 0;
     foreach ($role_id_array as $v) {
         $user = getUserByRoleId($v);
         $add_count = $m_leads->where(array('is_deleted' => 0, 'creator_role_id' => $v, 'create_time' => $create_time))->count();
         $own_count = $m_leads->where(array('is_deleted' => 0, 'owner_role_id' => $v, 'create_time' => $create_time))->count();
         $success_count = $m_leads->where(array('is_deleted' => 0, 'is_transformed' => 1, 'owner_role_id' => $v, 'create_time' => $create_time))->count();
         $deal_count = $m_leads->where('is_deleted = 0 and owner_role_id = %d and is_transformed != 1 and update_time>create_time', $v)->count();
         $reportList[] = array("user" => $user, "add_count" => $add_count, "own_count" => $own_count, "success_count" => $success_count, "deal_count" => $deal_count);
         $add_count_total += $add_count;
         $own_count_total += $own_count;
         $success_count_total += $success_count;
         $deal_count_total += $deal_count;
     }
     $this->total_report = array("add_count" => $add_count_total, "own_count" => $own_count_total, "success_count" => $success_count_total, "deal_count" => $deal_count_total);
     $this->reportList = $reportList;
     if (session('?admin')) {
         $idArray = M('role')->where('user_id <> 0')->getField('role_id', true);
     } else {
         $idArray = getSubRoleId();
     }
     $roleList = array();
     foreach ($idArray as $roleId) {
         $roleList[$roleId] = getUserByRoleId($roleId);
     }
     $this->roleList = $roleList;
     $departments = M('roleDepartment')->select();
     $departmentList[] = M('roleDepartment')->where('department_id = %d', session('department_id'))->find();
     $departmentList = array_merge($departmentList, getSubDepartment(session('department_id'), $departments, ''));
     $this->assign('departmentList', $departmentList);
     $this->alert = parseAlert();
     $this->display();
 }
 public function user_role_relation()
 {
     if (!session('?name') || !session('?user_id')) {
         redirect(U('User/login/'), 0, L('PLEASE_LOGIN_FIRSET'));
     }
     //用户添加到岗位
     if ($_GET['by'] == 'user_role') {
         if ($_GET['id']) {
             $this->user = M('User')->where('user_id = %d', $_GET['id'])->find();
             //占位符操作 %d整型 %f浮点型 %s字符串
             $department = M('roleDepartment');
             $department_list = $department->select();
             $departmentList = getSubDepartment(0, $department_list, '');
             $role = M('Role');
             foreach ($departmentList as $key => $value) {
                 $roleList = $role->where('department_id =' . $value['department_id'])->select();
                 $departmentList[$key]['roleList'] = $roleList;
             }
             $this->assign('departmentList', $departmentList);
             $this->display('User:user_role');
         } elseif ($_POST['user_id']) {
             $m_user = M('user');
             $user = $m_user->where('user_id = %d', $_POST['user_id'])->find();
             if ($user['status'] == 0) {
                 alert('error', L('GRANT_PERMISSION_FAILED_FOR_NOT_PASS_AUDIT', array($user['name'])), $_SERVER['HTTP_REFERER']);
             } elseif ($user['status'] == -1) {
                 alert('error', L('GRANT_PERMISSION_FAILED_FOR_NOT_PASS_AUDIT', array($user['name'])), $_SERVER['HTTP_REFERER']);
             } else {
                 $role_ids = is_array($_POST['role']) ? implode(',', $_POST['role']) : '';
                 $m_role = M('role');
                 $m_role->where("role_id in ('%s')", $role_ids)->setField('user_id', $_POST['user_id']);
                 $m_role->where("role_id not in ('%s') and user_id=%d", $role_ids, $_POST['user_id'])->setField('user_id', '');
                 alert('success', L('EDIT_SOMEONE_POSITION_SUCCESS', array($user['name'])), $_SERVER['HTTP_REFERER']);
             }
         } else {
             alert('error', L('PARAMETER_ERROR'), $_SERVER['HTTP_REFERER']);
         }
         //岗位添加用户
     } else {
         if ($_GET['by'] == 'role_user') {
             $role = M('role');
             if ($_GET['role_id']) {
                 $this->role = $role->where('role_id = %d', $_GET['role_id'])->find();
                 $this->userList = M('user')->where('status = %d', 1)->select();
                 $this->display('User:role_user_add');
             } elseif ($_POST['role_id']) {
                 $role->create();
                 $m_user = M('user');
                 $user = $m_user->where('user_id = %d', $_POST['user_id'])->find();
                 if (!$user['role_id']) {
                     $m_user->where('user_id = %d', $_POST['user_id'])->setField('role_id', $_POST['role_id']);
                 }
                 if ($role->save()) {
                     alert('success', L('SETTING_SUCCESS'), $_SERVER['HTTP_REFERER']);
                 } else {
                     alert('error', L('SETTING_FAILED'), $_SERVER['HTTP_REFERER']);
                 }
             }
         }
     }
 }
 public function authorize()
 {
     if ($_GET['by'] == 'permission') {
         if ($_POST['control_id']) {
             $roleList = $_POST['roleList'];
             $permission = M('UserPermission');
             $data['control_id'] = $_POST['control_id'];
             $temp = $permission->where('control_id =%s', $_POST['control_id'])->select();
             $idList = array();
             foreach ($temp as $value) {
                 $idList[] = $value['role_id'];
             }
             $add_permission = array_diff($roleList, $idList);
             $delete_permission = array_diff($idList, $roleList);
             if (!empty($add_permission)) {
                 foreach ($add_permission as $value) {
                     $data['role_id'] = $value;
                     if ($permission->add($data)) {
                     } else {
                         alert('error', L('OPERATION_SAVE_FAILED'), $_SERVER['HTTP_REFERER']);
                     }
                 }
             }
             if (!empty($delete_permission)) {
                 foreach ($delete_permission as $value) {
                     if ($permission->where('control_id = %d and role_id = %d', $_POST['control_id'], $value)->delete()) {
                     } else {
                         alert('error', L('OPERATION_SAVE_FAILED'), $_SERVER['HTTP_REFERER']);
                     }
                 }
             }
             alert('success', L('OPERATION_SAVE_SUCCESSED'), $_SERVER['HTTP_REFERER']);
         } else {
             if ($_GET['control_id']) {
                 $where = isset($_GET['control_id']) ? 'control_id =' . $_GET['control_id'] : '';
                 $role = M('UserRole');
                 $role_list = array();
                 $permission = M('UserPermission');
                 $department = M('UserDepartment');
                 $permissionList = $permission->where($where)->select();
                 $department_temp = $department->select();
                 $department_list = getSubDepartment(0, $department_temp, '');
                 //按部门查询岗位
                 foreach ($department_list as $key => $value) {
                     $role_list = $role->where('department_id = %d', $value['department_id'])->select();
                     foreach ($role_list as $key2 => $value2) {
                         foreach ($permissionList as $key3 => $value3) {
                             if ($value2['role_id'] == $value3['role_id']) {
                                 $role_list[$key2]['checked'] = 'checked';
                             }
                         }
                     }
                     $department_list[$key]['role'] = $role_list;
                 }
                 $this->temp = $_GET['control_id'];
                 $this->roleList = $department_list;
                 $this->display();
             } else {
                 alert('error', L('OPERATION_SAVE_FAILED'), $_SERVER['HTTP_REFERER']);
             }
         }
     } elseif ($_GET['by'] == 'user') {
         if ($_POST['role_id']) {
             $controlList = $_POST['controlList'];
             $permission = M('UserPermission');
             $data['role_id'] = $_POST['role_id'];
             $temp = $permission->where('role_id =%s', $_POST['role_id'])->select();
             $idList = array();
             foreach ($temp as $value) {
                 $idList[] = $value['control_id'];
             }
             $add_permission = array_diff($controlList, $idList);
             $delete_permission = array_diff($idList, $controlList);
             if (!empty($add_permission)) {
                 foreach ($add_permission as $value) {
                     $data['control_id'] = $value;
                     if ($permission->add($data)) {
                     } else {
                         alert('error', L('OPERATION_SAVE_FAILED'), $_SERVER['HTTP_REFERER']);
                     }
                 }
             }
             if (!empty($delete_permission)) {
                 foreach ($delete_permission as $value) {
                     if ($permission->where('control_id = %d and role_id = %d', $value, $_POST['role_id'])->delete()) {
                     } else {
                         alert('error', L('OPERATION_SAVE_FAILED'), $_SERVER['HTTP_REFERER']);
                     }
                 }
             }
             alert('success', L('OPERATION_SAVE_SUCCESSED'), $_SERVER['HTTP_REFERER']);
         } else {
             if ($_GET['role_id']) {
                 $where = isset($_GET['role_id']) ? 'role_id = ' . $_GET['role_id'] : '';
                 $module = M('ControlModule');
                 $permission = M('UserPermission');
                 $control = M('Control');
                 $controlList = $module->select();
                 $existsList = $permission->where($where)->select();
                 foreach ($controlList as $key => $value) {
                     $controls = $control->where('module_id = %d', $value['module_id'])->select();
                     foreach ($existsList as $key2 => $value2) {
                         foreach ($controls as $key3 => $value3) {
                             if ($value2['control_id'] == $value3['control_id']) {
                                 $controls[$key3]['checked'] = 'checked';
                             }
                         }
                     }
                     $controlList[$key]['control'] = $controls;
                 }
                 $role = M('UserRole');
                 $this->temp = $role->where('role_id =' . $_GET['role_id'])->find();
                 $this->controlList = $controlList;
                 $this->display('User:authorize');
             } else {
                 alert('error', L('SAVE_FAILED_PARAMETER_ERRORS'), $_SERVER['HTTP_REFERER']);
             }
         }
     }
 }
 /**
  *商机统计
  *
  **/
 public function analytics()
 {
     $m_business = M('Business');
     if ($_GET['role']) {
         $role_id = intval($_GET['role']);
     } else {
         $role_id = 'all';
     }
     if ($_GET['department'] && $_GET['department'] != 'all') {
         $department_id = intval($_GET['department']);
     } else {
         $department_id = D('RoleView')->where('role.role_id = %d', session('role_id'))->getField('department_id');
     }
     if ($_GET['start_time']) {
         $start_time = strtotime(date('Y-m-d', strtotime($_GET['start_time'])));
     }
     $end_time = $_GET['end_time'] ? strtotime(date('Y-m-d 23:59:59', strtotime($_GET['end_time']))) : strtotime(date('Y-m-d 23:59:59', time()));
     if ($role_id == "all") {
         $roleList = getRoleByDepartmentId($department_id);
         $role_id_array = array();
         foreach ($roleList as $v2) {
             $role_id_array[] = $v2['role_id'];
         }
         $where_source['creator_role_id'] = array('in', implode(',', $role_id_array));
         $where_status['owner_role_id'] = array('in', implode(',', $role_id_array));
         $where_money['owner_role_id'] = array('in', implode(',', $role_id_array));
         $where_day_create['creator_role_id'] = array('in', implode(',', $role_id_array));
         $where_day_success['owner_role_id'] = array('in', implode(',', $role_id_array));
     } else {
         $where_source['creator_role_id'] = $role_id;
         $where_status['owner_role_id'] = $role_id;
         $where_money['owner_role_id'] = $role_id;
         $where_day_create['creator_role_id'] = array('in', implode(',', $role_id_array));
         $where_day_success['owner_role_id'] = array('in', implode(',', $role_id_array));
     }
     if ($start_time) {
         $where_source['create_time'] = array(array('lt', $end_time), array('gt', $start_time), 'and');
         $where_status['create_time'] = array(array('lt', $end_time), array('gt', $start_time), 'and');
         $where_money['create_time'] = array(array('lt', $end_time), array('gt', $start_time), 'and');
     } else {
         $where_source['create_time'] = array('lt', $end_time);
         $where_status['create_time'] = array('lt', $end_time);
         $where_money['create_time'] = array('lt', $end_time);
     }
     //统计表内容
     $role_id_array = array();
     if ($role_id == "all") {
         if ($department_id != "all") {
             if (session('?admin')) {
                 $roleList = M('role')->where('user_id <> 0')->getField('role_id', true);
             } else {
                 $roleList = getRoleByDepartmentId($department_id);
             }
             foreach ($roleList as $v) {
                 $role_id_array[] = $v;
             }
         } else {
             $role_id_array = getSubRoleId();
         }
     } else {
         $role_id_array[] = $role_id;
     }
     if ($start_time) {
         $create_time = array(array('lt', $end_time), array('gt', $start_time), 'and');
     } else {
         $create_time = array('lt', $end_time);
     }
     $add_count_total = 0;
     $own_count_total = 0;
     $success_count_total = 0;
     $deal_count_total = 0;
     foreach ($role_id_array as $v) {
         $user = getUserByRoleId($v);
         $add_count = $m_business->where(array('is_deleted' => 0, 'creator_role_id' => $v, 'create_time' => $create_time))->count();
         $own_count = $m_business->where(array('is_deleted' => 0, 'owner_role_id' => $v, 'create_time' => $create_time))->count();
         $success_count = $m_business->where(array('is_deleted' => 0, 'status_id' => 100, 'owner_role_id' => $v, 'create_time' => $create_time))->count();
         $deal_count = $m_business->where('is_deleted = 0 and status_id not in(99,100) and owner_role_id = %d and update_time>create_time', $v)->count();
         $reportList[] = array("user" => $user, "add_count" => $add_count, "own_count" => $own_count, "success_count" => $success_count, "deal_count" => $deal_count);
         $add_count_total += $add_count;
         $own_count_total += $own_count;
         $success_count_total += $success_count;
         $deal_count_total += $deal_count;
     }
     //商机来源统计图
     $source_count_array = array();
     $setting = M('Fields')->where("model = 'business' and field = 'origin'")->getField('setting');
     $setting_str = '$sourceList=' . $setting . ';';
     eval($setting_str);
     $where_source['is_deleted'] = 0;
     $source_total_count = 0;
     foreach ($sourceList['data'] as $v) {
         unset($where_source['origin']);
         $where_source['origin'] = $v;
         $target_count = $m_business->where($where_source)->count();
         $source_count_array[] = '[' . '"' . $v . '",' . $target_count . ']';
         $source_total_count += $target_count;
     }
     $source_count_array[] = '["' . L('OTHER') . '",' . ($add_count_total - $source_total_count) . ']';
     $this->source_count = implode(',', $source_count_array);
     //商机阶段统计图
     $status_count_array = array();
     $statusList = M('BusinessStatus')->order('order_id desc')->where('status_id <> 99')->select();
     $where_status['is_deleted'] = 0;
     $temp_count = 0;
     foreach ($statusList as $v) {
         unset($where_status['status_id']);
         $where_status['status_id'] = $v['status_id'];
         $target_count = $m_business->where($where_status)->count();
         $status_count_array[] = '[' . '"' . $v['name'] . '",' . ($target_count + $temp_count) . ']';
         $temp_count += $target_count;
     }
     $this->status_count = implode(',', array_reverse($status_count_array));
     /*时间序列图(按日)*/
     if ($end_time - 86400 * 30 > $start_time) {
         $this_time = $end_time - 86400 * 30;
     } else {
         $this_time = $start_time;
     }
     while (date('Y-m-d', $this_time) <= date('Y-m-d', $end_time)) {
         $day_count_array[] = "'" . date('Y/m/d', $this_time) . "'";
         $time1 = strtotime(date('Y-m-d', $this_time));
         $time2 = $time1 + 86400;
         $where_day_create['create_time'] = array(array('lt', $time2), array('gt', $time1), 'and');
         $day_create_count_array[] = $m_business->where($where_day_create)->count();
         $where_day_success['update_time'] = array(array('lt', $time2), array('gt', $time1), 'and');
         $where_day_success['status_id'] = 100;
         $day_success_count_array[] = $m_business->where($where_day_success)->count();
         $this_time += 86400;
     }
     $this->day_count = implode(',', $day_count_array);
     $this->day_create_count = implode(',', $day_create_count_array);
     $this->day_success_count = implode(',', $day_success_count_array);
     /*时间序列图(按周)*/
     if ($end_time - 86400 * 365 > $start_time) {
         $this_time = $end_time - 86400 * 365 - 86400 * date('w');
     } else {
         $this_time = $start_time - 86400 * date('w');
     }
     while (date('Y-m-d', $this_time) <= date('Y-m-d', $end_time)) {
         $week_count_array[] = "'" . date('Y', $this_time) . ' s' . date('W', $this_time) . "'";
         $time1 = strtotime(date('Y-m-d', $this_time));
         $time2 = $time1 + 86400 * 7;
         $where_week_create['create_time'] = array(array('lt', $time2), array('gt', $time1), 'and');
         $week_create_count_array[] = $m_business->where($where_week_create)->count();
         $where_week_success['update_time'] = array(array('lt', $time2), array('gt', $time1), 'and');
         $where_week_success['status_id'] = 100;
         $week_success_count_array[] = $m_business->where($where_week_success)->count();
         $this_time += 86400 * 7;
     }
     $this->week_count = implode(',', $week_count_array);
     $this->week_create_count = implode(',', $week_create_count_array);
     $this->week_success_count = implode(',', $week_success_count_array);
     /*时间序列图(按月)*/
     if ($end_time - 86400 * 365 > $start_time) {
         $this_time = $end_time - 86400 * 365;
     } else {
         $this_time = $start_time;
     }
     while (date('Y-m-d', $this_time) <= date('Y-m-d', $end_time)) {
         $month_count_array[] = "'" . date('Y/m', $this_time) . "'";
         $time1 = strtotime(date('Y-m', $this_time));
         $time2 = mktime(0, 0, 0, date('m', $this_time) + 1, 1, date('Y', $this_time));
         $where_month_create['create_time'] = array(array('lt', $time2), array('gt', $time1), 'and');
         $month_create_count_array[] = $m_business->where($where_month_create)->count();
         $where_month_success['update_time'] = array(array('lt', $time2), array('gt', $time1), 'and');
         $where_month_success['status_id'] = 100;
         $month_success_count_array[] = $m_business->where($where_month_success)->count();
         $this_time = mktime(date('H', $this_time), date('i', $this_time), date('s', $this_time), date('m', $this_time) + 1, date('d', $this_time), date('Y', $this_time));
     }
     $this->month_count = implode(',', $month_count_array);
     $this->month_create_count = implode(',', $month_create_count_array);
     $this->month_success_count = implode(',', $month_success_count_array);
     $max_money = $m_business->where($where_money)->Max('total_price');
     $min_money = $m_business->where($where_money)->Min('total_price');
     if ($max_money == $min_money) {
         $target_count = $m_business->where($where_money)->count();
         $money_count_array[] = '["' . $max_money . L('YUAN') . '",' . $target_count . ']';
     } else {
         $rank1 = round($min_money, 2);
         $rank2 = round($min_money + ($max_money - $min_money) * 0.25, 2);
         $rank3 = round($min_money + ($max_money - $min_money) * 0.5, 2);
         $rank4 = round($min_money + ($max_money - $min_money) * 0.75, 2);
         $rank5 = round($max_money, 2);
         $money_where = array(array('name' => $rank1 . '~' . $rank2 . L('YUAN'), 'where_money' => array(array('elt', $rank2), array('egt', $rank1), 'and')), array('name' => $rank2 . '~' . $rank3 . L('YUAN'), 'where_money' => array(array('elt', $rank3), array('gt', $rank2), 'and')), array('name' => $rank3 . '~' . $rank4 . L('YUAN'), 'where_money' => array(array('elt', $rank4), array('gt', $rank3), 'and')), array('name' => $rank4 . '~' . $rank5 . L('YUAN'), 'where_money' => array(array('elt', $rank5), array('egt', $rank4), 'and')));
         $money_count_array = array();
         foreach ($money_where as $v) {
             $where_money['total_price'] = $v['where_money'];
             $target_count = $m_business->where($where_money)->count();
             $money_count_array[] = '[' . '"' . $v['name'] . '",' . $target_count . ']';
         }
     }
     $this->money_count = implode(',', $money_count_array);
     $this->total_report = array("add_count" => $add_count_total, "own_count" => $own_count_total, "success_count" => $success_count_total, "deal_count" => $deal_count_total);
     $this->reportList = $reportList;
     if (session('?admin')) {
         $idArray = M('role')->where('user_id <> 0')->getField('role_id', true);
     } else {
         $idArray = getSubRoleId();
     }
     $roleList = array();
     foreach ($idArray as $roleId) {
         $roleList[$roleId] = getUserByRoleId($roleId);
     }
     $this->roleList = $roleList;
     $departments = M('roleDepartment')->select();
     $departmentList[] = M('roleDepartment')->where('department_id = %d', session('department_id'))->find();
     $departmentList = array_merge($departmentList, getSubDepartment(session('department_id'), $departments, ''));
     $this->assign('departmentList', $departmentList);
     $this->alert = parseAlert();
     $this->display();
 }
Exemple #5
0
function getSubDepartment($department_id, $department, $separate, $no_separater)
{
    $array = array();
    if ($no_separater) {
        foreach ($department as $value) {
            if ($department_id == $value['parent_id']) {
                $array[] = array('department_id' => $value['department_id'], 'name' => $separate . $value['name'], 'description' => $value['description']);
                $array = array_merge($array, getSubDepartment($value['department_id'], $department, $separate, 1));
            }
        }
    } else {
        foreach ($department as $value) {
            if ($department_id == $value['parent_id']) {
                $array[] = array('department_id' => $value['department_id'], 'name' => $separate . $value['name'], 'description' => $value['description']);
                $array = array_merge($array, getSubDepartment($value['department_id'], $department, $separate . '--'));
            }
        }
    }
    return $array;
}
 public function analytics()
 {
     $m_customer = M('Customer');
     if ($_GET['role']) {
         $role_id = intval($_GET['role']);
     } else {
         $role_id = 'all';
     }
     if ($_GET['department'] && $_GET['department'] != 'all') {
         $department_id = intval($_GET['department']);
     } else {
         $department_id = D('RoleView')->where('role.role_id = %d', session('role_id'))->getField('department_id');
     }
     if ($_GET['start_time']) {
         $start_time = strtotime(date('Y-m-d', strtotime($_GET['start_time'])));
     }
     $end_time = $_GET['end_time'] ? strtotime(date('Y-m-d 23:59:59', strtotime($_GET['end_time']))) : strtotime(date('Y-m-d 23:59:59', time()));
     if ($role_id == "all") {
         $roleList = getRoleByDepartmentId($department_id);
         $role_id_array = array();
         foreach ($roleList as $v2) {
             $role_id_array[] = $v2['role_id'];
         }
         $where_role_id = array('in', implode(',', $role_id_array));
         $where_source['creator_role_id'] = $where_role_id;
         $where_industry['owner_role_id'] = $where_role_id;
         $where_renenue['creator_role_id'] = $where_role_id;
         $where_employees['creator_role_id'] = $where_role_id;
     } else {
         $where_source['creator_role_id'] = $role_id;
         $where_industry['owner_role_id'] = $role_id;
         $where_renenue['creator_role_id'] = $role_id;
         $where_employees['creator_role_id'] = $role_id;
     }
     if ($start_time) {
         $where_create_time = array(array('elt', $end_time), array('egt', $start_time), 'and');
         $where_source['create_time'] = $where_create_time;
         $where_industry['create_time'] = $where_create_time;
         $where_renenue['create_time'] = $where_create_time;
         $where_employees['create_time'] = $where_create_time;
     } else {
         $where_source['create_time'] = array('elt', $end_time);
         $where_industry['create_time'] = array('elt', $end_time);
         $where_renenue['create_time'] = array('elt', $end_time);
         $where_employees['create_time'] = array('elt', $end_time);
     }
     //统计表内容
     $role_id_array = array();
     if ($role_id == "all") {
         if ($_GET['department'] != 'all') {
             if (session('?admin')) {
                 $roleList = M('role')->where('user_id <> 0')->getField('role_id', true);
             } else {
                 $roleList = getRoleByDepartmentId($department_id);
             }
             foreach ($roleList as $v) {
                 $role_id_array[] = $v;
             }
         } else {
             $role_id_array = getSubRoleId();
         }
     } else {
         $role_id_array[] = $role_id;
     }
     if ($start_time) {
         $create_time = array(array('elt', $end_time), array('egt', $start_time), 'and');
     } else {
         $create_time = array('elt', $end_time);
     }
     $add_count_total = 0;
     $own_count_total = 0;
     $success_count_total = 0;
     $deal_count_total = 0;
     $busi_customer_array = M('Business')->getField('customer_id', true);
     $busi_customer_id = implode(',', $busi_customer_array);
     foreach ($role_id_array as $v) {
         $user = getUserByRoleId($v);
         $add_count = $m_customer->where(array('is_deleted' => 0, 'creator_role_id' => $v, 'create_time' => $create_time))->count();
         $own_count = $m_customer->where(array('is_deleted' => 0, 'owner_role_id' => $v, 'create_time' => $create_time))->count();
         $success_count = $m_customer->where(array('is_deleted' => 0, 'customer_id' => array('in', $busi_customer_id), 'owner_role_id' => $v, 'create_time' => $create_time))->count();
         $reportList[] = array("user" => $user, "add_count" => $add_count, "own_count" => $own_count, "success_count" => $success_count);
         $add_count_total += $add_count;
         $own_count_total += $own_count;
         $success_count_total += $success_count;
     }
     //来源统计图
     $source_count_array = array();
     $setting = M('Fields')->where("model = 'customer' and field = 'origin'")->getField('setting');
     $setting_str = '$sourceList=' . $setting . ';';
     eval($setting_str);
     $source_total_count = 0;
     foreach ($sourceList[data] as $v) {
         unset($where_source['origin']);
         $where_source['origin'] = $v;
         $target_count = $m_customer->where($where_source)->count();
         $source_count_array[] = '[' . '"' . $v . '",' . $target_count . ']';
         $source_total_count += $target_count;
     }
     $source_count_array[] = '["' . L('OTHER') . '",' . ($add_count_total - $source_total_count) . ']';
     $this->source_count = implode(',', $source_count_array);
     //客户行业统计图
     $industry_count_array = array();
     $setting = M('Fields')->where("model = 'customer' and field = 'industry'")->getField('setting');
     $setting_str = '$industryList=' . $setting . ';';
     eval($setting_str);
     $where_industry['is_deleted'] = 0;
     $industry_total_count = 0;
     foreach ($industryList['data'] as $v) {
         unset($where_employees['industry']);
         $where_industry['industry'] = $v;
         $target_count = $m_customer->where($where_industry)->count();
         $industry_total_count += $target_count;
         $industry_count_array[] = '["' . $v . '",' . $target_count . ']';
     }
     $industry_count_array[] = '["' . L('OTHER') . '",' . ($add_count_total - $industry_total_count) . ']';
     $this->industry_count = implode(',', $industry_count_array);
     //客户员工数统计
     $employees_count_array = array();
     $setting = M('Fields')->where("model = 'customer' and field = 'no_of_employees'")->getField('setting');
     $setting_str = '$no_List=' . $setting . ';';
     eval($setting_str);
     $where_employees['is_deleted'] = 0;
     $no_total_count = 0;
     foreach ($no_List['data'] as $v) {
         unset($where_employees['no_of_employees']);
         $where_employees['no_of_employees'] = $v;
         $target_count = $m_customer->where($where_employees)->count();
         $no_total_count += $target_count;
         $employees_count_array[] = '["' . $v . '",' . $target_count . ']';
     }
     $employees_count_array[] = '["' . L('OTHER') . '",' . ($add_count_total - $no_total_count) . ']';
     $this->employees_count = implode(',', $employees_count_array);
     //客户营业额统计
     $revenue_count_array = array();
     $setting = M('Fields')->where("model = 'customer' and field = 'annual_revenue'")->getField('setting');
     $setting_str = '$revenueList=' . $setting . ';';
     eval($setting_str);
     $where_renenue['is_deleted'] = 0;
     $revenue_total_count = 0;
     foreach ($revenueList['data'] as $v) {
         unset($where_renenue['annual_revenue']);
         $where_renenue['annual_revenue'] = $v;
         $target_count = $m_customer->where($where_renenue)->count();
         $revenue_count_array[] = '[' . '"' . $v . '",' . $target_count . ']';
         $revenue_total_count += $target_count;
     }
     $revenue_count_array[] = '["' . L('OTHER') . '",' . ($add_count_total - $target_count) . ']';
     $this->revenue_count = implode(',', $revenue_count_array);
     $this->total_report = array("add_count" => $add_count_total, "own_count" => $own_count_total, "success_count" => $success_count_total);
     $this->reportList = $reportList;
     if (session('?admin')) {
         $idArray = M('role')->where('user_id <> 0')->getField('role_id', true);
     } else {
         $idArray = getSubRoleId();
     }
     $roleList = array();
     foreach ($idArray as $roleId) {
         $roleList[$roleId] = getUserByRoleId($roleId);
     }
     $this->roleList = $roleList;
     $departments = M('roleDepartment')->select();
     $departmentList[] = M('roleDepartment')->where('department_id = %d', session('department_id'))->find();
     $departmentList = array_merge($departmentList, getSubDepartment(session('department_id'), $departments, ''));
     $this->assign('departmentList', $departmentList);
     $this->alert = parseAlert();
     $this->display();
 }
 public function analytics()
 {
     $m_shoukuan = M('receivables');
     $m_shoukuandan = M('receivingorder');
     $m_fukuan = M('payables');
     $m_fukuandan = M('paymentorder');
     if ($_GET['role']) {
         $role_id = intval($_GET['role']);
     } else {
         $role_id = 'all';
     }
     if ($_GET['department'] && $_GET['department'] != 'all') {
         $department_id = intval($_GET['department']);
     } else {
         $department_id = D('RoleView')->where('role.role_id = %d', session('role_id'))->getField('department_id');
     }
     if ($_GET['start_time']) {
         $start_time = strtotime(date('Y-m-d', strtotime($_GET['start_time'])));
     }
     $end_time = $_GET['end_time'] ? strtotime(date('Y-m-d 23:59:59', strtotime($_GET['end_time']))) : strtotime(date('Y-m-d 23:59:59', time()));
     if ($role_id == "all") {
         $roleList = getRoleByDepartmentId($department_id);
         $role_id_array = array();
         foreach ($roleList as $v2) {
             $role_id_array[] = $v2['role_id'];
         }
         $where_role_id = array('in', implode(',', $role_id_array));
         $where_shoukuan['owner_role_id'] = $where_role_id;
     } else {
         $where_shoukuan['owner_role_id'] = $role_id;
     }
     $year = date('Y');
     $moon = 1;
     $shoukuan_moon_count = array();
     $fukuan_moon_count = array();
     $shijishoukuan_moon_count = array();
     $shijifukuan_moon_count = array();
     while ($moon <= 12) {
         if ($moon == 12) {
             $where_shoukuan['pay_time'] = array(array('egt', strtotime($year . '-' . $moon . '-1')), array('lt', strtotime($year + 1 . '-1-1')), 'and');
         } else {
             $where_shoukuan['pay_time'] = array(array('egt', strtotime($year . '-' . $moon . '-1')), array('lt', strtotime($year . '-' . ($moon + 1) . '-1')), 'and');
         }
         $shoukuanList = $m_shoukuan->where($where_shoukuan)->select();
         $fukuanList = $m_fukuan->where($where_shoukuan)->select();
         $total_shoukuan_money = 0;
         $total_shijishoukuan_money = 0;
         foreach ($shoukuanList as $v) {
             $total_shoukuan_money += $v['price'];
             $shoukuandan_list = $m_shoukuandan->where('receivables_id = %d', $v['receivables_id'])->getField('money', true);
             foreach ($shoukuandan_list as $v2) {
                 $total_shijishoukuan_money += $v2;
             }
         }
         $total_fukuan_money = 0;
         $total_shijifukuan_money = 0;
         foreach ($fukuanList as $v) {
             $total_fukuan_money += $v['price'];
             $fukuandan_list = $m_fukuandan->where('payables_id = %d', $v['payables_id'])->getField('money', true);
             foreach ($fukuandan_list as $v2) {
                 $total_shijifukuan_money += $v2;
             }
         }
         $shoukuan_moon_count[] = $total_shoukuan_money;
         $shijishoukuan_moon_count[] = $total_shijishoukuan_money;
         $fukuan_moon_count[] = $total_fukuan_money;
         $shijifukuan_moon_count[] = $total_shijifukuan_money;
         $moon++;
     }
     $moon_count['shoukuan'] = '[' . implode(',', $shoukuan_moon_count) . ']';
     $moon_count['shijishoukuan'] = '[' . implode(',', $shijishoukuan_moon_count) . ']';
     $moon_count['fukuan'] = '[' . implode(',', $fukuan_moon_count) . ']';
     $moon_count['shijifukuan'] = '[' . implode(',', $shijifukuan_moon_count) . ']';
     $this->moon_count = $moon_count;
     $previous_year = $year - 1;
     $moon = 1;
     $shoukuan_thisyear_count = array();
     $shoukuan_previousyear_count = array();
     $fukuan_thisyear_count = array();
     $fukuan_previousyear_count = array();
     while ($moon <= 12) {
         if ($moon == 12) {
             $where_thisyear_shoukuan['pay_time'] = array(array('egt', strtotime($year . '-' . $moon . '-1')), array('lt', strtotime($year + 1 . '-1-1')), 'and');
             $where_previousyear_shoukuan['pay_time'] = array(array('egt', strtotime($previous_year . '-' . $moon . '-1')), array('lt', strtotime($previous_year + 1 . '-1-1')), 'and');
         } else {
             $where_thisyear_shoukuan['pay_time'] = array(array('egt', strtotime($year . '-' . $moon . '-1')), array('lt', strtotime($year . '-' . ($moon + 1) . '-1')), 'and');
             $where_previousyear_shoukuan['pay_time'] = array(array('egt', strtotime($previous_year . '-' . $moon . '-1')), array('lt', strtotime($previous_year . '-' . ($moon + 1) . '-1')), 'and');
         }
         $thisyear_shoukuanList = $m_shoukuan->where($where_thisyear_shoukuan)->select();
         $previousyear_shoukuanList = $m_shoukuan->where($where_previousyear_shoukuan)->select();
         $thisyear_fukuanList = $m_fukuan->where($where_thisyear_shoukuan)->select();
         $previousyear_fukuanList = $m_fukuan->where($where_previousyear_shoukuan)->select();
         $total_thisyear_shoukuan_count = 0;
         $total_previousyear_shoukuan_count = 0;
         foreach ($thisyear_shoukuanList as $v) {
             $total_thisyear_shoukuan_count += $v['price'];
         }
         foreach ($previousyear_shoukuanList as $v) {
             $total_previousyear_shoukuan_count += $v['price'];
         }
         $shoukuan_thisyear_count[] = $total_thisyear_shoukuan_count;
         $shoukuan_previousyear_count[] = $total_previousyear_shoukuan_count;
         $total_thisyear_fukuan_count = 0;
         $total_previousyear_fukuan_count = 0;
         foreach ($thisyear_fukuanList as $v) {
             $total_thisyear_fukuan_count += $v['price'];
         }
         foreach ($previousyear_fukuanList as $v) {
             $total_previousyear_fukuan_count += $v['price'];
         }
         $fukuan_thisyear_count[] = $total_thisyear_fukuan_count;
         $fukuan_previousyear_count[] = $total_previousyear_fukuan_count;
         $moon++;
     }
     $year_count['shoukuan_previousyear'] = '[' . implode(',', $shoukuan_previousyear_count) . ']';
     $year_count['shoukuan_thisyear'] = '[' . implode(',', $shoukuan_thisyear_count) . ']';
     $year_count['fukuan_previousyear'] = '[' . implode(',', $fukuan_previousyear_count) . ']';
     $year_count['fukuan_thisyear'] = '[' . implode(',', $fukuan_thisyear_count) . ']';
     $this->year_count = $year_count;
     //统计表内容
     $role_id_array = array();
     if ($role_id == "all") {
         if ($department_id != "all") {
             if (session('?admin')) {
                 $roleList = M('role')->where('user_id <> 0')->getField('role_id', true);
             } else {
                 $roleList = getRoleByDepartmentId($department_id);
             }
             foreach ($roleList as $v) {
                 $role_id_array[] = $v;
             }
         } else {
             $role_id_array = getSubRoleId();
         }
     } else {
         $role_id_array[] = $role_id;
     }
     if ($start_time) {
         $create_time = array(array('elt', $end_time), array('egt', $start_time), 'and');
     } else {
         $create_time = array('elt', $end_time);
     }
     //应收款数 未收款 部分收款 应收金额 实际收款金额 应付款数 未付款 部分付款 应付金额 实际付款金额
     $reportList = array();
     $shoukuan_count_total = 0;
     $weishou_count_total = 0;
     $bufenshoukuan_count_total = 0;
     $shoukuan_money_total = 0;
     $yishou_money_total = 0;
     $shoukuandan_count_total = 0;
     $fukuan_count_total = 0;
     $weifu_count_total = 0;
     $bufenfukuan_count_total = 0;
     $fukuan_money_total = 0;
     $yifu_money_total = 0;
     $fukuandan_count_total = 0;
     foreach ($role_id_array as $v) {
         $user = getUserByRoleId($v);
         $shoukuan_count = 0;
         $weishou_count = 0;
         $bufenshoukuan_count = 0;
         $shoukuan_money = 0;
         $yishou_money = 0;
         $shoukuandan_count = 0;
         $fukuan_count = 0;
         $weifu_count = 0;
         $bufenfukuan_count = 0;
         $fukuan_money = 0;
         $yifu_money = 0;
         $fukuandan_count = 0;
         $shoukuan_count = $m_shoukuan->where(array('is_deleted' => 0, 'owner_role_id' => $v, 'pay_time' => $create_time))->count();
         $weishou_count = $m_shoukuan->where(array('is_deleted' => 0, 'status' => 0, 'owner_role_id' => $v, 'pay_time' => $create_time))->count();
         $bufenshoukuan_count = $m_shoukuan->where(array('is_deleted' => 0, 'status' => 1, 'owner_role_id' => $v, 'pay_time' => $create_time))->count();
         $shoukuandan_count = $m_shoukuandan->where(array('is_deleted' => 0, 'owner_role_id' => $v, 'pay_time' => $create_time))->count();
         $shoukuan_money = round($m_shoukuan->where(array('is_deleted' => 0, 'owner_role_id' => $v, 'pay_time' => $create_time))->sum('price'), 2);
         $shoukuan_id_array = $m_shoukuan->where(array('is_deleted' => 0, 'owner_role_id' => $v, 'pay_time' => $create_time))->getField('receivables_id', true);
         $shijishoukuan_money = 0;
         foreach ($shoukuan_id_array as $v2) {
             $shoukuandan_list = $m_shoukuandan->where('status = 1 and is_deleted=0 and receivables_id = %d', $v2)->getField('money', true);
             foreach ($shoukuandan_list as $v3) {
                 $shijishoukuan_money += $v3;
             }
         }
         $yishou_money = round($shijishoukuan_money, 2);
         $fukuan_count = $m_fukuan->where(array('is_deleted' => 0, 'owner_role_id' => $v, 'pay_time' => $create_time))->count();
         $weifu_count = $m_fukuan->where(array('is_deleted' => 0, 'status' => 0, 'owner_role_id' => $v, 'pay_time' => $create_time))->count();
         $bufenfukuan_count = $m_fukuan->where(array('is_deleted' => 0, 'status' => 1, 'owner_role_id' => $v, 'pay_time' => $create_time))->count();
         $fukuandan_count = $m_fukuandan->where(array('is_deleted' => 0, 'owner_role_id' => $v, 'pay_time' => $create_time))->count();
         $fukuan_money = $n = round($m_fukuan->where(array('is_deleted' => 0, 'owner_role_id' => $v, 'pay_time' => $create_time))->sum('price'), 2);
         $fukuan_id_array = $m_fukuan->where(array('is_deleted' => 0, 'owner_role_id' => $v, 'pay_time' => $create_time))->getField('payables_id', true);
         $shijifukuan_money = 0;
         foreach ($fukuan_id_array as $v4) {
             $fukuandan_list = $m_fukuandan->where('status = 1 and is_deleted=0 and payables_id = %d', $v4)->getField('money', true);
             foreach ($fukuandan_list as $v5) {
                 $shijifukuan_money += $v5;
             }
         }
         $yifu_money = round($shijifukuan_money, 2);
         $reportList[] = array("user" => $user, "shoukuan_count" => $shoukuan_count, "shoukuan_money" => $shoukuan_money, "weishou_count" => $weishou_count, "bufenshoukuan_count" => $bufenshoukuan_count, "yishou_money" => $yishou_money, "shoukuandan_count" => $shoukuandan_count, "fukuan_count" => $fukuan_count, 'weifu_count' => $weifu_count, "bufenfukuan_count" => $bufenfukuan_count, "fukuan_money" => $fukuan_money, "yifu_money" => $yifu_money, "fukuandan_count" => $fukuandan_count);
         $shoukuan_count_total += $shoukuan_count;
         $weishou_count_total += $weishou_count;
         $bufenshoukuan_count_total += $bufenshoukuan_count;
         $shoukuan_money_total += $shoukuan_money;
         $yishou_money_total += $yishou_money;
         $shoukuandan_count_total += $shoukuandan_count;
         $fukuan_count_total += $fukuan_count;
         $weifu_count_total += $weifu_count;
         $bufenfukuan_count_total += $bufenfukuan_count;
         $fukuan_money_total += $fukuan_money;
         $yifu_money_total += $yifu_money;
         $fukuandan_count_total += $fukuandan_count;
     }
     $total_report = array("shoukuan_count" => $shoukuan_count_total, "weishou_count" => $weishou_count_total, "bufenshoukuan_count" => $bufenshoukuan_count_total, "shoukuan_money" => $shoukuan_money_total, "yishou_money" => $yishou_money_total, "shoukuandan_count" => $shoukuandan_count_total, "fukuan_count" => $fukuan_count_total, "weifu_count" => $weifu_count_total, "bufenfukuan_count" => $bufenfukuan_count_total, "fukuan_money" => $fukuan_money_total, "yifu_money" => $yifu_money_total, "fukuandan_count" => $fukuandan_count_total);
     $this->reportList = $reportList;
     $this->total_report = $total_report;
     if (session('?admin')) {
         $idArray = M('role')->where('user_id <> 0')->getField('role_id', true);
     } else {
         $idArray = getSubRoleId();
     }
     $roleList = array();
     foreach ($idArray as $roleId) {
         $roleList[$roleId] = getUserByRoleId($roleId);
     }
     $this->roleList = $roleList;
     $departments = M('roleDepartment')->select();
     $departmentList[] = M('roleDepartment')->where('department_id = %d', session('department_id'))->find();
     $departmentList = array_merge($departmentList, getSubDepartment(session('department_id'), $departments, ''));
     $this->assign('departmentList', $departmentList);
     $this->display();
 }
 public function analytics()
 {
     $m_task = M('Task');
     if ($_GET['role']) {
         $role_id = intval($_GET['role']);
     } else {
         $role_id = 'all';
     }
     if ($_GET['department'] && $_GET['department'] != 'all') {
         $department_id = intval($_GET['department']);
     } else {
         $department_id = D('RoleView')->where('role.role_id = %d', session('role_id'))->getField('department_id');
     }
     if ($_GET['start_time']) {
         $start_time = strtotime($_GET['start_time']);
     }
     $end_time = $_GET['end_time'] ? strtotime($_GET['end_time']) : time();
     if ($role_id == "all") {
         if (session('?admin')) {
             $roleList = M('role')->where('user_id <> 0')->getField('role_id', true);
         } else {
             $roleList = getRoleByDepartmentId($department_id);
         }
         //$roleList = getRoleByDepartmentId($department_id);
         $role_id_array = array();
         foreach ($roleList as $v) {
             $role_id_array[] = '%,' . $v['role_id'] . ',%';
         }
         $where_completion['owner_role_id'] = array('like', $role_id_array, 'or');
     } else {
         $where_completion['owner_role_id'] = array('like', '%,' . $role_id . ',%');
     }
     if ($start_time) {
         $where_create_time = array(array('lt', $end_time), array('gt', $start_time), 'and');
         $where_completion['create_time'] = $where_create_time;
     } else {
         $where_completion['create_time'] = array('lt', $end_time);
     }
     $completion_count_array = array();
     $statusList = array(L('NOT_START'), L('DELAY'), L('ONGOING'), L('COMPLETE'));
     $where_completion['is_deleted'] = 0;
     $where_completion['isclose'] = 0;
     foreach ($statusList as $v) {
         $where_completion['status'] = $v;
         $target_count = $m_task->where($where_completion)->count();
         $completion_count_array[] = '[' . '"' . $v . '",' . $target_count . ']';
     }
     $this->completion_count = implode(',', $completion_count_array);
     $role_id_array = array();
     if ($role_id == "all") {
         if ($department_id != "all") {
             if (session('?admin')) {
                 $roleList = M('role')->where('user_id <> 0')->getField('role_id', true);
             } else {
                 $roleList = getRoleByDepartmentId($department_id);
             }
             //$roleList = getRoleByDepartmentId($department_id);
             foreach ($roleList as $v) {
                 $role_id_array[] = $v;
             }
         } else {
             $role_id_array = getSubRoleId();
         }
     } else {
         $role_id_array[] = $role_id;
     }
     if ($start_time) {
         $create_time = array(array('lt', $end_time), array('gt', $start_time), 'and');
     } else {
         $create_time = array('lt', $end_time);
     }
     $own_count_total = 0;
     $new_count_total = 0;
     $late_count_total = 0;
     $deal_count_total = 0;
     $success_count_total = 0;
     $busi_customer_array = M('Business')->getField('customer_id', true);
     $busi_customer_id = implode(',', $busi_customer_array);
     foreach ($role_id_array as $v) {
         $user = getUserByRoleId($v);
         $owner_role_id = array('like', '%,' . $v . ',%');
         $own_count = $m_task->where(array('is_deleted' => 0, 'isclose' => 0, 'owner_role_id' => $owner_role_id, 'create_date' => $create_time))->count();
         $new_count = $m_task->where(array('is_deleted' => 0, 'isclose' => 0, 'status' => L('NOT_START'), 'owner_role_id' => $owner_role_id, 'create_date' => $create_time))->count();
         $late_count = $m_task->where(array('is_deleted' => 0, 'isclose' => 0, 'status' => L('DELAY'), 'owner_role_id' => $owner_role_id, 'create_date' => $create_time))->count();
         $deal_count = $m_task->where(array('is_deleted' => 0, 'isclose' => 0, 'status' => L('ONGOING'), 'owner_role_id' => $owner_role_id, 'create_date' => $create_time))->count();
         $success_count = $m_task->where(array('is_deleted' => 0, 'isclose' => 0, 'status' => L('COMPLETE'), 'owner_role_id' => $owner_role_id, 'create_date' => $create_time))->count();
         $reportList[] = array("user" => $user, "new_count" => $new_count, "late_count" => $late_count, "own_count" => $own_count, "success_count" => $success_count, "deal_count" => $deal_count);
         $late_count_total += $late_count;
         $own_count_total += $own_count;
         $success_count_total += $success_count;
         $deal_count_total += $deal_count;
         $new_count_total += $new_count;
     }
     $this->total_report = array("new_count" => $new_count_total, "late_count" => $late_count_total, "own_count" => $own_count_total, "success_count" => $success_count_total, "deal_count" => $deal_count_total);
     $this->reportList = $reportList;
     if (session('?admin')) {
         $idArray = M('role')->where('user_id <> 0')->getField('role_id', true);
     } else {
         $idArray = getSubRoleId();
     }
     //$idArray = getSubRoleId();
     $roleList = array();
     foreach ($idArray as $roleId) {
         $roleList[$roleId] = getUserByRoleId($roleId);
     }
     $this->roleList = $roleList;
     $departments = M('roleDepartment')->select();
     $department_id = D('RoleView')->where('role.role_id = %d', session('role_id'))->getField('department_id');
     $departmentList[] = M('roleDepartment')->where('department_id = %d', $department_id)->find();
     $departmentList = array_merge($departmentList, getSubDepartment($department_id, $departments, ''));
     $this->assign('departmentList', $departmentList);
     $this->display();
 }
 public function edit()
 {
     $announcement_id = $_POST['announcement_id'] ? intval($_POST['announcement_id']) : intval($_GET['id']);
     if ($announcement_id && !check_permission($announcement_id, 'announcement', 'role_id')) {
         $this->error(L('HAVE NOT PRIVILEGES'));
     }
     if ($this->isPost()) {
         $title = trim($_POST['title']);
         if ($title == '' || $title == null) {
             alert('error', L('THE NAME CANNOT BE EMPTY'), $_SERVER['HTTP_REFERER']);
         }
         $m_announcement = M('Announcement');
         if ($m_announcement->create()) {
             $m_announcement->department = '(' . implode('),(', $_POST['announce_department']) . ')';
             $m_announcement->update_time = time();
             if ($m_announcement->save()) {
                 if ($_POST['submit'] == L('SAVE')) {
                     alert('success', L('ANNOUNCEMENT SAVED SUCCESSFULLY'), U('announcement/index'));
                 } else {
                     alert('success', L('SAVE THE SUCCESS PLEASE CONTINUE TO INPUT'), U('announcement/add'));
                 }
             } else {
                 alert('error', L(''), U('announcement/index'));
             }
         } else {
             $this->error($m_announcement->getError());
         }
     } elseif ($_GET['id']) {
         $m_announcement = M('Announcement');
         $m_department = M('RoleDepartment');
         $department_list = getSubDepartment(0, $m_department->order('department_id')->select(), '', 1);
         $announcement = $m_announcement->where('announcement_id = %d', $_GET['id'])->find();
         $department_id_array = explode(',', $announcement['department']);
         foreach ($department_list as $k => $v) {
             if (in_array('(' . $v['department_id'] . ')', $department_id_array)) {
                 $department_list[$k]['checked'] = 'checked';
             }
         }
         $this->assign('department_list', $department_list);
         $this->announcement = $announcement;
         $this->display();
     } else {
         $this->error(L('PARAMETER ERROR'));
     }
 }