Skip to content

Commit

Permalink
Allow exporting the total inventory value report, and re-enable the f…
Browse files Browse the repository at this point in the history
…ilter button when exporting
  • Loading branch information
smellsblue committed Oct 8, 2024
1 parent d60e590 commit 50c233c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def net_suite_export
def total_inventory_value
params[:date] ||= Time.zone.now.strftime("%m/%d/%Y")
@report = Reports::TotalInventoryValue.new(params, session)

if params[:csv].present?
send_csv @report, filename: @report.csv_filename
end
end

def value_by_donor
Expand Down
1 change: 1 addition & 0 deletions app/javascript/stockaid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import "./guards"
import "./item_program_ratios"
import "./items"
import "./migrate_donations"
import "./optional_disable_with"
import "./orders"
import "./organization"
import "./programs"
Expand Down
8 changes: 8 additions & 0 deletions app/javascript/stockaid/optional_disable_with.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$(document).on("click", "form :submit:not([data-disable-with])", function() {
let $this = $(this);
let buttonsToReEnable = $this.parents("form:first").find(":submit[data-disable-with]").not($this);

setTimeout(function() {
buttonsToReEnable.each(function() { $(this).prop("disabled", false) });
}, 1000);
});
7 changes: 6 additions & 1 deletion app/models/concerns/csv_export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ def csv_export_row(row)
self.class::FIELDS.map { |field| row.send(self.class.fields_to_method_names[field]) }
end

# This method can be overridden, such as in Reports::TotalInventoryValue
def each_csv_row(&)
each(&)
end

def to_csv(output = "")
output << CSV.generate_line(csv_export_header)

each do |row|
each_csv_row do |row|
output << CSV.generate_line(csv_export_row(row))
end

Expand Down
28 changes: 27 additions & 1 deletion app/models/reports/total_inventory_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,44 @@ def self.new(params, _session)
end

module Common
include CsvExport
attr_reader :total_value

def date
return if @params[:date].blank?
Time.strptime(@params[:date], "%m/%d/%Y").end_of_day
@date ||= Time.strptime(@params[:date], "%m/%d/%Y").end_of_day
end

def csv_filename
date_part = "_#{date.strftime('%m-%d-%Y')}" if date
"total-inventory-value_#{csv_filename_scope}#{date_part}_#{Time.zone.now.strftime('%Y%m%d%H%M%S')}.csv"
end

def csv_export_row(row)
row
end

def each_csv_row
each do |description, value|
yield([description, value])
end
end
end

class SingleCategory
include Common
attr_reader :category
FIELDS = %w[Item Value]

def initialize(params)
@category = Category.find(params[:category_id])
@params = params
end

def csv_filename_scope
category.description.gsub(/[^a-zA-Z0-9_]/, "_").gsub(/_+/, "_")
end

def each
@total_value = 0.0

Expand All @@ -40,12 +61,17 @@ def each
class AllCategories
include Common
attr_reader :categories
FIELDS = %w[Category Value]

def initialize(params)
@categories = Category.all
@params = params
end

def csv_filename_scope
"All_Categories"
end

def each
@total_value = 0.0

Expand Down
1 change: 1 addition & 0 deletions app/views/reports/_total_inventory_value_filters.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<%= text_field_tag :date, params[:date], data: { provide: "datepicker" } %>
<%= hidden_field_tag :category_id, request.params[:category_id] %>
<%= submit_tag "Filter", class: "btn btn-default" %>
<%= submit_tag "Export", name: "csv", class: "btn btn-primary", data: { disable_with: false } %>
<% end %>
</form>
</div>
Expand Down

0 comments on commit 50c233c

Please sign in to comment.