mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-21 15:54:22 +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
|
class AccountingPeriod < ActiveRecord::Base
|
||||||
before_destroy { false }
|
before_destroy { false }
|
||||||
before_update { false }
|
before_update { false }
|
||||||
|
before_create :compute_totals
|
||||||
after_create :archive_closed_data
|
after_create :archive_closed_data
|
||||||
|
|
||||||
validates :start_at, :end_at, :closed_at, :closed_by, presence: true
|
validates :start_at, :end_at, :closed_at, :closed_by, presence: true
|
||||||
@ -19,6 +20,10 @@ class AccountingPeriod < ActiveRecord::Base
|
|||||||
false
|
false
|
||||||
end
|
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
|
def archive_file
|
||||||
dir = 'accounting'
|
dir = 'accounting'
|
||||||
|
|
||||||
@ -37,6 +42,9 @@ class AccountingPeriod < ActiveRecord::Base
|
|||||||
partial: 'archive/accounting',
|
partial: 'archive/accounting',
|
||||||
locals: {
|
locals: {
|
||||||
invoices: invoices,
|
invoices: invoices,
|
||||||
|
period_total: period_total,
|
||||||
|
perpetual_total: perpetual_total,
|
||||||
|
period_footprint: footprint,
|
||||||
code_checksum: code_checksum,
|
code_checksum: code_checksum,
|
||||||
last_archive_checksum: last_archive_checksum,
|
last_archive_checksum: last_archive_checksum,
|
||||||
previous_file: previous_file,
|
previous_file: previous_file,
|
||||||
@ -52,8 +60,21 @@ class AccountingPeriod < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def archive_closed_data
|
def archive_closed_data
|
||||||
data = Invoice.where('created_at >= :start_date AND created_at < :end_date', start_date: start_at, end_date: end_at)
|
data = invoices.includes(:invoice_items)
|
||||||
.includes(:invoice_items)
|
|
||||||
File.write(archive_file, to_json_archive(data))
|
File.write(archive_file, to_json_archive(data))
|
||||||
end
|
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
|
end
|
||||||
|
@ -50,6 +50,11 @@ json.invoices do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
json.totals do
|
||||||
|
json.period_total period_total / 100.0
|
||||||
|
json.perpetual_total perpetual_total / 100.0
|
||||||
|
end
|
||||||
|
|
||||||
json.software do
|
json.software do
|
||||||
json.name 'Fab-Manager'
|
json.name 'Fab-Manager'
|
||||||
json.version software_version
|
json.version software_version
|
||||||
@ -60,3 +65,5 @@ json.previous_archive do
|
|||||||
json.filename previous_file
|
json.filename previous_file
|
||||||
json.checksum last_archive_checksum
|
json.checksum last_archive_checksum
|
||||||
end
|
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.
|
# 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
@ -36,8 +36,11 @@ ActiveRecord::Schema.define(version: 20190211124726) do
|
|||||||
t.date "end_at"
|
t.date "end_at"
|
||||||
t.datetime "closed_at"
|
t.datetime "closed_at"
|
||||||
t.integer "closed_by"
|
t.integer "closed_by"
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
|
t.integer "period_total"
|
||||||
|
t.integer "perpetual_total"
|
||||||
|
t.string "footprint"
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "addresses", force: :cascade do |t|
|
create_table "addresses", force: :cascade do |t|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user