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, #lazada_quantity, #listing, #reserved_stocks, #shipping_providers, #shopify_inventory_location, #stock_allocs, #variant_listings, #variants, #wh_spaces, #zalora_reserved_stock, #zilingo_quantity

Instance Method Summary collapse

Methods included from ItemBuilderMwh::Modes

#base, #bundle, #bundle_variants, #credential, #existing_allocated_stock, #initialize, #listings, #multiwarehouse, #pca, #stock_alloc, #variant, #variant_id, #warehouse, #warehouse_list, #warehouse_mapping, #warehouse_spaces, #warehouses, #wh_mapping, #wh_routing

Instance Method Details

#available_quantity(qty) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 108

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

#local_qtyObject



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

def local_qty
  if quantity_name == 'Zilingo'
    return 0 if zilingo_quantity.blank?

    zilingo_quantity[listing.local_id].to_i
  elsif quantity_name == 'Zalora'
    return 0 if zalora_reserved_stock.blank?

    zalora_reserved_stock[listing.local_id].to_i
  else
    reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
  end
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



117
118
119
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 117

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), local_qty.to_i).perform
end

#quantity_nameObject



121
122
123
# File 'lib/item_builder_mwh/modes/simple_service.rb', line 121

def quantity_name
  QUANTITY_CHANNEL[listing.channel_id].to_s
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 || 0
  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: warehouse_space.present? ? wh_mapping(
      warehouse_space.warehouse_id
    ) : ''
  }
end