diff --git a/lib/derivantes_web/live/derivantes/derivantes_live.ex b/lib/derivantes_web/live/derivantes/derivantes_live.ex
index c4087cc..b1dcf8e 100755
--- a/lib/derivantes_web/live/derivantes/derivantes_live.ex
+++ b/lib/derivantes_web/live/derivantes/derivantes_live.ex
@@ -1,109 +1,163 @@
-defmodule DerivantesWeb.DerivantesLive do
- use DerivantesWeb, :live_view
- use Phoenix.Component
- import Ecto.Query
-
- def mount(_params, _session, socket) do
- {:ok,
- socket
- |> assign(studies: get_studies(socket))
- }
- end
-
- def render(assigns) do
- ~H"""
-
- <%= if length(@studies) > 0 do %>
-
-
Bienvenido, <%= @current_user.email %>
-
Para ver un estudio haga click en el icono de Informe o de Imágenes.
-
-
- <%= for study <- @studies do %>
-
-
-
Fecha y hora: <%= format_date(study.fecha) %> - <%= format_hour(study.hora) %>
-
DNI: <%= study.dni %>
-
Accession N° <%= study.accession %>
-
Matricula derivante <%= @current_user.matricula %>
-
-
-
- <%= case {study.modality, study.esteco, study.estrx} do %>
- <% {"US", "F", _} -> %>
-
-
-
- <% {modality, _, "E"} when modality != "US" -> %>
-
-
-
- <% _ -> %>
-
- <% end %>
- <%= if study.modality != "US" do %>
-
-
-
- <% else %>
-
- <% end %>
-
-
- <% end %>
- <% else %>
-
-
- No hay estudios para mostrar
-
-
- <% end %>
-
-
- """
- end
-
- def format_date(date_string) when is_binary(date_string) do
- anio = String.slice(date_string, 0..3)
- mes = String.slice(date_string, 4..5)
- dia = String.slice(date_string, 6..7)
- "#{dia}/#{mes}/#{anio}"
- end
- def format_hour(hour_string) when is_binary(hour_string) do
- hora = String.slice(hour_string, 0..1)
- minutos = String.slice(hour_string, 2..3)
- segundos = String.slice(hour_string, 4..6)
- "#{hora}:#{minutos}:#{segundos}"
- end
-
- def get_studies(socket) do
- query = from vl in "V_LISTAINGRESOS",
- select: %{
- idstudy: field(vl, :ID),
- estrx: field(vl, :ESTRX),
- esteco: field(vl, :ESTECO),
-# desc: field(vl, :PROD_DESC),
- hora: field(vl, :START_TIME),
- fecha: field(vl, :START_DATE),
- modality: field(vl, :MODALITY),
- dni: field(vl, :TIPO_DOCUMENTO),
- lastname: field(vl, :PATIENTLASTNAME),
- accession: field(vl, :ACCESSIONNUMBER),
- patientname: field(vl, :PATIENTFIRSTNAME)
- },
- where: field(vl, :MATDER) == ^socket.assigns.current_user.matricula,
- order_by: [desc: field(vl, :START_DATE)],
- limit: 20
- estudios = Derivantes.TdsRepo.all(query)
- estudios
- end
-
-end
+defmodule DerivantesWeb.DerivantesLive do
+ use DerivantesWeb, :live_view
+ use Phoenix.Component
+ import Ecto.Query
+
+ def mount(_params, _session, socket) do
+ filters = to_form(%{}, as: "filters")
+ {:ok,
+ socket
+ |> assign(studies: get_studies(socket, %{}))
+ |> assign(filters: filters)
+ }
+ end
+
+ def render(assigns) do
+ ~H"""
+
+
+
+
Bienvenido, <%= @current_user.email %>
+
Para ver un estudio haga click en el icono de Informe o de Imágenes.
+
+
+ <.simple_form
+ for={@filters}
+ id="filters"
+ phx-update="ignore"
+ phx-submit="filter_study"
+ >
+ <.input field={@filters[:dni]} type="text"/>
+ <.input field={@filters[:accession]} type="text"/>
+ <:actions>
+ <.button phx-disable-with="Buscando..." class="w-full">Buscar
+
+
+
+ <%= if length(@studies) > 0 do %>
+
+ <%= for study <- @studies do %>
+
+
+
Fecha y hora: <%= format_date(study.fecha) %> - <%= format_hour(study.hora) %>
+
DNI: <%= study.dni %>
+
Accession N° <%= study.accession %>
+
Matricula derivante <%= @current_user.matricula %>
+
+
+
+ <%= case {study.modality, study.esteco, study.estrx} do %>
+ <% {"US", "F", _} -> %>
+
+
+
+ <% {modality, _, "E"} when modality != "US" -> %>
+
+
+
+ <% _ -> %>
+
+ <% end %>
+ <%= if study.modality != "US" do %>
+
+
+
+ <% else %>
+
+ <% end %>
+
+
+ <% end %>
+ <% else %>
+
+
+ No hay estudios para mostrar
+
+
+ <% end %>
+
+
+ """
+ end
+
+ def format_date(date_string) when is_binary(date_string) do
+ anio = String.slice(date_string, 0..3)
+ mes = String.slice(date_string, 4..5)
+ dia = String.slice(date_string, 6..7)
+ "#{dia}/#{mes}/#{anio}"
+ end
+ def format_hour(hour_string) when is_binary(hour_string) do
+ hora = String.slice(hour_string, 0..1)
+ minutos = String.slice(hour_string, 2..3)
+ segundos = String.slice(hour_string, 4..6)
+ "#{hora}:#{minutos}:#{segundos}"
+ end
+
+ def get_studies(socket, filter) do
+
+ keys = Map.keys(filter)
+ IO.inspect(keys)
+ mat_filter = dynamic([vl], vl.matder == ^socket.assigns.current_user.matricula)
+ filter_conditions =
+ Enum.reduce(keys, mat_filter, fn v, filter_conditions ->
+ case v do
+ "dni" ->
+ if(filter["dni"] != "") do
+ dynamic([vl], vl.tipo_documento == ^filter["dni"] and ^filter_conditions)
+ else
+ filter_conditions
+ end
+ "accession" ->
+ if(filter["accession"] != "") do
+ dynamic([vl], vl.accessionnumber == ^filter["accession"] and ^filter_conditions)
+ else
+ filter_conditions
+ end
+ _ ->
+ filter_conditions
+ end
+ end)
+
+
+ query = from vl in "V_LISTAINGRESOS",
+ select: %{
+ idstudy: field(vl, :ID),
+ estrx: field(vl, :ESTRX),
+ esteco: field(vl, :ESTECO),
+# desc: field(vl, :PROD_DESC),
+ hora: field(vl, :START_TIME),
+ fecha: field(vl, :START_DATE),
+ modality: field(vl, :MODALITY),
+ dni: field(vl, :TIPO_DOCUMENTO),
+ lastname: field(vl, :PATIENTLASTNAME),
+ accession: field(vl, :ACCESSIONNUMBER),
+ patientname: field(vl, :PATIENTFIRSTNAME)
+ },
+ where: ^filter_conditions,
+ order_by: [desc: field(vl, :START_DATE)],
+ limit: 20
+ estudios = Derivantes.TdsRepo.all(query)
+ estudios
+ end
+
+ def handle_event(event, params, socket) do
+ case event do
+ "filter_study" ->
+ studies = get_studies(socket, params["filters"])
+ {:noreply,
+ socket
+ |> assign(studies: studies)
+ }
+ end
+ end
+end