Skip to content

Commit

Permalink
Allow exporting inventory adjustments as CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
smellsblue committed Oct 8, 2024
1 parent 7a54dbc commit 1b2f21f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def inventory_adjustments
params[:start_date] ||= 1.month.ago.strftime("%m/%d/%Y")
params[:end_date] ||= Time.zone.now.strftime("%m/%d/%Y")
@report = Reports::InventoryAdjustments.new(params, session)

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

def net_suite_export
Expand Down
64 changes: 64 additions & 0 deletions app/models/reports/inventory_adjustments.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module Reports
class InventoryAdjustments
include CsvExport
FILTERABLE_REASONS = %w[reconciliation spoilage transfer transfer_internal transfer_external purchase
donation donation_adjustment
adjustment order_adjustment
purchase_shipment_received purchase_shipment_deleted].freeze
FIELDS = %w[Item Reason Description Amount Value TotalValue Date]

def self.reason_label(reason)
case reason
Expand All @@ -30,10 +32,65 @@ def self.reason_label(reason)
end
end

def self.short_reason_label(reason)
case reason
when "reconciliation"
"Reconcile"
when "spoilage"
"Spoil"
when "transfer"
"LgcyXfer"
when "transfer_internal"
"IntXfer"
when "transfer_external"
"ExtXfer"
when "purchase"
"LgcyPurchase"
when "donation"
"Donation"
when "donation_adjustment"
"DeletedDonation"
when "adjustment"
"Other"
when "order_adjustment"
"Order"
when "purchase_shipment_received"
"Purchase"
when "purchase_shipment_deleted"
"DeletedPurchase"
else
reason.humanize.capitalize
end
end

def initialize(params, _session)
@params = params
end

def csv_filename
reasons =
if all_reasons?
"AllReasons"
else
filtered_reasons.map { |x| self.class.short_reason_label(x) }.join("_")
end

date_part = "#{start_date.strftime('%m-%d-%Y')}_to_#{end_date.strftime('%m-%d-%Y')}"
"inventory-adjustments_#{reasons}_#{date_part}_#{Time.zone.now.strftime('%Y%m%d%H%M%S')}.csv"
end

def csv_export_row(row)
[
row.item_description,
row.reason,
row.edit_description,
row.amount,
row.value,
row.total_value,
row.date.strftime("%m/%d/%Y")
]
end

def start_date
@start_date ||= Time.strptime(@params[:start_date], "%m/%d/%Y").beginning_of_day
end
Expand All @@ -50,6 +107,13 @@ def each
end
end

def all_reasons?
return true if @params[:reasons].blank?

chosen_reasons = Set.new(@params[:reasons])
Reports::InventoryAdjustments::FILTERABLE_REASONS.all? { |x| chosen_reasons.include?(x) }
end

def filtered_reasons
(@params[:reasons].presence || Reports::InventoryAdjustments::FILTERABLE_REASONS).dup
end
Expand Down
1 change: 1 addition & 0 deletions app/views/reports/inventory_adjustments.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<%= text_field_tag :end_date, params[:end_date], data: { provide: "datepicker" } %>
<%= 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 1b2f21f

Please sign in to comment.