if ($_REQUEST['date1']) {
$date1 = date("d.m.Y", strtotime(form_eng_time($_REQUEST['date1'])));
} else {
$date1 = date("d.m.Y", mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
}
if ($_REQUEST['date2']) {
$date2 = date("d.m.Y", strtotime(form_eng_time($_REQUEST['date2'])));
} else {
$date2 = date("d.m.Y");
}
$date1_fet = form_eng_time($date1 . ' 00:00:00');
$date2_fet = form_eng_time($date2 . ' 23:59:59');
if ($_REQUEST['_type'] || $_REQUEST['_manager'] || $_REQUEST['_dates']) {
reset_filters(62);
if ($_REQUEST['_dates']) {
set_filter(724, "period", $date1 . " 00:00:00", $date2 . " 23:59:59");
}
if ($_REQUEST['_manager']) {
set_filter(727, "=", intval($_REQUEST['_manager']));
}
if ($_REQUEST['_type']) {
set_filter(773, "=", form_input($_REQUEST['_type']));
}
set_filter(1053, "=", "Да");
header("Location: " . $config["site_root"] . "/fields.php?table=62");
}
if ($_REQUEST['manager']) {
$manager = intval(
$_REQUEST['manager']
);
} elseif ($user['group_id'] != 1) {
$manager = $user['id'];
}
$result = sql_select_field(FIELDS_TABLE, "type_value", "id='773'");
$row = sql_fetch_assoc($result);
$types = explode("\r\n", $row['type_value']);
$h_pos = array();
$data_date = array();
$list_id_manager = '';
$sums_total = 0;
if (!$manager) {
$data_total = array();
$data_total['Имя'] = 'Всего';
}
if ($_REQUEST['sel_groups']) {
$sel_groups = $_REQUEST['sel_groups'];
if (isset($config['report_settings_' . $_REQUEST['id']])) {
sql_update(
CONFIG_TABLE,
array('value' => implode(",", $sel_groups)),
"name='report_settings_" . $_REQUEST['id'] . "'"
);
} else {
sql_insert(
CONFIG_TABLE,
array('value' => implode(",", $sel_groups), 'name' => "report_settings_" . $_REQUEST['id'])
);
}
} elseif (isset($config['report_settings_' . $_REQUEST['id']])) {
$sel_groups = explode(",", $config['report_settings_' . $_REQUEST['id']]);
} else {
$sel_groups = array(1, 2);
}
if ($manager) {
$sqlCond = "id=" . intval($manager);
} elseif ($sel_groups) {
foreach ($sel_groups as $group_id) {
$groups_where[] = "group_id=" . intval($group_id);
}
$sqlCond = "arc=0 and (" . implode(" or ", $groups_where) . ")";
} else {
$sqlCond = "0";
}
$result = sql_select_field(USERS_TABLE, "id, fio", $sqlCond);
while ($row = sql_fetch_assoc($result)) {
$id_manager = $row['id'];
$name_manager = $row['fio'];
$list_id_manager .= $id_manager . ',';
$sums[$id_manager] = 0;
$data_m[$id_manager]['Имя'] = $name_manager;
for ($i = 0; $i < count($types); $i++) {
$subresult = data_select_field(62,
"COUNT(*) AS cnt",
"status='0' and f724>='",
$date1_fet,
"' AND f724<='",
$date2_fet,
"' AND f727='$id_manager' AND f773='",
$types[$i],
"' AND status='0' AND f1053='Да'"
);
$subrow = sql_fetch_assoc($subresult);
$data_m[$id_manager][$types[$i]] = $subrow['cnt'];
if (!$manager) {
$data_total[$types[$i]] += $subrow['cnt'];
}
$sums[$id_manager] += $subrow['cnt'];
if (!$manager) {
$sums_total += $subrow['cnt'];
}
if (!in_array($types[$i], $h_pos)) {
$h_pos[] = $types[$i];
}
}
$data_m[$id_manager]['Всего'] = "$sums[$id_manager]";
if (!in_array('Всего', $h_pos)) {
$h_pos[] = 'Всего';
}
}
if (!$manager) {
$data_total['Всего'] = "$sums_total";
}
if ($user['group_id'] == 1) {
$sel_manager = "<option value=''>Все</option>\r\n";
}
$result = sql_query("SELECT DISTINCT user.id, user.fio FROM " . USERS_TABLE . " AS `user`, " . GROUPS_TABLE . " AS `group` WHERE user.arc=0 AND user.group_id!='777'");
while ($row = sql_fetch_assoc($result)) {
$sel_manager .= "<option value='" . $row['id'] . "'" . (($row['id'] == $manager) ? " selected" : "") . ">" . $row['fio'] . "</option>\r\n";
}
// для графика
$date1_fet = date('d.m', strtotime($date1_fet)) . "." . (date('Y', strtotime($date1_fet)) - 2);
$list_id_manager = substr($list_id_manager, 0, -1);
$tCn = 0;
$y_min = 0;
$y_max = 0;
foreach ($types as $val) {
$full = 0;
$subresult = data_select_field(62,
" COUNT(*) AS cnt, DATE_FORMAT(f724,'%Y-%m-%d') AS date_act, f773 AS action",
"status='0' and f724>='",
$date1_fet,
"' AND f724<='",
$date2_fet,
"' AND f773 LIKE '",
$val,
"' AND status='0' AND f1053='Да' GROUP BY action, date_act ORDER BY date_act"
);
while ($row = sql_fetch_assoc($subresult)) {
$full += $row['cnt'];
$data_date [$row['action']] [$row['date_act']] = $full;
}
if (!$data_date [$row['action']]) {
$tCn--;
}
$tCn++;
}
$result = sql_select_field(SCHEMES_TABLE, "color3", "active='1'");
$row = sql_fetch_assoc($result);
if ($tCn == 0) {
$data_date [$types[0]][date('Y-m-d', strtotime($date1))] = 0;
}
$types2 = $types;
$d1 = substr(form_eng_time($date1), 0, 10);
$d2 = substr(form_eng_time(date('d.m.Y', strtotime($date2) + 24 * 60 * 60)), 0, 10);
$date_cur = $d1;
$data_for_gr = array();
// заполняем данными и удаляем промежуточные нули
foreach ($types2 as $val) {
$data_for_gr[$val] = array();
if ($data_date[$val]) {
foreach ($data_date[$val] as $k => $v) {
$data_for_gr[$val][$k] = $v;
if ($y_max < $v) {
$y_max = $v;
}
}
ksort($data_for_gr[$val]);
}
}
if ($y_max > 0) {
$y_max = $y_max + ($y_max * 0.1);
} elseif ($y_min > 0) {
$y_max = $y_max + ($y_min * 0.1);
} else {
$y_max = $y_max - ($y_min * 0.1);
}
if ($y_max < 4) {
$y_max = 4;
}
// заполняем серии
$is_graph = false;
$graph_data = array();
foreach ($types2 as $val) {
if ($data_for_gr[$val]) {
$is_graph = true or $is_graph;
}
$graph_data[] = $data_for_gr[$val];
}
$d1 = substr(form_eng_time($date1), 0, 10);
$d2 = substr(form_eng_time($date2), 0, 10);
$graph_settings = array(
'type_graph' => 'LineDate',
'div_id' => 'chart1',
'series_names' => "'" . implode("','", $types2) . "'",
'x_max' => $d2,
'x_min' => $d1,
'y_max' => $y_max,
'zoom' => true
);
$graph = draw_graph($graph_data, $graph_settings);
$is_graph = false;
if ($_REQUEST['xsl'] == 1) {
$xsl = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>\r\n";
$xsl .= "<table>\r\n";
$xsl .= "<tr><td><h1>Холодные звонки</h1></td></tr>\r\n";
$xsl .= "<tr><td><h3>Период с: $date1 по $date2</h3></td></tr>\r\n";
$xsl .= "</table>";
$xsl .= "<table>\r\n<tr><td width=\"100\"> </td>\r\n";
foreach ($h_pos AS $s_pos) {
$xsl .= "<td width=\"100\" style=\"text-align: center\" align=\"center\"><b>$s_pos</b></td>\r\n";
}
$xsl .= "</tr>\r\n";
$pos = 1;
foreach ($data_m AS $m_id => $m_data) {
$xsl .= "<tr>";
foreach ($m_data AS $value) {
if ($pos % 2) {
$xsl .= "<td style=\"background-color: #d0d0d0; text-align: center\" bgcolor=\"#d0d0d0\" align=\"center\">$value</td>\r\n";
} else {
$xsl .= "<td style=\"text-align: center\" align=\"center\">$value</td>\r\n";
}
}
$xsl .= "</tr>\r\n";
$pos += 1;
}
$xsl .= "</table></body></html>";
$name = 'perfomance_report.xls';
header("Content-type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=" . $name);
echo $xsl;
exit;
} elseif ($_REQUEST['act'] == 'settings') {
$groups = sql_select_array(GROUPS_TABLE, "ALL_ROWS");
foreach ($groups as $k => $group) {
if (in_array($group['id'], $sel_groups)) {
$groups[$k]['sel'] = 1;
}
}
$smarty->assign("groups", $groups);
} else {
if ($user['group_id'] == 1) {
$smarty->assign("adm_rules", 1);
}
$smarty->assign("color3", $row['color3']);
$smarty->assign("date1", $date1);
$smarty->assign("date2", $date2);
$smarty->assign("data", $data_m);
if (!$manager) {
$smarty->assign("data_total", $data_total);
}
$smarty->assign("graph", $graph);
$smarty->assign("is_graph", $is_graph);
$smarty->assign("h_pos", $h_pos);
$smarty->assign("manager", $manager);
$smarty->assign("sel_manager", $sel_manager);
$smarty->assign("user_group", $user['group_id']);
}