Skip to content

Commit

Permalink
- Changed erpItemTab_control to script scope so it can be used in wpf…
Browse files Browse the repository at this point in the history
… events

- Passing erpItemTab_control into ValidateErpMaterialTab
- Copied changes to item tab
  • Loading branch information
ThomasRossmeisl committed Nov 28, 2022
1 parent 59a3514 commit 61a3bb1
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 118 deletions.
47 changes: 25 additions & 22 deletions Files/powerEvents/Events/TransferERPItemViaFileTab.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Add-VaultTab($name, $EntityType, $Action){
'@)
$debugERPTab_window = [Windows.Markup.XamlReader]::Load($xamlReader)
$debugERPTab_window.Title = "powerGate Debug Window for Tab: $name"
$debugERPTab_window.AddChild($action.InvokeReturnAsIs($selectedFile))
$debugERPTab_window.AddChild($action.InvokeReturnAsIs($selectedFile))
$debugERPTab_window.ShowDialog()
}

Expand All @@ -48,7 +48,7 @@ Set-LogFilePath -Path $logPath
Add-VaultTab -Name 'ERP Item' -EntityType 'File' -Action {
param($selectedFile)

$erpItemTab_control = [Windows.Markup.XamlReader]::Load([System.Xml.XmlNodeReader]::new([xml](Get-Content "$PSScriptRoot\ERPItem_Tab.xaml")))
$Script:erpItemTab_control = [Windows.Markup.XamlReader]::Load([System.Xml.XmlNodeReader]::new([xml](Get-Content "$PSScriptRoot\ERPItem_Tab.xaml")))

#region Initialize viewmodel
$materialTabContext = New-Object -Type PsObject -Property @{
Expand All @@ -71,33 +71,33 @@ Add-VaultTab -Name 'ERP Item' -EntityType 'File' -Action {
$erpItemTab_Description.Add_TextChanged({
param($Sender)

ValidateErpMaterialTab -MaterialTabContext $Sender.DataContext
ValidateErpMaterialTab -ErpItemTab $Script:erpItemTab_control
})

$erpItemTab_MaterialTypeList = $erpItemTab_control.FindName('MaterialTypeList')
$erpItemTab_MaterialTypeList = $Script:erpItemTab_control.FindName('MaterialTypeList')
$erpItemTab_MaterialTypeList.Add_SelectionChanged({
param($Sender)

ValidateErpMaterialTab -MaterialTabContext $Sender.DataContext
ValidateErpMaterialTab -ErpItemTab $Script:erpItemTab_control
})
#endregion Register Validate tab events

$erpItemTab_LinkMaterialButton = $erpItemTab_control.FindName("LinkMaterialButton")
$erpItemTab_LinkMaterialButton = $Script:erpItemTab_control.FindName("LinkMaterialButton")
$erpItemTab_LinkMaterialButton.IsEnabled = (IsEntityUnlocked -Entity $selectedFile)
$erpItemTab_LinkMaterialButton.Add_Click({
param($Sender)
param($Sender, $EventArgs)

LinkErpMaterial -MaterialTabContext $Sender.DataContext
LinkErpMaterial -ErpItemTab $Script:erpItemTab_control
})

$erpItemTab_CreateOrUpdateMaterialButton = $erpItemTab_control.FindName("CreateOrUpdateMaterialButton")
$erpItemTab_CreateOrUpdateMaterialButton = $Script:erpItemTab_control.FindName("CreateOrUpdateMaterialButton")
$erpItemTab_CreateOrUpdateMaterialButton.Add_Click({
param($Sender)

CreateOrUpdateErpMaterial -MaterialTabContext $Sender.DataContext
})

$erpItemTab_GoToMaterialButton = $erpItemTab_control.FindName("GoToMaterialButton")
$erpItemTab_GoToMaterialButton = $Script:erpItemTab_control.FindName("GoToMaterialButton")
$erpItemTab_GoToMaterialButton.IsEnabled = $true
$erpItemTab_GoToMaterialButton.Add_Click({
param($Sender)
Expand All @@ -109,33 +109,36 @@ Add-VaultTab -Name 'ERP Item' -EntityType 'File' -Action {

$erpServices = Get-ERPServices -Available
if (-not $erpServices) {
$erpItemTab_control.FindName("lblStatusMessage").Content = "One or more services are not available!"
$erpItemTab_control.FindName("lblStatusMessage").Foreground = "Red"
$erpItemTab_control.IsEnabled = $false
return $erpItemTab
$Script:erpItemTab_control.FindName("lblStatusMessage").Content = "One or more services are not available!"
$Script:erpItemTab_control.FindName("lblStatusMessage").Foreground = "Red"
$Script:erpItemTab_control.IsEnabled = $false
return $Script:erpItemTab_control
}

$number = GetEntityNumber -entity $selectedFile
$erpMaterial = Get-ERPObject -EntitySet "Materials" -Keys @{ Number = $number }
if(-not $?) {
$erpItemTab_control.FindName("lblStatusMessage").Content = $Error[0]
$erpItemTab_control.FindName("lblStatusMessage").Foreground = "Red"
$erpItemTab_control.IsEnabled = $false
return $erpItemTab
$Script:erpItemTab_control.FindName("lblStatusMessage").Content = $Error[0]
$Script:erpItemTab_control.FindName("lblStatusMessage").Foreground = "Red"
$Script:erpItemTab_control.IsEnabled = $false
return $Script:erpItemTab_control
}

if (-not $erpMaterial) {
$erpMaterial = NewErpMaterial
$erpMaterial = PrepareErpMaterialForCreate -erpMaterial $erpMaterial -vaultEntity $selectedFile
$materialTabContext.IsCreate = $true
$materialTabContext.ErpEntity = $erpMaterial
$erpItemTab_control.FindName("GoToMaterialButton").IsEnabled = $false
$Script:erpItemTab_control.FindName("GoToMaterialButton").IsEnabled = $false
}
else {
$materialTabContext.ErpEntity = $erpMaterial
$erpItemTab_control.FindName("GoToMaterialButton").IsEnabled = $true
$Script:erpItemTab_control.FindName("GoToMaterialButton").IsEnabled = $true
}

$erpItemTab_control.DataContext = $materialTabContext
ValidateErpMaterialTab
$Script:erpItemTab_control.DataContext = $materialTabContext
ValidateErpMaterialTab -ErpItemTab $Script:erpItemTab_control
#$materialTabContext.IsCreate = $true
return $Script:erpItemTab_control

}
186 changes: 97 additions & 89 deletions Files/powerEvents/Events/TransferERPItemViaItemTab.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ Import-Module powerEvents
Open-VaultConnection -Server $env:Computername -Vault Vault -User Administrator -Password ""
$selectedItem = Get-VaultItem -Number 'co-00000'

function Add-VaultTab($name, $EntityType, $Action){
Add-Type -AssemblyName PresentationFramework

$xamlReader = New-Object System.Xml.XmlNodeReader ([xml]@'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="Background" Value="#FFFDFDFD" />
</Style>
</Window.Resources>
</Window>
'@)
$debugERPTab_window = [Windows.Markup.XamlReader]::Load($xamlReader)
$debugERPTab_window.Title = "powerGate Debug Window for Tab: $name"
$debugERPTab_window.AddChild($action.InvokeReturnAsIs($selectedItem))
$debugERPTab_window.ShowDialog()
}


Import-Module powergate
Connect-ERP -Service 'http://thomas-rossi:8080/PGS/ErpServices'
#endregion
Expand All @@ -24,113 +45,100 @@ $logPath = Join-Path $env:LOCALAPPDATA "coolOrange\Projects\VDS_Vault-powerGate.
Set-LogFilePath -Path $logPath


Add-VaultTab -Name 'ERP Item' -EntityType 'File' -Action {
param($selectedItem)

# if ($vaultConnection.Vault -notin $vaultToPgsMapping.Keys) {
# throw "The currently connected Vault '$($vaultConnection.Vault)' is not mapped to any powerGateServer URL. Please extend the configuration and re-submit the job!"
# }
$Script:erpItemTab_control = [Windows.Markup.XamlReader]::Load([System.Xml.XmlNodeReader]::new([xml](Get-Content "$PSScriptRoot\ERPItem_Tab.xaml")))

# Write-Host "Connecting to powerGateServer on: $($vaultToPgsMapping[$vaultConnection.Vault])"
# $connected = Connect-ERP -Service "http://$($vaultToPgsMapping[$vaultConnection.Vault]):8080/PGS/ErpServices"
# if(-not $connected) {
# throw("Connection to ERP could not be established! Reason: $($Error[0]) (Source: $($Error[0].Exception.Source))")
# }
#region Initialize viewmodel
$materialTabContext = New-Object -Type PsObject -Property @{
ErpEntity = $null
VaultEntity = $selectedItem
IsCreate = $false

#region Initialize material tab
Add-Type -AssemblyName PresentationFramework
[xml]$erpMaterialTabXaml = Get-Content -LiteralPath "C:\ProgramData\coolOrange\powerEvents\Events\ErpTab.Item.xaml"
$xamlReader = [System.Xml.XmlNodeReader]::new($erpMaterialTabXaml)
$erpMaterialTab = [Windows.Markup.XamlReader]::Load($xamlReader)
#endregion Initialize material tab
Lists = @{
UomList = @(GetUnitOfMeasuresList)
MaterialTypeList = @(GetMaterialTypeList)
CategoryList = @(GetCategoryList)
}
}
#endregion Initialize viewmodel


#region Initialize viewmodel
$materialTabContext = New-Object -Type PsObject -Property @{
ErpEntity = $null
VaultEntity = $selectedItem
IsCreate = $false

Lists = @{
UomList = @(GetUnitOfMeasuresList)
MaterialTypeList = @(GetMaterialTypeList)
CategoryList = @(GetCategoryList)
}
}
#endregion Initialize viewmodel
#region Initialize UI Components
#region Register Validate tab events
$erpItemTab_Description = $erpItemTab_control.FindName('Description')
$erpItemTab_Description.Add_TextChanged({
param($Sender)

ValidateErpMaterialTab -ErpItemTab $Script:erpItemTab_control
})

#region Initialize UI Components
#region Register Validate tab events
$erpMaterialTab_Description = $erpMaterialTab.FindName('Description')
$erpMaterialTab_Description.Add_TextChanged({
param($Sender)
$erpItemTab_MaterialTypeList = $Script:erpItemTab_control.FindName('MaterialTypeList')
$erpItemTab_MaterialTypeList.Add_SelectionChanged({
param($Sender)

ValidateErpMaterialTab -MaterialTabContext $Sender.DataContext
})
ValidateErpMaterialTab -ErpItemTab $Script:erpItemTab_control
})
#endregion Register Validate tab events

$erpMaterialTab_MaterialTypeList = $erpMaterialTab.FindName('MaterialTypeList')
$erpMaterialTab_MaterialTypeList.Add_SelectionChanged({
param($Sender)
$erpItemTab_LinkMaterialButton = $Script:erpItemTab_control.FindName("LinkMaterialButton")
$erpItemTab_LinkMaterialButton.IsEnabled = (IsEntityUnlocked -Entity $selectedItem)
$erpItemTab_LinkMaterialButton.Add_Click({
param($Sender, $EventArgs)

ValidateErpMaterialTab -MaterialTabContext $Sender.DataContext
})
#endregion Register Validate tab events
LinkErpMaterial -ErpItemTab $Script:erpItemTab_control
})

$erpMaterialTab_LinkMaterialButton = $erpMaterialTab.FindName("LinkMaterialButton")
$erpMaterialTab_LinkMaterialButton.IsEnabled = (IsEntityUnlocked -Entity $selectedItem)
$erpMaterialTab_LinkMaterialButton.Add_Click({
param($Sender)
$erpItemTab_CreateOrUpdateMaterialButton = $Script:erpItemTab_control.FindName("CreateOrUpdateMaterialButton")
$erpItemTab_CreateOrUpdateMaterialButton.Add_Click({
param($Sender)

LinkErpMaterial -MaterialTabContext $Sender.DataContext
})
CreateOrUpdateErpMaterial -MaterialTabContext $Sender.DataContext
})

$erpMaterialTab_CreateOrUpdateMaterialButton = $erpMaterialTab.FindName("CreateOrUpdateMaterialButton")
$erpMaterialTab_CreateOrUpdateMaterialButton.Add_Click({
param($Sender)
$erpItemTab_GoToMaterialButton = $Script:erpItemTab_control.FindName("GoToMaterialButton")
$erpItemTab_GoToMaterialButton.IsEnabled = $true
$erpItemTab_GoToMaterialButton.Add_Click({
param($Sender)

CreateOrUpdateErpMaterial -MaterialTabContext $Sender.DataContext
})
GoToErpMaterial -MaterialTabContext $Sender.DataContext
})
#endregion Initialize UI Components

$erpMaterialTab_GoToMaterialButton = $erpMaterialTab.FindName("GoToMaterialButton")
$erpMaterialTab_GoToMaterialButton.IsEnabled = $true
$erpMaterialTab_GoToMaterialButton.Add_Click({
param($Sender)

GoToErpMaterial -MaterialTabContext $Sender.DataContext
})
#endregion Initialize UI Components
$erpServices = Get-ERPServices -Available
if (-not $erpServices) {
$Script:erpItemTab_control.FindName("lblStatusMessage").Content = "One or more services are not available!"
$Script:erpItemTab_control.FindName("lblStatusMessage").Foreground = "Red"
$Script:erpItemTab_control.IsEnabled = $false
return $Script:erpItemTab_control
}

$number = GetEntityNumber -entity $selectedItem
$erpMaterial = Get-ERPObject -EntitySet "Materials" -Keys @{ Number = $number }
if(-not $?) {
$Script:erpItemTab_control.FindName("lblStatusMessage").Content = $Error[0]
$Script:erpItemTab_control.FindName("lblStatusMessage").Foreground = "Red"
$Script:erpItemTab_control.IsEnabled = $false
return $Script:erpItemTab_control
}

$erpServices = Get-ERPServices -Available
if (-not $erpServices) {
$erpMaterialTab.FindName("lblStatusMessage").Content = "One or more services are not available!"
$erpMaterialTab.FindName("lblStatusMessage").Foreground = "Red"
$erpMaterialTab.IsEnabled = $false
$erpMaterialTab.ShowDialog()
return
}
if (-not $erpMaterial) {
$erpMaterial = NewErpMaterial
$erpMaterial = PrepareErpMaterialForCreate -erpMaterial $erpMaterial -vaultEntity $selectedItem
$materialTabContext.IsCreate = $true
$materialTabContext.ErpEntity = $erpMaterial
$Script:erpItemTab_control.FindName("GoToMaterialButton").IsEnabled = $false
}
else {
$materialTabContext.ErpEntity = $erpMaterial
$Script:erpItemTab_control.FindName("GoToMaterialButton").IsEnabled = $true
}

$number = GetEntityNumber -entity $selectedItem
$erpMaterial = Get-ERPObject -EntitySet "Materials" -Keys @{ Number = $number }
if(-not $?) {
$erpMaterialTab.FindName("lblStatusMessage").Content = $Error[0]
$erpMaterialTab.FindName("lblStatusMessage").Foreground = "Red"
$erpMaterialTab.IsEnabled = $false
$tab_control.ShowDialog()
return
}
$Script:erpItemTab_control.DataContext = $materialTabContext
ValidateErpMaterialTab -ErpItemTab $Script:erpItemTab_control
#$materialTabContext.IsCreate = $true
return $Script:erpItemTab_control

if (-not $erpMaterial) {
$erpMaterial = NewErpMaterial
$erpMaterial = PrepareErpMaterialForCreate -erpMaterial $erpMaterial -vaultEntity $selectedItem
$materialTabContext.IsCreate = $true
$materialTabContext.ErpEntity = $erpMaterial
$erpMaterialTab.FindName("GoToMaterialButton").IsEnabled = $false
}
else {
$materialTabContext.ErpEntity = $erpMaterial
$erpMaterialTab.FindName("GoToMaterialButton").IsEnabled = $true
}

$erpMaterialTab.DataContext = $materialTabContext
ValidateErpMaterialTab
$erpMaterialTab.ShowDialog()
18 changes: 11 additions & 7 deletions Files/powerEvents/Modules/ErpTabs.Material.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ function IsEntityUnlocked($Entity) {
}

function ValidateErpMaterialTab {
param($MaterialTabContext)
param(
$ErpItemTab
)

$erpMaterial = $MaterialTabContext.ErpEntity
$erpMaterial = $ErpItemTab.DataContext.ErpEntity

if ($erpMaterial.Number) {
$entityUnlocked = $true
}
else {
$entityUnlocked = (IsEntityUnlocked -Entity $MaterialTabContext.VaultEntity)
$entityUnlocked = (IsEntityUnlocked -Entity $ErpItemTab.DataContext.VaultEntity)
}

#TODO: Setup obligatory fields that need to be filled out to activate the 'Create' button
Expand All @@ -32,7 +34,7 @@ function ValidateErpMaterialTab {
}
$enabled = $entityUnlocked -and $type -and $description

$erpMaterialTab.FindName("CreateOrUpdateMaterialButton").IsEnabled = $enabled
$ErpItemTab.FindName("CreateOrUpdateMaterialButton").IsEnabled = $enabled
}

function CreateOrUpdateErpMaterial {
Expand Down Expand Up @@ -68,14 +70,16 @@ function GoToErpMaterial {
}

function LinkErpMaterial {
param($MaterialTabContext)
param(
$ErpItemTab
)

$erpMaterial = OpenErpSearchWindow
if (-not $erpMaterial) {
return
}

$vaultEntity = $MaterialTabContext.VaultEntity
$vaultEntity = $ErpItemTab.DataContext.VaultEntity
if ($vaultEntity._EntityTypeID -eq "ITEM") {
$existingEntity = Get-VaultItem -Number $erpMaterial.Number
if ($existingEntity) {
Expand All @@ -101,7 +105,7 @@ function LinkErpMaterial {
$answer = ShowMessageBox -Message ($message + "Do you really want to link the item '$($erpMaterial.Number)'?") -Title "powerGate ERP - Link Item" -Button "YesNo" -Icon "Question"
if ($answer -eq [System.Windows.Forms.DialogResult]::Yes) {
SetEntityProperties -erpMaterial $erpMaterial -vaultEntity $vaultEntity
$erpMaterialTab.DataContext.ErpEntity = $erpMaterial
$ErpItemTab.DataContext.ErpEntity = $erpMaterial
[System.Windows.Forms.SendKeys]::SendWait("{F5}")
}
}

0 comments on commit 61a3bb1

Please sign in to comment.