From f9364b3872c1f38b6f457f13b64c55181bf518c5 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 12 Feb 2019 14:45:21 +0100 Subject: [PATCH] chains invoice and invoiceItem records. save them in archives --- app/models/invoice.rb | 16 +++++++++------- app/models/invoice_item.rb | 15 +++++++++++++++ app/views/archive/_accounting.json.jbuilder | 4 ++-- db/schema.rb | 16 ++++++++++++++-- lib/tasks/fablab/setup.rake | 21 +++++++++++++++++++++ 5 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 lib/tasks/fablab/setup.rake diff --git a/app/models/invoice.rb b/app/models/invoice.rb index aed068070..6e88a3ec8 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -212,14 +212,16 @@ class Invoice < ActiveRecord::Base end def chain_record + max_date = created_at || DateTime.now + previous = Invoice.where('created_at < ?', max_date) + .order('created_at DESC') + .limit(1) + + columns = Invoice.columns.map(&:name) + .delete_if { |c| c == 'footprint' } + sha256 = Digest::SHA256.new - - previous = Invoice.where(created_at: Invoice.select('MAX(created_at)')).limit(1).first - - self.footprint = sha256.hexdigest "#{id}#{invoice_id}#{invoiced_type}#{stp_invoice_id}#{total}#{created_at}#{updated_at}" \ - "#{user_id}#{reference}#{avoir_mode}#{avoir_date}#{invoice_id}#{type}" \ - "#{subscription_to_expire}#{description}#{wallet_amount}#{wallet_transaction_id}" \ - "#{coupon_id}#{previous ? previous.footprint : ''}" + self.footprint = sha256.hexdigest "#{columns.map { |c| self[c] }.join}#{previous.first ? previous.first.footprint : ''}" end private diff --git a/app/models/invoice_item.rb b/app/models/invoice_item.rb index 23eeeeaa6..e710e581c 100644 --- a/app/models/invoice_item.rb +++ b/app/models/invoice_item.rb @@ -3,4 +3,19 @@ class InvoiceItem < ActiveRecord::Base belongs_to :subscription has_one :invoice_item # to associated invoice_items of an invoice to invoice_items of an avoir + + after_create :chain_record + + def chain_record + max_date = created_at || Time.current + previous = InvoiceItem.where('created_at < ?', max_date) + .order('created_at DESC') + .limit(1) + + columns = InvoiceItem.columns.map(&:name) + .delete_if { |c| c == 'footprint' } + + sha256 = Digest::SHA256.new + self.footprint = sha256.hexdigest "#{columns.map { |c| self[c] }.join}#{previous.first ? previous.first.footprint : ''}" + end end diff --git a/app/views/archive/_accounting.json.jbuilder b/app/views/archive/_accounting.json.jbuilder index 6ea88f785..e8ee7bd6b 100644 --- a/app/views/archive/_accounting.json.jbuilder +++ b/app/views/archive/_accounting.json.jbuilder @@ -1,6 +1,6 @@ json.invoices do json.array!(invoices) do |invoice| - json.extract! invoice, :id, :stp_invoice_id, :created_at, :reference + json.extract! invoice, :id, :stp_invoice_id, :created_at, :reference, :footprint json.total number_to_currency(invoice.total / 100.0) json.invoiced do json.type invoice.invoiced_type @@ -44,7 +44,7 @@ json.invoices do end end json.invoice_items invoice.invoice_items do |item| - json.extract! item, :id, :stp_invoice_item_id, :created_at, :description + json.extract! item, :id, :stp_invoice_item_id, :created_at, :description, :footprint json.amount number_to_currency(item.amount / 100.0) end end diff --git a/db/schema.rb b/db/schema.rb index 49ee108f1..b08bed605 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,12 +11,12 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20190110150532) do +ActiveRecord::Schema.define(version: 20190211124726) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - enable_extension "unaccent" enable_extension "pg_trgm" + enable_extension "unaccent" create_table "abuses", force: :cascade do |t| t.integer "signaled_id" @@ -31,6 +31,15 @@ ActiveRecord::Schema.define(version: 20190110150532) do add_index "abuses", ["signaled_type", "signaled_id"], name: "index_abuses_on_signaled_type_and_signaled_id", using: :btree + create_table "accounting_periods", force: :cascade do |t| + t.date "start_at" + t.date "end_at" + t.datetime "closed_at" + t.integer "closed_by" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "addresses", force: :cascade do |t| t.string "address" t.string "street_number" @@ -241,6 +250,7 @@ ActiveRecord::Schema.define(version: 20190110150532) do t.text "description" t.integer "subscription_id" t.integer "invoice_item_id" + t.string "footprint" end add_index "invoice_items", ["invoice_id"], name: "index_invoice_items_on_invoice_id", using: :btree @@ -263,6 +273,7 @@ ActiveRecord::Schema.define(version: 20190110150532) do t.integer "wallet_amount" t.integer "wallet_transaction_id" t.integer "coupon_id" + t.string "footprint" end add_index "invoices", ["coupon_id"], name: "index_invoices_on_coupon_id", using: :btree @@ -855,6 +866,7 @@ ActiveRecord::Schema.define(version: 20190110150532) do add_index "wallets", ["user_id"], name: "index_wallets_on_user_id", using: :btree + add_foreign_key "accounting_periods", "users", column: "closed_by" add_foreign_key "availability_tags", "availabilities" add_foreign_key "availability_tags", "tags" add_foreign_key "event_price_categories", "events" diff --git a/lib/tasks/fablab/setup.rake b/lib/tasks/fablab/setup.rake new file mode 100644 index 000000000..76afc30af --- /dev/null +++ b/lib/tasks/fablab/setup.rake @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +namespace :fablab do + namespace :setup do + desc 'assign all footprints to existing Invoice records' + task chain_invoices_records: :environment do + Invoice.order(:created_at).all.each do |i| + i.chain_record + i.save! + end + end + + desc 'assign all footprints to existing InvoiceItem records' + task chain_invoices_items_records: :environment do + InvoiceItem.order(:created_at).all.each do |i| + i.chain_record + i.save! + end + end + end +end \ No newline at end of file