2019-10-07 12:08:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-03-25 17:45:53 +01:00
|
|
|
# Machine is an hardware equipment hosted in the Fablab that is available for reservation to the members
|
2020-03-25 10:16:47 +01:00
|
|
|
class Machine < ApplicationRecord
|
2015-05-05 03:10:25 +02:00
|
|
|
extend FriendlyId
|
|
|
|
friendly_id :name, use: :slugged
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
has_one :machine_image, as: :viewable, dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :machine_image, allow_destroy: true
|
|
|
|
has_many :machine_files, as: :viewable, dependent: :destroy
|
2016-03-23 18:39:41 +01:00
|
|
|
accepts_nested_attributes_for :machine_files, allow_destroy: true, reject_if: :all_blank
|
|
|
|
|
2019-10-07 12:08:08 +02:00
|
|
|
has_and_belongs_to_many :projects, join_table: 'projects_machines'
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
has_many :machines_availabilities, dependent: :destroy
|
|
|
|
has_many :availabilities, through: :machines_availabilities
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2019-10-07 12:08:08 +02:00
|
|
|
has_and_belongs_to_many :trainings, join_table: 'trainings_machines'
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
validates :name, presence: true, length: { maximum: 50 }
|
|
|
|
validates :description, presence: true
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
has_many :prices, as: :priceable, dependent: :destroy
|
2021-06-25 17:24:34 +02:00
|
|
|
has_many :prepaid_packs, as: :priceable, dependent: :destroy
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
has_many :reservations, as: :reservable, dependent: :destroy
|
|
|
|
has_many :credits, as: :creditable, dependent: :destroy
|
|
|
|
has_many :plans, through: :credits
|
|
|
|
|
2021-04-23 17:54:59 +02:00
|
|
|
has_one :payment_gateway_object, as: :item
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
after_create :create_statistic_subtype
|
|
|
|
after_create :create_machine_prices
|
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 not_subscribe_price(group_id)
|
|
|
|
prices.find_by(plan_id: nil, group_id: group_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prices_by_group(group_id, plan_id = nil)
|
|
|
|
prices.where.not(plan_id: plan_id).where(group_id: group_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_statistic_subtype
|
|
|
|
index = StatisticIndex.where(es_type_key: 'machine')
|
2019-10-07 12:08:08 +02:00
|
|
|
StatisticSubType.create!(statistic_types: index.first.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: 'machine')
|
2019-10-07 12:08:08 +02:00
|
|
|
subtype = StatisticSubType.joins(statistic_type_sub_types: :statistic_type)
|
|
|
|
.where(key: slug, statistic_types: { statistic_index_id: index.first.id })
|
|
|
|
.first
|
|
|
|
subtype.label = name
|
2016-03-23 18:39:41 +01:00
|
|
|
subtype.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_statistic_subtype
|
2019-10-07 12:08:08 +02:00
|
|
|
subtype = StatisticSubType.where(key: slug).first
|
2016-03-23 18:39:41 +01:00
|
|
|
subtype.destroy!
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_machine_prices
|
|
|
|
Group.all.each do |group|
|
|
|
|
Price.create(priceable: self, group: group, amount: 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
Plan.all.includes(:group).each do |plan|
|
|
|
|
Price.create(group: plan.group, plan: plan, priceable: self, amount: 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroyable?
|
|
|
|
reservations.empty?
|
|
|
|
end
|
2020-11-12 12:14:51 +01:00
|
|
|
|
2021-06-25 17:24:34 +02:00
|
|
|
def packs?(user)
|
|
|
|
prepaid_packs.where(group_id: user.group_id)
|
|
|
|
.where(disabled: [false, nil])
|
|
|
|
.count
|
|
|
|
.positive?
|
|
|
|
end
|
|
|
|
|
2020-11-12 12:14:51 +01:00
|
|
|
private
|
|
|
|
|
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(Machine.name, id)
|
2020-11-12 12:14:51 +01:00
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|