PreorderBilling WIP & Api changed

This commit is contained in:
Frank Schubert
2025-03-20 16:02:05 +01:00
parent 2a7e7f1724
commit 9353591421
13 changed files with 335 additions and 54 deletions

View File

@@ -512,6 +512,14 @@ $pagination_entity_name = "Vorbestellungen";
<script type="text/javascript">
$(document).ready(function() {
$(".datepicker").datepicker({
//orientation: "bottom",
language: 'de',
format: "dd.mm.yyyy",
showWeekDays: true,
todayBtn: 'linked',
autoclose: true
});
$("#filter_type").select2({closeOnSelect: false});
$("#filter_status").select2({closeOnSelect: false});
$("#filter_partner_id").select2({closeOnSelect: false});
@@ -1195,6 +1203,62 @@ $pagination_entity_name = "Vorbestellungen";
'json');
}
function toggleActivationdateControl(pid) {
// set select to current status id
//$("#preorder-detail-activationdate-" + pid + "-input select").val($("#preorder-detail-activationdate-" + pid + "-text").data("activationdate"));
// toggle controls
$("#preorder-detail-activationdate-" + pid + "-text").toggle();
$("#preorder-detail-activationdate-" + pid + "-input").toggle();
return false;
}
function savePreorderActivationdateControl(pid) {
if(!Number.isInteger(pid) || pid < 1) {
return false;
}
var value = $("#preorder-detail-activationdate-" + pid + "-input input").val();
//console.log("add opacity-5 to ")
$("#preorder-" + pid + "-body").addClass("opacity-5");
$("#preorder-" + pid + "-body .loader-big").show();
// reset loading overlay if request times out
setTimeout(() => {
$("#preorder-" + pid + "-body").removeClass("opacity-5");
$("#preorder-" + pid + "-body .loader-big").hide();
}, 5000);
$.post("<?=self::getUrl("Preorder","Api")?>",
{
'do': "saveActivationdate",
id: pid,
activation_date: value
},
function(success) {
if(success.status == "OK") {
console.log(success);
var new_activationdate = success.result.activation_date;
var pid = success.result.preorder_id;
//console.log(updates);
$("#preorder-detail-activationdate-" + pid + "-textpart").text(new_activationdate);
$("#preorder-detail-activationdate-" + pid + "-input").val(new_activationdate);
$("#preorder-detail-activationdate-" + pid + "-text").addClass("text-success");
setTimeout(() => { $("#preorder-detail-activationdate-" + pid + "-text").removeClass("text-success") }, 1500);
toggleActivationdateControl(pid);
}
$("#preorder-" + pid + "-body").removeClass("opacity-5");
$("#preorder-" + pid + "-body .loader-big").hide();
},
'json');
}
function createWorkorder(pid) {
if(!Number.isInteger(pid) || pid < 1) {
return false;

View File

@@ -85,6 +85,20 @@
</div>
</td>
</tr><tr>
<th>Aktivierungsdatum (Status 500):</th>
<td>
<?php if($preorder->getStatuschangeTo(500)): ?>
<span id="preorder-detail-activationdate-<?=$preorder->id?>-text" data-activationdate="<?=($preorder->getStatuschangeTo(500)) ? (new DateTime("@".$preorder->getStatuschangeTo(500)))->format("d.m.Y") : ""?>"><span id="preorder-detail-activationdate-<?=$preorder->id?>-textpart"><?=($preorder->getStatuschangeTo(500)) ? (new DateTime("@".$preorder->getStatuschangeTo(500)))->format("d.m.Y") : ""?></span> <a href="#" onclick="return toggleActivationdateControl(<?=$preorder->id?>)"><i class="fas fa-fw fa-edit"></i></a></span>
<div class="input-group" id="preorder-detail-activationdate-<?=$preorder->id?>-input" style="display:none">
<input type="text" class="form-control datepicker" value="<?=($preorder->getStatuschangeTo(500)) ? (new DateTime("@".$preorder->getStatuschangeTo(500)))->format("d.m.Y") : ""?>" />
<div class="input-group-append">
<button type="button" class="btn btn-primary" title="Speichern" onclick="savePreorderActivationdateControl(<?=$preorder->id?>)"><i class="fas fa-check"></i></button>
<button type="button" class="btn btn-secondary" title="Abbrechen" onclick="toggleActivationdateControl(<?=$preorder->id?>)"><i class="fas fa-times"></i></button>
</div>
</div>
<?php endif; ?>
</td>
</tr><tr>
<th>Erstellt:</th>
<td class="text-monospace"><?=date("d.m.Y H:i",$preorder->create)?> (<?=$preorder->creator->name?>)</td>
</tr><tr>

View File

@@ -929,28 +929,24 @@ class AddressdbApicontroller extends mfBaseApicontroller {
}
$prices_return = [
"price_inet" => (float)$prices["operator_usage"]->price_inet,
"price_inet_tv" => (float)$prices["operator_usage"]->price_inet_tv,
"price_catv" => (float)$prices["operator_usage"]->price_catv,
"operator_setup_price" => (float)$prices["operator_setup"]->price_setup,
"operator_setup_info" => $prices["operator_setup"]->description,
"operator_setup_valid_until" => $prices["operator_setup"]->end_date,
"enduser_setup_price" => (float)$prices["enduser_setup"]->price_setup,
"oaid" => $oaid,
"enduser_setup_price_net" => (float)$prices["enduser_setup"]->price_setup,
"enduser_setup_price_gross" => (float)$prices["enduser_setup"]->price_setup * 1.2,
"enduser_setup_info" => $prices["enduser_setup"]->description,
"enduser_setup_valid_until" => $prices["enduser_setup"]->end_date,
"vatrate" => 20,
];
if(PreorderBilling::getFirst(["adb_wohneinheit_id" => $unit->id, "product_id" => $enduser_setup_product_id])) {
$prices_return["enduser_setup_price"] = 0;
//$paid = $unit->enduser_setup_paid;
if(PreorderBilling::getFirst(["adb_wohneinheit_id" => $unit->id, "product_id" => $enduser_setup_product_id, "invoice_id" => false])) {
$prices_return["enduser_setup_price_net"] = 0;
$prices_return["enduser_setup_price_gross"] = 0;
$prices_return["enduser_setup_info"] = "paid";
$prices_return["enduser_setup_valid_until"] = null;
}
return mfResponse::Ok(['oaid' => $oaid, 'prices' => $prices_return]);
return mfResponse::Ok($prices_return);
}
protected function exportAddresses() {

View File

@@ -21,6 +21,11 @@ class Activation extends Modules\ApiControllerModule {
if($this->me->is("Preorderreadonly")) return \mfResponse::Forbidden();
$code = trim($code);
$activation_date = false;
if(array_key_exists("activation_date", $this->get)) {
$activation_date = trim($this->get['activation_date']);
}
if(!$code) {
return \mfResponse::NotFound(["message" => "Preorder not found"]);
}
@@ -42,15 +47,42 @@ class Activation extends Modules\ApiControllerModule {
if($preorder->partner_id != $this->me->address_id) {
return \mfResponse::NotFound(["message" => "Preorder not found"]);
}
if($activation_date) {
try {
$adate = new \DateTime($activation_date);
$adate->setTimezone(new \DateTimeZone("Europe/Vienna"));
$adate->setTime(2, 0, 0); // set time to 2:00:00
// activation date cannot be in the future
if($adate > new \DateTime()) {
return \mfResponse::BadRequest(["message" => "Activation date can only be in the past"]);
}
} catch(\Exception $e) {
return \mfResponse::BadRequest(["message" => "Invalid activation_date format"]);
}
}
// set status to 500
if($preorder->status->code < 500) {
if($preorder->status->code < 242) {
return \mfResponse::BadRequest(["message" => "Service cannot be activated, because Construction work is not yet finished"]);
}
$new_status = \PreorderstatusModel::getFirst(["code" => 500]);
if(!$new_status) {
return \mfResponse::InternalServerError();
}
$preorder->status_id = $new_status->id;
$preorder->save();
if($adate) {
$history = \PreorderHistoryModel::getLastStatusChangeTo($preorder->id, 500);
if($history) {
$history->changed = $adate->getTimestamp();
$history->save();
}
}
}
return \mfResponse::Ok(["message" => "Status successfully updated."]);

View File

@@ -856,11 +856,31 @@ class PreorderApicontroller extends mfBaseApicontroller {
}
if(trim($this->post['orderDate'])) {
$order_date = trim($this->post['orderDate']);
try {
$order_date = new DateTime(trim($this->post['orderDate']));
$order_date->setTime(4, 0, 0);
$order_date->setTimezone(new DateTimeZone('Europe/Vienna'));
// cannot be older than 14 days
$now = new DateTime();
$now->setTime(4, 0, 0);
$now->setTimezone(new DateTimeZone('Europe/Vienna'));
$diff = $now->diff($order_date);
if($diff->days > 14) {
return mfResponse::BadRequest(["message" => "orderDate cannot be older than 14 days"]);
}
$preorder_data['order_date'] = $order_date->getTimestamp();
} catch(Exception $e) {
$this->log->debug(__METHOD__.": Error parsing orderDate: " . $e->getMessage());
// ignore
}
/*$order_date = trim($this->post['orderDate']);
$m = [];
if(preg_match('/^(\d\d\d\d)-(\d\d)-(\d\d)$/', $order_date, $m)) {
$preorder_data['order_date'] = mktime(4, 0, 0, $m[2], $m[3], $m[1]);
}
}*/
}
if($is_additional_order) {

View File

@@ -99,6 +99,20 @@ class Preorder extends mfBaseModel {
mfValuecache::singleton()->delete("preorder-save-nesting-level-" . $this->id);
}
public function getStatuschangeTo($status_code) {
$status = PreorderstatusModel::getFirst(["code" => $status_code]);
if(!$status) {
return false;
}
$history = PreorderHistoryModel::getLastStatusChangeTo($this->id, $status_code);
if(!$history) {
return false;
}
return $history->changed;
}
public function updateRimoWorkorderContact() {
$contact_fields = [
"company",
@@ -141,7 +155,8 @@ class Preorder extends mfBaseModel {
"preorder_id" => $this->id,
"key" => $field,
"old_value" => $this->_old_data->$field,
"new_value" => $this->data->$field
"new_value" => $this->data->$field,
"changed" => date("U")
]);
$history->save();
}

View File

@@ -1064,6 +1064,9 @@ class PreorderController extends mfBaseController {
case "saveOrderdate":
$return = $this->saveOrderdateApi();
break;
case "saveActivationdate":
$return = $this->saveActivationdateApi();
break;
default:
$return = false;
}
@@ -1097,6 +1100,34 @@ class PreorderController extends mfBaseController {
return ["message" => "Orderdate saved successfully", "preorder_id" => $preorder_id, "order_date" => $order_date];
}
private function saveActivationdateApi() {
$preorder_id = $this->request->id;
$activation_date = $this->request->activation_date;
$preorder = new Preorder($preorder_id);
if(!$preorder->id) {
$this->log->debug(__METHOD__.": preorder ($preorder_id) not found");
return false;
}
try {
$activationdate = DateTime::createFromFormat("d.m.Y", $activation_date, new DateTimeZone("Europe/Vienna"));
$activationdate->getTimestamp();
} catch(Exception $e) {
return false;
}
$history = PreorderHistoryModel::getLastStatusChangeTo($preorder->id, 500);
if(!$history) {
return false;
}
$history->changed = $activationdate->getTimestamp();
$history->save();
return ["message" => "Activationdate saved successfully", "preorder_id" => $preorder_id, "activation_date" => $activation_date];
}
private function addWorkorderRemarkApi() {
$preorder_id = $this->request->preorder_id;
$workorder_id = $this->request->workorder_id;

View File

@@ -199,12 +199,12 @@ class PreorderBilling extends mfBaseModel {
if(array_key_exists("invoice_id", $filter)) {
$invoice_id = $filter['invoice_id'];
if(is_numeric($invoice_id)) {
$where .= " AND PreorderBilling.invoice_id=$invoice_id";
} elseif($invoice_id === null || $invoice_id === false) {
if($invoice_id === null || $invoice_id === false) {
$where .= " AND (PreorderBilling.invoice_id IS NULL OR PreorderBilling.invoice_id=0)";
} elseif($invoice_id === true) {
$where .= " AND (PreorderBilling.invoice_id IS NOT NULL AND PreorderBilling.invoice_id > 0)";
} elseif(is_numeric($invoice_id)) {
$where .= " AND PreorderBilling.invoice_id=$invoice_id";
}
}

View File

@@ -255,7 +255,7 @@ class PreorderBillingController extends mfBaseController {
if($preorder->status->code >= 899) {
// TODO is canceled, need to determine if setup still needs to be billed
$this->log->debug(__METHOD__." already cancelled");
$this->log->debug(__METHOD__.": already cancelled");
return true;
}
@@ -263,14 +263,14 @@ class PreorderBillingController extends mfBaseController {
$product->setNetoperatorId($netoperator->id);
$price = $product->getCampaignPrice($preorder->preordercampaign_id, $order_date->format("Y-m-d"));
if(!$price) {
die("operator_setup price not found for campaign ".$preorder->preordercampaign_id." and date ".$order_date->format("Y-m-d"));
die("operator_setup price not found for netoperator ".$netoperator->id.", campaign ".$preorder->preordercampaign_id." and date ".$order_date->format("Y-m-d"));
}
// check for existing billing record
//var_dump($product);
if(PreorderBilling::getFirst(["preorder_id" => $preorder->id, "product_id" => $product->id])) {
//echo "billing record exists\n<br />";
$this->log->debug(__METHOD__."billing record exists");
$this->log->debug(__METHOD__.": billing record exists");
return true; // already billed
}
@@ -432,7 +432,7 @@ class PreorderBillingController extends mfBaseController {
//var_dump($product, $price);exit;
$status_change = PreorderHistoryModel::getFirstStatusChangeToOrHigher($preorder->id, 500);
$status_change = PreorderHistoryModel::getLastStatusChangeTo($preorder->id, 500);
if(!$status_change) {
$this->log->debug(__METHOD__.": No status change to 500 found for preorder ".$preorder->id." so using creation date");
$status_change = $preorder;

View File

@@ -9,6 +9,35 @@ class PreorderHistory extends mfBaseModel {
private $old;
private $new;
public function afterSave() {
$this->createHistoryEntry();
}
protected function createHistoryEntry() {
if(!$this->id) return true;
$changed = $this->getChangedFields();
if($this->key == "status_id" && in_array("changed", $changed) && $this->_old_data->changed) {
try {
$status = new Preorderstatus($this->new_value);
if(!$status->id) return true;
$this->log->debug(__METHOD__ . ": 'changed' changed from '" . $this->_old_data->changed . "' to '" . $this->data->changed . "'");
$history = PreorderHistoryModel::create([
"preorder_id" => $this->preorder_id,
"key" => "status_changed-".$status->code,
"old_value" => $this->_old_data->changed,
"new_value" => $this->data->changed,
"changed" => date("U")
]);
$history->save();
} catch(Exception $e) {
$this->log->debug($e->getTraceAsString());
}
}
}
public function getValue($type = "new", $raw = false) {
if($type != "old" && $type != "new") return null;
@@ -51,6 +80,16 @@ class PreorderHistory extends mfBaseModel {
return "Status Flag ".$psf->code." - ".$psf->name;
}
}
if(preg_match('/status_changed-(\d+)/', $key, $m)) {
if(array_key_exists(1, $m)) {
$code = $m[1];
$ps = PreorderstatusModel::getFirst(["code" => $code]);
if($code == 500) {
return "Aktivierungsdatum (Status 500)";
}
return "Status ".$ps->code." Timestamp";
}
}
return $key;
}
@@ -73,6 +112,10 @@ class PreorderHistory extends mfBaseModel {
return date("d.m.Y", $value);
}
if(preg_match('/^status_changed-/', $this->key)) {
return date("d.m.Y", $value);
}
if(!is_object($value)) {
return $value;
}

View File

@@ -6,6 +6,7 @@ class PreorderHistoryModel {
public $key;
public $old_value;
public $new_value;
public $changed;
public $create_by;
public $edit_by;
public $create;
@@ -47,9 +48,9 @@ class PreorderHistoryModel {
$status_id = $status->id;
$sql = "SELECT * FROM PreorderHistory WHERE preorder_id = $preorder_id AND `key` = 'status_id' AND new_value = $status_id";
if($type == "first") {
$sql .= " ORDER BY id ASC LIMIT 1";
$sql .= " ORDER BY `create` ASC LIMIT 1";
} else {
$sql .= " ORDER BY id DESC LIMIT 1";
$sql .= " ORDER BY `create` DESC LIMIT 1";
}
//mfLoghandler::singleton()->debug($sql);
$res = FronkDB::singleton()->query($sql);

View File

@@ -52,28 +52,13 @@ class PreordercampaignOperatorIspModel {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT PreordercampaignOperatorIsp.* FROM PreordercampaignOperatorIsp
LEFT JOIN PreordercampaignOperator ON (PreordercampaignOperatorIsp.campaignoperator_id = PreordercampaignOperator.id)
WHERE $where
ORDER BY id
LIMIT 1";
mfLoghandler::singleton()->debug($where);
$res = $db->select("PreordercampaignOperatorIsp", "*", "$where ORDER BY id");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreordercampaignOperatorIsp($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function getFirstOaid($oaid) {
$db = FronkDB::singleton();
if(!$oaid) return null;
$where = self::getSqlFilter(["oaid" => $oaid]);
//mfLoghandler::singleton()->debug($where);
$res = $db->select("PreordercampaignOperatorIsp", "*", "$where ORDER BY id");
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreordercampaignOperatorIsp($data);
@@ -90,8 +75,9 @@ class PreordercampaignOperatorIspModel {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM PreordercampaignOperatorIsp
WHERE $where
$sql = "SELECT COUNT(PreordercampaignOperatorIsp.*) as cnt FROM PreordercampaignOperatorIsp
LEFT JOIN PreordercampaignOperator ON (PreordercampaignOperatorIsp.campaignoperator_id = PreordercampaignOperator.id)
WHERE $where
";
$res = $db->query($sql);
@@ -107,9 +93,10 @@ class PreordercampaignOperatorIspModel {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreordercampaignOperatorIsp
WHERE $where
ORDER BY id";
$sql = "SELECT PreordercampaignOperatorIsp.* FROM PreordercampaignOperatorIsp
LEFT JOIN PreordercampaignOperator ON (PreordercampaignOperatorIsp.campaignoperator_id = PreordercampaignOperator.id)
WHERE $where
ORDER BY id";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
@@ -153,6 +140,14 @@ class PreordercampaignOperatorIspModel {
}
}
if(array_key_exists("preordercampaign_id", $filter)) {
$preordercampaign_id = $filter['preordercampaign_id'];
if(is_numeric($preordercampaign_id)) {
$where .= " AND preordercampaign_id = $preordercampaign_id";
}
}
//var_dump($filter, $where);exit;
return $where;
}

View File

@@ -234,6 +234,69 @@ paths:
Parameter missing or malformed
'401':
description: Unauthorized
/addressdb/{oaid}/pricing:
get:
tags:
- addressdb
summary: Liefert Endkundenpreise
description: Liefert Endkundenpreise für die angegebene Adresse
operationId: getAddressPricing
parameters:
- name: oaid
description: "**OAID** der Wohneinheit; Wie in `searchAddress` oder `findAddress` zurückgegeben. Preise sind `0`, wenn Herstellungsentgelt bereits bezahlt wurde."
in: path
schema:
type: string
required: true
responses:
'200':
description: Successful operation
content:
application/json:
schema:
type: object
properties:
status:
type: string
description: Status string
example: OK
result:
type: object
properties:
oaid:
type: string
description: OAID der Wohneinheit
example: "AT-9999-abcdef01"
enduser_setup_price_net:
type: number
description: Herstellungsentgelt für Endkunde Netto
example: 1250
enduser_setup_price_gross:
type: number
description: Herstellungsentgelt für Endkunde Brutto
example: 1500
enduser_setup_info:
type: string
description: Zusätzliche Information zum Preis
example: Rabattaktion in Gemeinde
nullable: true
enduser_setup_valid_until:
type: string
format: date-time
description: Gültigkeitsdatum des Herstellungsentgelts, sofern in Aktionszeitraum, sonst `null`
example: "2025-05-31"
nullable: true
vatrate:
type: number
description: "Mehrwertsteuersatz in %"
example: 20
'400':
description: |
Bad Request
Parameter missing or malformed
'401':
description: Unauthorized
/addressdb/findStreet:
get:
tags:
@@ -831,8 +894,8 @@ paths:
tags:
- preorder
summary: Bestellung auf Status 500 - Fertiggestellt setzen
description: Setzt Status der Bestellung auf 500 - Fertiggestellt. Wird gesetzt, wenn beim Kunden alle Installationsarbeiten abgeschlossen sind und der Service aktiv ist.
operationId: setInhouseIntallationFinished
description: Setzt Status der Bestellung auf 500 - Fertiggestellt. Wird gesetzt, wenn beim Kunden alle Installationsarbeiten abgeschlossen sind und der Service aktiv ist. Optional mit Aktivierungsdateum
operationId: setInhouseIntallationFinishedWithActivationDate
parameters:
- name: id
in: path
@@ -840,6 +903,13 @@ paths:
required: true
schema:
type: string
- name: activation_date
in: query
description: (Optional) Tatsächliches Aktivierungsdatum bei nachträglicher Aktivmeldung. Muss in der Vergangenheit liegen. ISO 8601 Format
required: false
schema:
type: string
format: date-time
responses:
'200':
description: Successful operation
@@ -1262,7 +1332,7 @@ components:
orderDate:
type: string
format: date
description: Bestell- oder Vertragsdatum ISO 8601 Format
description: Bestell- oder Vertragsdatum ISO 8601 Format. Darf nicht älter als 14 Tage sein
example: "2023-02-01"
preorderType:
type: string