Skip to content

Commit

Permalink
OX6-154: Fix missing refnr entries and gap in table
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Boulanger committed Nov 10, 2023
1 parent fe51cb4 commit b88f3ea
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/fcpohelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,4 +643,47 @@ protected static function _useRegistry()
return self::$_blUseRegistry;
}

/**
* Method that updates fcporefnr table if combination Prefix+RefNr isn't there yet
*
* @param string $sRefNr
* @param bool $blIncludesPrefix
* @return int
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
*/
public function fcpoUpdateRefNr($sRefNr, $blIncludesPrefix = true)
{
try {
$iRet = 0;

$sRawPrefix = (string) $this->getConfig()->getConfigParam('sFCPORefPrefix');
$sBasePrefix = $blIncludesPrefix ? substr($sRefNr, 0, strlen($sRawPrefix)) : '';
if ($blIncludesPrefix && $sRawPrefix !== $sBasePrefix) {
// abort, the $sRefNr was built with a different prefix. Not possible to extract incremental part
return -1;
}
$sRawIncrement = $blIncludesPrefix ? substr($sRefNr, strlen($sRawPrefix)) : $sRefNr;

$oDb = oxDb::getDb();
$sPrefix = $oDb->quote($sRawPrefix);
$sIncrement = $oDb->quote($sRawIncrement);

$sQuery = "SELECT fcpo_refnr FROM fcporefnr WHERE fcpo_refprefix = {$sPrefix} AND fcpo_refnr = {$sIncrement}";
$sExistingRefNr = $oDb->GetOne($sQuery);

if ($sExistingRefNr) {
return $iRet;
}

$sQuery = "INSERT INTO fcporefnr (fcpo_refnr, fcpo_txid, fcpo_refprefix) VALUES ({$sIncrement}, '', {$sPrefix})";
$oResult = $oDb->Execute($sQuery);
if ($oResult) {
$iRet = 1;
}

return $iRet;
} catch (Exception $oEx) {
return -1;
}
}
}
2 changes: 2 additions & 0 deletions lib/fcporequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3322,6 +3322,7 @@ public function getRefNr($oOrder = false, $blAddPrefixToSession = false)
if ($blUseSessionRefNr) {
$sRefNrComplete = ($blAddPrefixToSession) ?
$sRawPrefix . $sSessionRefNr : $sSessionRefNr;
$this->_oFcpoHelper->fcpoUpdateRefNr($sSessionRefNr, false);
return $sRefNrComplete;
}

Expand All @@ -3330,6 +3331,7 @@ public function getRefNr($oOrder = false, $blAddPrefixToSession = false)

if ($oOrder && !empty($oOrder->oxorder__oxordernr->value)) {
$sRefNr = $oOrder->oxorder__oxordernr->value;
$this->_oFcpoHelper->fcpoUpdateRefNr($sRefNr, false);
} else {
$sQuery = "SELECT MAX(fcpo_refnr) FROM fcporefnr WHERE fcpo_refprefix = {$sPrefix}";
$iMaxRefNr = $oDb->GetOne($sQuery);
Expand Down

0 comments on commit b88f3ea

Please sign in to comment.