Class: ItemBuilderMwh::Modes::MapCreateService

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

Constant Summary collapse

MAP_CREATE_CHANNEL =
{}.tap do |hash|
  hash[12]      = :Shopee
  hash[15]      = :Tokopedia
end.freeze
QUANTITY_CHANNEL =

Get Quantity Data

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

Get Price

{}.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, #existing_allocated_stock, #initialize, #listings, #multiwarehouse, #pca, #stock_alloc, #variant, #variant_id, #warehouse, #warehouse_list, #warehouse_mapping, #warehouse_spaces, #warehouses, #wh_mapping

Instance Method Details

#available_quantity(warehouse_space) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 144

def available_quantity(warehouse_space)
  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: warehouse_space.quantity, wh_routing: wh_routing,
    selected_warehouse_space: warehouse_space
  ).perform
end

#channel_nameObject



77
78
79
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 77

def channel_name
  MAP_CREATE_CHANNEL[listing.channel_id].to_s
end

#credentialObject



132
133
134
135
136
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 132

def credential
  return shopify_inventory_location['credential'] if shopify_inventory_location['credential'].present?

  nil
end

#imagesObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 51

def images
  listing.item_listing_variant_images.map do |img|
    {
      id: img.id,
      local_id: img.local_id,
      local_url: img.local_url,
      url: img.image.image.url(:xlarge, timestamp: false)
    }
  end
end

#local_qtyObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 116

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

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

    zalora_reserved_stock[listing.local_id].to_i
  else
    return 0 if lazada_quantity.blank?

    lazada_quantity[listing.local_id].to_i
  end
end

#map_createObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 33

def map_create
  {
    name: listing.name,
    description: listing.description,
    weight: listing.package_weight,
    length: listing.package_length,
    width: listing.package_width,
    height: listing.package_height,
    condition: listing.new_condition,
    option_name: listing.option_name,
    option_value: listing.option_value,
    option2_name: listing.option2_name,
    option2_value: listing.option2_value,
    internal_data: listing.internal_data,
    images: images
  }
end

#map_create_channelObject



71
72
73
74
75
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 71

def map_create_channel
  class_name = "ItemBuilderMwh::Modes::MapCreate::#{channel_name}Service"
  create_channel_service = class_name.constantize
  create_channel_service.new(listing).perform
end

#performObject



29
30
31
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 29

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

#priceObject



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 164

def price
  if price_channel_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



177
178
179
180
181
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 177

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

#price_channel_nameObject



183
184
185
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 183

def price_channel_name
  PRICE_CHANNEL[listing.channel_id].to_s
end

#qty(warehouse_space) ⇒ Object



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

def qty(warehouse_space)
  if quantity_channel_name.empty? || quantity_channel_name == "Shopify"
    available_quantity(warehouse_space)
  else
    qty_channel(warehouse_space)
  end
end

#qty_channel(warehouse_space) ⇒ Object



106
107
108
109
110
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 106

def qty_channel(warehouse_space)
  class_name = "ItemBuilderMwh::Modes::Quantity::#{quantity_channel_name}Service"
  qty_channel_service = class_name.constantize
  qty_channel_service.new(listing, available_quantity(warehouse_space), local_qty.to_i).perform
end

#quantity_channel_nameObject



112
113
114
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 112

def quantity_channel_name
  QUANTITY_CHANNEL[listing.channel_id].to_s
end

#real_quantity(warehouse_space) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 90

def real_quantity(warehouse_space)
  if quantity_channel_name == 'Zilingo'
    qty(warehouse_space)
  else
    [qty(warehouse_space), 0].sort[1]
  end
end

#to_h(warehouse_space) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 62

def to_h(warehouse_space)
  {
    quantity: warehouse_space.present? ? real_quantity(warehouse_space) : 0,
    warehouse_id: warehouse_space.present? ? wh_mapping(
      warehouse_space.warehouse_id
    ) : ''
  }.merge(price)
end

#wh_routingObject



138
139
140
141
142
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 138

def wh_routing
  return credential['warehouse_routing'] if credential.present? && credential['warehouse_routing'].present?

  0
end