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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9267900d48fc70b5b7c41943cda2dde2b7a1c5732c26085bf2411e0683fe677a
4
- data.tar.gz: bd40a5dfc2a2f9dfbc4278e59bdd795e8b375666e466faf641b9235fccbcfba3
3
+ metadata.gz: d27673689edf403c7602e972c9b68dea4a3f22c0122c0a9badcb2deafe18b6df
4
+ data.tar.gz: d93a48b95ca90754696afe36673bb8932e1d61d92d9bae41994cebeb15b67b56
5
5
  SHA512:
6
- metadata.gz: 9beefb0a4650bf6bdf4d5ff7613c220b503764091395573de87f09ad2db2208ff5e6cc329384c36ae76252ad44cfc88fb86929d78b17b96dcf525491e0663f49
7
- data.tar.gz: 9303229b16cb1fee06661b10103e85602eb465b56d25a0336cc5663011013ac0e7e4f5da6b8f32b4cba6fd2144fbbd371b124a4bf9876b7f55692f5f17f303ad
6
+ metadata.gz: 87ca59a67d0cf7351aeb460d53e29e31c79f5c026ef84e866bdff0f3f73719a57ee32690c8886f20dc2cbf8a7cfed53e2eb3da57b147b24c049cf38eadd24b95
7
+ data.tar.gz: 4db562d9bad83182d25e4fcbf3b7caaf64683841a3053a6f3576fc512380658a3398cc2afa8a9843da9bc0aef5fc882fd42145b297c7edc52e4c29a633eb5e04
@@ -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({zilingo_delta_quantity: zilingo_delta_quantity})
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
- modes[mode].new(listing: listing).perform
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 zilingo_delta_quantity
122
- @zilingo_delta_quantity ||= ItemBuilderMwh::ZilingoQuantityService.new(
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
- [@listing_wh_sp_quantity, 0].sort[1]
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 :zilingo_delta_quantity
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
- @zilingo_delta_quantity = args.fetch(:zilingo_delta_quantity, [])
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
@@ -6,14 +6,14 @@ class ItemBuilderMwh
6
6
  class Base
7
7
  attr_reader :listing
8
8
  attr_reader :available_quantity
9
- attr_reader :reserved_stock
9
+ attr_reader :local_qty
10
10
 
11
- def initialize(listing, available_quantity, reserved_stock)
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
- @reserved_stock = reserved_stock
16
+ @local_qty = local_qty
17
17
  end
18
18
  end
19
19
  end
@@ -6,7 +6,7 @@ class ItemBuilderMwh
6
6
  module Quantity
7
7
  class LazadaService < Base
8
8
  def perform
9
- available_quantity + reserved_stock.to_i
9
+ available_quantity + local_qty
10
10
  end
11
11
  end
12
12
  end
@@ -6,7 +6,7 @@ class ItemBuilderMwh
6
6
  module Quantity
7
7
  class ZaloraService < Base
8
8
  def perform
9
- available_quantity + reserved_stock.to_i
9
+ available_quantity + local_qty
10
10
  end
11
11
  end
12
12
  end
@@ -6,7 +6,7 @@ class ItemBuilderMwh
6
6
  module Quantity
7
7
  class ZilingoService < Base
8
8
  def perform
9
- available_quantity - reserved_stock.to_i
9
+ available_quantity - local_qty
10
10
  end
11
11
  end
12
12
  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
- 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)
29
- {
30
- quantity: [qty(warehouse_space.quantity), 0].sort[1],
31
- warehouse_id: wh_mapping(
32
- warehouse_space.warehouse_id
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), reserved_stock).perform
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 reserved_stock
69
+ def local_qty
56
70
  if channel_name == 'Zilingo'
57
- zilingo_delta_quantity[listing.local_id].to_i
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), reserved_stock).perform
91
+ qty_channel_service.new(listing, available_quantity(qty), local_qty.to_i).perform
92
92
  end
93
93
 
94
- def reserved_stock
95
- reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ItemBuilderMwh
4
- VERSION = '0.1.16'
4
+ VERSION = '0.1.21'
5
5
  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
- end
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.16
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-04-27 00:00:00.000000000 Z
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