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?
|
|
|
|
last_period = AccountingPeriod.order(start_at: 'DESC').first
|
|
|
|
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?
|
|
|
|
last_period = AccountingPeriod.order(start_at: 'DESC').first
|
|
|
|
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|
|
|
|
|
raise 'Missing argument. Usage exemple: rake fablab:setup:add_vat_rate[20,2014-01-01]. Use 0 to disable' unless args.rate && args.date
|
|
|
|
|
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-02-12 14:45:21 +01:00
|
|
|
end
|
2019-02-13 10:59:44 +01:00
|
|
|
end
|