item_builder_mwh 0.1.15 → 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: 56fc0a284bc21a40a3f253bdc3a176f306df5c0766f7ba3cc298ab92665e1dd7
4
- data.tar.gz: 9050d240f828a6aa8bd577b3a3696024ed0ed72f75174d758a3db92afcd78756
3
+ metadata.gz: 2066320137fbc2c20d44ab35a5732415b32c454792cef09dc13ec5feac54c273
4
+ data.tar.gz: 76e567dedbd037d03f39afe5bbc2e0161eba44a8a8884818d2ef418fdabe4310
5
5
  SHA512:
6
- metadata.gz: 49fb0ffbacca4075e9042dc5e92eadc66c47836734174df6f384e35e2cc7efae3f5834ccc5aae5e4caf5a7482b68fe5d86c4418b3048c4de407203e92271e522
7
- data.tar.gz: b51b059793226e0de15dcc696a2266e11a0ea704480dd5227c0a7641f7a173064bc4ed4911cc65e678fa43984b58a26e0e7169752b4a1570014d46e78d5952b4
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
@@ -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,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
@@ -38,7 +40,7 @@ class ItemBuilderMwh
38
40
  def check_consignment_variant?
39
41
  listing.consignment? ||
40
42
  (
41
- !listing.active? && [11, 12, 15].include?(listing.channel_id)
43
+ !listing.active? && [11, 12, 15, 19, 2, 18].include?(listing.channel_id)
42
44
  )
43
45
  end
44
46
 
@@ -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
@@ -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,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)
@@ -45,29 +50,52 @@ class ItemBuilderMwh
45
50
  def qty_channel(qty)
46
51
  class_name = "ItemBuilderMwh::Modes::Quantity::#{channel_name}Service"
47
52
  qty_channel_service = class_name.constantize
48
- qty_channel_service.new(listing, available_quantity(qty), reserved_stock).perform
53
+ qty_channel_service.new(listing, available_quantity(qty), local_qty.to_i).perform
49
54
  end
50
55
 
51
56
  def channel_name
52
57
  QUANTITY_CHANNEL[listing.channel_id].to_s
53
58
  end
54
59
 
55
- def reserved_stock
60
+ def local_qty
56
61
  if channel_name == 'Zilingo'
57
- zilingo_delta_quantity[listing.local_id].to_i
62
+ return 0 if zilingo_quantity.blank?
63
+
64
+ zilingo_quantity[listing.local_id].to_i
58
65
  elsif channel_name == 'Zalora'
66
+ return 0 if zalora_reserved_stock.blank?
67
+
59
68
  zalora_reserved_stock[listing.local_id].to_i
60
69
  else
61
70
  reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
62
71
  end
63
72
  end
64
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
+
65
92
  def available_quantity(qty)
66
93
  ItemBuilderMwh::GetQuantityService.new(
67
94
  listing: listing, stock_alloc: stock_alloc,
68
95
  variant: variant, bundle_variants: bundle_variants,
69
96
  existing_allocated_stock: existing_allocated_stock,
70
- listing_wh_sp_quantity: qty
97
+ listing_wh_sp_quantity: qty, multiwarehouse: multiwarehouse,
98
+ wh_routing: wh_routing
71
99
  ).perform
72
100
  end
73
101
  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.15'
4
+ VERSION = '0.1.20'
5
5
  end
@@ -43,6 +43,15 @@ class ItemBuilderMwh
43
43
  end
44
44
 
45
45
  def response_process(resp)
46
+ if resp.dig("response_code") && resp.dig("response_code") != 200
47
+ raise "Response Code is #{resp.dig("response_code")}"
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)
46
55
  hash = Hash.new
47
56
  resp.dig("SuccessResponse", "Body", "ProductStocks", "ProductStock").each do |sku|
48
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.15
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-04-12 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