Packages
hl7v2
2.7.1
3.10.1
3.9.0
3.8.0
3.7.0
3.6.0
3.5.0
3.4.0
3.3.6
3.3.5
3.3.4
3.3.3
3.3.2
3.3.1
3.3.0
3.2.0
3.1.1
3.1.0
3.0.2
3.0.1
3.0.0
2.11.0
2.10.0
2.9.1
2.9.0
2.8.2
2.8.1
2.8.0
2.7.1
2.7.0
2.6.0
2.5.0
2.4.0
2.3.0
2.2.0
2.1.3
2.1.2
2.1.1
2.1.0
1.4.6
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.0
1.2.0
1.1.0
1.0.0
0.6.0
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.1.0
Pure Elixir HL7 v2.x toolkit — schema-driven parsing, typed segments, message builder, MLLP transport
Current section
Files
Jump to
Current section
Files
lib/hl7v2/type/cp.ex
defmodule HL7v2.Type.CP do
@moduledoc """
Composite Price (CP) -- HL7v2 composite data type.
Used for price information with optional range and type qualifiers.
6 components:
1. Price (MO) -- sub-components delimited by `&` (quantity & denomination)
2. Price Type (ID) -- Table 0205
3. From Value (NM)
4. To Value (NM)
5. Range Units (CE) -- sub-components delimited by `&`
6. Range Type (ID) -- Table 0298
"""
@behaviour HL7v2.Type
alias HL7v2.Type
alias HL7v2.Type.{MO, CE}
defstruct [:price, :price_type, :from_value, :to_value, :range_units, :range_type]
@type t :: %__MODULE__{
price: MO.t() | nil,
price_type: binary() | nil,
from_value: binary() | nil,
to_value: binary() | nil,
range_units: CE.t() | nil,
range_type: binary() | nil
}
@doc """
Parses a CP from a list of components.
## Examples
iex> HL7v2.Type.CP.parse(["100.00&USD", "UP"])
%HL7v2.Type.CP{price: %HL7v2.Type.MO{quantity: "100.00", denomination: "USD"}, price_type: "UP"}
iex> HL7v2.Type.CP.parse(["50"])
%HL7v2.Type.CP{price: %HL7v2.Type.MO{quantity: "50"}}
iex> HL7v2.Type.CP.parse([])
%HL7v2.Type.CP{}
"""
@spec parse(list()) :: t()
def parse(components) when is_list(components) do
%__MODULE__{
price: Type.parse_sub(MO, Type.get_component(components, 0)),
price_type: Type.get_component(components, 1),
from_value: Type.get_component(components, 2),
to_value: Type.get_component(components, 3),
range_units: Type.parse_sub(CE, Type.get_component(components, 4)),
range_type: Type.get_component(components, 5)
}
end
@doc """
Encodes a CP to a list of component strings.
## Examples
iex> HL7v2.Type.CP.encode(%HL7v2.Type.CP{price: %HL7v2.Type.MO{quantity: "100.00", denomination: "USD"}, price_type: "UP"})
["100.00&USD", "UP"]
iex> HL7v2.Type.CP.encode(%HL7v2.Type.CP{price: %HL7v2.Type.MO{quantity: "50"}})
["50"]
iex> HL7v2.Type.CP.encode(nil)
[]
"""
@spec encode(t() | nil) :: list()
def encode(nil), do: []
def encode(%__MODULE__{} = cp) do
[
Type.encode_sub(MO, cp.price),
cp.price_type || "",
cp.from_value || "",
cp.to_value || "",
Type.encode_sub(CE, cp.range_units),
cp.range_type || ""
]
|> Type.trim_trailing()
end
end