claim.ex (3128B)
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.Claim do 19 @moduledoc """ 20 A claim for a future economic event(s) in reciprocity for an economic 21 event that already occurred. For example, a claim for payment for goods 22 received. 23 """ 24 use Zenflows.DB.Schema 25 26 alias Ecto.Changeset 27 alias Zenflows.DB.{Schema, Validate} 28 alias Zenflows.VF.{ 29 Action, 30 Agent, 31 EconomicEvent, 32 Measure, 33 ResourceSpecification, 34 Unit, 35 } 36 37 @type t() :: %__MODULE__{ 38 action: Action.t(), 39 provider: Agent.t(), 40 receiver: Agent.t(), 41 resource_classified_as: [String.t()] | nil, 42 resource_conforms_to: ResourceSpecification.t() | nil, 43 resource_quantity: Measure.t() | nil, 44 effort_quantity: Measure.t() | nil, 45 triggered_by: EconomicEvent.t(), 46 due: DateTime.t() | nil, 47 finished: boolean(), 48 note: String.t() | nil, 49 agreed_in: String.t() | nil, 50 # in_scope_of: 51 } 52 53 schema "vf_claim" do 54 field :action_id, Action.ID 55 field :action, :map, virtual: true 56 belongs_to :provider, Agent 57 belongs_to :receiver, Agent 58 field :resource_classified_as, {:array, :string} 59 belongs_to :resource_conforms_to, ResourceSpecification 60 field :resource_quantity, :map, virtual: true 61 belongs_to :resource_quantity_has_unit, Unit 62 field :resource_quantity_has_numerical_value, :decimal 63 field :effort_quantity, :map, virtual: true 64 belongs_to :effort_quantity_has_unit, Unit 65 field :effort_quantity_has_numerical_value, :decimal 66 belongs_to :triggered_by, EconomicEvent 67 field :due, :utc_datetime_usec 68 field :finished, :boolean 69 field :note, :string 70 field :agreed_in, :string 71 # field :in_scope_of 72 timestamps() 73 end 74 75 @reqr ~w[action_id provider_id receiver_id]a 76 @cast @reqr ++ ~w[ 77 resource_classified_as resource_conforms_to_id 78 resource_quantity effort_quantity 79 triggered_by_id due finished note agreed_in 80 ]a # in_scope_of 81 82 @doc false 83 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 84 def changeset(schema \\ %__MODULE__{}, params) do 85 schema 86 |> Changeset.cast(params, @cast) 87 |> Changeset.validate_required(@reqr) 88 |> Validate.note(:note) 89 |> Validate.class(:resource_classified_as) 90 |> Measure.cast(:resource_quantity) 91 |> Measure.cast(:effort_quantity) 92 |> Changeset.assoc_constraint(:provider) 93 |> Changeset.assoc_constraint(:receiver) 94 |> Changeset.assoc_constraint(:resource_conforms_to) 95 |> Changeset.assoc_constraint(:triggered_by) 96 end 97 end