2016-03-23 18:39:41 +01:00
|
|
|
class Reservation < ActiveRecord::Base
|
|
|
|
include NotifyWith::NotificationAttachedObject
|
|
|
|
|
|
|
|
belongs_to :user
|
2017-02-27 13:15:31 +01:00
|
|
|
|
|
|
|
has_many :slots_reservations, dependent: :destroy
|
|
|
|
has_many :slots, through: :slots_reservations
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
accepts_nested_attributes_for :slots, allow_destroy: true
|
|
|
|
belongs_to :reservable, polymorphic: true
|
|
|
|
|
2016-08-25 18:41:33 +02:00
|
|
|
has_many :tickets
|
|
|
|
accepts_nested_attributes_for :tickets, allow_destroy: false
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
has_one :invoice, -> {where(type: nil)}, as: :invoiced, dependent: :destroy
|
|
|
|
|
|
|
|
validates_presence_of :reservable_id, :reservable_type
|
|
|
|
validate :machine_not_already_reserved, if: -> { self.reservable.is_a?(Machine) }
|
|
|
|
validate :training_not_fully_reserved, if: -> { self.reservable.is_a?(Training) }
|
|
|
|
|
|
|
|
attr_accessor :card_token, :plan_id, :subscription
|
|
|
|
|
|
|
|
after_commit :notify_member_create_reservation, on: :create
|
|
|
|
after_commit :notify_admin_member_create_reservation, on: :create
|
|
|
|
after_save :update_event_nb_free_places, if: Proc.new { |reservation| reservation.reservable_type == 'Event' }
|
2016-07-08 17:24:51 +02:00
|
|
|
after_create :debit_user_wallet
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2016-08-10 17:37:17 +02:00
|
|
|
##
|
2016-03-23 18:39:41 +01:00
|
|
|
# Generate an array of {Stripe::InvoiceItem} with the elements in the current reservation, price included.
|
|
|
|
# The training/machine price is depending of the member's group, subscription and credits already used
|
|
|
|
# @param on_site {Boolean} true if an admin triggered the call
|
2016-08-10 17:37:17 +02:00
|
|
|
# @param coupon_code {String} pass a valid code to appy a coupon
|
|
|
|
##
|
|
|
|
def generate_invoice_items(on_site = false, coupon_code = nil)
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
# returning array
|
|
|
|
invoice_items = []
|
|
|
|
|
|
|
|
# prepare the plan
|
|
|
|
if user.subscribed_plan
|
|
|
|
plan = user.subscribed_plan
|
|
|
|
new_plan_being_bought = false
|
|
|
|
elsif plan_id
|
|
|
|
plan = Plan.find(plan_id)
|
|
|
|
new_plan_being_bought = true
|
|
|
|
else
|
|
|
|
plan = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
case reservable
|
|
|
|
|
|
|
|
# === Machine reservation ===
|
|
|
|
when Machine
|
|
|
|
base_amount = reservable.prices.find_by(group_id: user.group_id, plan_id: plan.try(:id)).amount
|
2016-04-08 11:17:52 +02:00
|
|
|
users_credits_manager = UsersCredits::Manager.new(reservation: self, plan: plan)
|
2016-04-07 16:02:09 +02:00
|
|
|
|
|
|
|
slots.each_with_index do |slot, index|
|
|
|
|
description = reservable.name + " #{I18n.l slot.start_at, format: :long} - #{I18n.l slot.end_at, format: :hour_minute}"
|
|
|
|
|
|
|
|
ii_amount = base_amount # ii_amount default to base_amount
|
|
|
|
|
|
|
|
if users_credits_manager.will_use_credits?
|
|
|
|
ii_amount = (index < users_credits_manager.free_hours_count) ? 0 : base_amount
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2016-04-07 16:02:09 +02:00
|
|
|
|
|
|
|
ii_amount = 0 if slot.offered and on_site # if it's a local payment and slot is offered free
|
|
|
|
|
|
|
|
unless on_site # if it's local payment then do not create Stripe::InvoiceItem
|
|
|
|
ii = Stripe::InvoiceItem.create(
|
|
|
|
customer: user.stp_customer_id,
|
|
|
|
amount: ii_amount,
|
|
|
|
currency: Rails.application.secrets.stripe_currency,
|
|
|
|
description: description
|
|
|
|
)
|
|
|
|
invoice_items << ii
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2016-04-07 16:02:09 +02:00
|
|
|
self.invoice.invoice_items.push InvoiceItem.new(amount: ii_amount, stp_invoice_item_id: (ii.id if ii), description: description)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2016-04-07 16:02:09 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
# === Training reservation ===
|
|
|
|
when Training
|
|
|
|
base_amount = reservable.amount_by_group(user.group_id).amount
|
|
|
|
|
2016-04-08 11:12:03 +02:00
|
|
|
# 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?
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
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
|
|
|
|
ii_amount = 0 if (slot.offered and on_site)
|
|
|
|
unless on_site
|
|
|
|
ii = Stripe::InvoiceItem.create(
|
|
|
|
customer: user.stp_customer_id,
|
|
|
|
amount: ii_amount,
|
|
|
|
currency: Rails.application.secrets.stripe_currency,
|
|
|
|
description: description
|
|
|
|
)
|
|
|
|
invoice_items << ii
|
|
|
|
end
|
|
|
|
self.invoice.invoice_items.push InvoiceItem.new(amount: ii_amount, stp_invoice_item_id: (ii.id if ii), description: description)
|
|
|
|
end
|
|
|
|
|
|
|
|
# === Event reservation ===
|
|
|
|
when Event
|
2016-08-29 15:43:47 +02:00
|
|
|
amount = reservable.amount * nb_reserve_places
|
|
|
|
tickets.each do |ticket|
|
|
|
|
amount += ticket.booked * ticket.event_price_category.amount
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
slots.each do |slot|
|
2016-11-03 12:17:19 +01:00
|
|
|
description = "#{reservable.name} "
|
|
|
|
(slot.start_at.to_date..slot.end_at.to_date).each do |d|
|
|
|
|
description += "\n" if slot.start_at.to_date != slot.end_at.to_date
|
|
|
|
description += "#{I18n.l d, format: :long} #{I18n.l slot.start_at, format: :hour_minute} - #{I18n.l slot.end_at, format: :hour_minute}"
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
ii_amount = amount
|
|
|
|
ii_amount = 0 if (slot.offered and on_site)
|
|
|
|
unless on_site
|
|
|
|
ii = Stripe::InvoiceItem.create(
|
|
|
|
customer: user.stp_customer_id,
|
|
|
|
amount: ii_amount,
|
|
|
|
currency: Rails.application.secrets.stripe_currency,
|
|
|
|
description: description
|
|
|
|
)
|
|
|
|
invoice_items << ii
|
|
|
|
end
|
|
|
|
self.invoice.invoice_items.push InvoiceItem.new(amount: ii_amount, stp_invoice_item_id: (ii.id if ii), description: description)
|
2017-02-23 17:45:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# === Space reservation ===
|
|
|
|
when Space
|
|
|
|
base_amount = reservable.prices.find_by(group_id: user.group_id, plan_id: plan.try(:id)).amount
|
2017-02-27 17:38:15 +01:00
|
|
|
users_credits_manager = UsersCredits::Manager.new(reservation: self, plan: plan)
|
2017-02-23 17:45:55 +01:00
|
|
|
|
|
|
|
slots.each_with_index do |slot, index|
|
|
|
|
description = reservable.name + " #{I18n.l slot.start_at, format: :long} - #{I18n.l slot.end_at, format: :hour_minute}"
|
|
|
|
|
|
|
|
ii_amount = base_amount # ii_amount default to base_amount
|
2017-02-27 17:38:15 +01:00
|
|
|
|
|
|
|
if users_credits_manager.will_use_credits?
|
|
|
|
ii_amount = (index < users_credits_manager.free_hours_count) ? 0 : base_amount
|
|
|
|
end
|
|
|
|
|
2017-02-23 17:45:55 +01:00
|
|
|
ii_amount = 0 if slot.offered and on_site # if it's a local payment and slot is offered free
|
|
|
|
|
|
|
|
unless on_site # if it's local payment then do not create Stripe::InvoiceItem
|
|
|
|
ii = Stripe::InvoiceItem.create(
|
|
|
|
customer: user.stp_customer_id,
|
|
|
|
amount: ii_amount,
|
|
|
|
currency: Rails.application.secrets.stripe_currency,
|
|
|
|
description: description
|
|
|
|
)
|
|
|
|
invoice_items << ii
|
|
|
|
end
|
|
|
|
self.invoice.invoice_items.push InvoiceItem.new(amount: ii_amount, stp_invoice_item_id: (ii.id if ii), description: description)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# === Unknown reservation type ===
|
|
|
|
else
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-08-10 17:37:17 +02:00
|
|
|
# === Coupon ===
|
|
|
|
unless coupon_code.nil?
|
2016-11-24 17:57:48 +01:00
|
|
|
@coupon = Coupon.find_by(code: coupon_code)
|
|
|
|
if not @coupon.nil? and @coupon.status(user.id) == 'active'
|
2016-11-29 11:12:58 +01:00
|
|
|
total = get_cart_total
|
2016-11-24 17:57:48 +01:00
|
|
|
|
|
|
|
discount = 0
|
|
|
|
if @coupon.type == 'percent_off'
|
|
|
|
discount = (total * @coupon.percent_off / 100).to_i
|
|
|
|
elsif @coupon.type == 'amount_off'
|
|
|
|
discount = @coupon.amount_off
|
|
|
|
else
|
|
|
|
raise InvalidCouponError
|
|
|
|
end
|
|
|
|
|
2016-09-08 15:19:12 +02:00
|
|
|
unless on_site
|
|
|
|
invoice_items << Stripe::InvoiceItem.create(
|
|
|
|
customer: user.stp_customer_id,
|
2016-11-24 17:57:48 +01:00
|
|
|
amount: -discount,
|
2016-09-08 15:19:12 +02:00
|
|
|
currency: Rails.application.secrets.stripe_currency,
|
2016-11-29 11:12:58 +01:00
|
|
|
description: "coupon #{@coupon.code}"
|
2016-09-08 15:19:12 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise InvalidCouponError
|
2016-08-30 09:47:03 +02:00
|
|
|
end
|
2016-08-10 17:37:17 +02:00
|
|
|
end
|
|
|
|
|
2016-07-08 17:24:51 +02:00
|
|
|
@wallet_amount_debit = get_wallet_amount_debit
|
|
|
|
if @wallet_amount_debit != 0 and !on_site
|
2016-07-07 15:57:06 +02:00
|
|
|
invoice_items << Stripe::InvoiceItem.create(
|
|
|
|
customer: user.stp_customer_id,
|
|
|
|
amount: -@wallet_amount_debit,
|
|
|
|
currency: Rails.application.secrets.stripe_currency,
|
|
|
|
description: "wallet -#{@wallet_amount_debit / 100.0}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
# let's return the resulting array of items
|
|
|
|
invoice_items
|
|
|
|
end
|
|
|
|
|
2016-08-10 15:34:47 +02:00
|
|
|
def save_with_payment(coupon_code = nil)
|
2016-03-23 18:39:41 +01:00
|
|
|
build_invoice(user: user)
|
2016-08-10 17:37:17 +02:00
|
|
|
invoice_items = generate_invoice_items(false, coupon_code)
|
2016-03-23 18:39:41 +01:00
|
|
|
if valid?
|
|
|
|
# TODO: refactoring
|
|
|
|
customer = Stripe::Customer.retrieve(user.stp_customer_id)
|
|
|
|
if plan_id
|
|
|
|
self.subscription = Subscription.find_or_initialize_by(user_id: user.id)
|
|
|
|
self.subscription.attributes = {plan_id: plan_id, user_id: user.id, card_token: card_token, expired_at: nil}
|
2016-11-29 11:12:58 +01:00
|
|
|
if subscription.save_with_payment(false)
|
2016-03-23 18:39:41 +01:00
|
|
|
self.stp_invoice_id = invoice_items.first.refresh.invoice
|
|
|
|
self.invoice.stp_invoice_id = invoice_items.first.refresh.invoice
|
|
|
|
self.invoice.invoice_items.push InvoiceItem.new(amount: subscription.plan.amount, stp_invoice_item_id: subscription.stp_subscription_id, description: subscription.plan.name, subscription_id: subscription.id)
|
2016-08-10 16:33:26 +02:00
|
|
|
set_total_and_coupon(coupon_code)
|
2016-03-23 18:39:41 +01:00
|
|
|
save!
|
2016-04-08 16:59:09 +02:00
|
|
|
#
|
|
|
|
# IMPORTANT NOTE: here, we don't have to create a stripe::invoice and pay it
|
|
|
|
# because subscription.create (in subscription.rb) will pay all waiting stripe invoice items
|
|
|
|
#
|
2016-03-23 18:39:41 +01:00
|
|
|
else
|
|
|
|
# error handling
|
|
|
|
invoice_items.each(&:delete)
|
|
|
|
errors[:card] << subscription.errors[:card].join
|
|
|
|
if subscription.errors[:payment]
|
|
|
|
errors[:payment] << subscription.errors[:payment].join
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
if invoice_items.map(&:amount).map(&:to_i).reduce(:+) > 0
|
|
|
|
card = customer.sources.create(card: card_token)
|
|
|
|
if customer.default_source.present?
|
|
|
|
customer.default_source = card.id
|
|
|
|
customer.save
|
|
|
|
end
|
|
|
|
end
|
2016-04-08 16:59:09 +02:00
|
|
|
#
|
|
|
|
# IMPORTANT NOTE: here, we have to create an invoice manually and pay it to pay all waiting stripe invoice items
|
|
|
|
#
|
2016-07-07 15:57:06 +02:00
|
|
|
stp_invoice = Stripe::Invoice.create(
|
2016-03-23 18:39:41 +01:00
|
|
|
customer: user.stp_customer_id,
|
|
|
|
)
|
2016-07-07 15:57:06 +02:00
|
|
|
stp_invoice.pay
|
2016-03-23 18:39:41 +01:00
|
|
|
card.delete if card
|
2016-07-07 15:57:06 +02:00
|
|
|
self.stp_invoice_id = stp_invoice.id
|
|
|
|
self.invoice.stp_invoice_id = stp_invoice.id
|
2016-08-10 16:33:26 +02:00
|
|
|
set_total_and_coupon(coupon_code)
|
2016-03-23 18:39:41 +01:00
|
|
|
save!
|
|
|
|
rescue Stripe::CardError => card_error
|
2016-07-07 15:57:06 +02:00
|
|
|
clear_payment_info(card, stp_invoice, invoice_items)
|
2016-03-23 18:39:41 +01:00
|
|
|
logger.info card_error
|
|
|
|
errors[:card] << card_error.message
|
|
|
|
return false
|
|
|
|
rescue Stripe::InvalidRequestError => e
|
|
|
|
# Invalid parameters were supplied to Stripe's API
|
2016-07-07 15:57:06 +02:00
|
|
|
clear_payment_info(card, stp_invoice, invoice_items)
|
2016-03-23 18:39:41 +01:00
|
|
|
logger.error e
|
|
|
|
errors[:payment] << e.message
|
|
|
|
return false
|
|
|
|
rescue Stripe::AuthenticationError => e
|
|
|
|
# Authentication with Stripe's API failed
|
|
|
|
# (maybe you changed API keys recently)
|
2016-07-07 15:57:06 +02:00
|
|
|
clear_payment_info(card, stp_invoice, invoice_items)
|
2016-03-23 18:39:41 +01:00
|
|
|
logger.error e
|
|
|
|
errors[:payment] << e.message
|
|
|
|
return false
|
|
|
|
rescue Stripe::APIConnectionError => e
|
|
|
|
# Network communication with Stripe failed
|
2016-07-07 15:57:06 +02:00
|
|
|
clear_payment_info(card, stp_invoice, invoice_items)
|
2016-03-23 18:39:41 +01:00
|
|
|
logger.error e
|
|
|
|
errors[:payment] << e.message
|
|
|
|
return false
|
|
|
|
rescue Stripe::StripeError => e
|
|
|
|
# Display a very generic error to the user, and maybe send
|
|
|
|
# yourself an email
|
2016-07-07 15:57:06 +02:00
|
|
|
clear_payment_info(card, stp_invoice, invoice_items)
|
2016-03-23 18:39:41 +01:00
|
|
|
logger.error e
|
|
|
|
errors[:payment] << e.message
|
|
|
|
return false
|
|
|
|
rescue => e
|
|
|
|
# Something else happened, completely unrelated to Stripe
|
2016-07-07 15:57:06 +02:00
|
|
|
clear_payment_info(card, stp_invoice, invoice_items)
|
2016-03-23 18:39:41 +01:00
|
|
|
logger.error e
|
|
|
|
errors[:payment] << e.message
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-20 11:24:28 +02:00
|
|
|
UsersCredits::Manager.new(reservation: self).update_credits
|
2016-04-13 18:21:01 +02:00
|
|
|
true
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_payment_info(card, invoice, invoice_items)
|
|
|
|
begin
|
|
|
|
card.delete if card
|
|
|
|
if invoice
|
|
|
|
invoice.closed = true
|
|
|
|
invoice.save
|
|
|
|
end
|
|
|
|
if invoice_items.size > 0
|
|
|
|
invoice_items.each(&:delete)
|
|
|
|
end
|
|
|
|
rescue Stripe::InvalidRequestError => e
|
|
|
|
logger.error e
|
|
|
|
rescue Stripe::AuthenticationError => e
|
|
|
|
logger.error e
|
|
|
|
rescue Stripe::APIConnectionError => e
|
|
|
|
logger.error e
|
|
|
|
rescue Stripe::StripeError => e
|
|
|
|
logger.error e
|
|
|
|
rescue => e
|
|
|
|
logger.error e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-08-10 15:34:47 +02:00
|
|
|
def save_with_local_payment(coupon_code = nil)
|
2016-03-23 18:39:41 +01:00
|
|
|
if user.invoicing_disabled?
|
|
|
|
if valid?
|
2016-07-08 17:24:51 +02:00
|
|
|
|
2016-08-10 15:34:47 +02:00
|
|
|
### generate invoice only for calcul price, TODO refactor!!
|
2016-07-08 17:24:51 +02:00
|
|
|
build_invoice(user: user)
|
2016-08-10 17:37:17 +02:00
|
|
|
generate_invoice_items(true, coupon_code)
|
2016-07-08 17:24:51 +02:00
|
|
|
@wallet_amount_debit = get_wallet_amount_debit
|
|
|
|
self.invoice = nil
|
|
|
|
###
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
save!
|
2016-04-20 11:24:28 +02:00
|
|
|
UsersCredits::Manager.new(reservation: self).update_credits
|
2016-03-23 18:39:41 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
build_invoice(user: user)
|
2016-08-10 17:37:17 +02:00
|
|
|
generate_invoice_items(true, coupon_code)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if valid?
|
|
|
|
if plan_id
|
|
|
|
self.subscription = Subscription.find_or_initialize_by(user_id: user.id)
|
|
|
|
self.subscription.attributes = {plan_id: plan_id, user_id: user.id, expired_at: nil}
|
|
|
|
if subscription.save_with_local_payment(false)
|
|
|
|
self.invoice.invoice_items.push InvoiceItem.new(amount: subscription.plan.amount, description: subscription.plan.name, subscription_id: subscription.id)
|
2016-08-10 16:33:26 +02:00
|
|
|
set_total_and_coupon(coupon_code)
|
2016-03-23 18:39:41 +01:00
|
|
|
save!
|
|
|
|
else
|
|
|
|
errors[:card] << subscription.errors[:card].join
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
else
|
2016-08-10 16:33:26 +02:00
|
|
|
set_total_and_coupon(coupon_code)
|
2016-03-23 18:39:41 +01:00
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
2016-04-20 11:24:28 +02:00
|
|
|
UsersCredits::Manager.new(reservation: self).update_credits
|
2016-04-07 16:02:09 +02:00
|
|
|
return true
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-30 09:47:03 +02:00
|
|
|
def total_booked_seats
|
|
|
|
total = nb_reserve_places
|
|
|
|
if tickets.count > 0
|
|
|
|
total += tickets.map(&:booked).map(&:to_i).reduce(:+)
|
|
|
|
end
|
|
|
|
total
|
|
|
|
end
|
|
|
|
|
2016-08-10 16:33:26 +02:00
|
|
|
private
|
2016-03-23 18:39:41 +01:00
|
|
|
def machine_not_already_reserved
|
|
|
|
already_reserved = false
|
|
|
|
self.slots.each do |slot|
|
2017-02-27 13:15:31 +01:00
|
|
|
same_hour_slots = Slot.joins(:reservations).where(
|
2016-03-23 18:39:41 +01:00
|
|
|
reservations: { reservable_type: self.reservable_type,
|
|
|
|
reservable_id: self.reservable_id
|
|
|
|
},
|
|
|
|
start_at: slot.start_at,
|
|
|
|
end_at: slot.end_at,
|
|
|
|
availability_id: slot.availability_id,
|
|
|
|
canceled_at: nil)
|
|
|
|
if same_hour_slots.any?
|
|
|
|
already_reserved = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
errors.add(:machine, "already reserved") if already_reserved
|
|
|
|
end
|
|
|
|
|
|
|
|
def training_not_fully_reserved
|
|
|
|
slot = self.slots.first
|
|
|
|
errors.add(:training, "already fully reserved") if Availability.find(slot.availability_id).is_completed
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def notify_member_create_reservation
|
|
|
|
NotificationCenter.call type: 'notify_member_create_reservation',
|
|
|
|
receiver: user,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_admin_member_create_reservation
|
|
|
|
NotificationCenter.call type: 'notify_admin_member_create_reservation',
|
|
|
|
receiver: User.admins,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_event_nb_free_places
|
|
|
|
if reservable_id_was.blank?
|
2016-08-29 15:43:47 +02:00
|
|
|
# simple reservation creation, we subtract the number of booked seats from the previous number
|
2016-08-30 13:07:47 +02:00
|
|
|
nb_free_places = reservable.nb_free_places - total_booked_seats
|
2016-03-23 18:39:41 +01:00
|
|
|
else
|
2016-08-29 15:43:47 +02:00
|
|
|
# reservation moved from another date (for recurring events)
|
2016-08-30 13:07:47 +02:00
|
|
|
seats = total_booked_seats
|
2016-08-29 15:43:47 +02:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
reservable_was = Event.find(reservable_id_was)
|
2016-08-29 15:43:47 +02:00
|
|
|
nb_free_places = reservable_was.nb_free_places + seats
|
2016-03-23 18:39:41 +01:00
|
|
|
reservable_was.update_columns(nb_free_places: nb_free_places)
|
2016-08-29 15:43:47 +02:00
|
|
|
nb_free_places = reservable.nb_free_places - seats
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
reservable.update_columns(nb_free_places: nb_free_places)
|
|
|
|
end
|
2016-07-07 15:57:06 +02:00
|
|
|
|
2016-11-29 11:12:58 +01:00
|
|
|
def get_cart_total
|
2016-08-10 15:34:47 +02:00
|
|
|
total = (self.invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+) or 0)
|
2016-07-08 17:24:51 +02:00
|
|
|
if plan_id.present?
|
2016-07-21 12:57:13 +02:00
|
|
|
plan = Plan.find(plan_id)
|
2016-07-08 17:24:51 +02:00
|
|
|
total += plan.amount
|
|
|
|
end
|
2016-11-29 11:12:58 +01:00
|
|
|
total
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_wallet_amount_debit
|
|
|
|
total = get_cart_total
|
2016-09-20 16:54:23 +02:00
|
|
|
if @coupon
|
2016-11-29 14:57:43 +01:00
|
|
|
total = CouponService.new.apply(total, @coupon, user.id)
|
2016-09-20 16:54:23 +02:00
|
|
|
end
|
2016-07-08 17:24:51 +02:00
|
|
|
wallet_amount = (user.wallet.amount * 100).to_i
|
2016-08-10 15:34:47 +02:00
|
|
|
|
|
|
|
wallet_amount >= total ? total : wallet_amount
|
2016-07-08 17:24:51 +02:00
|
|
|
end
|
|
|
|
|
2016-07-07 15:57:06 +02:00
|
|
|
def debit_user_wallet
|
|
|
|
if @wallet_amount_debit.present? and @wallet_amount_debit != 0
|
|
|
|
amount = @wallet_amount_debit / 100.0
|
2016-07-20 15:07:43 +02:00
|
|
|
wallet_transaction = WalletService.new(user: user, wallet: user.wallet).debit(amount, self)
|
2016-11-24 14:09:34 +01:00
|
|
|
# wallet debit success
|
|
|
|
if wallet_transaction
|
|
|
|
# payment by online or (payment by local and invoice isnt disabled)
|
|
|
|
if stp_invoice_id or !user.invoicing_disabled?
|
|
|
|
self.invoice.update_columns(wallet_amount: @wallet_amount_debit, wallet_transaction_id: wallet_transaction.id)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise DebitWalletError
|
2016-07-20 15:07:43 +02:00
|
|
|
end
|
2016-07-07 15:57:06 +02:00
|
|
|
end
|
|
|
|
end
|
2016-08-10 16:33:26 +02:00
|
|
|
|
|
|
|
##
|
|
|
|
# Set the total price to the reservation's invoice, summing its whole items.
|
|
|
|
# Additionally a coupon may be applied to this invoice to make a discount on the total price
|
|
|
|
# @param [coupon_code] {String} optional coupon code to apply to the invoice
|
|
|
|
##
|
|
|
|
def set_total_and_coupon(coupon_code = nil)
|
|
|
|
total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+)
|
|
|
|
|
|
|
|
unless coupon_code.nil?
|
2016-11-23 16:30:19 +01:00
|
|
|
cp = Coupon.find_by(code: coupon_code)
|
2016-09-08 15:19:12 +02:00
|
|
|
if not cp.nil? and cp.status(user.id) == 'active'
|
2016-11-29 14:57:43 +01:00
|
|
|
total = CouponService.new.apply(total, cp, user.id)
|
2016-09-08 15:19:12 +02:00
|
|
|
self.invoice.coupon_id = cp.id
|
|
|
|
else
|
|
|
|
raise InvalidCouponError
|
|
|
|
end
|
2016-08-10 16:33:26 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
self.invoice.total = total
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|