2021-09-02 23:31:14 -04:00
|
|
|
defmodule Cannery.Ammo.AmmoGroup do
|
2022-01-22 21:40:29 -05:00
|
|
|
@moduledoc """
|
|
|
|
A group of a certain ammunition type.
|
|
|
|
|
|
|
|
Can be placed in a container, and contains auxiliary information such as the
|
|
|
|
amount paid for that ammunition, or what condition it is in
|
|
|
|
"""
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
use Ecto.Schema
|
2022-07-04 20:06:41 -04:00
|
|
|
import CanneryWeb.Gettext
|
2021-09-02 23:31:14 -04:00
|
|
|
import Ecto.Changeset
|
2022-01-31 20:08:01 -05:00
|
|
|
alias Cannery.Ammo.{AmmoGroup, AmmoType}
|
2022-11-10 21:45:50 -05:00
|
|
|
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Containers, Containers.Container}
|
2022-01-31 20:08:01 -05:00
|
|
|
alias Ecto.{Changeset, UUID}
|
2021-09-02 23:31:14 -04:00
|
|
|
|
2022-11-09 23:33:50 -05:00
|
|
|
@derive {Jason.Encoder,
|
|
|
|
only: [
|
|
|
|
:id,
|
|
|
|
:count,
|
|
|
|
:notes,
|
|
|
|
:price_paid,
|
|
|
|
:staged,
|
|
|
|
:ammo_type_id,
|
|
|
|
:container_id
|
|
|
|
]}
|
2021-09-02 23:31:14 -04:00
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
|
|
|
@foreign_key_type :binary_id
|
|
|
|
schema "ammo_groups" do
|
|
|
|
field :count, :integer
|
|
|
|
field :notes, :string
|
|
|
|
field :price_paid, :float
|
2022-02-15 17:33:45 -05:00
|
|
|
field :staged, :boolean, default: false
|
2022-11-19 14:52:01 -05:00
|
|
|
field :purchased_on, :date
|
2021-09-12 18:54:53 -04:00
|
|
|
|
2022-01-31 20:08:01 -05:00
|
|
|
belongs_to :ammo_type, AmmoType
|
|
|
|
belongs_to :container, Container
|
|
|
|
belongs_to :user, User
|
2021-09-02 23:31:14 -04:00
|
|
|
|
2022-02-15 21:56:01 -05:00
|
|
|
has_many :shot_groups, ShotGroup
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2022-01-31 20:08:01 -05:00
|
|
|
@type t :: %AmmoGroup{
|
|
|
|
id: id(),
|
|
|
|
count: integer,
|
2022-02-09 23:45:10 -05:00
|
|
|
notes: String.t() | nil,
|
|
|
|
price_paid: float() | nil,
|
2022-02-15 17:33:45 -05:00
|
|
|
staged: boolean(),
|
2022-11-19 14:52:01 -05:00
|
|
|
purchased_on: Date.t(),
|
2022-02-09 23:45:10 -05:00
|
|
|
ammo_type: AmmoType.t() | nil,
|
2022-01-31 20:08:01 -05:00
|
|
|
ammo_type_id: AmmoType.id(),
|
2022-02-09 23:45:10 -05:00
|
|
|
container: Container.t() | nil,
|
2022-01-31 20:08:01 -05:00
|
|
|
container_id: Container.id(),
|
2022-02-09 23:45:10 -05:00
|
|
|
user: User.t() | nil,
|
2022-01-31 20:08:01 -05:00
|
|
|
user_id: User.id(),
|
|
|
|
inserted_at: NaiveDateTime.t(),
|
|
|
|
updated_at: NaiveDateTime.t()
|
|
|
|
}
|
|
|
|
@type new_ammo_group :: %AmmoGroup{}
|
|
|
|
@type id :: UUID.t()
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
@doc false
|
2022-07-04 20:06:41 -04:00
|
|
|
@spec create_changeset(
|
|
|
|
new_ammo_group(),
|
|
|
|
AmmoType.t() | nil,
|
|
|
|
Container.t() | nil,
|
|
|
|
User.t(),
|
|
|
|
attrs :: map()
|
|
|
|
) :: Changeset.t(new_ammo_group())
|
|
|
|
def create_changeset(
|
|
|
|
ammo_group,
|
|
|
|
%AmmoType{id: ammo_type_id},
|
|
|
|
%Container{id: container_id, user_id: user_id},
|
|
|
|
%User{id: user_id},
|
|
|
|
attrs
|
|
|
|
)
|
|
|
|
when not (ammo_type_id |> is_nil()) and not (container_id |> is_nil()) and
|
|
|
|
not (user_id |> is_nil()) do
|
2021-09-02 23:31:14 -04:00
|
|
|
ammo_group
|
2022-07-04 20:06:41 -04:00
|
|
|
|> change(ammo_type_id: ammo_type_id)
|
|
|
|
|> change(user_id: user_id)
|
|
|
|
|> change(container_id: container_id)
|
2022-11-19 14:52:01 -05:00
|
|
|
|> cast(attrs, [:count, :price_paid, :notes, :staged, :purchased_on])
|
2022-02-14 20:51:09 -05:00
|
|
|
|> validate_number(:count, greater_than: 0)
|
2022-11-19 14:52:01 -05:00
|
|
|
|> validate_required([:count, :staged, :purchased_on, :ammo_type_id, :container_id, :user_id])
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
2022-02-10 00:57:56 -05:00
|
|
|
|
2022-07-04 20:06:41 -04:00
|
|
|
@doc """
|
|
|
|
Invalid changeset, used to prompt user to select ammo type and container
|
|
|
|
"""
|
|
|
|
def create_changeset(ammo_group, _invalid_ammo_type, _invalid_container, _invalid_user, attrs) do
|
|
|
|
ammo_group
|
|
|
|
|> cast(attrs, [:ammo_type_id, :container_id])
|
|
|
|
|> validate_required([:ammo_type_id, :container_id])
|
|
|
|
|> add_error(:invalid, dgettext("errors", "Please select an ammo type and container"))
|
|
|
|
end
|
|
|
|
|
2022-02-10 00:57:56 -05:00
|
|
|
@doc false
|
2022-11-10 21:45:50 -05:00
|
|
|
@spec update_changeset(t() | new_ammo_group(), attrs :: map(), User.t()) ::
|
2022-02-10 00:57:56 -05:00
|
|
|
Changeset.t(t() | new_ammo_group())
|
2022-11-10 21:45:50 -05:00
|
|
|
def update_changeset(ammo_group, attrs, user) do
|
2022-02-10 00:57:56 -05:00
|
|
|
ammo_group
|
2022-11-19 14:52:01 -05:00
|
|
|
|> cast(attrs, [:count, :price_paid, :notes, :staged, :purchased_on, :container_id])
|
2022-04-19 19:38:03 -04:00
|
|
|
|> validate_number(:count, greater_than_or_equal_to: 0)
|
2022-11-10 21:45:50 -05:00
|
|
|
|> validate_container_id(user)
|
2022-11-19 14:52:01 -05:00
|
|
|
|> validate_required([:count, :staged, :purchased_on, :container_id])
|
2022-11-10 21:45:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
defp validate_container_id(changeset, user) do
|
|
|
|
container_id = changeset |> Changeset.get_field(:container_id)
|
|
|
|
|
|
|
|
if container_id do
|
|
|
|
Containers.get_container!(container_id, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
changeset
|
2022-02-14 20:51:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
This range changeset is used when "using up" ammo groups, and allows for
|
|
|
|
updating the count to 0
|
|
|
|
"""
|
|
|
|
@spec range_changeset(t() | new_ammo_group(), attrs :: map()) ::
|
|
|
|
Changeset.t(t() | new_ammo_group())
|
|
|
|
def range_changeset(ammo_group, attrs) do
|
|
|
|
ammo_group
|
2022-02-15 17:33:45 -05:00
|
|
|
|> cast(attrs, [:count, :staged])
|
2022-07-01 19:53:44 -04:00
|
|
|
|> validate_required([:count, :staged])
|
2022-02-10 00:57:56 -05:00
|
|
|
end
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|