From 705bedc25c4bcce78dfae87c7b191dfba1268bb3 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 1 Apr 2020 12:51:18 +0200 Subject: [PATCH] [bug] crediting a wallet w/ refund invoice prevent statistics generation (#196) --- CHANGELOG.md | 3 +++ app/services/statistic_service.rb | 6 +++++- app/services/wallet_service.rb | 13 +++++++++++-- lib/tasks/fablab/fix.rake | 14 ++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ba403a13..7470d394a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Updated compass-rails & compass-core - Renamed production documentation - Syntax improvements in scss files +- Fix a bug: crediting a wallet w/ refund invoice prevent statistics generation (#196) - Fix a bug: invalid translation keys in closing accounting period interface - Fix a bug: since PostgreSQL release 9.6.17, the new installations will fail to start complaining for missing password (#194) - Fix a bug: missing translations for some error messages @@ -19,6 +20,8 @@ - Fix a security issue: updated mkdirp to fix [CVE-2020-7598](https://nvd.nist.gov/vuln/detail/CVE-2020-7598) - Fix a security issue: updated acorn to fix [CVE-2020-7598](https://nvd.nist.gov/vuln/detail/CVE-2020-7598) - Fix a security issue: updated actionview to fix [CVE-2020-5267](https://nvd.nist.gov/vuln/detail/CVE-2020-5267) +- [TODO DEPLOY] `rails fablab:fix:avoirs_wallet_transaction` +- [TODO DEPLOY] `rails fablab:es:generate_stats[289]` only if you had missing statistics since some date ago. You can replace 289 by the difference of days between the day you run this task and the last day you had statistics ## v4.3.2 2020 March 11 diff --git a/app/services/statistic_service.rb b/app/services/statistic_service.rb index abb7f883d..5e4e36064 100644 --- a/app/services/statistic_service.rb +++ b/app/services/statistic_service.rb @@ -273,10 +273,12 @@ class StatisticService Avoir.where('invoices.created_at >= :start_date AND invoices.created_at <= :end_date', options) .eager_load(:invoice_items, statistic_profile: [:group]) .each do |i| + # the following line is a workaround for issue #196 + profile = i.statistic_profile || i.invoiced&.wallet&.user&.statistic_profile avoirs_ca_list.push OpenStruct.new({ date: options[:start_date].to_date, ca: calcul_avoir_ca(i) - }.merge(user_info(i.statistic_profile))) + }.merge(user_info(profile))) end reservations_ca_list.concat(subscriptions_ca_list).concat(avoirs_ca_list).each do |e| profile = StatisticProfile.find(e.statistic_profile_id) @@ -365,6 +367,8 @@ class StatisticService end def user_info(statistic_profile) + return {} unless statistic_profile + { statistic_profile_id: statistic_profile.id, user_id: statistic_profile.user_id, diff --git a/app/services/wallet_service.rb b/app/services/wallet_service.rb index 3dd88bc7c..ff3fd1e11 100644 --- a/app/services/wallet_service.rb +++ b/app/services/wallet_service.rb @@ -1,3 +1,6 @@ +# frozen_string_literal: true + +# Provides methods to manage wallets class WalletService def initialize(user: nil, wallet: nil) @user = user @@ -8,7 +11,12 @@ class WalletService def credit(amount) ActiveRecord::Base.transaction do if @wallet.credit(amount) - transaction = WalletTransaction.new(invoicing_profile: @user.invoicing_profile, wallet: @wallet, transaction_type: 'credit', amount: amount) + transaction = WalletTransaction.new( + invoicing_profile: @user.invoicing_profile, + wallet: @wallet, + transaction_type: 'credit', + amount: amount + ) if transaction.save NotificationCenter.call type: 'notify_user_wallet_is_credited', receiver: @wallet.user, @@ -53,7 +61,8 @@ class WalletService avoir.description = description avoir.payment_method = 'wallet' avoir.subscription_to_expire = false - avoir.invoicing_profile_id = wallet_transaction.wallet.user.invoicing_profile.id + avoir.invoicing_profile_id = wallet_transaction.invoicing_profile_id + avoir.statistic_profile_id = wallet_transaction.wallet.user.statistic_profile.id avoir.total = wallet_transaction.amount * 100.0 avoir.save! diff --git a/lib/tasks/fablab/fix.rake b/lib/tasks/fablab/fix.rake index e8c06892c..af7836254 100644 --- a/lib/tasks/fablab/fix.rake +++ b/lib/tasks/fablab/fix.rake @@ -150,5 +150,19 @@ namespace :fablab do name: 'theme' ) end + + desc '[release 4.3.3] add statistic_profile_id to refund invoices for WalletTransactions' + task avoirs_wallet_transaction: :environment do + Avoir.where(invoiced_type: WalletTransaction.name).each do |a| + next unless a.statistic_profile_id.nil? + + begin + a.statistic_profile_id = a.invoiced.wallet.user&.statistic_profile&.id + a.save! + rescue ActiveRecord::RecordInvalid => e + printf "Unable to modify the refund invoice (id %s): %s\nIgnoring that record...\n", id: a.id, error: e + end + end + end end end