160 lines
4.1 KiB
Ruby
160 lines
4.1 KiB
Ruby
class Member < ApplicationRecord
|
|
default_scope { where(excluded: false) }
|
|
has_many :contributions
|
|
has_many :notifications
|
|
has_many :payments
|
|
has_paper_trail
|
|
|
|
def self.ransackable_attributes(auth_object = nil)
|
|
%w[display_name legal_name email identification_number]
|
|
end
|
|
|
|
def create_payment
|
|
payments.create(status: "pending")
|
|
end
|
|
|
|
def cancelled_on
|
|
expires_on + 90.days
|
|
end
|
|
|
|
def obtains_full_rights_on
|
|
joined_on + 6.months
|
|
end
|
|
|
|
def reset_status!
|
|
update(status: expected_status)
|
|
end
|
|
|
|
def employed?
|
|
category == "normal" || category == "employed"
|
|
end
|
|
|
|
def remove_personal_information!
|
|
update(
|
|
excluded: true,
|
|
display_name: "",
|
|
legal_name: "",
|
|
pronouns: "",
|
|
email: nil,
|
|
identification_number: "",
|
|
category: "",
|
|
address: "",
|
|
)
|
|
end
|
|
|
|
def expected_status
|
|
if joined_on.nil?
|
|
:pending
|
|
elsif obtains_full_rights_on.future?
|
|
:passive
|
|
elsif expires_on.future?
|
|
:active
|
|
elsif cancelled_on.future?
|
|
:expired
|
|
else
|
|
:cancelled
|
|
end
|
|
end
|
|
|
|
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
|
|
|
|
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
|
|
|
|
def regenerate_notifications(from=Date.today)
|
|
notifications.where(status: 'scheduled').delete_all
|
|
|
|
dates = notifications.pluck(:to_be_sent_on)
|
|
|
|
return if expires_on.nil?
|
|
|
|
[
|
|
{ to_be_sent_on: expires_on - 60.days, template: "expiration_in_60d" },
|
|
{ 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" },
|
|
{ to_be_sent_on: expires_on + 90.days, template: "cancelled" },
|
|
].reject do |n|
|
|
n[:to_be_sent_on].before?(from) || dates.include?(n[:to_be_sent_on])
|
|
end.each do |n|
|
|
notifications.create(n.merge(status: "scheduled"))
|
|
end
|
|
end
|
|
|
|
def generate_missing_ifthenpay_links!
|
|
self.regular_ifthenpay_link = IfThenPay.generate_gateway_link(
|
|
id: number,
|
|
amount: "%.2f" % Config.regular_payment_value,
|
|
description: Config.ifthenpay_payment_title,
|
|
) unless self.regular_ifthenpay_link.present?
|
|
|
|
self.reduced_ifthenpay_link = IfThenPay.generate_gateway_link(
|
|
id: number,
|
|
amount: "%.2f" % Config.reduced_payment_value,
|
|
description: Config.ifthenpay_payment_title,
|
|
) 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
|
|
ActiveRecord::Base.transaction do
|
|
ActiveRecord::Base.connection.execute('LOCK members IN ACCESS EXCLUSIVE MODE')
|
|
|
|
Member.all.each do |member|
|
|
member.regenerate_notifications
|
|
end
|
|
end
|
|
end
|
|
end
|