2020-08-07 01:14:14 +02:00
|
|
|
class CreateFinancialTransactions < ActiveRecord::Migration[4.2]
|
2009-01-06 11:49:19 +01:00
|
|
|
def self.up
|
|
|
|
# Create Financial Transactions
|
|
|
|
create_table :financial_transactions do |t|
|
2023-05-12 13:01:12 +02:00
|
|
|
t.column :order_group_id, :integer, null: false
|
|
|
|
t.column :amount, :decimal, precision: 8, scale: 2, null: false
|
|
|
|
t.column :note, :text, null: false
|
|
|
|
t.column :user_id, :integer, null: false
|
|
|
|
t.column :created_on, :datetime, null: false
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# add column for the finance role
|
|
|
|
puts 'add column in "groups" for the finance role'
|
2023-05-12 13:01:12 +02:00
|
|
|
add_column :groups, :role_finance, :boolean, default: false, null: false
|
2009-01-06 11:49:19 +01:00
|
|
|
Group.reset_column_information
|
|
|
|
puts "Give #{CreateGroups::GROUP_ADMIN} the role finance .."
|
2023-05-12 13:01:12 +02:00
|
|
|
raise 'Failed' unless Group.find_by_name(CreateGroups::GROUP_ADMIN).update_attribute(:role_finance, true)
|
2009-01-06 11:49:19 +01:00
|
|
|
raise 'Cannot find admin user!' unless admin = User.find_by_nick(CreateUsers::USER_ADMIN)
|
|
|
|
raise 'Failed to enable role_finance with admin user!' unless admin.role_finance?
|
|
|
|
|
|
|
|
# Add transactions to the sample order group
|
|
|
|
puts "Add 30 transactions to the group '#{CreateGroups::GROUP_ORDER}'..."
|
|
|
|
raise "Group '#{CreateGroups::GROUP_ORDER}' not found!" unless ordergroup = Group.find_by_name(CreateGroups::GROUP_ORDER)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
balance = 0
|
|
|
|
for i in 1..30
|
|
|
|
ordergroup.addFinancialTransaction(i, "Sample Transaction Nr. #{i}", admin)
|
|
|
|
balance += i
|
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
raise 'Failed!' unless financial_transaction = FinancialTransaction.find_by_note('Sample Transaction Nr. 1')
|
|
|
|
raise 'Failed to update account_balance!' unless OrderGroup.find(ordergroup.id).account_balance == balance
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
def self.down
|
|
|
|
remove_column :groups, :role_finance
|
|
|
|
drop_table :financial_transactions
|
|
|
|
end
|
|
|
|
end
|