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[2]       = :Shopify
  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, #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



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

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



66
67
68
# File 'lib/item_builder_mwh/modes/quantity_service.rb', line 66

def channel_name
  QUANTITY_CHANNEL[listing.channel_id].to_s
end

#credentialObject



86
87
88
89
90
# File 'lib/item_builder_mwh/modes/quantity_service.rb', line 86

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

  nil
end

#local_qtyObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/item_builder_mwh/modes/quantity_service.rb', line 70

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

    zilingo_quantity[listing.local_id].to_i
  elsif 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

#performObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/item_builder_mwh/modes/quantity_service.rb', line 23

def perform
  if channel_name == "Shopify"
    dataSIL = shopify_inventory_location[listing.local_id]

    base.merge!(dataSIL, warehouse: warehouses ) if dataSIL.present?
  else
    base.merge!(
      warehouse: warehouses
    )
  end
end

#qty(warehouse_space) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/item_builder_mwh/modes/quantity_service.rb', line 52

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

#qty_channel(warehouse_space) ⇒ Object



60
61
62
63
64
# File 'lib/item_builder_mwh/modes/quantity_service.rb', line 60

def qty_channel(warehouse_space)
  class_name = "ItemBuilderMwh::Modes::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

#real_quantity(warehouse_space) ⇒ Object



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

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

#to_h(warehouse_space) ⇒ Object



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

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
    ) : ''
  }
end

#wh_routingObject



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

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

  0
end