item_builder_mwh 0.1.7 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/lib/item_builder_mwh.rb +116 -14
  3. data/lib/item_builder_mwh/get_quantity_service.rb +132 -0
  4. data/lib/item_builder_mwh/modes.rb +47 -1
  5. data/lib/item_builder_mwh/modes/active_service.rb +1 -1
  6. data/lib/item_builder_mwh/modes/base_service.rb +1 -1
  7. data/lib/item_builder_mwh/modes/price/base.rb +1 -1
  8. data/lib/item_builder_mwh/modes/price/blibli_service.rb +1 -1
  9. data/lib/item_builder_mwh/modes/price/bukalapak_service.rb +1 -1
  10. data/lib/item_builder_mwh/modes/price/jd_service.rb +1 -1
  11. data/lib/item_builder_mwh/modes/price/sale_price_policy.rb +6 -2
  12. data/lib/item_builder_mwh/modes/price/shopify_service.rb +1 -1
  13. data/lib/item_builder_mwh/modes/price/zalora_service.rb +1 -1
  14. data/lib/item_builder_mwh/modes/price_service.rb +1 -1
  15. data/lib/item_builder_mwh/modes/quantity/base.rb +4 -58
  16. data/lib/item_builder_mwh/modes/quantity/lazada_service.rb +1 -1
  17. data/lib/item_builder_mwh/modes/quantity/zalora_service.rb +1 -1
  18. data/lib/item_builder_mwh/modes/quantity_service.rb +20 -10
  19. data/lib/item_builder_mwh/modes/simple/base.rb +2 -12
  20. data/lib/item_builder_mwh/modes/simple/blibli_service.rb +1 -1
  21. data/lib/item_builder_mwh/modes/simple/bukalapak_service.rb +3 -3
  22. data/lib/item_builder_mwh/modes/simple/jd_service.rb +2 -2
  23. data/lib/item_builder_mwh/modes/simple/lazada_service.rb +1 -1
  24. data/lib/item_builder_mwh/modes/simple/sale_price_policy.rb +1 -1
  25. data/lib/item_builder_mwh/modes/simple/shopee_service.rb +2 -2
  26. data/lib/item_builder_mwh/modes/simple/shopify_service.rb +2 -2
  27. data/lib/item_builder_mwh/modes/simple/zalora_service.rb +1 -1
  28. data/lib/item_builder_mwh/modes/simple_service.rb +22 -11
  29. data/lib/item_builder_mwh/version.rb +2 -2
  30. metadata +7 -8
  31. data/lib/item_builder_mwh/modes/quantity/blibli_service.rb +0 -51
  32. data/lib/item_builder_mwh/modes/quantity/shopee_service.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a39d996fe505e2fd15b46f95671f349384ae92da5366e88008b81f82619fe75
4
- data.tar.gz: d511eb1becb767fef12750734edf75a3d00c6c8118b6edcb2562f3526af3de86
3
+ metadata.gz: 5d1bdd2602056502397904701b901ca0e64fc1e1720d19ad6224bf2730abb35f
4
+ data.tar.gz: d38c4c58bc14bb122b5134709eb6716dd050dcd2c7bf1a6af953c4c26df06f7a
5
5
  SHA512:
6
- metadata.gz: 224bb9928a797411817583da70b34681a4435ad1bd6be095649504480c41cee258d419605f4bfc2adf4e0a8ca1c65705f9a13ee37b4ffa8ac6a7a4e580e085be
7
- data.tar.gz: 8ca72e701203575d82f9f60ca308d782c65ee74f30ded4b5161f5136af7cbfa9f148e9e621895a3be0b72102085b27c34408942fdbe9081c733fb7acc1307685
6
+ metadata.gz: 5e9db45fb157df0b8a978e169a0b5a54704421df77028f1996584010d407e8f8649642db4d4db2c7dd427c266d020ea3f131ae772d45ba357d4dff9935db2f63
7
+ data.tar.gz: d3014d0450be1a652e55bc0b708ed89eeed6fedc29a279a58fad42134b52b5c845bd30eeb7d7eb860ae6b70a8df22514d67124a56cdc11282b7d4f8457652f84
@@ -8,22 +8,124 @@ require 'item_builder_mwh/modes/quantity_service'
8
8
  require 'item_builder_mwh/modes/simple_service'
9
9
  require 'item_models'
10
10
 
