1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-04 15:24:23 +01:00
fab-manager/app/services/statistics/concerns/compute_concern.rb

41 lines
1.2 KiB
Ruby
Raw Normal View History

2022-08-29 17:34:09 +02:00
# frozen_string_literal: true
# Provides methods to compute totals in statistics
module Statistics::Concerns::ComputeConcern
extend ActiveSupport::Concern
class_methods do
def calcul_ca(invoice)
2022-12-22 17:18:34 +01:00
return 0 unless invoice
2022-08-29 17:34:09 +02:00
ca = 0
# sum each items in the invoice (+ for invoices/- for refunds)
invoice.invoice_items.each do |ii|
next if ii.object_type == 'Subscription'
ca = if invoice.is_a?(Avoir)
ca - ii.amount.to_i
else
ca + ii.amount.to_i
end
end
# subtract coupon discount from invoices and refunds
cs = CouponService.new
ca = cs.ventilate(cs.invoice_total_no_coupon(invoice), ca, invoice.coupon) unless invoice.coupon_id.nil?
# divide the result by 100 to convert from centimes to monetary unit
ca.zero? ? ca : ca / 100.0
end
def calcul_avoir_ca(invoice)
ca = 0
invoice.invoice_items.each do |ii|
ca -= ii.amount.to_i
end
# subtract coupon discount from the refund
cs = CouponService.new
ca = cs.ventilate(cs.invoice_total_no_coupon(invoice), ca, invoice.coupon) unless invoice.coupon_id.nil?
ca.zero? ? ca : ca / 100.0
end
end
end