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/create/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/create_service.rb,
lib/item_builder/modes/simple_service.rb,
lib/item_builder/modes/update_service.rb,
lib/item_builder/modes/map_create/base.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/modes/map_create_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/create/shopee_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/create/tokopedia_service.rb,
lib/item_builder/modes/precondition_create/base.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,
lib/item_builder/modes/map_create/shopee_service.rb,
lib/item_builder/modes/precondition_create_service.rb,
lib/item_builder/modes/map_create/tokopedia_service.rb,
lib/item_builder/modes/precondition_create/shopee_service.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
'0.1.78'

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.



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

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.



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

def listing_ids
  @listing_ids
end

#listingsObject (readonly)

Returns the value of attribute listings.



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

def listings
  @listings
end

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

#variant_idsObject (readonly)

Returns the value of attribute variant_ids.



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

def variant_ids
  @variant_ids
end

#variantsObject (readonly)

Returns the value of attribute variants.



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

def variants
  @variants
end

#wh_idObject (readonly)

Returns the value of attribute wh_id.



31
32
33
# File 'lib/item_builder.rb', line 31

def wh_id
  @wh_id
end

#wh_mappingObject (readonly)

Returns the value of attribute wh_mapping.



30
31
32
# File 'lib/item_builder.rb', line 30

def wh_mapping
  @wh_mapping
end

#wh_spacesObject (readonly)

Returns the value of attribute wh_spaces.



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

def wh_spaces
  @wh_spaces
end

Class Method Details

.build(listing_ids, mode) ⇒ Object



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

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

Instance Method Details

#account_idObject



226
227
228
# File 'lib/item_builder.rb', line 226

def 
   ||= listings[0].profile_channel_association_id
end

#bundle_idsObject



133
134
135
# File 'lib/item_builder.rb', line 133

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

#bundlesObject



137
138
139
# File 'lib/item_builder.rb', line 137

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

#channel_association_idsObject



179
180
181
# File 'lib/item_builder.rb', line 179

def channel_association_ids
  @listing_item_ids ||= listings.map(&:channel_association_id).uniq
end

#create_modeObject



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

def create_mode
  item_listings.map do |listing|
    param =
      if listing.channel_id == 12
        qty_simple_params(listing)
          .merge(shipping_providers: shipping_providers)
      else
        qty_simple_params(listing)
      end
    modes[mode].new(param).perform
  end
end

#defaultObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/item_builder.rb', line 100

def default
  datas =
    if mode == :precondition_create || mode == :precondition_update
      item_listings
    else
      listings
    end

  datas.map do |listing|
    modes[mode].new(listing: listing).perform
  end
end

#existing_alloc_stocksObject



113
114
115
116
117
# File 'lib/item_builder.rb', line 113

def existing_alloc_stocks
  @existing_alloc_stocks ||= VariantListingStockAllocation.where(
    variant_association_id: vl_ids
  ).where('end_at >= NOW()')
end

#item_bundle_variantsObject



127
128
129
130
131
# File 'lib/item_builder.rb', line 127

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

#item_listingsObject



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

def item_listings
  @item_listings ||= VariantListing.joins(:variant).includes(:item_listing).where(
    id: listing_ids
  ).group(:channel_association_id)
end

#lazada_quantityObject



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

def lazada_quantity
  @lazada_quantity ||= nil
end

#map_create_modeObject



94
95
96
97
98
# File 'lib/item_builder.rb', line 94

def map_create_mode
  listings.map do |listing|
    modes[mode].new(qty_simple_params(listing)).perform
  end
end

#mode_checkObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/item_builder.rb', line 38

def mode_check
  if mode == :quantity || mode == :simple || mode == :active
    quantity_simple_mode
  elsif mode == :create || mode == :item_create || mode == :update
    create_mode
  elsif mode == :map_create
    map_create_mode
  else
    default
  end
end

#modesObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/item_builder.rb', line 205

def modes
  {
    price: ItemBuilder::Modes::PriceService,
    quantity: ItemBuilder::Modes::QuantityService,
    simple: ItemBuilder::Modes::SimpleService,
    active: ItemBuilder::Modes::ActiveService,
    update: ItemBuilder::Modes::CreateService,
    create: ItemBuilder::Modes::CreateService,
    map_create: ItemBuilder::Modes::MapCreateService,
    precondition_create: ItemBuilder::Modes::CreateService,
    precondition_update: ItemBuilder::Modes::CreateService,
    item_create: ItemBuilder::Modes::CreateService,
  }
end

#qty_simple_params(listing) ⇒ Object



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

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, wh_id: wh_id
  }
end

#quantity_simple_modeObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/item_builder.rb', line 50

def quantity_simple_mode
  listings.map do |listing|
    param =
      if listing.channel_id == 18
        qty_simple_params(listing)
          .merge(zilingo_quantity: zilingo_quantity)
      elsif listing.channel_id == 13
        qty_simple_params(listing)
          .merge(zalora_reserved_stock: zalora_reserved_stock)
      elsif listing.channel_id == 2
        qty_simple_params(listing)
          .merge({shopify_inventory_location: shopify_inventory_location})
      elsif listing.channel_id == 3
        qty_simple_params(listing)
          .merge({lazada_quantity: lazada_quantity})
      else
        qty_simple_params(listing)
      end
    modes[mode].new(param).perform
  end.compact
end

#shipping_providersObject



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

def shipping_providers
  @shipping_providers ||= ShopeeShippingProvider.where(
    enabled: true, mask_channel_id: 0, account_id: 
  )
end

#shopify_inventory_locationObject



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

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

#skusObject



175
176
177
# File 'lib/item_builder.rb', line 175

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

#stock_allocsObject



141
142
143
144
145
# File 'lib/item_builder.rb', line 141

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

#variant_listingsObject



123
124
125
# File 'lib/item_builder.rb', line 123

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

#vl_idsObject



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

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

#zalora_reserved_stockObject



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

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

#zilingo_quantityObject



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

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