item_builder_mwh 0.1.16 → 0.1.21
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/item_builder_mwh.rb +24 -4
- data/lib/item_builder_mwh/get_quantity_service.rb +12 -2
- data/lib/item_builder_mwh/modes.rb +28 -4
- data/lib/item_builder_mwh/modes/quantity/base.rb +3 -3
- data/lib/item_builder_mwh/modes/quantity/lazada_service.rb +1 -1
- data/lib/item_builder_mwh/modes/quantity/zalora_service.rb +1 -1
- data/lib/item_builder_mwh/modes/quantity/zilingo_service.rb +1 -1
- data/lib/item_builder_mwh/modes/quantity_service.rb +44 -14
- data/lib/item_builder_mwh/modes/simple_service.rb +13 -3
- data/lib/item_builder_mwh/shopify_quantity_service.rb +120 -0
- data/lib/item_builder_mwh/version.rb +1 -1
- data/lib/item_builder_mwh/zalora_quantity_service.rb +7 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d27673689edf403c7602e972c9b68dea4a3f22c0122c0a9badcb2deafe18b6df
|
4
|
+
data.tar.gz: d93a48b95ca90754696afe36673bb8932e1d61d92d9bae41994cebeb15b67b56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87ca59a67d0cf7351aeb460d53e29e31c79f5c026ef84e866bdff0f3f73719a57ee32690c8886f20dc2cbf8a7cfed53e2eb3da57b147b24c049cf38eadd24b95
|
7
|
+
data.tar.gz: 4db562d9bad83182d25e4fcbf3b7caaf64683841a3053a6f3576fc512380658a3398cc2afa8a9843da9bc0aef5fc882fd42145b297c7edc52e4c29a633eb5e04
|
data/lib/item_builder_mwh.rb
CHANGED
@@ -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
|
@@ -38,13 +39,18 @@ class ItemBuilderMwh
|
|
38
39
|
listings.map do |listing|
|
39
40
|
if listing.channel_id == 18
|
40
41
|
new_param = qty_simple_params(listing)
|
41
|
-
.merge({
|
42
|
+
.merge({zilingo_quantity: zilingo_quantity})
|
42
43
|
|
43
44
|
modes[mode].new(new_param).perform
|
44
45
|
elsif listing.channel_id == 13
|
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
|
@@ -54,7 +60,15 @@ class ItemBuilderMwh
|
|
54
60
|
|
55
61
|
def default
|
56
62
|
listings.map do |listing|
|
57
|
-
|
63
|
+
if listing.channel_id == 2 && mode == :active
|
64
|
+
modes[mode].new(qty_simple_params(listing)).perform
|
65
|
+
elsif listing.channel_id == 18 && mode == :active
|
66
|
+
modes[mode].new(qty_simple_params(listing)
|
67
|
+
.merge(zilingo_quantity: zilingo_quantity)
|
68
|
+
).perform
|
69
|
+
else
|
70
|
+
modes[mode].new(listing: listing).perform
|
71
|
+
end
|
58
72
|
end
|
59
73
|
end
|
60
74
|
|
@@ -118,8 +132,8 @@ class ItemBuilderMwh
|
|
118
132
|
@skus ||= listings.map(&:local_id).uniq
|
119
133
|
end
|
120
134
|
|
121
|
-
def
|
122
|
-
@
|
135
|
+
def zilingo_quantity
|
136
|
+
@zilingo_quantity ||= ItemBuilderMwh::ZilingoQuantityService.new(
|
123
137
|
listings: listings, skus: skus
|
124
138
|
).perform
|
125
139
|
end
|
@@ -130,6 +144,12 @@ class ItemBuilderMwh
|
|
130
144
|
).perform
|
131
145
|
end
|
132
146
|
|
147
|
+
def shopify_inventory_location
|
148
|
+
@shopify_inventory_location ||= ItemBuilderMwh::ShopifyQuantityService.new(
|
149
|
+
listings: listings, skus: skus
|
150
|
+
).perform
|
151
|
+
end
|
152
|
+
|
133
153
|
def order_host
|
134
154
|
url = ENV['ORDERS_URL'] || 'orders.forstok.com'
|
135
155
|
url + '/api/v3/item_line/reserved_stock'
|
@@ -12,6 +12,7 @@ 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
|
+
@wh_routing = args.fetch(:wh_routing, 0)
|
15
16
|
end
|
16
17
|
|
17
18
|
def perform
|
@@ -38,7 +39,7 @@ class ItemBuilderMwh
|
|
38
39
|
def check_consignment_variant?
|
39
40
|
listing.consignment? ||
|
40
41
|
(
|
41
|
-
!listing.active? && [11, 12, 15, 19].include?(listing.channel_id)
|
42
|
+
!listing.active? && [11, 12, 15, 19, 2, 18].include?(listing.channel_id)
|
42
43
|
)
|
43
44
|
end
|
44
45
|
|
@@ -70,7 +71,16 @@ class ItemBuilderMwh
|
|
70
71
|
|
71
72
|
def qty_default
|
72
73
|
# set lower limit for quantity
|
73
|
-
|
74
|
+
if @wh_routing == 1
|
75
|
+
[listing_warehouse_routing_quantity, 0].sort[1]
|
76
|
+
else
|
77
|
+
[@listing_wh_sp_quantity, 0].sort[1]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def listing_warehouse_routing_quantity
|
82
|
+
warehouse_spaces = WarehouseSpace.where(item_variant_id: variant.id)
|
83
|
+
warehouse_spaces.sum(:quantity)
|
74
84
|
end
|
75
85
|
|
76
86
|
def qty_bundle
|
@@ -16,8 +16,9 @@ class ItemBuilderMwh
|
|
16
16
|
attr_reader :existing_alloc_stocks
|
17
17
|
attr_reader :variant_listings
|
18
18
|
attr_reader :reserved_stocks
|
19
|
-
attr_reader :
|
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, [])
|
@@ -28,8 +29,9 @@ class ItemBuilderMwh
|
|
28
29
|
@existing_alloc_stocks = args.fetch(:existing_alloc_stocks, [])
|
29
30
|
@variant_listings = args.fetch(:variant_listings, [])
|
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
|
-
|
77
|
-
warehouses << to_h(
|
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
|
@@ -6,14 +6,14 @@ class ItemBuilderMwh
|
|
6
6
|
class Base
|
7
7
|
attr_reader :listing
|
8
8
|
attr_reader :available_quantity
|
9
|
-
attr_reader :
|
9
|
+
attr_reader :local_qty
|
10
10
|
|
11
|
-
def initialize(listing, available_quantity,
|
11
|
+
def initialize(listing, available_quantity, local_qty)
|
12
12
|
raise 'listing is not set' if listing.nil?
|
13
13
|
|
14
14
|
@listing = listing
|
15
15
|
@available_quantity = available_quantity
|
16
|
-
@
|
16
|
+
@local_qty = local_qty
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
@@ -15,27 +15,41 @@ 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
|
-
|
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)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
34
|
+
if channel_name == "Zilingo"
|
35
|
+
{
|
36
|
+
quantity: qty(warehouse_space.quantity),
|
37
|
+
warehouse_id: wh_mapping(
|
38
|
+
warehouse_space.warehouse_id
|
39
|
+
)
|
40
|
+
}
|
41
|
+
else
|
42
|
+
{
|
43
|
+
quantity: [qty(warehouse_space.quantity), 0].sort[1],
|
44
|
+
warehouse_id: wh_mapping(
|
45
|
+
warehouse_space.warehouse_id
|
46
|
+
)
|
47
|
+
}
|
48
|
+
end
|
35
49
|
end
|
36
50
|
|
37
51
|
def qty(qty)
|
38
|
-
if channel_name.empty?
|
52
|
+
if channel_name.empty? || channel_name == "Shopify"
|
39
53
|
available_quantity(qty)
|
40
54
|
else
|
41
55
|
qty_channel(qty)
|
@@ -45,29 +59,45 @@ class ItemBuilderMwh
|
|
45
59
|
def qty_channel(qty)
|
46
60
|
class_name = "ItemBuilderMwh::Modes::Quantity::#{channel_name}Service"
|
47
61
|
qty_channel_service = class_name.constantize
|
48
|
-
qty_channel_service.new(listing, available_quantity(qty),
|
62
|
+
qty_channel_service.new(listing, available_quantity(qty), local_qty.to_i).perform
|
49
63
|
end
|
50
64
|
|
51
65
|
def channel_name
|
52
66
|
QUANTITY_CHANNEL[listing.channel_id].to_s
|
53
67
|
end
|
54
68
|
|
55
|
-
def
|
69
|
+
def local_qty
|
56
70
|
if channel_name == 'Zilingo'
|
57
|
-
|
71
|
+
return 0 if zilingo_quantity.blank?
|
72
|
+
|
73
|
+
zilingo_quantity[listing.local_id].to_i
|
58
74
|
elsif channel_name == 'Zalora'
|
75
|
+
return 0 if zalora_reserved_stock.blank?
|
76
|
+
|
59
77
|
zalora_reserved_stock[listing.local_id].to_i
|
60
78
|
else
|
61
79
|
reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
|
62
80
|
end
|
63
81
|
end
|
64
82
|
|
83
|
+
def credential
|
84
|
+
return shopify_inventory_location['credential'] if shopify_inventory_location['credential'].present?
|
85
|
+
|
86
|
+
nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def wh_routing
|
90
|
+
return credential['warehouse_routing'] if credential.present? && credential['warehouse_routing'].present?
|
91
|
+
|
92
|
+
0
|
93
|
+
end
|
94
|
+
|
65
95
|
def available_quantity(qty)
|
66
96
|
ItemBuilderMwh::GetQuantityService.new(
|
67
97
|
listing: listing, stock_alloc: stock_alloc,
|
68
98
|
variant: variant, bundle_variants: bundle_variants,
|
69
99
|
existing_allocated_stock: existing_allocated_stock,
|
70
|
-
listing_wh_sp_quantity: qty
|
100
|
+
listing_wh_sp_quantity: qty, wh_routing: wh_routing
|
71
101
|
).perform
|
72
102
|
end
|
73
103
|
end
|
@@ -88,11 +88,21 @@ class ItemBuilderMwh
|
|
88
88
|
def qty_channel(qty)
|
89
89
|
class_name = "ItemBuilderMwh::Modes::Quantity::#{quantity_name}Service"
|
90
90
|
qty_channel_service = class_name.constantize
|
91
|
-
qty_channel_service.new(listing, available_quantity(qty),
|
91
|
+
qty_channel_service.new(listing, available_quantity(qty), local_qty.to_i).perform
|
92
92
|
end
|
93
93
|
|
94
|
-
def
|
95
|
-
|
94
|
+
def local_qty
|
95
|
+
if channel_name == 'Zilingo'
|
96
|
+
return 0 if zilingo_quantity.blank?
|
97
|
+
|
98
|
+
zilingo_quantity[listing.local_id].to_i
|
99
|
+
elsif channel_name == 'Zalora'
|
100
|
+
return 0 if zalora_reserved_stock.blank?
|
101
|
+
|
102
|
+
zalora_reserved_stock[listing.local_id].to_i
|
103
|
+
else
|
104
|
+
reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
|
105
|
+
end
|
96
106
|
end
|
97
107
|
|
98
108
|
def available_quantity(qty)
|
@@ -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
|
@@ -45,8 +45,13 @@ class ItemBuilderMwh
|
|
45
45
|
def response_process(resp)
|
46
46
|
if resp.dig("response_code") && resp.dig("response_code") != 200
|
47
47
|
raise "Response Code is #{resp.dig("response_code")}"
|
48
|
-
|
49
|
-
|
48
|
+
elsif resp.dig('ErrorResponse', 'Head', 'ErrorMessage') == 'E009: Access Denied'
|
49
|
+
return nil
|
50
|
+
end
|
51
|
+
success_handle(resp)
|
52
|
+
end
|
53
|
+
|
54
|
+
def success_handle(resp)
|
50
55
|
hash = Hash.new
|
51
56
|
resp.dig("SuccessResponse", "Body", "ProductStocks", "ProductStock").each do |sku|
|
52
57
|
hash[sku['SellerSku']] = sku['ReservedStock']
|
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.
|
4
|
+
version: 0.1.21
|
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-
|
11
|
+
date: 2021-07-06 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
|