|
| 1 | +module View |
| 2 | + Donations = Data.define( |
| 3 | + :donations, |
| 4 | + :filters, |
| 5 | + :item_categories, |
| 6 | + :paginated_donations, |
| 7 | + :product_drives, |
| 8 | + :product_drive_participants, |
| 9 | + :storage_locations, |
| 10 | + :donation_sites, |
| 11 | + :manufacturers |
| 12 | + ) do |
| 13 | + include DateRangeHelper |
| 14 | + |
| 15 | + class << self |
| 16 | + def filter_params(params) |
| 17 | + if params.key?(:filters) |
| 18 | + params.require(:filters).permit( |
| 19 | + :at_storage_location, :by_source, :from_donation_site, |
| 20 | + :by_product_drive, :by_product_drive_participant, |
| 21 | + :from_manufacturer, :by_category |
| 22 | + ) |
| 23 | + else |
| 24 | + {} |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + def from_params(params:, organization:, helpers:) |
| 29 | + filters = filter_params(params) |
| 30 | + donations = organization.donations |
| 31 | + .includes(:storage_location, |
| 32 | + :donation_site, |
| 33 | + :product_drive, |
| 34 | + :product_drive_participant, |
| 35 | + :manufacturer, |
| 36 | + line_items: [:item]) |
| 37 | + .order(created_at: :desc) |
| 38 | + .class_filter(filters) |
| 39 | + .during(helpers.selected_range) |
| 40 | + |
| 41 | + paginated_donations = donations.page(params[:page]) |
| 42 | + |
| 43 | + storage_locations = donations.filter_map do |donation| |
| 44 | + donation.storage_location unless donation.storage_location.discarded_at |
| 45 | + end.compact.uniq.sort |
| 46 | + |
| 47 | + manufacturers = donations.collect(&:manufacturer).compact.uniq.sort |
| 48 | + |
| 49 | + new( |
| 50 | + donations: donations, |
| 51 | + filters: filters, |
| 52 | + item_categories: organization.item_categories.pluck(:name).uniq, |
| 53 | + paginated_donations: paginated_donations, |
| 54 | + product_drives: organization.product_drives.alphabetized, |
| 55 | + product_drive_participants: organization.product_drive_participants.alphabetized, |
| 56 | + storage_locations: storage_locations, |
| 57 | + donation_sites: donations.map(&:donation_site).compact.uniq.sort_by { |site| site.name.downcase }, |
| 58 | + manufacturers: manufacturers |
| 59 | + ) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + def selected_storage_location |
| 64 | + filters[:at_storage_location] |
| 65 | + end |
| 66 | + |
| 67 | + def selected_source |
| 68 | + filters[:by_source] |
| 69 | + end |
| 70 | + |
| 71 | + def selected_item_category |
| 72 | + filters[:by_category] |
| 73 | + end |
| 74 | + |
| 75 | + def sources |
| 76 | + donations.map(&:source).uniq.sort |
| 77 | + end |
| 78 | + |
| 79 | + def donations_quantity |
| 80 | + donations.map(&:total_quantity).sum |
| 81 | + end |
| 82 | + |
| 83 | + def selected_donation_site |
| 84 | + filters[:from_donation_site] |
| 85 | + end |
| 86 | + |
| 87 | + def selected_product_drive |
| 88 | + filters[:by_product_drive] |
| 89 | + end |
| 90 | + |
| 91 | + def selected_product_drive_participant |
| 92 | + filters[:by_product_drive_participant] |
| 93 | + end |
| 94 | + |
| 95 | + def selected_manufacturer |
| 96 | + filters[:from_manufacturer] |
| 97 | + end |
| 98 | + |
| 99 | + def paginated_donations_quantity |
| 100 | + paginated_donations.map(&:total_quantity).sum |
| 101 | + end |
| 102 | + |
| 103 | + def paginated_in_kind_value |
| 104 | + paginated_donations.sum { |donation| donation.value_per_itemizable } |
| 105 | + end |
| 106 | + |
| 107 | + def total_money_raised |
| 108 | + donations.sum { |d| d.money_raised.to_i } |
| 109 | + end |
| 110 | + |
| 111 | + def total_value_all_donations |
| 112 | + donations.sum { |donation| donation.value_per_itemizable } |
| 113 | + end |
| 114 | + end |
| 115 | +end |
0 commit comments