UpgradeTools/Create-AdditionsUpgradeObjects.ps1

function Create-AdditionsUpgradeObjects {
    Param(
        [Parameter(Mandatory=$false)]
        [string]
        $BranchPath = (Get-TfsBranches | Out-GridView -OutputMode Single),
        [Parameter(Mandatory=$false)]
        [int]
        $UpgradeTableID = 99000,
        [Parameter(Mandatory=$false)]
        [String]
        $Additions
    )
 
    $Tables = @()
    $VersionLists = Get-ObjectSummaryByVersionList (Get-BranchObjectsDifferentToBase -BranchPath $BranchPath)    
    foreach ($Version in $VersionLists.GetEnumerator()) {
        if ($Version.Name -ne ''){
            if (Get-VersionListIsAddition -VersionCode $Version.Name -SearchVersionCodes $Additions) {
              #find correct changeset for addition version
              $AddsBranches = Get-TFSBranches | Where{$_.Contains((Get-AdditionNameFromCode -AdditionCode $Version.Name))}
              $ChangesetNo = 0
              $AddBranch = ''
              foreach ($TempAddBranch in $AddsBranches) {
                $TempChangesetNo = Get-ChangesetNumberForVersionList -BranchPath $TempAddBranch -VersionList ($Version.Name + $Version.Value.ToString()) -NoError
                if ($TempChangesetNo -gt 0) {
                    $ChangesetNo = $TempChangesetNo
                    $AddBranch = $TempAddBranch
                }
              }
              if ($ChangesetNo -eq 0){
                $ChangesetNo = Get-ChangesetsInBranch -BranchPath $TempAddBranch -ChangeSetOption Newest
                $AddBranch = $TempAddBranch
              }
                $Tables += Get-NewTablesAndFieldsInBranch -BranchPath $AddBranch -ChangesetNo $ChangesetNo -VersionCode $Version.Name
            }
        }
    }

    #generate text files of upgrade table objects
    $ResultPath = Create-TempDirectory
    $UniqueTables = $Tables.TableId | Sort | Get-Unique
    foreach($UniqueTable in $UniqueTables){
        $FilePath = Join-Path $ResultPath (-join('TAB' ,$UpgradeTableID,'.txt'))
        New-Item -Path $FilePath -ItemType File
        $FieldVar = '';
        foreach ($Table in $Tables | Where-Object TableId -eq $UniqueTable){
            $TableFields = $Table.Fields            
            foreach($TableField in $TableFields){
                if($TableField.Type -eq 'Option'){
                    $FieldVar += (-join '{' ,$TableField.FieldID, ' ; ;', $TableField.Name,';',$TableField.Type,'; Description=',$TableField.Code,'; OptionString=A,B,C,D}')
                }else{
                    $FieldVar += (-join '{' ,$TableField.FieldID, ' ; ;', $TableField.Name,';',$TableField.Type,'; Description=',$TableField.Code,'}')
                }
            }              
        }

        if($Table.PrimaryKeyFields -ne $null){
            foreach($PrimaryKeyField in $Table.PrimaryKeyFields){
                if($PrimaryKeyField.Type -eq 'Option'){
                    $FieldVar += (-join '{' ,$PrimaryKeyField.FieldID, ' ; ;', $PrimaryKeyField.Name,';',$PrimaryKeyField.Type,'; OptionString=A,B,C,D}')
                }else{
                    $FieldVar += (-join '{' ,$PrimaryKeyField.FieldID, ' ; ;', $PrimaryKeyField.Name,';',$PrimaryKeyField.Type,'}')
                }
            }     
        }
        Create-NAVRecordFile -FilePath $FilePath -TableId $UpgradeTableID -TableName $Table.TableName -Fields $FieldVar -KeyDef $Table.KeyDef
        $UpgradeTableID++  
    }

    $DesktopPath = Join-Path ([Environment]::GetFolderPath('Desktop')) 'UpgradeResult'
    Create-EmptyDirectory $DesktopPath
    
    Join-NAVApplicationObjectFile -Destination (Join-Path $DesktopPath 'Result.TXT') -Source $ResultPath
    Split-NAVApplicationObjectFile -Source (Join-Path $DesktopPath 'Result.TXT') -Destination  (Join-Path $DesktopPath 'Result')

    #generate v2 upgrade app(?)
}

