• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sleede / fab-manager / #106

pending completion
#106

push

coveralls-ruby

sylvainbx
Merge branch 'dev' for release 6.0.0

704 of 704 new or added lines in 168 files covered. (100.0%)

7919 of 13474 relevant lines covered (58.77%)

15.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

10.26
/lib/tasks/fablab/fix_references.rake
1
# frozen_string_literal: true
2

3
namespace :fablab do
1✔
4
  desc 'Fill the holes in the logical sequence of invoices references'
1✔
5
  task fix_references: :environment do |_task, _args|
1✔
6
    include ActionView::Helpers::NumberHelper
×
7
    include DbHelper
×
8

9
    user = User.adminsys || User.admins.first
×
10

11
    ActiveRecord::Base.transaction do
×
12
      missing_references = {}
×
13

14
      # browse invoices to list missing reference
15
      puts 'Computing missing references...'
×
16
      not_closed(Invoice).find_each do |invoice|
×
17
        number = Invoices::NumberService.number(invoice)
×
18
        next if number == 1
×
19

20
        previous = Invoice.where('created_at < :date', date: db_time(invoice.created_at))
×
21
                          .order(created_at: :desc)
22
                          .limit(1)
23
                          .first
24
        previous_saved_number = Invoices::NumberService.number(previous)
×
25
        previous_number = number - 1
×
26
        loop do
×
27
          break if previous_number.zero? || previous_number == previous_saved_number
×
28

29
          previous_invoice = Invoices::NumberService.find_by_number(previous_number, date: invoice.created_at)
×
30
          break if previous_invoice.present?
×
31

32
          missing_references[invoice.created_at] ||= []
×
33
          missing_references[invoice.created_at].push(previous_number)
×
34

35
          previous_number -= 1
×
36
        end
37
      end
38

39
      # create placeholder invoices for found missing references
40
      puts 'Creating missing invoices...'
×
41
      total = missing_references.values.filter(&:present?).flatten.count
×
42
      counter = 1
×
43
      missing_references.each_pair do |date, numbers|
×
44
        numbers.each_with_index do |number, index|
×
45
          print "#{counter} / #{total}\r"
×
46
          invoice = Invoice.new(
×
47
            total: 0,
48
            invoicing_profile: user.invoicing_profile,
49
            statistic_profile: user.statistic_profile,
50
            operator_profile: user.invoicing_profile,
51
            payment_method: '',
52
            created_at: date - (index + 1).seconds,
×
53
            invoice_items_attributes: [{
54
              amount: 0,
55
              description: I18n.t('invoices.null_invoice'),
56
              object_type: 'Error',
57
              object_id: 1,
58
              main: true
59
            }]
60
          )
61
          invoice.reference = PaymentDocumentService.generate_numbered_reference(number, invoice)
×
62
          invoice.save!
×
63
          counter += 1
×
64
        end
65
      end
66
      print "\n"
×
67
    end
68
  end
69

70
  # @param klass [Class]
71
  # @return [ActiveRecord::Relation<klass>,Class]
72
  def not_closed(klass)
1✔
73
    if AccountingPeriod.count.positive?
×
74
      last_period = AccountingPeriod.order(start_at: :desc).first
×
75
      klass.where('created_at > ?', last_period.end_at)
×
76
    else
77
      klass
×
78
    end
79
  end
80
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc