commitment.ex (4402B)
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.Commitment do 19 @moduledoc """ 20 A planned economic flow that has been promised by an agent to another 21 agent. 22 """ 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 Agreement, 32 EconomicResource, 33 Measure, 34 Plan, 35 Process, 36 ResourceSpecification, 37 SpatialThing, 38 Unit, 39 } 40 41 @type t() :: %__MODULE__{ 42 action: Action.t(), 43 provider: Agent.t(), 44 receiver: Agent.t(), 45 input_of: Process.t() | nil, 46 output_of: Process.t() | nil, 47 resource_classified_as: [String.t()] | nil, 48 resource_conforms_to: ResourceSpecification.t() | nil, 49 resource_inventoried_as: EconomicResource.t() | nil, 50 resource_quantity: Measure.t() | nil, 51 effort_quantity: Measure.t() | nil, 52 has_beginning: DateTime.t() | nil, 53 has_end: DateTime.t() | nil, 54 has_point_in_time: DateTime.t() | nil, 55 due: DateTime.t() | nil, 56 finished: boolean(), 57 note: String.t() | nil, 58 # in_scope_of: 59 agreed_in: String.t() | nil, 60 independent_demand_of: Plan.t() | nil, 61 at_location: SpatialThing.t() | nil, 62 clause_of: Agreement.t() | nil, 63 } 64 65 schema "vf_commitment" do 66 field :action_id, Action.ID 67 field :action, :map, virtual: true 68 belongs_to :provider, Agent 69 belongs_to :receiver, Agent 70 belongs_to :input_of, Process 71 belongs_to :output_of, Process 72 field :resource_classified_as, {:array, :string} 73 belongs_to :resource_conforms_to, ResourceSpecification 74 belongs_to :resource_inventoried_as, EconomicResource 75 field :resource_quantity, :map, virtual: true 76 belongs_to :resource_quantity_has_unit, Unit 77 field :resource_quantity_has_numerical_value, :decimal 78 field :effort_quantity, :map, virtual: true 79 belongs_to :effort_quantity_has_unit, Unit 80 field :effort_quantity_has_numerical_value, :decimal 81 field :has_beginning, :utc_datetime_usec 82 field :has_end, :utc_datetime_usec 83 field :has_point_in_time, :utc_datetime_usec 84 field :due, :utc_datetime_usec 85 field :finished, :boolean, default: false 86 field :note, :string 87 # field :in_scope_of 88 field :agreed_in, :string 89 belongs_to :independent_demand_of, Plan 90 belongs_to :at_location, SpatialThing 91 belongs_to :clause_of, Agreement 92 timestamps() 93 end 94 95 @reqr ~w[action_id provider_id receiver_id]a 96 @cast @reqr ++ ~w[ 97 input_of_id output_of_id resource_classified_as 98 resource_conforms_to_id resource_inventoried_as_id 99 resource_quantity effort_quantity 100 has_beginning has_end has_point_in_time due 101 finished note agreed_in 102 independent_demand_of_id at_location_id clause_of_id 103 ]a # in_scope_of_id 104 105 @doc false 106 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 107 def changeset(schema \\ %__MODULE__{}, params) do 108 schema 109 |> Changeset.cast(params, @cast) 110 |> Changeset.validate_required(@reqr) 111 |> Validate.exist_or([:has_point_in_time, :has_beginning, :has_end, :due]) 112 |> Validate.exist_nand([:has_point_in_time, :has_beginning]) 113 |> Validate.exist_nand([:has_point_in_time, :has_end]) 114 |> Validate.exist_xor([:resource_conforms_to_id, :resource_inventoried_as_id], method: :both) 115 |> Validate.note(:note) 116 |> Validate.class(:resource_classified_as) 117 |> Measure.cast(:effort_quantity) 118 |> Measure.cast(:resource_quantity) 119 |> Changeset.assoc_constraint(:provider) 120 |> Changeset.assoc_constraint(:receiver) 121 |> Changeset.assoc_constraint(:input_of) 122 |> Changeset.assoc_constraint(:output_of) 123 |> Changeset.assoc_constraint(:resource_conforms_to) 124 |> Changeset.assoc_constraint(:resource_inventoried_as) 125 |> Changeset.assoc_constraint(:independent_demand_of) 126 |> Changeset.assoc_constraint(:at_location) 127 |> Changeset.assoc_constraint(:clause_of) 128 end 129 end