11
- module ItemBuilderMwh
12
- class << self
13
- def build(listing_ids, mode)
14
- @mode = mode
15
- VariantListing.where(id: listing_ids).map do |listing|
16
- modes[mode].new(listing: listing).perform
17
- end
11
+ class ItemBuilderMwh
12
+ attr_reader :listing_ids
13
+ attr_reader :listings
14
+ attr_reader :mode
15
+ attr_reader :wh_spaces
16
+ attr_reader :variants
17
+ attr_reader :variant_ids
18
+ def initialize(listing_ids, mode)
19
+ @listing_ids = listing_ids
20
+ @mode = mode
21
+ end
22
+
23
+ def self.build(listing_ids, mode)
24
+ new(listing_ids, mode).mode_check
25
+ end
26
+
27
+ def mode_check
28
+ if mode == :quantity || mode == :simple
29
+ quantity_simple_mode
30
+ else
31
+ default
32
+ end
33
+ end
34
+
35
+ def quantity_simple_mode
36
+ listings.map do |listing|
37
+ modes[mode].new(qty_simple_params(listing)).perform
18
38
  end
39
+ end
19
40
 
20
- def modes
21
- {
22
- price: ItemBuilderMwh::Modes::PriceService,
23
- quantity: ItemBuilderMwh::Modes::QuantityService,
24
- simple: ItemBuilderMwh::Modes::SimpleService,
25
- active: ItemBuilderMwh::Modes::ActiveService
26
- }
41
+ def default
42
+ listings.map do |listing|
43
+ modes[mode].new(listing: listing).perform
27
44
  end
28
45
  end
46
+
47
+ def qty_simple_params(listing)
48
+ {
49
+ listing: listing, wh_spaces: wh_spaces, variants: variants,
50
+ stock_allocs: stock_allocs, variant_listings: variant_listings,
51
+ bundles: bundles, item_bundle_variants: item_bundle_variants,
52
+ existing_alloc_stocks: existing_alloc_stocks,
53
+ reserved_stocks: reserved_stocks
54
+ }
55
+ end
56
+
57
+ def existing_alloc_stocks
58
+ @existing_alloc_stocks ||= VariantListingStockAllocation.where(
59
+ variant_association_id: vl_ids
60
+ ).where('ADDTIME(end_at, "07:00") >= NOW()')
61
+ end
62
+
63
+ def vl_ids
64
+ @vl_ids ||= variant_listings.map(&:id).uniq
65
+ end
66
+
67
+ def variant_listings
68
+ @variant_listings ||= VariantListing.where(variant_id: variant_ids)
69
+ end
70
+
71
+ def item_bundle_variants
72
+ @item_bundle_variants ||= BundleVariant.where(
73
+ bundle_id: bundle_ids
74
+ )
75
+ end
76
+
77
+ def bundle_ids
78
+ @bundle_ids ||= bundles.map(&:id).uniq
79
+ end
80
+
81
+ def bundles
82
+ @bundles ||= Bundle.where(variant_id: variant_ids).group(:variant_id)
83
+ end
84
+
85
+ def stock_allocs
86
+ @stock_allocs ||= VariantListingStockAllocation.where(
87
+ variant_association_id: vl_ids
88
+ )
89
+ end
90
+
91
+ def wh_spaces
92
+ @wh_spaces ||= WarehouseSpace.where(item_variant_id: variant_ids)
93
+ end
94
+
95
+ def variants
96
+ @variants ||= Variant.where(id: variant_ids)
97
+ end
98
+
99
+ def variant_ids
100
+ @variant_ids ||= listings.map(&:variant_id).uniq
101
+ end
102
+
103
+ def order_host
104
+ url = ENV['ORDERS_URL'] || 'orders.forstok.com'
105
+ url + '/api/v3/item_line/reserved_stock'
106
+ end
107
+
108
+ def reserved_params
109
+ "account_id=#{listings[0].profile_channel_association_id}
110
+ &item_variant_ids=#{variant_ids.join(',')}"
111
+ end
112
+
113
+ def reserved_stocks
114
+ @reserved_stocks ||= JSON.parse(RestClient.get(
115
+ "#{order_host}?#{reserved_params}"
116
+ ).body) if [3,13].include?(listings[0].channel_id)
117
+ end
118
+
119
+ def modes
120
+ {
121
+ price: ItemBuilderMwh::Modes::PriceService,
122
+ quantity: ItemBuilderMwh::Modes::QuantityService,
123
+ simple: ItemBuilderMwh::Modes::SimpleService,
124
+ active: ItemBuilderMwh::Modes::ActiveService
125
+ }
126
+ end
127
+
128
+ def listings
129
+ @listings ||= VariantListing.where(id: listing_ids)
130
+ end
29
131
  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,27 @@ 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
