Billomat.psm1
$baseUrl = "https://guidnew.billomat.net/api"; <# .SYNOPSIS Connects to Billomat. .DESCRIPTION Credentials can be persisted using https://www.powershellgallery.com/packages/CredentialsManager/ by setting the parameter UseCredentialsManager to $True. o $Credential -eq $Null -and $UseCredentialsManager -eq $Null -> credentials are requested and are not persisted o $Credential -ne $Null -and $UseCredentialsManager -eq $Null -> passed credentials are used and not persisted o $Credential -ne $Null -and $UseCredentialsManager -ne $Null -> passed credentials are used and persisted o $Credential -eq $Null -and $UseCredentialsManager -ne $Null -> Persisted credentials are used .PARAMETER Credential Credential to access Billomat with. I not provided they get requested. .PARAMETER UseCredentialsManager If $True, credentials are read and written using the CredentialsManager. Only Password part, containing the APIKey, is used. #> function Connect-BMT { [CmdletBinding()] Param( [PSCredential]$Credential, [Boolean]$UseCredentialsManager = $False ) #end param # check module prerequisites if($UseCredentialsManager) { $module = Get-Module -ListAvailable -Name "CredentialsManager"; if (!$module) { throw "Module 'CredentialsManager' needed. Please install executing 'Install-Module -Name CredentialsManager' as Administrator."; } } if($UseCredentialsManager -and $Credential -eq $Null) { $Credential = Read-Credential -ListAvailable | Where { $_.Environment -eq "Billomat" } } if(!$Credential) { $Credential = Get-Credential -Message "Please enter API Key for Billomat as password (username can be anything)."; } if($UseCredentialsManager) { Write-Credential "Billomat" -Credential $Credential; } $script:key = $Credential.GetNetworkCredential().password ; } class InvoiceItem { [string]$unit [decimal]$quantity [decimal]$unit_price [string]$title [string]$description } <# .SYNOPSIS Adds an invoice to Billomat. .DESCRIPTION .PARAMETER ClientId Id of client to add the invoice for .PARAMETER Label Optional Label of Invoice .PARAMETER InvoiceItems Items of the Invoice #> function Add-BMTInvoice { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$ClientId, [string]$Label, [InvoiceItem[]]$InvoiceItems ) #end param if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } $uri = New-HttpQueryString "${baseUrl}/invoices" $parameters $bodyAsJson = ConvertTo-JSON( @{ invoice= @{ client_id=$ClientId; label=$Label; } }); $result = $(Invoke-RestMethod -Uri $uri -Method Post -Body $bodyAsJson -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}); foreach($item in $InvoiceItems) { Add-BMTInvoiceItem -InvoiceId $result.invoice.id -Item $item | Out-Null; } return $result.invoice; } <# .SYNOPSIS Adds an item to an invoice. .DESCRIPTION .PARAMETER InvoiceId Id of invoice to add item to .PARAMETER Item Item to be added #> function Add-BMTInvoiceItem { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$InvoiceId, [InvoiceItem]$Item ) #end param if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } $uri = New-HttpQueryString "${baseUrl}/invoice-items" $parameters $bodyAsJson = ConvertTo-JSON( @{ "invoice-item"= @{ invoice_id=$InvoiceId; unit=$Item.unit; unit_price=$Item.unit_price; quantity=$Item.quantity; title=$Item.title; description=$Item.description; } }); $result = $(Invoke-RestMethod -Uri $uri -Method Post -Body $bodyAsJson -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}); return $result."invoice-item" } class CreditNoteItem { [string]$unit [decimal]$quantity [decimal]$unit_price [string]$title [string]$description } <# .SYNOPSIS Adds a credit note to Billomat. .DESCRIPTION .PARAMETER ClientId Id of client to add the credit note for .PARAMETER Label Optional Label of Credit Note .PARAMETER CreditNoteItems Items of the Credit note #> function Add-BMTCreditNote { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$ClientId, [string]$Label, [CreditNoteItem[]]$CreditNoteItems ) #end param if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } $uri = New-HttpQueryString "${baseUrl}/credit-notes" $parameters $bodyAsJson = ConvertTo-JSON( @{ "credit-note"= @{ client_id=$ClientId; label=$Label; } }); $result = $(Invoke-RestMethod -Uri $uri -Method Post -Body $bodyAsJson -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}); foreach($item in $CreditNoteItems) { Add-BMTCreditNoteItem -CreditNoteId $result."credit-note".id -Item $item | Out-Null; } return $result."credit-note"; } <# .SYNOPSIS Adds an item to a credit note. .DESCRIPTION .PARAMETER CreditNoteId Id of credit note to add item to .PARAMETER Item Item to be added #> function Add-BMTCreditNoteItem { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$CreditNoteId, [CreditNoteItem]$Item ) #end param if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } $uri = New-HttpQueryString "${baseUrl}/credit-note-items" $parameters $bodyAsJson = ConvertTo-JSON( @{ "credit-note-item"= @{ credit_note_id=$CreditNoteId; unit=$Item.unit; unit_price=$Item.unit_price; quantity=$Item.quantity; title=$Item.title; description=$Item.description; } }); $result = $(Invoke-RestMethod -Uri $uri -Method Post -Body $bodyAsJson -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}); return $result."credit-note-item" } <# .SYNOPSIS Get Invoice Payments by Invoice Id or from/until .PARAMETER InvoiceId Id of Invoice to filter the payments for. .PARAMETER From Date from which on to receive payments. Only date-part is considered. .PARAMETER To Date until which on to receive payments to. Only date-part is considered. #> function Get-BMTInvoicePayments { [CmdletBinding()] Param( [string]$InvoiceId, [DateTime]$From, [DateTime]$To ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } if($InvoiceId) { $parameters.Add("invoice_id", $InvoiceId) } if($From) { $parameters.Add("from", $From.ToString('yyyy-MM-dd')) } if($To) { $parameters.Add("to", $To.ToString('yyyy-MM-dd')) } $uri = New-HttpQueryString "${baseUrl}/invoice-payments" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; if($response."invoice-payments"."@total" -eq 0) { return @(); } return $response."invoice-payments"."invoice-payment" } <# .SYNOPSIS Get Invoice by ClientId or Status from Billomat .PARAMETER ClientId Id of client to get. .PARAMETER Status Status of invoices to get (DRAFT, OPEN, PAID, OVERDUE, CANCELED). Separate by comma for multiple stati #> function Get-BMTInvoices { [CmdletBinding()] Param( [string]$ClientId, [string]$Status ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } if($ClientNumber) { $parameters.Add("client_id", $ClientId) } if($Status) { $parameters.Add("status", $Status) } $uri = New-HttpQueryString "${baseUrl}/invoices" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; if($response.invoices."@total" -eq 0) { return @(); } return $response.invoices.invoice } <# .SYNOPSIS Get Invoice by InvoiceId .PARAMETER Id Id of invoice to get. #> function Get-BMTInvoice { [CmdletBinding()] Param( [Parameter(Mandatory = $true)][long]$Id ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } $uri = New-HttpQueryString "${baseUrl}/invoices/${Id}" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; return $response.invoice } <# .SYNOPSIS Get Client by Name or ClientNumber from Billomat .DESCRIPTION If no client is found, an empty array is returned. .PARAMETER Name Name of client to get. Returns all clients having the given value in their name. .PARAMETER ClientNumber Number of client to get. #> function Get-BMTClients { [CmdletBinding()] Param( [string]$Name, [string]$ClientNumber ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } if($Name) { $parameters.Add("name", $Name) } if($ClientNumber) { $parameters.Add("client_number", $ClientNumber) } $uri = New-HttpQueryString "${baseUrl}/clients" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; if($response.clients."@total" -eq 0) { return @(); } return $response.clients.client } <# .SYNOPSIS Get Client by Id .DESCRIPTION .PARAMETER Id Id of client to get. #> function Get-BMTClient { [CmdletBinding()] Param( [long]$Id ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } $uri = New-HttpQueryString "${baseUrl}/clients/${Id}" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; return $response.client } <# .SYNOPSIS Get Contacts by client .DESCRIPTION If no contact is found, an empty array is returned. .PARAMETER ClientId Id of client to receive contacts for. #> function Get-BMTContacts { [CmdletBinding()] Param( [int]$ClientId ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; client_id=$ClientId } $uri = New-HttpQueryString "${baseUrl}/contacts" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; if($response.contacts."@total" -eq 0) { return @(); } return $response.contacts.contact } <# .SYNOPSIS Get Contact by Id .DESCRIPTION .PARAMETER Id Id of contact to get. #> function Get-BMTContact { [CmdletBinding()] Param( [int]$Id ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; } $uri = New-HttpQueryString "${baseUrl}/contacts/${Id}" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; return $response.contact } <# .SYNOPSIS Get Offers by offernumber .DESCRIPTION .PARAMETER OfferNumber Number of offer to get. #> function Get-BMTOffers { [CmdletBinding()] Param( [string]$OfferNumber ) if(!$script:key) { Connect-BMT -UseCredentialsManager $True; } $parameters = @{ api_key=$script:key; offer_number=$OfferNumber; } $uri = New-HttpQueryString "${baseUrl}/offers" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json; charset=utf-8" -Headers @{Accept="application/json"}; if($response.offers."@total" -eq 0) { return @(); } return $response.offers.offer } ## Private Functions function New-HttpQueryString { [CmdletBinding()] param ( [Parameter(Mandatory = $true)][String]$Uri, [Parameter(Mandatory = $true)][Hashtable]$QueryParameter ) # Add System.Web Add-Type -AssemblyName System.Web # Create a http name value collection from an empty string $nvCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty) foreach ($key in $QueryParameter.Keys) { $nvCollection.Add($key, $QueryParameter.$key) } # Build the uri $uriRequest = [System.UriBuilder]$uri $uriRequest.Query = $nvCollection.ToString() return $uriRequest.Uri.OriginalString } |