Class: ItemBuilderMwh::Modes::QuantityService

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

Constant Summary collapse

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

Instance Attribute Summary

Attributes included from ItemBuilderMwh::Modes

#bundles, #existing_alloc_stocks, #item_bundle_variants, #listing, #reserved_stocks, #stock_allocs, #variant_listings, #variants, #wh_spaces, #zalora_reserved_stock, #zilingo_delta_quantity

Instance Method Summary collapse

Methods included from ItemBuilderMwh::Modes

#base, #bundle, #bundle_variants, #existing_allocated_stock, #initialize, #listings, #pca, #stock_alloc, #variant, #warehouse, #warehouse_mapping, #warehouse_spaces, #warehouses, #wh_mapping

Instance Method Details

#available_quantity(qty) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/item_builder_mwh/modes/quantity_service.rb', line 65

def available_quantity(qty)
  ItemBuilderMwh::GetQuantityService.new(
    listing: listing, stock_alloc: stock_alloc,
    variant: variant, bundle_variants: bundle_variants,
    existing_allocated_stock: existing_allocated_stock,
    listing_wh_sp_quantity: qty
  ).perform
end

#channel_nameObject



51
52
53
# File 'lib/item_builder_mwh/modes/quantity_service.rb', line 51

def channel_name
  QUANTITY_CHANNEL[listing.channel_id].to_s
end

#performObject



22
23
24
25
26
# File 'lib/item_builder_mwh/modes/quantity_service.rb', line 22

def perform
  base.merge!(
    warehouse: warehouses
  )
end

#qty(qty) ⇒ Object



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

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

#qty_channel(qty) ⇒ Object



45
46
47
48
49
# File 'lib/item_builder_mwh/modes/quantity_service.rb', line 45

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

#reserved_stockObject



55
56
57
58
59
60
61
62
63
# File 'lib/item_builder_mwh/modes/quantity_service.rb', line 55

def reserved_stock
  if channel_name == 'Zilingo'
    zilingo_delta_quantity[listing.local_id].to_i
  elsif channel_name == 'Zalora'
    zalora_reserved_stock[listing.local_id].to_i
  else
    reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
  end
end

#to_h(warehouse_space) ⇒ Object



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

def to_h(warehouse_space)
  {
    quantity: [qty(warehouse_space.quantity), 0].sort[1],
    warehouse_id: wh_mapping(
      warehouse_space.warehouse_id
    )
  }
end