mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-20 14:54:15 +01:00
compute, secure and archive period total and cumulative total of each accounting period
This commit is contained in:
parent
1321e196f2
commit
f11f629bcf
@ -8,6 +8,7 @@ require 'version'
|
||||
class AccountingPeriod < ActiveRecord::Base
|
||||
before_destroy { false }
|
||||
before_update { false }
|
||||
before_create :compute_totals
|
||||
after_create :archive_closed_data
|
||||
|
||||
validates :start_at, :end_at, :closed_at, :closed_by, presence: true
|
||||
@ -19,6 +20,10 @@ class AccountingPeriod < ActiveRecord::Base
|
||||
false
|
||||
end
|
||||
|
||||
def invoices
|
||||
Invoice.where('created_at >= :start_date AND created_at < :end_date', start_date: start_at, end_date: end_at)
|
||||
end
|
||||
|
||||
def archive_file
|
||||
dir = 'accounting'
|
||||
|
||||
@ -37,6 +42,9 @@ class AccountingPeriod < ActiveRecord::Base
|
||||
partial: 'archive/accounting',
|
||||
locals: {
|
||||
invoices: invoices,
|
||||
period_total: period_total,
|
||||
perpetual_total: perpetual_total,
|
||||
period_footprint: footprint,
|
||||
code_checksum: code_checksum,
|
||||
last_archive_checksum: last_archive_checksum,
|
||||
previous_file: previous_file,
|
||||
@ -52,8 +60,21 @@ class AccountingPeriod < ActiveRecord::Base
|
||||
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)
|
||||
data = invoices.includes(:invoice_items)
|
||||
File.write(archive_file, to_json_archive(data))
|
||||
end
|
||||
|
||||
def compute_totals
|
||||
self.period_total = invoices.all.map(&:total).reduce(:+)
|
||||
self.perpetual_total = Invoice.where('created_at < :end_date', end_date: end_at).all.map(&:total).reduce(:+)
|
||||
self.footprint = compute_footprint
|
||||
end
|
||||
|
||||
def compute_footprint
|
||||
columns = Invoice.columns.map(&:name)
|
||||
.delete_if { |c| %w[footprint updated_at].include? c }
|
||||
|
||||
sha256 = Digest::SHA256.new
|
||||
sha256.hexdigest "#{columns.map { |c| self[c] }.join}#{previous_period ? previous_period.footprint : ''}"
|
||||
end
|
||||
end
|
||||
|
@ -50,6 +50,11 @@ json.invoices do
|
||||
end
|
||||
end
|
||||
|
||||
json.totals do
|
||||
json.period_total period_total / 100.0
|
||||
json.perpetual_total perpetual_total / 100.0
|
||||
end
|
||||
|
||||
json.software do
|
||||
json.name 'Fab-Manager'
|
||||
json.version software_version
|
||||
@ -60,3 +65,5 @@ json.previous_archive do
|
||||
json.filename previous_file
|
||||
json.checksum last_archive_checksum
|
||||
end
|
||||
|
||||
json.period_footprint period_footprint
|
||||
|
@ -0,0 +1,6 @@
|
||||
class AddTotalsToAccountingPeriod < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :accounting_periods, :period_total, :integer
|
||||
add_column :accounting_periods, :perpetual_total, :integer
|
||||
end
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
class AddFootprintToAccountingPeriod < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :accounting_periods, :footprint, :string
|
||||
end
|
||||
end
|
@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20190211124726) do
|
||||
ActiveRecord::Schema.define(version: 20190225102847) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@ -36,8 +36,11 @@ ActiveRecord::Schema.define(version: 20190211124726) do
|
||||
t.date "end_at"
|
||||
t.datetime "closed_at"
|
||||
t.integer "closed_by"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "period_total"
|
||||
t.integer "perpetual_total"
|
||||
t.string "footprint"
|
||||
end
|
||||
|
||||
create_table "addresses", force: :cascade do |t|
|
||||
|
Loading…
x
Reference in New Issue
Block a user