forked from shibao/cannery
calculate shot groups in average cost
This commit is contained in:
parent
7110a2db21
commit
69eebc82cc
@ -60,9 +60,10 @@ defmodule Cannery.Ammo do
|
|||||||
) do
|
) do
|
||||||
Repo.one!(
|
Repo.one!(
|
||||||
from ag in AmmoGroup,
|
from ag in AmmoGroup,
|
||||||
|
left_join: sg in assoc(ag, :shot_groups),
|
||||||
where: ag.ammo_type_id == ^ammo_type_id,
|
where: ag.ammo_type_id == ^ammo_type_id,
|
||||||
where: not (ag.price_paid |> is_nil()),
|
where: not (ag.price_paid |> is_nil()),
|
||||||
select: sum(ag.price_paid) / sum(ag.count)
|
select: sum(ag.price_paid) / (sum(ag.count) + sum(sg.count))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ defmodule Cannery.Ammo.AmmoGroup do
|
|||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
alias Cannery.Ammo.{AmmoGroup, AmmoType}
|
alias Cannery.Ammo.{AmmoGroup, AmmoType}
|
||||||
alias Cannery.{Accounts.User, Containers.Container}
|
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Containers.Container}
|
||||||
alias Ecto.{Changeset, UUID}
|
alias Ecto.{Changeset, UUID}
|
||||||
|
|
||||||
@primary_key {:id, :binary_id, autogenerate: true}
|
@primary_key {:id, :binary_id, autogenerate: true}
|
||||||
@ -24,6 +24,8 @@ defmodule Cannery.Ammo.AmmoGroup do
|
|||||||
belongs_to :container, Container
|
belongs_to :container, Container
|
||||||
belongs_to :user, User
|
belongs_to :user, User
|
||||||
|
|
||||||
|
has_many :shot_groups, ShotGroup
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user