Option Compare Database
Option Explicit

' ============================================================
' : mVesselMovement
'
' :
'         - .
'
'  :
'   -  -  - ;
'   -  - ;
'   -   ;
'   -  - ;
'   -   .
'
' :
'   t      - .
'        t.
'
' :
'       mBeerMovement:
'   - GetVesselIdByTypeAndNumber
'   - GetVesselNameById
'   - GetVesselTypeById
'   - GetVesselMaxVolume
'   - GetVesselCurrentVolume
'   - GetVesselFreeVolume
' ============================================================


' -----------------------------
'  
' -----------------------------
Private Const VESSEL_TYPE_CCT As String = ""
Private Const VESSEL_TYPE_FORFAS As String = ""


' -----------------------------
'  
' -----------------------------
Private Const VESSEL_STATUS_FREE As String = ""
Private Const VESSEL_STATUS_BUSY As String = ""


' -----------------------------
'  
' -----------------------------
Private Const STAGE_CCT_TO_FORFAS_FILTRATION As String = " -  - "
Private Const STAGE_CCT_TO_KEG_FILLING As String = " - "
Private Const STAGE_CCT_ADJUSTMENT As String = " "

Private Const STAGE_FORFAS_TO_KEG_FILLING As String = " - "
Private Const STAGE_FORFAS_ADJUSTMENT As String = " "


' -----------------------------
'  
' -----------------------------
Private Const OPERATION_FILTRATION As String = ""
Private Const OPERATION_FILLING As String = ""
Private Const OPERATION_ADJUSTMENT As String = ""


' -----------------------------
'  
' -----------------------------
Private Const DEFAULT_KEG_VOLUME_L As Double = 30


Private Function EscapeSqlValue(ByVal sourceText As String) As String

    '         SQL.

    EscapeSqlValue = Replace(sourceText, "'", "''")

End Function


Private Function GetVesselStateFieldValue( _
    ByVal vesselId As Long, _
    ByVal fieldName As String _
) As Variant

    '     t   .
    '
    ' :
    '   GetVesselStateFieldValue(1, "")
    '   GetVesselStateFieldValue(1, " ")

    On Error GoTo ErrorHandler

    GetVesselStateFieldValue = DLookup( _
        "[" & fieldName & "]", _
        "t", _
        "[ ] = " & vesselId _
    )

    Exit Function

ErrorHandler:

    GetVesselStateFieldValue = Null

End Function


Private Function GetVesselSort(ByVal vesselId As Long) As String

    '     .

    On Error GoTo ErrorHandler

    GetVesselSort = Nz(GetVesselStateFieldValue(vesselId, ""), "")

    Exit Function

ErrorHandler:

    GetVesselSort = ""

End Function


Private Function GetVesselBrewNumbers(ByVal vesselId As Long) As String

    '      .

    On Error GoTo ErrorHandler

    GetVesselBrewNumbers = Nz(GetVesselStateFieldValue(vesselId, " "), "")

    Exit Function

ErrorHandler:

    GetVesselBrewNumbers = ""

End Function


Private Function GetVesselFermentationNumber(ByVal vesselId As Long) As Long

    '      .

    On Error GoTo ErrorHandler

    GetVesselFermentationNumber = Nz(GetVesselStateFieldValue(vesselId, " "), 0)

    Exit Function

ErrorHandler:

    GetVesselFermentationNumber = 0

End Function


Private Function GetVesselBusyState(ByVal vesselId As Long) As Boolean

    '    .
    '
    '    0,   ,
    '    []  -   .

    On Error GoTo ErrorHandler

    Dim currentVolumeL As Double
    Dim busyFlag As Boolean

    currentVolumeL = GetVesselCurrentVolume(vesselId)
    busyFlag = Nz(GetVesselStateFieldValue(vesselId, ""), False)

    If currentVolumeL > 0 Then
        GetVesselBusyState = True
    Else
        GetVesselBusyState = busyFlag
    End If

    Exit Function

ErrorHandler:

    GetVesselBusyState = False

End Function


Private Function CalculateFiltrationLossL( _
    ByVal sourceVolumeL As Double, _
    ByVal targetVolumeL As Double _
) As Double

    '    .
    '
    '  =    -   .

    CalculateFiltrationLossL = sourceVolumeL - targetVolumeL

End Function


Private Function IsValidPositiveVolume(ByVal volumeL As Double) As Boolean

    ' ,   .

    IsValidPositiveVolume = (volumeL > 0)

End Function


