Class: ItemBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/item_builder.rb,
lib/item_builder/modes.rb,
lib/item_builder/version.rb,
lib/item_builder/modes/price/base.rb,
lib/item_builder/modes/update/base.rb,
lib/item_builder/modes/base_service.rb,
lib/item_builder/modes/price_service.rb,
lib/item_builder/modes/quantity/base.rb,
lib/item_builder/get_quantity_service.rb,
lib/item_builder/modes/active_service.rb,
lib/item_builder/modes/simple_service.rb,
lib/item_builder/modes/update_service.rb,
lib/item_builder/modes/price/jd_service.rb,
lib/item_builder/modes/quantity_service.rb,
lib/item_builder/lazada_quantity_service.rb,
lib/item_builder/modes/update/jd_service.rb,
lib/item_builder/zalora_quantity_service.rb,
lib/item_builder/shopify_quantity_service.rb,
lib/item_builder/zilingo_quantity_service.rb,
lib/item_builder/modes/price/blibli_service.rb,
lib/item_builder/modes/price/zalora_service.rb,
lib/item_builder/modes/price/shopify_service.rb,
lib/item_builder/modes/update/blibli_service.rb,
lib/item_builder/modes/update/lazada_service.rb,
lib/item_builder/modes/update/zalora_service.rb,
lib/item_builder/modes/update/shopify_service.rb,
lib/item_builder/modes/price/bukalapak_service.rb,
lib/item_builder/modes/price/sale_price_policy.rb,
lib/item_builder/modes/quantity/lazada_service.rb,
lib/item_builder/modes/quantity/zalora_service.rb,
lib/item_builder/modes/quantity/zilingo_service.rb,
lib/item_builder/modes/update/bukalapak_service.rb,
lib/item_builder/modes/update/tokopedia_service.rb

Defined Under Namespace

Modules: Modes Classes: GetQuantityService, LazadaQuantityService, ShopifyQuantityService, ZaloraQuantityService, ZilingoQuantityService

Constant Summary collapse

VERSION =
'0.1.47'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(listing_ids, mode) ⇒ ItemBuilder

Returns a new instance of ItemBuilder.



27
28
29
30
# File 'lib/item_builder.rb', line 27

def initialize(listing_ids, mode)
  @listing_ids = listing_ids
  @mode = mode
end

Instance Attribute Details

#listing_idsObject (readonly)

Returns the value of attribute listing_ids.



20
21
22
# File 'lib/item_builder.rb', line 20

def listing_ids
  @listing_ids
end

#listingsObject (readonly)

Returns the value of attribute listings.



21
22
23
# File 'lib/item_builder.rb', line 21

def listings
  @listings
end

#modeObject (readonly)

Returns the value of attribute mode.



22
23
24
# File 'lib/item_builder.rb', line 22

def mode
  @mode
end

#variant_idsObject (readonly)

Returns the value of attribute variant_ids.



25
26
27
# File 'lib/item_builder.rb', line 25

def variant_ids
  @variant_ids
end

#variantsObject (readonly)

Returns the value of attribute variants.



24
25
26
# File 'lib/item_builder.rb', line 24

def variants
  @variants
end

#wh_spacesObject (readonly)

Returns the value of attribute wh_spaces.



23
24
25
# File 'lib/item_builder.rb', line 23

def wh_spaces
  @wh_spaces
end

Class Method Details

.build(listing_ids, mode) ⇒ Object



16
17
18
# File 'lib/item_builder.rb', line 16

def self.build(listing_ids, mode)
  new(listing_ids, mode).mode_check
end

Instance Method Details

#account_idObject



206
207
208
# File 'lib/item_builder.rb', line 206

def 
   ||= listings[0].profile_channel_association_id
end

#bundle_idsObject



116
117
118
# File 'lib/item_builder.rb', line 116

def bundle_ids
  @bundle_ids ||= bundles.map(&:id).uniq
end

#bundlesObject



120
121
122
# File 'lib/item_builder.rb', line 120

def bundles
  @bundles ||= Bundle.where(variant_id: variant_ids).group(:variant_id)
end

#defaultObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/item_builder.rb', line 80

def default
  listings.map do |listing|
    next unless listing.local_id.present?

    if listing.channel_id == 2 && mode == :active
      modes[mode].new(qty_simple_params(listing)).perform
    elsif listing.channel_id == 18 && mode == :active
      modes[mode].new(qty_simple_params(listing)
                      .merge(zilingo_quantity: zilingo_quantity)
                     ).perform
    else
      modes[mode].new(listing: listing).perform
    end
  end
end

#existing_alloc_stocksObject



96
97
98
99
100
# File 'lib/item_builder.rb', line 96

def existing_alloc_stocks
  @existing_alloc_stocks ||= VariantListingStockAllocation.where(
    variant_association_id: vl_ids
  ).where('ADDTIME(end_at, "07:00") >= NOW()')
end

#item_bundle_variantsObject



110
111
112
113
114
# File 'lib/item_builder.rb', line 110

def item_bundle_variants
  @item_bundle_variants ||= BundleVariant.where(
    bundle_id: bundle_ids
  )
