needlogin = true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me", $me);
if(!$me->is("Admin")) {
$this->redirect("Dashboard");
}
}
protected function indexAction() {
$this->layout()->setTemplate("Callcenterstats/Index");
$identities = CallcenterIdentity::getAll();
$this->layout()->set("identities", $identities);
$firstOfLastMonth = new DateTime("now");
$firstOfLastMonth->setTime(0,0,0);
$firstOfLastMonth->modify("first day of this month");
$firstOfLastMonth->modify("-1 month");
$lastOfLastMonth = clone($firstOfLastMonth);
$lastOfLastMonth->modify("last day of this month");
$today = new DateTime("now");
$today->setTime(23,59,59);
$this->layout()->set("from", $firstOfLastMonth);
$this->layout()->set("to", $lastOfLastMonth);
$this->layout()->set("today", $today);
$this->layout()->set("selected_identities", []);
if($this->request->run) {
return $this->runAction();
}
}
protected function runAction() {
$identities = $this->request->identities;
$from = $this->request->from;
$to = $this->request->to;
$direction = $this->request->direction;
$from_ts = self::dateToTimestamp($from);
$to_ts = self::dateToTimestamp($to);
$from_date = new DateTime("@".$from_ts);
$from_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$to_date = new DateTime("@".$to_ts);
$to_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$this->layout()->set("from", $from_date);
$this->layout()->set("to", $to_date);
$this->layout()->set("selected_identities", $identities);
$calls = [
"in" => [
"count" => 0,
"seconds" => 0,
"billable" => 0,
"cost" => 0,
],
"out" => [
"count" => 0,
"seconds" => 0,
"billable" => 0,
"cost" => 0,
],
];
$perMinute = 1;
// Eingehend
$destinations = [];
foreach($identities as $num) {
$ident = CallcenterIdentity::getFirst(["number" => $num]);
foreach($ident->inumbers as $inumber) {
$inum = $inumber->number;
$destinations[] = $inum;
$destinations[] = substr($inum, 2);
$destinations[] = "0".substr($inum, 2);
}
}
$sql = "SELECT * FROM VoiceCallHistory WHERE
`start` >= '".date("Y-m-d", $from_ts)."' AND `start` <= '".date("Y-m-d", $to_ts)." 23:59:59'
AND `destination` IN ('".join("','", $destinations)."')
";
//echo "$sql\n
";
$res = $this->db->query($sql);
while($data = $this->db->fetch_object($res)) {
//var_dump($data);
$calls["in"]["count"]++;
$calls["in"]["seconds"] += (int)$data->duration;
// 1 euro pro minute
$cost = (int)($data->duration / 60);
if($data->duration % 60) {
// wenn nächste Minute angefangen hat, +1 euro
$cost++;
}
//echo "cost: $cost\n
";
$calls["in"]["billable"] += $cost;
$calls["in"]["cost"] += $cost * $perMinute;
}
$this->layout()->set("in", $calls["in"]);
//var_dump($calls["in"]);
// ausgehend
$sources = [];
foreach($identities as $num) {
$ident = CallcenterIdentity::getFirst(["number" => $num]);
$sources[] = $ident->number;
$sources[] = substr($ident->number, 2);
$sources[] = "0".substr($ident->number, 2);
}
$sql = "SELECT * FROM VoiceCallHistory WHERE
`start` >= '".date("Y-m-d", $from_ts)."' AND `start` <= '".date("Y-m-d", $to_ts)." 23:59:59'
AND `source` IN ('".join("','", $sources)."')
";
//echo "$sql\n
";
$res = $this->db->query($sql);
while($data = $this->db->fetch_object($res)) {
$calls["out"]["count"]++;
$calls["out"]["seconds"] += $data->duration;
// 1 euro pro minute
$cost = (int)($data->duration / 60);
if($data->duration % 60) {
// wenn nächste Minute angefangen hat, +1 euro
$cost++;
}
$calls["out"]["billable"] += $cost;
$calls["out"]["cost"] += $cost*$perMinute;
}
$this->layout()->set("out", $calls["out"]);
//var_dump($calls["out"]);
//exit;
}
}