2019-09-10 11:46:14 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Subscription is an active or archived subscription of an User to a Plan
|
2020-03-25 10:16:47 +01:00
|
|
|
class Subscription < ApplicationRecord
|
2023-01-27 13:53:23 +01:00
|
|
|
include NotificationAttachedObject
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
belongs_to :plan
|
2019-06-04 16:50:23 +02:00
|
|
|
belongs_to :statistic_profile
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2021-05-27 16:11:23 +02:00
|
|
|
has_one :payment_schedule_object, as: :object, dependent: :destroy
|
2022-10-25 11:57:26 +02:00
|
|
|
has_one :payment_gateway_object, as: :item, dependent: :destroy
|
2021-05-25 17:28:35 +02:00
|
|
|
has_many :invoice_items, as: :object, dependent: :destroy
|
2016-03-23 18:39:41 +01:00
|
|
|
has_many :offer_days, dependent: :destroy
|
2022-12-28 17:51:27 +01:00
|
|
|
|
|
|
|
has_many :cart_item_free_extensions, class_name: 'CartItem::FreeExtension', dependent: :destroy
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2022-10-25 11:57:26 +02:00
|
|
|
validates :plan_id, presence: true
|
2016-04-07 12:28:25 +02:00
|
|
|
validates_with SubscriptionGroupValidator
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
# creation
|
2021-05-21 18:25:18 +02:00
|
|
|
before_create :set_expiration_date
|
2018-12-10 17:20:23 +01:00
|
|
|
after_save :notify_member_subscribed_plan
|
|
|
|
after_save :notify_admin_subscribed_plan
|
2016-03-23 18:39:41 +01:00
|
|
|
after_save :notify_partner_subscribed_plan, if: :of_partner_plan?
|
2020-12-16 18:33:43 +01:00
|
|
|
|
2022-10-25 11:57:26 +02:00
|
|
|
delegate :user, to: :statistic_profile
|
|
|
|
|
2019-09-09 17:37:54 +02:00
|
|
|
def generate_and_save_invoice(operator_profile_id)
|
|
|
|
generate_invoice(operator_profile_id).save
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2023-02-17 15:35:06 +01:00
|
|
|
def expire
|
|
|
|
Subscriptions::ExpireService.call(self)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2018-12-06 18:26:01 +01:00
|
|
|
def expired?
|
2023-02-14 13:10:58 +01:00
|
|
|
expired_at <= Time.current
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2018-12-10 13:24:00 +01:00
|
|
|
def expired_at
|
|
|
|
last_offered = offer_days.order(:end_at).last
|
|
|
|
return last_offered.end_at if last_offered
|
|
|
|
|
|
|
|
expiration_date
|
|
|
|
end
|
|
|
|
|
2021-02-10 13:32:13 +01:00
|
|
|
def original_payment_schedule
|
2021-05-28 17:34:20 +02:00
|
|
|
payment_schedule_object&.payment_schedule
|
|
|
|
end
|
2021-02-10 13:32:13 +01:00
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
# buying invoice
|
|
|
|
def original_invoice
|
|
|
|
invoice_items.select(:invoice_id)
|
|
|
|
.group(:invoice_id)
|
|
|
|
.map(&:invoice_id)
|
|
|
|
.map { |id| Invoice.find_by(id: id, type: nil) }
|
|
|
|
.first
|
2021-02-10 13:32:13 +01:00
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
private
|
2018-12-06 18:26:01 +01:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def notify_member_subscribed_plan
|
|
|
|
NotificationCenter.call type: 'notify_member_subscribed_plan',
|
|
|
|
receiver: user,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_admin_subscribed_plan
|
|
|
|
NotificationCenter.call type: 'notify_admin_subscribed_plan',
|
|
|
|
receiver: User.admins,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_partner_subscribed_plan
|
|
|
|
NotificationCenter.call type: 'notify_partner_subscribed_plan',
|
|
|
|
receiver: plan.partners,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:07:35 +02:00
|
|
|
def notify_subscription_extended
|
|
|
|
meta_data = { free_days: false }
|
2019-02-26 10:45:12 +01:00
|
|
|
NotificationCenter.call type: :notify_member_subscription_extended,
|
|
|
|
receiver: user,
|
|
|
|
attached_object: self,
|
|
|
|
meta_data: meta_data
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2019-02-26 10:45:12 +01:00
|
|
|
NotificationCenter.call type: :notify_admin_subscription_extended,
|
2020-04-29 15:34:30 +02:00
|
|
|
receiver: User.admins_and_managers,
|
2019-02-26 10:45:12 +01:00
|
|
|
attached_object: self,
|
|
|
|
meta_data: meta_data
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2018-12-11 15:07:21 +01:00
|
|
|
def set_expiration_date
|
2023-02-14 13:10:58 +01:00
|
|
|
start_at = self.start_at || Time.current
|
2018-12-11 15:07:21 +01:00
|
|
|
self.expiration_date = start_at + plan.duration
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def of_partner_plan?
|
|
|
|
plan.is_a?(PartnerPlan)
|
|
|
|
end
|
|
|
|
end
|