Skip to content

Commit

Permalink
Merge pull request #1265 from CDL-Dryad/tiered-base-rate
Browse files Browse the repository at this point in the history
Add a "base rate" calculation to tiered billing
  • Loading branch information
sfisher authored Jul 5, 2023
2 parents 6b1f433 + 02447c7 commit 1948684
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 28 deletions.
21 changes: 14 additions & 7 deletions documentation/reports.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ git push origin main
End-of-quarter reporting
------------------------

At the end of a quarter, combine the three monthly files into a single quarterly file so it can be used for the quarterly reports below.
At the end of a quarter, combine the three monthly files into a single quarterly
file so it can be used for the quarterly reports below. If there is more than
one quarterly file for the year, combine them into a yearly file, so it can be
used to calculate "base rates".

**Deferred payment reports**

Expand All @@ -64,20 +67,24 @@ year) generated by combining multiple shopping_cart_reports.
Run the deferred payment reports with a command like:
```
# This command must be run on the production server, to access the production database
RAILS_ENV=production bundle exec rails identifiers:deferred_journal_reports SC_REPORT=/tmp/shopping_cart_report_2020-01.csv
cp ~/journal-payments/shoppingcart/shopping_cart_report_2023* /tmp
RAILS_ENV=production bundle exec rails identifiers:deferred_journal_reports SC_REPORT=/tmp/shopping_cart_report_2020-Q1.csv
```

**Tiered payment reports**

For journals that have a tiered payment plan, a secondary task generates
PDF reports that can be be sent to the journal sponsors. The secondary task
takes as input a shopping_cart_report, as generated above, but it should only be
used with quarterly reports to follow the current billing model.
For journals that have a tiered payment plan, a secondary task generates PDF
reports that can be be sent to the journal sponsors. The secondary task takes as
input a shopping_cart_report, as generated above, but it also needs a cumulative
shopping_cart_report for prior quarters to calculate the "base rate". Both files
should be quarterly reports to follow the current billing model. For the first
quarter, the base report can be an empty file.

Run the tiered payment reports with a command like:
```
# This command must be run on the production server, to access the production database
RAILS_ENV=production bundle exec rails identifiers:tiered_journal_reports SC_REPORT=/tmp/shopping_cart_report_2023-Q1.csv
cp ~/journal-payments/shoppingcart/shopping_cart_report_2023* /tmp
RAILS_ENV=production bundle exec rails identifiers:tiered_journal_reports SC_REPORT=/tmp/shopping_cart_report_2023-Q1.csv BASE_REPORT=/tmp/shopping_cart_report_2023.csv
```


Expand Down
73 changes: 52 additions & 21 deletions lib/tasks/stash_engine_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,19 @@ namespace :identifiers do

desc 'Generate reports of items that should be billed for tiered journals'
task tiered_journal_reports: :environment do
# Get the input shopping cart report in SC_REPORT environment variable.
if ENV['SC_REPORT'].blank?
puts 'Usage: tiered_journal_reports SC_REPORT=<shopping_cart_report_filename>'
# Get the input shopping cart report in BASE_REPORT and SC_REPORT environment variables.
if ENV['SC_REPORT'].blank? || ENV['BASE_REPORT'].blank?
puts 'Usage: tiered_journal_reports BASE_REPORT=<shopping_cart_base_filename> SC_REPORT=<shopping_cart_report_filename>'
exit
else
sc_report_file = ENV['SC_REPORT']
puts "Producing tiered journal reports for #{sc_report_file}"
base_report_file = ENV.fetch('BASE_REPORT', nil)
sc_report_file = ENV.fetch('SC_REPORT', nil)
puts "Producing tiered journal reports for #{sc_report_file}, using base in #{base_report_file}"
end

base_values = tiered_base_values(base_report_file)
puts "Calculated base values #{base_values}"

sc_report = CSV.parse(File.read(sc_report_file), headers: true)

md = /(.*)shopping_cart_report_(.*).csv/.match(sc_report_file)
Expand Down Expand Up @@ -668,7 +672,7 @@ namespace :identifiers do
end
next if sponsor_summary.blank?

csv << [org.name, 'TOTAL', sponsor_total_count, tiered_price(sponsor_total_count)]
csv << [org.name, 'TOTAL', sponsor_total_count, tiered_price(sponsor_total_count, base_values[org.name])]
write_tiered_sponsor_summary(name: org.name, file_prefix: prefix, report_period: time_period,
table: sponsor_summary)
sponsor_summary = []
Expand All @@ -680,24 +684,51 @@ namespace :identifiers do
exit
end

def tiered_price(count)
return nil unless count.is_a?(Integer)
# Calculates each sponsor's "base" number of submissions, using data from prior quarters
def tiered_base_values(base_report_file)
base_values = {}
base_report = CSV.parse(File.read(base_report_file), headers: true)
sponsor_total_count = 0
StashEngine::JournalOrganization.all.each do |org|
journals = org.journals_sponsored_deep
journals.each do |j|
next unless j.payment_plan_type == 'TIERED' && j.top_level_org == org

free_datasets = 10
journal_item_count = 0
base_report.each do |item|
journal_item_count += 1 if item['JournalISSN'] == j.single_issn
end
sponsor_total_count += journal_item_count
end
next if sponsor_total_count == 0

price = if count <= free_datasets
0
elsif count <= 100
(count - free_datasets) * 135
elsif count <= 250
(count - free_datasets) * 100
elsif count <= 500
(count - free_datasets) * 85
else
(count - free_datasets) * 55
end
base_values[org.name] = sponsor_total_count
sponsor_total_count = 0
end
base_values
end

def tiered_price(current_count, base_count)
return nil unless current_count.is_a?(Integer) && base_count.is_a?(Integer)

free_datasets = 10

"$#{price}"
# the base_price is based on the total number of datasets, including the current quarter
total_datasets = current_count + base_count

base_price = if total_datasets <= free_datasets
0
elsif total_datasets <= 100
135
elsif total_datasets <= 250
100
elsif total_datasets <= 500
85
else
55
end

"$#{current_count * base_price}"
end

# Write a PDF that Dryad can send to the sponsor, summarizing the datasets published
Expand Down

0 comments on commit 1948684

Please sign in to comment.