Class: ItemBuilder::Modes::QuantityService

Inherits:
Object
  • Object
show all
Includes:
ItemBuilder::Modes
Defined in:
lib/item_builder/modes/quantity_service.rb

Constant Summary collapse

QUANTITY_CHANNEL =
{}.tap do |hash|
  hash[3]       = :Lazada
  hash[13]      = :Zalora
end.freeze

Instance Attribute Summary

Attributes included from ItemBuilder::Modes

#bundles, #existing_alloc_stocks, #item_bundle_variants, #listing, #stock_allocs, #variants, #wh_spaces

Instance Method Summary collapse

Methods included from ItemBuilder::Modes

#base, #initialize

Instance Method Details

#available_quantityObject



47
48
49
50
51
52
53
54
# File 'lib/item_builder/modes/quantity_service.rb', line 47

def available_quantity
  ItemBuilder::GetQuantityService.new(
    listing: listing, variant: variant,
    wh_sp: warehouse, stock_alloc: stock_alloc,
    bundle_variants: bundle_variants,
    existing_allocated_stock: existing_allocated_stock
  ).perform
end

#bundleObject



64
65
66
# File 'lib/item_builder/modes/quantity_service.rb', line 64

def bundle
  bundles.select {|b| b.variant_id == listing.variant_id }.first
end

#bundle_variantsObject



60
61
62
# File 'lib/item_builder/modes/quantity_service.rb', line 60

def bundle_variants
  item_bundle_variants.select {|ibv| ibv.bundle_id == bundle.id }
end

#channel_nameObject



43
44
45
# File 'lib/item_builder/modes/quantity_service.rb', line 43

def channel_name
  QUANTITY_CHANNEL[listing.channel_id].to_s
end

#existing_allocated_stockObject



56
57
58
# File 'lib/item_builder/modes/quantity_service.rb', line 56

def existing_allocated_stock
  existing_alloc_stocks.select {|eas| eas.variant_association_id == listing.id }
end

#performObject



19
20
21
# File 'lib/item_builder/modes/quantity_service.rb', line 19

def perform
  to_h.merge(base)
end

#qtyObject



29
30
31
32
33
34
35
# File 'lib/item_builder/modes/quantity_service.rb', line 29

def qty
  if channel_name.empty?
    available_quantity
  else
    qty_channel
  end
end

#qty_channelObject



37
38
39
40
41
# File 'lib/item_builder/modes/quantity_service.rb', line 37

def qty_channel
  class_name = "ItemBuilder::Modes::Quantity::#{channel_name}Service"
  qty_channel_service = class_name.constantize
  qty_channel_service.new(listing, available_quantity).perform
end

#stock_allocObject



68
69
70
# File 'lib/item_builder/modes/quantity_service.rb', line 68

def stock_alloc
  stock_allocs.select {|sa| sa.variant_association_id == listing.id }.first
end

#to_hObject



23
24
25
26
27
# File 'lib/item_builder/modes/quantity_service.rb', line 23

def to_h
  {
    quantity: qty
  }
end

#variantObject



76
77
78
# File 'lib/item_builder/modes/quantity_service.rb', line 76

def variant
  variants.select {|v| v.id == listing.variant_id }.first
end

#warehouseObject



72
73
74
# File 'lib/item_builder/modes/quantity_service.rb', line 72

def warehouse
  wh_spaces.select {|ws| ws.item_variant_id == listing.variant_id }.first
end