diff --git a/app/assets/stylesheets/modules/invoice.scss b/app/assets/stylesheets/modules/invoice.scss index f7439af14..5c1eee3ef 100644 --- a/app/assets/stylesheets/modules/invoice.scss +++ b/app/assets/stylesheets/modules/invoice.scss @@ -1,6 +1,14 @@ // admin invoices +.chained { + color: green; +} + +.broken { + color: red; +} + .invoice-placeholder { width: 80%; max-width: 800px; diff --git a/app/assets/templates/admin/invoices/index.html.erb b/app/assets/templates/admin/invoices/index.html.erb index 8243b6d88..1d8e5c6d1 100644 --- a/app/assets/templates/admin/invoices/index.html.erb +++ b/app/assets/templates/admin/invoices/index.html.erb @@ -61,6 +61,7 @@
{{ 'invoices.invoice_#' | translate }} | {{ 'invoices.date' | translate }} | @@ -74,6 +75,10 @@||
---|---|---|---|
+ + + | {{ invoice.reference }} | {{ invoice.date | amDateFormat:'L LTS' }} | {{ invoice.date | amDateFormat:'L' }} | diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 6e88a3ec8..cea959b65 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -212,16 +212,11 @@ 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) + self.footprint = compute_footprint + end - columns = Invoice.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 : ''}" + def check_footprint + invoice_items.map(&:check_footprint).all? && footprint == compute_footprint end private @@ -269,4 +264,17 @@ class Invoice < ActiveRecord::Base Invoice.where('created_at >= :start_date AND created_at < :end_date', start_date: start, end_date: ending).length end + def compute_footprint + 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| %w[footprint updated_at].include? c } + + sha256 = Digest::SHA256.new + sha256.hexdigest "#{columns.map { |c| self[c] }.join}#{previous.first ? previous.first.footprint : ''}" + end + end diff --git a/app/models/invoice_item.rb b/app/models/invoice_item.rb index e710e581c..caa02bee8 100644 --- a/app/models/invoice_item.rb +++ b/app/models/invoice_item.rb @@ -7,13 +7,23 @@ class InvoiceItem < ActiveRecord::Base after_create :chain_record def chain_record + self.footprint = compute_footprint + end + + def check_footprint + footprint == compute_footprint + end + + private + + def compute_footprint 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' } + .delete_if { |c| %w[footprint updated_at].include? c } sha256 = Digest::SHA256.new self.footprint = sha256.hexdigest "#{columns.map { |c| self[c] }.join}#{previous.first ? previous.first.footprint : ''}" diff --git a/app/views/api/invoices/list.json.jbuilder b/app/views/api/invoices/list.json.jbuilder index 648900267..629139c1d 100644 --- a/app/views/api/invoices/list.json.jbuilder +++ b/app/views/api/invoices/list.json.jbuilder @@ -12,4 +12,5 @@ json.array!(@invoices) do |invoice| json.stripe invoice.stp_invoice_id? json.date invoice.is_a?(Avoir) ? invoice.avoir_date : invoice.created_at json.prevent_refund invoice.prevent_refund? + json.chained_footprint invoice.check_footprint end