1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

ventilate coupon codes on items

This commit is contained in:
Sylvain 2019-09-17 17:39:05 +02:00
parent 30e7ce8377
commit 46c3497c85
3 changed files with 38 additions and 16 deletions

View File

@ -107,6 +107,7 @@ class AccountingExportService
# Generate the "reservation" row, which contains the credit to the reservation account, all taxes excluded
def reservation_row(invoice, item)
wo_taxes = (item.amount / (vat_service.invoice_vat(invoice) / 100.00 + 1)) / 100.00
wo_taxes_coupon = amount_after_coupon(invoice, wo_taxes)
row = ''
columns.each do |column|
case column
@ -123,13 +124,13 @@ class AccountingExportService
when 'line_label'
row << ''
when 'debit_origin'
row << debit(invoice, wo_taxes)
row << debit(invoice, wo_taxes_coupon)
when 'credit_origin'
row << credit(invoice, wo_taxes)
row << credit(invoice, wo_taxes_coupon)
when 'debit_euro'
row << debit(invoice, wo_taxes)
row << debit(invoice, wo_taxes_coupon)
when 'credit_euro'
row << credit(invoice, wo_taxes)
row << credit(invoice, wo_taxes_coupon)
when 'lettering'
row << ''
else
@ -144,6 +145,7 @@ class AccountingExportService
def subscription_row(invoice)
subscription_item = invoice.invoice_items.select(&:subscription).first
wo_taxes = (subscription_item.amount / (vat_service.invoice_vat(invoice) / 100.00 + 1)) / 100.00
wo_taxes_coupon = amount_after_coupon(invoice, wo_taxes)
row = ''
columns.each do |column|
case column
@ -160,13 +162,13 @@ class AccountingExportService
when 'line_label'
row << ''
when 'debit_origin'
row << debit(invoice, wo_taxes)
row << debit(invoice, wo_taxes_coupon)
when 'credit_origin'
row << credit(invoice, wo_taxes)
row << credit(invoice, wo_taxes_coupon)
when 'debit_euro'
row << debit(invoice, wo_taxes)
row << debit(invoice, wo_taxes_coupon)
when 'credit_euro'
row << credit(invoice, wo_taxes)
row << credit(invoice, wo_taxes_coupon)
when 'lettering'
row << ''
else
@ -179,6 +181,8 @@ class AccountingExportService
# Generate the "VAT" row, which contains the credit to the VAT account, with VAT amount only
def vat_row(invoice)
# FIXME, if VAT wasn't enabled, it's calculated anyway
# FIXME, round at +/-0.01 if total > sum(items)
vat = (invoice.total - (invoice.total / (vat_service.invoice_vat(invoice) / 100.00 + 1))) / 100.00
row = ''
columns.each do |column|
@ -261,6 +265,12 @@ class AccountingExportService
debit(invoice, amount)
end
# Return the given amount, after the coupon attached to the given invoice was applied, if any
def amount_after_coupon(invoice, amount)
cs = CouponService.new
invoice.coupon_id.nil? ? amount : cs.ventilate(cs.invoice_total_no_coupon(invoice), amount, invoice.coupon)
end
# Format the given number as a string, using the configured separator
def format_number(num)
number_to_currency(num, unit: '', separator: decimal_separator, delimiter: '', precision: 2)

View File

@ -1,3 +1,6 @@
# frozen_string_literal: true
# This class provides helper methods to deal with coupons
class CouponService
##
# Apply the provided coupon, if active, to the given price. Usability tests will be run depending on the
@ -54,4 +57,14 @@ class CouponService
end
price
end
##
# Compute the total amount of the given invoice, without the applied coupon
# Invoice.total stores the amount payed by the customer, coupon deducted
# @param invoice {Invoice} invoice object, its total before discount will be computed
##
def invoice_total_no_coupon(invoice)
total = (invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+) or 0)
total / 100.0
end
end

View File

@ -131,10 +131,12 @@ class StatisticService
next if i.invoice.is_a?(Avoir)
sub = i.subscription
next unless sub
ca = i.amount.to_i / 100.0
ca = CouponService.new.ventilate(get_invoice_total_no_coupon(i.invoice), ca, i.invoice.coupon) unless i.invoice.coupon_id.nil?
cs = CouponService.new
ca = cs.ventilate(cs.invoice_total_no_coupon(i.invoice), ca, i.invoice.coupon) unless i.invoice.coupon_id.nil?
profile = sub.statistic_profile
p = sub.plan
result.push OpenStruct.new({
@ -396,7 +398,8 @@ class StatisticService
end
end
# subtract coupon discount from invoices and refunds
ca = CouponService.new.ventilate(get_invoice_total_no_coupon(invoice), ca, invoice.coupon) unless invoice.coupon_id.nil?
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
@ -407,7 +410,8 @@ class StatisticService
ca -= ii.amount.to_i
end
# subtract coupon discount from the refund
ca = CouponService.new.ventilate(get_invoice_total_no_coupon(invoice), ca, invoice.coupon) unless invoice.coupon_id.nil?
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
@ -488,11 +492,6 @@ class StatisticService
# user_subscription_ca.inject {|sum,x| sum.ca + x.ca } || 0
# end
def get_invoice_total_no_coupon(invoice)
total = (invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+) or 0)
total / 100.0
end
def find_or_create_user_info_info_list(profile, list)
found = list.select do |l|
l.statistic_profile_id == profile.id