Ash FrameworkAF
Ash Frameworkโ€ข8mo agoโ€ข
12 replies
Failz

ash_double_entry different currency

Working with USD and creating transfers from AshPhoenix.Form.submit() works as expected. I added additional accounts with a different currency, EUR, and this fails with:

* ** (ArgumentError) Cannot subtract two monies with different currencies. Received :EUR and :USD.

which makes sense, but where did the :USD come from if both accounts have :EUR currencies?

I added some IO.inspects to AshDoubleEntry.Transfer.Changes.VerifyTransfer

and the changeset comes in with :USD

changeset: #Ash.Changeset<
  domain: App.Ledger,
  action_type: :create,
  action: :create,
  attributes: %{
    id: "01JW8T49T67BCRY1PNFVMJM4Z7",
    timestamp: ~U[2025-05-27 12:05:29.031480Z],
    category: "uncategorized",
    amount: Money.new(:USD, "4"),
    note: nil,
    updated_at: ~U[2025-05-27 12:05:29.031513Z],
    inserted_at: ~U[2025-05-27 12:05:29.031513Z],
    to_account_id: "0197119e-1d53-74b5-a958-8847d6b412e4",
    from_account_id: "0197119d-e91b-7566-ab42-3b626b5fdd6e",
    merchant: "unknown"
  }
>


Creating a transfer with :EUR works from iex:

App.Ledger.Transfer
  |> Ash.Changeset.for_create(:create, %{
    amount: Money.new!(:EUR, 20),
    from_account_id: ac1.id,
    to_account_id: ac2.id})
  |> Ash.create!()


So where is an appropriate place to take our form_params.amount and create an appropriate instance of Money with the correct currency? Would it be in the App.Ledger.Transfer.create code interface with a custom_input or somewhere else?

form_params: %{
  "amount" => "4",
  "category" => "uncategorized",
  "from_account_id" => "0197119d-e91b-7566-ab42-3b626b5fdd6e",
  "merchant" => "unknown",
  "note" => "",
  "to_account_id" => "0197119e-1d53-74b5-a958-8847d6b412e4"
}
Was this page helpful?