2019-02-12 14:45:21 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
namespace :fablab do
|
|
|
|
namespace :setup do
|
|
|
|
desc 'assign all footprints to existing Invoice records'
|
|
|
|
task chain_invoices_records: :environment do
|
2019-05-27 11:11:21 +02:00
|
|
|
if Invoice.where.not(footprint: nil).count.positive?
|
|
|
|
print 'WARNING: Footprints were already generated. Regenerate? (y/n) '
|
|
|
|
confirm = STDIN.gets.chomp
|
|
|
|
next unless confirm == 'y'
|
|
|
|
end
|
2019-02-13 10:59:44 +01:00
|
|
|
|
2019-05-27 11:11:21 +02:00
|
|
|
if AccountingPeriod.count.positive?
|
2020-03-25 12:34:33 +01:00
|
|
|
last_period = AccountingPeriod.order(start_at: :desc).first
|
2019-05-27 11:11:21 +02:00
|
|
|
puts "Regenerating from #{last_period.end_at}..."
|
|
|
|
Invoice.where('created_at > ?', last_period.end_at).order(:id).each(&:chain_record)
|
|
|
|
else
|
|
|
|
puts '(Re)generating all footprint...'
|
|
|
|
Invoice.order(:id).all.each(&:chain_record)
|
|
|
|
end
|
2019-02-12 14:45:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'assign all footprints to existing InvoiceItem records'
|
|
|
|
task chain_invoices_items_records: :environment do
|
2019-05-27 11:11:21 +02:00
|
|
|
if InvoiceItem.where.not(footprint: nil).count.positive?
|
|
|
|
print 'WARNING: Footprints were already generated. Regenerate? (y/n) '
|
|
|
|
confirm = STDIN.gets.chomp
|
|
|
|
next unless confirm == 'y'
|
|
|
|
end
|
2019-02-13 10:59:44 +01:00
|
|
|
|
2019-05-27 11:11:21 +02:00
|
|
|
if AccountingPeriod.count.positive?
|
2020-03-25 12:34:33 +01:00
|
|
|
last_period = AccountingPeriod.order(start_at: :desc).first
|
2019-05-27 11:11:21 +02:00
|
|
|
puts "Regenerating from #{last_period.end_at}..."
|
2019-05-27 16:09:27 +02:00
|
|
|
InvoiceItem.where('created_at > ?', last_period.end_at).order(:id).each(&:chain_record)
|
2019-05-27 11:11:21 +02:00
|
|
|
else
|
|
|
|
puts '(Re)generating all footprint...'
|
|
|
|
InvoiceItem.order(:id).all.each(&:chain_record)
|
|
|
|
end
|
2019-02-12 14:45:21 +01:00
|
|
|
end
|
2019-02-27 17:44:52 +01:00
|
|
|
|
2019-03-20 11:01:53 +01:00
|
|
|
desc 'assign all footprints to existing HistoryValue records'
|
|
|
|
task chain_history_values_records: :environment do
|
2019-05-27 11:11:21 +02:00
|
|
|
if HistoryValue.where.not(footprint: nil).count.positive?
|
|
|
|
print 'WARNING: Footprints were already generated. Regenerate? (y/n) '
|
|
|
|
confirm = STDIN.gets.chomp
|
|
|
|
next unless confirm == 'y'
|
|
|
|
end
|
2019-03-20 11:01:53 +01:00
|
|
|
|
2019-05-27 11:11:21 +02:00
|
|
|
HistoryValue.order(:id).all.each(&:chain_record)
|
2019-03-20 11:01:53 +01:00
|
|
|
end
|
|
|
|
|
2019-02-27 17:44:52 +01:00
|
|
|
desc 'assign environment value to all invoices'
|
|
|
|
task set_environment_to_invoices: :environment do
|
|
|
|
Invoice.all.each do |i|
|
|
|
|
i.environment = Rails.env
|
|
|
|
i.save!
|
|
|
|
end
|
|
|
|
end
|
2019-03-13 12:03:14 +01:00
|
|
|
|
|
|
|
desc 'add missing VAT rate to history'
|
|
|
|
task :add_vat_rate, %i[rate date] => :environment do |_task, args|
|
2020-03-25 17:58:53 +01:00
|
|
|
raise 'Missing argument. Usage exemple: rails fablab:setup:add_vat_rate[20,2014-01-01]. Use 0 to disable' unless args.rate && args.date
|
2019-03-13 12:03:14 +01:00
|
|
|
|
2019-03-27 12:58:08 +01:00
|
|
|
if args.rate == '0'
|
2019-03-13 12:03:14 +01:00
|
|
|
setting = Setting.find_by(name: 'invoice_VAT-active')
|
|
|
|
HistoryValue.create!(
|
|
|
|
setting_id: setting.id,
|
|
|
|
user_id: User.admins.first.id,
|
|
|
|
value: 'false',
|
|
|
|
created_at: DateTime.parse(args.date)
|
|
|
|
)
|
|
|
|
else
|
|
|
|
setting = Setting.find_by(name: 'invoice_VAT-rate')
|
|
|
|
HistoryValue.create!(
|
|
|
|
setting_id: setting.id,
|
|
|
|
user_id: User.admins.first.id,
|
|
|
|
value: args.rate,
|
|
|
|
created_at: DateTime.parse(args.date)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2019-06-11 16:56:11 +02:00
|
|
|
|
2019-06-12 12:22:38 +02:00
|
|
|
desc 'migrate PDF invoices to folders numbered by invoicing_profile'
|
|
|
|
task migrate_pdf_invoices_folders: :environment do
|
2019-06-17 15:01:23 +02:00
|
|
|
puts 'No invoices, exiting...' and return if Invoice.count.zero?
|
|
|
|
|
2019-06-11 16:56:11 +02:00
|
|
|
require 'fileutils'
|
|
|
|
Invoice.all.each do |i|
|
|
|
|
invoicing_profile = i.invoicing_profile
|
|
|
|
user_id = invoicing_profile.user_id
|
|
|
|
|
|
|
|
src = "invoices/#{user_id}/#{i.filename}"
|
|
|
|
dest = "tmp/invoices/#{invoicing_profile.id}"
|
|
|
|
|
2019-06-12 12:22:38 +02:00
|
|
|
if FileTest.exist?(src)
|
|
|
|
FileUtils.mkdir_p dest
|
|
|
|
FileUtils.mv src, "#{dest}/#{i.filename}", force: true
|
|
|
|
end
|
2019-06-11 16:56:11 +02:00
|
|
|
end
|
|
|
|
FileUtils.rm_rf 'invoices'
|
|
|
|
FileUtils.mv 'tmp/invoices', 'invoices'
|
|
|
|
end
|
2020-05-19 16:20:59 +02:00
|
|
|
|
|
|
|
desc 'migrate environment variables to the database (settings)'
|
|
|
|
task env_to_db: :environment do
|
2020-05-26 10:53:49 +02:00
|
|
|
include ApplicationHelper
|
|
|
|
|
2020-05-19 16:20:59 +02:00
|
|
|
mapping = [
|
2020-05-26 10:53:49 +02:00
|
|
|
%w[_ PHONE_REQUIRED phone_required true],
|
|
|
|
%w[_ GA_ID tracking_id],
|
|
|
|
%w[_ BOOK_SLOT_AT_SAME_TIME book_overlapping_slots true],
|
|
|
|
%w[_ SLOT_DURATION slot_duration 60],
|
|
|
|
%w[_ EVENTS_IN_CALENDAR events_in_calendar false],
|
2020-05-26 15:39:28 +02:00
|
|
|
%w[! FABLAB_WITHOUT_SPACES spaces_module true],
|
2020-05-26 18:07:07 +02:00
|
|
|
%w[! FABLAB_WITHOUT_PLANS plans_module false],
|
2020-05-27 12:29:45 +02:00
|
|
|
%w[! FABLAB_WITHOUT_INVOICES invoicing_module false],
|
2020-05-27 15:39:39 +02:00
|
|
|
%w[_ FACEBOOK_APP_ID facebook_app_id],
|
2020-05-27 16:35:30 +02:00
|
|
|
%w[_ TWITTER_NAME twitter_analytics],
|
|
|
|
%w[_ RECAPTCHA_SITE_KEY recaptcha_site_key],
|
2020-05-27 18:49:53 +02:00
|
|
|
%w[_ RECAPTCHA_SECRET_KEY recaptcha_secret_key],
|
2020-06-01 12:15:16 +02:00
|
|
|
%w[_ FEATURE_TOUR_DISPLAY feature_tour_display once],
|
2020-06-01 17:32:32 +02:00
|
|
|
%w[_ DEFAULT_MAIL_FROM email_from],
|
2020-06-03 16:25:13 +02:00
|
|
|
%w[_ DISQUS_SHORTNAME disqus_shortname],
|
|
|
|
%w[_ ALLOWED_EXTENSIONS allowed_cad_extensions],
|
2020-06-08 11:38:49 +02:00
|
|
|
%w[_ ALLOWED_MIME_TYPES allowed_cad_mime_types],
|
|
|
|
%w[_ OPENLAB_APP_ID openlab_app_id],
|
2020-06-08 16:15:44 +02:00
|
|
|
%w[_ OPENLAB_APP_SECRET openlab_app_secret],
|
2020-06-08 17:42:59 +02:00
|
|
|
%w[_ OPENLAB_DEFAULT openlab_default],
|
2020-06-09 13:09:31 +02:00
|
|
|
%w[! FABLAB_WITHOUT_ONLINE_PAYMENT online_payment_module false],
|
|
|
|
%w[_ STRIPE_PUBLISHABLE_KEY stripe_public_key],
|
2020-06-10 16:37:11 +02:00
|
|
|
%w[_ STRIPE_API_KEY stripe_secret_key],
|
2020-06-15 10:58:15 +02:00
|
|
|
%w[_ STRIPE_CURRENCY stripe_currency],
|
2020-06-15 11:57:13 +02:00
|
|
|
%w[_ INVOICE_PREFIX invoice_prefix FabManager_invoice],
|
2020-06-15 12:44:43 +02:00
|
|
|
%w[_ USER_CONFIRMATION_NEEDED_TO_SIGN_IN confirmation_required false],
|
2020-06-17 12:20:51 +02:00
|
|
|
%w[! FABLAB_WITHOUT_WALLET wallet_module false],
|
|
|
|
%w[! FABLAB_WITHOUT_STATISTICS statistics_module false]
|
2020-05-19 16:20:59 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
mapping.each do |m|
|
2020-05-26 10:53:49 +02:00
|
|
|
setting = Setting.find_or_initialize_by(name: m[2])
|
|
|
|
value = ENV.fetch(m[1], m[3])
|
|
|
|
next unless value
|
|
|
|
|
2020-05-27 15:39:39 +02:00
|
|
|
# if the array starts with a "!", invert the boolean value
|
2020-05-26 10:53:49 +02:00
|
|
|
value = (!str_to_bool(value)).to_s if m[0] == '!'
|
|
|
|
setting.value = value
|
|
|
|
setting.save
|
2020-05-19 16:20:59 +02:00
|
|
|
end
|
|
|
|
end
|
2019-02-12 14:45:21 +01:00
|
|
|
end
|
2019-02-13 10:59:44 +01:00
|
|
|
end
|