Added FCPs to TheToolAdded WarehouseOffer and WarehouseOfferTemplate, also fixed menu for Lager Point

This commit is contained in:
Luca Haid
2025-04-01 15:19:18 +02:00
parent a802ff8e05
commit 97529ba95f
12 changed files with 494 additions and 410 deletions

View File

@@ -1,195 +1,16 @@
<?php
class ADBRimoFcp extends mfBaseModel {
protected function init() {
$this->db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$this->table = "RimoFcp";
}
public function getProperty($name) {
if($this->$name == null) {
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new ADBRimoFcp();
$table_fields = [
"netzgebiet_id", "name", "rimo_id", "label", "building_type", "rimo_ex_state", "rimo_op_state", "gps_lat", "gps_long",
"create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
return $model;
}
public static function getFirst($filter) {
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$where = self::getSqlFilter($filter);
$sql = "SELECT RimoFcp.* FROM RimoFcp
WHERE $where
ORDER BY name
LIMIT 1";
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ADBRimoFcp($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$res = $db->select("RimoFcp", "*", "1=1 ORDER BY name");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ADBRimoFcp($data);
}
}
return $items;
}
public static function count($filter) {
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM RimoFcp
WHERE $where
";
$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) {
$items = [];
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$where = self::getSqlFilter($filter);
$sql = "SELECT RimoFcp.* FROM RimoFcp
WHERE $where
ORDER BY name";
mfLoghandler::singleton()->debug($sql);
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'];
}
}
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ADBRimoFcp($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
if(array_key_exists("netzgebiet_id", $filter)) {
$netzgebiet_id = $filter['netzgebiet_id'];
if(is_numeric($netzgebiet_id)) {
$where .= " AND netzgebiet_id=$netzgebiet_id";
} elseif(is_array($netzgebiet_id) && count($netzgebiet_id)) {
$where .= " AND netzgebiet_id IN (". implode(",", $netzgebiet_id).")";
} elseif($netzgebiet_id === null) {
$where .= " AND netzgebiet_id IS NULL";
}
}
if(array_key_exists("name", $filter)) {
$name = $db->escape($filter['name']);
if($name) {
$where .= " AND RimoFcp.name='$name'";
}
}
if(array_key_exists("label", $filter)) {
$label = $db->escape($filter['label']);
if($label) {
$where .= " AND RimoFcp.label='$label'";
}
}
if(array_key_exists("building_type", $filter)) {
$building_type = $db->escape($filter['building_type']);
if($building_type) {
$where .= " AND RimoFcp.building_type='$building_type'";
}
}
if(array_key_exists("rimo_ex_state", $filter)) {
$rimo_ex_state = $db->escape($filter['rimo_ex_state']);
if($rimo_ex_state) {
$where .= " AND RimoFcp.rimo_ex_state='$rimo_ex_state'";
}
}
if(array_key_exists("rimo_op_state", $filter)) {
$rimo_op_state = $db->escape($filter['rimo_op_state']);
if($rimo_op_state) {
$where .= " AND RimoFcp.rimo_op_state='$rimo_op_state'";
}
}
if(array_key_exists("rimo_id", $filter)) {
$rimo_id = $db->escape($filter['rimo_id']);
if($rimo_id) {
$where .= " AND RimoFcp.rimo_id='$rimo_id'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}
class ADBRimoFcp extends TTCrudBaseModel {
public int $id;
public int $netzgebiet_id;
public ?string $name;
public string $rimo_id;
public ?string $label;
public ?string $building_type;
public ?string $rimo_ex_state;
public ?string $rimo_op_state;
public ?float $gps_lat;
public ?float $gps_long;
public int $create;
public int $edit;
}

View File

@@ -0,0 +1,101 @@
<?php
class ADBRimoFcpController extends TTCrud {
protected string $headerTitle = 'Rimo FCPs';
protected string $singleText = 'Rimo FCP';
// @formatter:off
protected array $columns = [
['key' => 'netzgebiet_id', 'text' => 'Netzgebiet', 'required' => true, 'modal' => ['type' => 'select', 'items' => []], 'table' => ['filter' => 'select']],
['key' => 'name', 'text' => 'Name', 'required' => true],
['key' => 'rimo_id', 'text' => 'Rimo ID', 'required' => true],
['key' => 'label', 'text' => 'Label', 'required' => false],
['key' => 'building_type', 'text' => 'Gebäudetyp', 'required' => false],
['key' => 'rimo_ex_state', 'text' => 'Rimo Ex State', 'required' => false],
['key' => 'rimo_op_state', 'text' => 'Rimo Op State', 'required' => false],
['key' => 'gps_lat', 'text' => 'GPS Lat', 'required' => false],
['key' => 'gps_long', 'text' => 'GPS Long', 'required' => false],
['key' => 'create', 'text' => 'Erstellt', 'required' => true, 'modal' => false],
['key' => 'edit', 'text' => 'Bearbeitet', 'required' => true, 'modal' => false, 'table' => false],
['key' => 'actions', 'text' => 'Aktionen', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'sortable' => false, 'class' => 'text-center', 'priority' => 10]],
];
public function prepareCrudConfig() {
$netzgebiete = array_map(function ($netzgebiet) {
return ['value' => $netzgebiet->id, 'text' => $netzgebiet->name];
}, ADBNetzgebietModel::getAll());
$this->columns[0]['modal']['items'] = $netzgebiete;
}
public function ImportFCPsAction() {
$input = json_decode(file_get_contents('php://input'), true);
$fcpList = $input['data'] ?? [];
$networkAreaId = $input['networkAreaId'];
$counts = ['new' => 0, 'upd' => 0];
$now = date('U');
foreach ($fcpList as $fcpIn) {
$rimoId = $fcpIn['ExternalID'] ?? null;
if ($rimoId === null) continue;
$data = [
'netzgebiet_id' => $networkAreaId,
'name' => $fcpIn['Name'] ?? null,
'rimo_id' => $rimoId,
'label' => $fcpIn['User label'] ?? null,
'building_type' => $fcpIn['Building type'] ?? null,
'rimo_ex_state' => $fcpIn['Execution state'] ?? null,
'rimo_op_state' => $fcpIn['Operational state'] ?? null,
'gps_lat' => floatval(str_replace(',', '.', $fcpIn['Latitude'] ?? '0')),
'gps_long' => floatval(str_replace(',', '.', $fcpIn['Longitude'] ?? '0')),
'edit' => $now
];
$existing = ADBRimoFcp::getAll(['rimo_id' => $rimoId]);
if (count($existing) > 0 && $existing = $existing[0]) {
$data['id'] = $existing->id;
$data['create'] = $existing->create;
ADBRimoFcp::update($data);
$counts['upd']++;
} else {
$data['create'] = $now;
ADBRimoFcp::create($data);
$counts['new']++;
}
}
$msg = sprintf('%d new, %d updated FCPs.', $counts['new'], $counts['upd']);
self::returnJson(['success' => true, 'message' => $msg]);
}
public function ImportLocationsAction() {
$input = json_decode(file_get_contents('php://input'), true);
$fcpsByName = array_column(ADBRimoFcp::getAll(['netzgebiet_id' => $input['networkAreaId']]), null, 'name');
$counts = ['upd' => 0, 'fcpNF' => 0, 'noFCP' => 0, 'noExtId' => 0];
foreach ($input['data'] as $loc) {
$fcpName = trim($loc['FCP cluster name'] ?? '');
$extId = $loc['ExternalID'] ?? null;
if ($fcpName === '') { $counts['noFCP']++; continue; }
if (!isset($fcpsByName[$fcpName])) { $counts['fcpNF']++; continue; }
if ($extId === null) { $counts['noExtId']++; continue; }
$fcp = $fcpsByName[$fcpName];
if ($hn = ADBHausnummerModel::getFirst(['rimo_id' => $extId])) {
$hn->fcp_id = $fcp->id;
$hn->rimo_fcp_name = $fcp->name;
$hn->save();
$counts['upd']++;
}
}
$msg = sprintf('Updated: %d, FCP not Found: %d, No FCP in the CSV: %d, No Rimo ID: %d',
$counts['upd'], $counts['fcpNF'], $counts['noFCP'], $counts['noExtId']);
self::returnJson(['success' => true, 'message' => $msg]);
}
}

View File

@@ -13,6 +13,7 @@ class Preorder extends mfBaseModel {
private $building;
private $adb_hausnummer;
private $adb_wohneinheit;
private $fcp;
private $services;
private $ordered_services;
private $creator;
@@ -1396,6 +1397,11 @@ class Preorder extends mfBaseModel {
return $this->creator;
}
if($name === 'fcp') {
if(!$this->adb_hausnummer->fcp_id) return null;
return ADBRimoFcp::get($this->adb_hausnummer->fcp_id);
}
if($name == "editor") {
$this->editor = new User($this->edit_by);
return $this->editor;

View File

@@ -1042,6 +1042,9 @@ class PreorderController extends mfBaseController {
case "saveAttribute":
$return = $this->saveAttributeApi();
break;
case "getFCPsForCampaign":
$return = $this->getFCPsForCampaignApi();
break;
case "getFilteredPreorders":
$return = $this->getFilteredPreordersApi();
break;
@@ -1085,6 +1088,17 @@ class PreorderController extends mfBaseController {
$this->returnJson($data);
}
protected function getFCPsForCampaignApi(): array {
$campaign = new Preordercampaign($this->request->campaign_id);
if (!$campaign->id) return [];
return array_map(
fn($fcp) => ["id" => $fcp->name ?? null, "text" => $fcp->name ?? null],
ADBRimoFcp::getAll(["netzgebiet_id" => $campaign->network->adb_netzgebiet_id]) ?? []
);
}
private function setBilledApi() {
$preorder_id = $this->request->id;

View File

@@ -1006,6 +1006,17 @@ class PreorderModel
}
}
if (!empty($filter['fcp'])) {
$fcp = $filter['fcp'];
$db = FronkDB::singleton();
if (is_array($fcp)) {
$items = array_map(fn($i) => "'" . $db->escape($i) . "'", array_filter($fcp));
if ($items) $where .= " AND adb_hausnummer.rimo_fcp_name IN (" . implode(',', $items) . ")";
} else {
$where .= " AND adb_hausnummer.rimo_fcp_name = '" . $db->escape($fcp) . "'";
}
}
// custom where clause
if (array_key_exists("add-where", $filter)) {
$where .= " " . $filter['add-where'];