1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/models/invoice_item.rb

37 lines
951 B
Ruby
Raw Normal View History

2019-03-13 16:49:03 +01:00
# frozen_string_literal: true
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
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
private
2019-02-12 16:00:36 +01:00
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)
2019-02-12 16:00:36 +01:00
.delete_if { |c| %w[footprint updated_at].include? c }
Checksum.text("#{columns.map { |c| self[c] }.join}#{previous.first ? previous.first.footprint : ''}")
end
2016-03-23 18:39:41 +01:00
end