Added Callcenter Stats

This commit is contained in:
Frank Schubert
2025-07-17 20:30:53 +02:00
parent b3906626a5
commit 42d28c894e
8 changed files with 795 additions and 1 deletions

View File

@@ -0,0 +1,165 @@
<?php
/**
* @var string $mfLayoutPackage
*
* @var CallcenterIdentity[] $identities
* @var string[] $selected_identities
* @var DateTime $from
* @var DateTime $to
*/
?>
<?php include(realpath(dirname(__FILE__)."/../../$mfLayoutPackage")."/header.php"); ?>
<!-- start page title -->
<div class="row">
<div class="col-12">
<div class="page-title-box">
<div class="page-title-right">
<ol class="breadcrumb m-0">
<li class="breadcrumb-item"><a href="<?=self::getUrl("Dashboard")?>"><?=MFAPPNAME_SLUG?></a></li>
<li class="breadcrumb-item active">Callcenter Auswertung</li>
</ol>
</div>
<h4 class="page-title">Callcenter Auswertung</h4>
</div>
</div>
</div>
<!-- end page title -->
<div class="row">
<div class="col-6 col-xl-5">
<div class="card">
<div class="card-body">
<h4 class="card-header mb-2">Callcenter Auswertung</h4>
<form class="form-horizontal" method="post" action="<?=self::getUrl("Callcenterstats") ?>">
<div class="form-group row">
<label class="col-lg-2 col-form-label">Identität(en)</label>
<div class="col-lg-10">
<select name="identities[]" id="identities" multiple="multiple" class="form-control select2">
<?php foreach($identities as $ident): ?>
<option value="<?=$ident->number?>" data-color="<?=$ident->color?>" <?=($selected_identities && in_array($ident->number, $selected_identities) ? "selected='selected'" : "")?>><?=$ident->name?> - <?=$ident->display?></option>
<?php endforeach; ?>
</select>
<small><a href="#" onclick="selectColors(['green','pink']); return false;"><i class="fas fa-plus"></i> ESTMK</a></small>
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label">Zeitraum von:</label>
<div class="col-lg-10">
<input type="text" class="form-control datepicker" name="from" value="<?=$from->format("d.m.Y")?>" />
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label">Zeitraum bis:</label>
<div class="col-lg-10">
<input type="text" class="form-control datepicker" name="to" value="<?=$to->format("d.m.Y")?>"/>
</div>
</div>
<div class="form-group row">
<div class="col-lg-2"></div>
<div class="col-lg-10">
<input type="submit" class="btn btn-primary" name="run" value="Auswerten" />
</div>
</div>
</form>
</div>
</div>
<?php if(isset($in) || isset($out)): ?>
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-6">
<h4 class="text-right">Eingehend</h4>
<?php if(isset($out)): ?>
<table class="table table-sm table-striped">
<tr>
<th class="text-right">Anzahl eingehende Calls:</th>
<td class="text-monospace text-right"><?=$out["count"]?></td>
</tr>
<tr>
<th class="text-right">Sekunden gesamt:</th>
<td class="text-monospace text-right"><?=$out["seconds"]?></td>
</tr>
<tr>
<th class="text-right">Verrechenbare Minuten:</th>
<td class="text-monospace text-right"><?=$out["billable"]?></td>
</tr>
<tr>
<th class="text-right">Kosten:</th>
<td class="text-monospace text-right">€ <?=$out["cost"]?></td>
</tr>
</table>
<?php endif; ?>
</div>
<div class="col-6">
<h4 class="text-right">Ausgehend</h4>
<?php if(isset($in)): ?>
<table class="table table-sm table-striped">
<tr>
<th class="text-right">Anzahl ausgehende Calls:</th>
<td class="text-monospace text-right"><?=$in["count"]?></td>
</tr>
<tr>
<th class="text-right"h>Sekunden gesamt:</th>
<td class="text-monospace text-right"><?=$in["seconds"]?></td>
</tr>
<tr>
<th class="text-right">Verrechenbare Minuten:</th>
<td class="text-monospace text-right"><?=$in["billable"]?></td>
</tr>
<tr>
<th class="text-right">Kosten:</th>
<td class="text-monospace text-right">€ <?=$in["cost"]?></td>
</tr>
</table>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
<script>
$(".select2").select2({closeOnSelect: false});
$(".datepicker").datepicker({
language: 'de',
format: "dd.mm.yyyy",
showWeekDays: true,
todayBtn: 'linked',
autoclose: true,
orientation: "bottom"
});
function selectColors(colors = []) {
var pattern = "(" + colors.join("|") + ")";
$("#identities option").each(function(i, elem) {
var c = $(elem).data("color");
if(c.match(pattern)) {
$(elem).attr("selected", "selected");
$(elem).change();
}
});
return false;
}
</script>
<?php include(realpath(dirname(__FILE__)."/../../$mfLayoutPackage")."/footer.php"); ?>