end

#lazada_quantityObject



168
169
170
171
172
# File 'lib/item_builder.rb', line 168

def lazada_quantity
  @lazada_quantity ||= ItemBuilder::LazadaQuantityService.new(
    listings: listings, skus: skus
  ).perform
end

#mode_checkObject



32
33
34
35
36
37
38
# File 'lib/item_builder.rb', line 32

def mode_check
  if mode == :quantity || mode == :simple
    quantity_simple_mode
  else
    default
  end
end

#modesObject



174
175
176
177
178
179
180
181
182
# File 'lib/item_builder.rb', line 174

def modes
  {
    price: ItemBuilder::Modes::PriceService,
    quantity: ItemBuilder::Modes::QuantityService,
    simple: ItemBuilder::Modes::SimpleService,
    active: ItemBuilder::Modes::ActiveService,
    update: ItemBuilder::Modes::UpdateService
  }
end

#order_hostObject



184
185
186
187
# File 'lib/item_builder.rb', line 184

def order_host
  url = ENV['ORDERS_URL'] || 'orders.forstok.com'
  url + '/api/v3/item_line/reserved_stock'
end

#qty_simple_params(listing) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/item_builder.rb', line 70

def qty_simple_params(listing)
  {
    listing: listing, wh_spaces: wh_spaces, variants: variants,
    stock_allocs: stock_allocs, variant_listings: variant_listings,
    bundles: bundles, item_bundle_variants: item_bundle_variants,
    existing_alloc_stocks: existing_alloc_stocks,
    reserved_stocks: reserved_stocks, wh_id: wh_id
  }
end

#quantity_simple_modeObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/item_builder.rb', line 40

def quantity_simple_mode
  listings.map do |listing|
    next unless listing.local_id.present?

    if listing.channel_id == 18
      new_param = qty_simple_params(listing)
                  .merge(zilingo_quantity: zilingo_quantity)

      modes[mode].new(new_param).perform
    elsif listing.channel_id == 13
      new_param = qty_simple_params(listing)
                  .merge(zalora_reserved_stock: zalora_reserved_stock)

      modes[mode].new(new_param).perform
    elsif listing.channel_id == 2
      new_param = qty_simple_params(listing)
                  .merge({shopify_inventory_location: shopify_inventory_location})

      modes[mode].new(new_param).perform
    elsif listing.channel_id == 3
      new_param = qty_simple_params(listing)
                  .merge({lazada_quantity: lazada_quantity})

      modes[mode].new(new_param).perform
    else
      modes[mode].new(qty_simple_params(listing)).perform
    end
  end.compact
end

#reserved_paramsObject



189
190
191
192
# File 'lib/item_builder.rb', line 189

def reserved_params
  "account_id=#{account_id}
  &item_variant_ids=#{variant_ids.join(',')}"
end

#reserved_stocksObject



194
195
196
197
198
# File 'lib/item_builder.rb', line 194

def reserved_stocks
  @reserved_stocks ||= JSON.parse(RestClient.get(
    "#{order_host}?#{reserved_params}"
  ).body) if [3].include?(listings[0].channel_id)
end

#shopify_inventory_locationObject



162
163
164
165
166
# File 'lib/item_builder.rb', line 162

def shopify_inventory_location
  @shopify_inventory_location ||= ItemBuilder::ShopifyQuantityService.new(
    listings: listings, skus: skus
  ).perform
end

#skusObject



146
147
148
# File 'lib/item_builder.rb', line 146

def skus
  @skus ||= listings.map(&:local_id).uniq
end

#stock_allocsObject



124
125
126
127
128
# File 'lib/item_builder.rb', line 124

def stock_allocs
  @stock_allocs ||= VariantListingStockAllocation.where(
    variant_association_id: vl_ids
  )
end

#variant_listingsObject



106
107
108
# File 'lib/item_builder.rb', line 106

def variant_listings
  @variant_listings ||= VariantListing.where(variant_id: variant_ids)
end

#vl_idsObject



102
103
104
# File 'lib/item_builder.rb', line 102

def vl_ids
  @vl_ids ||= variant_listings.map(&:id).uniq
end

#wh_idObject



210
211
212
# File 'lib/item_builder.rb', line 210

def wh_id
  @wh_id ||= wh_mapping&.warehouse_id
end

#wh_mappingObject



200
201
202
203
204
# File 'lib/item_builder.rb', line 200

def wh_mapping
  wh_mapping ||= WarehouseMapping.where(
    profile_channel_association_id: 
  ).first
end

#zalora_reserved_stockObject



156
157
158
159
160
# File 'lib/item_builder.rb', line 156

def zalora_reserved_stock
  @zalora_reserved_stock ||= ItemBuilder::ZaloraQuantityService.new(
    listings: listings, skus: skus
  ).perform
end

#zilingo_quantityObject



150
151
152
153
154
# File 'lib/item_builder.rb', line 150

def zilingo_quantity
  @zilingo_quantity ||= ItemBuilder::ZilingoQuantityService.new(
    listings: listings, skus: skus
  ).perform
end