item_builder_mwh 0.1.19 → 0.1.20

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08f98253d8c700cbb5849d299ce485fc07ec96d5de0df1077e711dff490055b9'
4
- data.tar.gz: 56f60c4b7f0f5356fb22b58eab61b63bed29e603d5ad44155d55cc6b231d077a
3
+ metadata.gz: 2066320137fbc2c20d44ab35a5732415b32c454792cef09dc13ec5feac54c273
4
+ data.tar.gz: 76e567dedbd037d03f39afe5bbc2e0161eba44a8a8884818d2ef418fdabe4310
5
5
  SHA512:
6
- metadata.gz: 1937a3e7a8853bd0a13d554951ba4f32ff8579237ffe05e0c0510a6cbb689bcdae5bd0ff8320ac9d0b9754d043a4b35ca4670accf1e0928289921658e79605f1
7
- data.tar.gz: fafb92c7b40c166bd96265618935e1e8aba6aa47977ee13b0727b29326a5c29044ec286d9f64d9ba5cfbb2e7624b246144f8cbe83bb080cf47b55d1a818053d7
6
+ metadata.gz: f94fe24fe00eaa8a247ec41b5a4776529d2c3861c94a75c8933ef52bc61396b6018548ce08021c19599625f14c71d77c2eacf5df7da10e551f2c16fec617c024
7
+ data.tar.gz: 2234c4b0137ec329818da33822f25740cbeac2fb18f1d4033bc8f9c2025a4faffd61c84e388f878f5460e0f1e93f6d51a8ced950d037a3c294a98872e3e68706
@@ -9,6 +9,7 @@ require 'item_builder_mwh/modes/simple_service'
9
9
  require 'item_models'
10
10
  require 'item_builder_mwh/zilingo_quantity_service'
11
11
  require 'item_builder_mwh/zalora_quantity_service'
12
+ require 'item_builder_mwh/shopify_quantity_service'
12
13
 
13
14
  class ItemBuilderMwh
14
15
  attr_reader :listing_ids
@@ -45,6 +46,11 @@ class ItemBuilderMwh
45
46
  new_param = qty_simple_params(listing)
46
47
  .merge({zalora_reserved_stock: zalora_reserved_stock})
47
48
 
49
+ modes[mode].new(new_param).perform
50
+ elsif listing.channel_id == 2
51
+ new_param = qty_simple_params(listing)
52
+ .merge({shopify_inventory_location: shopify_inventory_location})
53
+
48
54
  modes[mode].new(new_param).perform
49
55
  else
50
56
  modes[mode].new(qty_simple_params(listing)).perform
@@ -138,6 +144,12 @@ class ItemBuilderMwh
138
144
  ).perform
139
145
  end
140
146
 
147
+ def shopify_inventory_location
148
+ @shopify_inventory_location ||= ItemBuilderMwh::ShopifyQuantityService.new(
149
+ listings: listings, skus: skus
150
+ ).perform
151
+ end
152
+
141
153
  def order_host
142
154
  url = ENV['ORDERS_URL'] || 'orders.forstok.com'
143
155
  url + '/api/v3/item_line/reserved_stock'
@@ -12,6 +12,8 @@ class ItemBuilderMwh
12
12
  @bundle_variants = args.fetch(:bundle_variants)
13
13
  @existing_allocated_stock = args.fetch(:existing_allocated_stock)
14
14
  @listing_wh_sp_quantity = args.fetch(:listing_wh_sp_quantity)
15
+ @multiwarehouse = args.fetch(:multiwarehouse)
16
+ @wh_routing = args.fetch(:wh_routing)
15
17
  end
16
18
 
17
19
  def perform
@@ -70,7 +72,16 @@ class ItemBuilderMwh
70
72
 
71
73
  def qty_default
72
74
  # set lower limit for quantity
73
- [@listing_wh_sp_quantity, 0].sort[1]
75
+ if @multiwarehouse && @wh_routing == 1
76
+ [listing_warehouse_routing_quantity, 0].sort[1]
77
+ else
78
+ [@listing_wh_sp_quantity, 0].sort[1]
79
+ end
80
+ end
81
+
82
+ def listing_warehouse_routing_quantity
83
+ warehouse_spaces = WarehouseSpace.where(item_variant_id: variant.id)
84
+ warehouse_spaces.sum(:quantity)
74
85
  end
75
86
 
76
87
  def qty_bundle
@@ -18,6 +18,7 @@ class ItemBuilderMwh
18
18
  attr_reader :reserved_stocks
