Compile Function Copy_Prod_Spec_To_Rev_Hist()
#pragma precomp SRP_PreCompiler

$Insert LOGICAL
$Insert PROD_SPEC_EQUATES
Declare function Database_Services, Error_Services
Declare subroutine Database_Services, Error_Services, Errmsg

TrimChars       = '. 1 2 3 4 5 6 7 8 9 0'
NextPSNo        = Database_Services('ReadDataRow', 'DICT.PROD_SPEC', '%SK%')
LastPSNo        = NextPSNo - 1
For PSNo = 1 to LastPSNo
    If RowExists('PROD_SPEC', PSNo, 0) then 
        PSRec           = Database_Services('ReadDataRow', 'PROD_SPEC', PSNo)
        If Error_Services("NoError") then
            PSRevHistRec    = ''
            Descriptions    = PSRec<PROD_SPEC_DESCRIPTION$>
            Usernames       = PSRec<PROD_SPEC_MOD_ID$>
            Dates           = PSRec<PROD_SPEC_MOD_DATE$>
            If ( Descriptions NE '' ) And ( Usernames NE '' ) And ( Dates NE '' ) then
                For each Line in Descriptions using @TM setting tPos
                    MatchFound  = ''
                    LineLength  = Len(Line)
                    Date        = Line[1, 'F ']
                    DescStart   = Col2() + 1
                    DescEnd     = LineLength - Col2()
                    Initials    = Line[-1, 'B ']
                    InitialsLen = Len(Initials)
                    DescEnd     = DescEnd - InitialsLen
                    Desc        = Line[DescStart, DescEnd]
                    InternalDate = IConv(Date, 'DH')
                    For each Character in TrimChars using ' '
                        Swap Character with '' in Initials
                    Next Character
                    Swap ' ' with '' in Initials
                    MatchFound = False$
                    Locate InternalDate in Dates using @VM setting vPos then                   
                        Loop
                            ModDate         = Dates<1, vPos>
                            If ModDate NE InternalDate then
                                // No username match found. Manually search for username.
                                Username = ''
                                For each Username in Usernames using @VM setting uPos
                                    UserInitials = Username[1,1]
                                    UserLastInitialPos = Index(Username, ' ', 1) + 1
                                    UserInitials := Username[UserLastInitialPos, 1]
                                    If Initials EQ UserInitials then
                                        MatchFound   = True$
                                        PSRevHistRec = Insert(PSRevHistRec, 1, -1, 0, Username)
                                        PSRevHistRec = Insert(PSRevHistRec, 2, -1, 0, InternalDate)
                                        PSRevHistRec = Insert(PSRevHistRec, 3, -1, 0, '')
                                        PSRevHistRec = Insert(PSRevHistRec, 4, -1, 0, Desc)
                                    end
                                Until MatchFound EQ True$
                                Next Username
                            end
                        While ModDate EQ InternalDate
                            ModName         = Usernames<1, vPos>
                            ModInitials     = ModName[1, 1]
                            LastInitialPos  = Index(ModName, ' ', 1) + 1
                            ModInitials     := ModName[LastInitialPos, 1]                            
                            If ( ModInitials EQ Initials ) then
                                MatchFound   = True$
                                PSRevHistRec = Insert(PSRevHistRec, 1, -1, 0, ModName)
                                PSRevHistRec = Insert(PSRevHistRec, 2, -1, 0, ModDate)
                                PSRevHistRec = Insert(PSRevHistRec, 3, -1, 0, '')
                                PSRevHistRec = Insert(PSRevHistRec, 4, -1, 0, Desc)        
                            end
                        Until MatchFound EQ True$
                            vPos = vPos + 1
                        Repeat
                    end else
                        // Date match not found. Use date supplied by user in description.
                        If InternalDate NE '' then
                            Username = ''
                            For each Username in Usernames using @VM setting uPos
                                UserInitials = Username[1,1]
                                UserLastInitialPos = Index(Username, ' ', 1) + 1
                                UserInitials := Username[UserLastInitialPos, 1]
                                If Initials EQ UserInitials then
                                    MatchFound   = True$
                                    PSRevHistRec = Insert(PSRevHistRec, 1, -1, 0, Username)
                                    PSRevHistRec = Insert(PSRevHistRec, 2, -1, 0, InternalDate)
                                    PSRevHistRec = Insert(PSRevHistRec, 3, -1, 0, '')
                                    PSRevHistRec = Insert(PSRevHistRec, 4, -1, 0, Desc)
                                end
                            Until MatchFound EQ True$
                            Next Username
                        end
                    end
                Next Line        
            end
            Database_Services('WriteDataRow', 'PROD_SPEC_REV_HIST', PSNo, PSRevHistRec)
            If Error_Services('NoError') then
                Null
            end else
                ErrMsg('Failed to write PROD_SPEC_REV_HIST record: ' : PSNo)
            end
        end else
            ErrMsg('Failed to read PROD_SPEC record: ' : PSNo)
        end
    end
Next PSNo

Return
