Skip to content

Commit

Permalink
Allow condensed inventory adjustments report
Browse files Browse the repository at this point in the history
  • Loading branch information
smellsblue committed Oct 8, 2024
1 parent 1b2f21f commit a5c82be
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 23 deletions.
87 changes: 80 additions & 7 deletions app/models/reports/inventory_adjustments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def initialize(params, _session)
end

def csv_filename
style_part = selected_style.capitalize

reasons =
if all_reasons?
"AllReasons"
Expand All @@ -76,7 +78,7 @@ def csv_filename
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"
"inventory-adjustments_#{style_part}_#{reasons}_#{date_part}_#{Time.zone.now.strftime('%Y%m%d%H%M%S')}.csv"
end

def csv_export_row(row)
Expand All @@ -87,10 +89,14 @@ def csv_export_row(row)
row.amount,
row.value,
row.total_value,
row.date.strftime("%m/%d/%Y")
row.date&.strftime("%m/%d/%Y")
]
end

def selected_style
@params[:style].presence || "full"
end

def start_date
@start_date ||= Time.strptime(@params[:start_date], "%m/%d/%Y").beginning_of_day
end
Expand All @@ -99,11 +105,15 @@ def end_date
@end_date ||= Time.strptime(@params[:end_date], "%m/%d/%Y").end_of_day
end

def each
Item.unscoped do
filtered_scope.each do |version|
yield Reports::InventoryAdjustments::Row.new(version)
end
def condensed?
selected_style == "condensed"
end

def each(&)
if condensed?
each_condensed_row(&)
else
each_row(&)
end
end

Expand All @@ -125,6 +135,65 @@ def filtered_scope
.where(created_at: (start_date..end_date))
end

def each_condensed_row
condensed_data = {}

each_row do |row|
condensed_data[row.condensed_key] ||= Reports::InventoryAdjustments::CondensedRow.new
condensed_data[row.condensed_key] << row
end

condensed_data.values.each do |condensed_row|
yield condensed_row
end
end

def each_row
Item.unscoped do
filtered_scope.each do |version|
yield Reports::InventoryAdjustments::Row.new(version)
end
end
end

class CondensedRow
def initialize
@rows = []
end

def <<(row)
@rows << row
end

def edit_description
nil
end

def item_description
@rows.first.item_description
end

def reason
@rows.first.reason
end

def amount
@rows.sum(&:amount)
end

def value
@rows.sum(&:value)
end

def total_value
@rows.sum(&:total_value)
end

def date
nil
end
end

class Row
attr_reader :version

Expand All @@ -135,6 +204,10 @@ def initialize(version)
@version = version
end

def condensed_key
"#{item.id}:#{reason}"
end

def date
version.created_at
end
Expand Down
51 changes: 35 additions & 16 deletions app/views/reports/inventory_adjustments.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,34 @@
<% content_for :content do %>
<div class="row">
<div class="col-xs-12">
<form class="form-inline pull-right">
<form class="form-inline">
<%= form_tag inventory_adjustments_reports_path, method: "get" do %>
<% Reports::InventoryAdjustments::FILTERABLE_REASONS.each do |reason| %>
<div class="checkbox">
<label><%= check_box_tag "reasons[]", reason, params.fetch(:reasons, []).include?(reason) %> <%= Reports::InventoryAdjustments.reason_label(reason).pluralize %></label>
</div>
<% end %>
<div class="row">
<% Reports::InventoryAdjustments::FILTERABLE_REASONS.each do |reason| %>
<div class="col-xs-12 col-sm-3 col-lg-2">
<div class="checkbox">
<label><%= check_box_tag "reasons[]", reason, params.fetch(:reasons, []).include?(reason) %> <%= Reports::InventoryAdjustments.reason_label(reason).pluralize %></label>
</div>
</div>
<% end %>
</div>

<br />
<div class="row">
<div class="col-xs-12">
<div class="pull-right">
<%= select_tag :style, options_for_select([["Condensed", "condensed"], ["Full", "full"]], selected: @report.selected_style), class: "form-control" %>
<%= label_tag :date, "Start Date", class: "control-label" %>
<%= text_field_tag :start_date, params[:start_date], data: { provide: "datepicker" } %>
<%= label_tag :date, "Start Date", class: "control-label" %>
<%= text_field_tag :start_date, params[:start_date], data: { provide: "datepicker" } %>
<%= label_tag :date, "End Date", class: "control-label" %>
<%= text_field_tag :end_date, params[:end_date], data: { provide: "datepicker" } %>
<%= label_tag :date, "End Date", class: "control-label" %>
<%= 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 } %>
<%= submit_tag "Filter", class: "btn btn-default" %>
<%= submit_tag "Export", name: "csv", class: "btn btn-primary", data: { disable_with: false } %>
</div>
</div>
</div>
<% end %>
</form>
</div>
Expand All @@ -36,7 +46,10 @@
<th class="text-center num-value">Amount</th>
<th class="text-center monetary-value no-total">Value</th>
<th class="text-center monetary-value">Total Value</th>
<th>Date</th>

<% unless @report.condensed? %>
<th>Date</th>
<% end %>
</tr>
</thead>

Expand All @@ -48,7 +61,10 @@
<td class="text-center"><%= row.amount %></td>
<td class="text-center"><%= number_to_currency row.value, precision: 2 %></td>
<td class="text-center"><%= number_to_currency row.total_value, precision: 2 %></td>
<td><%= local_time row.date, "%m/%d/%Y" %></td>

<% unless @report.condensed? %>
<td><%= local_time row.date, "%m/%d/%Y" %></td>
<% end %>
</tr>
<% end %>
</tbody>
Expand All @@ -60,7 +76,10 @@
<th class="text-center"><%# This is summarized dynamically %></th>
<th></th>
<th class="text-center"><%# This is summarized dynamically %></th>
<th></th>

<% unless @report.condensed? %>
<th></th>
<% end %>
</tr>
</tfoot>
</table>
Expand Down

0 comments on commit a5c82be

Please sign in to comment.