Skip to content

Commit bb71e47

Browse files
authored
Merge pull request #5408 from brodyf42/4678_add_package_count_option_to_distribution_export
[4678] Add option to include package count in distributions export CSV
2 parents c0b4211 + f6302cf commit bb71e47

File tree

12 files changed

+84
-38
lines changed

12 files changed

+84
-38
lines changed

app/controllers/organizations_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def organization_params
104104
:signature_for_distribution_pdf, :receive_email_on_requests,
105105
:bank_is_set_up,
106106
:include_in_kind_values_in_exported_files,
107+
:include_packages_in_distribution_export,
107108
partner_form_fields: [],
108109
request_unit_names: []
109110
)

app/models/organization.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# hide_package_column_on_receipt :boolean default(FALSE)
1616
# hide_value_columns_on_receipt :boolean default(FALSE)
1717
# include_in_kind_values_in_exported_files :boolean default(FALSE), not null
18+
# include_packages_in_distribution_export :boolean default(FALSE), not null
1819
# intake_location :integer
1920
# invitation_text :text
2021
# latitude :float

app/services/exports/export_distributions_csv_service.rb

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def generate_csv
2727
def generate_csv_data
2828
csv_data = []
2929

30-
csv_data << headers
30+
csv_data << base_headers + item_headers
3131
distributions.each do |distribution|
3232
csv_data << build_row_data(distribution)
3333
end
@@ -39,17 +39,6 @@ def generate_csv_data
3939

4040
attr_reader :distributions
4141

42-
def headers
43-
# Build the headers in the correct order
44-
base_headers + item_headers
45-
end
46-
47-
# Returns a Hash of keys to indexes so that obtaining the index
48-
# doesn't require a linear scan.
49-
def headers_with_indexes
50-
@headers_with_indexes ||= headers.each_with_index.to_h
51-
end
52-
5342
# This method keeps the base headers associated with the lambdas
5443
# for extracting the values for the base columns from the given
5544
# distribution.
@@ -132,35 +121,28 @@ def base_headers
132121
base_table.keys
133122
end
134123

135-
def item_headers
136-
return @item_headers if @item_headers
137-
138-
@item_headers = @organization.items.select("DISTINCT ON (LOWER(name)) items.name").order("LOWER(name) ASC").map(&:name)
139-
@item_headers = @item_headers.flat_map { |header| [header, "#{header} In-Kind Value"] } if @organization.include_in_kind_values_in_exported_files
124+
def item_names
125+
@item_names ||= @organization.items.pluck(:name).sort_by(&:downcase)
126+
end
140127

141-
@item_headers
128+
def item_headers
129+
in_kind_value_headers = @organization.include_in_kind_values_in_exported_files ? item_names.map { |item| "#{item} In-Kind Value" } : []
130+
package_headers = @organization.include_packages_in_distribution_export ? item_names.map { |item| "#{item} Packages" } : []
131+
item_names.zip(in_kind_value_headers, package_headers).flatten.compact
142132
end
143133

144134
def build_row_data(distribution)
145135
row = base_table.values.map { |closure| closure.call(distribution) }
146-
row += make_item_quantity_and_value_slots
147136

148-
distribution.line_items.each do |line_item|
149-
item_name = line_item.item.name
150-
item_column_idx = headers_with_indexes[item_name]
151-
next unless item_column_idx
137+
item_names.each do |item_name|
138+
line_items = distribution.line_items.where(item: @organization.items.find_by(name: item_name))
152139

153-
row[item_column_idx] += line_item.quantity
154-
row[item_column_idx + 1] += Money.new(line_item.value_per_line_item) if @organization.include_in_kind_values_in_exported_files
140+
row << line_items.sum(&:quantity)
141+
row << Money.new(line_items.sum(&:value_per_line_item)) if @organization.include_in_kind_values_in_exported_files
142+
row << line_items.map(&:has_packages).compact.sum.round(2) if @organization.include_packages_in_distribution_export
155143
end
156144

157145
row
158146
end
159-
160-
def make_item_quantity_and_value_slots
161-
slots = Array.new(item_headers.size, 0)
162-
slots = slots.map.with_index { |value, index| index.odd? ? Money.new(0) : value } if @organization.include_in_kind_values_in_exported_files
163-
slots
164-
end
165147
end
166148
end

app/views/organizations/_details.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@
254254
<p>
255255
<%= humanize_boolean(@organization.include_in_kind_values_in_exported_files) %>
256256
</p>
257+
<h6 class='font-weight-bold'>Include packages in distribution export:</h6>
258+
<p>
259+
<%= humanize_boolean(@organization.include_packages_in_distribution_export) %>
260+
</p>
257261
</div>
258262

259263
<h4>Annual Survey</h4>

app/views/organizations/edit.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171

172172
<h4>Exports</h4>
173173
<%= f.input :include_in_kind_values_in_exported_files, label: 'Include in-kind value in donation and distribution exports?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
174+
<%= f.input :include_packages_in_distribution_export, label: 'Include packages in distribution export?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
174175

175176
<h4>Annual Survey</h4>
176177
<%= f.input :repackage_essentials, label: 'Does your Bank repackage essentials?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddIncludePackagesInDistributionExportToOrganizations < ActiveRecord::Migration[8.0]
2+
def change
3+
add_column :organizations, :include_packages_in_distribution_export, :boolean, default: false, null: false
4+
end
5+
end

db/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@
520520
t.boolean "receive_email_on_requests", default: false, null: false
521521
t.boolean "include_in_kind_values_in_exported_files", default: false, null: false
522522
t.string "reminder_schedule_definition"
523+
t.boolean "include_packages_in_distribution_export", default: false, null: false
523524
t.boolean "bank_is_set_up", default: false, null: false
524525
t.index ["latitude", "longitude"], name: "index_organizations_on_latitude_and_longitude"
525526
end

docs/user_guide/bank/exports.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ Click "My Organization" in the left hand menu. Click "Edit" button. Set the "Inc
149149

150150
[!NOTE] Setting this affects both the donation and distribution exports.
151151

152+
### Add Package Counts for each item
153+
If you want to also have the export include the package count for each item in the distributions, you can set that option.
154+
- Click "My Organization" in the left hand menu.
155+
- Click "Edit" button.
156+
- Set the "Include packages in distribution export" to "yes"
157+
- Click "Save"
158+
152159
## Donations
153160

154161
### Navigating to export Donations

spec/factories/organizations.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# hide_package_column_on_receipt :boolean default(FALSE)
1616
# hide_value_columns_on_receipt :boolean default(FALSE)
1717
# include_in_kind_values_in_exported_files :boolean default(FALSE), not null
18+
# include_packages_in_distribution_export :boolean default(FALSE), not null
1819
# intake_location :integer
1920
# invitation_text :text
2021
# latitude :float

spec/models/organization_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# hide_package_column_on_receipt :boolean default(FALSE)
1616
# hide_value_columns_on_receipt :boolean default(FALSE)
1717
# include_in_kind_values_in_exported_files :boolean default(FALSE), not null
18+
# include_packages_in_distribution_export :boolean default(FALSE), not null
1819
# intake_location :integer
1920
# invitation_text :text
2021
# latitude :float

0 commit comments

Comments
 (0)