Created
October 12, 2021 13:12
-
-
Save dhmacher/dcfacd686d1edc6a86c696c48e07c9fe to your computer and use it in GitHub Desktop.
Generates a SIE accounting file from WooCommerce sales orders.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Provided as-is, without any warranty, implied or express. | |
# General: | |
$file = "Precon-" + (Get-Date).toString("yyyyMMdd-HHmmss") + ".se" | |
$baseUri = "https://example.com/wp-json/wc/v3/" | |
# WooCommerce authentication: | |
$key = "cs_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | |
$secret = "cs_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | |
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $key, $secret))) | |
# Stripe fees: | |
$feeFixed=1.8 | |
$feePercent=0.014 | |
$dateFormat="yyyyMMdd" | |
# Accounting settings: | |
$voucherSeries = "WC" | |
$costCenter = "XXX" | |
$companyName = "Company name goes here" | |
# Chart of accounts | |
$acctPrepaid = "2973" # Prepaid pre-conference revenues | |
$acctVAT = "2610" # Outgoing VAT | |
#$acctStripe = "6571" # Transaction fees, Stripe | |
$acctStripe = "2974" # Accrued costs, Stripe fees (invoiced by Stripe next month) | |
$acctSales = "3002" # Pre-conference sales | |
$acctCorr = "3790" # Correction, sales | |
#------------------------------------------------------------------------------ | |
# Get all outstanding orders (processing, not yet completed) | |
$orders = Invoke-RestMethod ` | |
-Method "GET" ` | |
-Uri ($baseUri + "orders?status=processing") ` | |
-Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} | |
# SIE header: | |
$sie = "#FLAGGA 0`n" + ` | |
"`#FORMAT PC8`n" + ` | |
"`#SIETYP 4`n" + ` | |
"`#PROGRAM `"WooCommerce`" "+$orders[0].version+"`n" + ` | |
"`#GEN " + ((Get-Date).tostring($dateFormat)) + "`n" + ` | |
"`#FNAMN `"" + $companyName + "`"`n" + ` | |
"`#KPTYP EUBAS97`n" | |
# Create the file: | |
# Note: SIE is kind of picky about the 1980s file encoding | |
$sie | Out-File -Encoding 437 -FilePath $file | |
# We only want to export a single year for each file, so as to not confuse the receiving system! | |
$year = $orders[0].date_paid.Year | |
"Processing transactions paid in $year." | |
# Loop through all of the orders, checking that they are in the correct year: | |
foreach ($order in $orders) { | |
if ($order.date_paid.Year -eq $year) { | |
$sie = "" | |
# Customer name: use company if there is one, otherwise first and last name: | |
$customer = "" | |
if ($order.billing.company) { | |
$customer = $order.billing.company | |
} else { | |
$customer = $order.billing.first_name + " " + $order.billing.last_name | |
} | |
# Voucher header: | |
$sie = $sie + ` | |
"#VER " + $voucherSeries + " " + $order.number + " " + ` | |
($order.date_paid.tostring($dateFormat)) + " " + ` | |
"`"" + $order.line_items[0].name + ": " + $customer.replace("`"", "") + "`"`n" + ` | |
"`{`n" | |
# Line items: | |
foreach ($item in $order.line_items) { | |
# Sales account: | |
$sie = $sie + "`#TRANS " + $acctSales + " {1 `""+$costCenter+"`"} " + (0-$item.subtotal).toString("0.00") + " `"`" `"" + $item.name + "`"`n" | |
# Periodization: | |
$sie = $sie + "`#TRANS " + $acctPrepaid + " {1 `""+$costCenter+"`"} " + (0-$item.subtotal).toString("0.00") + " `"`" `"`"`n" | |
$sie = $sie + "`#TRANS " + $acctCorr + " {1 `""+$costCenter+"`"} " + ($item.subtotal -as [double]).toString("0.00") + " `"`" `"`"`n" | |
} | |
# VAT: | |
foreach ($tax in $order.tax_lines) { | |
$sie = $sie + "`#TRANS " + $acctVAT + " {1 `""+$costCenter+"`"} " + (0-$tax.tax_total).toString("0.00") + " `"`" `"`"`n" | |
} | |
# Stripe fee: | |
$fee = ($order.total -as [double]) * $feePercent + $feeFixed | |
$sie = $sie + "`#TRANS " + $acctStripe + " {1 `""+$costCenter+"`"} " + $fee.toString("0.00") + " `"`" `"`"`n" | |
# Amount receivable: | |
$total = ($order.total -as [double]) - $fee | |
$sie = $sie + "`#TRANS 1942 {1 `""+$costCenter+"`"} " + $total.toString("0.00") + " `"`" `"`"`n" | |
$sie = $sie + "`}" | |
$res = Invoke-RestMethod ` | |
-Method "PUT" ` | |
-Uri ($order._links.self[0].href) ` | |
-Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} ` | |
-ContentType "application/json" ` | |
-Body (@{ status="completed" } | ConvertTo-Json) | |
"Order " + $res.id + ": " + $res.status | |
if ($res.status -eq "completed") { | |
$sie | Out-File -Encoding 437 -FilePath $file -Append | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment