mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-19 13:54:25 +01:00
chains invoice and invoiceItem records. save them in archives
This commit is contained in:
parent
cc124a0dee
commit
f9364b3872
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
16
db/schema.rb
16
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"
|
||||
|
21
lib/tasks/fablab/setup.rake
Normal file
21
lib/tasks/fablab/setup.rake
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user