item_builder_mwh 0.1.9 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/item_builder_mwh.rb +133 -13
- data/lib/item_builder_mwh/get_quantity_service.rb +132 -0
- data/lib/item_builder_mwh/modes.rb +49 -1
- data/lib/item_builder_mwh/modes/active_service.rb +1 -1
- data/lib/item_builder_mwh/modes/base_service.rb +1 -1
- data/lib/item_builder_mwh/modes/price/base.rb +1 -1
- data/lib/item_builder_mwh/modes/price/blibli_service.rb +1 -1
- data/lib/item_builder_mwh/modes/price/bukalapak_service.rb +1 -1
- data/lib/item_builder_mwh/modes/price/jd_service.rb +1 -1
- data/lib/item_builder_mwh/modes/price/sale_price_policy.rb +6 -2
- data/lib/item_builder_mwh/modes/price/shopify_service.rb +1 -1
- data/lib/item_builder_mwh/modes/price/zalora_service.rb +1 -1
- data/lib/item_builder_mwh/modes/price_service.rb +1 -1
- data/lib/item_builder_mwh/modes/quantity/base.rb +4 -48
- 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 +14 -0
- data/lib/item_builder_mwh/modes/quantity_service.rb +28 -9
- data/lib/item_builder_mwh/modes/simple/base.rb +1 -1
- data/lib/item_builder_mwh/modes/simple/blibli_service.rb +1 -1
- data/lib/item_builder_mwh/modes/simple/bukalapak_service.rb +1 -1
- data/lib/item_builder_mwh/modes/simple/jd_service.rb +1 -1
- data/lib/item_builder_mwh/modes/simple/lazada_service.rb +1 -1
- data/lib/item_builder_mwh/modes/simple/sale_price_policy.rb +1 -1
- data/lib/item_builder_mwh/modes/simple/shopee_service.rb +1 -1
- data/lib/item_builder_mwh/modes/simple/shopify_service.rb +1 -1
- data/lib/item_builder_mwh/modes/simple/zalora_service.rb +1 -1
- data/lib/item_builder_mwh/modes/simple_service.rb +22 -11
- data/lib/item_builder_mwh/version.rb +2 -2
- data/lib/item_builder_mwh/zilingo_quantity_service.rb +64 -0
- metadata +6 -4
- data/lib/item_builder_mwh/modes/quantity/blibli_service.rb +0 -51
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a9f73afc814a44d6e54abb4e1b2fa988ed233ec7e6858be9aa964dc1e98c124
|
4
|
+
data.tar.gz: 7faa7a9622d86db9130acbcaa14febed1f35cf2f93e0603c832b87c92f9e6e4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d78506e0099bb9e9326ef25ddc00db9d482728777d15e8cb37f750fb18d031767942e5233f23c28a236a9cfa2c92531b34abd3a08b5e7b69c9812b0ad72cc2b
|
7
|
+
data.tar.gz: 02d0fc58a57ca7fb34429c59bb651f582412d8cb2abf70aa0de5b85e6fd11b6fbfe3e4410f897f46dae7989425f2bb03c12828194f5d754f073b2e0fc1df0de3
|
data/lib/item_builder_mwh.rb
CHANGED
@@ -7,23 +7,143 @@ require 'item_builder_mwh/modes/price_service'
|
|
7
7
|
require 'item_builder_mwh/modes/quantity_service'
|
8
8
|
require 'item_builder_mwh/modes/simple_service'
|
9
9
|
require 'item_models'
|
10
|
+
require 'item_builder_mwh/zilingo_quantity_service'
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
class ItemBuilderMwh
|
13
|
+
attr_reader :listing_ids
|
14
|
+
attr_reader :listings
|
15
|
+
attr_reader :mode
|
16
|
+
attr_reader :wh_spaces
|
17
|
+
attr_reader :variants
|
18
|
+
attr_reader :variant_ids
|
19
|
+
def initialize(listing_ids, mode)
|
20
|
+
@listing_ids = listing_ids
|
21
|
+
@mode = mode
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.build(listing_ids, mode)
|
25
|
+
new(listing_ids, mode).mode_check
|
26
|
+
end
|
27
|
+
|
28
|
+
def mode_check
|
29
|
+
if mode == :quantity || mode == :simple
|
30
|
+
quantity_simple_mode
|
31
|
+
else
|
32
|
+
default
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def quantity_simple_mode
|
37
|
+
listings.map do |listing|
|
38
|
+
if listing.channel_id == 18
|
39
|
+
new_param = qty_simple_params(listing)
|
40
|
+
.merge({zilingo_delta_quantity: zilingo_delta_quantity})
|
41
|
+
|
42
|
+
modes[mode].new(new_param).perform
|
43
|
+
else
|
44
|
+
modes[mode].new(qty_simple_params(listing)).perform
|
17
45
|
end
|
18
46
|
end
|
47
|
+
end
|
19
48
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
quantity: ItemBuilderMwh::Modes::QuantityService,
|
24
|
-
simple: ItemBuilderMwh::Modes::SimpleService,
|
25
|
-
active: ItemBuilderMwh::Modes::ActiveService
|
26
|
-
}
|
49
|
+
def default
|
50
|
+
listings.map do |listing|
|
51
|
+
modes[mode].new(listing: listing).perform
|
27
52
|
end
|
28
53
|
end
|
54
|
+
|
55
|
+
def qty_simple_params(listing)
|
56
|
+
{
|
57
|
+
listing: listing, wh_spaces: wh_spaces, variants: variants,
|
58
|
+
stock_allocs: stock_allocs, variant_listings: variant_listings,
|
59
|
+
bundles: bundles, item_bundle_variants: item_bundle_variants,
|
60
|
+
existing_alloc_stocks: existing_alloc_stocks,
|
61
|
+
reserved_stocks: reserved_stocks
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
def existing_alloc_stocks
|
66
|
+
@existing_alloc_stocks ||= VariantListingStockAllocation.where(
|
67
|
+
variant_association_id: vl_ids
|
68
|
+
).where('ADDTIME(end_at, "07:00") >= NOW()')
|
69
|
+
end
|
70
|
+
|
71
|
+
def vl_ids
|
72
|
+
@vl_ids ||= variant_listings.map(&:id).uniq
|
73
|
+
end
|
74
|
+
|
75
|
+
def variant_listings
|
76
|
+
@variant_listings ||= VariantListing.where(variant_id: variant_ids)
|
77
|
+
end
|
78
|
+
|
79
|
+
def item_bundle_variants
|
80
|
+
@item_bundle_variants ||= BundleVariant.where(
|
81
|
+
bundle_id: bundle_ids
|
82
|
+
)
|
83
|
+
end
|
84
|
+
|
85
|
+
def bundle_ids
|
86
|
+
@bundle_ids ||= bundles.map(&:id).uniq
|
87
|
+
end
|
88
|
+
|
89
|
+
def bundles
|
90
|
+
@bundles ||= Bundle.where(variant_id: variant_ids).group(:variant_id)
|
91
|
+
end
|
92
|
+
|
93
|
+
def stock_allocs
|
94
|
+
@stock_allocs ||= VariantListingStockAllocation.where(
|
95
|
+
variant_association_id: vl_ids
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
def wh_spaces
|
100
|
+
@wh_spaces ||= WarehouseSpace.where(item_variant_id: variant_ids)
|
101
|
+
end
|
102
|
+
|
103
|
+
def variants
|
104
|
+
@variants ||= Variant.where(id: variant_ids)
|
105
|
+
end
|
106
|
+
|
107
|
+
def variant_ids
|
108
|
+
@variant_ids ||= listings.map(&:variant_id).uniq
|
109
|
+
end
|
110
|
+
|
111
|
+
def skus
|
112
|
+
@skus ||= listings.map(&:local_id).uniq
|
113
|
+
end
|
114
|
+
|
115
|
+
def zilingo_delta_quantity
|
116
|
+
@zilingo_delta_quantity ||= ItemBuilderMwh::ZilingoQuantityService.new(
|
117
|
+
listings: listings, skus: skus
|
118
|
+
).perform
|
119
|
+
end
|
120
|
+
|
121
|
+
def order_host
|
122
|
+
url = ENV['ORDERS_URL'] || 'orders.forstok.com'
|
123
|
+
url + '/api/v3/item_line/reserved_stock'
|
124
|
+
end
|
125
|
+
|
126
|
+
def reserved_params
|
127
|
+
"account_id=#{listings[0].profile_channel_association_id}
|
128
|
+
&item_variant_ids=#{variant_ids.join(',')}"
|
129
|
+
end
|
130
|
+
|
131
|
+
def reserved_stocks
|
132
|
+
@reserved_stocks ||= JSON.parse(RestClient.get(
|
133
|
+
"#{order_host}?#{reserved_params}"
|
134
|
+
).body) if [3,13].include?(listings[0].channel_id)
|
135
|
+
end
|
136
|
+
|
137
|
+
def modes
|
138
|
+
{
|
139
|
+
price: ItemBuilderMwh::Modes::PriceService,
|
140
|
+
quantity: ItemBuilderMwh::Modes::QuantityService,
|
141
|
+
simple: ItemBuilderMwh::Modes::SimpleService,
|
142
|
+
active: ItemBuilderMwh::Modes::ActiveService
|
143
|
+
}
|
144
|
+
end
|
145
|
+
|
146
|
+
def listings
|
147
|
+
@listings ||= VariantListing.where(id: listing_ids)
|
148
|
+
end
|
29
149
|
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'warehouse_models'
|
4
|
+
require 'item_builder_mwh/modes.rb'
|
5
|
+
class ItemBuilderMwh
|
6
|
+
class GetQuantityService
|
7
|
+
attr_reader :listing, :variant
|
8
|
+
def initialize(args)
|
9
|
+
@listing = args.fetch(:listing)
|
10
|
+
@variant = args.fetch(:variant)
|
11
|
+
@stock_alloc = args.fetch(:stock_alloc)
|
12
|
+
@bundle_variants = args.fetch(:bundle_variants)
|
13
|
+
@existing_allocated_stock = args.fetch(:existing_allocated_stock)
|
14
|
+
@listing_wh_sp_quantity = args.fetch(:listing_wh_sp_quantity)
|
15
|
+
end
|
16
|
+
|
17
|
+
def perform
|
18
|
+
return 0 if check_consignment_variant?
|
19
|
+
|
20
|
+
if allocated_stock_active?
|
21
|
+
# yang masuk di kondisi ini,
|
22
|
+
# artinya akun tersebut ada allocated stock yang aktif
|
23
|
+
allocated_stock
|
24
|
+
elsif @existing_allocated_stock.present?
|
25
|
+
# yang masuk di kondisi ini,
|
26
|
+
# artinya akun tersebut tidak ada allocated stock yang aktif,
|
27
|
+
# namun ada allocated stock yg aktif dari channel lain
|
28
|
+
warehouse_stock - count_existing_alloc_stock + count_alloc_rsvd_stock
|
29
|
+
else
|
30
|
+
# yang masuk di kondisi ini,
|
31
|
+
# artinya tidak ada allocated stock di item tersebut
|
32
|
+
warehouse_stock
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def check_consignment_variant?
|
39
|
+
listing.consignment? ||
|
40
|
+
(
|
41
|
+
!listing.active? && [11, 12, 15].include?(listing.channel_id)
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
def allocated_stock_active?
|
46
|
+
listing.present? && @stock_alloc.present? && allocated_start_end?
|
47
|
+
end
|
48
|
+
|
49
|
+
def allocated_start_end?
|
50
|
+
@stock_alloc.start_at.beginning_of_day <= Time.now &&
|
51
|
+
@stock_alloc.end_at.end_of_day >= Time.now
|
52
|
+
end
|
53
|
+
|
54
|
+
def allocated_stock
|
55
|
+
@stock_alloc.quantity.to_i - one_alloc_rsvd_stock(@stock_alloc).to_i
|
56
|
+
end
|
57
|
+
|
58
|
+
# warehouse_stock fungsi untuk mendapatkan available quantity
|
59
|
+
# baik itu bundle atau default
|
60
|
+
def warehouse_stock
|
61
|
+
case variant.config.downcase
|
62
|
+
when 'default'
|
63
|
+
qty_default
|
64
|
+
when 'free gift', 'combo'
|
65
|
+
qty_bundle
|
66
|
+
else
|
67
|
+
raise "config not recognize => #{variant.config}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def qty_default
|
72
|
+
# set lower limit for quantity
|
73
|
+
[@listing_wh_sp_quantity, 0].sort[1]
|
74
|
+
end
|
75
|
+
|
76
|
+
def qty_bundle
|
77
|
+
# Quantity for bundle config
|
78
|
+
if @bundle_variants.present?
|
79
|
+
qty_list = []
|
80
|
+
@bundle_variants.each do |bvr|
|
81
|
+
warehouse_space = wh_space.select {|ws| ws.item_variant_id == bvr.variant_id }.first
|
82
|
+
qty = warehouse_space.quantity if warehouse_space.present?
|
83
|
+
qty ||= 0
|
84
|
+
qty = qty / bvr.unit
|
85
|
+
qty_list.push(qty)
|
86
|
+
end
|
87
|
+
[qty_list.min, 0].sort[1]
|
88
|
+
else
|
89
|
+
qty_default
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def wh_space
|
94
|
+
@wh_space ||= WarehouseSpace.where(item_variant_id: variant_ids)
|
95
|
+
end
|
96
|
+
|
97
|
+
def variant_ids
|
98
|
+
@variant_ids ||= @bundle_variants.map(&:variant_id).uniq
|
99
|
+
end
|
100
|
+
|
101
|
+
def count_existing_alloc_stock
|
102
|
+
return 0 if @existing_allocated_stock.blank?
|
103
|
+
|
104
|
+
@existing_allocated_stock.sum(&:quantity)
|
105
|
+
end
|
106
|
+
|
107
|
+
def count_alloc_rsvd_stock
|
108
|
+
stock = 0
|
109
|
+
@existing_allocated_stock.each do |allocated_stock|
|
110
|
+
stock += one_alloc_rsvd_stock(allocated_stock).to_i
|
111
|
+
end
|
112
|
+
stock
|
113
|
+
end
|
114
|
+
|
115
|
+
def host
|
116
|
+
url = ENV['ORDERS_URL'] || 'orders.forstok.com'
|
117
|
+
url + '/api/v2/item_line/count_one_allocated_reserved_stock'
|
118
|
+
end
|
119
|
+
|
120
|
+
def params(allocated_stock)
|
121
|
+
"channel_id=#{listing.channel_id}&item_variant_id=#{listing.variant_id}
|
122
|
+
&start_at=#{allocated_stock.start_at.to_date.beginning_of_day}
|
123
|
+
&end_at=#{allocated_stock.end_at.to_date.end_of_day}"
|
124
|
+
end
|
125
|
+
|
126
|
+
# one_alloc_rsvd_stock fungsi untuk mendapatkan satu
|
127
|
+
# allocated reserved stock
|
128
|
+
def one_alloc_rsvd_stock(allocated_stock)
|
129
|
+
RestClient.get("#{host}?#{params(allocated_stock)}")
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -5,11 +5,29 @@ require 'item_builder_mwh/modes/price_service'
|
|
5
5
|
require 'item_builder_mwh/modes/quantity_service'
|
6
6
|
require 'item_builder_mwh/modes/simple_service'
|
7
7
|
require 'item_builder_mwh/modes/active_service'
|
8
|
-
|
8
|
+
class ItemBuilderMwh
|
9
9
|
module Modes
|
10
10
|
attr_reader :listing
|
11
|
+
attr_reader :wh_spaces
|
12
|
+
attr_reader :variants
|
13
|
+
attr_reader :stock_allocs
|
14
|
+
attr_reader :bundles
|
15
|
+
attr_reader :item_bundle_variants
|
16
|
+
attr_reader :existing_alloc_stocks
|
17
|
+
attr_reader :variant_listings
|
18
|
+
attr_reader :reserved_stocks
|
19
|
+
attr_reader :zilingo_delta_quantity
|
11
20
|
def initialize(args)
|
12
21
|
@listing = args.fetch(:listing)
|
22
|
+
@wh_spaces = args.fetch(:wh_spaces, [])
|
23
|
+
@variants = args.fetch(:variants, [])
|
24
|
+
@stock_allocs = args.fetch(:stock_allocs, [])
|
25
|
+
@bundles = args.fetch(:bundles, [])
|
26
|
+
@item_bundle_variants = args.fetch(:item_bundle_variants, [])
|
27
|
+
@existing_alloc_stocks = args.fetch(:existing_alloc_stocks, [])
|
28
|
+
@variant_listings = args.fetch(:variant_listings, [])
|
29
|
+
@reserved_stocks = args.fetch(:reserved_stocks, [])
|
30
|
+
@zilingo_delta_quantity = args.fetch(:zilingo_delta_quantity, [])
|
13
31
|
warehouse
|
14
32
|
end
|
15
33
|
|
@@ -22,6 +40,36 @@ module ItemBuilderMwh
|
|
22
40
|
}
|
23
41
|
end
|
24
42
|
|
43
|
+
def existing_allocated_stock
|
44
|
+
existing_alloc_stocks.map do |els|
|
45
|
+
if listings.pluck(:id).include?(els.variant_association_id)
|
46
|
+
els
|
47
|
+
end
|
48
|
+
end.compact
|
49
|
+
end
|
50
|
+
|
51
|
+
def listings
|
52
|
+
variant_listings.select {|vl| vl.variant_id == listing.variant_id}
|
53
|
+
end
|
54
|
+
|
55
|
+
def bundle_variants
|
56
|
+
if bundle.present?
|
57
|
+
item_bundle_variants.select {|ibv| ibv.bundle_id == bundle.id }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def bundle
|
62
|
+
bundles.select {|b| b.variant_id == listing.variant_id }.first
|
63
|
+
end
|
64
|
+
|
65
|
+
def stock_alloc
|
66
|
+
stock_allocs.select {|sa| sa.variant_association_id == listing.id }.first
|
67
|
+
end
|
68
|
+
|
69
|
+
def variant
|
70
|
+
variants.select {|v| v.id == listing.variant_id }.first
|
71
|
+
end
|
72
|
+
|
25
73
|
def warehouse
|
26
74
|
warehouse_spaces.each do |warehouse_space|
|
27
75
|
warehouses << to_h(warehouse_space)
|
@@ -3,11 +3,15 @@
|
|
3
3
|
require 'date'
|
4
4
|
require 'item_builder_mwh/modes.rb'
|
5
5
|
require 'item_builder_mwh/modes/price_service'
|
6
|
-
|
6
|
+
class ItemBuilderMwh
|
7
7
|
module Modes
|
8
8
|
module Price
|
9
9
|
class SalePricePolicy
|
10
|
-
|
10
|
+
attr_reader :listing
|
11
|
+
def initialize(args)
|
12
|
+
@listing = args.fetch(:listing)
|
13
|
+
end
|
14
|
+
|
11
15
|
def sale_price
|
12
16
|
# has sale price but no sale date defined
|
13
17
|
# then push sale price immediately
|
@@ -8,7 +8,7 @@ require 'item_builder_mwh/modes/price/zalora_service'
|
|
8
8
|
require 'item_builder_mwh/modes/price/shopify_service'
|
9
9
|
require 'item_builder_mwh/modes/price/sale_price_policy'
|
10
10
|
require 'item_builder_mwh/modes/price/jd_service'
|
11
|
-
|
11
|
+
class ItemBuilderMwh
|
12
12
|
module Modes
|
13
13
|
class PriceService
|
14
14
|
include Modes
|
@@ -1,63 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
class ItemBuilderMwh
|
4
4
|
module Modes
|
5
5
|
module Quantity
|
6
6
|
class Base
|
7
7
|
attr_reader :listing
|
8
8
|
attr_reader :available_quantity
|
9
|
+
attr_reader :reserved_stock
|
9
10
|
|
10
|
-
def initialize(listing, available_quantity)
|
11
|
+
def initialize(listing, available_quantity, reserved_stock)
|
11
12
|
raise 'listing is not set' if listing.nil?
|
12
13
|
|
13
14
|
@listing = listing
|
14
15
|
@available_quantity = available_quantity
|
15
|
-
|
16
|
-
|
17
|
-
def order_host
|
18
|
-
url = ENV['ORDERS_URL'] || 'orders.forstok.com'
|
19
|
-
url + '/api/v2/item_line/reserved_stock'
|
20
|
-
end
|
21
|
-
|
22
|
-
def reserved_params
|
23
|
-
"account_id=#{listing.profile_channel_association_id}
|
24
|
-
&item_variant_id=#{listing.variant_id}"
|
25
|
-
end
|
26
|
-
|
27
|
-
def reserved_stock
|
28
|
-
RestClient.get("#{order_host}?#{reserved_params}")
|
29
|
-
end
|
30
|
-
|
31
|
-
def apigateway_get
|
32
|
-
RestClient.get("#{host}?#{params}")
|
33
|
-
end
|
34
|
-
|
35
|
-
def headers
|
36
|
-
{
|
37
|
-
content_type: :json,
|
38
|
-
accept: :json
|
39
|
-
}
|
40
|
-
end
|
41
|
-
|
42
|
-
def credential
|
43
|
-
account_id = listing.profile_channel_association_id
|
44
|
-
host = ENV['CREDENTIAL_URL'] || 'user.forstok.com'
|
45
|
-
RestClient.get("#{host}/credential?account_id=#{account_id}")
|
46
|
-
end
|
47
|
-
|
48
|
-
def data
|
49
|
-
{
|
50
|
-
"credential": JSON.parse(credential)['credential'],
|
51
|
-
"data": request
|
52
|
-
}
|
53
|
-
end
|
54
|
-
|
55
|
-
def api_data
|
56
|
-
data.to_json
|
57
|
-
end
|
58
|
-
|
59
|
-
def apigateway_post
|
60
|
-
RestClient.post(url, api_data, headers)
|
16
|
+
@reserved_stock = reserved_stock
|
61
17
|
end
|
62
18
|
end
|
63
19
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'item_builder_mwh/modes/quantity/base'
|
4
|
+
class ItemBuilderMwh
|
5
|
+
module Modes
|
6
|
+
module Quantity
|
7
|
+
class ZilingoService < Base
|
8
|
+
def perform
|
9
|
+
available_quantity - reserved_stock.to_i
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -2,19 +2,21 @@
|
|
2
2
|
|
3
3
|
require 'warehouse_models'
|
4
4
|
require 'item_builder_mwh/modes.rb'
|
5
|
+
require 'item_builder_mwh/get_quantity_service'
|
5
6
|
require 'item_builder_mwh/modes/quantity/base'
|
6
7
|
require 'item_builder_mwh/modes/quantity/lazada_service'
|
7
|
-
require 'item_builder_mwh/modes/quantity/blibli_service'
|
8
8
|
require 'item_builder_mwh/modes/quantity/zalora_service'
|
9
|
-
|
9
|
+
require 'item_builder_mwh/modes/quantity/zilingo_service'
|
10
|
+
|
11
|
+
class ItemBuilderMwh
|
10
12
|
module Modes
|
11
13
|
class QuantityService
|
12
14
|
include Modes
|
13
15
|
|
14
16
|
QUANTITY_CHANNEL = {}.tap do |hash|
|
15
17
|
hash[3] = :Lazada
|
16
|
-
hash[9] = :Blibli
|
17
18
|
hash[13] = :Zalora
|
19
|
+
hash[18] = :Zilingo
|
18
20
|
end.freeze
|
19
21
|
|
20
22
|
def perform
|
@@ -25,30 +27,47 @@ module ItemBuilderMwh
|
|
25
27
|
|
26
28
|
def to_h(warehouse_space)
|
27
29
|
{
|
28
|
-
quantity: qty(warehouse_space.quantity),
|
30
|
+
quantity: [qty(warehouse_space.quantity), 0].sort[1],
|
29
31
|
warehouse_id: wh_mapping(
|
30
32
|
warehouse_space.warehouse_id
|
31
33
|
)
|
32
34
|
}
|
33
35
|
end
|
34
36
|
|
35
|
-
def qty(
|
37
|
+
def qty(qty)
|
36
38
|
if channel_name.empty?
|
37
|
-
|
39
|
+
available_quantity(qty)
|
38
40
|
else
|
39
|
-
qty_channel(
|
41
|
+
qty_channel(qty)
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
43
|
-
def qty_channel(
|
45
|
+
def qty_channel(qty)
|
44
46
|
class_name = "ItemBuilderMwh::Modes::Quantity::#{channel_name}Service"
|
45
47
|
qty_channel_service = class_name.constantize
|
46
|
-
qty_channel_service.new(listing,
|
48
|
+
qty_channel_service.new(listing, available_quantity(qty), reserved_stock).perform
|
47
49
|
end
|
48
50
|
|
49
51
|
def channel_name
|
50
52
|
QUANTITY_CHANNEL[listing.channel_id].to_s
|
51
53
|
end
|
54
|
+
|
55
|
+
def reserved_stock
|
56
|
+
if channel_name == 'Zilingo'
|
57
|
+
zilingo_delta_quantity[listing.local_id].to_i
|
58
|
+
else
|
59
|
+
reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def available_quantity(qty)
|
64
|
+
ItemBuilderMwh::GetQuantityService.new(
|
65
|
+
listing: listing, stock_alloc: stock_alloc,
|
66
|
+
variant: variant, bundle_variants: bundle_variants,
|
67
|
+
existing_allocated_stock: existing_allocated_stock,
|
68
|
+
listing_wh_sp_quantity: qty
|
69
|
+
).perform
|
70
|
+
end
|
52
71
|
end
|
53
72
|
end
|
54
73
|
end
|
@@ -9,15 +9,13 @@ require 'item_builder_mwh/modes/simple/shopify_service'
|
|
9
9
|
require 'item_builder_mwh/modes/simple/jd_service'
|
10
10
|
require 'item_builder_mwh/modes/simple/lazada_service'
|
11
11
|
require 'item_builder_mwh/modes/simple/shopee_service'
|
12
|
-
|
12
|
+
class ItemBuilderMwh
|
13
13
|
module Modes
|
14
14
|
class SimpleService
|
15
15
|
include Modes
|
16
16
|
|
17
17
|
QUANTITY_CHANNEL = {}.tap do |hash|
|
18
18
|
hash[3] = :Lazada
|
19
|
-
hash[9] = :Blibli
|
20
|
-
hash[12] = :Shopee
|
21
19
|
hash[13] = :Zalora
|
22
20
|
end.freeze
|
23
21
|
|
@@ -50,20 +48,20 @@ module ItemBuilderMwh
|
|
50
48
|
end
|
51
49
|
|
52
50
|
def simple(warehouse_space)
|
53
|
-
|
54
|
-
qty(
|
51
|
+
qty = warehouse_space.quantity
|
52
|
+
qty(qty).merge(
|
55
53
|
price
|
56
54
|
)
|
57
55
|
end
|
58
56
|
|
59
|
-
def qty(
|
57
|
+
def qty(qty)
|
60
58
|
if quantity_name.empty?
|
61
59
|
{
|
62
|
-
quantity:
|
60
|
+
quantity: available_quantity(qty)
|
63
61
|
}
|
64
62
|
else
|
65
63
|
{
|
66
|
-
quantity: qty_channel(
|
64
|
+
quantity: qty_channel(qty)
|
67
65
|
}
|
68
66
|
end
|
69
67
|
end
|
@@ -84,13 +82,26 @@ module ItemBuilderMwh
|
|
84
82
|
def price_channel
|
85
83
|
class_name = "ItemBuilderMwh::Modes::Price::#{price_name}Service"
|
86
84
|
simple_channel_service = class_name.constantize
|
87
|
-
simple_channel_service.new(listing
|
85
|
+
simple_channel_service.new(listing).perform
|
88
86
|
end
|
89
87
|
|
90
|
-
def qty_channel(
|
88
|
+
def qty_channel(qty)
|
91
89
|
class_name = "ItemBuilderMwh::Modes::Quantity::#{quantity_name}Service"
|
92
90
|
qty_channel_service = class_name.constantize
|
93
|
-
qty_channel_service.new(listing,
|
91
|
+
qty_channel_service.new(listing, available_quantity(qty), reserved_stock).perform
|
92
|
+
end
|
93
|
+
|
94
|
+
def reserved_stock
|
95
|
+
reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
|
96
|
+
end
|
97
|
+
|
98
|
+
def available_quantity(qty)
|
99
|
+
ItemBuilderMwh::GetQuantityService.new(
|
100
|
+
listing: listing, stock_alloc: stock_alloc,
|
101
|
+
variant: variant, bundle_variants: bundle_variants,
|
102
|
+
existing_allocated_stock: existing_allocated_stock,
|
103
|
+
listing_wh_sp_quantity: qty
|
104
|
+
).perform
|
94
105
|
end
|
95
106
|
|
96
107
|
def price_name
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'item_builder_mwh/modes.rb'
|
4
|
+
class ItemBuilderMwh
|
5
|
+
class ZilingoQuantityService
|
6
|
+
attr_reader :listings, :skus
|
7
|
+
def initialize(args)
|
8
|
+
@listings = args.fetch(:listings)
|
9
|
+
@skus = args.fetch(:skus)
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform
|
13
|
+
args = {
|
14
|
+
method: :post, url: url, payload: data, headers: headers
|
15
|
+
}
|
16
|
+
resp = JSON.parse(rest_client(args, [200, 500, 406]))
|
17
|
+
response_process(resp)
|
18
|
+
end
|
19
|
+
|
20
|
+
def headers
|
21
|
+
{ content_type: :json, accept: :json }
|
22
|
+
end
|
23
|
+
|
24
|
+
def data
|
25
|
+
{
|
26
|
+
"credential": JSON.parse(credential)['credential'],
|
27
|
+
"data": { "skus": skus }
|
28
|
+
}.to_json
|
29
|
+
end
|
30
|
+
|
31
|
+
def credential
|
32
|
+
return @credential if @credential
|
33
|
+
|
34
|
+
account_id = listings[0].profile_channel_association_id
|
35
|
+
host = ENV['CREDENTIAL_URL'] || raise('credential url is not set')
|
36
|
+
url = "#{host}/credential?account_id=#{account_id}"
|
37
|
+
@credential = rest_client(method: :get, url: url)
|
38
|
+
end
|
39
|
+
|
40
|
+
def url
|
41
|
+
url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
|
42
|
+
url + "/zilingo/item_quantity_by_sku"
|
43
|
+
end
|
44
|
+
|
45
|
+
def response_process(resp)
|
46
|
+
hash = Hash.new
|
47
|
+
resp['zilingoSKUQuantities'].each do |sku|
|
48
|
+
hash[sku['zilingoSKUId']] = sku['quantity'] + sku['frozenQuantity']
|
49
|
+
end
|
50
|
+
hash
|
51
|
+
end
|
52
|
+
|
53
|
+
def rest_client(params, rescued_codes = 200)
|
54
|
+
RestClient::Request.execute(params.merge(timeout: 3)) do |response|
|
55
|
+
code = response.code
|
56
|
+
unless Array.wrap(rescued_codes).include?(code)
|
57
|
+
raise "Response Code is #{code}"
|
58
|
+
end
|
59
|
+
|
60
|
+
response
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
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.
|
4
|
+
version: 0.1.14
|
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-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -116,6 +116,7 @@ extensions: []
|
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
118
|
- lib/item_builder_mwh.rb
|
119
|
+
- lib/item_builder_mwh/get_quantity_service.rb
|
119
120
|
- lib/item_builder_mwh/modes.rb
|
120
121
|
- lib/item_builder_mwh/modes/active_service.rb
|
121
122
|
- lib/item_builder_mwh/modes/base_service.rb
|
@@ -128,9 +129,9 @@ files:
|
|
128
129
|
- lib/item_builder_mwh/modes/price/zalora_service.rb
|
129
130
|
- lib/item_builder_mwh/modes/price_service.rb
|
130
131
|
- lib/item_builder_mwh/modes/quantity/base.rb
|
131
|
-
- lib/item_builder_mwh/modes/quantity/blibli_service.rb
|
132
132
|
- lib/item_builder_mwh/modes/quantity/lazada_service.rb
|
133
133
|
- lib/item_builder_mwh/modes/quantity/zalora_service.rb
|
134
|
+
- lib/item_builder_mwh/modes/quantity/zilingo_service.rb
|
134
135
|
- lib/item_builder_mwh/modes/quantity_service.rb
|
135
136
|
- lib/item_builder_mwh/modes/simple/base.rb
|
136
137
|
- lib/item_builder_mwh/modes/simple/blibli_service.rb
|
@@ -143,6 +144,7 @@ files:
|
|
143
144
|
- lib/item_builder_mwh/modes/simple/zalora_service.rb
|
144
145
|
- lib/item_builder_mwh/modes/simple_service.rb
|
145
146
|
- lib/item_builder_mwh/version.rb
|
147
|
+
- lib/item_builder_mwh/zilingo_quantity_service.rb
|
146
148
|
homepage:
|
147
149
|
licenses: []
|
148
150
|
metadata: {}
|
@@ -161,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
161
163
|
- !ruby/object:Gem::Version
|
162
164
|
version: '0'
|
163
165
|
requirements: []
|
164
|
-
rubygems_version: 3.0.
|
166
|
+
rubygems_version: 3.0.9
|
165
167
|
signing_key:
|
166
168
|
specification_version: 4
|
167
169
|
summary: Item Builder Multiwarehouse
|
@@ -1,51 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'item_builder_mwh/modes/quantity/base'
|
4
|
-
module ItemBuilderMwh
|
5
|
-
module Modes
|
6
|
-
module Quantity
|
7
|
-
class BlibliService < Base
|
8
|
-
def perform
|
9
|
-
local_variant
|
10
|
-
end
|
11
|
-
|
12
|
-
def local_variant
|
13
|
-
qty = 0
|
14
|
-
local_product['value']['items'].each do |item|
|
15
|
-
qty = total_quantity(item) if item['itemSku'] == listing.local_id
|
16
|
-
end
|
17
|
-
qty
|
18
|
-
end
|
19
|
-
|
20
|
-
def total_quantity(item)
|
21
|
-
# Make stock 0
|
22
|
-
# if local reserved quantity is bigger than available quantity
|
23
|
-
if item['reservedStockLevel2'] > available_quantity
|
24
|
-
0
|
25
|
-
else
|
26
|
-
available_quantity
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# Find local product based on id
|
31
|
-
# @return [Hash] Local Blibli product
|
32
|
-
def local_product
|
33
|
-
@local_product ||= JSON.parse(apigateway_post)
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
def request
|
39
|
-
{
|
40
|
-
"gdnSku": listing.local_id
|
41
|
-
}
|
42
|
-
end
|
43
|
-
|
44
|
-
def url
|
45
|
-
url = ENV['API_GATEWAY_URL'] || 'apigateway.forstok.com'
|
46
|
-
url + '/blibli/item'
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|