2020-03-30 11:33:12 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Training is a course for members to acquire knowledge on a specific matter.
|
|
|
|
# Trainings are designed to be scheduled periodically through Availabilities.
|
|
|
|
# A Training can be a prerequisite before members can book a Machine.
|
2020-03-25 10:16:47 +01:00
|
|
|
class Training < ApplicationRecord
|
2016-03-23 18:39:41 +01:00
|
|
|
extend FriendlyId
|
|
|
|
friendly_id :name, use: :slugged
|
|
|
|
|
2016-07-13 18:15:14 +02:00
|
|
|
has_one :training_image, as: :viewable, dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :training_image, allow_destroy: true
|
|
|
|
|
2022-11-10 16:14:49 +01:00
|
|
|
has_many :trainings_machines, dependent: :destroy
|
|
|
|
has_many :machines, through: :trainings_machines
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2022-11-10 16:14:49 +01:00
|
|
|
has_many :trainings_availabilities, dependent: :destroy
|
2016-03-23 18:39:41 +01:00
|
|
|
has_many :availabilities, through: :trainings_availabilities, dependent: :destroy
|
|
|
|
|
|
|
|
has_many :reservations, as: :reservable, dependent: :destroy
|
|
|
|
|
2019-06-05 16:21:39 +02:00
|
|
|
# members who has validated the trainings
|
|
|
|
has_many :statistic_profile_trainings, dependent: :destroy
|
|
|
|
has_many :statistic_profiles, through: :statistic_profile_trainings
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
has_many :trainings_pricings, dependent: :destroy
|
|
|
|
|
|
|
|
has_many :credits, as: :creditable, dependent: :destroy
|
|
|
|
has_many :plans, through: :credits
|
|
|
|
|
2023-02-07 19:35:33 +01:00
|
|
|
has_one :payment_gateway_object, -> { order id: :desc }, inverse_of: :training, as: :item, dependent: :destroy
|
2022-11-10 16:14:49 +01:00
|
|
|
|
|
|
|
has_one :advanced_accounting, as: :accountable, dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :advanced_accounting, allow_destroy: true
|
2021-04-23 17:54:59 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
after_create :create_statistic_subtype
|
|
|
|
after_create :create_trainings_pricings
|
2021-04-30 16:07:19 +02:00
|
|
|
after_create :update_gateway_product
|
|
|
|
after_update :update_gateway_product, if: :saved_change_to_name?
|
2020-03-24 16:45:27 +01:00
|
|
|
after_update :update_statistic_subtype, if: :saved_change_to_name?
|
2016-03-23 18:39:41 +01:00
|
|
|
after_destroy :remove_statistic_subtype
|
|
|
|
|
|
|
|
def amount_by_group(group)
|
|
|
|
trainings_pricings.where(group_id: group).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_statistic_subtype
|
2022-11-10 16:14:49 +01:00
|
|
|
index = StatisticIndex.find_by(es_type_key: 'training')
|
|
|
|
StatisticSubType.create!(statistic_types: index.statistic_types, key: slug, label: name)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_statistic_subtype
|
|
|
|
index = StatisticIndex.where(es_type_key: 'training')
|
2018-12-12 13:49:14 +01:00
|
|
|
subtype = StatisticSubType.joins(statistic_type_sub_types: :statistic_type)
|
2022-11-10 16:14:49 +01:00
|
|
|
.find_by(key: slug, statistic_types: { statistic_index_id: index.id })
|
|
|
|
subtype.update(label: name)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_statistic_subtype
|
2022-11-10 16:14:49 +01:00
|
|
|
subtype = StatisticSubType.find_by(key: slug)
|
2016-03-23 18:39:41 +01:00
|
|
|
subtype.destroy!
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroyable?
|
|
|
|
reservations.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2018-12-12 13:49:14 +01:00
|
|
|
|
|
|
|
def create_trainings_pricings
|
2022-11-10 16:14:49 +01:00
|
|
|
Group.find_each do |group|
|
2018-12-12 13:49:14 +01:00
|
|
|
TrainingsPricing.create(training: self, group: group, amount: 0)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2018-12-12 13:49:14 +01:00
|
|
|
end
|
2020-11-12 12:14:51 +01:00
|
|
|
|
2021-04-30 16:07:19 +02:00
|
|
|
def update_gateway_product
|
2021-05-19 18:12:52 +02:00
|
|
|
PaymentGatewayService.new.create_or_update_product(Training.name, id)
|
2020-11-12 12:14:51 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|