19
19
  attr_reader :zilingo_quantity
20
20
  attr_reader :zalora_reserved_stock
21
+ attr_reader :shopify_inventory_location
21
22
  def initialize(args)
22
23
  @listing = args.fetch(:listing)
23
24
  @wh_spaces = args.fetch(:wh_spaces, [])
@@ -30,6 +31,7 @@ class ItemBuilderMwh
30
31
  @reserved_stocks = args.fetch(:reserved_stocks, [])
31
32
  @zilingo_quantity = args.fetch(:zilingo_quantity, [])
32
33
  @zalora_reserved_stock = args.fetch(:zalora_reserved_stock, [])
34
+ @shopify_inventory_location = args.fetch(:shopify_inventory_location, [])
33
35
  warehouse
34
36
  end
35
37
 
@@ -73,8 +75,12 @@ class ItemBuilderMwh
73
75
  end
74
76
 
75
77
  def warehouse
76
- warehouse_spaces.each do |warehouse_space|
77
- warehouses << to_h(warehouse_space)
78
+ if multiwarehouse && wh_routing == 1
79
+ warehouses << to_h(warehouse_spaces.first)
80
+ else
81
+ warehouse_spaces.each do |warehouse_space|
82
+ warehouses << to_h(warehouse_space)
83
+ end
78
84
  end
79
85
  end
80
86
 
@@ -104,6 +110,24 @@ class ItemBuilderMwh
104
110
  warehouse_mapping(warehouse_id)[0].channel_warehouse_id
105
111
  end
106
112
 
113
+ def credential
114
+ return @shopify_inventory_location['credential'] if @shopify_inventory_location['credential'].present?
115
+
116
+ nil
117
+ end
118
+
119
+ def multiwarehouse
120
+ return credential['multiwarehouse'] if credential.present?
121
+
122
+ false
123
+ end
124
+
125
+ def wh_routing
126
+ return credential['warehouse_routing'] if credential.present? && credential['warehouse_routing'].present?
127
+
128
+ 0
129
+ end
130
+
107
131
  def warehouses
108
132
  @warehouses ||= []
109
133
  end
@@ -15,14 +15,19 @@ class ItemBuilderMwh
15
15
 
16
16
  QUANTITY_CHANNEL = {}.tap do |hash|
17
17
  hash[3] = :Lazada
18
+ hash[2] = :Shopify
18
19
  hash[13] = :Zalora
19
20
  hash[18] = :Zilingo
20
21
  end.freeze
21
22
 
22
23
  def perform
23
- base.merge!(
24
- warehouse: warehouses
25
- )
24
+ if channel_name == "Shopify"
25
+ base.merge!(shopify_inventory_location[listing.local_id], warehouse: warehouses )
26
+ else
27
+ base.merge!(
28
+ warehouse: warehouses
29
+ )
30
+ end
26
31
  end
27
32
 
28
33
  def to_h(warehouse_space)
@@ -35,7 +40,7 @@ class ItemBuilderMwh
35
40
  end
36
41
 
37
42
  def qty(qty)
38
- if channel_name.empty?
43
+ if channel_name.empty? || channel_name == "Shopify"
39
44
  available_quantity(qty)
40
45
  else
41
46
  qty_channel(qty)
@@ -66,12 +71,31 @@ class ItemBuilderMwh
66
71
  end
67
72
  end
68
73
 
74
+ def credential
75
+ return shopify_inventory_location['credential'] if shopify_inventory_location['credential'].present?
76
+
77
+ nil
78
+ end
79
+
80
+ def multiwarehouse
81
+ return credential['multiwarehouse'] if credential.present?
82
+
83
+ false
84
+ end
85
+
86
+ def wh_routing
87
+ return credential['warehouse_routing'] if credential.present? && credential['warehouse_routing'].present?
88
+
89
+ 0
90
+ end
91
+
69
92
  def available_quantity(qty)
70
93
  ItemBuilderMwh::GetQuantityService.new(
71
94
  listing: listing, stock_alloc: stock_alloc,
72
95
  variant: variant, bundle_variants: bundle_variants,
73
96
  existing_allocated_stock: existing_allocated_stock,
74
- listing_wh_sp_quantity: qty
97
+ listing_wh_sp_quantity: qty, multiwarehouse: multiwarehouse,
98
+ wh_routing: wh_routing
75
99
  ).perform
76
100
  end