- module ItemBuilderMwh
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
11
19
  def initialize(args)
12
20
  @listing = args.fetch(:listing)
21
+ @wh_spaces = args.fetch(:wh_spaces, [])
22
+ @variants = args.fetch(:variants, [])
23
+ @stock_allocs = args.fetch(:stock_allocs, [])
24
+ @bundles = args.fetch(:bundles, [])
25
+ @item_bundle_variants = args.fetch(:item_bundle_variants, [])
26
+ @existing_alloc_stocks = args.fetch(:existing_alloc_stocks, [])
27
+ @variant_listings = args.fetch(:variant_listings, [])
28
+ @reserved_stocks = args.fetch(:reserved_stocks, [])
13
29
  warehouse
14
30
  end
15
31
 
@@ -22,6 +38,36 @@ module ItemBuilderMwh
22
38
  }
23
39
  end
24
40
 
41
+ def existing_allocated_stock
42
+ existing_alloc_stocks.map do |els|
43
+ if listings.pluck(:id).include?(els.variant_association_id)
44
+ els
45
+ end
46
+ end.compact
47
+ end
48
+
49
+ def listings
50
+ variant_listings.select {|vl| vl.variant_id == listing.variant_id}
51
+ end
52
+
53
+ def bundle_variants
54
+ if bundle.present?
55
+ item_bundle_variants.select {|ibv| ibv.bundle_id == bundle.id }
56
+ end
57
+ end
58
+
59
+ def bundle
60
+ bundles.select {|b| b.variant_id == listing.variant_id }.first
61
+ end
62
+
63
+ def stock_alloc
64
+ stock_allocs.select {|sa| sa.variant_association_id == listing.id }.first
65
+ end
66
+
67
+ def variant
68
+ variants.select {|v| v.id == listing.variant_id }.first
69
+ end
70
+
25
71
  def warehouse
26
72
  warehouse_spaces.each do |warehouse_space|
27
73
  warehouses << to_h(warehouse_space)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'item_builder_mwh/modes.rb'
4
- module ItemBuilderMwh
4
+ class ItemBuilderMwh
5
5
  module Modes
6
6
  class ActiveService
7
7
  include Modes
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'item_builder_mwh/modes.rb'
4
- module ItemBuilderMwh
4
+ class ItemBuilderMwh
5
5
  module Modes
6
6
  class BaseService
7
7
  attr_reader :listing
@@ -4,7 +4,7 @@ require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/price_service'
6
6
  require 'item_builder_mwh/modes/price/sale_price_policy'
7
- module ItemBuilderMwh
7
+ class ItemBuilderMwh
8
8
  module Modes
9
9
  module Price
10
10
  class Base
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/price/base'
6
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Price
9
9
  class BlibliService < Base
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/price/base'
6
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Price
9
9
  class BukalapakService < Base
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/price/base'
6
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Price
9
9
  class JdService < Base
@@ -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
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Price
9
9
  class SalePricePolicy
10
- include Modes
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
@@ -4,7 +4,7 @@ require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/price/base'
6
6
  require 'item_builder_mwh/modes/price/sale_price_policy'
7
- module ItemBuilderMwh
7
+ class ItemBuilderMwh
8
8
  module Modes
9
9
  module Price
10
10
  class ShopifyService < Base
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/price/base'
6
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Price
9
9
  class ZaloraService < Base
@@ -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
- module ItemBuilderMwh
11
+ class ItemBuilderMwh
12
12
  module Modes
13
13
  class PriceService
14
14
  include Modes
@@ -1,73 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ItemBuilderMwh
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
- 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
16
+ @reserved_stock = reserved_stock
71
17
  end
72
18
  end
73
19
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'item_builder_mwh/modes/quantity/base'
4
- module ItemBuilderMwh
4
+ class ItemBuilderMwh
5
5
  module Modes
6
6
  module Quantity
7
7
  class LazadaService < Base
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'item_builder_mwh/modes/quantity/base'
4
- module ItemBuilderMwh
4
+ class ItemBuilderMwh
5
5
  module Modes
6
6
  module Quantity
7
7
  class ZaloraService < Base
@@ -2,20 +2,17 @@
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
- require 'item_builder_mwh/modes/quantity/shopee_service'
9
8
  require 'item_builder_mwh/modes/quantity/zalora_service'
10
- module ItemBuilderMwh
9
+ class ItemBuilderMwh
11
10
  module Modes
