fulfillment.ex (2307B)
1 # Zenflows is designed to implement the Valueflows vocabulary, 2 # written and maintained by srfsh <info@dyne.org>. 3 # Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>. 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU Affero General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU Affero General Public License for more details. 14 # 15 # You should have received a copy of the GNU Affero General Public License 16 # along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 defmodule Zenflows.VF.Fulfillment do 19 @moduledoc """ 20 Represents many-to-many relationships between commitments and economic 21 events that fully or partially satisfy one or more commitments. 22 """ 23 24 use Zenflows.DB.Schema 25 26 alias Ecto.Changeset 27 alias Zenflows.DB.{Schema, Validate} 28 alias Zenflows.VF.{ 29 Commitment, 30 EconomicEvent, 31 Measure, 32 Unit, 33 } 34 35 @type t() :: %__MODULE__{ 36 note: String.t() | nil, 37 fulfilled_by: EconomicEvent.t(), 38 fulfills: Commitment.t(), 39 resource_quantity: Measure.t() | nil, 40 effort_quantity: Measure.t() | nil, 41 } 42 43 schema "vf_fulfillment" do 44 field :note, :string 45 belongs_to :fulfilled_by, EconomicEvent 46 belongs_to :fulfills, Commitment 47 field :resource_quantity, :map, virtual: true 48 belongs_to :resource_quantity_has_unit, Unit 49 field :resource_quantity_has_numerical_value, :decimal 50 field :effort_quantity, :map, virtual: true 51 belongs_to :effort_quantity_has_unit, Unit 52 field :effort_quantity_has_numerical_value, :decimal 53 timestamps() 54 end 55 56 @reqr ~w[fulfilled_by_id fulfills_id]a 57 @cast @reqr ++ ~w[resource_quantity effort_quantity note]a 58 59 @doc false 60 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 61 def changeset(schema \\ %__MODULE__{}, params) do 62 schema 63 |> Changeset.cast(params, @cast) 64 |> Changeset.validate_required(@reqr) 65 |> Validate.note(:note) 66 |> Measure.cast(:resource_quantity) 67 |> Measure.cast(:effort_quantity) 68 |> Changeset.assoc_constraint(:fulfilled_by) 69 |> Changeset.assoc_constraint(:fulfills) 70 end 71 end