Added Call detail CSV to invoice list

This commit is contained in:
Frank Schubert
2025-11-11 20:03:28 +01:00
parent 7a3b699a51
commit d00f340a25
2 changed files with 108 additions and 1 deletions

View File

@@ -212,6 +212,112 @@ class InvoiceController extends mfBaseController {
exit;
}
protected function downloadInvoiceVoiceDetailsAction() {
$id = $this->request->id;
if (!is_numeric($id) || !$id) {
$this->layout()->setFlash("Rechnung nicht gefunden", "error");
$this->redirect("Invoice");
}
$invoice = new Invoice($id);
if (!$invoice->id) {
$this->layout()->setFlash("Rechnung nicht gefunden", "error");
$this->redirect("Invoice");
}
$csv = "Startzeit;Abgehende Nummer;Zielnummer;Zone;Dauer;Kosten\n";
$destinations_cache = [];
foreach($invoice->voicenumbers as $voicenumber) {
$start_date = new DateTime($voicenumber->start_date);
//$start_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$start_date->setTime(0,0,0);
$end_date = new DateTime($voicenumber->end_date);
//$end_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$end_date->setTime(23,59,59);
foreach(VoiceCallHistoryModel::getVoiceCallHistoryAsEntity([
"voice_account" => $voicenumber->voicenumber,
"start" => [
"from" => $start_date->getTimestamp(),
"to" => $end_date->getTimestamp()
],
"billable" => 1,
], null, 0, ["key" => "start", "order" => "ASC"]) as $call) {
if(!$call->contract_id) continue;
//$voiceplan = new Voiceplan($voicenumber->voiceplan_id);
$voiceplan = VoiceplanModel::getFirst(["name" => $voicenumber->voiceplan]);
if(!$voiceplan) {
$this->log->warning(__METHOD__.": Voiceplan not found");
};
$number = $voicenumber->voicenumber;
$dest_nummer = $call->destination;
if (substr($dest_nummer, 0, 2) == "00") {
$dest_nummer = substr($dest_nummer, 2);
}
if (substr($dest_nummer, 0, 1) == "+") {
$dest_nummer = substr($dest_nummer, 1);
}
if (array_key_exists($dest_nummer, $destinations_cache)) {
$destination = $destinations_cache[$dest_nummer];
} else {
$destination = $voiceplan->getDestinationByNumber($dest_nummer);
if (!$destination) {
die("Destination für Zielrufnummer " . $call->destination . " nicht gefunden");
}
$destinations_cache[$dest_nummer] = $destination;
}
//var_dump($destination);
$zone = $destination->voiceplanzone;
if (!$zone) {
die("Keine Zone für Destination " . $dest_nummer . " gefunden");
}
//var_dump($zone);exit;
// inc_first - first minimumm duration to bill
// inc - subsequent minimum duration to bill
$inc_first = $zone->increment_first;
$inc = $zone->increment;
$billable_duration = $call->duration;
if ($billable_duration <= 0) continue;
// calculate price of first duration unit
// then subtract first minimum duration from duration
$sec_price = $zone->price / 60;
$call_price = $inc_first * $sec_price;
$billable_duration -= $inc_first;
// calculate price of remaining duration and make sure to bill in full duration units
if ($billable_duration > 0) {
$multi = ceil($billable_duration / $inc);
$call_price += ($multi * $inc) * $sec_price;
}
$csv .= '"'.$call->start.'";';
$csv .= '"'.$call->source.'"; ';
$csv .= '"'.$call->destination.'"; ';
$csv .= '"'.$zone->name.'"; ';
$csv .= $call->duration.'; ';
$csv .= '"'.str_replace(".",",", $call_price).'"';
$csv .= "\n";
}
}
header("Content-type: text/csv; charset=utf-8");
header('Content-disposition: attachment; filename="'.$invoice->invoice_number.'-egn.csv"');
echo $csv;
exit;
}
protected function createJob() {
$r = $this->request;