2022-06-25 12:48:46 +00:00
|
|
|
class Member < ApplicationRecord
|
2022-06-25 20:27:02 +00:00
|
|
|
default_scope { where(excluded: false) }
|
2022-06-25 12:48:46 +00:00
|
|
|
has_many :contributions
|
|
|
|
has_many :notifications
|
2023-07-12 18:13:08 +00:00
|
|
|
has_many :payments
|
2022-06-26 11:57:00 +00:00
|
|
|
has_paper_trail
|
2022-06-25 12:48:46 +00:00
|
|
|
|
2023-04-19 21:52:31 +00:00
|
|
|
def self.ransackable_attributes(auth_object = nil)
|
|
|
|
%w[display_name legal_name email identification_number]
|
|
|
|
end
|
|
|
|
|
2023-07-12 18:13:08 +00:00
|
|
|
def create_payment
|
|
|
|
payments.create(status: "pending")
|
|
|
|
end
|
|
|
|
|
2022-06-25 12:48:46 +00:00
|
|
|
def cancelled_on
|
2023-09-20 13:19:35 +00:00
|
|
|
expires_on + Config.payment_pending_grace_period
|
2022-06-25 12:48:46 +00:00
|
|
|
end
|
|
|
|
|
2023-07-12 18:13:08 +00:00
|
|
|
def obtains_full_rights_on
|
2023-09-20 13:19:35 +00:00
|
|
|
joined_on + Config.full_rights_vesting_period
|
2023-07-12 18:13:08 +00:00
|
|
|
end
|
|
|
|
|
2022-06-25 12:48:46 +00:00
|
|
|
def reset_status!
|
|
|
|
update(status: expected_status)
|
|
|
|
end
|
|
|
|
|
2023-03-23 18:15:45 +00:00
|
|
|
def employed?
|
2023-09-20 13:19:35 +00:00
|
|
|
# normal is deprecated, here for retrocompatibility reasons
|
2023-03-23 18:15:45 +00:00
|
|
|
category == "normal" || category == "employed"
|
|
|
|
end
|
|
|
|
|
2022-06-25 20:27:02 +00:00
|
|
|
def remove_personal_information!
|
|
|
|
update(
|
|
|
|
excluded: true,
|
|
|
|
display_name: "",
|
2023-03-31 01:24:16 +00:00
|
|
|
legal_name: "",
|
|
|
|
pronouns: "",
|
2022-06-25 20:27:02 +00:00
|
|
|
email: nil,
|
|
|
|
identification_number: "",
|
|
|
|
category: "",
|
|
|
|
address: "",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-06-25 12:48:46 +00:00
|
|
|
def expected_status
|
|
|
|
if joined_on.nil?
|
|
|
|
:pending
|
2023-07-12 18:13:08 +00:00
|
|
|
elsif obtains_full_rights_on.future?
|
2022-06-25 12:48:46 +00:00
|
|
|
:passive
|
|
|
|
elsif expires_on.future?
|
|
|
|
:active
|
|
|
|
elsif cancelled_on.future?
|
|
|
|
:expired
|
|
|
|
else
|
|
|
|
:cancelled
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-07 12:50:50 +00:00
|
|
|
def register_contribution(contribution_params, overriden_expires_on, should_send_notification)
|
|
|
|
Contribution.transaction do
|
|
|
|
is_first_contribution = self.contributions.empty?
|
|
|
|
|
|
|
|
contribution = self.contributions.build(contribution_params)
|
|
|
|
if contribution.save
|
|
|
|
self.handle_new_contribution(contribution, overriden_expires_on)
|
|
|
|
self.reset_status!
|
|
|
|
self.regenerate_notifications
|
|
|
|
|
|
|
|
if should_send_notification
|
|
|
|
if is_first_contribution
|
|
|
|
NotificationMailer
|
|
|
|
.with(contribution: contribution)
|
|
|
|
.first_payment_confirmation
|
|
|
|
.deliver_now!
|
|
|
|
else
|
|
|
|
NotificationMailer
|
|
|
|
.with(contribution: contribution)
|
|
|
|
.payment_renewal_confirmation
|
|
|
|
.deliver_now!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-25 12:48:46 +00:00
|
|
|
def handle_new_contribution(contribution, overriden_expires_on)
|
|
|
|
if joined_on.nil?
|
|
|
|
self.joined_on = contribution.payment_on
|
|
|
|
self.expires_on = overriden_expires_on.presence || (joined_on + 1.year)
|
|
|
|
else
|
|
|
|
self.expires_on = overriden_expires_on.presence || expires_on + 1.year
|
|
|
|
end
|
|
|
|
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
2023-01-24 23:06:12 +00:00
|
|
|
def regenerate_notifications(from=Date.today)
|
2022-06-25 12:48:46 +00:00
|
|
|
notifications.where(status: 'scheduled').delete_all
|
|
|
|
|
2023-01-24 18:26:10 +00:00
|
|
|
dates = notifications.pluck(:to_be_sent_on)
|
|
|
|
|
2022-06-25 12:48:46 +00:00
|
|
|
return if expires_on.nil?
|
|
|
|
|
|
|
|
[
|
2022-10-18 10:29:15 +00:00
|
|
|
{ to_be_sent_on: expires_on - 60.days, template: "expiration_in_60d" },
|
2022-06-25 12:48:46 +00:00
|
|
|
{ to_be_sent_on: expires_on - 30.days, template: "expiration_in_30d" },
|
|
|
|
{ to_be_sent_on: expires_on + 0.days, template: "expired" },
|
|
|
|
{ to_be_sent_on: expires_on + 30.days, template: "expired_30d_ago" },
|
|
|
|
{ to_be_sent_on: expires_on + 60.days, template: "expired_60d_ago" },
|
2023-09-20 13:19:35 +00:00
|
|
|
{ to_be_sent_on: expires_on + Config.payment_pending_grace_period, template: "cancelled" },
|
2023-01-24 18:26:10 +00:00
|
|
|
].reject do |n|
|
2023-01-24 23:06:12 +00:00
|
|
|
n[:to_be_sent_on].before?(from) || dates.include?(n[:to_be_sent_on])
|
2023-01-24 18:26:10 +00:00
|
|
|
end.each do |n|
|
2022-06-25 12:48:46 +00:00
|
|
|
notifications.create(n.merge(status: "scheduled"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_missing_ifthenpay_links!
|
|
|
|
self.regular_ifthenpay_link = IfThenPay.generate_gateway_link(
|
|
|
|
id: number,
|
2023-07-12 18:13:08 +00:00
|
|
|
amount: "%.2f" % Config.regular_payment_value,
|
|
|
|
description: Config.ifthenpay_payment_title,
|
2022-06-25 12:48:46 +00:00
|
|
|
) unless self.regular_ifthenpay_link.present?
|
|
|
|
|
|
|
|
self.reduced_ifthenpay_link = IfThenPay.generate_gateway_link(
|
|
|
|
id: number,
|
2023-07-12 18:13:08 +00:00
|
|
|
amount: "%.2f" % Config.reduced_payment_value,
|
|
|
|
description: Config.ifthenpay_payment_title,
|
2022-06-25 12:48:46 +00:00
|
|
|
) unless self.reduced_ifthenpay_link.present?
|
|
|
|
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset_all_status!
|
|
|
|
Member.all.each do |member|
|
|
|
|
member.reset_status!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.generate_all_missing_ifthenpay_links!
|
|
|
|
Member.all.each do |member|
|
|
|
|
member.generate_missing_ifthenpay_links!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.regenerate_all_notifications
|
2023-04-05 22:04:48 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
ActiveRecord::Base.connection.execute('LOCK members IN ACCESS EXCLUSIVE MODE')
|
|
|
|
|
|
|
|
Member.all.each do |member|
|
|
|
|
member.regenerate_notifications
|
|
|
|
end
|
2022-06-25 12:48:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|