2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2022-06-16 17:21:47 +02:00
|
|
|
|
class Logic::ChampValue < Logic::Term
|
2022-06-21 14:39:13 +02:00
|
|
|
|
MANAGED_TYPE_DE_CHAMP = TypeDeChamp.type_champs.slice(
|
|
|
|
|
:yes_no,
|
|
|
|
|
:checkbox,
|
|
|
|
|
:integer_number,
|
|
|
|
|
:decimal_number,
|
2022-09-09 15:36:50 +02:00
|
|
|
|
:drop_down_list,
|
2023-09-19 16:15:32 +02:00
|
|
|
|
:multiple_drop_down_list,
|
2023-10-17 11:27:36 +02:00
|
|
|
|
:communes,
|
2023-10-17 11:23:56 +02:00
|
|
|
|
:epci,
|
2023-10-17 11:14:58 +02:00
|
|
|
|
:departements,
|
2024-01-29 15:15:18 +01:00
|
|
|
|
:regions,
|
|
|
|
|
:address
|
2022-06-21 14:39:13 +02:00
|
|
|
|
)
|
|
|
|
|
|
2022-06-22 16:46:00 +02:00
|
|
|
|
CHAMP_VALUE_TYPE = {
|
2022-09-20 15:37:00 +02:00
|
|
|
|
boolean: :boolean, # from yes_no or checkbox champ
|
|
|
|
|
number: :number, # from integer or decimal number champ
|
|
|
|
|
enum: :enum, # a choice from a dropdownlist
|
2023-12-06 17:11:32 +01:00
|
|
|
|
commune_enum: :commune_enum,
|
|
|
|
|
epci_enum: :epci_enum,
|
|
|
|
|
departement_enum: :departement_enum,
|
2024-01-29 15:15:18 +01:00
|
|
|
|
address: :address,
|
2022-09-20 15:37:00 +02:00
|
|
|
|
enums: :enums, # multiple choice from a dropdownlist (multipledropdownlist)
|
2022-06-28 12:49:25 +02:00
|
|
|
|
empty: :empty,
|
|
|
|
|
unmanaged: :unmanaged
|
2022-06-22 16:46:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 17:21:47 +02:00
|
|
|
|
attr_reader :stable_id
|
|
|
|
|
|
2022-07-06 09:44:54 +02:00
|
|
|
|
def initialize(stable_id)
|
2022-06-16 17:21:47 +02:00
|
|
|
|
@stable_id = stable_id
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-04 11:10:10 +01:00
|
|
|
|
def sources
|
|
|
|
|
[@stable_id]
|
|
|
|
|
end
|
|
|
|
|
|
2022-06-16 17:21:47 +02:00
|
|
|
|
def compute(champs)
|
2022-07-19 17:40:50 +02:00
|
|
|
|
targeted_champ = champ(champs)
|
|
|
|
|
|
2024-02-29 15:13:44 +01:00
|
|
|
|
return nil if targeted_champ.nil?
|
2022-07-19 17:40:50 +02:00
|
|
|
|
return nil if !targeted_champ.visible?
|
2023-07-26 17:29:54 +02:00
|
|
|
|
return nil if targeted_champ.blank? & !targeted_champ.drop_down_other?
|
2022-07-19 17:40:50 +02:00
|
|
|
|
|
2022-10-13 15:01:46 +02:00
|
|
|
|
case targeted_champ.type
|
|
|
|
|
when "Champs::YesNoChamp",
|
|
|
|
|
"Champs::CheckboxChamp"
|
2022-07-19 17:40:50 +02:00
|
|
|
|
targeted_champ.true?
|
2022-10-13 15:01:46 +02:00
|
|
|
|
when "Champs::IntegerNumberChamp", "Champs::DecimalNumberChamp"
|
2024-10-21 13:09:40 +02:00
|
|
|
|
# TODO expose raw typed value of champs
|
|
|
|
|
targeted_champ.type_de_champ.champ_value_for_api(targeted_champ, version: 1)
|
2022-10-13 15:01:46 +02:00
|
|
|
|
when "Champs::DropDownListChamp"
|
2022-08-02 12:57:41 +02:00
|
|
|
|
targeted_champ.selected
|
2022-10-13 15:01:46 +02:00
|
|
|
|
when "Champs::MultipleDropDownListChamp"
|
2022-09-09 15:36:50 +02:00
|
|
|
|
targeted_champ.selected_options
|
2024-02-13 11:24:58 +01:00
|
|
|
|
when "Champs::RegionChamp"
|
2023-08-29 11:35:39 +02:00
|
|
|
|
targeted_champ.code
|
2024-02-13 11:24:58 +01:00
|
|
|
|
when "Champs::DepartementChamp"
|
|
|
|
|
{
|
|
|
|
|
value: targeted_champ.code,
|
|
|
|
|
code_region: targeted_champ.code_region
|
|
|
|
|
}
|
2024-01-29 15:15:18 +01:00
|
|
|
|
when "Champs::CommuneChamp", "Champs::EpciChamp", "Champs::AddressChamp"
|
2024-02-13 11:24:58 +01:00
|
|
|
|
{
|
|
|
|
|
code_departement: targeted_champ.code_departement,
|
|
|
|
|
code_region: targeted_champ.code_region
|
|
|
|
|
}
|
2023-12-06 17:11:32 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-26 21:21:55 +02:00
|
|
|
|
def to_s(type_de_champs) = type_de_champ(type_de_champs)&.libelle # TODO: gerer le cas ou un tdc est supprimé
|
2022-06-16 17:21:47 +02:00
|
|
|
|
|
2022-09-26 21:07:43 +02:00
|
|
|
|
def type(type_de_champs)
|
|
|
|
|
case type_de_champ(type_de_champs)&.type_champ # TODO: gerer le cas ou un tdc est supprimé
|
2022-06-21 14:39:13 +02:00
|
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:yes_no),
|
|
|
|
|
MANAGED_TYPE_DE_CHAMP.fetch(:checkbox)
|
2022-06-22 16:46:00 +02:00
|
|
|
|
CHAMP_VALUE_TYPE.fetch(:boolean)
|
2022-06-21 14:39:13 +02:00
|
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:integer_number), MANAGED_TYPE_DE_CHAMP.fetch(:decimal_number)
|
2022-06-22 16:46:00 +02:00
|
|
|
|
CHAMP_VALUE_TYPE.fetch(:number)
|
2023-10-17 11:23:56 +02:00
|
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:drop_down_list),
|
2023-10-17 11:27:36 +02:00
|
|
|
|
MANAGED_TYPE_DE_CHAMP.fetch(:regions)
|
|
|
|
|
CHAMP_VALUE_TYPE.fetch(:enum)
|
2023-12-06 17:11:32 +01:00
|
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:communes)
|
|
|
|
|
CHAMP_VALUE_TYPE.fetch(:commune_enum)
|
|
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:epci)
|
|
|
|
|
CHAMP_VALUE_TYPE.fetch(:epci_enum)
|
|
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:departements)
|
|
|
|
|
CHAMP_VALUE_TYPE.fetch(:departement_enum)
|
2024-01-29 15:15:18 +01:00
|
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:address)
|
|
|
|
|
CHAMP_VALUE_TYPE.fetch(:address)
|
2022-09-09 15:36:50 +02:00
|
|
|
|
when MANAGED_TYPE_DE_CHAMP.fetch(:multiple_drop_down_list)
|
|
|
|
|
CHAMP_VALUE_TYPE.fetch(:enums)
|
2022-06-21 14:39:13 +02:00
|
|
|
|
else
|
2022-06-28 12:49:25 +02:00
|
|
|
|
CHAMP_VALUE_TYPE.fetch(:unmanaged)
|
2022-06-16 17:21:47 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-26 21:11:43 +02:00
|
|
|
|
def errors(type_de_champs)
|
|
|
|
|
if !type_de_champs.map(&:stable_id).include?(stable_id)
|
2022-09-14 10:43:18 +02:00
|
|
|
|
[{ type: :not_available }]
|
2022-06-16 17:21:47 +02:00
|
|
|
|
else
|
|
|
|
|
[]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def to_h
|
|
|
|
|
{
|
2022-07-05 14:47:32 +02:00
|
|
|
|
"term" => self.class.name,
|
2022-07-06 09:44:54 +02:00
|
|
|
|
"stable_id" => @stable_id
|
2022-06-16 17:21:47 +02:00
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.from_h(h)
|
2022-07-06 09:44:54 +02:00
|
|
|
|
self.new(h['stable_id'])
|
2022-06-16 17:21:47 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
|
self.class == other.class && @stable_id == other.stable_id
|
|
|
|
|
end
|
|
|
|
|
|
2023-12-06 17:11:32 +01:00
|
|
|
|
def options(type_de_champs, operator_name = nil)
|
2022-09-26 21:07:43 +02:00
|
|
|
|
tdc = type_de_champ(type_de_champs)
|
2023-10-17 11:14:58 +02:00
|
|
|
|
|
2023-12-08 14:03:06 +01:00
|
|
|
|
if operator_name.in?([Logic::InRegionOperator.name, Logic::NotInRegionOperator.name]) || tdc.type_champ == MANAGED_TYPE_DE_CHAMP.fetch(:regions)
|
2023-10-17 11:14:58 +02:00
|
|
|
|
APIGeoService.regions.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] }
|
2024-01-29 15:15:18 +01:00
|
|
|
|
elsif operator_name.in?([Logic::InDepartementOperator.name, Logic::NotInDepartementOperator.name]) || tdc.type_champ.in?([MANAGED_TYPE_DE_CHAMP.fetch(:communes), MANAGED_TYPE_DE_CHAMP.fetch(:epci), MANAGED_TYPE_DE_CHAMP.fetch(:departements), MANAGED_TYPE_DE_CHAMP.fetch(:address)])
|
2023-12-06 17:11:32 +01:00
|
|
|
|
APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] }
|
2022-08-02 12:57:41 +02:00
|
|
|
|
else
|
2024-09-19 17:11:14 +02:00
|
|
|
|
tdc.drop_down_options_with_other.map { _1.is_a?(Array) ? _1 : [_1, _1] }
|
2022-08-02 12:57:41 +02:00
|
|
|
|
end
|
2022-06-09 14:20:06 +02:00
|
|
|
|
end
|
|
|
|
|
|
2022-06-16 17:21:47 +02:00
|
|
|
|
private
|
|
|
|
|
|
2022-09-26 21:07:43 +02:00
|
|
|
|
def type_de_champ(type_de_champs)
|
|
|
|
|
type_de_champs.find { |c| c.stable_id == stable_id }
|
2022-06-16 17:21:47 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def champ(champs)
|
|
|
|
|
champs.find { |c| c.stable_id == stable_id }
|
|
|
|
|
end
|
|
|
|
|
end
|