Verified Commit 53523e37 authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻
Browse files

add billing_expense_utils model concern for BillingExpense and Volunteer model

parent b8025f33
class BillingExpense < ApplicationRecord
include ImportRelation
include FullBankDetails
include BillingExpenseSemesterUtils
SEMESTER_LENGTH = 6.months
......@@ -98,14 +99,6 @@ class BillingExpense < ApplicationRecord
semesters
end
def self.semester_of_year(date)
if (6..11).cover? date.month
2
else
1
end
end
def final_amount
overwritten_amount.presence || amount
end
......
module BillingExpenseSemesterUtils
extend ActiveSupport::Concern
included do # rubocop:disable Metrics/BlockLength
def self.semester_of_year(date)
if (6..11).cover? date.month
2
else
1
end
end
def self.semester_display_year(date)
if date.month == 12
date.year + 1
else
date.year
end
end
def self.semester_from_hours(scope, date_position: :maximum)
hours = Hour.public_send(scope)
if hours.blank?
current_semester_start
elsif hours.count > 1
dates_semester_start(hours.public_send(date_position, :meeting_date))
else
dates_semester_start(hours.first.meeting_date)
end
end
def self.billing_expense_semester(date)
if date.blank?
current_semester_start
else
billable_semester_date(date)
end
end
def self.billable_semester_date(date)
if date.blank?
nil
elsif date.is_a?(Time)
date.beginning_of_day
else
Time.zone.parse(date).beginning_of_day
end
end
def self.dates_semester_start(date)
if semester_of_year(date) == 2
Time.zone.parse("#{date.year}-06-01").beginning_of_day
elsif date.month == 12
Time.zone.parse("#{date.year}-12-01").beginning_of_day
else
Time.zone.parse("#{date.year - 1}-12-01").beginning_of_day
end
end
def self.current_semester_start
@current_semester_start ||= dates_semester_start(Time.zone.now)
end
end
end
......@@ -6,6 +6,7 @@ class Volunteer < ApplicationRecord
include ImportRelation
include FullBankDetails
include AcceptanceAttributes
include BillingExpenseSemesterUtils
before_validation :handle_user_with_external_change, if: :external_changed?
before_save :build_user_if_accepted_or_import_invited
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment