From 3031bb43cf510c225e17e2ecc6cd9217dce1621e Mon Sep 17 00:00:00 2001 From: James Mason Date: Mon, 8 Oct 2018 08:52:58 -0700 Subject: [PATCH] Prevent math errors in calculating ticket 'turnover' --- app/models/conference.rb | 2 +- app/models/ticket.rb | 9 ++++----- app/models/ticket_purchase.rb | 5 ----- app/views/admin/tickets/index.html.haml | 2 +- spec/models/ticket_spec.rb | 2 +- 5 files changed, 7 insertions(+), 13 deletions(-) diff --git a/app/models/conference.rb b/app/models/conference.rb index 7f606927..146b5994 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -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 diff --git a/app/models/ticket.rb b/app/models/ticket.rb index ee8926d9..2b3f6cc3 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -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 diff --git a/app/models/ticket_purchase.rb b/app/models/ticket_purchase.rb index 92f12e63..f6db257a 100644 --- a/app/models/ticket_purchase.rb +++ b/app/models/ticket_purchase.rb @@ -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 diff --git a/app/views/admin/tickets/index.html.haml b/app/views/admin/tickets/index.html.haml index 020cc1c7..17ecb932 100644 --- a/app/views/admin/tickets/index.html.haml +++ b/app/views/admin/tickets/index.html.haml @@ -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 diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index 543e9aaf..55a3ebe6 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -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)