Wire skyai-finance push: cloud invoices flow into Sky AI's books
After InvoiceIngestWorker writes cost lines and matches resources, it
pushes a normalized invoice payload to skyai-finance-svc's new endpoint
POST /api/v1/integrations/cloud/import-invoice. Idempotent — finance
dedups on (tenant_id, source, external_id), so re-runs return "updated"
not duplicate rows.
ArcadiaCloud.Integrations.SkyaiFinance is the HTTP client. Auth is a
short-lived JWT minted via Guardian (shared secret per memory) with
identity {tenant_id: "platform-admin", roles: ["admin"]} — the role
satisfies finance's RequireWriteRole plug. Identity + base URL are
configurable; SKYAI_FINANCE_URL env var can override the default
http://localhost:4010 for arcadia-dev / prod.
GST line detection: lines whose description contains "gst" or "tax"
get summed into amount_tax_minor; everything else into amount_net_minor;
sum stays gross. Phase 1 enough — proper tax handling lands when
real per-tenant invoices flow.
cloud_invoices gains pushed_to_finance_at + finance_invoice_id so we
don't re-push uselessly (Billing.mark_invoice_pushed/2 records both).
A missing finance config (no :skyai_finance app env) makes the push a
silent skip rather than a worker failure — environments without finance
configured still get a working ingest.
Live verified end-to-end against both services:
- April 2026 DO invoice (33 lines, $86.92) lands in finance as a row
with gross=$86.92, tax=$7.90, source=digitalocean, tenant=platform-admin
- DigitalOcean vendor auto-created (category=cloud) under platform-admin
- Re-running the worker returns action: "updated" not "created";
finance still has exactly 1 row for the invoice
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -29,12 +29,84 @@ defmodule ArcadiaCloud.Sync.InvoiceIngestWorker do
|
||||
|
||||
{:ok, _} = Billing.replace_cost_lines(invoice, lines)
|
||||
matched = Billing.match_cost_lines_to_resources(invoice)
|
||||
{:ok, _} = Billing.mark_invoice_ingested(invoice)
|
||||
{:ok, invoice} = Billing.mark_invoice_ingested(invoice)
|
||||
|
||||
{:ok, %{lines: length(lines), matched: matched}}
|
||||
push_result = push_to_finance(invoice, lines)
|
||||
|
||||
{:ok, %{lines: length(lines), matched: matched, finance_push: push_result}}
|
||||
end
|
||||
end
|
||||
|
||||
# ---- push to skyai-finance ------------------------------------------------
|
||||
|
||||
defp push_to_finance(%CloudInvoice{} = invoice, lines) do
|
||||
payload = build_finance_payload(invoice, lines)
|
||||
|
||||
case ArcadiaCloud.Integrations.SkyaiFinance.push_invoice(payload) do
|
||||
{:ok, %{"invoice_id" => fid} = body} ->
|
||||
Billing.mark_invoice_pushed(invoice, fid)
|
||||
{:ok, body["action"] || "ok"}
|
||||
|
||||
{:error, {:skyai_finance_not_configured, _}} ->
|
||||
# Push is optional — skipped in environments without finance configured.
|
||||
:skipped
|
||||
|
||||
{:error, reason} ->
|
||||
# Log and let Oban retry the whole worker; nothing destructive happened.
|
||||
require Logger
|
||||
Logger.warning("skyai-finance push failed for invoice #{invoice.id}: #{inspect(reason)}")
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp build_finance_payload(%CloudInvoice{} = invoice, lines) do
|
||||
gst_minor = total_tax_minor(lines)
|
||||
gross_minor = invoice.amount_cents || total_amount_minor(lines)
|
||||
net_minor = max(gross_minor - gst_minor, 0)
|
||||
|
||||
%{
|
||||
"invoice" => %{
|
||||
"source" => invoice.provider,
|
||||
"external_id" => invoice.provider_invoice_id,
|
||||
"date" => Date.to_iso8601(invoice.invoice_period),
|
||||
"period_start" => Date.to_iso8601(invoice.invoice_period),
|
||||
"period_end" => Date.to_iso8601(end_of_month(invoice.invoice_period)),
|
||||
"currency" => "USD",
|
||||
"amount_gross_minor" => gross_minor,
|
||||
"amount_tax_minor" => gst_minor,
|
||||
"amount_net_minor" => net_minor,
|
||||
"gst_inclusive" => true,
|
||||
"notes" =>
|
||||
"Auto-imported from arcadia-cloud. #{length(lines)} line items; #{Enum.count(lines, & &1[:kind])} kind-classified."
|
||||
},
|
||||
"vendor" => %{
|
||||
"name" => "DigitalOcean",
|
||||
"category" => "cloud",
|
||||
"default_currency" => "USD"
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
defp total_amount_minor(lines) do
|
||||
Enum.reduce(lines, 0, fn l, acc -> acc + (l[:amount_cents] || 0) end)
|
||||
end
|
||||
|
||||
defp total_tax_minor(lines) do
|
||||
Enum.reduce(lines, 0, fn l, acc ->
|
||||
desc = (l[:description] || "") |> String.downcase()
|
||||
|
||||
if String.contains?(desc, "gst") or String.contains?(desc, "tax") do
|
||||
acc + (l[:amount_cents] || 0)
|
||||
else
|
||||
acc
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp end_of_month(%Date{} = d) do
|
||||
d |> Date.end_of_month()
|
||||
end
|
||||
|
||||
# ---- CSV parsing ----------------------------------------------------------
|
||||
|
||||
# DO invoice CSV columns (as of v2 API):
|
||||
|
||||
Reference in New Issue
Block a user