ordersprinter/webapp/php/utilities/operations.php

62 lines
2.4 KiB
PHP
Raw Normal View History

2020-11-19 23:14:48 +01:00
<?php
require_once (__DIR__. '/../dbutils.php');
class Operations {
public static function createOperation($pdo,$opType,$table,
$logtime,
$trans,
$signtxt,
$tseSignature,
$pubkeyRef,
$sigalgRef,
$serialNoRef,
$certificateRef,
$sigcounter,
$tseerror) {
$terminalInfo = Terminals::getTerminalInfo();
$terminalEntryId = Terminals::createOrReferenceTerminalDbEntry($pdo, $terminalInfo);
$range = RANGE_ORDER;
if ($opType == DbUtils::$PROCESSTYPE_VORGANG) {
$range = RANGE_ORDER;
} else if ($opType == DbUtils::$PROCESSTYPE_BELEG) {
$range = RANGE_BILL;
2020-11-19 23:14:58 +01:00
} else if ($opType == DbUtils::$PROCESSTYPE_SONSTIGER_VORGANG) {
$range = RANGE_CLOSING;
2020-11-19 23:14:48 +01:00
}
$sql = "SELECT MAX(COALESCE(bonid,0)) as maxbonid FROM %operations% WHERE typerange=?";
$res = CommonUtils::fetchSqlAll($pdo, $sql,array($range));
$maxbonid = intval($res[0]["maxbonid"]);
$sql = "SELECT MAX(COALESCE(id,0)) as maxid FROM %operations%";
$res = CommonUtils::fetchSqlAll($pdo, $sql);
$maxid = intval($res[0]["maxid"]);
2020-11-19 23:15:04 +01:00
if (strlen($signtxt) > 120) {
// the signtxt is only for debug purposes. So cut it and show that it was cut
$signtxt = substr($signtxt, 0, 117) . "...";
}
2020-11-19 23:15:07 +01:00
// In parallel transactions and with transaction isolatoon level REPEATABLE READ it may be possible that the max id is already set by another transaction.
// Therefore run this in a try catch block just in case of a deadlock and increment the maxid in catch
// In a very very unikely case of race condition still the bonid can be created multiple times, but the unique primary key id is evidence that there is no
// intention in creating an invalid operation. Therefore also this comment is left in this source code!
$attempt = 0;
$ok = false;
while (!$ok) {
try {
$sql = "INSERT INTO %operations% (id,typerange,bonid,processtype,handledintable,logtime,trans,sigcounter,tsesignature,pubkey,sigalg,serialno,certificate,signtxt,tseerror,terminalid) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
CommonUtils::execSql($pdo, $sql, array($maxid+1,$range,$maxbonid+1,$opType,$table,$logtime,$trans, $sigcounter, $tseSignature, $pubkeyRef, $sigalgRef, $serialNoRef,$certificateRef, $signtxt, $tseerror, $terminalEntryId));
$ok = true;
} catch (Exception $ex) {
$maxid++;
$attempt++;
}
}
2020-11-19 23:14:48 +01:00
$opid = $pdo->lastInsertId();
return $opid;
}
}