function Get-VersionListIsAddition {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$VersionCode,
        [Parameter(Mandatory=$false)]
        [string]$SearchVersionCodes
    )

    if($SearchVersionCodes -ne ''){
        $AdditionVersionCodes = $SearchVersionCodes.Split(',')
    }else{
        $AdditionVersionCodes = ('ADV','ASI','ATT','CC','CH','DTR','DVT','DFD','DD','DL','EC','EDI','GBS','IBD','LC','PF','PGY','PRO','QPM','SIL','SPM','SAI','SFDC','UPSL')
    }
    $AdditionVersionCodes.Contains($VersionCode)
}

function Get-AdditionNameFromCode {
    Param(
        [Parameter(Mandatory=$false)]
        [string]$AdditionCode
    )

    switch ($AdditionCode) {
        'ADV' {'AddressValidation';break}
        'ASI' {'AssociatedItems';break}
        'ATT' {'Attributes';break}
        'CC' {}
        'CH' {'CreditManagement';break}
        'DTR' {'DataReplication';break}
        'DVT' {'DataValidation';break}
        'DFD' {'DefaultDimensions';break}
        'DD' {'DocumentDelivery';break}
        'DL' {'DocumentLinks';break}
        'EC' {'eCommerce';break}
        'EDI' {'EDI';break}
        'GBS' {'GlobalSearch';break}
        'IBD' {'InboundDocuments';break}
        'LC' {'LandedCosts';break}
        'PF' {'PortalFramework';break}
        'PGY' {'PaymentGateway';break}
        'PRO' {'Promotions';break}
        'QPM' {'QualityProcessManagement';break}
        'SIL' {'SalesInputLog';break}
        'SFDC' {'ShopFloorDataCapture';break}
        'SPM' {'ShipmentManagement';break}
        'SAI' {'ShippingAgentIntegration';break}
        'UPSL' {'Upselling';break}
    }
}

function Create-NAVRecordFile{
    Param(
    [Parameter(Mandatory=$true)]
    [string]$FilePath,
    [Parameter(Mandatory=$true)]
    [string]$TableId,
    [Parameter(Mandatory=$true)]
    [string]$TableName,
    [Parameter(Mandatory=$true)]
    $Fields,
    [Parameter(Mandatory=$true)]
    $KeyDef
    )

    if ($TableName.Length -gt 26){
        $TableName = $TableName.Substring(0,26)
    }
    Set-Content -Path $FilePath -Value ("OBJECT Table $TableId $TableName UPG" + [Environment]::NewLine +
                                        "{" + [Environment]::NewLine +
                                        "OBJECT-PROPERTIES" + [Environment]::NewLine + 
                                        "{" + [Environment]::NewLine +
                                        "Date=" + (Get-Date -UFormat "%d/%m/%y") + ";" + [Environment]::NewLine +
                                        "Time=12:00:00;" + [Environment]::NewLine +
                                        "Version List=Clever Dynamics UPG;" + [Environment]::NewLine +
                                        "}" + [Environment]::NewLine + 
                                        "PROPERTIES" + [Environment]::NewLine + "{" + [Environment]::NewLine + "}" + [Environment]::NewLine +
                                        "FIELDS" + [Environment]::NewLine + "{" + [Environment]::NewLine + $Fields + [Environment]::NewLine + "}" + [Environment]::NewLine +
                                        $KeyDef + [Environment]::NewLine + 
                                        "FIELDGROUPS"+ [Environment]::NewLine + "{" + [Environment]::NewLine + "}" + [Environment]::NewLine +
                                        "CODE" + [Environment]::NewLine + "{" + [Environment]::NewLine + "BEGIN" + [Environment]::NewLine + "END." + [Environment]::NewLine + "}" + [Environment]::NewLine + "}")
}

Export-ModuleMember -Function Create-AdditionsUpgradeObjects