Class: Cats::Core::StackService

Inherits:
Object
  • Object
show all
Defined in:
app/services/cats/core/stack_service.rb

Instance Method Summary collapse

Instance Method Details

#dispatch_stacks(dispatch) ⇒ Object



45
46
47
48
49
50
51
52
# File 'app/services/cats/core/stack_service.rb', line 45

def dispatch_stacks(dispatch)
  location = dispatch.dispatch_plan_item.source
  warehouses = location.children
  stores = Store.where(warehouse: warehouses)

  commodity = dispatch.dispatch_plan_item.commodity
  Stack.where(commodity: commodity, store: stores, stack_status: Stack::ALLOCATED)
end

#items_for_location(user) ⇒ Object

Raises:

  • (StandardError)


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

#receipt_stacks(authorization_id) ⇒ Object



39
40
41
42
43
# File 'app/services/cats/core/stack_service.rb', line 39

def receipt_stacks(authorization_id)
  authorization = ReceiptAuthorization.find(authorization_id)
  commodity = authorization.dispatch.dispatch_plan_item.commodity
  Stack.where(commodity: commodity, store_id: authorization.store_id)
end