Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #61 and #215 #216

Merged
merged 9 commits into from
Sep 12, 2022
3 changes: 0 additions & 3 deletions Files/DataStandard/Vault.Custom/addinVault/powerGateMain.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
$global:addinPath = $PSScriptRoot
Import-Module "C:\ProgramData\coolOrange\powerGate\Modules\Initialize.psm1" -Global
Initialize-CoolOrange

ConnectToErpServerWithMessageBox
PatrickGrub marked this conversation as resolved.
Show resolved Hide resolved

function OnTabContextChanged_powerGate($xamlFile) {
Open-VaultConnection
if ($xamlFile -eq "ERP Item.xaml") {
Expand Down
19 changes: 19 additions & 0 deletions Files/powerEvents/Events/ErpService.Lifecycle.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,23 @@ function AddPdfJob($files, $successful) {
finally {
Log -End
}
}
Register-VaultEvent -EventName UpdateItemStates_Post -Action 'AddItemPdfJob'
function AddItemPdfJob($items) {
Log -Begin
try {
$releasedItems = @($items | Where-Object { $_._ReleasedRevision -eq $true})
foreach ($item in $releasedItems) {
$jobType = "Erp.Service.CreatePDFFromItem"
Write-Host "Adding job '$jobType' for item '$($item._Name)' to queue."
Add-VaultJob -Name $jobType -Parameters @{ "EntityId" = $item.Id; "EntityClassId" = "ITEM" } -Description "Create PDF for item '$($item._Name)' and upload to ERP system" -Priority 101
}
}
catch {
Write-Error -Message $_.Exception.Message
ShowMessageBox -Message $_.Exception.Message -Icon Error
}
finally {
Log -End
}
}
2 changes: 1 addition & 1 deletion Files/powerEvents/Modules/Import.Modules.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ if ($allowedProcesses -contains $processName) {
$logPath = Join-Path $env:LOCALAPPDATA "coolOrange\Projects\powerEvents.log"
Set-LogFilePath -Path $logPath

ConnectToErpServer
ConnectToConfiguredErpServer
}
33 changes: 30 additions & 3 deletions Files/powerGate/Modules/Communication.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@ function getRelatedPGServerName {
return $PGServerDefinitions["PROD"]
}
}

function ConnectToErpServerWithMessageBox {
function CreateUrlFromPGServerName {
Log -Begin
$powerGateServerName = getRelatedPGServerName
if (-not $powerGateServerName) {
return;
}
$powerGateServerErpPluginUrl = "http://$($powerGateServerName):$($powerGateServerPort)/coolOrange/ErpServices"
#$powerGateServerErpPluginUrl = "http://$($powerGateServerName):$($powerGateServerPort)/coolOrange/DynamicsNav"
# Dynamics NAV 2017 Plugin available here: https://github.com/coolOrangeLabs/powergate-dynamics-nav-sample/releases
Log -End
return $powerGateServerErpPluginUrl;

if (-not $powerGateServerName){
}
function ConnectToErpServerWithMessageBox {
Log -Begin
$powerGateServerErpPluginUrl = CreateUrlFromPGServerName
if (-not $powerGateServerErpPluginUrl){
ShowMessageBox -Message "The current connected VAULT $($vaultConnection.Vault) is not mapped in the configuration for any ERP.`nChange the configuration and restart vault!" -Icon Error | Out-Null
}
else {
Expand All @@ -50,6 +58,25 @@ function ConnectToErpServerWithMessageBox {
}
Log -End
}
# Use this function in Jobs
function ConnectToConfiguredErpServer {
Log -Begin
$powerGateServerErpPluginUrl = CreateUrlFromPGServerName
if (-not $powerGateServerErpPluginUrl){
Write-Error -Message "no ERP Server URL specified!"
return
}
else {
$connected = ConnectToErpServer -PowerGateServerErpPluginUrl $powerGateServerErpPluginUrl
if (-not $connected) {
$connectionError = ("Error on connecting to powerGateServer service! Check if powerGateServer is running on following host: '{0}' or try to access following link via your browser: '{1}'" -f (([Uri]$powerGateServerErpPluginUrl).Authority), $powerGateServerErpPluginUrl)
Write-Error -Message $connectionError
}
return $connected
}
Log -End
}


function ConnectToErpServer($PowerGateServerErpPluginUrl) {
Log -Begin
Expand Down
62 changes: 62 additions & 0 deletions Files/powerJobs/Jobs/Erp.Service.CreatePDFFromItem.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# JobEntityType = ITEM
$logPath = Join-Path $env:LOCALAPPDATA "coolOrange\Projects\ErpService.Create.Pdf-Job.log"
Set-LogFilePath -Path $logPath
Import-Module "C:\ProgramData\coolOrange\powerGate\Modules\Communication.psm1"
Write-Host "Starting job 'Create PDF as attachment' for item '$($item._Name)' ..."
$attachedDrawings = Get-VaultItemAssociations -Number $item._Number
foreach ($drawing in $attachedDrawings) {
$hidePDF = $false
$workingDirectory = "C:\Temp\$($drawing._Name)"
$localPDFfileLocation = "$workingDirectory\$($drawing._Name).pdf"
$vaultPDFfileLocation = $drawing._EntityPath + "/" + (Split-Path -Leaf $localPDFfileLocation)

Log -Message "Create PDF as attachment for file '$($drawing._Name)' ..."

if ( @("idw", "dwg") -notcontains $drawing._Extension ) {
Log -Message "Files with extension: '$($drawing._Extension)' are not supported"
continue
}

$downloadedFiles = Save-VaultFile -File $drawing._FullPath -DownloadDirectory $workingDirectory
$drawing = $downloadedFiles | Select-Object -First 1
$openResult = Open-Document -LocalFile $drawing.LocalPath
if ($openResult) {
if ($openResult.Application.Name -like 'Inventor*') {
$configFile = "$($env:POWERJOBS_MODULESDIR)Export\PDF_2D.ini"
}
else {
$configFile = "$($env:POWERJOBS_MODULESDIR)Export\PDF.dwg"
}
$exportResult = Export-Document -Format 'PDF' -To $localPDFfileLocation -Options $configFile
if ($exportResult) {
$PDFfile = Add-VaultFile -From $localPDFfileLocation -To $vaultPDFfileLocation -FileClassification DesignVisualization -Hidden $hidePDF
$item = Update-VaultItem -Number $item._Number -AddAttachments @($PDFfile.'Full Path')
Log -Message "Connecting to powerGate..."
# $connected = ConnectToErpServer
# if (-not $connected) {
# throw "Connection to powerGateServer could not be established!"
# }

# Log -Message "Uploading PDF file $($PDFfile._Name) to ERP system..."
# $d = New-ERPObject -EntityType "Document"
# $d.FileName = $PDFfile._Name
# $d.Number = $drawing._PartNumber
# $d.Description = $drawing._Description
# Add-ERPMedia -EntitySet "Documents" -Properties $d -ContentType "application/pdf" -File $localPDFfileLocation
}
$closeResult = Close-Document
}

Clean-Up -folder $workingDirectory

if (-not $openResult) {
throw("Failed to open document $($drawing.LocalPath)! Reason: $($openResult.Error.Message)")
}
if (-not $exportResult) {
throw("Failed to export document $($drawing.LocalPath) to $localPDFfileLocation! Reason: $($exportResult.Error.Message)")
}
if (-not $closeResult) {
throw("Failed to close document $($drawing.LocalPath)! Reason: $($closeResult.Error.Message))")
}
Log -Message "Completed job Create PDF as attachment"
}
2 changes: 1 addition & 1 deletion Files/powerJobs/Jobs/ErpService.Create.PDF.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if ($openResult) {
$file = Update-VaultFile -File $file._FullPath -AddAttachments @($PDFfile._FullPath)

Log -Message "Connecting to powerGate..."
$connected = ConnectToErpServer
$connected = ConnectToConfiguredErpServer
if (-not $connected) {
throw "Connection to powerGateServer could not be established!"
}
Expand Down