Merge branch 'fronkdev' into 'master'

Preorder admin function: Create Rimo Workorder

See merge request fronk/thetool!19
This commit is contained in:
Frank Schubert
2023-05-18 13:53:20 +00:00
4 changed files with 181 additions and 23 deletions

View File

@@ -25,12 +25,37 @@
<div class="card">
<div class="card-body">
<div class="row col" id="map-link">
<div class="form-group">
<a href="<?=self::getUrl("Preordercampaign","createRimoWorkorders", ["id" => $campaign->id])?>" class="btn btn-secondary"><i class="far fa-r"></i> <i class="far fa-gears"></i> Workorders in RIMO erstellen</a><br />
Erstellt Workorders für Vorbestelllungen, für die noch keine Workorders erstellt wurden
<h4>Workorders erstellen</h4>
<div class="row justify-content-end">
<div class="col-10">
<div class="row">
<div class="col">
Workorders erstellen für Vorbestellungen, für die noch keine Workorders erstellt wurden:<br />
</div>
</div>
<div class="row mt-1">
<div class="col">
<a href="<?=self::getUrl("Preordercampaign","createRimoWorkorders", ["id" => $campaign->id])?>" class="btn btn-primary"><i class="far fa-r"></i> <i class="far fa-gears"></i> Workorders in RIMO erstellen</a><br />
</div>
</div>
</div>
</div>
<?php if($missing_extref_count || $failed_count): ?>
<div class="row justify-content-end mt-2">
<div class="col-10">
<div class="alert alert-warning">
<?php if($missing_extref_count): ?>
<?=$missing_extref_count?> Wohneinheiten ohne extref (SDIHome_...)<br />
<?php endif; ?>
<?php if($failed_count): ?>
<?=$failed_count?> Workorders konnten nicht erstellt werden.
<?php endif; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>

View File

@@ -441,6 +441,13 @@ class PreorderModel {
}
}
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)";
}
}
if(array_key_exists("gemeinde", $filter)) {
$gemeinde = FronkDB::singleton()->escape($filter['gemeinde']);
if($gemeinde) {

View File

@@ -130,24 +130,6 @@ class PreordercampaignController extends mfBaseController {
return $owners;
}
protected function adminAction() {
$this->layout()->setTemplate("Preordercampaign/Admin");
$id = $this->request->id;
if(!is_numeric($id) || !$id) {
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
$this->redirect("Preordercampaign");
}
$campaign = new Preordercampaign($id);
if(!$campaign->id) {
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
$this->redirect("Preordercampaign");
}
$this->layout()->set("campaign", $campaign);
}
protected function addAction() {
if(!$this->me->is("Admin")) {
$this->redirect("Preordercampaign");
@@ -425,4 +407,148 @@ class PreordercampaignController extends mfBaseController {
}
}
protected function adminAction() {
$this->layout()->setTemplate("Preordercampaign/Admin");
$id = $this->request->id;
if(!is_numeric($id) || !$id) {
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
$this->redirect("Preordercampaign");
}
$campaign = new Preordercampaign($id);
if(!$campaign->id) {
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
$this->redirect("Preordercampaign");
}
$this->layout()->set("campaign", $campaign);
}
protected function createRimoWorkordersAction() {
$this->layout()->setTemplate("Preordercampaign/Admin");
$id = $this->request->id;
if(!is_numeric($id) || !$id) {
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
$this->redirect("Preordercampaign");
}
$campaign = new Preordercampaign($id);
if(!$campaign->id) {
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
$this->redirect("Preordercampaign");
}
$missing_units = [];
$missing_extrefs = [];
$workorders_created = 0;
$workorders_failed = 0;
foreach(PreorderModel::search(["preordercampaign_id" => $id, "workorder_export_date" => null]) as $preorder) {
if($preorder->workorder_export_date) {
$this->log->warning(__METHOD__.": workorder_export_date not null");
}
if(!$preorder->adb_wohneinheit_id) {
//echo "Keine Wohneinheit in Preorder (".$preorder->id.") ".$preorder->ucode." ".$preorder->oaid."\n";
$missing_units[] = $preorder;
continue;
}
$wohneinheit = new ADBWohneinheit($preorder->adb_wohneinheit_id);
if(!$wohneinheit->id) {
$this->log->error(__METHOD__.": Wohneinheit nicht gefunden (Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid.")");
continue;
}
if(!$wohneinheit->extref) {
$missing_extrefs[] = $preorder;
continue;
}
if(!$preorder->oaid) {
if(!$wohneinheit->oaid) {
$wohneinheit->oaid = $wohneinheit->getNewOAID();
$wohneinheit->save();
}
$preorder->oaid = $wohneinheit->oaid;
$preorder->save();
}
$params['apiKey'] = RIMO_API_JSON_APIKEY;
$params["homeId"] = $wohneinheit->extref;
$params["firstName"] = trim($preorder->firstname);
$params["lastName"] = trim($preorder->lastname);
$params["company"] = trim($preorder->company);
$params["phone"] = trim($preorder->phone);
$params["email"] = trim($preorder->email);
$params["orderId"] = $preorder->ucode;
//$params["openAccessId"] = $preorder->oaid; // TODO: flag in campaign to include OAID or not
$ctx_opts = [
'http' => [
'method' => 'POST',
'header' => 'accept: application/json'
]
];
$qs = http_build_query($params);
//echo $qs."\n";
$createOrderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_CREATE_WORKORDER;
$post_url = $createOrderEp."?".$qs;
$ctx = stream_context_create($ctx_opts);
$this->log->debug(__METHOD__.": Creating Workorder: $post_url");
$response = file_get_contents($post_url, false, $ctx);
if($response === false) {
echo "Fehler beim Exportieren der Workorder für Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid."\n";
$workorders_failed++;
continue;
}
$preorder->workorder_export_date = date('U');
$preorder->workorder_export_data = $response;
$preorder->save();
$workorders_created++;
}
$errors = [];
$warnings = [];
if(count($missing_units)) {
$warnings[] = count($missing_units)." Vobestellungen ohne Wohneinheit";
}
if(count($missing_extrefs)) {
$warnings[] = count($missing_extrefs)." Wohneinheiten ohne extref (SDIHome_)";
}
if($workorders_failed) {
$errors[] = "$workorders_failed Workorders konnten nicht erstellt werden.";
}
if(count($errors)) {
$this->layout()->setFlash(implode("<br />\n", $errors), "error");
}
if(count($warnings)) {
$this->layout()->setFlash(implode("<br />\n", $warnings), "warning");
}
if($workorders_created) {
$this->layout()->setFlash("$workorders_created Workorders erfolgreich erstellt", "success");
} else {
$this->layout()->setFlash("Es konnten keine Workorders erstellt werden", "info");
}
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
}
}

View File

@@ -30,7 +30,7 @@ $ctx_opts = [
$missing_units = [];
$missing_extrefs = [];
foreach(PreorderModel::search(["preordercampaign_id" => $campaign_prem_sued ]) as $preorder) {
foreach(PreorderModel::search(["preordercampaign_id" => $campaign_prem_sued]) as $preorder) {
if(in_array($preorder->id, $ignore_preorders)) continue;
if($preorder->workorder_export_date) continue;