diff --git a/app/models/invoice.rb b/app/models/invoice.rb index d51533a77..6eb2e7bec 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -149,7 +149,7 @@ class Invoice < ActiveRecord::Base end # handle coupon unless avoir.coupon_id.nil? - discount = avoir.total * avoir.coupon.percent_off / 100 + discount = avoir.total * avoir.coupon.percent_off / 100.0 avoir.total -= discount end avoir diff --git a/app/models/price.rb b/app/models/price.rb index 932225408..e1c0988d7 100644 --- a/app/models/price.rb +++ b/app/models/price.rb @@ -114,7 +114,7 @@ class Price < ActiveRecord::Base # === apply Coupon if any === unless coupon_code.nil? _coupon = Coupon.find_by_code(coupon_code) - _amount = _amount - (_amount * _coupon.percent_off / 100) + _amount = _amount - (_amount * _coupon.percent_off / 100.0) end # return result diff --git a/app/models/reservation.rb b/app/models/reservation.rb index a4ad7f0f9..946ef44ae 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -132,7 +132,7 @@ class Reservation < ActiveRecord::Base total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+) invoice_items << Stripe::InvoiceItem.create( customer: user.stp_customer_id, - amount: -(total * cp.percent_off / 100), + amount: -(total * cp.percent_off / 100.0), currency: Rails.application.secrets.stripe_currency, description: "coupon #{cp.code}" ) @@ -394,7 +394,7 @@ class Reservation < ActiveRecord::Base unless coupon_code.nil? cp = Coupon.find_by_code(coupon_code) - total = total - (total * cp.percent_off / 100) + total = total - (total * cp.percent_off / 100.0) self.invoice.coupon_id = cp.id end diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 5717a1dce..558d217d0 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -39,7 +39,7 @@ class Subscription < ActiveRecord::Base total = plan.amount Stripe::InvoiceItem.create( customer: user.stp_customer_id, - amount: -(total * cp.percent_off / 100), + amount: -(total * cp.percent_off / 100.0), currency: Rails.application.secrets.stripe_currency, description: "coupon #{cp.code}" ) @@ -136,7 +136,7 @@ class Subscription < ActiveRecord::Base unless coupon_code.nil? coupon = Coupon.find_by_code(coupon_code) coupon_id = coupon.id - total = plan.amount - (plan.amount * coupon.percent_off / 100) + total = plan.amount - (plan.amount * coupon.percent_off / 100.0) end invoice = Invoice.new(invoiced_id: id, invoiced_type: 'Subscription', user: user, total: total, stp_invoice_id: stp_invoice_id, coupon_id: coupon_id) diff --git a/app/pdfs/pdf/invoice.rb b/app/pdfs/pdf/invoice.rb index decc83f54..21c4689df 100644 --- a/app/pdfs/pdf/invoice.rb +++ b/app/pdfs/pdf/invoice.rb @@ -141,7 +141,7 @@ module PDF # subtract the coupon, if any unless invoice.coupon_id.nil? cp = invoice.coupon - discount = total_calc * cp.percent_off / 100 + discount = total_calc * cp.percent_off / 100.0 total_calc = total_calc - discount # add a row for the coupon diff --git a/app/services/statistic_service.rb b/app/services/statistic_service.rb index 968bd8c91..e5d0f3f8c 100644 --- a/app/services/statistic_service.rb +++ b/app/services/statistic_service.rb @@ -121,10 +121,14 @@ class StatisticService def subscriptions_list(options = default_options) result = [] InvoiceItem.where('invoice_items.created_at >= :start_date AND invoice_items.created_at <= :end_date', options) - .eager_load(subscription: [:plan, user: [:profile, :group]]).each do |i| + .eager_load(invoice: [:coupon], subscription: [:plan, user: [:profile, :group]]).each do |i| unless i.invoice.is_a?(Avoir) sub = i.subscription if sub + ca = i.amount.to_i / 100.0 + unless i.invoice.coupon_id.nil? + ca = ca - ( ca * i.invoice.coupon.percent_off / 100.0 ) + end u = sub.user p = sub.plan result.push OpenStruct.new({ @@ -138,7 +142,7 @@ class StatisticService duration: p.duration.to_i, subscription_id: sub.id, invoice_item_id: i.id, - ca: i.amount.to_i / 100.0 + ca: ca }.merge(user_info(u))) end end @@ -337,6 +341,7 @@ class StatisticService def calcul_ca(invoice) return nil unless invoice ca = 0 + # sum each items in the invoice (+ for invoices/- for refunds) invoice.invoice_items.each do |ii| unless ii.subscription_id if invoice.is_a?(Avoir) @@ -346,6 +351,11 @@ class StatisticService end end end + # subtract coupon discount from invoices and refunds + unless invoice.coupon_id.nil? + ca = ca - ( ca * invoice.coupon.percent_off / 100.0 ) + end + # divide the result by 100 to convert from centimes to monetary unit ca == 0 ? ca : ca / 100.0 end @@ -354,6 +364,10 @@ class StatisticService invoice.invoice_items.each do |ii| ca = ca - ii.amount.to_i end + # subtract coupon discount from the refund + unless invoice.coupon_id.nil? + ca = ca - ( ca * invoice.coupon.percent_off / 100.0 ) + end ca == 0 ? ca : ca / 100.0 end