83 lines
3.8 KiB
PowerShell
83 lines
3.8 KiB
PowerShell
param([string]$PayloadJson)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
try {
|
|
$p = $PayloadJson | ConvertFrom-Json
|
|
|
|
# ── Connect ──────────────────────────────────────────────────────
|
|
$co = New-Object -ComObject SAPbobsCOM.Company
|
|
$co.Server = $p.Server
|
|
$co.CompanyDB = $p.CompanyDB
|
|
$co.UserName = $p.UserName
|
|
$co.Password = $p.Password
|
|
$co.DbUserName = $p.DbUserName
|
|
$co.DbPassword = $p.DbPassword
|
|
$co.DbServerType = 11 # confirmed working for this SAP B1 installation
|
|
$co.UseTrusted = $false
|
|
|
|
[Console]::Error.WriteLine("[DI-API] Connecting as $($p.UserName) to $($p.CompanyDB)")
|
|
|
|
$ret = $co.Connect()
|
|
if ($ret -ne 0) {
|
|
$err = $co.GetLastErrorDescription()
|
|
Write-Output ("{`"success`":false,`"error`":`"DI Login failed [$ret]: $err`"}")
|
|
exit 1
|
|
}
|
|
|
|
# ── Purchase Request ─────────────────────────────────────────────
|
|
$pr = $co.GetBusinessObject(1470000113)
|
|
|
|
# Header — use payload values, fall back to sensible defaults
|
|
$pr.DocDate = if ($p.DocDate) { [DateTime]$p.DocDate } else { Get-Date }
|
|
$pr.DocDueDate = if ($p.DocDueDate) { [DateTime]$p.DocDueDate } else { (Get-Date).AddDays(2) }
|
|
$pr.RequriedDate = if ($p.RequriedDate) { [DateTime]$p.RequriedDate } else { (Get-Date).AddDays(2) }
|
|
|
|
if ($p.Comments) { $pr.Comments = $p.Comments }
|
|
if ($p.CardCode) { $pr.CardCode = $p.CardCode }
|
|
if ($p.NumAtCard) { $pr.NumAtCard = $p.NumAtCard }
|
|
if ($p.RequesterEmail) { $pr.RequesterEmail = $p.RequesterEmail }
|
|
|
|
# UDFs
|
|
try { if ($p.U_ReqDqte) { $pr.UserFields.Fields.Item("U_ReqDqte").Value = $p.U_ReqDqte } } catch {}
|
|
try { if ($p.U_Depart) { $pr.UserFields.Fields.Item("U_Depart").Value = $p.U_Depart } } catch {}
|
|
try { if ($p.U_Department) { $pr.UserFields.Fields.Item("U_Department").Value = $p.U_Department } } catch {}
|
|
try { if ($p.U_STS_REQD) { $pr.UserFields.Fields.Item("U_STS_REQD").Value = $p.U_STS_REQD } } catch {}
|
|
|
|
# ── Lines ─────────────────────────────────────────────────────────
|
|
$first = $true
|
|
foreach ($line in $p.DocumentLines) {
|
|
if (-not $first) { $pr.Lines.Add() | Out-Null }
|
|
|
|
$pr.Lines.ItemCode = $line.ItemCode
|
|
|
|
if ($line.ItemDescription) { $pr.Lines.ItemDescription = $line.ItemDescription }
|
|
if ($line.Quantity) { $pr.Lines.Quantity = [double]$line.Quantity }
|
|
if ($null -ne $line.UnitPrice) { $pr.Lines.UnitPrice = [double]$line.UnitPrice }
|
|
if ($line.WarehouseCode) { $pr.Lines.WarehouseCode = $line.WarehouseCode }
|
|
if ($line.RequiredDate) { $pr.Lines.RequiredDate = [DateTime]$line.RequiredDate }
|
|
|
|
$first = $false
|
|
}
|
|
|
|
# ── Add Document ─────────────────────────────────────────────────
|
|
[Console]::Error.WriteLine("[PR] Adding Purchase Request...")
|
|
|
|
$addRet = $pr.Add()
|
|
if ($addRet -ne 0) {
|
|
$err = $co.GetLastErrorDescription()
|
|
$co.Disconnect()
|
|
Write-Output ("{`"success`":false,`"error`":`"PR Add failed [$addRet]: $err`"}")
|
|
exit 1
|
|
}
|
|
|
|
$docEntry = $co.GetNewObjectKey()
|
|
[Console]::Error.WriteLine("[PR] SUCCESS : $docEntry")
|
|
|
|
$co.Disconnect()
|
|
Write-Output ("{`"success`":true,`"DocEntry`":$docEntry}")
|
|
|
|
} catch {
|
|
Write-Output ("{`"success`":false,`"error`":`"$($_.Exception.Message -replace '`"','')`"}")
|
|
}
|