item_builder 0.1.3 → 0.1.8
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/get_quantity_service.rb +141 -0
- data/lib/item_builder/modes.rb +0 -29
- data/lib/item_builder/modes/active_service.rb +9 -7
- data/lib/item_builder/modes/price/base.rb +26 -0
- data/lib/item_builder/modes/price/blibli_service.rb +26 -0
- data/lib/item_builder/modes/price/bukalapak_service.rb +60 -0
- data/lib/item_builder/modes/price/jd_service.rb +30 -0
- data/lib/item_builder/modes/price/sale_price_policy.rb +40 -0
- data/lib/item_builder/modes/price/shopify_service.rb +26 -0
- data/lib/item_builder/modes/price/zalora_service.rb +26 -0
- data/lib/item_builder/modes/price_service.rb +39 -13
- data/lib/item_builder/modes/quantity/base.rb +75 -0
- data/lib/item_builder/modes/quantity/blibli_service.rb +51 -0
- data/lib/item_builder/modes/quantity/lazada_service.rb +14 -0
- data/lib/item_builder/modes/quantity/shopee_service.rb +14 -0
- data/lib/item_builder/modes/quantity/zalora_service.rb +14 -0
- data/lib/item_builder/modes/quantity_service.rb +50 -8
- data/lib/item_builder/modes/simple_service.rb +11 -14
- data/lib/item_builder/version.rb +1 -1
- metadata +18 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c64fdf8fcf5b5a10d2d347e642cbc388acb98d5c7f4a9e2ec485a15291b06c43
|
4
|
+
data.tar.gz: 6abbe3d99475537e7a9cd347b23db0ad14298bd9608b3d17997e0dfc8fbc4ac3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 913765098ea5e4f64d71010eb0587ee164899683fcc05e0c30129c1d939dfbda73d11a8d53563fd0a4f2ce98bbdd4344b7f1c44ab87e65bfafc96dc963a86223
|
7
|
+
data.tar.gz: 1cef844215ba56c6297494bb441109b969322dcae52edb983295c9d9b01d6e7e75b6dfc2f80cf448989412ee555ee4bc097763a101d6408c1fff76c4e180b6cd
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'warehouse_models'
|
4
|
+
require 'item_builder/modes.rb'
|
5
|
+
module ItemBuilder
|
6
|
+
class GetQuantityService
|
7
|
+
attr_reader :listing, :wh_sp, :variant
|
8
|
+
def initialize(args)
|
9
|
+
@listing = args.fetch(:listing)
|
10
|
+
@wh_sp = args.fetch(:wh_sp)
|
11
|
+
@variant = args.fetch(:variant)
|
12
|
+
end
|
13
|
+
|
14
|
+
def perform
|
15
|
+
return 0 if check_consignment_variant?
|
16
|
+
|
17
|
+
if allocated_stock_active?
|
18
|
+
# yang masuk di kondisi ini,
|
19
|
+
# artinya akun tersebut ada allocated stock yang aktif
|
20
|
+
allocated_stock
|
21
|
+
elsif existing_allocated_stock.present?
|
22
|
+
# yang masuk di kondisi ini,
|
23
|
+
# artinya akun tersebut tidak ada allocated stock yang aktif,
|
24
|
+
# namun ada allocated stock yg aktif dari channel lain
|
25
|
+
warehouse_stock - count_existing_alloc_stock + count_alloc_rsvd_stock
|
26
|
+
else
|
27
|
+
# yang masuk di kondisi ini,
|
28
|
+
# artinya tidak ada allocated stock di item tersebut
|
29
|
+
warehouse_stock
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def check_consignment_variant?
|
36
|
+
listing.consignment? ||
|
37
|
+
(
|
38
|
+
!listing.active? && listing.channel_id == 12
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
def allocated_stock_active?
|
43
|
+
listing.present? && stock_alloc.present? && allocated_start_end?
|
44
|
+
end
|
45
|
+
|
46
|
+
def allocated_start_end?
|
47
|
+
stock_alloc.start_at.beginning_of_day <= Time.now &&
|
48
|
+
stock_alloc.end_at.end_of_day >= Time.now
|
49
|
+
end
|
50
|
+
|
51
|
+
def allocated_stock
|
52
|
+
stock_alloc.quantity.to_i - one_alloc_rsvd_stock(stock_alloc).to_i
|
53
|
+
end
|
54
|
+
|
55
|
+
def stock_alloc
|
56
|
+
@stock_alloc ||= listing.variant_listing_stock_allocation
|
57
|
+
end
|
58
|
+
|
59
|
+
# warehouse_stock fungsi untuk mendapatkan available quantity
|
60
|
+
# baik itu bundle atau default
|
61
|
+
def warehouse_stock
|
62
|
+
case variant.config.downcase
|
63
|
+
when 'default'
|
64
|
+
qty_default
|
65
|
+
when 'free gift', 'combo'
|
66
|
+
qty_bundle
|
67
|
+
else
|
68
|
+
raise "config not recognize => #{variant.config}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def qty_default
|
73
|
+
# set lower limit for quantity
|
74
|
+
[listing_wh_sp_quantity, 0].sort[1]
|
75
|
+
end
|
76
|
+
|
77
|
+
def qty_bundle
|
78
|
+
# Quantity for bundle config
|
79
|
+
qty_list = []
|
80
|
+
bundle_variants.each do |bvr|
|
81
|
+
qty = 0
|
82
|
+
if bundle_condition(bvr)
|
83
|
+
qty = bvr.variant.warehouse_spaces.first.quantity / bvr.unit
|
84
|
+
end
|
85
|
+
qty_list.push(qty)
|
86
|
+
end
|
87
|
+
[qty_list.min, 0].sort[1]
|
88
|
+
end
|
89
|
+
|
90
|
+
def bundle_condition(bvr)
|
91
|
+
bvr.variant.present? && bvr.variant.warehouse_spaces.present?
|
92
|
+
end
|
93
|
+
|
94
|
+
def bundle_variants
|
95
|
+
Bundle.where('variant_id = ?', variant.id).first.bundle_variant
|
96
|
+
end
|
97
|
+
|
98
|
+
def listing_wh_sp_quantity
|
99
|
+
@listing_wh_sp_quantity ||= wh_sp&.quantity || 0
|
100
|
+
end
|
101
|
+
|
102
|
+
def existing_allocated_stock
|
103
|
+
VariantListingStockAllocation.where(
|
104
|
+
variant_association_id: variant.variant_listings.pluck(:id)
|
105
|
+
).where('ADDTIME(end_at, "07:00") >= NOW()')
|
106
|
+
end
|
107
|
+
|
108
|
+
def count_existing_alloc_stock
|
109
|
+
return 0 if existing_allocated_stock.blank?
|
110
|
+
|
111
|
+
existing_allocated_stock.sum(:quantity)
|
112
|
+
end
|
113
|
+
|
114
|
+
def count_alloc_rsvd_stock
|
115
|
+
stock = 0
|
116
|
+
existing_allocated_stock.each do |allocated_stock|
|
117
|
+
stock += one_alloc_rsvd_stock(allocated_stock).to_i
|
118
|
+
end
|
119
|
+
stock
|
120
|
+
end
|
121
|
+
|
122
|
+
def host
|
123
|
+
url = ENV['ORDERS_URL'] || 'orders.forstok.com'
|
124
|
+
url + '/api/v2/item_line/count_one_allocated_reserved_stock'
|
125
|
+
end
|
126
|
+
|
127
|
+
def params(allocated_stock)
|
128
|
+
"channel_id=#{listing.channel_id}&item_variant_id=#{listing.variant_id}
|
129
|
+
&start_at=#{allocated_stock.start_at.to_date.beginning_of_day}
|
130
|
+
&end_at=#{allocated_stock.end_at.to_date.end_of_day}"
|
131
|
+
end
|
132
|
+
|
133
|
+
# one_alloc_rsvd_stock fungsi untuk mendapatkan satu
|
134
|
+
# allocated reserved stock
|
135
|
+
def one_alloc_rsvd_stock(allocated_stock)
|
136
|
+
RestClient.get("#{host}?#{params(allocated_stock)}")
|
137
|
+
rescue RestClient::ExceptionWithResponse => e
|
138
|
+
e.response
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
data/lib/item_builder/modes.rb
CHANGED
@@ -10,7 +10,6 @@ module ItemBuilder
|
|
10
10
|
attr_reader :listing
|
11
11
|
def initialize(args)
|
12
12
|
@listing = args.fetch(:listing)
|
13
|
-
warehouse
|
14
13
|
end
|
15
14
|
|
16
15
|
def base
|
@@ -21,33 +20,5 @@ module ItemBuilder
|
|
21
20
|
sku: listing.sku
|
22
21
|
}
|
23
22
|
end
|
24
|
-
|
25
|
-
def warehouse
|
26
|
-
warehouse_spaces.each do |warehouse_space|
|
27
|
-
warehouses << to_h(warehouse_space)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def warehouse_spaces
|
32
|
-
WarehouseSpace.where(item_variant_id: listing.variant_id)
|
33
|
-
end
|
34
|
-
|
35
|
-
def warehouse_mapping(warehouse_id)
|
36
|
-
WarehouseMapping
|
37
|
-
.where(profile_channel_association_id:
|
38
|
-
listing.profile_channel_association_id,
|
39
|
-
warehouse_id: warehouse_id)
|
40
|
-
end
|
41
|
-
|
42
|
-
def wh_mapping(warehouse_id)
|
43
|
-
data = warehouse_mapping(warehouse_id)
|
44
|
-
return nil if data.empty?
|
45
|
-
|
46
|
-
warehouse_mapping(warehouse_id)[0].channel_warehouse_id
|
47
|
-
end
|
48
|
-
|
49
|
-
def warehouses
|
50
|
-
@warehouses ||= []
|
51
|
-
end
|
52
23
|
end
|
53
24
|
end
|
@@ -7,19 +7,21 @@ module ItemBuilder
|
|
7
7
|
include Modes
|
8
8
|
|
9
9
|
def perform
|
10
|
-
|
11
|
-
warehouse: warehouses
|
12
|
-
)
|
10
|
+
to_h.merge(base)
|
13
11
|
end
|
14
12
|
|
15
|
-
def to_h
|
13
|
+
def to_h
|
16
14
|
{
|
17
15
|
active: listing.active,
|
18
|
-
|
19
|
-
warehouse_space.warehouse_id
|
20
|
-
)
|
16
|
+
active_variant: active_variant.count
|
21
17
|
}
|
22
18
|
end
|
19
|
+
|
20
|
+
def active_variant
|
21
|
+
VariantListing
|
22
|
+
.where(local_item_id: listing.local_item_id,
|
23
|
+
active: 1)
|
24
|
+
end
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'item_builder/modes.rb'
|
5
|
+
require 'item_builder/modes/price_service'
|
6
|
+
require 'item_builder/modes/price/sale_price_policy'
|
7
|
+
module ItemBuilder
|
8
|
+
module Modes
|
9
|
+
module Price
|
10
|
+
class Base
|
11
|
+
attr_reader :listing
|
12
|
+
|
13
|
+
def initialize(listing)
|
14
|
+
raise 'listing is not set' if listing.nil?
|
15
|
+
|
16
|
+
@listing = listing
|
17
|
+
end
|
18
|
+
|
19
|
+
def sale_price_policy
|
20
|
+
@sale_price_policy ||=
|
21
|
+
ItemBuilder::Modes::Price::SalePricePolicy.new(listing: listing)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'item_builder/modes/price/base'
|
5
|
+
module ItemBuilder
|
6
|
+
module Modes
|
7
|
+
module Price
|
8
|
+
class BlibliService < Base
|
9
|
+
def perform
|
10
|
+
{
|
11
|
+
price: listing.price,
|
12
|
+
sale_price: sale_price,
|
13
|
+
sale_start_at: listing.sale_start_at,
|
14
|
+
sale_end_at: listing.sale_end_at
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def sale_price
|
21
|
+
sale_price_policy.on_sale? ? listing.sale_price : listing.price
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'item_builder/modes/price/base'
|
5
|
+
module ItemBuilder
|
6
|
+
module Modes
|
7
|
+
module Price
|
8
|
+
class BukalapakService < Base
|
9
|
+
def perform
|
10
|
+
if listing.sale_price.nil?
|
11
|
+
bukalapak_price
|
12
|
+
else
|
13
|
+
deal
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def bukalapak_price
|
18
|
+
{
|
19
|
+
price: listing.price,
|
20
|
+
sale_price: listing.sale_price,
|
21
|
+
sale_start_at: listing.sale_start_at,
|
22
|
+
sale_end_at: listing.sale_end_at
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def deal
|
27
|
+
{
|
28
|
+
percentage: deal_percentage,
|
29
|
+
sale_start_at: sale_start_at,
|
30
|
+
sale_end_at: sale_end_at,
|
31
|
+
price: listing.price,
|
32
|
+
sale_price: listing.sale_price
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
def deal_percentage
|
37
|
+
price = listing.price.to_f
|
38
|
+
sale_price = listing.sale_price.to_f
|
39
|
+
100 - (sale_price / price * 100)
|
40
|
+
end
|
41
|
+
|
42
|
+
def sale_start_at
|
43
|
+
return DateTime.now if listing.sale_start_at.nil?
|
44
|
+
return DateTime.now if listing.sale_start_at < DateTime.now
|
45
|
+
|
46
|
+
listing.sale_start_at
|
47
|
+
end
|
48
|
+
|
49
|
+
def sale_end_at
|
50
|
+
month_later = DateTime.now + 1.month
|
51
|
+
return month_later if listing.sale_end_at.nil?
|
52
|
+
return month_later if listing.sale_end_at > month_later
|
53
|
+
return month_later if listing.sale_end_at < DateTime.now
|
54
|
+
|
55
|
+
listing.sale_end_at
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'item_builder/modes/price/base'
|
5
|
+
module ItemBuilder
|
6
|
+
module Modes
|
7
|
+
module Price
|
8
|
+
class JdService < Base
|
9
|
+
def perform
|
10
|
+
{
|
11
|
+
price: listing.price,
|
12
|
+
sale_price: jd_price,
|
13
|
+
sale_start_at: listing.sale_start_at,
|
14
|
+
sale_end_at: listing.sale_end_at
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def jd_price
|
19
|
+
increment_price =
|
20
|
+
if (listing.price % 1000).positive?
|
21
|
+
1000
|
22
|
+
else
|
23
|
+
0
|
24
|
+
end
|
25
|
+
(listing.price.to_i / 1000) * 1000 + increment_price
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'item_builder/modes.rb'
|
5
|
+
require 'item_builder/modes/price_service'
|
6
|
+
module ItemBuilder
|
7
|
+
module Modes
|
8
|
+
module Price
|
9
|
+
class SalePricePolicy
|
10
|
+
include Modes
|
11
|
+
def sale_price
|
12
|
+
# has sale price but no sale date defined
|
13
|
+
# then push sale price immediately
|
14
|
+
# assuming it starts today and no end date specified
|
15
|
+
return listing.sale_price if sale_price? && !sale_date?
|
16
|
+
|
17
|
+
# has sale price and today is on sale then push sale price immediately
|
18
|
+
listing.sale_price if sale_price? && on_sale?
|
19
|
+
# don't push / don't return anything if 2 rules above unfulfilled
|
20
|
+
end
|
21
|
+
|
22
|
+
def sale_price?
|
23
|
+
listing.sale_price
|
24
|
+
end
|
25
|
+
|
26
|
+
def sale_date?
|
27
|
+
listing.sale_start_at && listing.sale_end_at
|
28
|
+
end
|
29
|
+
|
30
|
+
def on_sale?
|
31
|
+
return false unless sale_price?
|
32
|
+
|
33
|
+
sale_start_at = listing.sale_start_at || DateTime.now
|
34
|
+
sale_end_at = listing.sale_end_at || DateTime.now + 1_000_000.years
|
35
|
+
sale_start_at <= DateTime.now && sale_end_at >= DateTime.now
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'item_builder/modes/price/base'
|
5
|
+
module ItemBuilder
|
6
|
+
module Modes
|
7
|
+
module Price
|
8
|
+
class ShopifyService < Base
|
9
|
+
def perform
|
10
|
+
{
|
11
|
+
price: listing.price,
|
12
|
+
sale_price: sale_price,
|
13
|
+
sale_start_at: listing.sale_start_at,
|
14
|
+
sale_end_at: listing.sale_end_at
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def sale_price
|
21
|
+
sale_price_policy.on_sale? ? listing.sale_price : listing.price
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'item_builder/modes/price/base'
|
5
|
+
module ItemBuilder
|
6
|
+
module Modes
|
7
|
+
module Price
|
8
|
+
class ZaloraService < Base
|
9
|
+
def perform
|
10
|
+
{
|
11
|
+
price: listing.price,
|
12
|
+
sale_price: sale_price,
|
13
|
+
sale_start_at: listing.sale_start_at,
|
14
|
+
sale_end_at: listing.sale_end_at
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def sale_price
|
21
|
+
sale_price_policy.on_sale? ? listing.sale_price : listing.price
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,27 +1,53 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'item_builder/modes.rb'
|
4
|
+
require 'item_builder/modes/price/blibli_service'
|
5
|
+
require 'item_builder/modes/price/bukalapak_service'
|
6
|
+
require 'item_builder/modes/price/zalora_service'
|
7
|
+
require 'item_builder/modes/price/shopify_service'
|
8
|
+
require 'item_builder/modes/price/jd_service'
|
4
9
|
module ItemBuilder
|
5
10
|
module Modes
|
6
11
|
class PriceService
|
7
12
|
include Modes
|
8
13
|
|
14
|
+
PRICE_CHANNEL = {}.tap do |hash|
|
15
|
+
hash[2] = :Shopify
|
16
|
+
hash[9] = :Blibli
|
17
|
+
hash[11] = :Bukalapak
|
18
|
+
hash[13] = :Zalora
|
19
|
+
hash[16] = :Jd
|
20
|
+
end.freeze
|
21
|
+
|
9
22
|
def perform
|
10
|
-
|
11
|
-
|
12
|
-
|
23
|
+
to_h.merge(base)
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_h
|
27
|
+
price
|
28
|
+
end
|
29
|
+
|
30
|
+
def price
|
31
|
+
if channel_name.empty?
|
32
|
+
{
|
33
|
+
price: listing.price,
|
34
|
+
sale_price: listing.sale_price,
|
35
|
+
sale_start_at: listing.sale_start_at,
|
36
|
+
sale_end_at: listing.sale_end_at
|
37
|
+
}
|
38
|
+
else
|
39
|
+
price_channel
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def price_channel
|
44
|
+
class_name = "ItemBuilder::Modes::Price::#{channel_name}Service"
|
45
|
+
price_channel_service = class_name.constantize
|
46
|
+
price_channel_service.new(listing).perform
|
13
47
|
end
|
14
48
|
|
15
|
-
def
|
16
|
-
|
17
|
-
price: listing.price,
|
18
|
-
sale_price: listing.sale_price,
|
19
|
-
sale_start_at: listing.sale_start_at,
|
20
|
-
sale_end_at: listing.sale_end_at,
|
21
|
-
warehouse_id: wh_mapping(
|
22
|
-
warehouse_space.warehouse_id
|
23
|
-
)
|
24
|
-
}
|
49
|
+
def channel_name
|
50
|
+
PRICE_CHANNEL[listing.channel_id].to_s
|
25
51
|
end
|
26
52
|
end
|
27
53
|
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ItemBuilder
|
4
|
+
module Modes
|
5
|
+
module Quantity
|
6
|
+
class Base
|
7
|
+
attr_reader :listing
|
8
|
+
attr_reader :available_quantity
|
9
|
+
|
10
|
+
def initialize(listing, available_quantity)
|
11
|
+
raise 'listing is not set' if listing.nil?
|
12
|
+
|
13
|
+
@listing = listing
|
14
|
+
@available_quantity = available_quantity
|
15
|
+
end
|
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
|
+
rescue RestClient::ExceptionWithResponse => e
|
30
|
+
e.response
|
31
|
+
end
|
32
|
+
|
33
|
+
def apigateway_get
|
34
|
+
RestClient.get("#{host}?#{params}")
|
35
|
+
rescue RestClient::ExceptionWithResponse => e
|
36
|
+
e.response
|
37
|
+
end
|
38
|
+
|
39
|
+
def headers
|
40
|
+
{
|
41
|
+
content_type: :json,
|
42
|
+
accept: :json
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
def credential
|
47
|
+
account_id = listing.profile_channel_association_id
|
48
|
+
host = ENV['CREDENTIAL_URL'] || 'user.forstok.com'
|
49
|
+
begin
|
50
|
+
RestClient.get("#{host}/credential?account_id=#{account_id}")
|
51
|
+
rescue RestClient::ExceptionWithResponse => e
|
52
|
+
e.response
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def data
|
57
|
+
{
|
58
|
+
"credential": JSON.parse(credential)['credential'],
|
59
|
+
"data": request
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
def api_data
|
64
|
+
data.to_json
|
65
|
+
end
|
66
|
+
|
67
|
+
def apigateway_post
|
68
|
+
RestClient.post(url, api_data, headers)
|
69
|
+
rescue RestClient::ExceptionWithResponse => e
|
70
|
+
e.response
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'item_builder/modes/quantity/base'
|
4
|
+
module ItemBuilder
|
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
|
+
item['availableStockLevel2'] + item['reservedStockLevel2']
|
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
|
@@ -2,25 +2,67 @@
|
|
2
2
|
|
3
3
|
require 'warehouse_models'
|
4
4
|
require 'item_builder/modes.rb'
|
5
|
+
require 'item_builder/get_quantity_service'
|
6
|
+
require 'item_builder/modes/quantity/base'
|
7
|
+
require 'item_builder/modes/quantity/lazada_service'
|
8
|
+
require 'item_builder/modes/quantity/blibli_service'
|
9
|
+
require 'item_builder/modes/quantity/shopee_service'
|
10
|
+
require 'item_builder/modes/quantity/zalora_service'
|
5
11
|
module ItemBuilder
|
6
12
|
module Modes
|
7
13
|
class QuantityService
|
8
14
|
include Modes
|
9
15
|
|
16
|
+
QUANTITY_CHANNEL = {}.tap do |hash|
|
17
|
+
hash[3] = :Lazada
|
18
|
+
hash[9] = :Blibli
|
19
|
+
hash[12] = :Shopee
|
20
|
+
hash[13] = :Zalora
|
21
|
+
end.freeze
|
22
|
+
|
10
23
|
def perform
|
11
|
-
|
12
|
-
warehouse: warehouses
|
13
|
-
)
|
24
|
+
to_h.merge(base)
|
14
25
|
end
|
15
26
|
|
16
|
-
def to_h
|
27
|
+
def to_h
|
17
28
|
{
|
18
|
-
quantity:
|
19
|
-
warehouse_id: wh_mapping(
|
20
|
-
warehouse_space.warehouse_id
|
21
|
-
)
|
29
|
+
quantity: qty
|
22
30
|
}
|
23
31
|
end
|
32
|
+
|
33
|
+
def qty
|
34
|
+
if channel_name.empty?
|
35
|
+
available_quantity
|
36
|
+
else
|
37
|
+
qty_channel
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def qty_channel
|
42
|
+
class_name = "ItemBuilder::Modes::Quantity::#{channel_name}Service"
|
43
|
+
qty_channel_service = class_name.constantize
|
44
|
+
qty_channel_service.new(listing, available_quantity).perform
|
45
|
+
end
|
46
|
+
|
47
|
+
def channel_name
|
48
|
+
QUANTITY_CHANNEL[listing.channel_id].to_s
|
49
|
+
end
|
50
|
+
|
51
|
+
def available_quantity
|
52
|
+
ItemBuilder::GetQuantityService.new(
|
53
|
+
listing: listing,
|
54
|
+
variant: variant,
|
55
|
+
wh_sp: warehouse
|
56
|
+
).perform
|
57
|
+
end
|
58
|
+
|
59
|
+
def warehouse
|
60
|
+
WarehouseSpace.find_by(item_variant_id: listing.variant_id)
|
61
|
+
end
|
62
|
+
|
63
|
+
def variant
|
64
|
+
@variant ||= Variant.find(listing.variant_id)
|
65
|
+
end
|
24
66
|
end
|
25
67
|
end
|
26
68
|
end
|
@@ -9,22 +9,19 @@ module ItemBuilder
|
|
9
9
|
include Modes
|
10
10
|
|
11
11
|
def perform
|
12
|
-
|
13
|
-
warehouse: warehouses
|
14
|
-
)
|
12
|
+
to_h.merge(base)
|
15
13
|
end
|
16
14
|
|
17
|
-
def to_h
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
}
|
15
|
+
def to_h
|
16
|
+
quantity.merge(price)
|
17
|
+
end
|
18
|
+
|
19
|
+
def quantity
|
20
|
+
QuantityService.new(listing: listing).to_h
|
21
|
+
end
|
22
|
+
|
23
|
+
def price
|
24
|
+
PriceService.new(listing: listing).to_h
|
28
25
|
end
|
29
26
|
end
|
30
27
|
end
|
data/lib/item_builder/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: item_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- okaaryanata
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-07-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,14 +86,14 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 0.0.
|
89
|
+
version: 0.0.11
|
90
90
|
type: :runtime
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: 0.0.
|
96
|
+
version: 0.0.11
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: warehouse_models
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,10 +116,23 @@ extensions: []
|
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
118
|
- lib/item_builder.rb
|
119
|
+
- lib/item_builder/get_quantity_service.rb
|
119
120
|
- lib/item_builder/modes.rb
|
120
121
|
- lib/item_builder/modes/active_service.rb
|
121
122
|
- lib/item_builder/modes/base_service.rb
|
123
|
+
- lib/item_builder/modes/price/base.rb
|
124
|
+
- lib/item_builder/modes/price/blibli_service.rb
|
125
|
+
- lib/item_builder/modes/price/bukalapak_service.rb
|
126
|
+
- lib/item_builder/modes/price/jd_service.rb
|
127
|
+
- lib/item_builder/modes/price/sale_price_policy.rb
|
128
|
+
- lib/item_builder/modes/price/shopify_service.rb
|
129
|
+
- lib/item_builder/modes/price/zalora_service.rb
|
122
130
|
- lib/item_builder/modes/price_service.rb
|
131
|
+
- lib/item_builder/modes/quantity/base.rb
|
132
|
+
- lib/item_builder/modes/quantity/blibli_service.rb
|
133
|
+
- lib/item_builder/modes/quantity/lazada_service.rb
|
134
|
+
- lib/item_builder/modes/quantity/shopee_service.rb
|
135
|
+
- lib/item_builder/modes/quantity/zalora_service.rb
|
123
136
|
- lib/item_builder/modes/quantity_service.rb
|
124
137
|
- lib/item_builder/modes/simple_service.rb
|
125
138
|
- lib/item_builder/version.rb
|
@@ -141,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
154
|
- !ruby/object:Gem::Version
|
142
155
|
version: '0'
|
143
156
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
157
|
+
rubygems_version: 3.0.6
|
145
158
|
signing_key:
|
146
159
|
specification_version: 4
|
147
160
|
summary: Item builder
|