ConvertTo-Invoice.ps1
<#
.SYNOPSIS Creates a Invoice containing data created by createSupportReport.ps1 .DESCRIPTION Uses the Billomat PS-Module to create the invoice per Closed Ticket a invoice position is created. .PARAMETER raw The Json containing the SupportReport .PARAMETER label Label of Invoice. .PARAMETER customerNumber Number of Customer to invoice to. .EXAMPLE Creates a Billomat Invoice based on a json report .\createSupportReport.ps1 -tcCustomerNames "Opticon" -to 2020-01-31 | .\ConvertTo-Invoice.ps1 -label "Support Jan 2020" -customerNumber KD61 #> [CmdletBinding(SupportsShouldProcess=$true)] param( [Parameter(ValueFromPipeline)] [string] $raw, [string] $label, [string] $customerNumber ) # Fetch customer to Bill from Billomat $client = Get-BMTClients -ClientNumber $customerNumber if(!$client) { throw "No customer having customerNumber '${customerNumber}' found."; } # Add the Invoice $invoice = Add-BMTInvoice -ClientId $client.id -Label $label # fetch report data $data = $raw | ConvertFrom-Json $customers = $data | Sort-Object -Property @{Expression = "isClosed"; Descending = $True}, @{Expression = "type"; Descending = $False}, @{Expression = "categoryNumber"; Descending = $False} | Group-Object -Property customerCode foreach($customer in $customers) { $totalHours = $($customer.Group | Measure-Object -Property durationBilled -Sum).Sum $totalAmount = $($customer.Group | Measure-Object -Property amountBilled -Sum).Sum $line = "Details zu $($customer.Name) - $($customer.Count) - $($totalHours.ToString('0.##'))h"; if($showBilling) { $line += " - $($totalAmount.ToString('C'))"; } Write-Output $line; $items = $customer.Group | Group-Object -Property subType foreach($subType in $items) { $totalHours = $($subType.Group | Measure-Object -Property durationBilled -Sum).Sum $totalAmount = $($subType.Group | Measure-Object -Property amountBilled -Sum).Sum $line = "### $($subType.Count) $($subType.Name) - $($totalHours.ToString('0.##'))h"; if($showBilling) { $line += " - $($totalAmount.ToString('C'))"; } $line += " ###" Write-Output $line foreach($item in $subType.Group) { $title = "" $duration = $Null; $start = [datetime]$item.beginDate; $end = [datetime]$item.endDate; if($start.Date -eq $end.Date) { $duration = "am $($start.ToString('dd.MM.yyyy'))"; } else { $duration = "im Zeitraum $($start.ToString('dd.MM.yyyy'))-$($end.ToString('dd.MM.yyyy'))"; } if($item.isClosed) { if($item.durationBilled -gt 0) { $unbilledInfo = "" if($item.durationUnbilled -gt 0) { $unbilledInfo = "(+$($item.durationUnbilled)h nicht verrechnet) " } $title = "$($item.categoryName) $($item.taskCode) ($($item.taskDescription)) ${unbilledInfo}"; } else { $title = "$($item.categoryName) $($item.taskCode) ($($item.taskDescription)) (nicht verrechnet)"; } $description = "" foreach($booking in $item.bookings) { $description += "$($booking.Description) ($($booking.beginTime) - $($booking.endTime), $($booking.user))`n" } $invoiceItem = @{ unit="Stunden"; quantity=$item.durationBilled; unit_price=$item.hourlyRate; title=$title; description=$description } Add-BMTInvoiceItem -InvoiceId $invoice.id -Item $invoiceItem | Out-Null } } } } |