View File

@@ -113,10 +113,11 @@
<li><a href="<?=self::getUrl("PreorderBilling")?>"><i class="far fa-fw fa-cash-register text-info"></i> Vorbestellkampagnen Verrechnung</a></li>
<?php endif; ?>
<?php if($me->isAdmin() || !empty(json_decode((new WorkerFlag($me->id, "constructionConsent_projects"))->value() ?? '[]'))): ?>
<li><a href="<?=self::getUrl("ConstructionConsentProject")?>"><i class="far fa-fw fa-clipboard-question text-info"></i> Zustimmungserklärungen</a></li>
<li class="border-top"><a href="<?=self::getUrl("ConstructionConsentProject")?>"><i class="far fa-fw fa-clipboard-question text-info"></i> Zustimmungserklärungen</a></li>
<?php endif; ?>
<?php if($me->is(["Admin"])): ?><li class="border-top"><a href="<?=self::getUrl("MailtemplateDispatch")?>"><i class="far fa-fw fa-envelope text-info"></i> Emailaussendungen</a></li><?php endif; ?>
<?php if($me->is(["Admin"])): ?><li class="border-top"><a href="<?=self::getUrl("Callcenterstats")?>"><i class="fas fa-fw fa-phone-office text-info"></i> Callcenter Auswertung</a></li><?php endif; ?>
<?php if($me->is(["Admin"])): ?><li class="border-top"><a href="<?=self::getUrl("AssetManagement")?>"><i class="far fa-fw fa-clipboard-list text-info"></i> Asset Management</a></li><?php endif; ?>
</ul>
</li>

View File

