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

include VAT in archives

This commit is contained in:
Sylvain 2019-03-12 12:15:14 +01:00
parent 4e7a62bc2b
commit b9e427a9fc
5 changed files with 94 additions and 45 deletions

View File

@ -21,7 +21,17 @@ class AccountingPeriod < ActiveRecord::Base
end
def invoices
Invoice.where('created_at >= :start_date AND created_at <= :end_date', start_date: start_at, end_date: end_at)
Invoice.where('created_at >= :start_date AND CAST(created_at AS DATE) <= :end_date', start_date: start_at, end_date: end_at)
end
def invoices_with_vat(invoices)
invoices.map do |i|
if i.type == 'Avoir'
{ invoice: i, vat_rate: vat_rate(i.avoir_date) }
else
{ invoice: i, vat_rate: vat_rate(i.created_at) }
end
end
end
def archive_file
@ -36,8 +46,28 @@ class AccountingPeriod < ActiveRecord::Base
footprint == compute_footprint
end
def vat_rate(date)
first_rate = vat_history.first
return first_rate[:rate] if date < first_rate[:date]
vat_history.each do |h|
return h[:rate] if h[:date] <= date
end
end
private
def vat_history
key_dates = []
Setting.find_by(name: 'invoice_VAT-rate').history_values.each do |rate|
key_dates.push(date: rate.created_at, rate: (rate.value.to_i / 100.0))
end
Setting.find_by(name: 'invoice_VAT-active').history_values.each do |v|
key_dates.push(date: v.created_at, rate: 0) if v.value == 'false'
end
key_dates.sort_by { |k| k[:date] }
end
def to_json_archive(invoices)
previous_file = previous_period&.archive_file
code_checksum = Checksum.code
@ -45,7 +75,7 @@ class AccountingPeriod < ActiveRecord::Base
ApplicationController.new.view_context.render(
partial: 'archive/accounting',
locals: {
invoices: invoices,
invoices: invoices_with_vat(invoices),
period_total: period_total,
perpetual_total: perpetual_total,
period_footprint: footprint,
@ -70,9 +100,12 @@ class AccountingPeriod < ActiveRecord::Base
end
def compute_totals
self.period_total = invoices.all.map(&:total).reduce(:+) || 0
self.perpetual_total = Invoice.where('created_at <= :end_date AND type IS NULL', end_date: end_at)
.all.map(&:total).reduce(:+) || 0
self.period_total = (invoices.where(type: nil).all.map(&:total).reduce(:+) || 0) -
(invoices.where(type: 'Avoir').all.map(&:total).reduce(:+) || 0)
self.perpetual_total = (Invoice.where('CAST(created_at AS DATE) <= :end_date AND type IS NULL', end_date: end_at)
.all.map(&:total).reduce(:+) || 0) -
(Invoice.where("CAST(created_at AS DATE) <= :end_date AND type = 'Avoir'", end_date: end_at)
.all.map(&:total).reduce(:+) || 0)
self.footprint = compute_footprint
end

View File

@ -1,58 +1,35 @@
# frozen_string_literal: true
json.invoices do
json.array!(invoices) do |invoice|
json.extract! invoice, :id, :stp_invoice_id, :created_at, :reference, :footprint
json.total number_to_currency(invoice.total / 100.0)
json.extract! invoice[:invoice], :id, :stp_invoice_id, :created_at, :reference, :footprint
json.total number_to_currency(invoice[:invoice].total / 100.0)
json.invoiced do
json.type invoice.invoiced_type
json.id invoice.invoiced_id
if invoice.invoiced_type == Subscription.name
json.extract! invoice.invoiced, :stp_subscription_id, :created_at, :expiration_date, :canceled_at
json.plan do
json.extract! invoice.invoiced.plan, :id, :base_name, :interval, :interval_count, :stp_plan_id, :is_rolling
json.group do
json.extract! invoice.invoiced.plan.group, :id, :name
end
end
elsif invoice.invoiced_type == Reservation.name
json.extract! invoice.invoiced, :created_at, :stp_invoice_id
json.reservable do
json.type invoice.invoiced.reservable_type
json.id invoice.invoiced.reservable_id
if [Training.name, Machine.name, Space.name].include?(invoice.invoiced.reservable_type) &&
!invoice.invoiced.reservable.nil?
json.extract! invoice.invoiced.reservable, :name, :created_at
elsif invoice.invoiced.reservable_type == Event.name && !invoice.invoiced.reservable.nil?
json.extract! invoice.invoiced.reservable, :title, :created_at
json.prices do
json.standard_price number_to_currency(invoice.invoiced.reservable.amount / 100.0)
json.other_prices invoice.invoiced.reservable.event_price_categories do |price|
json.amount number_to_currency(price.amount / 100.0)
json.price_category do
json.extract! price.price_category, :id, :name, :created_at
end
end
end
end
end
json.type invoice[:invoice].invoiced_type
json.id invoice[:invoice].invoiced_id
if invoice[:invoice].invoiced_type == Subscription.name
json.partial! 'archive/subscription', invoiced: invoice[:invoice].invoiced
elsif invoice[:invoice].invoiced_type == Reservation.name
json.partial! 'archive/reservation', invoiced: invoice[:invoice].invoiced, vat_rate: invoice[:vat_rate]
end
end
json.user do
json.extract! invoice.user, :id, :email, :created_at
json.extract! invoice[:invoice].user, :id, :email, :created_at
json.profile do
json.extract! invoice.user.profile, :id, :first_name, :last_name, :birthday, :phone
json.gender invoice.user.profile.gender ? 'male' : 'female'
json.extract! invoice[:invoice].user.profile, :id, :first_name, :last_name, :birthday, :phone
json.gender invoice[:invoice].user.profile.gender ? 'male' : 'female'
end
end
json.invoice_items invoice.invoice_items do |item|
json.invoice_items invoice[:invoice].invoice_items do |item|
json.extract! item, :id, :stp_invoice_item_id, :created_at, :description, :footprint
json.amount number_to_currency(item.amount / 100.0)
json.partial! 'archive/vat', price: item.amount, vat_rate: invoice[:vat_rate]
end
end
end
json.totals do
json.period_total period_total / 100.0
json.perpetual_total perpetual_total / 100.0
json.period_total number_to_currency(period_total / 100.0)
json.perpetual_total number_to_currency(perpetual_total / 100.0)
end
json.software do

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
json.extract! invoiced, :created_at, :stp_invoice_id
json.reservable do
json.type invoiced.reservable_type
json.id invoiced.reservable_id
if [Training.name, Machine.name, Space.name].include?(invoiced.reservable_type) && !invoiced.reservable.nil?
json.extract! invoiced.reservable, :name, :created_at
elsif invoiced.reservable_type == Event.name && !invoiced.reservable.nil?
json.extract! invoiced.reservable, :title, :created_at
json.prices do
json.standard_price do
json.partial! 'archive/vat', price: invoiced.reservable.amount, vat_rate: vat_rate
end
json.other_prices invoiced.reservable.event_price_categories do |price|
json.partial! 'archive/vat', price: price.amount, vat_rate: vat_rate
json.price_category do
json.extract! price.price_category, :id, :name, :created_at
end
end
end
end
end

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
json.extract! invoiced, :stp_subscription_id, :created_at, :expiration_date, :canceled_at
json.plan do
json.extract! invoiced.plan, :id, :base_name, :interval, :interval_count, :stp_plan_id, :is_rolling
json.group do
json.extract! invoiced.plan.group, :id, :name
end
end

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
json.amount do
json.without_tax number_to_currency((price - (price * vat_rate)) / 100.0)
json.all_taxes_included number_to_currency(price / 100.0)
json.vat_rate vat_rate.positive? ? "#{vat_rate * 100} %" : 'none'
end