2022-11-18 16:42:11 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Periodically build the accounting data (AccountingLine) from the Invoices & Avoirs
|
|
|
|
class AccountingWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
2023-01-04 10:27:14 +01:00
|
|
|
attr_reader :performed
|
|
|
|
|
|
|
|
def perform(action = :yesterday, *params)
|
2022-11-22 17:43:19 +01:00
|
|
|
send(action, *params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def today
|
2022-11-18 16:42:11 +01:00
|
|
|
service = Accounting::AccountingService.new
|
2023-01-04 10:27:14 +01:00
|
|
|
start = DateTime.current.beginning_of_day
|
|
|
|
finish = DateTime.current.end_of_day
|
|
|
|
ids = service.build(start, finish)
|
|
|
|
@performed = "today: #{start} -> #{finish}; invoices: #{ids}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def yesterday
|
|
|
|
service = Accounting::AccountingService.new
|
|
|
|
start = DateTime.yesterday.beginning_of_day
|
|
|
|
finish = DateTime.yesterday.end_of_day
|
|
|
|
ids = service.build(start, finish)
|
|
|
|
@performed = "yesterday: #{start} -> #{finish}; invoices: #{ids}"
|
2022-11-18 16:42:11 +01:00
|
|
|
end
|
2022-11-22 17:43:19 +01:00
|
|
|
|
|
|
|
def invoices(invoices_ids)
|
2022-12-08 15:15:47 +01:00
|
|
|
# clean
|
2022-12-23 15:52:10 +01:00
|
|
|
AccountingLine.where(invoice_id: invoices_ids).delete_all
|
2022-12-08 15:15:47 +01:00
|
|
|
# build
|
2022-11-22 17:43:19 +01:00
|
|
|
service = Accounting::AccountingService.new
|
|
|
|
invoices = Invoice.where(id: invoices_ids)
|
2023-01-04 10:27:14 +01:00
|
|
|
ids = service.build_from_invoices(invoices)
|
|
|
|
@performed = "invoices: #{ids}"
|
2022-11-22 17:43:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def all
|
2022-12-08 15:15:47 +01:00
|
|
|
# clean
|
2022-12-08 16:48:55 +01:00
|
|
|
AccountingLine.delete_all
|
2022-12-08 15:15:47 +01:00
|
|
|
# build
|
2022-11-22 17:43:19 +01:00
|
|
|
service = Accounting::AccountingService.new
|
2023-01-04 10:27:14 +01:00
|
|
|
ids = service.build_from_invoices(Invoice.all)
|
|
|
|
@performed = "all: #{ids}"
|
2022-11-22 17:43:19 +01:00
|
|
|
end
|
2022-11-18 16:42:11 +01:00
|
|
|
end
|