Class: Workshop

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Includes:
DmCore::Concerns::DefinesCustomFields, DmCore::Concerns::FriendlyId
Defined in:
app/models/workshop.rb

Constant Summary collapse

EVENT_STYLES =
[['Workshop', 'workshop'], ['Crowdfunding', 'crowdfunding']]

Instance Method Summary collapse

Instance Method Details

#archived?Boolean


Returns:

  • (Boolean)


107
108
109
# File 'app/models/workshop.rb', line 107

def archived?
  self.archived_on ? true : false
end

#crowdfunding?Boolean


Returns:

  • (Boolean)


112
113
114
# File 'app/models/workshop.rb', line 112

def crowdfunding?
  self.event_style == 'crowdfunding'
end

#financial_details(level = :detail) ⇒ Object

Return financial summary and details level => :summary or :detail




119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/workshop.rb', line 119

def financial_details(level = :detail)
  #--- pick currency of first price
  financials = {summary: { total_possible: Money.new(0, base_currency),          total_possible_worst: Money.new(0, base_currency),
                              total_paid: Money.new(0, base_currency),              total_outstanding: Money.new(0, base_currency), 
                              total_outstanding_worst: Money.new(0, base_currency), total_discounts: Money.new(0, base_currency),
                              total_paid_percent: 0},
                collected: {},
                collected_monthly: {},
                payment_type: {},
                projected: {}
               }

  registrations.attending.includes(:workshop_price).each do |registration|
    if registration.workshop_price
      #--- Calculate the summary values
      financials[:summary][:total_possible]     += registration.discounted_price
      financials[:summary][:total_paid]         += registration.amount_paid.nil? ? Money.new(0, base_currency) : registration.amount_paid
      financials[:summary][:total_outstanding]  += registration.balance_owed
      financials[:summary][:total_discoutns]    += registration.discount
    end
  end

  if level == :detail
    registrations.attending.includes(:workshop_price, :payment_histories).each do |registration|
      if registration.workshop_price
        #--- Calculate what has been collected, by payment method
        registration.payment_histories.each do |payment_history|
          payment_method = payment_history.payment_method.titlecase
          financials[:collected]["#{payment_method}"] = Money.new(0, base_currency) if financials[:collected]["#{payment_method}"].nil?
          financials[:collected]["#{payment_method}"] += payment_history.total
    
          month = payment_history.payment_date.beginning_of_month
          financials[:collected_monthly][month] = Money.new(0, base_currency) if financials[:collected_monthly][month].nil?
          financials[:collected_monthly][month] += payment_history.total
        end
      end
    end
  end

  #--- give a worst case value - reduce by 20%
  financials[:summary][:total_possible_worst]               = financials[:summary][:total_possible] * 0.80
  financials[:summary][:total_outstanding_worst]            = financials[:summary][:total_outstanding] * 0.80
  financials[:summary][:total_paid_percent]                 = (100 * financials[:summary][:total_paid] / financials[:summary][:total_possible]).round if financials[:summary][:total_possible].positive?
  return financials
end

#header_accent_color(default = '') ⇒ Object




225
226
227
# File 'app/models/workshop.rb', line 225

def header_accent_color(default = '')
  self.preferred_header_accent_color || default
end

#header_image(default = nil) ⇒ Object




220
221
222
# File 'app/models/workshop.rb', line 220

def header_image(default = nil)
  self.image || default
end

#lost_users(days_ago = 10) ⇒ Object

Find list of newly createdusers that have not registered for any events, between the end of the workshop and up to 60 day before the start of the workshop. Not perfect, since people can register just to access special content. But gives rough idea of people creating an account but not realizing they need to register for the event they want to participate in.




235
236
237
238
239
240
241
242
243
244
245
246
# File 'app/models/workshop.rb', line 235

def lost_users(days_ago = 10)
  lost = []
  new_users = User.where(created_at: (self.starting_on - days_ago.day)..self.ending_on, account_id: Account.current.id)
  new_users.each do |user|
    if user.user_site_profiles.where(account_id: Account.current.id)
      if user..registrations.count == 0
        lost << user
      end
    end
  end
  lost
