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
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



141
142
143
144
145
146
147
148
149
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 141

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



74
75
76
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 74

def channel_name
  MAP_CREATE_CHANNEL[listing.channel_id].to_s
end

#credentialObject



129
130
131
132
133
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 129

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

  nil
end

#imagesObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 48

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
    }
  end
end

#local_qtyObject



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

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



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

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



68
69
70
71
72
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 68

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



26
27
28
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 26

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

#priceObject



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 161

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



174
175
176
177
178
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 174

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



180
181
182
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 180

def price_channel_name
  PRICE_CHANNEL[listing.channel_id].to_s
end

#qty(warehouse_space) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 95

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



103
104
105
106
107
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 103

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



109
110
111
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 109

def quantity_channel_name
  QUANTITY_CHANNEL[listing.channel_id].to_s
end

#real_quantity(warehouse_space) ⇒ Object



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

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



59
60
61
62
63
64
65
66
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 59

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



135
136
137
138
139
# File 'lib/item_builder_mwh/modes/map_create_service.rb', line 135

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

  0
end