2019-09-19 15:34:21 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Group is way to bind users with prices. Different prices can be defined for each plan/reservable, for each group
|
2020-03-25 10:16:47 +01:00
|
|
|
class Group < ApplicationRecord
|
2022-10-25 11:57:26 +02:00
|
|
|
has_many :plans, dependent: :destroy
|
|
|
|
has_many :users, dependent: :nullify
|
|
|
|
has_many :statistic_profiles, dependent: :nullify
|
2015-05-05 03:10:25 +02:00
|
|
|
has_many :trainings_pricings, dependent: :destroy
|
2022-10-25 11:57:26 +02:00
|
|
|
has_many :machines_prices, -> { where(priceable_type: 'Machine') }, class_name: 'Price', dependent: :destroy, inverse_of: :group
|
|
|
|
has_many :spaces_prices, -> { where(priceable_type: 'Space') }, class_name: 'Price', dependent: :destroy, inverse_of: :group
|
2022-03-18 19:44:30 +01:00
|
|
|
has_many :proof_of_identity_types_groups, dependent: :destroy
|
|
|
|
has_many :proof_of_identity_types, through: :proof_of_identity_types_groups
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
extend FriendlyId
|
|
|
|
friendly_id :name, use: :slugged
|
|
|
|
|
|
|
|
validates :name, :slug, presence: true
|
2020-10-20 15:52:14 +02:00
|
|
|
validates :disabled, inclusion: { in: [false, nil] }, if: :group_has_users?
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
after_create :create_prices
|
|
|
|
after_create :create_statistic_subtype
|
2020-03-24 16:45:27 +01:00
|
|
|
after_update :update_statistic_subtype, if: :saved_change_to_name?
|
|
|
|
after_update :disable_plans, if: :saved_change_to_disabled?
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
def destroyable?
|
|
|
|
users.empty? and plans.empty?
|
|
|
|
end
|
|
|
|
|
2019-09-19 15:34:21 +02:00
|
|
|
def group_has_users?
|
|
|
|
users.count.positive?
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
private
|
|
|
|
|
2018-11-26 11:33:48 +01:00
|
|
|
def create_prices
|
|
|
|
create_trainings_pricings
|
|
|
|
create_machines_prices
|
|
|
|
create_spaces_prices
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_trainings_pricings
|
2022-10-25 11:57:26 +02:00
|
|
|
Training.find_each do |training|
|
2018-11-26 11:33:48 +01:00
|
|
|
TrainingsPricing.create(group: self, training: training, amount: 0)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2018-11-26 11:33:48 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-26 11:33:48 +01:00
|
|
|
def create_machines_prices
|
2022-10-25 11:57:26 +02:00
|
|
|
Machine.find_each do |machine|
|
2018-11-26 11:33:48 +01:00
|
|
|
Price.create(priceable: machine, group: self, amount: 0)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2018-11-26 11:33:48 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2017-05-15 15:25:27 +02:00
|
|
|
def create_spaces_prices
|
2022-10-25 11:57:26 +02:00
|
|
|
Space.find_each do |space|
|
2017-05-15 15:25:27 +02:00
|
|
|
Price.create(priceable: space, group: self, amount: 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def create_statistic_subtype
|
|
|
|
user_index = StatisticIndex.find_by(es_type_key: 'user')
|
2022-10-25 11:57:26 +02:00
|
|
|
StatisticSubType.create!(statistic_types: user_index.statistic_types, key: slug, label: name)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def update_statistic_subtype
|
|
|
|
user_index = StatisticIndex.find_by(es_type_key: 'user')
|
2018-11-26 11:33:48 +01:00
|
|
|
subtype = StatisticSubType.joins(statistic_type_sub_types: :statistic_type)
|
|
|
|
.where(key: slug, statistic_types: { statistic_index_id: user_index.id })
|
|
|
|
.first
|
|
|
|
subtype.label = name
|
2016-03-23 18:39:41 +01:00
|
|
|
subtype.save!
|
|
|
|
end
|
2019-09-19 15:34:21 +02:00
|
|
|
|
|
|
|
def disable_plans
|
|
|
|
plans.each do |plan|
|
2022-10-25 11:57:26 +02:00
|
|
|
plan.update(disabled: disabled)
|
2019-09-19 15:34:21 +02:00
|
|
|
end
|
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|