2019-01-08 17:32:45 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-31 12:19:50 +01:00
|
|
|
require 'checksum'
|
|
|
|
require 'version'
|
2019-03-13 17:48:35 +01:00
|
|
|
require 'zip'
|
2019-01-31 12:19:50 +01:00
|
|
|
|
2019-01-08 17:32:45 +01:00
|
|
|
# AccountingPeriod is a period of N days (N > 0) which as been closed by an admin
|
|
|
|
# to prevent writing new accounting lines (invoices & refunds) during this period of time.
|
2020-03-25 10:16:47 +01:00
|
|
|
class AccountingPeriod < ApplicationRecord
|
2019-01-07 12:29:52 +01:00
|
|
|
before_destroy { false }
|
|
|
|
before_update { false }
|
2019-02-25 14:51:19 +01:00
|
|
|
before_create :compute_totals
|
2019-04-04 11:37:23 +02:00
|
|
|
after_commit :archive_closed_data, on: [:create]
|
2019-01-07 12:29:52 +01:00
|
|
|
|
2019-01-08 17:32:45 +01:00
|
|
|
validates :start_at, :end_at, :closed_at, :closed_by, presence: true
|
|
|
|
validates_with DateRangeValidator
|
2019-04-03 13:04:19 +02:00
|
|
|
validates_with DurationValidator
|
2019-04-03 17:27:25 +02:00
|
|
|
validates_with PastPeriodValidator
|
2019-01-08 17:32:45 +01:00
|
|
|
validates_with PeriodOverlapValidator
|
2019-02-12 17:38:44 +01:00
|
|
|
validates_with PeriodIntegrityValidator
|
2019-01-08 17:32:45 +01:00
|
|
|
|
2019-01-07 12:29:52 +01:00
|
|
|
def delete
|
|
|
|
false
|
|
|
|
end
|
2019-01-10 10:52:29 +01:00
|
|
|
|
2019-02-25 14:51:19 +01:00
|
|
|
def invoices
|
2019-03-12 12:15:14 +01:00
|
|
|
Invoice.where('created_at >= :start_date AND CAST(created_at AS DATE) <= :end_date', start_date: start_at, end_date: end_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def invoices_with_vat(invoices)
|
2019-08-01 11:26:40 +02:00
|
|
|
vat_service = VatHistoryService.new
|
2019-03-12 12:15:14 +01:00
|
|
|
invoices.map do |i|
|
2019-08-01 11:26:40 +02:00
|
|
|
{ invoice: i, vat_rate: vat_service.invoice_vat(i) }
|
2019-03-12 12:15:14 +01:00
|
|
|
end
|
2019-02-25 14:51:19 +01:00
|
|
|
end
|
|
|
|
|
2019-03-13 17:48:35 +01:00
|
|
|
def archive_folder
|
2019-03-18 14:33:30 +01:00
|
|
|
dir = "accounting/#{id}"
|
2019-01-10 10:52:29 +01:00
|
|
|
|
|
|
|
# create directory if it doesn't exists (accounting)
|
|
|
|
FileUtils.mkdir_p dir
|
2019-03-13 17:48:35 +01:00
|
|
|
dir
|
|
|
|
end
|
|
|
|
|
|
|
|
def archive_file
|
|
|
|
"#{archive_folder}/#{start_at.iso8601}_#{end_at.iso8601}.zip"
|
|
|
|
end
|
|
|
|
|
|
|
|
def archive_json_file
|
2019-03-18 14:33:30 +01:00
|
|
|
"#{start_at.iso8601}_#{end_at.iso8601}.json"
|
2019-01-10 10:52:29 +01:00
|
|
|
end
|
|
|
|
|
2019-03-11 16:11:49 +01:00
|
|
|
def check_footprint
|
|
|
|
footprint == compute_footprint
|
|
|
|
end
|
|
|
|
|
2019-04-03 17:57:21 +02:00
|
|
|
def previous_period
|
|
|
|
AccountingPeriod.where('closed_at < ?', closed_at).order(closed_at: :desc).limit(1).last
|
|
|
|
end
|
|
|
|
|
2019-01-10 15:12:22 +01:00
|
|
|
private
|
|
|
|
|
2019-01-10 10:52:29 +01:00
|
|
|
def archive_closed_data
|
2019-04-03 17:57:21 +02:00
|
|
|
ArchiveWorker.perform_async(id)
|
2019-01-10 10:52:29 +01:00
|
|
|
end
|
2019-02-25 14:51:19 +01:00
|
|
|
|
2019-03-12 13:36:10 +01:00
|
|
|
def price_without_taxe(invoice)
|
|
|
|
invoice[:invoice].total - (invoice[:invoice].total * invoice[:vat_rate])
|
|
|
|
end
|
|
|
|
|
2019-02-25 14:51:19 +01:00
|
|
|
def compute_totals
|
2019-03-12 13:36:10 +01:00
|
|
|
period_invoices = invoices_with_vat(invoices.where(type: nil))
|
|
|
|
period_avoirs = invoices_with_vat(invoices.where(type: 'Avoir'))
|
|
|
|
self.period_total = (period_invoices.map(&method(:price_without_taxe)).reduce(:+) || 0) -
|
|
|
|
(period_avoirs.map(&method(:price_without_taxe)).reduce(:+) || 0)
|
|
|
|
|
|
|
|
all_invoices = invoices_with_vat(Invoice.where('CAST(created_at AS DATE) <= :end_date AND type IS NULL', end_date: end_at))
|
|
|
|
all_avoirs = invoices_with_vat(Invoice.where("CAST(created_at AS DATE) <= :end_date AND type = 'Avoir'", end_date: end_at))
|
|
|
|
self.perpetual_total = (all_invoices.map(&method(:price_without_taxe)).reduce(:+) || 0) -
|
|
|
|
(all_avoirs.map(&method(:price_without_taxe)).reduce(:+) || 0)
|
2019-02-25 14:51:19 +01:00
|
|
|
self.footprint = compute_footprint
|
|
|
|
end
|
|
|
|
|
|
|
|
def compute_footprint
|
2019-03-11 16:11:49 +01:00
|
|
|
columns = AccountingPeriod.columns.map(&:name)
|
2019-03-12 15:26:17 +01:00
|
|
|
.delete_if { |c| %w[id footprint created_at updated_at].include? c }
|
2019-02-25 14:51:19 +01:00
|
|
|
|
2019-03-13 17:48:35 +01:00
|
|
|
Checksum.text("#{columns.map { |c| self[c] }.join}#{previous_period ? previous_period.footprint : ''}")
|
2019-02-25 14:51:19 +01:00
|
|
|
end
|
2019-01-07 12:29:52 +01:00
|
|
|
end
|