Files
thetool/application/VoiceCallHistory/VoiceCallHistoryController.php
2024-07-06 18:37:40 +02:00

194 lines
6.6 KiB
PHP

<?php
class VoiceCallHistoryController extends mfBaseController {
private User $me;
private string $VOICE_PORTAL_HOST = KOLMISOFT_API_HOST;
private string $VOICE_PORTAL_API_KEY = KOLMISOFT_API_KEY;
private string $VOICE_PORTAL_USERNAME = KOLMISOFT_API_USERNAME;
private KolmisoftMore $kolmisoftMore;
protected function init(): void {
$me = new User();
$me->loadMe();
$this->layout()->set("me", $me);
$this->me = $me;
if (!$this->me->isAdmin()) {
$this->redirect("dashboard");
}
$this->kolmisoftMore = new KolmisoftMore($this->VOICE_PORTAL_HOST, $this->VOICE_PORTAL_API_KEY, $this->VOICE_PORTAL_USERNAME);
}
protected function indexAction(): void {
$JSGlobals = ["BASE_URL" => self::getUrl("Domain"),
"DASHBOARD_URL" => self::getUrl("Dashboard"),
"MFAPPNAME" => MFAPPNAME_SLUG,
"PAGE_TITLE" => "Voice Call History",
"PATH" => [
["text" => MFAPPNAME_SLUG, "href" => self::getUrl("Dashboard")],
["text" => "Voice Call History", "href" => self::getUrl("VoiceCallHistory")]
],
"VOICE_CALL_HISTORY_API_URL" => self::getUrl("VoiceCallHistory/api"),
];
$this->layout()->set("vueViewName", "VoiceCallHistory");
$this->layout()->set("JSGlobals", $JSGlobals);
$this->layout()->setTemplate("VueViews/Vue");
}
protected function apiAction() {
$do = $this->request->do;
if (!$this->me->isAdmin()) {
$this->redirect("dashboard");
}
switch ($do) {
case "getCalls":
$return = $this->getCalls();
break;
case "importCallsFromToday":
$return = $this->importCallsFromToday();
break;
default:
$return = false;
break;
}
if (!$return) {
$return = [
"status" => "error",
"message" => "Invalid request."
];
}
die(json_encode($return));
}
private function importCallsFromToday(): array {
$startDate = strtotime(date("Y-m-d 0:00:00"));
$endDate = strtotime(date("Y-m-d 23:59:59"));
$callHistory = $this->kolmisoftMore->getVoiceCallHistory($startDate, $endDate);
return VoiceCallHistoryModel::importCallsFromKolmisoft($callHistory);
}
private function getCalls(): array {
$json = json_decode(file_get_contents('php://input'), true);
$filters = $json['filters'] ?? [];
$order = $json['order'] ?? [];
$page = $json['pagination']['page'] ?? 1;
$perPage = $json['pagination']['per_page'] ?? 10;
if (isset($filters['start']['from']) && isset($filters['start']['to']) && is_numeric($filters['start']['to'])) {
$filters['start']['from'] += 7200;
$filters['start']['to'] += 7200;
}
$calls = VoiceCallHistoryModel::getVoiceCallHistory($filters, $perPage, $perPage * $page - $perPage, $order);
$filtered_available = VoiceCallHistoryModel::countVoiceCallHistory($filters);
$totalRows = VoiceCallHistoryModel::countVoiceCallHistory([]);
return [
"rows" => $calls,
"pagination" => [
"page" => $page,
"total_pages" => ceil($filtered_available / $perPage),
"per_page" => $perPage,
"filtered_available" => intval($filtered_available),
"total_rows" => intval($totalRows)
]
];
}
public function addContractIds() {
$ignore_numbers = [
"4331641220846",
"43623237705",
"4367761737195"
];
$unknown_numbers = [];
// get calls without contract id
foreach(VoiceCallHistoryModel::getVoiceCallHistoryAsEntity(["contract_id" => null, "billable" => "1", "duration" => ["from" => 1]]) as $call) {
//var_dump($call);exit;
//echo "\n";
$number = $call->voice_account;
if(!$number) continue;
// server side failed/disconnected calls
if($call->duration == 1 && $call->state == 38) continue;
if(in_array($number, $unknown_numbers)) continue;
$voicenumber = VoicenumberModel::getFirst(["number" => $number]);
// TODO: Mail an office
if(!$voicenumber) {
$this->log->debug(__METHOD__.": Voicenumber $number not found.");
$unknown_numbers[] = $number;
continue;
}
if(!$voicenumber->contract_id) {
$this->log->debug(__METHOD__.": Missing Contract_ID in Voicenumber ".$voicenumber->number);
continue;
}
$contract = new Contract($voicenumber->contract_id);
if(!$contract) {
$this->log->debug(__METHOD__.": No Contract with Contract_ID ".$voicenumber->contract_id." in Voicenumber ".$voicenumber->number);
continue;
}
if($contract->isCancelled()) {
// mail an office
$this->log->warning(__METHOD__.": Contract ".$voicenumber->contract_id." for Voicenumber ".$voicenumber->number." is cancelled!");
}
$calldate = new DateTime($call->start);
$calldate->setTimezone(new DateTimeZone("Europe/Vienna"));
// calls from before first IVT Import must be right, we don't have historic contract data
if($calldate->format("Y-m") < "2024-08") {
$call->contract_id = $contract->id;
//echo "save 1\n";
if(!$call->save()) {
die("Cannot save call ".$call->id);
}
continue;
}
// check if contract was active at this time ...
if($calldate->getTimestamp() <= $contract->finish_date && (!$contract->cancel_date || $calldate->getTimestamp() <= $contract->cancel_date)) {
// 2023-08 is the oldest we can know because of contract import in July 2024
$call->contract_id = $contract->id;
echo "save 2\n";
$call->save();
}
// ... else look for contract with this number active at that time ...
// ... else use current active contract
if(!$contract) {
// find contract by ContractConfigValue voicenumberblock_voicenumber
}
$call->contract_id = $voicenumber->contract_id;
echo "save 3\n";
$call->save();
}
}
}