148 lines
6.5 KiB
PHP
148 lines
6.5 KiB
PHP
<?php
|
|
|
|
class PreorderIFrameController extends mfBaseController
|
|
{
|
|
private PreorderIFrameModel $preorderIFrameModel;
|
|
|
|
public function init()
|
|
{
|
|
$this->preorderIFrameModel = new PreorderIFrameModel();
|
|
}
|
|
|
|
public function indexAction()
|
|
{
|
|
$vue_config = [
|
|
'baseUrl' => '/PreorderIFrame',
|
|
'clusterId' => $this->request->get('clusterId') ? intval($this->request->get('clusterId')) : null,
|
|
'gemeindeId' => $this->request->get('gemeindeId') ? intval($this->request->get('gemeindeId')) : null,
|
|
'color' => htmlspecialchars($this->request->get('color', 'blue')),
|
|
'orderType' => htmlspecialchars($this->request->get('orderType', 'order')), // 'order' or 'interest'
|
|
];
|
|
|
|
$this->layout()->set("JSGlobals", $vue_config);
|
|
$this->layout()->setTemplate("VueViews/PreorderIFrame");
|
|
}
|
|
|
|
// --- API ENDPOINTS ---
|
|
|
|
public function getClustersAction()
|
|
{
|
|
self::returnJson(['clusters' => $this->preorderIFrameModel->getClusters($_SERVER['HTTP_X_FRAME_REFERRER'])]);
|
|
}
|
|
|
|
public function getClusterInfoAction()
|
|
{
|
|
$clusterId = $this->request->get('cluster_id');
|
|
if (!$clusterId) self::sendError("Cluster ID is required.");
|
|
|
|
$allClusters = $this->preorderIFrameModel->getClusters($_SERVER['HTTP_X_FRAME_REFERRER']);
|
|
$clusterInfo = null;
|
|
foreach ($allClusters as $cluster) {
|
|
if ($cluster['id'] == $clusterId) {
|
|
$clusterInfo = $cluster;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$clusterInfo) self::sendError("No cluster found for the given ID or origin.");
|
|
|
|
$preorderCampaign = new Preordercampaign($clusterInfo['campaign_id']);
|
|
self::returnJson(['iframe_consents' => json_decode($preorderCampaign->iframe_consents ?? '[]')]);
|
|
}
|
|
|
|
public function findCityAction()
|
|
{
|
|
$params = [
|
|
'zip' => $this->request->get('zip'),
|
|
'clusterId' => $this->request->get('cluster_id'),
|
|
'gemeindeId' => $this->request->get('gemeinde_id'),
|
|
];
|
|
self::returnJson(['cities' => $this->preorderIFrameModel->findCities($params)]);
|
|
}
|
|
|
|
public function findStreetAction()
|
|
{
|
|
$params = [
|
|
'zip' => $this->request->get('zip'),
|
|
'city' => $this->request->get('city'),
|
|
'clusterId' => $this->request->get('cluster_id'),
|
|
'gemeindeId' => $this->request->get('gemeinde_id'),
|
|
];
|
|
self::returnJson(['streets' => $this->preorderIFrameModel->findStreets($params)]);
|
|
}
|
|
|
|
public function findAddressAction()
|
|
{
|
|
self::returnJson(['addresses' => $this->preorderIFrameModel->findAddresses($_GET)]);
|
|
}
|
|
|
|
public function submitOrderAction()
|
|
{
|
|
$preorderData = json_decode(file_get_contents('php://input'), true);
|
|
if (json_last_error() !== JSON_ERROR_NONE) self::sendError("Invalid JSON data.");
|
|
|
|
// Determine network/campaign from clusterId or gemeindeId
|
|
$networkId = null;
|
|
if (!empty($preorderData['additionalData']['clusterId'])) {
|
|
$tt_network = NetworkModel::getFirst(['adb_network_id' => $preorderData['additionalData']['clusterId']]);
|
|
if ($tt_network) $networkId = $tt_network->id;
|
|
} elseif (!empty($preorderData['additionalData']['gemeindeId'])) {
|
|
$gn = ADBGemeindeNetzgebietModel::getFirst(['gemeinde_id' => $preorderData['additionalData']['gemeindeId']]);
|
|
if ($gn) {
|
|
$tt_network = NetworkModel::getFirst(['adb_netzgebiet_id' => $gn->netzgebiet_id]);
|
|
if ($tt_network) $networkId = $tt_network->id;
|
|
}
|
|
}
|
|
|
|
if (!$networkId) self::sendError("No network/campaign found for the given area.");
|
|
$campaign = PreordercampaignModel::getFirst(['network_id' => $networkId]);
|
|
if (!$campaign) self::sendError("No campaign found for the given area.");
|
|
|
|
$h = new ADBHausnummer($preorderData['address']['hausnummer_id']);
|
|
if (!$h->id) self::sendError("Invalid house number ID provided.");
|
|
|
|
$w = new ADBWohneinheit($preorderData['address']['wohneinheit_id']);
|
|
if ($preorderData['address']['wohneinheit_id'] && !$w->id) self::sendError("Invalid unit ID provided.");
|
|
|
|
$data = [];
|
|
$data['preordercampaign_id'] = $campaign->id;
|
|
$data['adb_hausnummer_id'] = $preorderData['address']['hausnummer_id'];
|
|
$data['adb_wohneinheit_id'] = $preorderData['address']['wohneinheit_id'];
|
|
|
|
$new_status = null;
|
|
if ($data['adb_wohneinheit_id'] && $w->id) {
|
|
$status_code = max($w->status->code, $w->hausnummer->status->code);
|
|
$new_status = PreorderstatusModel::getFirst(["code" => $status_code]);
|
|
} elseif ($data['adb_hausnummer_id'] && $h->id) {
|
|
$new_status = PreorderstatusModel::getFirst(["code" => $h->status->code]);
|
|
}
|
|
$data["status_id"] = $new_status ? $new_status->id : 1;
|
|
|
|
$data['type'] = $preorderData['preorderType'] ?? 'order'; // 'order', 'interest', 'provision'
|
|
$data['connection_type'] = $preorderData['customerType'] === 'business' ? 'business' : 'single-dwelling';
|
|
|
|
$data['accept_agb'] = $preorderData['acceptAgb'] ? 1 : 0;
|
|
$data['accept_dsgvo'] = $preorderData['acceptDsgvo'] ? 1 : 0;
|
|
$data['accept_marketing'] = $preorderData['acceptMarketing'] ? 1 : 0;
|
|
$data['accept_withdrawal'] = $preorderData['acceptWithdrawal'] ? 1 : 0;
|
|
$data['submit_request'] = json_encode($preorderData);
|
|
|
|
$data['firstname'] = trim($preorderData['customer']['firstname']);
|
|
$data['lastname'] = trim($preorderData['customer']['lastname']);
|
|
$data['company'] = (trim($preorderData['customer']['company'])) ?: null;
|
|
$data['street'] = (trim($preorderData['customer']['street'])) ?: null;
|
|
$data['housenumber'] = (trim($preorderData['customer']['housenumber'])) ?: null;
|
|
$data['zip'] = (trim($preorderData['customer']['zip'])) ?: null;
|
|
$data['city'] = (trim($preorderData['customer']['city'])) ?: null;
|
|
$data['phone'] = (trim($preorderData['customer']['phone'])) ?: null;
|
|
$data['email'] = (trim($preorderData['customer']['email'])) ?: null;
|
|
$data['edit_by'] = 1;
|
|
$data['create_by'] = 1;
|
|
|
|
$preorder = PreorderModel::create($data);
|
|
$preorder->createUcode();
|
|
if (!$preorder->save()) self::sendError("Failed to create preorder record.");
|
|
|
|
self::returnJson(['orderCode' => $preorder->ucode, 'status' => 'success']);
|
|
}
|
|
} |