economic_resource.ex (5182B)
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.EconomicResource do 19 @moduledoc "A resource which is useful to people or the ecosystem." 20 21 use Zenflows.DB.Schema 22 23 alias Ecto.Changeset 24 alias Zenflows.DB.{Schema, Validate} 25 alias Zenflows.File 26 alias Zenflows.VF.{ 27 Action, 28 Agent, 29 EconomicEvent, 30 EconomicResource, 31 Measure, 32 ProcessSpecification, 33 ProductBatch, 34 ResourceSpecification, 35 SpatialThing, 36 Unit, 37 } 38 39 @type t() :: %__MODULE__{ 40 name: String.t(), 41 note: String.t() | nil, 42 images: [File.t()], 43 tracking_identifier: String.t() | nil, 44 classified_as: [String.t()] | nil, 45 conforms_to: ResourceSpecification.t(), 46 accounting_quantity: Measure.t() | nil, 47 accounting_quantity_has_unit: Unit.t(), 48 accounting_quantity_has_numerical_value: Decimal.t(), 49 onhand_quantity: Measure.t() | nil, 50 onhand_quantity_has_unit: Unit.t(), 51 onhand_quantity_has_numerical_value: Decimal.t(), 52 primary_accountable: Agent.t(), 53 custodian: Agent.t(), 54 stage: ProcessSpecification.t() | nil, 55 state: Action.t() | nil, 56 state_id: Action.ID.t() | nil, 57 lot: ProductBatch.t() | nil, 58 current_location: SpatialThing.t() | nil, 59 unit_of_effort: Unit.t() | nil, 60 contained_in: EconomicResource.t() | nil, 61 okhv: String.t() | nil, 62 repo: String.t() | nil, 63 version: String.t() | nil, 64 licensor: String.t() | nil, 65 license: String.t() | nil, 66 metadata: map() | nil, 67 previous_event: nil | EconomicEvent.t(), 68 } 69 70 @derive {Jason.Encoder, only: ~w[ 71 id 72 name note tracking_identifier classified_as state_id okhv 73 repo version licensor license metadata 74 accounting_quantity_has_numerical_value 75 accounting_quantity_has_unit_id 76 onhand_quantity_has_numerical_value 77 onhand_quantity_has_unit_id 78 conforms_to_id primary_accountable_id custodian_id 79 stage_id current_location_id lot_id contained_in_id 80 unit_of_effort_id previous_event_id 81 ]a} 82 schema "vf_economic_resource" do 83 field :name, :string 84 field :note, :string 85 has_many :images, File 86 field :tracking_identifier, :string 87 field :classified_as, {:array, :string} 88 belongs_to :conforms_to, ResourceSpecification 89 field :accounting_quantity, :map, virtual: true 90 belongs_to :accounting_quantity_has_unit, Unit 91 field :accounting_quantity_has_numerical_value, :decimal 92 field :onhand_quantity, :map, virtual: true 93 belongs_to :onhand_quantity_has_unit, Unit 94 field :onhand_quantity_has_numerical_value, :decimal 95 belongs_to :primary_accountable, Agent 96 belongs_to :custodian, Agent 97 belongs_to :stage, ProcessSpecification 98 field :state_id, Action.ID 99 field :state, :map, virtual: true 100 belongs_to :current_location, SpatialThing 101 belongs_to :lot, ProductBatch 102 belongs_to :contained_in, EconomicResource 103 belongs_to :unit_of_effort, Unit 104 field :okhv, :string 105 field :repo, :string 106 field :version, :string 107 field :licensor, :string 108 field :license, :string 109 field :metadata, :map 110 belongs_to :previous_event, EconomicEvent 111 timestamps() 112 end 113 114 @reqr ~w[ 115 name 116 conforms_to_id 117 primary_accountable_id custodian_id 118 accounting_quantity_has_unit_id accounting_quantity_has_numerical_value 119 onhand_quantity_has_unit_id onhand_quantity_has_numerical_value 120 previous_event_id 121 ]a 122 @cast @reqr ++ ~w[ 123 note tracking_identifier 124 classified_as 125 stage_id state_id current_location_id 126 lot_id contained_in_id unit_of_effort_id 127 okhv repo version licensor license metadata 128 ]a 129 130 @doc false 131 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 132 def changeset(schema \\ %__MODULE__{}, params) do 133 schema 134 |> Changeset.cast(params, @cast) 135 |> Changeset.validate_required(@reqr) 136 |> Validate.name(:name) 137 |> Validate.note(:note) 138 |> Validate.class(:classified_as) 139 |> Validate.name(:okhv) 140 |> Validate.uri(:repo) 141 |> Validate.name(:version) 142 |> Validate.name(:licensor) 143 |> Validate.name(:license) 144 |> Validate.value_eq([:accounting_quantity_has_unit_id, :onhand_quantity_has_unit_id]) 145 |> Changeset.cast_assoc(:images) 146 |> Changeset.assoc_constraint(:conforms_to) 147 |> Changeset.assoc_constraint(:accounting_quantity_has_unit) 148 |> Changeset.assoc_constraint(:onhand_quantity_has_unit) 149 |> Changeset.assoc_constraint(:primary_accountable) 150 |> Changeset.assoc_constraint(:custodian) 151 |> Changeset.assoc_constraint(:stage) 152 |> Changeset.assoc_constraint(:current_location) 153 |> Changeset.assoc_constraint(:lot) 154 |> Changeset.assoc_constraint(:contained_in) 155 |> Changeset.assoc_constraint(:unit_of_effort) 156 |> Changeset.assoc_constraint(:previous_event) 157 end 158 end