1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

(bug) empty object for some invoice item

This commit is contained in:
Sylvain 2023-01-06 10:14:45 +01:00
parent 44a167f4c7
commit 5dae938078
5 changed files with 85 additions and 6 deletions

View File

@ -1,7 +1,10 @@
# Changelog Fab-manager
- Fix a bug: unable to build accounting lines if no invoices
- Fix a bug: unable to apply rounding correction on accounting lines
- Fix a bug: empty object for some invoice item
- Fix a security issue: updated json5 to 1.0.2 to fix [CVE-2022-46175](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-46175)
- [TODO DEPLOY] `rails fablab:fix_invoice_items` => run this script BEFORE running the migrations
## v5.6.0 2023 January 5

View File

@ -169,6 +169,6 @@ class Accounting::AccountingService
diff = debit_sum - credit_sum
fixable_line = lines.filter { |l| l[:line_type] == 'payment' }.last
fixable_line.credit += diff
fixable_line[:credit] += diff
end
end

View File

@ -0,0 +1,10 @@
# frozen_string_literal: true
# From this migration, ths object_type and object_id columns in InvoiceItem won't be able to be null anymore
# This will prevent issues while building the accounting data, and ensure data integrity
class AddNotNullToInvoiceItemsObject < ActiveRecord::Migration[5.2]
def change
change_column_null :invoice_items, :object_type, false
change_column_null :invoice_items, :object_id, false
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_12_27_141529) do
ActiveRecord::Schema.define(version: 2023_01_06_081943) do
# These are extensions that must be enabled in order to support this database
enable_extension "fuzzystrmatch"
@ -250,7 +250,8 @@ ActiveRecord::Schema.define(version: 2022_12_27_141529) do
t.index ["user_id"], name: "index_exports_on_user_id"
end
create_table "footprint_debugs", force: :cascade do |t|
create_table "footprint_debugs", id: false, force: :cascade do |t|
t.bigserial "id", null: false
t.string "footprint"
t.string "data"
t.string "klass"
@ -331,8 +332,8 @@ ActiveRecord::Schema.define(version: 2022_12_27_141529) do
t.text "description"
t.integer "invoice_item_id"
t.string "footprint"
t.string "object_type"
t.bigint "object_id"
t.string "object_type", null: false
t.bigint "object_id", null: false
t.boolean "main"
t.index ["invoice_id"], name: "index_invoice_items_on_invoice_id"
t.index ["object_type", "object_id"], name: "index_invoice_items_on_object_type_and_object_id"
@ -415,7 +416,8 @@ ActiveRecord::Schema.define(version: 2022_12_27_141529) do
t.bigint "machine_id", null: false
end
create_table "notifications", id: :serial, force: :cascade do |t|
create_table "notifications", id: false, force: :cascade do |t|
t.serial "id", null: false
t.integer "receiver_id"
t.integer "attached_object_id"
t.string "attached_object_type"

View File

@ -0,0 +1,64 @@
# frozen_string_literal: true
require 'integrity/archive_helper'
# This take will ensure data integrity for invoices_items.
# Due a an unknown bug, some invoice items may not contains the reference to their object.
# This task will re-associate these items with their reservation/subscription/etc
namespace :fablab do
desc 'Associate the invoice_items w/o object'
task fix_invoice_items: :environment do |_task, _args|
next unless InvoiceItem.where(object_type: nil)
.or(InvoiceItem.where(object_id: nil))
.count
.positive?
include ActionView::Helpers::NumberHelper
# check the footprints and save the archives
Integrity::ArchiveHelper.check_footprints
ActiveRecord::Base.transaction do
periods = Integrity::ArchiveHelper.backup_and_remove_periods
# fix invoice items data
InvoiceItem.where(object_type: nil)
.or(InvoiceItem.where(object_id: nil))
.find_each do |ii|
invoice = ii.invoice
other_items = invoice.invoice_items.where.not(id: ii.id)
puts "\e[4;33mFound an invalid InvoiceItem\e[0m"
puts '=============================================='
puts "Invoice #{invoice.id} (# #{invoice.reference})"
puts "Total: #{number_to_currency(invoice.total / 100.0)}"
puts "Customer: #{invoice.invoicing_profile.full_name} (#{invoice.invoicing_profile.email})"
puts "Operator: #{invoice.operator_profile&.user&.profile&.full_name} (#{invoice.operator_profile&.user&.email})"
puts "Date: #{invoice.created_at}"
puts '=============================================='
puts "Concerned item: #{ii.id}"
puts "Item subject: #{ii.description}."
other_items.find_each do |oii|
puts '=============================================='
puts "Other item: #{oii.description} (#{oii.id})"
puts "Other item object: #{oii.object_type} #{oii.object_id}"
puts "Other item slots: #{oii.object.try(:slots)&.map { |s| "#{s.start_at} - #{s.end_at}" }}"
print "\e[1;34m[ ? ]\e[0m Associate the item with #{oii.object_type} #{oii.object_id} ? (y/N) > "
confirm = $stdin.gets.chomp
ii.update(object_id: oii.object_id, object_type: oii.object_type) if confirm == 'y'
end
ii.reload
if ii.object_id.nil? || ii.object_type.nil?
puts "\n\e[0;31mERROR\e[0m: InvoiceItem(#{ii.id}) was not associated with an object. Please open a rails console " \
"to manually fix the issue using `InvoiceItem.find(#{ii.id}.update(object_id: XXX, object_type: 'XXX')`.\n"
end
end
# chain records
puts 'Chaining all record. This may take a while...'
InvoiceItem.order(:id).all.each(&:chain_record)
Invoice.order(:id).all.each(&:chain_record)
# re-create all archives from the memory dump
Integrity::ArchiveHelper.restore_periods(periods)
end
end
end