mirror of
https://github.com/openSUSE/osem.git
synced 2026-08-13 19:54:02 +00:00
Fix tickets turnover calculation
This commit is contained in:
parent
69de418ccd
commit
58cb62dd6d
3 changed files with 20 additions and 6 deletions
|
|
@ -61,12 +61,10 @@ class Ticket < ApplicationRecord
|
|||
end
|
||||
|
||||
def tickets_turnover_total(id)
|
||||
tickets = TicketPurchase.where(ticket_id: id).paid
|
||||
if tickets.blank?
|
||||
Money.new(0, Ticket.find(id).price_currency)
|
||||
else
|
||||
tickets.inject(Money.new(0, tickets.first.price_currency)){ |sum, ticket| sum + (ticket.amount_paid * ticket.quantity) }
|
||||
end
|
||||
ticket = Ticket.find(id)
|
||||
return Money.new(0, 'USD') unless ticket
|
||||
sum = ticket.ticket_purchases.paid.total
|
||||
Money.new(sum, ticket.price_currency)
|
||||
end
|
||||
|
||||
def tickets_sold
|
||||
|
|
|
|||
|
|
@ -69,6 +69,11 @@ class TicketPurchase < ApplicationRecord
|
|||
purchase
|
||||
end
|
||||
|
||||
# Total amount
|
||||
def self.total
|
||||
sum('amount_paid * quantity')
|
||||
end
|
||||
|
||||
def pay(payment)
|
||||
update_attributes(paid: true, payment: payment)
|
||||
PhysicalTicket.transaction do
|
||||
|
|
|
|||
|
|
@ -65,6 +65,17 @@ describe Ticket do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#tickets_turnover_total' do
|
||||
let!(:purchase1) { create :ticket_purchase, ticket: ticket, amount_paid: 5_000, quantity: 1, paid: true, user: user }
|
||||
let!(:purchase2) { create :ticket_purchase, ticket: ticket, amount_paid: 5_000, quantity: 2, paid: true, user: user }
|
||||
let!(:purchase3) { create :ticket_purchase, ticket: ticket, amount_paid: 5_000, quantity: 10, paid: false, user: user }
|
||||
subject { ticket.tickets_turnover_total ticket.id }
|
||||
|
||||
it 'returns turnover as Money with ticket\'s currency' do
|
||||
is_expected.to eq Money.new(5_000 * 3, ticket.price_currency)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#unpaid?' do
|
||||
let!(:ticket_purchase) { create(:ticket_purchase, user: user, ticket: ticket) }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue