DoubleEntryLedger.Utils.Currency (double_entry_ledger v0.1.0)

View Source

Provides types and helper functions for currency handling within the ledger system.

This module facilitates currency operations such as converting amounts to Money structs and retrieving supported currency atoms.

Summary

Functions

Returns a list of all supported currency atoms.

Converts an amount and a currency to a Money struct, ensuring the amount is positive.

Converts an amount and a currency to a Money struct.

Types

currency_atom()

@type currency_atom() ::
  :NAD
  | :AFN
  | :XAF
  | :ERN
  | :XAG
  | :BOB
  | :HNL
  | :BWP
  | :NPR
  | :STN
  | :USD
  | :PAB
  | :LKR
  | :BDT
  | :SLL
  | :GNF
  | :XSU
  | :MYR
  | :SHP
  | :UYU
  | :CNY
  | :CLF
  | :XBB
  | :DJF
  | :KWD
  | :IQD
  | :XOF
  | :BAM
  | :KYD
  | :NOK
  | :MAD
  | :XUA
  | :USN
  | :GMD
  | :AWG
  | :CLP
  | :LSL
  | :SYP
  | :MXV
  | :INR
  | :TJS
  | :TZS
  | :TRY
  | :LRD
  | :TOP
  | :KZT
  | :RWF
  | :SSP
  | :CDF
  | :EGP
  | :WST
  | :AED
  | :BBD
  | :PHP
  | :AUD
  | :BHD
  | :BND
  | :AZN
  | :CVE
  | :JOD
  | :HUF
  | :PGK
  | :PYG
  | :XPF
  | :LYD
  | :BOV
  | :UGX
  | :NZD
  | :ETB
  | :SBD
  | :ZAR
  | :AMD
  | :BSD
  | :ALL
  | :FJD
  | :UYI
  | :CAD
  | :COP
  | :XAU
  | :MWK
  | :HKD
  | :BZD
  | :AOA
  | :XPT
  | :HTG
  | :BYN
  | :ILS
  | :RON
  | :GBP
  | :PEN
  | :DZD
  | :MRU
  | :VEF
  | :NIO
  | :KPW
  | :RSD
  | :ZMW
  | :DOP
  | :KES
  | :MZN
  | :VES
  | :MOP
  | :MMK
  | :XPD
  | :OMR
  | :THB
  | :VND
  | :ANG
  | :PLN
  | :SCR
  | :BRL
  | :YER
  | :LAK
  | :FKP
  | :LBP
  | :DKK
  | :XBC
  | :XDR
  | :JPY
  | :MNT
  | :SLE
  | :JMD
  | :LTL
  | :GEL
  | :MVR
  | :PKR
  | :XFU
  | :IDR
  | :EUR
  | :GHS
  | :RUB
  | :GYD
  | :HRK
  | :VUV
  | :MRO
  | :CUC
  | :UAH
  | :SOS
  | :BMD
  | :ISK
  | :STD
  | :XBA
  | :KRW
  | :CHF
  | :TMT
  | :SRD
  | :CRC
  | :TWD
  | :COU
  | :MKD
  | :SGD
  | :BYR
  | :QAR
  | :EEK
  | :TND
  | :SAR
  | :LVL
  | :TTD
  | :KGS
  | :SVC
  | :BIF
  | :KMF
  | :GIP
  | :MUR
  | :MXN
  | :XCD
  | :GTQ
  | :XBD
  | :CUP
  | :BGN
  | :XTS
  | :MGA
  | :BTN
  | :SDG
  | :SZL
  | :SEK
  | :IRR
  | :NGN
  | :MDL
  | :UZS
  | :CZK
  | :ZWL
  | :ARS
  | :KHR

Functions

currency_atoms()

@spec currency_atoms() :: [currency_atom()]

Returns a list of all supported currency atoms.

Examples

iex> Currency.currency_atoms() |> Enum.sort() |> Enum.take(3)
[:AED, :AFN, :ALL]

to_abs_money(amount, currency)

@spec to_abs_money(integer(), currency_atom() | binary()) ::
  Money.t() | {:error, String.t()}

Converts an amount and a currency to a Money struct, ensuring the amount is positive.

Parameters

  • amount - The integer amount to convert.
  • currency - The currency atom for the amount.

Examples

iex> Currency.to_abs_money(-100, :USD)
%Money{amount: 100, currency: :USD}

iex> Currency.to_abs_money(100, "USD")
%Money{amount: 100, currency: :USD}

iex> Currency.to_abs_money(100, "XYZ")
{:error, "Invalid currency"}

iex> Currency.to_abs_money("100", 100)
{:error, "Invalid amount or currency"}

Returns

  • A Money.t() struct with an absolute value of the amount.

to_money(amount, currency)

@spec to_money(integer(), currency_atom() | binary()) ::
  Money.t() | {:error, String.t()}

Converts an amount and a currency to a Money struct.

Parameters

  • amount - The integer amount to convert.
  • currency - The currency atom for the amount.

Examples

iex> Currency.to_money(-100, :USD)
%Money{amount: -100, currency: :USD}

iex> Currency.to_money(100, "USD")
%Money{amount: 100, currency: :USD}

iex> Currency.to_money(100, "XYZ")
{:error, "Invalid currency"}

iex> Currency.to_money("100", 100)
{:error, "Invalid amount or currency"}

Returns

  • A Money.t() struct with an absolute value of the amount.