1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-26 20:54:21 +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' reservable_type: 'Event'
slots_attributes: [] slots_attributes: []
nb_reserve_places: $scope.reserve.nbReservePlaces 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 reservation.slots_attributes.push
start_at: $scope.event.start_date start_at: $scope.event.start_date
end_at: $scope.event.end_date end_at: $scope.event.end_date
availability_id: $scope.event.availability.id 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 $scope.attempting = true
# save the reservation to the API
Reservation.save reservation: reservation, (reservation) -> Reservation.save reservation: reservation, (reservation) ->
# reservation successfull
afterPayment(reservation) afterPayment(reservation)
$scope.attempting = false $scope.attempting = false
, (response)-> , (response)->
# reservation failed
$scope.alerts = [] $scope.alerts = []
$scope.alerts.push $scope.alerts.push
msg: response.data.card[0] msg: response.data.card[0]
type: 'danger' type: 'danger'
# unset the attempting marker
$scope.attempting = false $scope.attempting = false
@ -419,10 +430,7 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", "
$scope.ctrl.member = $scope.currentUser $scope.ctrl.member = $scope.currentUser
# initialize the "reserve" object with the event's data # initialize the "reserve" object with the event's data
$scope.reserve.nbPlaces.normal = [0..$scope.event.nb_free_places] resetEventReserve()
for price in $scope.event.prices
$scope.reserve.nbPlaces[price.id] = [0..$scope.event.nb_free_places]
$scope.reserve.tickets[price.id] = 0
# if non-admin, get the current user's reservations into $scope.reservations # if non-admin, get the current user's reservations into $scope.reservations
if $scope.currentUser if $scope.currentUser
@ -468,10 +476,12 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", "
availability_id: event.availability.id availability_id: event.availability.id
offered: event.offered || false offered: event.offered || false
for price in event.prices for evt_px_cat in event.prices
reservation.tickets_attributes.push booked = reserve.tickets[evt_px_cat.id]
event_price_category_id: price.id if booked > 0
booked: reserve.tickets[price.id] reservation.tickets_attributes.push
event_price_category_id: evt_px_cat.id
booked: booked
reservation reservation
@ -497,12 +507,17 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", "
resetEventReserve = -> resetEventReserve = ->
if $scope.event if $scope.event
$scope.reserve = $scope.reserve =
nbPlaces: [0..$scope.event.nb_free_places] nbPlaces:
nbReducedPlaces: [0..$scope.event.nb_free_places] #FIXME normal: [0..$scope.event.nb_free_places]
nbReservePlaces: 0 nbReservePlaces: 0
nbReserveReducedPlaces: 0 tickets: {}
toReserve: false toReserve: false
amountTotal : 0 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 $scope.event.offered = false

View File

@ -4,7 +4,7 @@
</div> </div>
<div class="modal-body"> <div class="modal-body">
<p translate>{{ 'you_can_shift_this_reservation_on_the_following_slots' }}</p> <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> <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> </select>
</div> </div>

View File

@ -44,7 +44,7 @@ class API::PricesController < API::ApiController
@amount = {elements: nil, total: 0} @amount = {elements: nil, total: 0}
else else
_reservable = _price_params[:reservable_type].constantize.find(_price_params[:reservable_id]) _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 end

View File

@ -13,11 +13,11 @@ class Price < ActiveRecord::Base
# @param slots {Array<Slot>} when did the reservation will occur # @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 [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_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 # @param [coupon_code] {String} Code of the coupon to apply to the total price
# @return {Hash} total and price detail # @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 _amount = 0
_elements = Hash.new _elements = Hash.new
_elements[:slots] = Array.new _elements[:slots] = Array.new
@ -91,11 +91,10 @@ class Price < ActiveRecord::Base
# Event reservation # Event reservation
when Event when Event
if reservable.reduced_amount and nb_reduced_places amount = reservable.amount * nb_places
amount = reservable.amount * nb_places + (reservable.reduced_amount * nb_reduced_places) tickets.each do |ticket|
else amount += ticket[:booked] * EventPriceCategory.find(ticket[:event_price_category_id]).amount
amount = reservable.amount * nb_places end unless tickets.nil?
end
slots.each do |slot| slots.each do |slot|
_amount += get_slot_price(amount, slot, admin, _elements) _amount += get_slot_price(amount, slot, admin, _elements)
end end

View File

@ -102,10 +102,9 @@ class Reservation < ActiveRecord::Base
# === Event reservation === # === Event reservation ===
when Event when Event
if reservable.reduced_amount and nb_reserve_reduced_places amount = reservable.amount * nb_reserve_places
amount = reservable.amount * nb_reserve_places + (reservable.reduced_amount * nb_reserve_reduced_places) tickets.each do |ticket|
else amount += ticket.booked * ticket.event_price_category.amount
amount = reservable.amount * nb_reserve_places
end end
slots.each do |slot| slots.each do |slot|
description = reservable.name + " #{I18n.l slot.start_at, format: :long} - #{I18n.l slot.end_at, format: :hour_minute}" 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(:+) total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+)
invoice_items << Stripe::InvoiceItem.create( invoice_items << Stripe::InvoiceItem.create(
customer: user.stp_customer_id, 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, currency: Rails.application.secrets.stripe_currency,
description: "coupon #{cp.code}" description: "coupon #{cp.code}"
) )
@ -356,12 +355,22 @@ class Reservation < ActiveRecord::Base
def update_event_nb_free_places def update_event_nb_free_places
if reservable_id_was.blank? 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 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) 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) 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 end
reservable.update_columns(nb_free_places: nb_free_places) reservable.update_columns(nb_free_places: nb_free_places)
end end

View File

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