|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class ItemsFlowQuery |
| 4 | + attr_reader :organization |
| 5 | + attr_reader :filter_params |
| 6 | + attr_reader :storage_location |
| 7 | + |
| 8 | + def initialize(organization:, storage_location:, filter_params: nil) |
| 9 | + @organization = organization |
| 10 | + @storage_location = storage_location |
| 11 | + @filter_params = filter_params |
| 12 | + end |
| 13 | + |
| 14 | + def call |
| 15 | + query = <<~SQL |
| 16 | + WITH line_items_with_flags AS ( |
| 17 | + SELECT |
| 18 | + li.item_id, |
| 19 | + it.name AS item_name, |
| 20 | + -- in quantity for this row (0 if not matching) |
| 21 | + CASE |
| 22 | + WHEN (donations.storage_location_id = :id |
| 23 | + OR purchases.storage_location_id = :id |
| 24 | + OR (adjustments.storage_location_id = :id AND li.quantity > 0) |
| 25 | + OR transfers.to_id = :id) |
| 26 | + AND it.organization_id = :organization_id |
| 27 | + THEN li.quantity |
| 28 | + ELSE 0 |
| 29 | + END AS quantity_in, |
| 30 | + -- out quantity normalized to positive numbers (0 if not matching) |
| 31 | + CASE |
| 32 | + WHEN (distributions.storage_location_id = :id |
| 33 | + OR (adjustments.storage_location_id = :id AND li.quantity < 0) |
| 34 | + OR transfers.from_id = :id) |
| 35 | + AND it.organization_id = :organization_id |
| 36 | + THEN CASE WHEN li.quantity < 0 THEN -li.quantity ELSE li.quantity END |
| 37 | + ELSE 0 |
| 38 | + END AS quantity_out, |
| 39 | + -- mark rows that are relevant for the overall WHERE in original query |
| 40 | + CASE |
| 41 | + WHEN (donations.storage_location_id = :id |
| 42 | + OR purchases.storage_location_id = :id |
| 43 | + OR distributions.storage_location_id = :id |
| 44 | + OR transfers.from_id = :id |
| 45 | + OR transfers.to_id = :id |
| 46 | + OR adjustments.storage_location_id = :id) |
| 47 | + AND it.organization_id = :organization_id |
| 48 | + THEN 1 ELSE 0 |
| 49 | + END AS relevant |
| 50 | + FROM line_items li |
| 51 | + LEFT JOIN donations ON donations.id = li.itemizable_id AND li.itemizable_type = 'Donation' |
| 52 | + LEFT JOIN purchases ON purchases.id = li.itemizable_id AND li.itemizable_type = 'Purchase' |
| 53 | + LEFT JOIN distributions ON distributions.id = li.itemizable_id AND li.itemizable_type = 'Distribution' |
| 54 | + LEFT JOIN adjustments ON adjustments.id = li.itemizable_id AND li.itemizable_type = 'Adjustment' |
| 55 | + LEFT JOIN transfers ON transfers.id = li.itemizable_id AND li.itemizable_type = 'Transfer' |
| 56 | + LEFT JOIN items it ON it.id = li.item_id |
| 57 | + ) |
| 58 | + SELECT |
| 59 | + item_id, |
| 60 | + item_name, |
| 61 | + SUM(quantity_in) AS quantity_in, |
| 62 | + SUM(quantity_out) AS quantity_out, |
| 63 | + SUM(quantity_in) - SUM(quantity_out) AS change, |
| 64 | + SUM(SUM(quantity_in)) OVER () AS total_quantity_in, |
| 65 | + SUM(SUM(quantity_out)) OVER () AS total_quantity_out, |
| 66 | + SUM(SUM(quantity_in) - SUM(quantity_out)) OVER () AS total_change |
| 67 | + FROM line_items_with_flags |
| 68 | + WHERE relevant = 1 |
| 69 | + GROUP BY item_id, item_name |
| 70 | + ORDER BY item_name; |
| 71 | + SQL |
| 72 | + |
| 73 | + ActiveRecord::Base.connection.exec_query( |
| 74 | + ActiveRecord::Base.send(:sanitize_sql_array, [query, {id: @storage_location.id, organization_id: @organization.id}]) |
| 75 | + ) |
| 76 | + end |
| 77 | +end |
0 commit comments