show original count, current value, percentage remaining and shot history for ammo groups

This commit is contained in:
2022-02-19 00:05:22 -05:00
parent 91ff0c14e4
commit dba53106fb
7 changed files with 168 additions and 31 deletions

View File

@ -162,10 +162,12 @@ defmodule Cannery.Ammo do
@spec list_ammo_groups_for_type(AmmoType.t(), User.t()) :: [AmmoGroup.t()]
def list_ammo_groups_for_type(%AmmoType{id: ammo_type_id, user_id: user_id}, %User{id: user_id}) do
Repo.all(
from am in AmmoGroup,
where: am.ammo_type_id == ^ammo_type_id,
where: am.user_id == ^user_id,
order_by: am.id
from ag in AmmoGroup,
left_join: sg in assoc(ag, :shot_groups),
where: ag.ammo_type_id == ^ammo_type_id,
where: ag.user_id == ^user_id,
preload: [shot_groups: sg],
order_by: ag.id
)
end
@ -182,12 +184,18 @@ defmodule Cannery.Ammo do
@spec list_ammo_groups(User.t(), include_empty :: boolean()) :: [AmmoGroup.t()]
def list_ammo_groups(%User{id: user_id}, include_empty \\ false) do
if include_empty do
from am in AmmoGroup, where: am.user_id == ^user_id, order_by: am.id
from ag in AmmoGroup,
left_join: sg in assoc(ag, :shot_groups),
where: ag.user_id == ^user_id,
preload: [shot_groups: sg],
order_by: ag.id
else
from am in AmmoGroup,
where: am.user_id == ^user_id,
where: not (am.count == 0),
order_by: am.id
from ag in AmmoGroup,
left_join: sg in assoc(ag, :shot_groups),
where: ag.user_id == ^user_id,
where: not (ag.count == 0),
preload: [shot_groups: sg],
order_by: ag.id
end
|> Repo.all()
end
@ -204,10 +212,12 @@ defmodule Cannery.Ammo do
@spec list_staged_ammo_groups(User.t()) :: [AmmoGroup.t()]
def list_staged_ammo_groups(%User{id: user_id}) do
Repo.all(
from am in AmmoGroup,
where: am.user_id == ^user_id,
where: am.staged == true,
order_by: am.id
from ag in AmmoGroup,
left_join: sg in assoc(ag, :shot_groups),
where: ag.user_id == ^user_id,
where: ag.staged == true,
preload: [shot_groups: sg],
order_by: ag.id
)
end
@ -226,8 +236,42 @@ defmodule Cannery.Ammo do
"""
@spec get_ammo_group!(AmmoGroup.id(), User.t()) :: AmmoGroup.t()
def get_ammo_group!(id, %User{id: user_id}),
do: Repo.one!(from am in AmmoGroup, where: am.id == ^id and am.user_id == ^user_id)
def get_ammo_group!(id, %User{id: user_id}) do
Repo.one!(
from ag in AmmoGroup,
left_join: sg in assoc(ag, :shot_groups),
where: ag.id == ^id,
where: ag.user_id == ^user_id,
preload: [shot_groups: sg]
)
end
@doc """
Returns the number of shot rounds for an ammo group
"""
@spec get_used_count(AmmoGroup.t()) :: non_neg_integer()
def get_used_count(%AmmoGroup{} = ammo_group) do
ammo_group
|> Repo.preload(:shot_groups)
|> Map.get(:shot_groups)
|> Enum.map(fn %{count: count} -> count end)
|> Enum.sum()
end
@doc """
Calculates the percentage remaining of an ammo group out of 100
"""
@spec get_percentage_remaining(AmmoGroup.t()) :: non_neg_integer()
def get_percentage_remaining(%AmmoGroup{count: 0}), do: 0
def get_percentage_remaining(%AmmoGroup{count: count} = ammo_group) do
ammo_group = ammo_group |> Repo.preload(:shot_groups)
shot_group_sum =
ammo_group.shot_groups |> Enum.map(fn %{count: count} -> count end) |> Enum.sum()
round(count / (count + shot_group_sum) * 100)
end
@doc """
Creates a ammo_group.

View File

@ -33,7 +33,7 @@
<%= gettext("Price paid") %>
</th>
<th class="p-2">
<%= gettext("Notes") %>
<%= gettext("% left") %>
</th>
<th class="p-2">
<%= gettext("Range") %>
@ -68,7 +68,7 @@
</td>
<td class="p-2">
<%= ammo_group.notes %>
<%= "#{ammo_group |> Ammo.get_percentage_remaining()}%" %>
</td>
<td class="p-2">

View File

@ -9,6 +9,16 @@
<%= @ammo_group.count %>
</span>
<span class="rounded-lg title text-lg">
<%= gettext("Original count:") %>
<%= @ammo_group.count + Ammo.get_used_count(@ammo_group) %>
</span>
<span class="rounded-lg title text-lg">
<%= gettext("Percentage left:") %>
<%= "#{@ammo_group |> Ammo.get_percentage_remaining()}%" %>
</span>
<%= if @ammo_group.notes do %>
<span class="rounded-lg title text-lg">
<%= gettext("Notes:") %>
@ -18,11 +28,20 @@
<%= if @ammo_group.price_paid do %>
<span class="rounded-lg title text-lg">
<%= gettext("Price paid:") %>
<%= gettext("Original cost:") %>
<%= gettext("$%{amount}",
amount: @ammo_group.price_paid |> :erlang.float_to_binary(decimals: 2)
) %>
</span>
<span class="rounded-lg title text-lg">
<%= gettext("Current value:") %>
<%= gettext("$%{amount}",
amount:
(@ammo_group.price_paid * Ammo.get_percentage_remaining(@ammo_group) / 100)
|> :erlang.float_to_binary(decimals: 2)
) %>
</span>
<% end %>
</div>
@ -84,6 +103,47 @@
<%= gettext("This ammo group is not in a container") %>
<% end %>
</div>
<%= unless @ammo_group.shot_groups |> Enum.empty?() do %>
<hr class="mb-4 w-full" />
<h1 class="mb-4 px-4 py-2 text-center rounded-lg title text-xl">
<%= gettext("Rounds used") %>
</h1>
<div class="w-full overflow-x-auto border border-gray-600 rounded-lg shadow-lg bg-black">
<table class="min-w-full table-auto text-center bg-white">
<thead class="border-b border-primary-600">
<tr>
<th class="p-2">
<%= gettext("Rounds shot") %>
</th>
<th class="p-2">
<%= gettext("Notes") %>
</th>
<th class="p-2">
<%= gettext("Date") %>
</th>
</tr>
</thead>
<tbody id="shot_groups">
<%= for shot_group <- @ammo_group.shot_groups do %>
<tr id={"shot_group-#{shot_group.id}"}>
<td class="p-2">
<%= shot_group.count %>
</td>
<td class="p-2">
<%= shot_group.notes %>
</td>
<td class="p-2">
<%= shot_group.date |> display_date() %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% end %>
</div>
<%= case @live_action do %>