1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00

include coupons discounts in statistics CA

This commit is contained in:
Sylvain 2016-08-16 10:29:08 +02:00
parent 8ae3cf25b8
commit 396d7b4df7
6 changed files with 23 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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