@@ -0,0 +1,198 @@
<?php
class CallcenterIdentity extends mfBaseModel {
protected $forcestr = [];
private $inumbers;
public function getProperty($name) {
if($this->$name == null) {
if($name == "inumbers") {
$inumbers = CallcenterIdentityIncomingnumber::search(["callcenteridentity_id" => $this->id]);
if($inumbers) {
$this->inumbers = $inumbers;
}
return $this->inumbers;
}
$classname = ucfirst($name);
$idfield = $name . "_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-" . $this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-" . $this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new CallcenterIdentity();
$table_fields = [
"name", "number", "display",
"create_by","edit_by","create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("CallcenterIdentity", "*", "1 = 1 ORDER BY `order`");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new CallcenterIdentity($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM CallcenterIdentity
WHERE $where
ORDER BY `order` LIMIT 1";
//var_dump($sql);exit;
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new CallcenterIdentity($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM CallcenterIdentity
WHERE $where";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "`order` ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM CallcenterIdentity
WHERE $where
ORDER BY $order";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new CallcenterIdentity($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("order", $filter)) {
$order = $filter['order'];
if(is_numeric($order)) {
$where .= " AND CallcenterIdentity.order=$order";
}
}
if(array_key_exists("number", $filter)) {
$number = FronkDB::singleton()->escape($filter['number']);
if($number) {
$where .= " AND CallcenterIdentity.number LIKE '$number'";
}
}
if(array_key_exists("name", $filter)) {
$name = FronkDB::singleton()->escape($filter['name']);
if($name) {
$where .= " AND CallcenterIdentity.name LIKE '$name'";
}
}
if(array_key_exists("display", $filter)) {
$display = FronkDB::singleton()->escape($filter['display']);
if($display) {
$where .= " AND CallcenterIdentity.display LIKE '$display'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}

View File

@@ -0,0 +1,26 @@
<?php
class CallcenterstatsController extends mfBaseController {
protected function init() {
$this->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);
}
}

View File

@@ -0,0 +1,190 @@
<?php
class CallcenterIdentityIncomingnumber extends mfBaseModel {
protected $forcestr = [];
private $incoming_numbers;
public function getProperty($name) {
if($this->$name == null) {
$classname = ucfirst($name);
$idfield = $name . "_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-" . $this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-" . $this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new CallcenterIdentityIncomingnumber();
$table_fields = [
"callcenteridentity_id", "number", "display",
"create_by","edit_by","create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("CallcenterIdentityIncomingnumber", "*", "1 = 1 ORDER BY `number`");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new CallcenterIdentityIncomingnumber($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM CallcenterIdentityIncomingnumber
ORDER BY `number` LIMIT 1";
//var_dump($sql);exit;
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new CallcenterIdentityIncomingnumber($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM CallcenterIdentityIncomingnumber
WHERE $where";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "`number` ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM CallcenterIdentityIncomingnumber
WHERE $where
ORDER BY $order";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new CallcenterIdentityIncomingnumber($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("callcenteridentity_id", $filter)) {
$callcenteridentity_id = $filter['callcenteridentity_id'];
if(is_numeric($callcenteridentity_id)) {
$where .= " AND CallcenterIdentityIncomingnumber.callcenteridentity_id=$callcenteridentity_id";
}
}
if(array_key_exists("number", $filter)) {
$number = FronkDB::singleton()->escape($filter['number']);
if($number) {
$where .= " AND CallcenterIdentityIncomingnumber.number LIKE '$number'";
}
}
if(array_key_exists("name", $filter)) {
$name = FronkDB::singleton()->escape($filter['name']);
if($name) {
$where .= " AND CallcenterIdentityIncomingnumber.name LIKE '$name'";
}
}
if(array_key_exists("display", $filter)) {
$display = FronkDB::singleton()->escape($filter['display']);
if($display) {
$where .= " AND CallcenterIdentityIncomingnumber.display LIKE '$display'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}

View File

@@ -0,0 +1,158 @@
<?php
class CallcenterstatsController extends mfBaseController {
protected function init() {
$this->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<br />";
$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<br />";
$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<br />";
$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;
}
}

View File

@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class CallcenterIdentity extends AbstractMigration
{
public function up(): void
{
if($this->getEnvironment() == "thetool") {
$ci = $this->table("CallcenterIdentity");
$ci->addColumn("name", "string", ["null" => false, "length" => 64]);
$ci->addColumn("order", "integer", ["null" => true]);
$ci->addColumn("number", "string", ["null" => false, "length" => 64]);
$ci->addColumn("display", "string", ["null" => false, "length" => 64]);
$ci->addColumn("color", "string", ["null" => false, "length" => 64]);
$ci->addColumn("create_by", "integer", ["null" => false]);
$ci->addColumn("edit_by", "integer", ["null" => false]);
$ci->addColumn("create", "integer", ["null" => false]);
$ci->addColumn("edit", "integer", ["null" => false]);
$ci->create();
$ciin = $this->table("CallcenterIdentityIncomingnumber");
$ciin->addColumn("callcenteridentity_id", "integer", ["null" => false]);
$ciin->addColumn("number", "string", ["null" => false, "length" => 64]);
$ciin->addColumn("display", "string", ["null" => false, "length" => 64]);
$ciin->addColumn("create_by", "integer", ["null" => false]);
$ciin->addColumn("edit_by", "integer", ["null" => false]);
$ciin->addColumn("create", "integer", ["null" => false]);
$ciin->addColumn("edit", "integer", ["null" => false]);
$ciin->create();
}
if($this->getEnvironment() == "addressdb") {
}
}
public function down(): void
{
if($this->getEnvironment() == "thetool") {
$this->table("CallcenterIdentityIncomingnumber")->drop()->save();
$this->table("CallcenterIdentity")->drop()->save();
}
if($this->getEnvironment() == "addressdb") {
}
}
}

View File

@@ -7,13 +7,18 @@ class mfBaseController
{
protected $log;
protected $needlogin = false;
/** @var mfRequest */
protected $request;
private $mfAction;
/** @var FronkDB */
private $mfDBI;
/** @var Layout */
private $mfLayout;
private $mfMenu;
private $mfUser;
/** @var mixed|null Called module (Controller) */
protected $mod;
/** @var mixed|null Called action method in Controller */
protected $action;
public function __construct($params = NULL)