diff --git a/lib/fcpohelper.php b/lib/fcpohelper.php index ee561a7b..1d40aacc 100644 --- a/lib/fcpohelper.php +++ b/lib/fcpohelper.php @@ -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; + } + } } diff --git a/lib/fcporequest.php b/lib/fcporequest.php index 1b5d8f1f..33583d60 100755 --- a/lib/fcporequest.php +++ b/lib/fcporequest.php @@ -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; } @@ -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);