mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-26 20:54:21 +01:00
(feat) build accounting data for yesterday by default
This commit is contained in:
parent
43f45c383f
commit
7bb97d267d
@ -14,16 +14,20 @@ class Accounting::AccountingService
|
||||
def build(start_date, end_date)
|
||||
invoices = Invoice.where('created_at >= ? AND created_at <= ?', start_date, end_date).order('created_at ASC')
|
||||
build_from_invoices(invoices)
|
||||
invoices.map(&:id)
|
||||
end
|
||||
|
||||
# build accounting lines for the provided invoices
|
||||
def build_from_invoices(invoices)
|
||||
lines = []
|
||||
processed = []
|
||||
invoices.find_each do |i|
|
||||
Rails.logger.debug { "processing invoice #{i.id}..." } unless Rails.env.test?
|
||||
lines.concat(generate_lines(i))
|
||||
processed.push(i.id)
|
||||
end
|
||||
AccountingLine.create!(lines)
|
||||
processed
|
||||
end
|
||||
|
||||
def self.status
|
||||
|
@ -4,13 +4,26 @@
|
||||
class AccountingWorker
|
||||
include Sidekiq::Worker
|
||||
|
||||
def perform(action = :today, *params)
|
||||
attr_reader :performed
|
||||
|
||||
def perform(action = :yesterday, *params)
|
||||
send(action, *params)
|
||||
end
|
||||
|
||||
def today
|
||||
service = Accounting::AccountingService.new
|
||||
service.build(DateTime.current.beginning_of_day, DateTime.current.end_of_day)
|
||||
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}"
|
||||
end
|
||||
|
||||
def invoices(invoices_ids)
|
||||
@ -19,7 +32,8 @@ class AccountingWorker
|
||||
# build
|
||||
service = Accounting::AccountingService.new
|
||||
invoices = Invoice.where(id: invoices_ids)
|
||||
service.build_from_invoices(invoices)
|
||||
ids = service.build_from_invoices(invoices)
|
||||
@performed = "invoices: #{ids}"
|
||||
end
|
||||
|
||||
def all
|
||||
@ -27,6 +41,7 @@ class AccountingWorker
|
||||
AccountingLine.delete_all
|
||||
# build
|
||||
service = Accounting::AccountingService.new
|
||||
service.build_from_invoices(Invoice.all)
|
||||
ids = service.build_from_invoices(Invoice.all)
|
||||
@performed = "all: #{ids}"
|
||||
end
|
||||
end
|
||||
|
39
test/workers/accounting_worker_test.rb
Normal file
39
test/workers/accounting_worker_test.rb
Normal file
@ -0,0 +1,39 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'test_helper'
|
||||
require 'minitest/autorun'
|
||||
require 'sidekiq/testing'
|
||||
|
||||
class AccountingWorkerTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
Sidekiq::Testing.inline!
|
||||
@worker = AccountingWorker.new
|
||||
end
|
||||
|
||||
test 'build accounting lines for yesterday by default' do
|
||||
date = DateTime.current.midnight
|
||||
travel_to(date)
|
||||
@worker.perform
|
||||
assert_match(/^yesterday:/, @worker.performed)
|
||||
assert_match(date.yesterday.to_date.iso8601, @worker.performed)
|
||||
end
|
||||
|
||||
test 'build accounting lines for today' do
|
||||
@worker.perform(:today)
|
||||
assert_match(/^today:/, @worker.performed)
|
||||
assert_match(DateTime.current.to_date.iso8601, @worker.performed)
|
||||
end
|
||||
|
||||
test 'build specified invoices selection' do
|
||||
ids = [5820, 5821, 5822]
|
||||
@worker.perform(:invoices, ids)
|
||||
assert_match(/^invoices:/, @worker.performed)
|
||||
assert_match(ids.to_s, @worker.performed)
|
||||
end
|
||||
|
||||
test 'build all invoices' do
|
||||
@worker.perform(:all)
|
||||
assert_match(/^all:/, @worker.performed)
|
||||
assert_match(Invoice.all.map(&:id).to_s, @worker.performed)
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user