From c78e010888b4672cc4586046687d96468464667b Mon Sep 17 00:00:00 2001 From: Nicolas Florentin Date: Fri, 8 Apr 2016 11:12:03 +0200 Subject: [PATCH] refacto method generate_invoice_items using UsersCredits::Manager service --- app/models/reservation.rb | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/app/models/reservation.rb b/app/models/reservation.rb index 04e493c37..ea31b7158 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -73,28 +73,12 @@ class Reservation < ActiveRecord::Base # === Training reservation === when Training - # TO BE REFACTORED WHEN PLAN PURCHASE AND RESERVATION PURCHASE WILL BE DECOUPLED - base_amount = reservable.amount_by_group(user.group_id).amount - if plan - # Return True if the subscription link a training credit for training reserved by the user - training_is_creditable = plan.training_credits.select {|credit| credit.creditable_id == reservable.id}.size > 0 - # Training reserved by the user is free when : + # be careful, variable plan can be the user's plan OR the plan user is currently purchasing + users_credits_manager = UsersCredits::Manager.new(reservation: self, plan: plan) + base_amount = 0 if users_credits_manager.will_use_credits? - # |-> the user already has a current subscription and if training_is_creditable is true and has at least one credit available. - if !new_plan_being_bought - if user.training_credits.size < plan.training_credit_nb and training_is_creditable - base_amount = 0 - end - # |-> the user buys a new subscription and if training_is_creditable is true. - else - if training_is_creditable - base_amount = 0 - end - end - - end slots.each do |slot| description = reservable.name + " #{I18n.l slot.start_at, format: :long} - #{I18n.l slot.end_at, format: :hour_minute}" ii_amount = base_amount