end

#member?(user) ⇒ Boolean

is the passed in user attending? Used in some deep level authorization checks, which rely on the “member?” method. This does not consider a userless registration as a “member”, since there is no way they can login


Returns:

  • (Boolean)


203
204
205
# File 'app/models/workshop.rb', line 203

def member?(user)
  self.registrations.attending.where(user_profile_id: user..id).count > 0
end

#member_countObject

Provide a list of users that are members (does not include userless registrations)




209
210
211
# File 'app/models/workshop.rb', line 209

def member_count
  self.registrations.attending.joins(user_profile: [:user]).references(:user_profile).where('user_profiles.user_id IS NOT NULL').count
end

#member_listObject

Return list of Users that are attending (does not include userless registrations)




215
216
217
# File 'app/models/workshop.rb', line 215

def member_list
  User.includes(:user_profile).references(:user_profile).where(user_profiles: { id: self.registrations.attending.map(&:user_profile_id) } )
end

#model_slugObject




66
67
68
# File 'app/models/workshop.rb', line 66

def model_slug
  send("title_#{Account.current.preferred_default_locale}")
end

#past?Boolean

Is this workshop in the past?


Returns:

  • (Boolean)


80
81
82
# File 'app/models/workshop.rb', line 80

def past?
  ending_on < Time.now
end

#price_sold_out?(workshop_price) ⇒ Boolean

If the total_available is nil, then there are unlimited tickets to be sold.

Otherwise, check if we have sold out


Returns:

  • (Boolean)


73
74
75
76
# File 'app/models/workshop.rb', line 73

def price_sold_out?(workshop_price)
  # p.sold_out?(@workshop.event_registration.number_of(:registrations_by_paymenttype, payment_id: p.id)
  false # TODO
end

#published?Boolean


Returns:

  • (Boolean)


96
97
98
# File 'app/models/workshop.rb', line 96

def published?
  published
end

#registration_closed?Boolean

Is the registration closed? If deadline is null, then registration is open ended


Returns:

  • (Boolean)


91
92
93
# File 'app/models/workshop.rb', line 91

def registration_closed?
  !published? || (deadline_on ? (deadline_on < Time.now.to_date) : false)
end

#send_payment_reminder_emails(registration_id = 'all') ⇒ Object

Send out payment reminder emails to unpaid attendees, or to a specific one. if a specific registration, then always send out the email




179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/models/workshop.rb', line 179

def send_payment_reminder_emails(registration_id = 'all')
  success     = failed = 0
  unpaid_list = ( registration_id == 'all' ? registrations.unpaid : registrations.unpaid.where(id: registration_id) )
  unpaid_list.each do |registration|
    if (registration.payment_reminder_due? && registration.payment_owed.positive?) || registration_id != 'all'
      email = PaymentReminderMailer.payment_reminder(registration).deliver_now
      if email
        registration.update_attribute(:payment_reminder_sent_on, Time.now)
        registration.update_attribute(:payment_reminder_history,  [Time.now] + registration.payment_reminder_history)
        success += 1
      else
        failed += 1
      end
    end
  end
  return {success: success, failed: failed}
end

#show_social_buttons?Boolean


Returns:

  • (Boolean)


85
86
87
# File 'app/models/workshop.rb', line 85

def show_social_buttons?
  preferred_show_social_buttons?
end

#toggle_archiveObject

toggle the archive state of the workshop




102
103
104
# File 'app/models/workshop.rb', line 102

def toggle_archive
  archived_on ? update_attribute(:archived_on, nil) : update_attribute(:archived_on, Time.now)
end

#total_paidObject




166
167
168
169
170
171
172
173
174
# File 'app/models/workshop.rb', line 166

def total_paid
  total_paid = Money.new(0, base_currency)
  registrations.attending.each do |registration|
    if registration.workshop_price
      total_paid += registration.amount_paid.nil? ? Money.new(0, base_currency) : registration.amount_paid
    end
  end
  return total_paid
end