From e50c90b30d93ae54163c10ef4818a218877313b4 Mon Sep 17 00:00:00 2001 From: Du Peng Date: Mon, 3 Jul 2023 17:43:27 +0200 Subject: [PATCH] (bug) Accouning Line in duplicate --- CHANGELOG.md | 2 ++ Gemfile.lock | 3 +++ app/services/accounting/accounting_service.rb | 16 ++++++++++++---- app/workers/accounting_worker.rb | 4 ---- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bd02d555..b01f784dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog Fab-manager - Fix a bug: unable to refresh machine/space/training calender after pay an reservation +- Fix a bug: Accouning Line in duplicate +- [TODO DEPLOY] `rails fablab:setup:build_accounting_lines` ## v6.0.7 2023 June 20 diff --git a/Gemfile.lock b/Gemfile.lock index 4606b128b..5cb4eaf82 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -269,6 +269,8 @@ GEM net-smtp (0.3.3) net-protocol nio4r (2.5.8) + nokogiri (1.14.3-x86_64-darwin) + racc (~> 1.4) nokogiri (1.14.3-x86_64-linux) racc (~> 1.4) oauth2 (1.4.4) @@ -524,6 +526,7 @@ GEM zeitwerk (2.6.7) PLATFORMS + x86_64-darwin-20 x86_64-linux DEPENDENCIES diff --git a/app/services/accounting/accounting_service.rb b/app/services/accounting/accounting_service.rb index ee224065e..1ecfb4b28 100644 --- a/app/services/accounting/accounting_service.rb +++ b/app/services/accounting/accounting_service.rb @@ -22,11 +22,19 @@ class Accounting::AccountingService 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) + Rails.logger.debug { "[AccountLine] processing invoice #{i.id}..." } unless Rails.env.test? + if i.main_item.nil? + Rails.logger.error { "[AccountLine] invoice #{i.id} main_item is nil" } unless Rails.env.test? + else + lines.concat(generate_lines(i)) + processed.push(i.id) + end + end + ActiveRecord::Base.transaction do + ids = invoices.map(&:id) + AccountingLine.where(invoice_id: ids).delete_all + AccountingLine.create!(lines) end - AccountingLine.create!(lines) processed end diff --git a/app/workers/accounting_worker.rb b/app/workers/accounting_worker.rb index e2cf5f448..71b5a443a 100644 --- a/app/workers/accounting_worker.rb +++ b/app/workers/accounting_worker.rb @@ -27,8 +27,6 @@ class AccountingWorker end def invoices(invoices_ids) - # clean - AccountingLine.where(invoice_id: invoices_ids).delete_all # build service = Accounting::AccountingService.new invoices = Invoice.where(id: invoices_ids) @@ -37,8 +35,6 @@ class AccountingWorker end def all - # clean - AccountingLine.delete_all # build service = Accounting::AccountingService.new ids = service.build_from_invoices(Invoice.all)