Add total amount spent by user

User can see the total amount that he paid for the tickets and total amount spent by him on each ticket
This commit is contained in:
rahul 2017-09-25 00:52:13 +05:30
parent b761e83b45
commit 0d70832f5b
4 changed files with 10 additions and 4 deletions

View file

@ -27,8 +27,9 @@ class ConferenceRegistrationsController < ApplicationController
end
def show
@total_price = Ticket.total_price(@conference, current_user, paid: true)
@total_price = Ticket.total_price_user(@conference, current_user, paid: true)
@tickets = current_user.ticket_purchases.by_conference(@conference).paid
@total_price_per_ticket = @tickets.group(:ticket_id).sum('amount_paid * quantity')
@ticket_payments = @tickets.group_by(&:ticket_id)
@total_quantity = @tickets.group(:ticket_id).sum(:quantity)
end

View file

@ -55,6 +55,11 @@ class Ticket < ActiveRecord::Base
result ? result : Money.new(0, 'USD')
end
def self.total_price_user(conference, user, paid: false)
tickets = TicketPurchase.where(conference: conference, user: user, paid: paid)
tickets.inject(0){ |sum, ticket| sum + (ticket.amount_paid * ticket.quantity) }
end
def tickets_sold
ticket_purchases.paid.sum(:quantity)
end

View file

@ -101,7 +101,7 @@
= word_pluralize(@total_quantity[ticket_id], 'Ticket')
for
= tickets.first.price.symbol
= humanized_money tickets.first.price
= humanized_money @total_price_per_ticket[ticket_id]
%br
- if @tickets.any?
= link_to 'Get more tickets', conference_tickets_path(@conference.short_title), class: "btn btn-default"

View file

@ -252,7 +252,7 @@ describe ConferenceRegistrationsController, type: :controller do
end
it 'does not assign price of purchased tickets to total_price and purchased tickets to tickets without payment' do
expect(assigns(:total_price)).to eq Money.new(0, 'USD')
expect(assigns(:total_price)).to eq 0
end
end
@ -262,7 +262,7 @@ describe ConferenceRegistrationsController, type: :controller do
end
it 'assigns 0 dollars to total_price and empty array to tickets variables' do
expect(assigns(:total_price)).to eq Money.new(0, 'USD')
expect(assigns(:total_price)).to eq 0
expect(assigns(:tickets)).to match_array []
end
end