1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-29 18:52:22 +01:00

factorized footprint computation

This commit is contained in:
Sylvain 2019-09-18 15:09:14 +02:00
parent 276a99c068
commit 02c7cb801f
5 changed files with 26 additions and 27 deletions

View File

@ -25,14 +25,6 @@ class HistoryValue < ActiveRecord::Base
private
def compute_footprint
max_date = created_at || Time.current
previous = HistoryValue.where('created_at < ?', max_date)
.order('created_at DESC')
.limit(1)
columns = HistoryValue.columns.map(&:name)
.delete_if { |c| %w[footprint updated_at].include? c }
Checksum.text("#{columns.map { |c| self[c] }.join}#{previous.first ? previous.first.footprint : ''}")
FootprintService.compute_footprint(HistoryValue, self, 'created_at')
end
end

View File

@ -302,14 +302,7 @@ class Invoice < ActiveRecord::Base
end
def compute_footprint
previous = Invoice.where('id < ?', id)
.order('id DESC')
.limit(1)
columns = Invoice.columns.map(&:name)
.delete_if { |c| %w[footprint updated_at].include? c }
Checksum.text("#{columns.map { |c| self[c] }.join}#{previous.first ? previous.first.footprint : ''}")
FootprintService.compute_footprint(Invoice, self)
end
def log_changes

View File

@ -24,14 +24,7 @@ class InvoiceItem < ActiveRecord::Base
private
def compute_footprint
previous = InvoiceItem.where('id < ?', id)
.order('id DESC')
.limit(1)
columns = InvoiceItem.columns.map(&:name)
.delete_if { |c| %w[footprint updated_at].include? c }
Checksum.text("#{columns.map { |c| self[c] }.join}#{previous.first ? previous.first.footprint : ''}")
FootprintService.compute_footprint(InvoiceItem, self)
end
def log_changes

View File

@ -186,7 +186,7 @@ class AccountingExportService
# we do not render the VAT row if it was disabled for this invoice
return nil if rate.zero?
# FIXME, round at +/-0.01 if total > sum(items)
# FIXME, +/-0.01
vat = (invoice.total - (invoice.total / (rate / 100.00 + 1))) / 100.00
row = ''
columns.each do |column|
@ -237,7 +237,7 @@ class AccountingExportService
end
when :reservation
if invoice.invoiced_type == 'Reservation'
Setting.find_by(name: "accounting_#{invoice.invoiced.reservable_type}_#{type}")&.value
Setting.find_by(name: "accounting_#{invoice.invoiced.reservable_type}_#{type}")&.value
else
puts "WARN: Invoice #{invoice.id} has no reservation"
end

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
# Provides helper methods to compute footprints
class FootprintService
# Compute the footprint
# @param class_name Invoice|InvoiceItem|HistoryValue
# @param item an instance of the provided class
# @param sort the items in database by the provided criterion, to find the previous one
def self.compute_footprint(klass, item, sort_on = 'id')
raise TypeError unless item.class.name == klass.name
previous = klass.where("#{sort_on} < ?", item[sort_on])
.order("#{sort_on} DESC")
.limit(1)
columns = klass.columns.map(&:name)
.delete_if { |c| %w[footprint updated_at].include? c }
Checksum.text("#{columns.map { |c| item[c] }.join}#{previous.first ? previous.first.footprint : ''}")
end
end