Files
thetool/application/Voiceplan/Voiceplan.php
2023-12-05 18:11:17 +01:00

192 lines
5.7 KiB
PHP

<?php
class Voiceplan extends mfBaseModel {
private $creator;
private $editor;
private $zones;
private $destinations;
public $import_errors;
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();
}
}
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;
}
if(!$name || !$price_ek) {
$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" => 0,
"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 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;
}
}