Class: ItemBuilderMwh::Modes::SimpleService

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

Constant Summary collapse

QUANTITY_CHANNEL =
{}.tap do |hash|
  hash[3]       = :Lazada
  hash[13]      = :Zalora
end.freeze
PRICE_CHANNEL =
{}.tap do |hash|
  hash[2]       = :Shopify
  hash[9]       = :Blibli
  hash[11]      = :Bukalapak
  hash[13]      = :Zalora
  hash[16]      = :Jd
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



98
99
100
101
102
103
104
105
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 98

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

#performObject



30
31
32
33
34
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 30

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

#priceObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 69

def price
  if price_name.empty?
    {
      price: listing.price,
      sale_price: listing.sale_price,
      sale_start_at: listing.sale_start_at,
      sale_end_at: listing.sale_end_at
    }
  else
    price_channel
  end
end

#price_channelObject



82
83
84
85
86
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 82

def price_channel
  class_name = "ItemBuilderMwh::Modes::Price::#{price_name}Service"
  simple_channel_service = class_name.constantize
  simple_channel_service.new(listing).perform
end

#price_nameObject



107
108
109
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 107

def price_name
  PRICE_CHANNEL[listing.channel_id].to_s
end

#qty(qty) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 57

def qty(qty)
  if quantity_name.empty?
    {
      quantity: available_quantity(qty)
    }
  else
    {
      quantity: qty_channel(qty)
    }
  end
end

#qty_channel(qty) ⇒ Object



88
89
90
91
92
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 88

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

#quantity_nameObject



111
112
113
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 111

def quantity_name
  QUANTITY_CHANNEL[listing.channel_id].to_s
end

#reserved_stockObject



94
95
96
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 94

def reserved_stock
  reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
end

#simple(warehouse_space) ⇒ Object



50
51
52
53
54
55
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 50

def simple(warehouse_space)
  qty = warehouse_space.quantity
  qty(qty).merge(
    price
  )
end

#to_h(warehouse_space) ⇒ Object



36
37
38
39
40
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 36

def to_h(warehouse_space)
  warehouse_ids(warehouse_space).merge(
    simple(warehouse_space)
  )
end

#warehouse_ids(warehouse_space) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 42

def warehouse_ids(warehouse_space)
  {
    warehouse_id: wh_mapping(
      warehouse_space.warehouse_id
    )
  }
end