Public Function RegisterCctToForfasWithFiltration( _
    ByVal sourceCctId As Long, _
    ByVal targetForfasId As Long, _
    ByVal sourceVolumeL As Double, _
    ByVal targetVolumeL As Double, _
    Optional ByVal commentText As String = "" _
) As Boolean

    '  :
    '    -  - 
    '
    ' sourceCctId:
    '     ,   .
    '
    ' targetForfasId:
    '     ,   .
    '
    ' sourceVolumeL:
    '   ,      .
    '
    ' targetVolumeL:
    '   ,      .
    '
    '  :
    '   sourceVolumeL - targetVolumeL
    '
    ' :
    '   t  .
    '        -    t.

    On Error GoTo ErrorHandler

    Dim db As DAO.Database
    Dim ws As DAO.Workspace
    Dim rsMovement As DAO.Recordset
    Dim rsSourceState As DAO.Recordset
    Dim rsTargetState As DAO.Recordset

    Dim transactionStarted As Boolean
    Dim stepName As String
    Dim errorDescription As String

    Dim sourceType As String
    Dim targetType As String

    Dim sourceName As String
    Dim targetName As String

    Dim sourceCurrentVolumeL As Double
    Dim targetCurrentVolumeL As Double

    Dim sourceNewVolumeL As Double
    Dim targetNewVolumeL As Double

    Dim targetMaxVolumeL As Double
    Dim filtrationLossL As Double

    Dim sourceSort As String
    Dim targetSort As String

    Dim sourceBrewNumbers As String
    Dim targetBrewNumbers As String
    Dim mergedBrewNumbers As String

    Dim sourceFermentationNumber As Long
    Dim targetFermentationNumber As Long
    Dim resultFermentationNumber As Long

    Dim sourceLastBrewDate As Variant
    Dim targetLastBrewDate As Variant
    Dim resultLastBrewDate As Variant

    RegisterCctToForfasWithFiltration = False
    transactionStarted = False

    Set ws = DBEngine.Workspaces(0)
    Set db = CurrentDb

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    If sourceCctId <= 0 Then
        Err.Raise vbObjectError + 3000, , "   -."
    End If

    If targetForfasId <= 0 Then
        Err.Raise vbObjectError + 3001, , "   -."
    End If

    If sourceCctId = targetForfasId Then
        Err.Raise vbObjectError + 3002, , "          ."
    End If

    If sourceVolumeL <= 0 Then
        Err.Raise vbObjectError + 3003, , "      0."
    End If

    If targetVolumeL <= 0 Then
        Err.Raise vbObjectError + 3004, , "      0."
    End If

    filtrationLossL = CalculateFiltrationLossL(sourceVolumeL, targetVolumeL)

    If filtrationLossL < 0 Then
        Err.Raise vbObjectError + 3005, , _
            "         ." & vbCrLf & _
            "  : " & sourceVolumeL & " " & vbCrLf & _
            "  : " & targetVolumeL & " "
    End If

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    sourceType = GetVesselTypeById(sourceCctId)
    targetType = GetVesselTypeById(targetForfasId)

    sourceName = GetVesselNameById(sourceCctId)
    targetName = GetVesselNameById(targetForfasId)

    If sourceType <> VESSEL_TYPE_CCT Then
        Err.Raise vbObjectError + 3006, , _
            "   ." & vbCrLf & _
            "  : " & sourceType
    End If

    If targetType <> VESSEL_TYPE_FORFAS Then
        Err.Raise vbObjectError + 3007, , _
            "   ." & vbCrLf & _
            "  : " & targetType
    End If

    If sourceName = "" Then
        sourceName = "   " & sourceCctId
    End If

    If targetName = "" Then
        targetName = "   " & targetForfasId
    End If

    ' ------------------------------------------------------------
    '    
    ' ------------------------------------------------------------
    stepName = "   "

    If EnsureVesselStateRecordForMovement(sourceCctId) = False Then
        Err.Raise vbObjectError + 3008, , "    ."
    End If

    If EnsureVesselStateRecordForMovement(targetForfasId) = False Then
        Err.Raise vbObjectError + 3009, , "    ."
    End If

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    Set rsSourceState = db.OpenRecordset( _
        "SELECT * FROM [t] " & _
        "WHERE [ ] = " & sourceCctId & ";", _
        dbOpenDynaset _
    )

    Set rsTargetState = db.OpenRecordset( _
        "SELECT * FROM [t] " & _
        "WHERE [ ] = " & targetForfasId & ";", _
        dbOpenDynaset _
    )

    If rsSourceState.EOF Then
        Err.Raise vbObjectError + 3010, , "    ."
    End If

    If rsTargetState.EOF Then
        Err.Raise vbObjectError + 3011, , "    ."
    End If

    sourceCurrentVolumeL = Nz(rsSourceState.Fields("()").value, 0)
    targetCurrentVolumeL = Nz(rsTargetState.Fields("()").value, 0)

    sourceSort = Nz(rsSourceState.Fields("").value, "")
    targetSort = Nz(rsTargetState.Fields("").value, "")

    sourceBrewNumbers = Nz(rsSourceState.Fields(" ").value, "")
    targetBrewNumbers = Nz(rsTargetState.Fields(" ").value, "")

    sourceFermentationNumber = Nz(rsSourceState.Fields(" ").value, 0)
    targetFermentationNumber = Nz(rsTargetState.Fields(" ").value, 0)

    sourceLastBrewDate = rsSourceState.Fields("  ").value
    targetLastBrewDate = rsTargetState.Fields("  ").value

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " -"

    If sourceCurrentVolumeL <= 0 Then
        Err.Raise vbObjectError + 3012, , sourceName & " .  ."
    End If

    If sourceVolumeL > sourceCurrentVolumeL Then
        Err.Raise vbObjectError + 3013, , _
            "    ,    ." & vbCrLf & _
            " " & sourceName & ": " & sourceCurrentVolumeL & " " & vbCrLf & _
            " : " & sourceVolumeL & " "
    End If

    If sourceSort = "" Then
        Err.Raise vbObjectError + 3014, , sourceName & " ,    ."
    End If

    If sourceFermentationNumber <= 0 Then
        Err.Raise vbObjectError + 3015, , sourceName & " ,     ."
    End If

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " -"

    targetMaxVolumeL = GetVesselMaxVolume(targetForfasId)

    If targetMaxVolumeL <= 0 Then
        Err.Raise vbObjectError + 3016, , " " & targetName & "    ."
    End If

    If targetCurrentVolumeL > 0 Then

        If StrComp(Trim(targetSort), Trim(sourceSort), vbTextCompare) <> 0 Then
            Err.Raise vbObjectError + 3017, , _
                targetName & "    """ & targetSort & """." & vbCrLf & _
                "   """ & sourceSort & """."
        End If

    End If

    targetNewVolumeL = targetCurrentVolumeL + targetVolumeL

    If targetNewVolumeL > targetMaxVolumeL Then
        Err.Raise vbObjectError + 3018, , _
            " :     " & targetName & "." & vbCrLf & _
            " : " & targetCurrentVolumeL & " " & vbCrLf & _
            "  : " & targetVolumeL & " " & vbCrLf & _
            " : " & targetNewVolumeL & " " & vbCrLf & _
            " : " & targetMaxVolumeL & " "
    End If

    sourceNewVolumeL = sourceCurrentVolumeL - sourceVolumeL

    ' ------------------------------------------------------------
    '      
    ' ------------------------------------------------------------
    stepName = "   "

    If targetCurrentVolumeL <= 0 Then

        '    ,     .
        resultFermentationNumber = sourceFermentationNumber
        mergedBrewNumbers = sourceBrewNumbers
        resultLastBrewDate = sourceLastBrewDate

    Else

        '       ,
        '    ,    .
        If targetFermentationNumber > 0 Then
            resultFermentationNumber = targetFermentationNumber
        Else
            resultFermentationNumber = sourceFermentationNumber
        End If

        mergedBrewNumbers = MergeDelimitedTextLists(targetBrewNumbers, sourceBrewNumbers)
        resultLastBrewDate = GetLaterDateValue(targetLastBrewDate, sourceLastBrewDate)

    End If

    ' ------------------------------------------------------------
    ' 
    ' ------------------------------------------------------------
    stepName = " "

    ws.BeginTrans
    transactionStarted = True

    ' ------------------------------------------------------------
    '    t
    ' ------------------------------------------------------------
    stepName = "  "

    Set rsMovement = db.OpenRecordset("t", dbOpenDynaset)

    rsMovement.AddNew

    rsMovement.Fields(" ").value = Now()
    rsMovement.Fields(" ").value = STAGE_CCT_TO_FORFAS_FILTRATION
    rsMovement.Fields(" ").value = OPERATION_FILTRATION

    rsMovement.Fields("  ").value = sourceCctId
    rsMovement.Fields("  ").value = targetForfasId

    rsMovement.Fields(" ").value = sourceFermentationNumber
    rsMovement.Fields("").value = sourceSort
    rsMovement.Fields(" ").value = sourceBrewNumbers

    rsMovement.Fields("  ()").value = sourceVolumeL
    rsMovement.Fields("  ()").value = targetVolumeL
    rsMovement.Fields("()").value = filtrationLossL

    rsMovement.Fields(" ").value = Null
    rsMovement.Fields(" ()").value = Null

    rsMovement.Fields("").value = commentText
    rsMovement.Fields("").value = False

    rsMovement.Update

    ' ------------------------------------------------------------
    '  -
    ' ------------------------------------------------------------
    stepName = " -"

    rsSourceState.Edit

    If sourceNewVolumeL <= 0 Then

        '      ,  .
        rsSourceState.Fields("").value = Null
        rsSourceState.Fields("").value = Null
        rsSourceState.Fields("()").value = 0
        rsSourceState.Fields("").value = False
        rsSourceState.Fields("").value = VESSEL_STATUS_FREE
        rsSourceState.Fields(" ").value = Null
        rsSourceState.Fields(" ").value = Null
        rsSourceState.Fields("  ").value = Null
        rsSourceState.Fields(" ").value = False
        rsSourceState.Fields("  ").value = Null

    Else

        '    ,   .
        rsSourceState.Fields("()").value = sourceNewVolumeL
        rsSourceState.Fields("").value = True
        rsSourceState.Fields("").value = VESSEL_STATUS_BUSY

    End If

    rsSourceState.Update

    ' ------------------------------------------------------------
    '  -
    ' ------------------------------------------------------------
    stepName = " -"

    rsTargetState.Edit

    '    []     
    '     .
    '
    ' :
    '   -      ,   ;
    '   -          ,
    '         .
    '
    '   "  "   
    '         .
    If targetCurrentVolumeL <= 0 Or IsNull(rsTargetState.Fields("").value) Then
        rsTargetState.Fields("").value = Date
    End If

    rsTargetState.Fields("").value = sourceSort
    rsTargetState.Fields("()").value = targetNewVolumeL
    rsTargetState.Fields("").value = True
    rsTargetState.Fields("").value = VESSEL_STATUS_BUSY
    rsTargetState.Fields(" ").value = resultFermentationNumber
    rsTargetState.Fields(" ").value = mergedBrewNumbers
    rsTargetState.Fields("  ").value = resultLastBrewDate

    '         .
    '   ,       .
    rsTargetState.Fields(" ").value = False
    rsTargetState.Fields("  ").value = Null

    rsTargetState.Update

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " "

    If transactionStarted = True Then
        ws.CommitTrans
        transactionStarted = False
    End If

    RegisterCctToForfasWithFiltration = True

ExitPoint:

    On Error Resume Next

    If Not rsMovement Is Nothing Then
        rsMovement.Close
    End If

    If Not rsSourceState Is Nothing Then
        rsSourceState.Close
    End If

    If Not rsTargetState Is Nothing Then
        rsTargetState.Close
    End If

    Set rsMovement = Nothing
    Set rsSourceState = Nothing
    Set rsTargetState = Nothing
    Set db = Nothing
    Set ws = Nothing

    Exit Function

ErrorHandler:

    errorDescription = Err.Description

    On Error Resume Next

    If transactionStarted = True Then
        ws.Rollback
        transactionStarted = False
    End If

    RegisterCctToForfasWithFiltration = False

    MsgBox "     -  - ." & vbCrLf & vbCrLf & _
           ": " & stepName & vbCrLf & _
           ": " & errorDescription, _
           vbExclamation, _
           " "

    Resume ExitPoint

End Function




Public Function RegisterCctToKegFilling( _
    ByVal sourceCctId As Long, _
    ByVal packageId As Long, _
    ByVal packageCount As Long, _
    ByVal packageUnitVolumeL As Double, _
    ByVal packageType As String, _
    ByVal packageName As String, _
    Optional ByVal commentText As String = "" _
) As Boolean

    '  :
    '    - 
    '
    '      :
    '    30 ,  50 ,  1,5   ..
    '
    '      
    '   [ ]  [ ()].
    '      :
    '   [ ]
    '   [  ()]

    On Error GoTo ErrorHandler

    Dim db As DAO.Database
    Dim ws As DAO.Workspace
    Dim rsMovement As DAO.Recordset
    Dim rsSourceState As DAO.Recordset

    Dim transactionStarted As Boolean
    Dim stepName As String
    Dim errorDescription As String

    Dim sourceType As String
    Dim sourceName As String

    Dim sourceCurrentVolumeL As Double
    Dim sourceNewVolumeL As Double
    Dim fillingVolumeL As Double

    Dim sourceSort As String
    Dim sourceBrewNumbers As String
    Dim sourceFermentationNumber As Long

    RegisterCctToKegFilling = False
    transactionStarted = False

    Set ws = DBEngine.Workspaces(0)
    Set db = CurrentDb

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    If sourceCctId <= 0 Then
        Err.Raise vbObjectError + 3200, , "   -."
    End If

    If packageId <= 0 Then
        Err.Raise vbObjectError + 3201, , "  ."
    End If

    If Trim(packageType) = "" Then
        Err.Raise vbObjectError + 3202, , "   ."
    End If

    If Trim(packageName) = "" Then
        Err.Raise vbObjectError + 3203, , "   ."
    End If

    If packageCount <= 0 Then
        Err.Raise vbObjectError + 3204, , "     0."
    End If

    If packageUnitVolumeL <= 0 Then
        Err.Raise vbObjectError + 3205, , "      0."
    End If

    fillingVolumeL = packageCount * packageUnitVolumeL

    If fillingVolumeL <= 0 Then
        Err.Raise vbObjectError + 3206, , "     0."
    End If

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    sourceType = GetVesselTypeById(sourceCctId)
    sourceName = GetVesselNameById(sourceCctId)

    If sourceType <> VESSEL_TYPE_CCT Then
        Err.Raise vbObjectError + 3207, , _
            "   ." & vbCrLf & _
            "  : " & sourceType
    End If

    If sourceName = "" Then
        sourceName = "   " & sourceCctId
    End If

    ' ------------------------------------------------------------
    '    
    ' ------------------------------------------------------------
    stepName = "   "

    If EnsureVesselStateRecordForMovement(sourceCctId) = False Then
        Err.Raise vbObjectError + 3208, , "    ."
    End If

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    Set rsSourceState = db.OpenRecordset( _
        "SELECT * FROM [t] " & _
        "WHERE [ ] = " & sourceCctId & ";", _
        dbOpenDynaset _
    )

    If rsSourceState.EOF Then
        Err.Raise vbObjectError + 3209, , "    ."
    End If

    sourceCurrentVolumeL = Nz(rsSourceState.Fields("()").value, 0)
    sourceSort = Nz(rsSourceState.Fields("").value, "")
    sourceBrewNumbers = Nz(rsSourceState.Fields(" ").value, "")
    sourceFermentationNumber = Nz(rsSourceState.Fields(" ").value, 0)

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " -"

    If sourceCurrentVolumeL <= 0 Then
        Err.Raise vbObjectError + 3210, , sourceName & " .  ."
    End If

    If fillingVolumeL > sourceCurrentVolumeL Then
        Err.Raise vbObjectError + 3211, , _
            "  ,    ." & vbCrLf & _
            " " & sourceName & ": " & sourceCurrentVolumeL & " " & vbCrLf & _
            ": " & packageName & vbCrLf & _
            " : " & packageCount & vbCrLf & _
            "  : " & packageUnitVolumeL & " " & vbCrLf & _
            " : " & fillingVolumeL & " "
    End If

    If sourceSort = "" Then
        Err.Raise vbObjectError + 3212, , sourceName & " ,    ."
    End If

    If sourceFermentationNumber <= 0 Then
        Err.Raise vbObjectError + 3213, , sourceName & " ,     ."
    End If

    sourceNewVolumeL = sourceCurrentVolumeL - fillingVolumeL

    ' ------------------------------------------------------------
    ' 
    ' ------------------------------------------------------------
    stepName = " "

    ws.BeginTrans
    transactionStarted = True

    ' ------------------------------------------------------------
    '    t
    ' ------------------------------------------------------------
    stepName = "  "

    Set rsMovement = db.OpenRecordset("t", dbOpenDynaset)

    rsMovement.AddNew

    rsMovement.Fields(" ").value = Now()
    rsMovement.Fields(" ").value = STAGE_CCT_TO_KEG_FILLING
    rsMovement.Fields(" ").value = OPERATION_FILLING

    rsMovement.Fields("  ").value = sourceCctId
    rsMovement.Fields("  ").value = Null

    rsMovement.Fields(" ").value = sourceFermentationNumber
    rsMovement.Fields("").value = sourceSort
    rsMovement.Fields(" ").value = sourceBrewNumbers

    rsMovement.Fields("  ()").value = fillingVolumeL
    rsMovement.Fields("  ()").value = fillingVolumeL
    rsMovement.Fields("()").value = 0

    '     .
    rsMovement.Fields(" ").value = packageCount
    rsMovement.Fields(" ()").value = packageUnitVolumeL

    '    .
    rsMovement.Fields(" ").value = packageId
    rsMovement.Fields(" ").value = packageType
    rsMovement.Fields(" ").value = packageName
    rsMovement.Fields(" ").value = packageCount
    rsMovement.Fields("  ()").value = packageUnitVolumeL

    rsMovement.Fields("").value = commentText
    rsMovement.Fields("").value = False

    rsMovement.Update

    ' ------------------------------------------------------------
    '  -
    ' ------------------------------------------------------------
    stepName = " -"

    rsSourceState.Edit

    If sourceNewVolumeL <= 0 Then

        '      ,  .
        rsSourceState.Fields("").value = Null
        rsSourceState.Fields("").value = Null
        rsSourceState.Fields("()").value = 0
        rsSourceState.Fields("").value = False
        rsSourceState.Fields("").value = VESSEL_STATUS_FREE
        rsSourceState.Fields(" ").value = Null
        rsSourceState.Fields(" ").value = Null
        rsSourceState.Fields("  ").value = Null
        rsSourceState.Fields(" ").value = False
        rsSourceState.Fields("  ").value = Null

    Else

        '    ,   .
        rsSourceState.Fields("()").value = sourceNewVolumeL
        rsSourceState.Fields("").value = True
        rsSourceState.Fields("").value = VESSEL_STATUS_BUSY

    End If

    rsSourceState.Update

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " "

    If transactionStarted = True Then
        ws.CommitTrans
        transactionStarted = False
    End If

    RegisterCctToKegFilling = True

ExitPoint:

    On Error Resume Next

    If Not rsMovement Is Nothing Then
        rsMovement.Close
    End If

    If Not rsSourceState Is Nothing Then
        rsSourceState.Close
    End If

    Set rsMovement = Nothing
    Set rsSourceState = Nothing
    Set db = Nothing
    Set ws = Nothing

    Exit Function

ErrorHandler:

    errorDescription = Err.Description

    On Error Resume Next

    If transactionStarted = True Then
        ws.Rollback
        transactionStarted = False
    End If

    RegisterCctToKegFilling = False

    MsgBox "     - ." & vbCrLf & vbCrLf & _
           ": " & stepName & vbCrLf & _
           ": " & errorDescription, _
           vbExclamation, _
           " "

    Resume ExitPoint

End Function



Public Function RegisterCctAdjustment( _
    ByVal sourceCctId As Long, _
    ByVal adjustmentVolumeL As Double, _
    ByVal commentText As String _
) As Boolean

    '  :
    '    
    '
    ' adjustmentVolumeL:
    '   > 0     ;
    '   < 0     ;
    '   = 0   .
    '
    '     .
    '
    ' :
    '      ,  ,
    '        .
    '
    '         0,
    '    .
    '
    '        ,
    '      ,     .

    On Error GoTo ErrorHandler

    Dim db As DAO.Database
    Dim ws As DAO.Workspace
    Dim rsMovement As DAO.Recordset
    Dim rsSourceState As DAO.Recordset

    Dim transactionStarted As Boolean
    Dim stepName As String
    Dim errorDescription As String

    Dim sourceType As String
    Dim sourceName As String

    Dim sourceCurrentVolumeL As Double
    Dim sourceNewVolumeL As Double
    Dim sourceMaxVolumeL As Double

    Dim sourceSort As String
    Dim sourceBrewNumbers As String
    Dim sourceFermentationNumber As Long

    RegisterCctAdjustment = False
    transactionStarted = False

    Set ws = DBEngine.Workspaces(0)
    Set db = CurrentDb

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    If sourceCctId <= 0 Then
        Err.Raise vbObjectError + 3400, , "   ."
    End If

    If adjustmentVolumeL = 0 Then
        Err.Raise vbObjectError + 3401, , "      0."
    End If

    If Trim(Nz(commentText, "")) = "" Then
        Err.Raise vbObjectError + 3402, , "     ."
    End If

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    sourceType = GetVesselTypeById(sourceCctId)
    sourceName = GetVesselNameById(sourceCctId)

    If sourceType <> VESSEL_TYPE_CCT Then
        Err.Raise vbObjectError + 3403, , _
            "   ." & vbCrLf & _
            "  : " & sourceType
    End If

    If sourceName = "" Then
        sourceName = "   " & sourceCctId
    End If

    ' ------------------------------------------------------------
    '    
    ' ------------------------------------------------------------
    stepName = "   "

    If EnsureVesselStateRecordForMovement(sourceCctId) = False Then
        Err.Raise vbObjectError + 3404, , "    ."
    End If

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    Set rsSourceState = db.OpenRecordset( _
        "SELECT * FROM [t] " & _
        "WHERE [ ] = " & sourceCctId & ";", _
        dbOpenDynaset _
    )

    If rsSourceState.EOF Then
        Err.Raise vbObjectError + 3405, , "    ."
    End If

    sourceCurrentVolumeL = Nz(rsSourceState.Fields("()").value, 0)
    sourceSort = Nz(rsSourceState.Fields("").value, "")
    sourceBrewNumbers = Nz(rsSourceState.Fields(" ").value, "")
    sourceFermentationNumber = Nz(rsSourceState.Fields(" ").value, 0)
    sourceMaxVolumeL = GetVesselMaxVolume(sourceCctId)

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " "

    If sourceCurrentVolumeL <= 0 Then
        Err.Raise vbObjectError + 3406, , sourceName & " .  ."
    End If

    If sourceSort = "" Then
        Err.Raise vbObjectError + 3407, , sourceName & " ,    ."
    End If

    If sourceFermentationNumber <= 0 Then
        Err.Raise vbObjectError + 3408, , sourceName & " ,     ."
    End If

    If sourceMaxVolumeL <= 0 Then
        Err.Raise vbObjectError + 3409, , " " & sourceName & "    ."
    End If

    sourceNewVolumeL = sourceCurrentVolumeL + adjustmentVolumeL

    If sourceNewVolumeL < 0 Then
        Err.Raise vbObjectError + 3410, , _
            " :    ." & vbCrLf & _
            " : " & sourceCurrentVolumeL & " " & vbCrLf & _
            ": " & adjustmentVolumeL & " " & vbCrLf & _
            " : " & sourceNewVolumeL & " "
    End If

    If sourceNewVolumeL > sourceMaxVolumeL Then
        Err.Raise vbObjectError + 3411, , _
            " :     " & sourceName & "." & vbCrLf & _
            " : " & sourceCurrentVolumeL & " " & vbCrLf & _
            ": " & adjustmentVolumeL & " " & vbCrLf & _
            " : " & sourceNewVolumeL & " " & vbCrLf & _
            " : " & sourceMaxVolumeL & " "
    End If

    ' ------------------------------------------------------------
    ' 
    ' ------------------------------------------------------------
    stepName = " "

    ws.BeginTrans
    transactionStarted = True

    ' ------------------------------------------------------------
    '    t
    ' ------------------------------------------------------------
    stepName = "  "

    Set rsMovement = db.OpenRecordset("t", dbOpenDynaset)

    rsMovement.AddNew

    rsMovement.Fields(" ").value = Now()
    rsMovement.Fields(" ").value = STAGE_CCT_ADJUSTMENT
    rsMovement.Fields(" ").value = OPERATION_ADJUSTMENT

    rsMovement.Fields("  ").value = sourceCctId
    rsMovement.Fields("  ").value = Null

    rsMovement.Fields(" ").value = sourceFermentationNumber
    rsMovement.Fields("").value = sourceSort
    rsMovement.Fields(" ").value = sourceBrewNumbers

    '       .
    rsMovement.Fields("  ()").value = adjustmentVolumeL
    rsMovement.Fields("  ()").value = Null
    rsMovement.Fields("()").value = 0

    rsMovement.Fields(" ").value = Null
    rsMovement.Fields(" ()").value = Null

    rsMovement.Fields("").value = commentText
    rsMovement.Fields("").value = False

    rsMovement.Update

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " "

    rsSourceState.Edit

    If sourceNewVolumeL <= 0 Then

        '      0,
        '  .
        rsSourceState.Fields("").value = Null
        rsSourceState.Fields("").value = Null
        rsSourceState.Fields("()").value = 0
        rsSourceState.Fields("").value = False
        rsSourceState.Fields("").value = VESSEL_STATUS_FREE
        rsSourceState.Fields(" ").value = Null
        rsSourceState.Fields(" ").value = Null
        rsSourceState.Fields("  ").value = Null
        rsSourceState.Fields(" ").value = False
        rsSourceState.Fields("  ").value = Null

    Else

        '    ,
        '      .
        rsSourceState.Fields("()").value = sourceNewVolumeL
        rsSourceState.Fields("").value = True
        rsSourceState.Fields("").value = VESSEL_STATUS_BUSY

    End If

    rsSourceState.Update

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " "

    If transactionStarted = True Then
        ws.CommitTrans
        transactionStarted = False
    End If

    RegisterCctAdjustment = True

ExitPoint:

    On Error Resume Next

    If Not rsMovement Is Nothing Then
        rsMovement.Close
    End If

    If Not rsSourceState Is Nothing Then
        rsSourceState.Close
    End If

    Set rsMovement = Nothing
    Set rsSourceState = Nothing
    Set db = Nothing
    Set ws = Nothing

    Exit Function

ErrorHandler:

    errorDescription = Err.Description

    On Error Resume Next

    If transactionStarted = True Then
        ws.Rollback
        transactionStarted = False
    End If

    RegisterCctAdjustment = False

    MsgBox "     ." & vbCrLf & vbCrLf & _
           ": " & stepName & vbCrLf & _
           ": " & errorDescription, _
           vbExclamation, _
           " "

    Resume ExitPoint

End Function



Public Function RegisterForfasToKegFilling( _
    ByVal sourceForfasId As Long, _
    ByVal packageId As Long, _
    ByVal packageCount As Long, _
    ByVal packageUnitVolumeL As Double, _
    ByVal packageType As String, _
    ByVal packageName As String, _
    Optional ByVal commentText As String = "" _
) As Boolean

    '  :
    '    - 
    '
    '      :
    '    30 ,  50 ,  1,5   ..
    '
    '      
    '   [ ]  [ ()].
    '      :
    '   [ ]
    '   [  ()]

    On Error GoTo ErrorHandler

    Dim db As DAO.Database
    Dim ws As DAO.Workspace
    Dim rsMovement As DAO.Recordset
    Dim rsSourceState As DAO.Recordset

    Dim transactionStarted As Boolean
    Dim stepName As String
    Dim errorDescription As String

    Dim sourceType As String
    Dim sourceName As String

    Dim sourceCurrentVolumeL As Double
    Dim sourceNewVolumeL As Double
    Dim fillingVolumeL As Double

    Dim sourceSort As String
    Dim sourceBrewNumbers As String
    Dim sourceFermentationNumber As Long

    RegisterForfasToKegFilling = False
    transactionStarted = False

    Set ws = DBEngine.Workspaces(0)
    Set db = CurrentDb

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    If sourceForfasId <= 0 Then
        Err.Raise vbObjectError + 3200, , "   -."
    End If

    If packageId <= 0 Then
        Err.Raise vbObjectError + 3201, , "  ."
    End If

    If Trim(packageType) = "" Then
        Err.Raise vbObjectError + 3202, , "   ."
    End If

    If Trim(packageName) = "" Then
        Err.Raise vbObjectError + 3203, , "   ."
    End If

    If packageCount <= 0 Then
        Err.Raise vbObjectError + 3204, , "     0."
    End If

    If packageUnitVolumeL <= 0 Then
        Err.Raise vbObjectError + 3205, , "      0."
    End If

    fillingVolumeL = packageCount * packageUnitVolumeL

    If fillingVolumeL <= 0 Then
        Err.Raise vbObjectError + 3206, , "     0."
    End If

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    sourceType = GetVesselTypeById(sourceForfasId)
    sourceName = GetVesselNameById(sourceForfasId)

    If sourceType <> VESSEL_TYPE_FORFAS Then
        Err.Raise vbObjectError + 3207, , _
            "   ." & vbCrLf & _
            "  : " & sourceType
    End If

    If sourceName = "" Then
        sourceName = "   " & sourceForfasId
    End If

    ' ------------------------------------------------------------
    '    
    ' ------------------------------------------------------------
    stepName = "   "

    If EnsureVesselStateRecordForMovement(sourceForfasId) = False Then
        Err.Raise vbObjectError + 3208, , "    ."
    End If

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    Set rsSourceState = db.OpenRecordset( _
        "SELECT * FROM [t] " & _
        "WHERE [ ] = " & sourceForfasId & ";", _
        dbOpenDynaset _
    )

    If rsSourceState.EOF Then
        Err.Raise vbObjectError + 3209, , "    ."
    End If

    sourceCurrentVolumeL = Nz(rsSourceState.Fields("()").value, 0)
    sourceSort = Nz(rsSourceState.Fields("").value, "")
    sourceBrewNumbers = Nz(rsSourceState.Fields(" ").value, "")
    sourceFermentationNumber = Nz(rsSourceState.Fields(" ").value, 0)

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " -"

    If sourceCurrentVolumeL <= 0 Then
        Err.Raise vbObjectError + 3210, , sourceName & " .  ."
    End If

    If fillingVolumeL > sourceCurrentVolumeL Then
        Err.Raise vbObjectError + 3211, , _
            "  ,    ." & vbCrLf & _
            " " & sourceName & ": " & sourceCurrentVolumeL & " " & vbCrLf & _
            ": " & packageName & vbCrLf & _
            " : " & packageCount & vbCrLf & _
            "  : " & packageUnitVolumeL & " " & vbCrLf & _
            " : " & fillingVolumeL & " "
    End If

    If sourceSort = "" Then
        Err.Raise vbObjectError + 3212, , sourceName & " ,    ."
    End If

    If sourceFermentationNumber <= 0 Then
        Err.Raise vbObjectError + 3213, , sourceName & " ,     ."
    End If

    sourceNewVolumeL = sourceCurrentVolumeL - fillingVolumeL

    ' ------------------------------------------------------------
    ' 
    ' ------------------------------------------------------------
    stepName = " "

    ws.BeginTrans
    transactionStarted = True

    ' ------------------------------------------------------------
    '    t
    ' ------------------------------------------------------------
    stepName = "  "

    Set rsMovement = db.OpenRecordset("t", dbOpenDynaset)

    rsMovement.AddNew

    rsMovement.Fields(" ").value = Now()
    rsMovement.Fields(" ").value = STAGE_FORFAS_TO_KEG_FILLING
    rsMovement.Fields(" ").value = OPERATION_FILLING

    rsMovement.Fields("  ").value = sourceForfasId
    rsMovement.Fields("  ").value = Null

    rsMovement.Fields(" ").value = sourceFermentationNumber
    rsMovement.Fields("").value = sourceSort
    rsMovement.Fields(" ").value = sourceBrewNumbers

    rsMovement.Fields("  ()").value = fillingVolumeL
    rsMovement.Fields("  ()").value = fillingVolumeL
    rsMovement.Fields("()").value = 0

    '     .
    rsMovement.Fields(" ").value = packageCount
    rsMovement.Fields(" ()").value = packageUnitVolumeL

    '    .
    rsMovement.Fields(" ").value = packageId
    rsMovement.Fields(" ").value = packageType
    rsMovement.Fields(" ").value = packageName
    rsMovement.Fields(" ").value = packageCount
    rsMovement.Fields("  ()").value = packageUnitVolumeL

    rsMovement.Fields("").value = commentText
    rsMovement.Fields("").value = False

    rsMovement.Update

    ' ------------------------------------------------------------
    '  -
    ' ------------------------------------------------------------
    stepName = " -"

    rsSourceState.Edit

    If sourceNewVolumeL <= 0 Then

        '      ,  .
        rsSourceState.Fields("").value = Null
        rsSourceState.Fields("").value = Null
        rsSourceState.Fields("()").value = 0
        rsSourceState.Fields("").value = False
        rsSourceState.Fields("").value = VESSEL_STATUS_FREE
        rsSourceState.Fields(" ").value = Null
        rsSourceState.Fields(" ").value = Null
        rsSourceState.Fields("  ").value = Null
        rsSourceState.Fields(" ").value = False
        rsSourceState.Fields("  ").value = Null

    Else

        '    ,   .
        rsSourceState.Fields("()").value = sourceNewVolumeL
        rsSourceState.Fields("").value = True
        rsSourceState.Fields("").value = VESSEL_STATUS_BUSY

    End If

    rsSourceState.Update

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " "

    If transactionStarted = True Then
        ws.CommitTrans
        transactionStarted = False
    End If

    RegisterForfasToKegFilling = True

ExitPoint:

    On Error Resume Next

    If Not rsMovement Is Nothing Then
        rsMovement.Close
    End If

    If Not rsSourceState Is Nothing Then
        rsSourceState.Close
    End If

    Set rsMovement = Nothing
    Set rsSourceState = Nothing
    Set db = Nothing
    Set ws = Nothing

    Exit Function

ErrorHandler:

    errorDescription = Err.Description

    On Error Resume Next

    If transactionStarted = True Then
        ws.Rollback
        transactionStarted = False
    End If

    RegisterForfasToKegFilling = False

    MsgBox "     - ." & vbCrLf & vbCrLf & _
           ": " & stepName & vbCrLf & _
           ": " & errorDescription, _
           vbExclamation, _
           " "

    Resume ExitPoint

End Function


Public Function RegisterForfasAdjustment( _
    ByVal sourceForfasId As Long, _
    ByVal adjustmentVolumeL As Double, _
    ByVal commentText As String _
) As Boolean

    '  :
    '    
    '
    ' adjustmentVolumeL:
    '   > 0     ;
    '   < 0     ;
    '   = 0   .
    '
    '     .
    '
    '    ,  ,
    '  ,       .

    On Error GoTo ErrorHandler

    Dim db As DAO.Database
    Dim ws As DAO.Workspace
    Dim rsMovement As DAO.Recordset
    Dim rsSourceState As DAO.Recordset

    Dim transactionStarted As Boolean
    Dim stepName As String
    Dim errorDescription As String

    Dim sourceType As String
    Dim sourceName As String

    Dim sourceCurrentVolumeL As Double
    Dim sourceNewVolumeL As Double
    Dim sourceMaxVolumeL As Double

    Dim sourceSort As String
    Dim sourceBrewNumbers As String
    Dim sourceFermentationNumber As Long

    RegisterForfasAdjustment = False
    transactionStarted = False

    Set ws = DBEngine.Workspaces(0)
    Set db = CurrentDb

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    If sourceForfasId <= 0 Then
        Err.Raise vbObjectError + 3800, , "   ."
    End If

    If adjustmentVolumeL = 0 Then
        Err.Raise vbObjectError + 3801, , "      0."
    End If

    If Trim(Nz(commentText, "")) = "" Then
        Err.Raise vbObjectError + 3802, , "     ."
    End If

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    sourceType = GetVesselTypeById(sourceForfasId)
    sourceName = GetVesselNameById(sourceForfasId)

    If sourceType <> VESSEL_TYPE_FORFAS Then
        Err.Raise vbObjectError + 3803, , _
            "   ." & vbCrLf & _
            "  : " & sourceType
    End If

    If sourceName = "" Then
        sourceName = "   " & sourceForfasId
    End If

    ' ------------------------------------------------------------
    '    
    ' ------------------------------------------------------------
    stepName = "   "

    If EnsureVesselStateRecordForMovement(sourceForfasId) = False Then
        Err.Raise vbObjectError + 3804, , "    ."
    End If

    ' ------------------------------------------------------------
    '   
    ' ------------------------------------------------------------
    stepName = "  "

    Set rsSourceState = db.OpenRecordset( _
        "SELECT * FROM [t] " & _
        "WHERE [ ] = " & sourceForfasId & ";", _
        dbOpenDynaset _
    )

    If rsSourceState.EOF Then
        Err.Raise vbObjectError + 3805, , "    ."
    End If

    sourceCurrentVolumeL = Nz(rsSourceState.Fields("()").value, 0)
    sourceSort = Nz(rsSourceState.Fields("").value, "")
    sourceBrewNumbers = Nz(rsSourceState.Fields(" ").value, "")
    sourceFermentationNumber = Nz(rsSourceState.Fields(" ").value, 0)
    sourceMaxVolumeL = GetVesselMaxVolume(sourceForfasId)

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " "

    If sourceCurrentVolumeL <= 0 Then
        Err.Raise vbObjectError + 3806, , sourceName & " .  ."
    End If

    If sourceSort = "" Then
        Err.Raise vbObjectError + 3807, , sourceName & " ,    ."
    End If

    If sourceFermentationNumber <= 0 Then
        Err.Raise vbObjectError + 3808, , sourceName & " ,     ."
    End If

    If sourceMaxVolumeL <= 0 Then
        Err.Raise vbObjectError + 3809, , " " & sourceName & "    ."
    End If

    sourceNewVolumeL = sourceCurrentVolumeL + adjustmentVolumeL

    If sourceNewVolumeL < 0 Then
        Err.Raise vbObjectError + 3810, , _
            " :    ." & vbCrLf & _
            " : " & sourceCurrentVolumeL & " " & vbCrLf & _
            ": " & adjustmentVolumeL & " " & vbCrLf & _
            " : " & sourceNewVolumeL & " "
    End If

    If sourceNewVolumeL > sourceMaxVolumeL Then
        Err.Raise vbObjectError + 3811, , _
            " :     " & sourceName & "." & vbCrLf & _
            " : " & sourceCurrentVolumeL & " " & vbCrLf & _
            ": " & adjustmentVolumeL & " " & vbCrLf & _
            " : " & sourceNewVolumeL & " " & vbCrLf & _
            " : " & sourceMaxVolumeL & " "
    End If

    ' ------------------------------------------------------------
    ' 
    ' ------------------------------------------------------------
    stepName = " "

    ws.BeginTrans
    transactionStarted = True

    ' ------------------------------------------------------------
    '    t
    ' ------------------------------------------------------------
    stepName = "  "

    Set rsMovement = db.OpenRecordset("t", dbOpenDynaset)

    rsMovement.AddNew

    rsMovement.Fields(" ").value = Now()
    rsMovement.Fields(" ").value = STAGE_FORFAS_ADJUSTMENT
    rsMovement.Fields(" ").value = OPERATION_ADJUSTMENT

    rsMovement.Fields("  ").value = sourceForfasId
    rsMovement.Fields("  ").value = Null

    rsMovement.Fields(" ").value = sourceFermentationNumber
    rsMovement.Fields("").value = sourceSort
    rsMovement.Fields(" ").value = sourceBrewNumbers

    '       .
    rsMovement.Fields("  ()").value = adjustmentVolumeL
    rsMovement.Fields("  ()").value = Null
    rsMovement.Fields("()").value = 0

    rsMovement.Fields(" ").value = Null
    rsMovement.Fields(" ()").value = Null

    rsMovement.Fields("").value = commentText
    rsMovement.Fields("").value = False

    rsMovement.Update

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " "

    rsSourceState.Edit

    If sourceNewVolumeL <= 0 Then

        '      0,
        '  .
        rsSourceState.Fields("").value = Null
        rsSourceState.Fields("").value = Null
        rsSourceState.Fields("()").value = 0
        rsSourceState.Fields("").value = False
        rsSourceState.Fields("").value = VESSEL_STATUS_FREE
        rsSourceState.Fields(" ").value = Null
        rsSourceState.Fields(" ").value = Null
        rsSourceState.Fields("  ").value = Null
        rsSourceState.Fields(" ").value = False
        rsSourceState.Fields("  ").value = Null

    Else

        '    ,
        '      .
        rsSourceState.Fields("()").value = sourceNewVolumeL
        rsSourceState.Fields("").value = True
        rsSourceState.Fields("").value = VESSEL_STATUS_BUSY

    End If

    rsSourceState.Update

    ' ------------------------------------------------------------
    '  
    ' ------------------------------------------------------------
    stepName = " "

    If transactionStarted = True Then
        ws.CommitTrans
        transactionStarted = False
    End If

    RegisterForfasAdjustment = True

ExitPoint:

    On Error Resume Next

    If Not rsMovement Is Nothing Then
        rsMovement.Close
    End If

    If Not rsSourceState Is Nothing Then
        rsSourceState.Close
    End If

    Set rsMovement = Nothing
    Set rsSourceState = Nothing
    Set db = Nothing
    Set ws = Nothing

    Exit Function

ErrorHandler:

    errorDescription = Err.Description

    On Error Resume Next

    If transactionStarted = True Then
        ws.Rollback
        transactionStarted = False
    End If

    RegisterForfasAdjustment = False

    MsgBox "     ." & vbCrLf & vbCrLf & _
           ": " & stepName & vbCrLf & _
           ": " & errorDescription, _
           vbExclamation, _
           " "

    Resume ExitPoint

End Function

Private Function EnsureVesselStateRecordForMovement(ByVal vesselId As Long) As Boolean

    '      .
    '
    '   ,   :
    '    = 0;
    '    = ;
    '    = .

    On Error GoTo ErrorHandler

    Dim db As DAO.Database
    Dim stateCount As Long

    Set db = CurrentDb

    stateCount = DCount("*", "t", "[ ] = " & vesselId)

    If stateCount = 0 Then

        db.Execute _
            "INSERT INTO [t] " & _
            "([ ], [()], [], [], [ ]) " & _
            "VALUES " & _
            "(" & vesselId & ", 0, False, '" & VESSEL_STATUS_FREE & "', False);", _
            dbFailOnError

    End If

    EnsureVesselStateRecordForMovement = True

ExitPoint:

    Set db = Nothing
    Exit Function

ErrorHandler:

    EnsureVesselStateRecordForMovement = False
    Resume ExitPoint

End Function


Private Function MergeDelimitedTextLists( _
    ByVal firstText As String, _
    ByVal secondText As String _
) As String

    '   ,    .
    '
    ' :
    '   "12; 13" + "13; 14"
    '   : "12; 13; 14"

    On Error GoTo ErrorHandler

    Dim resultText As String

    resultText = ""

    Call AppendDelimitedItems(resultText, firstText)
    Call AppendDelimitedItems(resultText, secondText)

    MergeDelimitedTextLists = resultText

    Exit Function

ErrorHandler:

    MergeDelimitedTextLists = Trim(Nz(firstText, ""))

End Function


Private Sub AppendDelimitedItems( _
    ByRef resultText As String, _
    ByVal sourceText As String _
)

    '    sourceText  resultText  .

    On Error GoTo ExitPoint

    Dim items() As String
    Dim index As Long
    Dim itemText As String

    If Trim(Nz(sourceText, "")) = "" Then
        Exit Sub
    End If

    items = Split(sourceText, ";")

    For index = LBound(items) To UBound(items)

        itemText = Trim(items(index))

        If itemText <> "" Then

            If DelimitedTextContains(resultText, itemText) = False Then

                If resultText = "" Then
                    resultText = itemText
                Else
                    resultText = resultText & "; " & itemText
                End If

            End If

        End If

    Next index

ExitPoint:

End Sub


Private Function DelimitedTextContains( _
    ByVal sourceText As String, _
    ByVal itemText As String _
) As Boolean

    '         .

    Dim normalizedSource As String
    Dim normalizedItem As String

    normalizedSource = "; " & Trim(Nz(sourceText, "")) & ";"
    normalizedSource = Replace(normalizedSource, ";;", ";")

    normalizedItem = "; " & Trim(Nz(itemText, "")) & ";"

    DelimitedTextContains = (InStr(1, normalizedSource, normalizedItem, vbTextCompare) > 0)

End Function


Private Function GetLaterDateValue( _
    ByVal firstDateValue As Variant, _
    ByVal secondDateValue As Variant _
) As Variant

    '      .
    '
    '    ,  .
    '   ,  Null.

    If IsNull(firstDateValue) And IsNull(secondDateValue) Then

        GetLaterDateValue = Null

    ElseIf IsNull(firstDateValue) Then

        GetLaterDateValue = secondDateValue

    ElseIf IsNull(secondDateValue) Then

        GetLaterDateValue = firstDateValue

    ElseIf CDate(firstDateValue) >= CDate(secondDateValue) Then

        GetLaterDateValue = firstDateValue

    Else

        GetLaterDateValue = secondDateValue

    End If

End Function