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 + Config.payment_pending_grace_period end def obtains_full_rights_on joined_on + Config.full_rights_vesting_period end def reset_status! update(status: expected_status) end def employed? # normal is deprecated, here for retrocompatibility reasons 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! if should_send_notification if is_first_contribution NotificationMailer .with(contribution: contribution) .first_payment_confirmation .deliver_now! self.notifications.create!( template: "first_payment_confirmation", to_be_sent_on: Date.today, sent_at: Time.current, status: "sent", ) else NotificationMailer .with(contribution: contribution) .payment_renewal_confirmation .deliver_now! self.notifications.create!( template: "payment_renewal_confirmation", to_be_sent_on: Date.today, sent_at: Time.current, status: "sent", ) 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 todays_notification candidate = Notification .templates .filter { |t| self.expires_on + t[:to_be_sent_on] <= Date.today } .last if candidate last_sent = self .notifications .where(status: 'sent', template: candidate[:template]) .where.not(sent_at: nil) .order(:sent_at) .last if last_sent && self.expires_on + candidate[:to_be_sent_on] <= last_sent.sent_at nil else self.notifications.new( template: candidate[:template], status: 'scheduled', to_be_sent_on: Date.today, ) end 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(&:reset_status!) end end