4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 
     | 
    
      # File 'app/services/cats/core/stack_service.rb', line 4
def items_for_location(user)
  details = user.details
  key_available = details.keys.any? { |key| %w[stores warehouse hub].include?(key) }
  raise(StandardError, "User does not have associated location.") unless key_available
  hub = if details["hub"]
          Location.find(details["hub"])
        elsif details["warehouse"]
          Location.find(details["warehouse"]).parent
        else
          Store.find(details["stores"][0]).warehouse.parent
        end
  allocation_items =
    DispatchPlanItem
    .joins(:dispatch_plan)
    .where(destination: hub, dispatch_plan: {status: DispatchPlan::APPROVED})
  allocation_items.map do |item|
    {
      reference_no: item.dispatch_plan.reference_no,
      source: item.source.name,
      destination: item.destination.name,
      quantity: item.quantity,
      batch_no: item.commodity.batch_no,
      commodity_id: item.commodity_id,
      commodity_name: item.commodity.name,
      unit: item.commodity.unit_abbreviation,
      package_unit_id: item.commodity.package_unit_id,
      package_unit_abbreviation: item.commodity.package_unit_abbreviation
    }
  end
end
     |