$value) { if (property_exists(get_called_class(), $field)) { if (substr($field, 0, 5) == "vlan_" && !$value) { $model->$field = null; continue; } $model->$field = $value; } } $me = mfValuecache::singleton()->get("me"); if (!$me) { $me = new User(); $me->loadMe(); mfValuecache::singleton()->set("me", $me); } if ($model->create_by === null) { $model->create_by = $me->id; } if ($model->edit_by === null) { $model->edit_by = $me->id; } return $model; } public static function createFromRimoWorkorder($workorder) { if(!is_object($workorder)) return false; $log = mfLoghandler::singleton(); $wo_home_external_id = $workorder->adb_wohneinheit->extref; $rimo_workorder_id = $workorder->rimo_id; if(!$wo_home_external_id || !$rimo_workorder_id) { $log->warn(__METHOD__.": Missing external_id or workorder_id for workorder: ".$workorder->id); return false; } $home = \ADBWohneinheitModel::getFirst(["extref" => $wo_home_external_id]); if(!$home) { $log->warn(__METHOD__.": No home found with external_id: ".$wo_home_external_id); return false; } $sbidi_order = SbidiOrder::getFirst(["workorder_rimo_id" => $rimo_workorder_id]); if(!$sbidi_order) return false; $network = \NetworkModel::getFirst(["adb_netzgebiet_id" => $home->hausnummer->netzgebiet_id]); if(!$network) { $log->warn(__METHOD__.": No network found for home: ".$home->id. " building: ".$home->hausnummer_id); return false; } $campaign = PreordercampaignModel::getFirst(["network_id" => $network->id]); if(!$campaign) { $log->warn(__METHOD__.": No campaign found for network: ".$network->id); return false; } try { $order_date = new DateTime($sbidi_order->order_date); $order_date->setTimezone(new DateTimeZone("Europe/Vienna")); } catch(Exception $e) { $log->error(__METHOD__.": Error parsing order_date in ".$sbidi_order->workorder_rimo_id." : ".$e->getMessage()); return false; } $preorder = self::create([ "preordercampaign_id" => $campaign->id, "adb_hausnummer_id" => $home->hausnummer_id, "adb_wohneinheit_id" => $home->id, "extref" => $sbidi_order->order_name, "connection_type" => ($sbidi_order->building_type == "md") ? "multi-dwelling" : "single-dwelling", "type" => "provision", "accept_agb" => 1, "accept_dsgvo" => 1, "accept_marketing" => 1, "accept_withdrawal" => 1, "accept_digging" => 1, "contact_type" => "owner", "uid" => $sbidi_order->uid, "firstname" => $sbidi_order->firstname ?: "", "lastname" => $sbidi_order->lastname ?: "", "street" => $sbidi_order->address ?: "", "zip" => $sbidi_order->zip ?: "", "city" => $sbidi_order->city ?: "", "phone" => $sbidi_order->phone ?: "", "email" => $sbidi_order->email ?: "", "submit_type" => "import", "order_date" => $order_date->getTimestamp(), ]); $preorder->createUcode(); $new_status = false; if($preorder->adb_wohneinheit_id) { $new_status = PreorderstatusModel::getFirst(["code" => $preorder->adb_wohneinheit->status->code]); if($preorder->adb_hausnummer->status->code > $preorder->adb_wohneinheit->status->code) { $new_status = PreorderstatusModel::getFirst(["code" => $preorder->adb_hausnummer->status->code]); } } elseif($preorder->adb_hausnummer_id) { $new_status = PreorderstatusModel::getFirst(["code" => $preorder->adb_hausnummer->status->code]); } if($new_status) { $preorder->status_id = $new_status->id; } else { $preorder->status_id = 1; } if(!$preorder->save()) { return false; } return $preorder; } public static function getAll() { $items = []; $db = FronkDB::singleton(); $res = $db->select("Preorder", "*"); if ($db->num_rows($res)) { while ($data = $db->fetch_object($res)) { $items[] = new Preorder($data); } } return $items; } public static function getFirstActive($filter = [], $order = false) { if (!is_array($filter)) return false; if (!array_key_exists("deleted", $filter)) { $filter["deleted"] = null; } if (!array_key_exists("status_code", $filter) && !array_key_exists("status_code", $filter) && !array_key_exists("status_id", $filter) && !array_key_exists("status_id", $filter)) { $filter["debug($sql); $res = $db->query($sql); if ($db->num_rows($res)) { $data = $db->fetch_object($res); // search in cache $item = mfValuecache::singleton()->get("mfObjectmodel-preorder-".$data->id); if($item) { return $item; } // if not in cache, load regularly $item = new Preorder($data); if ($item->id) { mfValuecache::singleton()->set("mfObjectmodel-preorder-".$data->id, $item); return $item; } else { return null; } } return null; } public static function getAllPartners(): array { $items = []; $db = FronkDB::singleton(); $res = $db->select("Preorder", "partner_id", "partner_id IS NOT NULL AND partner_id > 0 GROUP BY partner_id"); if ($db->num_rows($res)) { while ($data = $db->fetch_object($res)) { $partner = AddressModel::getOne($data->partner_id); $items[] = [ "partner_id" => $data->partner_id, "name" => $partner->getCompanyOrName() ]; } } return $items; } public static function countWithLogistics($filter) { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $sql = "SELECT COUNT(*) as cnt FROM `" . FRONKDB_DBNAME . "`.Preorder tt_preorder LEFT JOIN `" . FRONKDB_DBNAME . "`.Preorderstatus tt_preorderstatus ON (tt_preorder.status_id = tt_preorderstatus.id) LEFT JOIN `" . ADDRESSDB_DBNAME . "`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id) LEFT JOIN `" . ADDRESSDB_DBNAME . "`.Wohneinheit as adb_wohneinheit ON (tt_preorder.adb_wohneinheit_id = adb_wohneinheit.id) LEFT OUTER JOIN (SELECT adb_wohneinheit_id, count(*) cnt, MAX(rimo_status) as rimo_status, MAX(rimo_name) as rimo_name, MAX(rimo_team_name) as rimo_team_name, MAX(rimo_team_id) as rimo_team_id FROM `" . FRONKDB_DBNAME . "`.RimoWorkorder WHERE rimo_status NOT IN ('deleted', 'cancelled') GROUP BY adb_wohneinheit_id) workorder ON adb_wohneinheit.id = workorder.adb_wohneinheit_id LEFT JOIN Preorderlogistics ON (Preorderlogistics.preorder_id = tt_preorder.id) WHERE $where "; mfLoghandler::singleton()->debug($sql); $res = $db->query($sql); if ($db->num_rows($res)) { $data = $db->fetch_object($res); return $data->cnt; } return 0; } public static function searchWithLogistics($filter, $limit = false, $returnDBRessource = false) { $items = []; $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $sql = "SELECT tt_preorder.* FROM `" . FRONKDB_DBNAME . "`.Preorder tt_preorder LEFT JOIN `" . FRONKDB_DBNAME . "`.Preorderstatus tt_preorderstatus ON (tt_preorder.status_id = tt_preorderstatus.id) LEFT JOIN `" . ADDRESSDB_DBNAME . "`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id) LEFT JOIN `" . ADDRESSDB_DBNAME . "`.Wohneinheit as adb_wohneinheit ON (tt_preorder.adb_wohneinheit_id = adb_wohneinheit.id) LEFT OUTER JOIN (SELECT adb_wohneinheit_id, count(*) cnt, MAX(rimo_status) as rimo_status, MAX(rimo_name) as rimo_name, MAX(rimo_team_name) as rimo_team_name, MAX(rimo_team_id) as rimo_team_id FROM `" . FRONKDB_DBNAME . "`.RimoWorkorder WHERE rimo_status NOT IN ('deleted', 'cancelled') GROUP BY adb_wohneinheit_id) workorder ON adb_wohneinheit.id = workorder.adb_wohneinheit_id LEFT JOIN Preorderlogistics ON (Preorderlogistics.preorder_id = tt_preorder.id) WHERE $where ORDER BY lastname, firstname "; /*$sql = "SELECT tt_preorder.* FROM `".FRONKDB_DBNAME."`.Preorder tt_preorder LEFT JOIN `".FRONKDB_DBNAME."`.Preorderstatus tt_preorderstatus ON (tt_preorder.status_id = tt_preorderstatus.id) LEFT JOIN `".ADDRESSDB_DBNAME."`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id) WHERE $where ORDER BY lastname, firstname";*/ 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']; } } mfLoghandler::singleton()->debug($sql); $res = $db->query($sql); if ($db->num_rows($res)) { // hack for Preorder::exportAction if ($returnDBRessource) { return $res; } while ($data = $db->fetch_object($res)) { $items[] = new Preorder($data); } } return $items; } public static function countOrderedAddonservices($filter = []) { $db = FronkDB::singleton(); if (!array_key_exists("deleted", $filter)) { $filter["deleted"] = null; } if (!array_key_exists("status_code", $filter) && !array_key_exists("status_code", $filter)) { $filter["debug($sql); $res = $db->query($sql); if ($db->num_rows($res)) { $data = $db->fetch_object($res); return $data->cnt; } return 0; } public static function searchOrderedAddonservices($filter = [], $limit = false) { $items = []; $db = FronkDB::singleton(); if (!array_key_exists("deleted", $filter)) { $filter["deleted"] = null; } if (!array_key_exists("status_code", $filter) && !array_key_exists("status_code", $filter)) { $filter["debug($sql); $res = $db->query($sql); if ($db->num_rows($res)) { while ($data = $db->fetch_object($res)) { $items[] = new Preorder($data); } } return $items; } public static function countActive($filter = []) { if (!is_array($filter)) return false; if (!array_key_exists("deleted", $filter)) { $filter["deleted"] = null; } if (!array_key_exists("status_code", $filter) && !array_key_exists("status_code", $filter) && !array_key_exists("status_id", $filter) && !array_key_exists("status_id", $filter)) { $filter["status_code", $filter) && !array_key_exists("status_code", $filter) && !array_key_exists("status_id", $filter) && !array_key_exists("status_id", $filter)) { $filter["query($sql); mfLoghandler::singleton()->debug("[Query took: " . (microtime(true) - $queryStart) . " seconds] " . $sql); if ($db->num_rows($res)) { $data = $db->fetch_object($res); return $data->cnt; } return 0; } public static function search($filter = [], $limit = false, $returnDBRessource = false, $returnArray = false) { $items = []; $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $sql = "SELECT tt_preorder.* FROM `" . FRONKDB_DBNAME . "`.Preorder tt_preorder LEFT JOIN `" . FRONKDB_DBNAME . "`.Preorderstatus tt_preorderstatus ON (tt_preorder.status_id = tt_preorderstatus.id) LEFT JOIN `" . ADDRESSDB_DBNAME . "`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id) LEFT JOIN `" . ADDRESSDB_DBNAME . "`.Wohneinheit as adb_wohneinheit ON (tt_preorder.adb_wohneinheit_id = adb_wohneinheit.id) LEFT OUTER JOIN (SELECT adb_wohneinheit_id, count(*) cnt, MAX(rimo_status) as rimo_status, MAX(rimo_name) as rimo_name, MAX(rimo_team_name) as rimo_team_name, MAX(rimo_team_id) as rimo_team_id FROM `" . FRONKDB_DBNAME . "`.RimoWorkorder WHERE rimo_status NOT IN ('deleted', 'cancelled') GROUP BY adb_wohneinheit_id) workorder ON adb_wohneinheit.id = workorder.adb_wohneinheit_id WHERE $where ORDER BY lastname, firstname "; /*$sql = "SELECT tt_preorder.* FROM `".FRONKDB_DBNAME."`.Preorder tt_preorder LEFT JOIN `".FRONKDB_DBNAME."`.Preorderstatus tt_preorderstatus ON (tt_preorder.status_id = tt_preorderstatus.id) LEFT JOIN `".ADDRESSDB_DBNAME."`.view_hausnummer as adb_hausnummer ON (tt_preorder.adb_hausnummer_id = adb_hausnummer.hausnummer_id) WHERE $where ORDER BY lastname, firstname";*/ 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']; } } mfLoghandler::singleton()->debug($sql); $res = $db->query($sql); if ($db->num_rows($res)) { // hack for Preorder::exportAction if ($returnDBRessource) { return $res; } while ($data = $db->fetch_object($res)) { if ($returnArray) { $items[] = $data; } else { $item = mfValuecache::singleton()->get("mfObjectmodel-preorder-".$data->id); if($item) { $items[] = $item; } else { $item = new Preorder($data); mfValuecache::singleton()->set("mfObjectmodel-preorder-".$data->id, $item); $items[] = $item; } } } } return $items; } private static function getSqlFilter($filter) { if (isset($filter['status']) && !empty($filter['status']) && (!empty($filter['create_from']) || !empty($filter['create_to']))) { $historyConditions = [ "`key` = 'status_id'", "new_value = " . intval($filter['status'][0]) ]; if (!empty($filter['create_from']) && ($date = DateTime::createFromFormat('d.m.Y', $filter['create_from']))) { $historyConditions[] = "`create` >= " . $date->setTime(0, 0)->getTimestamp(); } if (!empty($filter['create_to']) && ($date = DateTime::createFromFormat('d.m.Y', $filter['create_to']))) { $historyConditions[] = "`create` <= " . $date->setTime(23, 59, 59)->getTimestamp(); } $filter['add-where'] = "AND tt_preorder.id IN ( SELECT preorder_id FROM `" . FRONKDB_DBNAME . "`.PreorderHistory WHERE " . implode(' AND ', $historyConditions) . " )"; unset($filter['status_id'], $filter['create_from'], $filter['create_to']); } $where = "1=1 "; if (array_key_exists("deleted", $filter)) { $deleted = $filter['deleted']; if ($deleted === null || $deleted === false || $deleted === 0) { $where .= " AND (deleted IS NULL OR deleted = 0)"; } elseif ($deleted) { $where .= " AND deleted > 0"; } } if (array_key_exists("partner_id", $filter)) { $partner_id = $filter['partner_id']; if (is_array($partner_id)) { $where .= " AND partner_id IN (" . join(",", $partner_id) . ")"; } elseif (is_numeric($partner_id)) { $where .= " AND partner_id=$partner_id"; } } if (array_key_exists("status_id", $filter)) { $status_id = $filter['status_id']; if (is_numeric($status_id)) { $where .= " AND tt_preorder.status_id=$status_id"; } elseif (is_array($status_id)) { $where .= " AND tt_preorder.status_id IN (" . implode(",", $status_id) . ")"; } } if (array_key_exists("status_id", $filter)) { $status_id = $filter['>status_id']; if (is_numeric($status_id)) { $where .= " AND tt_preorder.status_id > $status_id"; } } if (array_key_exists("preorder_status_flags", $filter)) { $preorder_status_flags = $filter['preorder_status_flags']; if (is_array($preorder_status_flags) && count($preorder_status_flags)) { $where .= " AND tt_preorder.id IN ( SELECT psv.preorder_id FROM `" . FRONKDB_DBNAME . "`.PreorderStatusflagValue psv WHERE psv.flag_id IN (" . implode(',', $preorder_status_flags) . ") AND psv.value = 1 GROUP BY psv.preorder_id HAVING COUNT(DISTINCT psv.flag_id) = " . count($preorder_status_flags) . " )"; } } if (!empty($filter['preorder_status_flags_all']) && is_array($filter['preorder_status_flags_all'])) { $sanitized_flags = array_map('intval', $filter['preorder_status_flags_all']); $flags_list = implode(',', $sanitized_flags); $flags_count = count($sanitized_flags); $dbName = FRONKDB_DBNAME; $where .= " AND tt_preorder.id IN ( SELECT psv.preorder_id FROM `{$dbName}`.PreorderStatusflagValue psv WHERE psv.flag_id IN ({$flags_list}) AND psv.value = 1 GROUP BY psv.preorder_id HAVING COUNT(DISTINCT psv.flag_id) = {$flags_count} )"; } if (array_key_exists("preorder_status_flags_disabled", $filter)) { $preorder_status_flags_disabled = $filter['preorder_status_flags_disabled']; if (is_array($preorder_status_flags_disabled) && count($preorder_status_flags_disabled)) { $where .= " AND tt_preorder.id NOT IN ( SELECT psv.preorder_id FROM `" . FRONKDB_DBNAME . "`.PreorderStatusflagValue psv WHERE psv.flag_id IN (" . implode(',', $preorder_status_flags_disabled) . ") AND psv.value = 1 )"; } } if (array_key_exists("created_from", $filter)) { $created_from = $filter['created_from']; if (is_numeric($created_from)) { // use mysql function for the date (column is unix epoch) $created_from_epoch = time() - $created_from * 86400; // var_dump($created_from_epoch); // die(); $where .= " AND FROM_UNIXTIME(tt_preorder.create) >= FROM_UNIXTIME($created_from_epoch)"; } } if (array_key_exists("status_code", $filter)) { $status_code = $filter['status_code']; if (is_numeric($status_code)) { $where .= " AND tt_preorderstatus.code=$status_code"; } elseif (is_array($status_code)) { $where .= " AND tt_preorderstatus.code IN (" . implode(",", $status_code) . ")"; } } if (array_key_exists("status_code", $filter)) { $status_code = $filter['>status_code']; if (is_numeric($status_code)) { $where .= " AND tt_preorderstatus.code > $status_code"; } } if (array_key_exists("!status_code", $filter)) { $status_code = $filter['!status_code']; if (is_numeric($status_code)) { $where .= " AND tt_preorderstatus.code != $status_code"; } elseif (is_array($status_code)) { $where .= " AND tt_preorderstatus.code NOT IN (" . implode(",", $status_code) . ")"; } } if (array_key_exists("<=status_code", $filter)) { $status_code = $filter['<=status_code']; if (is_numeric($status_code)) { $where .= " AND tt_preorderstatus.code <= $status_code"; } } if (array_key_exists(">=status_code", $filter)) { $status_code = $filter['>=status_code']; if (is_numeric($status_code)) { $where .= " AND tt_preorderstatus.code >= $status_code"; } } if (array_key_exists("borderpoint_status", $filter)) { $borderpoint_status = FronkDB::singleton()->escape($filter['borderpoint_status']); if ($borderpoint_status) { $where .= " AND tt_preorder.borderpoint_status='$borderpoint_status'"; } } if (array_key_exists("preordercampaign_id", $filter)) { $preordercampaign_id = $filter['preordercampaign_id']; if (is_numeric($preordercampaign_id)) { $where .= " AND preordercampaign_id=$preordercampaign_id"; } elseif (is_array($preordercampaign_id) && count($preordercampaign_id)) { $where .= " AND preordercampaign_id IN (" . implode(",", $preordercampaign_id) . ")"; } } if (array_key_exists("building_id", $filter)) { $building_id = $filter['building_id']; if (is_numeric($building_id)) { $where .= " AND building_id=$building_id"; } elseif (is_array($building_id)) { $where .= " AND building_id IN (" . implode(",", $building_id) . ")"; } elseif ($building_id === null) { $where .= " AND building_id IS NULL"; } } if (array_key_exists("adb_hausnummer_id", $filter)) { $adb_hausnummer_id = $filter['adb_hausnummer_id']; if (is_numeric($adb_hausnummer_id)) { $where .= " AND tt_preorder.adb_hausnummer_id=$adb_hausnummer_id"; } elseif (is_array($adb_hausnummer_id)) { $where .= " AND tt_preorder.adb_hausnummer_id IN (" . implode(",", $adb_hausnummer_id) . ")"; } elseif ($adb_hausnummer_id === null) { $where .= " AND tt_preorder.adb_hausnummer_id IS NULL"; } } if (array_key_exists("adb_wohneinheit_id", $filter)) { $adb_wohneinheit_id = $filter['adb_wohneinheit_id']; if (is_numeric($adb_wohneinheit_id)) { $where .= " AND tt_preorder.adb_wohneinheit_id=$adb_wohneinheit_id"; } elseif (is_array($adb_wohneinheit_id)) { $where .= " AND tt_preorder.adb_wohneinheit_id IN (" . implode(",", $adb_wohneinheit_id) . ")"; } elseif ($adb_wohneinheit_id === null) { $where .= " AND tt_preorder.adb_wohneinheit_id IS NULL"; } elseif ($adb_wohneinheit_id === true) { $where .= " AND tt_preorder.adb_wohneinheit_id IS NOT NULL AND tt_preorder.adb_wohneinheit_id > 0"; } } if (array_key_exists("operator_id", $filter)) { $operator_id = $filter['operator_id']; if (is_numeric($operator_id)) { $where .= " AND partner_id IN ( SELECT PreordercampaignOperatorIsp.isp_id as isp_id FROM PreordercampaignOperatorIsp LEFT JOIN PreordercampaignOperator ON (PreordercampaignOperatorIsp.campaignoperator_id = PreordercampaignOperator.id) WHERE operator_id = $operator_id )"; } } if (array_key_exists("product_id", $filter)) { $product_id = $filter['product_id']; if (is_numeric($product_id)) { $where .= " AND tt_preorder.product_id=$product_id"; } } if (array_key_exists("setup_product_id", $filter)) { $setup_product_id = $filter['setup_product_id']; if (is_numeric($setup_product_id)) { $where .= " AND tt_preorder.setup_product_id=$setup_product_id"; } } if (array_key_exists("address_created", $filter)) { $address_created = $filter['address_created']; if ($address_created === true) { $where .= " AND address_created > 0"; } elseif ($address_created == null) { $where .= " AND address_created IS NULL"; } } if (array_key_exists("type", $filter)) { $type = $filter['type']; if (is_array($type) && count($type)) { $where .= " AND tt_preorder.type IN ('" . implode("','", $type) . "')"; } else { $type = FronkDB::singleton()->escape($filter['type']); if ($type) { $where .= " AND tt_preorder.type like '%$type%'"; } } } if (array_key_exists("connection_type", $filter)) { $connection_type = $filter['connection_type']; if (is_array($connection_type) && count($connection_type)) { $where .= " AND tt_preorder.connection_type IN ('" . implode("','", $connection_type) . "')"; } else { $connection_type = FronkDB::singleton()->escape($filter['connection_type']); if ($connection_type) { $where .= " AND tt_preorder.connection_type like '%$connection_type%'"; } } } if (array_key_exists("tool_building_type", $filter)) { $tool_building_type = $filter['tool_building_type']; if (is_array($tool_building_type) && count($tool_building_type)) { $where .= " AND adb_hausnummer.tool_building_type IN ('" . implode("','", $tool_building_type) . "')"; } else { $tool_building_type = FronkDB::singleton()->escape($filter['tool_building_type']); if ($tool_building_type === '0' || $tool_building_type) { $where .= " AND adb_hausnummer.tool_building_type = $tool_building_type "; } } } if (array_key_exists("connection_count", $filter)) { $connection_count = $filter['connection_count']; if (intval($connection_count) === 1) { $where .= " AND connection_count = 1"; } elseif (intval($connection_count) === 2) { $where .= " AND connection_count > 1"; } } if (array_key_exists("ucode", $filter)) { $ucode = FronkDB::singleton()->escape($filter['ucode']); if ($ucode) { $where .= " AND ucode LIKE '$ucode'"; } } if (array_key_exists("ciftoken", $filter)) { $ciftoken = FronkDB::singleton()->escape($filter['ciftoken']); if ($ciftoken) { $where .= " AND ciftoken LIKE '$ciftoken'"; } } if (array_key_exists("oaid", $filter)) { $oaid = $filter['oaid']; if ($oaid) { $oaid = FronkDB::singleton()->escape($oaid); $where .= " AND tt_preorder.oaid LIKE '$oaid'"; } elseif ($oaid === null) { $where .= " AND tt_preorder.oaid IS NULL"; } } if (array_key_exists("extref", $filter)) { $extref = FronkDB::singleton()->escape($filter['extref']); if ($extref) { $where .= " AND tt_preorder.extref = '$extref'"; } } if (array_key_exists("email", $filter)) { $email = FronkDB::singleton()->escape($filter['email']); if ($email) { $where .= " AND tt_preorder.email = '$email'"; } } if (array_key_exists("phone", $filter)) { $phone = $filter["phone"]; if (is_array($phone)) { $where .= " AND REGEXP_REPLACE(tt_preorder.phone, '[^0-9]+', '') IN ('" . implode("','", $phone) . "')"; } else { $phone = FronkDB::singleton()->escape($filter['phone']); if ($phone) { $where .= " AND REGEXP_REPLACE(tt_preorder.phone, '[^0-9]+', '') = '$phone'"; } } } if (array_key_exists("workorder_export_date", $filter)) { $workorder_export_date = $filter['workorder_export_date']; if ($workorder_export_date === null || $workorder_export_date === false) { $where .= " AND (tt_preorder.workorder_export_date IS NULL OR tt_preorder.workorder_export_date = 0)"; } elseif ($workorder_export_date) { $where .= " AND tt_preorder.workorder_export_date > 0"; } } if (array_key_exists("rimo_workorder", $filter)) { $rimo_workorder = $filter['rimo_workorder']; if ($rimo_workorder === null || $rimo_workorder === false) { $where .= " AND (workorder.cnt IS NULL OR workorder.cnt = 0)"; } elseif ($rimo_workorder) { $where .= " AND workorder.cnt > 0"; } } if (array_key_exists("rimo_workorder_status", $filter)) { $rimo_workorder_status = $filter['rimo_workorder_status']; if (is_array($rimo_workorder_status) && count($rimo_workorder_status)) { $rimo_workorder_status = array_map(function ($value) { return FronkDB::singleton()->escape($value); }, $filter['rimo_workorder_status']); $where .= " AND workorder.rimo_status IN ('" . implode("','", $rimo_workorder_status) . "')"; } else if ($rimo_workorder_status) { $rimo_workorder_status = FronkDB::singleton()->escape($rimo_workorder_status); $where .= " AND workorder.rimo_status = '$rimo_workorder_status'"; } } if (array_key_exists("rimo_workorder_name", $filter)) { $rimo_workorder_name = FronkDB::singleton()->escape($filter['rimo_workorder_name']); if ($rimo_workorder_name) { $where .= " AND workorder.rimo_name = '$rimo_workorder_name'"; } } if (array_key_exists("rimo_workorder_team_name", $filter)) { $rimo_workorder_team_name = FronkDB::singleton()->escape($filter['rimo_workorder_team_name']); if ($rimo_workorder_team_name) { $where .= " AND workorder.rimo_team_name LIKE '%$rimo_workorder_team_name%'"; } } if (array_key_exists("rimo_workorder_team_id", $filter)) { $rimo_workorder_team_id = FronkDB::singleton()->escape($filter['rimo_workorder_team_id']); if ($rimo_workorder_team_id) { $where .= " AND workorder.rimo_team_id = '$rimo_workorder_team_id'"; } } 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)) { $where .= " AND netzgebiet_id IN (" . implode(",", $netzgebiet_id) . ")"; } elseif ($netzgebiet_id === null) { $where .= " AND netzgebiet_id IS NULL"; } } if (array_key_exists("gemeinde_id", $filter)) { if (!is_array($filter['gemeinde_id'])) { $gemeinde_id = FronkDB::singleton()->escape($filter['gemeinde_id']); $where .= " AND `" . ADDRESSDB_DBNAME . "`.adb_hausnummer.gemeinde_id = $gemeinde_id"; } elseif (count($filter['gemeinde_id'])) { $gemeinde_id = $filter['gemeinde_id']; $where .= " AND `" . ADDRESSDB_DBNAME . "`.adb_hausnummer.gemeinde_id IN (" . implode(",", $gemeinde_id) . ")"; } } if (array_key_exists("gemeinde", $filter)) { $gemeinde = FronkDB::singleton()->escape($filter['gemeinde']); if ($gemeinde) { $where .= " AND `" . ADDRESSDB_DBNAME . "`.view_hausnummer.gemeinde like '%$gemeinde%'"; } } if (array_key_exists("billed", $filter) && !empty($filter['billed'])) { $billed = $filter['billed']; if ($billed == 0) { $where .= " AND (tt_preorder.billed IS NULL OR tt_preorder.billed = 0)"; } elseif ($billed) { $where .= " AND tt_preorder.billed > 0"; } } if (array_key_exists("plz", $filter)) { $plz = FronkDB::singleton()->escape($filter['plz']); if ($plz) { $where .= " AND `" . ADDRESSDB_DBNAME . "`.view_hausnummer.plz like '%$plz%'"; } } if (array_key_exists("strasse", $filter)) { $strasse = FronkDB::singleton()->escape($filter['strasse']); if ($strasse) { $where .= " AND `" . ADDRESSDB_DBNAME . "`.view_hausnummer.strasse like '%$strasse%'"; } } if (array_key_exists("hausnummer", $filter)) { $hausnummer = FronkDB::singleton()->escape($filter['hausnummer']); if ($hausnummer) { $where .= " AND adb_hausnummer.hausnummer like '%$hausnummer%'"; } } if (array_key_exists("stiege", $filter)) { $stiege = FronkDB::singleton()->escape($filter['stiege']); if ($stiege) { $where .= " AND adb_hausnummer.stiege like '%$stiege%'"; } } if (array_key_exists("unit_count", $filter)) { $unit_count = $filter['unit_count']; if (intval($unit_count) === 1) { $where .= " AND adb_hausnummer.unit_count = 1"; } elseif (intval($unit_count) === 2) { $where .= " AND adb_hausnummer.unit_count > 1"; } elseif (strlen($unit_count) && intval($unit_count) === 0) { $where .= " AND adb_hausnummer.unit_count = 0 OR adb_hausnummer.unit_count IS NULL"; } } if (array_key_exists("unit_count>=", $filter)) { $unit_count = $filter['unit_count>=']; if (is_numeric($unit_count)) { $where .= " AND adb_hausnummer.unit_count >= $unit_count"; } } if (array_key_exists("unit_count<=", $filter)) { $unit_count = $filter['unit_count<=']; if (is_numeric($unit_count)) { $where .= " AND adb_hausnummer.unit_count <= $unit_count"; } } if (array_key_exists("sent", $filter)) { $sent = $filter['sent']; if ($sent) { $where .= " AND Preorderlogistics.sent > 0"; } else { $where .= " AND (Preorderlogistics.sent = 0 OR Preorderlogistics.sent IS NULL)"; } } if (array_key_exists("attributes", $filter)) { $attributes = FronkDB::singleton()->escape($filter['attributes']); if (is_array($attributes) && count($attributes)) { if (array_key_exists("bep_specified", $attributes)) { if ($attributes['bep_specified']) { $where .= " AND JSON_EXTRACT(attributes, \"$.bep_specified\") = 1"; } } if (array_key_exists("inhouse_cabling_supplied", $attributes)) { if ($attributes['inhouse_cabling_supplied']) { $where .= " AND JSON_EXTRACT(attributes, \"$.inhouse_cabling_supplied\") = 1"; } } } } if (array_key_exists("attributes_bep_specified", $filter) && !empty($filter['attributes_bep_specified'])) { if ($filter['attributes_bep_specified'] == 1) { $where .= " AND JSON_EXTRACT(tt_preorder.attributes, \"$.bep_specified\") = 1"; } else if ($filter['attributes_bep_specified'] == 0) { $where .= " AND (JSON_EXTRACT(tt_preorder.attributes, \"$.bep_specified\") IS NULL OR JSON_EXTRACT(tt_preorder.attributes, \"$.bep_specified\") = '0')"; } } if (array_key_exists("create_from", $filter)) { $create_from = $filter['create_from']; if ($create_from) { $create_from = DateTime::createFromFormat('d.m.Y', $create_from); if ($create_from) { $create_from->setTime(0, 0, 0); } if ($create_from) { $where .= " AND tt_preorder.create >= " . $create_from->getTimestamp(); } } } if (array_key_exists("create_to", $filter)) { $create_to = $filter['create_to']; if ($create_to) { $create_to = DateTime::createFromFormat('d.m.Y', $create_to); if ($create_to) { $create_to->setTime(23, 59, 59); } if ($create_to) { $where .= " AND tt_preorder.create <= " . $create_to->getTimestamp(); } } } if (!empty($filter['fcp']) && array_key_exists("preordercampaign_id", $filter)) { $fcp = $filter['fcp']; $db = FronkDB::singleton(); $campaign = new Preordercampaign($filter['preordercampaign_id']); if (is_array($fcp)) { $where .= " AND adb_hausnummer.rimo_fcp_name IN ('" . implode("','", array_map(fn($i) => $db->escape($i), $fcp)) . "')"; } else { $fcp = $db->escape($fcp); if ($fcp) { $where .= " AND adb_hausnummer.rimo_fcp_name = '$fcp'"; } } } if (array_key_exists("onlyShowCustomMailSent", $filter)) { // Only apply the filter if the value is truthy (e.g., true, 1) if ($filter['onlyShowCustomMailSent']) { $where .= " AND tt_preorder.id IN (SELECT preorder_id FROM PreorderStatusnotificationLog WHERE email_type = '300-custom')"; } } // custom where clause if (array_key_exists("add-where", $filter)) { $where .= " " . $filter['add-where']; } //var_dump($filter, $where);exit; return $where; } public static function countStatusFlagsActive($filter = [], $statusFlag = null) { if ($statusFlag === null) { die("Please select a statusflag"); } if (!is_array($filter)) return false; if (!array_key_exists("deleted", $filter)) { $filter["deleted"] = null; } if (!array_key_exists("status_code", $filter) && !array_key_exists("status_code", $filter) && !array_key_exists("status_id", $filter) && !array_key_exists("status_id", $filter)) { $filter["query($sql); mfLoghandler::singleton()->debug("[Query took: " . (microtime(true) - $queryStart) . " seconds] " . $sql); if ($db->num_rows($res)) { $data = $db->fetch_object($res); return $data->cnt; } return 0; } public static function countActivePreorder($preorderCampaignId = null) { $db = FronkDB::singleton(); // The base WHERE clause no longer excludes 'Not2Connect' states. // It only filters for active, non-deleted preorders. $where = "p.deleted = 0 AND tt_preorderstatus.code NOT IN (20) AND tt_preorderstatus.code < 899"; if ($preorderCampaignId) { $where .= " AND p.preordercampaign_id = " . (int)$preorderCampaignId; } $sql = "SELECT -- MD count: Sums connection counts for preorders in Multi-Dwelling Units (tool_building_type = 2) COALESCE(SUM(CASE WHEN h.tool_building_type = 2 THEN COALESCE(p.connection_count, 1) ELSE 0 END), 0) AS md_count, -- Total count: Sums all connection counts for active preorders COALESCE(SUM(COALESCE(p.connection_count, 1)), 0) AS total_count, -- NEW Not2Connect count: Sums connection counts only for preorders where the building state is 'Not2Connect' COALESCE(SUM(CASE WHEN h.rimo_op_state = 'Not2Connect' THEN COALESCE(p.connection_count, 1) ELSE 0 END), 0) AS not2connect_count FROM `".FRONKDB_DBNAME."`.Preorder p LEFT JOIN `".ADDRESSDB_DBNAME."`.Hausnummer h ON p.adb_hausnummer_id = h.id LEFT JOIN `".FRONKDB_DBNAME."`.Preorderstatus tt_preorderstatus ON p.status_id = tt_preorderstatus.id WHERE " . $where; $queryStart = microtime(true); $res = $db->query($sql); mfLoghandler::singleton()->debug("[Query took: ".(microtime(true) - $queryStart)." seconds] " . $sql); if ($db->num_rows($res)) { $data = $db->fetch_object($res); return [ 'md_count' => (int)$data->md_count, 'sd_count' => (int)($data->total_count - $data->md_count), 'total_count' => (int)$data->total_count, 'not2connect_count' => (int)$data->not2connect_count // New return value ]; } return [ 'md_count' => 0, 'sd_count' => 0, 'total_count' => 0, 'not2connect_count' => 0 ]; } public static function countTotalUnits($preorderCampaignId = null) { $db = FronkDB::singleton(); // The new WHERE condition is more complex and implemented directly in the main query. $where = "1=1"; if ($preorderCampaignId) { $where .= " AND pc.id = " . (int)$preorderCampaignId; } // This query now implements the conditional logic for counting units. // A unit is counted if its building type is standard, OR if its type is special AND has an active preorder. $sql = "SELECT pc.id AS campaign_id, -- Total unit count based on the new logic COUNT(w.id) AS total_unit_count, -- SD unit count (Single Dwelling) SUM(CASE WHEN h.tool_building_type IN (0, 1) THEN 1 ELSE 0 END) AS total_unit_count_sd, -- MD unit count (Multi Dwelling) SUM(CASE WHEN h.tool_building_type = 2 THEN 1 ELSE 0 END) AS total_unit_count_md, -- NEW Not2Connect unit count SUM(CASE WHEN h.rimo_op_state = 'Not2Connect' THEN 1 ELSE 0 END) AS total_unit_count_not2connect FROM `".FRONKDB_DBNAME."`.Preordercampaign pc LEFT JOIN `".FRONKDB_DBNAME."`.PreordercampaignSalescluster pcs ON pc.id = pcs.preordercampaign_id LEFT JOIN `".ADDRESSDB_DBNAME."`.Netzgebiet n ON pcs.salescluster_id = n.id LEFT JOIN `".ADDRESSDB_DBNAME."`.Hausnummer h ON n.id = h.netzgebiet_id LEFT JOIN `".ADDRESSDB_DBNAME."`.Wohneinheit w ON h.id = w.hausnummer_id -- Subquery to find all buildings that have at least one active preorder LEFT JOIN ( SELECT p_sub.adb_hausnummer_id FROM `".FRONKDB_DBNAME."`.Preorder p_sub JOIN `".FRONKDB_DBNAME."`.Preorderstatus ps_sub ON p_sub.status_id = ps_sub.id WHERE p_sub.deleted = 0 AND ps_sub.code NOT IN (20) AND ps_sub.code < 899 GROUP BY p_sub.adb_hausnummer_id ) AS active_preorders ON h.id = active_preorders.adb_hausnummer_id WHERE ($where) AND ( -- Condition 1: Include unit if its building rimo_type is NOT one of the special types. h.rimo_type NOT IN ('greenfield', 'other', 'transmitting station', 'transformer station', 'outdoor cabinet') OR -- Condition 2: OR if the rimo_type IS special (or NULL), include it ONLY IF an active preorder exists for the building. ( (h.rimo_type IS NULL OR h.rimo_type IN ('greenfield', 'other', 'transmitting station', 'transformer station', 'outdoor cabinet')) AND active_preorders.adb_hausnummer_id IS NOT NULL ) ) GROUP BY pc.id"; $queryStart = microtime(true); $res = $db->query($sql); mfLoghandler::singleton()->debug("[Query took: ".(microtime(true) - $queryStart)." seconds] " . $sql); if ($db->num_rows($res)) { $data = $db->fetch_object($res); return [ 'total_unit_count' => (int)$data->total_unit_count, 'total_unit_count_sd' => (int)$data->total_unit_count_sd, 'total_unit_count_md' => (int)$data->total_unit_count_md, 'total_unit_count_not2connect' => (int)$data->total_unit_count_not2connect // New return value ]; } return [ 'total_unit_count' => 0, 'total_unit_count_sd' => 0, 'total_unit_count_md' => 0, 'total_unit_count_not2connect' => 0 ]; } public static function countHistoryStatus($filter = [], $status_code = null) { if ($status_code === null) { die("Please select a status code"); } if (!is_array($filter)) return false; if (!array_key_exists("deleted", $filter)) { $filter["deleted"] = null; } if (!array_key_exists("status_code", $filter) && !array_key_exists("status_code", $filter) && !array_key_exists("status_id", $filter) && !array_key_exists("status_id", $filter)) { $filter["query($sql); mfLoghandler::singleton()->debug("[Query took: " . (microtime(true) - $queryStart) . " seconds] " . $sql); if ($db->num_rows($res)) { $data = $db->fetch_object($res); return $data->cnt; } return 0; } public static function getAll300CustomPreorders() { $sql = "SELECT p.id AS preorder_id, p.ucode, p.email AS preorder_email, p.type AS preorder_type, p.preordercampaign_id, pc.name AS campaign_name, ps_current.code AS current_status_code, ps_current.name AS current_status_name, psnl.email AS notification_logged_email, psnl.email_type AS notification_type_logged, FROM_UNIXTIME(psnl.create) AS notification_log_timestamp FROM PreorderStatusnotificationLog psnl INNER JOIN Preorder p ON psnl.preorder_id = p.id INNER JOIN Preordercampaign pc ON p.preordercampaign_id = pc.id INNER JOIN Preorderstatus ps_current ON p.status_id = ps_current.id WHERE psnl.email_type = '300-custom' ORDER BY psnl.create DESC;"; $db = FronkDB::singleton(); $res = $db->query($sql); $items = []; if ($db->num_rows($res)) { while ($data = $db->fetch_object($res)) { $items[] = $data; } } return $items; } public static function getPreorderRimoTypeData(int $campaignId): array { $db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); $fronkDbName = defined('FRONKDB_DBNAME') ? FRONKDB_DBNAME : 'thetool'; $addressDbName = defined('ADDRESSDB_DBNAME') ? ADDRESSDB_DBNAME : 'addressdb'; $safeCampaignId = (int)$campaignId; // add time debug $startTime = microtime(true); $campaign = PreordercampaignModel::getFirst(['id' => $safeCampaignId]); $faultyHausnummerIds = []; if ($campaign && !empty($campaign->rimo_type_map_faults)) { $faults = json_decode($campaign->rimo_type_map_faults, true); foreach ($faults as $fault) { if (empty($fault['done'])) { $faultyHausnummerIds[] = (int)$fault['hausnummer_id']; } } } $visibilityCondition = "AND (h.visibility IS NULL OR h.visibility != 'private')"; if (!empty($faultyHausnummerIds)) { $ids = implode(',', $faultyHausnummerIds); $visibilityCondition = "AND ((h.visibility IS NULL OR h.visibility != 'private') OR h.id IN ({$ids}))"; } $sql = " SELECT h.id AS hausnummer_id, h.gps_lat, h.gps_long, h.rimo_type, h.rimo_op_state, h.rimo_ex_state, h.hausnummer, s.name AS strasse_name, plz.plz AS plz_name, o.name AS ortschaft_name, h.rimo_id, COUNT(DISTINCT we.id) AS wohneinheit_count, COUNT(DISTINCT pr.id) AS preorder_count FROM `{$addressDbName}`.`Hausnummer` AS h LEFT JOIN `{$addressDbName}`.`Wohneinheit` AS we ON h.id = we.hausnummer_id LEFT JOIN `{$fronkDbName}`.`Preorder` AS pr ON we.id = pr.adb_wohneinheit_id AND pr.preordercampaign_id = {$safeCampaignId} AND pr.deleted = 0 AND pr.status_id IS NOT NULL AND pr.status_id IN ( SELECT ps.id FROM `{$fronkDbName}`.`Preorderstatus` AS ps WHERE ps.code < 899 ) LEFT JOIN `{$fronkDbName}`.`Preorderstatus` AS ps ON pr.status_id = ps.id AND ps.code < 899 LEFT JOIN `{$addressDbName}`.`Strasse` AS s ON h.strasse_id = s.id LEFT JOIN `{$addressDbName}`.`Plz` AS plz ON h.plz_id = plz.id LEFT JOIN `{$addressDbName}`.`Ortschaft` AS o ON h.ortschaft_id = o.id WHERE h.netzgebiet_id = ( SELECT n.adb_netzgebiet_id FROM `{$fronkDbName}`.`Preordercampaign` pc JOIN `{$fronkDbName}`.`Network` n ON pc.network_id = n.id WHERE pc.id = {$safeCampaignId} ) AND h.gps_lat IS NOT NULL AND h.gps_long IS NOT NULL {$visibilityCondition} GROUP BY h.id ORDER BY h.id "; // die with query time $result = $db->query($sql); $endTime = microtime(true); $executionTime = $endTime - $startTime; // die("Query executed in {$executionTime} seconds.\n"); return $result ? $result->fetch_all(MYSQLI_ASSOC) : []; } public static function getPreorderRimoFaultsData(int $campaignId): array { $db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); $fronkDbName = defined('FRONKDB_DBNAME') ? FRONKDB_DBNAME : 'thetool'; $addressDbName = defined('ADDRESSDB_DBNAME') ? ADDRESSDB_DBNAME : 'addressdb'; $safeCampaignId = (int)$campaignId; $sql = " SELECT h.id AS hausnummer_id, h.rimo_id as extref, h.gps_lat, h.gps_long, h.rimo_type, h.rimo_op_state, h.rimo_ex_state, h.hausnummer, s.name AS strasse_name, plz.plz AS plz_name, o.name AS ortschaft_name, g.name as gemeinde_name, COUNT(DISTINCT we.id) AS wohneinheit_count FROM `{$addressDbName}`.`Hausnummer` AS h LEFT JOIN `{$addressDbName}`.`Wohneinheit` AS we ON h.id = we.hausnummer_id LEFT JOIN `{$addressDbName}`.`Strasse` AS s ON h.strasse_id = s.id LEFT JOIN `{$addressDbName}`.`Gemeinde` AS g ON s.gemeinde_id = g.id LEFT JOIN `{$addressDbName}`.`Plz` AS plz ON h.plz_id = plz.id LEFT JOIN `{$addressDbName}`.`Ortschaft` AS o ON h.ortschaft_id = o.id WHERE h.netzgebiet_id = ( SELECT n.adb_netzgebiet_id FROM `{$fronkDbName}`.`Preordercampaign` pc JOIN `{$fronkDbName}`.`Network` n ON pc.network_id = n.id WHERE pc.id = {$safeCampaignId} ) AND h.gps_lat IS NOT NULL AND h.gps_long IS NOT NULL GROUP BY h.id ORDER BY s.name, h.hausnummer "; $result = $db->query($sql); return $result ? $result->fetch_all(MYSQLI_ASSOC) : []; } }