Files
arcadia-cloud/lib/arcadia_cloud/billing/cloud_invoice.ex
Giuliano Silvestro 4f2e52af01 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>
2026-05-19 22:35:52 +10:00

35 lines
1.0 KiB
Elixir

defmodule ArcadiaCloud.Billing.CloudInvoice do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "cloud_invoices" do
field :provider, :string
field :provider_invoice_id, :string
field :invoice_period, :date
field :amount_cents, :integer
field :status, :string, default: "open"
field :issued_at, :utc_datetime
field :csv_fetched_at, :utc_datetime
field :lines_ingested_at, :utc_datetime
field :pushed_to_finance_at, :utc_datetime
field :finance_invoice_id, :string
field :raw, :map
timestamps(type: :utc_datetime)
end
@required ~w(provider provider_invoice_id invoice_period)a
@optional ~w(amount_cents status issued_at csv_fetched_at lines_ingested_at
pushed_to_finance_at finance_invoice_id raw)a
def changeset(invoice, attrs) do
invoice
|> cast(attrs, @required ++ @optional)
|> validate_required(@required)
|> unique_constraint([:provider, :provider_invoice_id])
end
end