1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +01:00

accounting archive: insert code checksum and previous archive checksum

This commit is contained in:
Sylvain 2019-01-31 12:19:50 +01:00
parent 55ae979213
commit 6e274afe3b
5 changed files with 126 additions and 43 deletions

View File

@ -1,4 +1,5 @@
# frozen_string_literal: true
require 'version'
# API Controller to get the fab-manager version
class API::VersionController < API::ApiController
@ -6,8 +7,7 @@ class API::VersionController < API::ApiController
def show
authorize :version
package = File.read('package.json')
version = JSON.parse(package)['version']
render json: { version: version }, status: :ok
render json: { version: Version.current }, status: :ok
end
end

View File

@ -1,5 +1,8 @@
# frozen_string_literal: true
require 'checksum'
require 'version'
# AccountingPeriod is a period of N days (N > 0) which as been closed by an admin
# to prevent writing new accounting lines (invoices & refunds) during this period of time.
class AccountingPeriod < ActiveRecord::Base
@ -26,14 +29,27 @@ class AccountingPeriod < ActiveRecord::Base
private
def to_json_archive(invoices)
previous_file = previous_period&.archive_file
code_checksum = Checksum.code
last_archive_checksum = Checksum.file(previous_file)
ApplicationController.new.view_context.render(
partial: 'archive/accounting',
locals: { invoices: invoices },
locals: {
invoices: invoices,
code_checksum: code_checksum,
last_archive_checksum: last_archive_checksum,
previous_file: previous_file,
software_version: Version.current
},
formats: [:json],
handlers: [:jbuilder]
)
end
def previous_period
AccountingPeriod.order(closed_at: :desc).limit(2).last
end
def archive_closed_data
data = Invoice.where('created_at >= :start_date AND created_at < :end_date', start_date: start_at, end_date: end_at)
.includes(:invoice_items)

View File

@ -1,49 +1,62 @@
json.array!(invoices) do |invoice|
json.extract! invoice, :id, :stp_invoice_id, :created_at, :reference
json.total number_to_currency(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
json.invoices do
json.array!(invoices) do |invoice|
json.extract! invoice, :id, :stp_invoice_id, :created_at, :reference
json.total number_to_currency(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
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
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
end
end
end
json.user do
json.extract! 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.user do
json.extract! 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'
end
end
json.invoice_items invoice.invoice_items do |item|
json.extract! item, :id, :stp_invoice_item_id, :created_at, :description
json.amount number_to_currency(item.amount / 100.0)
end
end
json.invoice_items invoice.invoice_items do |item|
json.extract! item, :id, :stp_invoice_item_id, :created_at, :description
json.amount number_to_currency(item.amount / 100.0)
end
end
json.software do
json.name 'Fab-Manager'
json.version software_version
json.code_checksum code_checksum
end
json.previous_archive do
json.filename previous_file
json.checksum last_archive_checksum
end

45
lib/checksum.rb Normal file
View File

@ -0,0 +1,45 @@
# frozen_string_literal: true
# provide checksums for archiving control
class Checksum
class << self
def code
dir = Dir.pwd
files = children_files("#{dir}/*")
.concat(children_files("#{dir}/app/**/*"))
.concat(children_files("#{dir}/bin/**/*"))
.concat(children_files("#{dir}/config/**/*"))
.concat(children_files("#{dir}/db/**/*"))
.concat(children_files("#{dir}/doc/**/*"))
.concat(children_files("#{dir}/docker/**/*"))
.concat(children_files("#{dir}/lib/**/*"))
.concat(children_files("#{dir}/node_modules/**/*"))
.concat(children_files("#{dir}/plugins/**/*"))
.concat(children_files("#{dir}/provision/**/*"))
.concat(children_files("#{dir}/scripts/**/*"))
.concat(children_files("#{dir}/test/**/*"))
.concat(children_files("#{dir}/vendor/**/*"))
content = files.map { |f| File.read(f) }.join
sha256 = Digest::SHA256.new
sha256.hexdigest content
end
def file(path)
return unless File.exist?(path)
content = File.read(path)
sha256 = Digest::SHA256.new
sha256.hexdigest content
end
private
def children_files(path)
Dir[path].reject { |f| File.directory?(f) }
end
end
end

9
lib/version.rb Normal file
View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
# retrieve the current Fab-manager version
class Version
def self.current
package = File.read('package.json')
JSON.parse(package)['version']
end
end