12
11
  class QuantityService
13
12
  include Modes
14
13
 
15
14
  QUANTITY_CHANNEL = {}.tap do |hash|
16
15
  hash[3] = :Lazada
17
- hash[9] = :Blibli
18
- hash[12] = :Shopee
19
16
  hash[13] = :Zalora
20
17
  end.freeze
21
18
 
@@ -34,23 +31,36 @@ module ItemBuilderMwh
34
31
  }
35
32
  end
36
33
 
37
- def qty(available_qty)
34
+ def qty(qty)
38
35
  if channel_name.empty?
39
- available_qty
36
+ available_quantity(qty)
40
37
  else
41
- qty_channel(available_qty)
38
+ qty_channel(qty)
42
39
  end
43
40
  end
44
41
 
45
- def qty_channel(available_qty)
42
+ def qty_channel(qty)
46
43
  class_name = "ItemBuilderMwh::Modes::Quantity::#{channel_name}Service"
47
44
  qty_channel_service = class_name.constantize
48
- qty_channel_service.new(listing, available_qty).perform
45
+ qty_channel_service.new(listing, available_quantity(qty), reserved_stock).perform
49
46
  end
50
47
 
51
48
  def channel_name
52
49
  QUANTITY_CHANNEL[listing.channel_id].to_s
53
50
  end
51
+
52
+ def reserved_stock
53
+ reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
54
+ end
55
+
56
+ def available_quantity(qty)
57
+ ItemBuilderMwh::GetQuantityService.new(
58
+ listing: listing, stock_alloc: stock_alloc,
59
+ variant: variant, bundle_variants: bundle_variants,
60
+ existing_allocated_stock: existing_allocated_stock,
61
+ listing_wh_sp_quantity: qty
62
+ ).perform
63
+ end
54
64
  end
55
65
  end
56
66
  end
@@ -4,7 +4,7 @@ require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/simple_service'
6
6
  require 'item_builder_mwh/modes/simple/sale_price_policy'
7
- module ItemBuilderMwh
7
+ class ItemBuilderMwh
8
8
  module Modes
9
9
  module Simple
10
10
  class Base
@@ -30,14 +30,10 @@ module ItemBuilderMwh
30
30
 
31
31
  def reserved_stock
32
32
  RestClient.get("#{order_host}?#{reserved_params}")
33
- rescue RestClient::ExceptionWithResponse => e
34
- e.response
35
33
  end
36
34
 
37
35
  def apigateway_get
38
36
  RestClient.get("#{host}?#{params}")
39
- rescue RestClient::ExceptionWithResponse => e
40
- e.response
41
37
  end
42
38
 
43
39
  def headers
@@ -50,11 +46,7 @@ module ItemBuilderMwh
50
46
  def credential
51
47
  account_id = listing.profile_channel_association_id
52
48
  host = ENV['CREDENTIAL_URL'] || 'user.forstok.com'
53
- begin
54
- RestClient.get("#{host}/credential?account_id=#{account_id}")
55
- rescue RestClient::ExceptionWithResponse => e
56
- e.response
57
- end
49
+ RestClient.get("#{host}/credential?account_id=#{account_id}")
58
50
  end
59
51
 
60
52
  def data
@@ -75,8 +67,6 @@ module ItemBuilderMwh
75
67
 
76
68
  def apigateway_post
77
69
  RestClient.post(url, api_data, headers)
78
- rescue RestClient::ExceptionWithResponse => e
79
- e.response
80
70
  end
81
71
  end
82
72
  end
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/simple/base'
6
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Simple
9
9
  class BlibliService < Base
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/simple/base'
6
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Simple
9
9
  class BukalapakService < Base
@@ -21,7 +21,7 @@ module ItemBuilderMwh
21
21
  sale_price: listing.sale_price,
22
22
  sale_start_at: listing.sale_start_at,
23
23
  sale_end_at: listing.sale_end_at,
24
- quantity: available_quantity + reserved_stock.to_i,
24
+ quantity: available_quantity,
25
25
  warehouse_id: wh_mapping(
26
26
  warehouse_space.warehouse_id
27
27
  )
@@ -35,7 +35,7 @@ module ItemBuilderMwh
35
35
  sale_end_at: sale_end_at,
36
36
  price: listing.price,
37
37
  sale_price: listing.sale_price,
38
- quantity: available_quantity + reserved_stock.to_i,
38
+ quantity: available_quantity,
39
39
  warehouse_id: wh_mapping(
40
40
  warehouse_space.warehouse_id
41
41
  )
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/simple/base'
6
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Simple
9
9
  class JdService < Base
