project BLF > class BNumber > method ApiPrepareReleasedNumbers
Description
Prepare number records with status 'RELEASED' for existing sequences.
Parameters
iiCompanyId | input | integer | |
iiNumbrYear | input | integer | |
icNumbrType | input | character | |
oiReturnStatus | output | integer | Return status of the method. |
Internal usage
BLF
program code (program1/bnumber.p)
/* =========================================================================== */
/* The operation will be reverted and retried in case of an optimistic lock. */
/* =========================================================================== */
repeat on error undo, throw:
assign viNumber = 0.
<M-55 run DataLoad
(input '' (icRowids),
input string(iiCompanyId) + chr(2) + string(iiNumbrYear) + chr(2) + icNumbrType (icPkeys),
input '' (icObjectIds),
input '' (icFreeform),
input no (ilKeepPrevious),
output viFcReturnSuper (oiReturnStatus)) in BNumber>
/* When sequence does not exist, it will be created */
if viFcReturnSuper = -4
then assign viFcReturnSuper = 0.
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.
find first bNumbr where bNumbr.NumbrStatus = {&NUMBERSTATUS-FREE} no-error.
if available bNumbr
then do:
/* If sequence is de-activated, return an error */
if bNumbr.NumbrIsActive <> true
then do:
<M-46 run SetMessage
(input #T-68'This sequence ($1) has been deactivated.':100(505)T-68# (icMessage),
input string(iiNumbrYear) + '/':U + icNumbrType (icArguments),
input '' (icFieldName),
input '' (icFieldValue),
input 'D':U (icType),
input 3 (iiSeverity),
input '' (icRowid),
input 'blf-496573':U (icFcMsgNumber),
input '' (icFcExplanation),
input '' (icFcIdentification),
input '' (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BNumber>
assign oiReturnStatus = -3.
return.
end.
/* Save date and time in UTC */
session:timezone = 0.
assign viNumber = bNumbr.Numbr
bNumbr.LastModifiedDate = today
bNumbr.LastModifiedTime = time
bNumbr.LastModifiedUser = vcUserLogin
bNumbr.tc_Status = "C":U.
session:timezone = viTimeOffset.
end.
else do:
/* If nothing is available, create a new sequence */
for each tNumbr:
if tNumbr.Numbr >= viNumber
then assign viNumber = tNumbr.Numbr.
end.
viNumber = viNumber + 1.
<M-26 run AddDetailLine
(input 'Numbr':U (icTable),
input '' (icParentRowid),
output viFcReturnSuper (oiReturnStatus)) in BNumber>
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.
assign tNumbr.Company_ID = iiCompanyId
tNumbr.NumbrYear = iiNumbrYear
tNumbr.NumbrType = icNumbrType
tNumbr.NumbrStatus = {&NUMBERSTATUS-FREE}
tNumbr.NumbrIsActive = yes.
find first bNumbr where rowid(bNumbr) = rowid(tNumbr).
end.
/* create a list of 'RELEASED' numbers */
do viFcCount1 = 1 to 30:
<M-27 run AddDetailLine
(input 'Numbr':U (icTable),
input '' (icParentRowid),
output viFcReturnSuper (oiReturnStatus)) in BNumber>
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.
assign tNumbr.Company_ID = iiCompanyId
tNumbr.NumbrYear = iiNumbrYear
tNumbr.NumbrType = icNumbrType
tNumbr.NumbrStatus = {&NUMBERSTATUS-RELEASED}
tNumbr.NumbrIsActive = yes
tNumbr.Numbr = viNumber.
viNumber = viNumber + 1.
end.
assign bNumbr.Numbr = viNumber
viRealTransactionId = viTransactionId
viTransactionId = 0
vlFcDataValidated = yes.
<M-52 run DataSave (output viFcReturnSuper (oiReturnStatus)) in BNumber>
assign viTransactionId = viRealTransactionId.
/* If the transaction failed because of optimistic locking, repeat it. */
if viFcReturnSuper = -2
then do:
assign viRetries = viRetries + 1.
if viRetries > 9
then do:
assign oiReturnStatus = viFcReturnSuper.
return.
end.
/* debugging */
publish "Logging.BusinessCode"
("Optimistic lock in GetNumber, retrying (":U + string(viRetries) + ")":U, "").
next.
end.
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.
leave.
end.