diff --git a/README.md b/README.md index d4d82e8..1bc0860 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Once Ruby is installed, you will need to install the Ruby Gem called [Highline] To install this Gem, open the command line shell and type the following commands: - gem install highline -# How to use +# Set up required before first use (for both .exe and .rb versions) ## Prepare your directory structure Choose or create a directory/folder in which to place the script (.rb or .exe). This directory can be called whatever you want, but here I'll call it the "ruby_scripts" directory. @@ -39,6 +39,23 @@ The structure should look like this: Put the payment_info_processor .rb or .exe file(s) in the ruby_scripts directory. +''Note for more advanced users: You can run the script from anywhere; there doesn't have to be a ruby_scripts directory. BUT, there must be a data directory and an output directory in whatever directory you are running the script from. It's clunky and inflexible, but it is the only way I know to make the .exe version work for my colleagues who are terrified of the command line.'' + +## Prepare your configuration file (First use only) +The configuration file will tell the script when your new fiscal year begins. I will use my institution as an example. Our fiscal year runs from July 1 to June 30. + +In the ruby_scripts/data directory, create a new text file named payment_processor_config.txt + +payment_processor_config.txt will consist of two lines. Except for the number at the end of the line, your text should match what is below exactly. Copy/paste it in to be sure. + +``` +fy_begin_month = 7 +fy_begin_day = 1 +``` + +Change the number at the end of each line to reflect when your new fiscal year begins. Save and close payment_processor_config.txt. + +# Regular use ## Prepare your input file Export from a Review File of order records in Millennium. diff --git a/payment_info_processor.rb b/payment_info_processor.rb index b90ee96..2d3f5c0 100644 --- a/payment_info_processor.rb +++ b/payment_info_processor.rb @@ -1,19 +1,84 @@ require 'rubygems' require 'highline/import' require 'date' - -# Script history -# 20101027 - Original script produced -# One output option: summary by fiscal year -# Output: .csv -# 20120531 - Added individual payment output option. -# 20130416 - Changed output to .txt due to Excel's poor recognition -# of character encoding when opening .csv files +require 'pathname' exit if Object.const_defined?(:Ocra) + +#test that config file exists +config_path = Pathname.new("data/payment_processor_config.txt") +unless config_path.exist? + puts "\n\nCannot find config file: data/payment_processor_config.txt" + puts "Please see documentation at:" + puts " https://github.com/UNC-Libraries/Millennium-Helpers\n\n" + exit +end + +#load config settings into array +config_lines = [] +config_path.each_line do |ln| + ln.chomp! + config_lines << ln +end + +#make sure the month is set properly +unless config_lines[0].match(/^fy_begin_month = \d\d?\s*$/) + puts "\n\nFY begin month not properly set in config file." + puts "Please see documentation at:" + puts " https://github.com/UNC-Libraries/Millennium-Helpers\n\n" + exit +end + +#make sure the day is set properly +unless config_lines[1].match(/^fy_begin_day = \d\d?\s*$/) + puts "\n\nFY begin day not properly set in config file." + puts "Please see documentation at:" + puts " https://github.com/UNC-Libraries/Millennium-Helpers\n\n" + exit +end + +#set the fy start variables +$fystartmonth = config_lines[0].gsub /^.* = /, '' +$fystartday = config_lines[1].gsub /^.* = /, '' + +#make sure date created from config month and year is valid +unless Date.valid_date?(2012, $fystartmonth.to_i, $fystartday.to_i) + puts "\n\nThe month and day in your config file do not combine to create a valid date." + puts "Please see documentation at:" + puts " https://github.com/UNC-Libraries/Millennium-Helpers\n\n" + exit +end + +def find_fy(adate) + theyear = adate.year + fystartnum = Date.new(theyear, $fystartmonth.to_i, $fystartday.to_i).yday + paydatenum = adate.yday + if paydatenum >= fystartnum + fy = theyear.to_i + else + fy = theyear.to_i - 1 + end +end + +def set_full_year(yr) + if yr.to_i > 50 + fullyr = '19' + yr + else + fullyr = '20' + yr + end + return fullyr +end + +def get_fy_label(yr) + thisyear = yr.to_i + nextyear = thisyear + 1 + label = "FY#{thisyear}-#{nextyear}" + return label +end + puts "\n\n\n\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" puts "Welcome to the Millennium Payment Data Processor".upcase -puts "version 1.2.0, 2013-04-16" +puts "version 1.3.0, 2014-01-23" puts "written by Kristina Spurgin, ESM, kspurgin@email.unc.edu" puts "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" puts "\n\nINPUT:" @@ -129,22 +194,15 @@ paid_date = payment[0] pd = paid_date.split "-" - if pd[2].to_i > 80 - paidyear = "19" + pd[2] - else - paidyear = "20" + pd[2] - end + paidyear = set_full_year(pd[2]) + paid_date_f = Date.new paidyear.to_i, pd[0].to_i, pd[1].to_i - yr = paid_date_f.year - mo = paid_date_f.month - if mo > 6 - fiscal_yr = yr - else - fiscal_yr = yr-1 - end - @output_lines << [order_num, fiscal_yr, other_data, payment].flatten.join("\t") + fiscalyr = find_fy(paid_date_f) + fylabel = get_fy_label(fiscalyr) + + @output_lines << [order_num, fylabel, other_data, payment].flatten.join("\t") end end @@ -205,26 +263,22 @@ def initialize paid_date, amount @pds = paid_date pd = paid_date.split "-" - if pd[2].to_i > 80 - pdyr = "19" + pd[2] - else - pdyr = "20" + pd[2] - end + pdyr = set_full_year(pd[2]) @paid_date = Date.new pdyr.to_i, pd[0].to_i, pd[1].to_i @amount = amount - @fy = find_fy @paid_date + @fy = find_fy(@paid_date) end - def find_fy date - yr = date.year - mo = date.month - if mo > 6 - return yr - else - return yr-1 - end - end + # def find_fy date + # yr = date.year + # mo = date.month + # if mo > 6 + # return yr + # else + # return yr-1 + # end + # end end lines.each do |l| @@ -274,10 +328,7 @@ def find_fy date output = [] yrlabels = [] @@rawyears.each do |yr| - yrn = yr.to_i - nextyr = yrn + 1 - label = "FY#{yrn}-#{nextyr}" - yrlabels << label + yrlabels << get_fy_label(yr) end output << [hdr[:onum], hdr[:other_headers], yrlabels].flatten.join("\t") @orders.each do |ord|