2019-03-13 16:49:03 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-13 17:48:35 +01:00
|
|
|
require 'checksum'
|
|
|
|
|
2019-03-13 16:49:03 +01:00
|
|
|
# A single line inside an invoice. Can be a subscription or a reservation
|
2016-03-23 18:39:41 +01:00
|
|
|
class InvoiceItem < ActiveRecord::Base
|
|
|
|
belongs_to :invoice
|
|
|
|
belongs_to :subscription
|
|
|
|
|
2016-04-07 16:00:12 +02:00
|
|
|
has_one :invoice_item # to associated invoice_items of an invoice to invoice_items of an avoir
|
2019-02-12 14:45:21 +01:00
|
|
|
|
|
|
|
after_create :chain_record
|
|
|
|
|
|
|
|
def chain_record
|
2019-02-12 16:00:36 +01:00
|
|
|
self.footprint = compute_footprint
|
2019-03-11 13:49:16 +01:00
|
|
|
save!
|
2019-02-12 16:00:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_footprint
|
|
|
|
footprint == compute_footprint
|
|
|
|
end
|
|
|
|
|
2019-03-20 11:01:53 +01:00
|
|
|
private
|
2019-02-12 16:00:36 +01:00
|
|
|
|
|
|
|
def compute_footprint
|
2019-05-27 11:11:21 +02:00
|
|
|
previous = InvoiceItem.where('id < ?', id)
|
|
|
|
.order('id DESC')
|
2019-02-12 14:45:21 +01:00
|
|
|
.limit(1)
|
|
|
|
|
|
|
|
columns = InvoiceItem.columns.map(&:name)
|
2019-02-12 16:00:36 +01:00
|
|
|
.delete_if { |c| %w[footprint updated_at].include? c }
|
2019-02-12 14:45:21 +01:00
|
|
|
|
2019-03-13 17:48:35 +01:00
|
|
|
Checksum.text("#{columns.map { |c| self[c] }.join}#{previous.first ? previous.first.footprint : ''}")
|
2019-02-12 14:45:21 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|