lib/appydave/tools/bank_reconciliation/clean/clean_transactions.rb in appydave-tools-0.7.0 vs lib/appydave/tools/bank_reconciliation/clean/clean_transactions.rb in appydave-tools-0.8.0
- old
+ new
@@ -8,28 +8,35 @@
class CleanTransactions
include Appydave::Tools::Configuration::Configurable
include KLog::Logging
attr_reader :transaction_folder
+ attr_reader :output_folder
attr_reader :transactions
# (config_file)
- def initialize(transaction_folder: '/Volumes/Expansion/Sync/bank-reconciliation/original-transactions')
+ def initialize(transaction_folder: nil, output_folder: nil)
+ # needs to use config.bank_reconciliation.transaction_folder
+ transaction_folder ||= '/Volumes/Expansion/Sync/bank-reconciliation/original-transactions'
+ output_folder ||= File.join(transaction_folder, 'clean')
+
@transaction_folder = transaction_folder
+ @output_folder = output_folder
end
- def clean_transactions(input_globs, _output_file)
+ def clean_transactions(input_globs, output_file)
raw_transactions = grab_raw_transactions(input_globs)
transactions, duplicates_count = deduplicate(raw_transactions)
transactions = Mapper.new.map(transactions)
- # tp transactions
+ # tp transactions, Appydave::Tools::BankReconciliation::Models::Transaction.csv_headers
+
log.kv 'Deduped consolidated transactions', duplicates_count if duplicates_count.positive?
- # transactions = normalize(transactions)
- # save_to_csv(transactions, output_file)
+ save_to_csv(transactions, output_file)
+
@transactions = transactions
end
private
@@ -40,10 +47,11 @@
begin
Dir.chdir(transaction_folder)
input_globs.each do |glob|
Dir.glob(glob).each do |file|
+ log.kv 'Reading transactions from', file
raw_transactions = ReadTransactions.new(file).read
deduped_transactions, duplicates_count = deduplicate(raw_transactions)
if duplicates_count.positive?
log.kv 'Duplicates count', duplicates_count
@@ -79,11 +87,14 @@
[unique_transactions, duplicates]
end
def save_to_csv(transactions, output_file)
+ FileUtils.mkdir_p(output_folder)
+ output_file = File.join(output_folder, output_file)
+
CSV.open(output_file, 'w') do |csv|
- csv << ReconciledTransaction.csv_headers
+ csv << Appydave::Tools::BankReconciliation::Models::Transaction.csv_headers
transactions.each do |transaction|
csv << transaction.to_csv_row
end
end
end