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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'app/services/cats/core/space_service.rb', line 8
def available_space(id, level)
case level
when HUB
hub = Cats::Core::Location.find(id)
warehouses = hub.children
warehouse_ids = warehouses.map(&:id)
stores = Cats::Core::Store.joins(:warehouse)
.where(
cats_core_locations: {
location_type: Cats::Core::Location::WAREHOUSE,
id: warehouse_ids
}
).group_by(&:warehouse_id)
wh_details = []
stores.each do |key, value|
total_space = value.inject(0) { |sum, val| sum + val.available_space }
details = value.map do |val|
{store_id: val.id, code: val.code, name: val.name, total_space: val.available_space}
end
warehouse = warehouses.find(key)
wh_details << {
warehouse_id: key,
code: warehouse.code,
name: warehouse.name,
total_space: total_space,
details: details
}
end
hub_space = wh_details.inject(0) { |sum, val| sum + val[:total_space] }
space = {
hub_id: id,
code: hub.code,
name: hub.name,
total_space: hub_space,
details: wh_details
}
when WAREHOUSE
warehouse = Cats::Core::Location.find(id)
stores = Cats::Core::Store.where(warehouse_id: id)
details = stores.map do |store|
{store_id: store.id, code: store.code, name: store.name, total_space: store.available_space}
end
total_space = stores.inject(0) { |sum, val| sum + val.available_space }
space = {
warehouse_id: id,
code: warehouse.code,
name: warehouse.name,
total_space: total_space,
details: details
}
when STORE
store = Cats::Core::Store.find(id)
space = {
store_id: store.id,
code: store.code,
name: store.name,
total_space: store.available_space
}
end
space
end
|