@@ -13,7 +13,7 @@ module ItemBuilderMwh
13
13
  sale_price: jd_price,
14
14
  sale_start_at: listing.sale_start_at,
15
15
  sale_end_at: listing.sale_end_at,
16
- quantity: available_quantity + reserved_stock.to_i,
16
+ quantity: available_quantity,
17
17
  warehouse_id: wh_mapping(
18
18
  warehouse_space.warehouse_id
19
19
  )
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/simple/base'
6
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Simple
9
9
  class LazadaService < Base
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes/simple_service'
5
- module ItemBuilderMwh
5
+ class ItemBuilderMwh
6
6
  module Modes
7
7
  module Simple
8
8
  class SalePricePolicy
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/simple/base'
6
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Simple
9
9
  class ShopeeService < Base
@@ -13,7 +13,7 @@ module ItemBuilderMwh
13
13
  sale_price: listing.sale_price,
14
14
  sale_start_at: listing.sale_start_at,
15
15
  sale_end_at: listing.sale_end_at,
16
- quantity: available_quantity + reserved_stock.to_i,
16
+ quantity: available_quantity,
17
17
  warehouse_id: wh_mapping(
18
18
  warehouse_space.warehouse_id
19
19
  )
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/simple/base'
6
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Simple
9
9
  class ShopifyService < Base
@@ -13,7 +13,7 @@ module ItemBuilderMwh
13
13
  sale_price: sale_price,
14
14
  sale_start_at: listing.sale_start_at,
15
15
  sale_end_at: listing.sale_end_at,
16
- quantity: available_quantity + reserved_stock.to_i,
16
+ quantity: available_quantity,
17
17
  warehouse_id: wh_mapping(
18
18
  warehouse_space.warehouse_id
19
19
  )
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require 'item_builder_mwh/modes.rb'
5
5
  require 'item_builder_mwh/modes/simple/base'
6
- module ItemBuilderMwh
6
+ class ItemBuilderMwh
7
7
  module Modes
8
8
  module Simple
9
9
  class ZaloraService < Base
@@ -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
- module ItemBuilderMwh
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
- available_qty = warehouse_space.quantity
54
- qty(available_qty).merge(
51
+ qty = warehouse_space.quantity
52
+ qty(qty).merge(
55
53
  price
56
54
  )
57
55
  end
58
56
 
59
- def qty(available_qty)
57
+ def qty(qty)
60
58
  if quantity_name.empty?
61
59
  {
62
- quantity: available_qty
60
+ quantity: available_quantity(qty)
63
61
  }
64
62
  else
65
63
  {
66
- quantity: qty_channel(available_qty)
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, available_qty).perform
85
+ simple_channel_service.new(listing).perform
88
86
  end
89
87
 
90
- def qty_channel(available_qty)
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, available_qty).perform
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ItemBuilderMwh
4
- VERSION = '0.1.7'
3
+ class ItemBuilderMwh
4
+ VERSION = '0.1.12'
5
5
  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.7
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Ivander
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-05 00:00:00.000000000 Z
11
+ date: 2021-02-16 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,7 @@ 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
- - lib/item_builder_mwh/modes/quantity/shopee_service.rb
134
133
  - lib/item_builder_mwh/modes/quantity/zalora_service.rb
135
134
  - lib/item_builder_mwh/modes/quantity_service.rb
136
135
  - lib/item_builder_mwh/modes/simple/base.rb
@@ -144,10 +143,10 @@ files:
144
143
  - lib/item_builder_mwh/modes/simple/zalora_service.rb
145
144
  - lib/item_builder_mwh/modes/simple_service.rb
146
145
  - lib/item_builder_mwh/version.rb
147
- homepage:
146
+ homepage:
148
147
  licenses: []
149
148
  metadata: {}
150
- post_install_message:
149
+ post_install_message:
151
150
  rdoc_options: []
152
151
  require_paths:
153
152
  - lib
@@ -163,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
162
  version: '0'
164
163
  requirements: []
165
164
  rubygems_version: 3.0.6
166
- signing_key:
165
+ signing_key:
167
166
  specification_version: 4
168
167
  summary: Item Builder Multiwarehouse
169
168
  test_files: []
@@ -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
@@ -1,14 +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 ShopeeService < Base
8
- def perform
9
- available_quantity + reserved_stock.to_i
10
- end
11
- end
12
- end
13
- end
14
- end