1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/models/subscription.rb

142 lines
4.0 KiB
Ruby
Raw Normal View History

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
2016-03-23 18:39:41 +01:00
include NotifyWith::NotificationAttachedObject
belongs_to :plan
belongs_to :statistic_profile
2016-03-23 18:39:41 +01:00
has_one :payment_schedule_object, as: :object, dependent: :destroy
has_one :payment_gateway_object, as: :item
has_many :invoice_items, as: :object, dependent: :destroy
2016-03-23 18:39:41 +01:00
has_many :offer_days, dependent: :destroy
validates_presence_of :plan_id
2016-04-07 12:28:25 +02:00
validates_with SubscriptionGroupValidator
2016-03-23 18:39:41 +01:00
# creation
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?
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
def expire(time)
if !expired?
2018-12-10 17:20:23 +01:00
update_columns(expiration_date: time, canceled_at: time)
2016-03-23 18:39:41 +01:00
notify_admin_subscription_canceled
notify_member_subscription_canceled
true
else
false
end
end
def expired?
expired_at <= DateTime.current
2016-03-23 18:39:41 +01:00
end
def expired_at
last_offered = offer_days.order(:end_at).last
return last_offered.end_at if last_offered
expiration_date
end
def free_extend(expiration, operator_profile_id)
return false if expiration <= expired_at
od = offer_days.create(start_at: expired_at, end_at: expiration)
2019-06-11 16:56:11 +02:00
invoice = Invoice.new(
invoicing_profile: user.invoicing_profile,
statistic_profile: user.statistic_profile,
operator_profile_id: operator_profile_id,
2019-06-11 16:56:11 +02:00
total: 0
)
invoice.invoice_items.push InvoiceItem.new(amount: 0, description: plan.base_name, object: od)
invoice.save
2016-03-23 18:39:41 +01:00
if save
notify_subscription_extended(true)
2016-03-23 18:39:41 +01:00
return true
end
false
2016-03-23 18:39:41 +01:00
end
def user
statistic_profile.user
end
def original_payment_schedule
payment_schedule_object&.payment_schedule
end
# 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
end
2016-03-23 18:39:41 +01:00
private
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_admin_subscription_canceled
NotificationCenter.call type: 'notify_admin_subscription_canceled',
2020-04-29 15:34:30 +02:00
receiver: User.admins_and_managers,
2016-03-23 18:39:41 +01:00
attached_object: self
end
def notify_member_subscription_canceled
NotificationCenter.call type: 'notify_member_subscription_canceled',
receiver: user,
attached_object: self
end
def notify_partner_subscribed_plan
NotificationCenter.call type: 'notify_partner_subscribed_plan',
receiver: plan.partners,
attached_object: self
end
def notify_subscription_extended(free_days)
meta_data = {}
meta_data[:free_days] = true if free_days
NotificationCenter.call type: :notify_member_subscription_extended,
receiver: user,
attached_object: self,
meta_data: meta_data
2016-03-23 18:39:41 +01:00
NotificationCenter.call type: :notify_admin_subscription_extended,
2020-04-29 15:34:30 +02:00
receiver: User.admins_and_managers,
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
start_at = DateTime.current.in_time_zone
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