77
101
  end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder_mwh/modes.rb'
4
+ class ItemBuilderMwh
5
+ class ShopifyQuantityService
6
+ attr_reader :listings, :skus
7
+ def initialize(args)
8
+ @listings = args.fetch(:listings)
9
+ @skus = args.fetch(:skus)
10
+ @account_id = listings[0].profile_channel_association_id
11
+ @variant_listings = {}
12
+ end
13
+
14
+ def perform
15
+ datas = {}
16
+ @listings.each do |listing|
17
+ @listing = listing
18
+ @sku = listing.local_id
19
+ datas[@sku] = response_process
20
+ end
21
+
22
+ # update variant real_local_id database icava
23
+ VariantListing.update(@variant_listings.keys, @variant_listings.values)
24
+ datas['credential'] = JSON.parse(credential)['credential']
25
+ datas
26
+ end
27
+
28
+ def inventory_item_id
29
+ real_local_id = @listing.real_local_id
30
+ listing_id = @listing.id
31
+
32
+ if real_local_id.nil? || real_local_id.blank?
33
+ # get data from shopify
34
+ args = {
35
+ method: :post, url: url_variant, payload: data_variant, headers: headers
36
+ }
37
+
38
+ resp = JSON.parse(rest_client(args, [200, 500, 406]))
39
+
40
+ hash = { listing_id => { "real_local_id" => resp['inventory_item_id'] } }
41
+ @variant_listings = @variant_listings.merge(hash)
42
+
43
+ resp['inventory_item_id']
44
+ else
45
+ # real_local_id exists
46
+ real_local_id
47
+ end
48
+ end
49
+
50
+ def location_id
51
+ cred = JSON.parse(credential)['credential']
52
+ if cred['primary_warehouse_id'].present?
53
+ cred['primary_warehouse_id']
54
+ else
55
+ # get data from shopify
56
+ args = {
57
+ method: :post, url: url_location, payload: data_location, headers: headers
58
+ }
59
+
60
+ resp = JSON.parse(rest_client(args, [200, 500, 406]))
61
+ resp['id']
62
+ end
63
+ end
64
+
65
+ def headers
66
+ { content_type: :json, accept: :json }
67
+ end
68
+
69
+ def data_location
70
+ {
71
+ "credential": JSON.parse(credential)['credential']
72
+ }.to_json
73
+ end
74
+
75
+ def data_variant
76
+ {
77
+ "credential": JSON.parse(credential)['credential'],
78
+ "data": { "local_id": @sku }
79
+ }.to_json
80
+ end
81
+
82
+ def credential
83
+ return @credential if @credential
84
+
85
+ host = ENV['CREDENTIAL_URL'] || raise('credential url is not set')
86
+ url = "#{host}/credential?account_id=#{@account_id}"
87
+ @credential = rest_client(method: :get, url: url)
88
+ end
89
+
90
+ def url_variant
91
+ url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
92
+ url + "/shopify/data_variant"
93
+ end
94
+
95
+ def url_location
96
+ url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
97
+ url + "/shopify/data_location"
98
+ end
99
+
100
+ def response_process
101
+ hash = Hash.new
102
+
103
+ hash[:inventory_item_id] = inventory_item_id.to_i
104
+ hash[:location_id] = location_id.to_i
105
+
106
+ hash
107
+ end
108
+
109
+ def rest_client(params, rescued_codes = 200)
110
+ RestClient::Request.execute(params.merge(timeout: 3)) do |response|
111
+ code = response.code
112
+ unless Array.wrap(rescued_codes).include?(code)
113
+ raise "Response Code is #{code}"
114
+ end
115
+
116
+ response
117
+ end
118
+ end
119
+ end
120
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ItemBuilderMwh
4
- VERSION = '0.1.19'
4
+ VERSION = '0.1.20'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: item_builder_mwh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Ivander
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-14 00:00:00.000000000 Z
11
+ date: 2021-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -143,6 +143,7 @@ files:
143
143
  - lib/item_builder_mwh/modes/simple/shopify_service.rb
144
144
  - lib/item_builder_mwh/modes/simple/zalora_service.rb
145
145
  - lib/item_builder_mwh/modes/simple_service.rb
146
+ - lib/item_builder_mwh/shopify_quantity_service.rb
146
147
  - lib/item_builder_mwh/version.rb
147
148
  - lib/item_builder_mwh/zalora_quantity_service.rb
148
149
  - lib/item_builder_mwh/zilingo_quantity_service.rb