mirror of
https://github.com/openSUSE/osem.git
synced 2026-08-16 21:24:05 +00:00
Prevent math errors in calculating ticket 'turnover'
This commit is contained in:
parent
d0f673b73d
commit
3031bb43cf
5 changed files with 7 additions and 13 deletions
|
|
@ -517,7 +517,7 @@ class Conference < ApplicationRecord
|
|||
if tickets && ticket_purchases
|
||||
tickets.each do |ticket|
|
||||
result[ticket.title] = {
|
||||
'value' => ApplicationController.helpers.humanized_money(ticket.tickets_turnover_total(ticket.id)).delete(',').to_i,
|
||||
'value' => ApplicationController.helpers.humanized_money(ticket.tickets_turnover_total).delete(',').to_i,
|
||||
'color' => "\##{Digest::MD5.hexdigest(ticket.title)[0..5]}"
|
||||
}
|
||||
end
|
||||
|
|
|
|||
|
|
@ -63,11 +63,10 @@ class Ticket < ApplicationRecord
|
|||
tickets.inject(0){ |sum, ticket| sum + (ticket.amount_paid * ticket.quantity) }
|
||||
end
|
||||
|
||||
def tickets_turnover_total(id)
|
||||
ticket = Ticket.find(id)
|
||||
return Money.new(0, 'USD') unless ticket
|
||||
sum = ticket.ticket_purchases.paid.total
|
||||
Money.new(sum, ticket.price_currency)
|
||||
def tickets_turnover_total
|
||||
purchases = ticket_purchases.paid
|
||||
total = purchases.sum { |purchase| (purchase.price_cents * purchase.quantity) }
|
||||
Money.new(total, price_currency)
|
||||
end
|
||||
|
||||
def tickets_sold
|
||||
|
|
|
|||
|
|
@ -71,11 +71,6 @@ 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
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@
|
|||
%td
|
||||
= ticket.tickets_sold
|
||||
%td
|
||||
= humanized_money_with_symbol ticket.tickets_turnover_total(ticket.id)
|
||||
= humanized_money_with_symbol ticket.tickets_turnover_total
|
||||
%td
|
||||
= ticket.registration_ticket? ? 'Yes' : 'No'
|
||||
%td
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ describe Ticket 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 }
|
||||
subject { ticket.tickets_turnover_total }
|
||||
|
||||
it 'returns turnover as Money with ticket\'s currency' do
|
||||
is_expected.to eq Money.new(5_000 * 3, ticket.price_currency)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue