1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00

reserve event with custom prices

This commit is contained in:
Sylvain 2016-08-29 15:43:47 +02:00
parent 12eadb8ceb
commit 06d65c785a
6 changed files with 52 additions and 30 deletions

View File

@ -274,20 +274,31 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", "
reservable_type: 'Event'
slots_attributes: []
nb_reserve_places: $scope.reserve.nbReservePlaces
nb_reserve_reduced_places: $scope.reserve.nbReserveReducedPlaces #FIXME
tickets_attributes: []
# a single slot is used for events
reservation.slots_attributes.push
start_at: $scope.event.start_date
end_at: $scope.event.end_date
availability_id: $scope.event.availability.id
# iterate over reservations per prices
for price_id, seats of $scope.reserve.tickets
reservation.tickets_attributes.push
event_price_category_id: price_id
booked: seats
# set the attempting marker
$scope.attempting = true
# save the reservation to the API
Reservation.save reservation: reservation, (reservation) ->
# reservation successfull
afterPayment(reservation)
$scope.attempting = false
, (response)->
# reservation failed
$scope.alerts = []
$scope.alerts.push
msg: response.data.card[0]
type: 'danger'
# unset the attempting marker
$scope.attempting = false
@ -419,10 +430,7 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", "
$scope.ctrl.member = $scope.currentUser
# initialize the "reserve" object with the event's data
$scope.reserve.nbPlaces.normal = [0..$scope.event.nb_free_places]
for price in $scope.event.prices
$scope.reserve.nbPlaces[price.id] = [0..$scope.event.nb_free_places]
$scope.reserve.tickets[price.id] = 0
resetEventReserve()
# if non-admin, get the current user's reservations into $scope.reservations
if $scope.currentUser
@ -468,10 +476,12 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", "
availability_id: event.availability.id
offered: event.offered || false
for price in event.prices
reservation.tickets_attributes.push
event_price_category_id: price.id
booked: reserve.tickets[price.id]
for evt_px_cat in event.prices
booked = reserve.tickets[evt_px_cat.id]
if booked > 0
reservation.tickets_attributes.push
event_price_category_id: evt_px_cat.id
booked: booked
reservation
@ -497,12 +507,17 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", "
resetEventReserve = ->
if $scope.event
$scope.reserve =
nbPlaces: [0..$scope.event.nb_free_places]
nbReducedPlaces: [0..$scope.event.nb_free_places] #FIXME
nbPlaces:
normal: [0..$scope.event.nb_free_places]
nbReservePlaces: 0
nbReserveReducedPlaces: 0
tickets: {}
toReserve: false
amountTotal : 0
for evt_px_cat in $scope.event.prices
$scope.reserve.nbPlaces[evt_px_cat.id] = [0..$scope.event.nb_free_places]
$scope.reserve.tickets[evt_px_cat.id] = 0
$scope.event.offered = false

View File

@ -4,7 +4,7 @@
</div>
<div class="modal-body">
<p translate>{{ 'you_can_shift_this_reservation_on_the_following_slots' }}</p>
<select ng-model="reservation.reservable_id">
<select ng-model="reservation.reservable_id" class="form-control">
<option value="{{e.id}}" ng-repeat="e in event.recurrence_events" ng-if="e.nb_free_places >= (reservation.nb_reserve_places + reservation.nb_reserve_reduced_places)">{{e.start_date | amDateFormat:'L'}}</option>
</select>
</div>

View File

@ -44,7 +44,7 @@ class API::PricesController < API::ApiController
@amount = {elements: nil, total: 0}
else
_reservable = _price_params[:reservable_type].constantize.find(_price_params[:reservable_id])
@amount = Price.compute(current_user.is_admin?, _user, _reservable, _price_params[:slots_attributes], _price_params[:plan_id], _price_params[:nb_reserve_places], _price_params[:nb_reserve_reduced_places], coupon_params[:coupon_code])
@amount = Price.compute(current_user.is_admin?, _user, _reservable, _price_params[:slots_attributes], _price_params[:plan_id], _price_params[:nb_reserve_places], _price_params[:tickets_attributes], coupon_params[:coupon_code])
end

View File

@ -13,11 +13,11 @@ class Price < ActiveRecord::Base
# @param slots {Array<Slot>} when did the reservation will occur
# @param [plan_id] {Number} if the user is subscribing to a plan at the same time of his reservation, specify the plan's ID here
# @param [nb_places] {Number} for _reservable_ of type Event, pass here the number of booked places
# @param [nb_reduced_places] {Number} for _reservable_ of type Event, pass here the number of booked places at reduced price
# @param [tickets] {Array<Ticket>} for _reservable_ of type Event, mapping of the number of seats booked per price's category
# @param [coupon_code] {String} Code of the coupon to apply to the total price
# @return {Hash} total and price detail
##
def self.compute(admin, user, reservable, slots, plan_id = nil, nb_places = nil, nb_reduced_places = nil, coupon_code = nil)
def self.compute(admin, user, reservable, slots, plan_id = nil, nb_places = nil, tickets = nil, coupon_code = nil)
_amount = 0
_elements = Hash.new
_elements[:slots] = Array.new
@ -91,11 +91,10 @@ class Price < ActiveRecord::Base
# Event reservation
when Event
if reservable.reduced_amount and nb_reduced_places
amount = reservable.amount * nb_places + (reservable.reduced_amount * nb_reduced_places)
else
amount = reservable.amount * nb_places
end
amount = reservable.amount * nb_places
tickets.each do |ticket|
amount += ticket[:booked] * EventPriceCategory.find(ticket[:event_price_category_id]).amount
end unless tickets.nil?
slots.each do |slot|
_amount += get_slot_price(amount, slot, admin, _elements)
end

View File

@ -102,10 +102,9 @@ class Reservation < ActiveRecord::Base
# === Event reservation ===
when Event
if reservable.reduced_amount and nb_reserve_reduced_places
amount = reservable.amount * nb_reserve_places + (reservable.reduced_amount * nb_reserve_reduced_places)
else
amount = reservable.amount * nb_reserve_places
amount = reservable.amount * nb_reserve_places
tickets.each do |ticket|
amount += ticket.booked * ticket.event_price_category.amount
end
slots.each do |slot|
description = reservable.name + " #{I18n.l slot.start_at, format: :long} - #{I18n.l slot.end_at, format: :hour_minute}"
@ -135,7 +134,7 @@ class Reservation < ActiveRecord::Base
total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+)
invoice_items << Stripe::InvoiceItem.create(
customer: user.stp_customer_id,
amount: -(total * cp.percent_off / 100.0),
amount: -(total * cp.percent_off / 100).to_i,
currency: Rails.application.secrets.stripe_currency,
description: "coupon #{cp.code}"
)
@ -356,12 +355,22 @@ class Reservation < ActiveRecord::Base
def update_event_nb_free_places
if reservable_id_was.blank?
nb_free_places = reservable.nb_free_places - nb_reserve_places - nb_reserve_reduced_places
# simple reservation creation, we subtract the number of booked seats from the previous number
nb_free_places = reservable.nb_free_places - nb_reserve_places
tickets.each do |ticket|
nb_free_places -= ticket.booked
end
else
# reservation moved from another date (for recurring events)
seats = nb_reserve_places
tickets.each do |ticket|
seats += ticket.booked
end
reservable_was = Event.find(reservable_id_was)
nb_free_places = reservable_was.nb_free_places + nb_reserve_places + nb_reserve_reduced_places
nb_free_places = reservable_was.nb_free_places + seats
reservable_was.update_columns(nb_free_places: nb_free_places)
nb_free_places = reservable.nb_free_places - nb_reserve_places - nb_reserve_reduced_places
nb_free_places = reservable.nb_free_places - seats
end
reservable.update_columns(nb_free_places: nb_free_places)
end

View File

@ -2,7 +2,6 @@ class Ticket < ActiveRecord::Base
belongs_to :reservation
belongs_to :event_price_category
validates :reservation_id, presence: true
validates :event_price_category_id, presence: true
validates :booked, presence: true
validates :booked, numericality: { only_integer: true, greater_than: 0 }