Files
thetool/application/Voiceplan/Voiceplan.php
2025-01-24 15:04:24 +01:00

260 lines
7.5 KiB
PHP

<?php
class Voiceplan extends mfBaseModel {
private $creator;
private $editor;
private $zones;
private $destinations;
public $import_errors;
protected function beforeUpdate($data) {
if(!array_key_exists("edit_by", $data)) {
$me = new User();
$me->loadMe();
$data["edit_by"] = $me->id;
}
return $data;
}
public function getDestinationByNumber($number) {
if(!$number) return false;
$prefix = $number;
while(strlen($prefix)) {
$destination = VoiceplandestinationModel::getFirst(["prefix" => $prefix, "voiceplan_id" => $this->id]);
if($destination) {
break;
}
$prefix = substr($prefix, 0, strlen($prefix) - 1);
}
if(!$destination) return false;
return $destination;
}
public function getZoneByNumber($number) {
if(!$number) return false;
$destination = $this->getDestinationByNumber($number);
if(!$destination) return false;
return $destination->voiceplanzone;
}
public function importDestinationsFromCsv(File $file) {
if(!$this->id) {
return false;
}
try {
$errors = [];
$i = 0;
$filename = $file->getFullPath();
$input = fopen($filename, "r");
while($csv = fgetcsv($input, 0, ";")) {
$i++;
if($i == 1) continue;
if(!trim($csv[0])) {
continue;
}
$zone_extref = trim($csv[0]);
$name = trim($csv[1]);
$prefix = trim($csv[2]);
if(!$zone_extref|| !$prefix || !$name) {
$this->log->warning("not Importing Voiceplandestination with empty value: destination: $name | extref: $zone_extref | prefix: $prefix");
continue;
}
$zone = VoiceplanzoneModel::getFirst(["extref" => $zone_extref, "voiceplan_id" => $this->id]);
if(!$zone) {
$this->log->error(__METHOD__.": Zone nicht gefunden: $zone_extref | $name | $prefix");
$errors[] = "Keine Zone für Destination +$prefix $name (Zone ID: $zone_extref)";
continue;
}
$destination = VoiceplandestinationModel::getFirst(["voiceplanzone_id" => $zone->id, "prefix" => $prefix]);
if($destination) {
$this->log->warning(__METHOD__. ": Destination gibts schon: voiceplanzone_id: ".$zone->id." | destination: $name | prefix: $prefix");
continue;
}
$destination = VoiceplandestinationModel::create([
'voiceplanzone_id' => $zone->id,
'destination' => $name,
'prefix' => $prefix
]);
$destination->save();
}
// check if there is a destination for 43 (Österreich), otherwise create it
$austria_zone = VoiceplanzoneModel::getFirst(["voiceplan_id" => $this->id, "name" => "Österreich"]);
if($austria_zone) {
$austria_destination = VoiceplandestinationModel::getFirst(["voiceplanzone_id" => $austria_zone->id, "prefix" => "43"]);
if(!$austria_destination) {
$this->log->debug(__METHOD__.": Creating Austria zone (43)");
$austria_destination = VoiceplandestinationModel::create([
'voiceplanzone_id' => $austria_zone->id,
'destination' => "Österreich",
'prefix' => "43"
]);
$austria_destination->save();
}
}
$this->checkFixpriceZones();
if($errors) {
$this->import_errors = $errors;
}
return true;
} catch(Exception $e) {
echo $e->getCode().": ".$e->getMessage();exit;
return false;
}
}
public function importZonesFromCsv(File $file) {
if(!$this->id) {
return false;
}
try {
$i = 0;
$filename = $file->getFullPath();
$input = fopen($filename, "r");
while($csv = fgetcsv($input, 0, ";")) {
$i++;
if($i == 1) continue;
if(!trim($csv[0])) {
continue;
}
$name = trim($csv[0]);
$extref = trim($csv[1]);
$price_default = str_replace(",",".", trim($csv[4]));
$price_special = str_replace(",",".", trim($csv[7]));
$price_ek = $price_default;
if(is_numeric($price_special) && $price_special) {
$price_ek = $price_special;
}
$price_vk = 0;
if($price_ek && $this->price_multiplicator) {
$price_vk = (float)$price_ek * (float)$this->price_multiplicator;
}
if((!$name || !$price_ek) && $extref != "900") {
$this->log->warning(__METHOD__.": not importing Voiceplanzone with empty value: name: $name | ek: $price_ek");
continue;
}
$zone = VoiceplanzoneModel::create([
"voiceplan_id" => $this->id,
"extref" => $extref,
"name" => $name,
"purchase_price" => $price_ek,
"price" => $price_vk,
"increment_first" => $this->increment_first,
"increment" => $this->increment
]);
if(!$zone->save()) {
$this->log->error(__METHOD__.": Fehler beim speichern der Voip zone: name: $name | extref: $extref | ek: $price_ek");
}
}
return true;
} catch(Exception $e) {
echo $e->getCode().": ".$e->getMessage();exit;
return false;
}
}
public function checkFixpriceZones() {
$destinations = [];
foreach(VoiceplandestinationModel::search(["voiceplan_id" => $this->id, "prefix" => "438%"]) as $dest) {
if(!preg_match('/^4386/', $dest->prefix)) {
$destinations[] = $dest;
}
}
foreach(VoiceplandestinationModel::search(["voiceplan_id" => $this->id, "prefix" => "439%"]) as $dest) {
$destinations[] = $dest;
}
foreach($destinations as $dest) {
if($dest->voiceplanzone->purchase_price != $dest->voiceplanzone->price) {
$dest->voiceplanzone->price = $dest->voiceplanzone->purchase_price;
$dest->voiceplanzone->save();
}
}
return true;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "destinations") {
$this->destinations = VoiceplandestinationModel::search(["voiceplan_id" => $this->id]);
return $this->destinations;
}
if($name == "zones") {
$this->zones = VoiceplanzoneModel::search(["voiceplan_id" => $this->id]);
return $this->zones;
}
if($name == "creator") {
$user = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($user) {
$this->creator = $user;
return $this->creator;
}
$this->creator = new User($this->create_by);
if($this->creator->id) {
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
}
return $this->creator;
}
if($name == "editor") {
$this->editor = new User($this->edit_by);
return $this->editor;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if($this->$name === null) {
$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;
}
}