Commit a4000dab authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻 Committed by Kaspar
Browse files

Refactored the way amount of billing expenses is calculated to...

...be calculated automaticly before_validation
parent fa94c708
class BillingExpensesController < ApplicationController
before_action :set_billing_expense, only: [:show, :destroy]
before_action :set_volunteer
before_action :set_volunteer_hours, only: :create
after_action :volunteer_hours_update, only: :create
def index
authorize BillingExpense
......@@ -13,10 +11,7 @@ class BillingExpensesController < ApplicationController
respond_to do |format|
format.html
format.pdf do
render pdf:
"Spesenauszahlung-#{@volunteer.contact.first_name}-#{@volunteer.contact.last_name}-#{@volunteer.hours.last.meeting_date}",
template: 'billing_expenses/show.html.slim',
layout: 'pdf.pdf', encoding: 'UTF-8'
render pdf: file_name, layout: 'pdf.pdf', encoding: 'UTF-8'
end
end
end
......@@ -29,8 +24,9 @@ class BillingExpensesController < ApplicationController
def create
@billing_expense = BillingExpense.new(
billing_expense_params.merge(@volunteer.slice(:bank, :iban))
.merge(amount: compute_hours, user_id: current_user.id)
)
@billing_expense.hours = @volunteer.hours.billable
@billing_expense.user = current_user
authorize @billing_expense
if @billing_expense.save
redirect_to volunteer_billing_expenses_url, make_notice
......@@ -55,27 +51,8 @@ class BillingExpensesController < ApplicationController
@volunteer = Volunteer.find(params[:volunteer_id]) if params[:volunteer_id]
end
def set_volunteer_hours
@volunteer_hours = @volunteer.hours.where(billing_expense: nil)
end
def compute_hours
return if @volunteer_hours.empty?
compute_amount(@volunteer_hours.sum(&:hours) + @volunteer_hours.sum(&:minutes)/60)
end
def compute_amount(hours)
if hours < 25
50
elsif hours < 50
100
else
150
end
end
def volunteer_hours_update
@volunteer_hours.update(billing_expense_id: @billing_expense.id)
def file_name
[@volunteer.contact.full_name, @volunteer.hours.maximum(:meeting_date)].join('-').parameterize
end
def billing_expense_params
......
class BillingExpense < ApplicationRecord
include FullBankDetails
before_validation :compute_amount
belongs_to :volunteer
belongs_to :user, -> { with_deleted }
has_many :hours
default_scope { order(created_at: :desc) }
AMOUNT = [50, 100, 150].freeze
AMOUNT = [0, 50, 100, 150].freeze
validates :amount, inclusion: { in: AMOUNT }
......@@ -19,4 +20,25 @@ class BillingExpense < ApplicationRecord
def billed_hours
Hour.where(billing_expense: id)
end
def amount=(_value)
super(compute_amount)
end
def compute_amount
hour_count = id ? hours_sum : volunteer.billable_hours_sum
if hour_count > 50
150
elsif hour_count > 25
100
elsif hour_count >= 1
50
else
0
end
end
def hours_sum
hours.sum(&:hours) + hours.sum(&:minutes) / 60
end
end
......@@ -8,6 +8,8 @@ class Hour < ApplicationRecord
validates :hours, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :meeting_date, presence: true
scope :billable, (-> { where(billing_expense: nil) })
HOUR_RANGE = (1..8).to_a
MINUTE_RANGE = [0, 15, 30, 45].freeze
end
......@@ -71,6 +71,10 @@ class Volunteer < ApplicationRecord
scope :with_hours, (-> { joins(:hours).distinct })
def billable_hours_sum
hours.billable.sum(&:hours) + hours.billable.sum(&:minutes) / 60
end
def hours_sum
hours.sum(&:hours) + hours.sum(&:minutes) / 60
end
......
= render 'billing_expenses/show.html.slim'
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