item_builder_mwh 0.1.6 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) 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 +26 -0
  8. data/lib/item_builder_mwh/modes/price/blibli_service.rb +27 -0
  9. data/lib/item_builder_mwh/modes/price/bukalapak_service.rb +61 -0
  10. data/lib/item_builder_mwh/modes/price/jd_service.rb +31 -0
  11. data/lib/item_builder_mwh/modes/price/sale_price_policy.rb +40 -0
  12. data/lib/item_builder_mwh/modes/price/shopify_service.rb +28 -0
  13. data/lib/item_builder_mwh/modes/price/zalora_service.rb +27 -0
  14. data/lib/item_builder_mwh/modes/price_service.rb +45 -5
  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 +21 -11
  19. data/lib/item_builder_mwh/modes/simple/base.rb +74 -0
  20. data/lib/item_builder_mwh/modes/{quantity → simple}/blibli_service.rb +19 -4
  21. data/lib/item_builder_mwh/modes/simple/bukalapak_service.rb +69 -0
  22. data/lib/item_builder_mwh/modes/simple/jd_service.rb +35 -0
  23. data/lib/item_builder_mwh/modes/simple/lazada_service.rb +25 -0
  24. data/lib/item_builder_mwh/modes/simple/sale_price_policy.rb +39 -0
  25. data/lib/item_builder_mwh/modes/simple/shopee_service.rb +25 -0
  26. data/lib/item_builder_mwh/modes/simple/shopify_service.rb +31 -0
  27. data/lib/item_builder_mwh/modes/simple/zalora_service.rb +31 -0
  28. data/lib/item_builder_mwh/modes/simple_service.rb +93 -8
  29. data/lib/item_builder_mwh/version.rb +2 -2
  30. metadata +23 -8
  31. 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: c514a7575658fabe86a470f41cbb2f35802235c1143ff6baacbba50084b6e31c
4
- data.tar.gz: 913dc65e2b64637db18ca789f6d4929b22001fce2e01b2ecc79bbf3d0deff07d
3
+ metadata.gz: 89d5e20049b164c7a4cc3e29f928a4518378294d36aeb683f0a4af884dd5ca60
4
+ data.tar.gz: 49bf961845d57d9af626bf10904dd81b142c049c7515bc8145488a7cf6950fd8
5
5
  SHA512:
6
- metadata.gz: 51810b83e87bf09c38f234eaa2071ac23023215d3a232c6a796d8984871edf80395a33f322c830601990d44ee524456ebcf5dd15309695142b0344de5f80f832
7
- data.tar.gz: 7a6c1f71bd00d768f314ed895ec7770323152bc7ac1567aea9ee022e6948cf565c93adfe95e7ef84d244af5719552f665361727c4baf9fb8960addf3c96a2c70
6
+ metadata.gz: 1afa288d9ba3c158798fb6a09a9b5e76fd04515fc81eb97e81e6ddcd37222a5094ee912a06fddc769aa27e17c09be601be51803a7e79b3b17bd8ea06a613cf82
7
+ data.tar.gz: 69c88f5ccdfa9672745822f683467147d43155cfaafed58e1ad4b56b5e5acec583e514e864019b525b495cc1e469c94852dfbcfc96a0e2e10d91f3c86e2a8630
@@ -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
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'item_builder_mwh/modes.rb'
5
+ require 'item_builder_mwh/modes/price_service'
6
+ require 'item_builder_mwh/modes/price/sale_price_policy'
7
+ class ItemBuilderMwh
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
+ ItemBuilderMwh::Modes::Price::SalePricePolicy.new(listing: listing)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'item_builder_mwh/modes.rb'
5
+ require 'item_builder_mwh/modes/price/base'
6
+ class ItemBuilderMwh
7
+ module Modes
8
+ module Price
9
+ class BlibliService < Base
10
+ def perform
11
+ {
12
+ price: listing.price,
13
+ sale_price: sale_price,
14
+ sale_start_at: listing.sale_start_at,
15
+ sale_end_at: listing.sale_end_at
16
+ }
17
+ end
18
+
19
+ private
20
+
21
+ def sale_price
22
+ sale_price_policy.on_sale? ? listing.sale_price : listing.price
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'item_builder_mwh/modes.rb'
5
+ require 'item_builder_mwh/modes/price/base'
6
+ class ItemBuilderMwh
7
+ module Modes
8
+ module Price
9
+ class BukalapakService < Base
10
+ def perform
11
+ if listing.sale_price.nil?
12
+ bukalapak_price
13
+ else
14
+ deal
15
+ end
16
+ end
17
+
18
+ def bukalapak_price
19
+ {
20
+ price: listing.price,
21
+ sale_price: listing.sale_price,
22
+ sale_start_at: listing.sale_start_at,
23
+ sale_end_at: listing.sale_end_at
24
+ }
25
+ end
26
+
27
+ def deal
28
+ {
29
+ percentage: deal_percentage,
30
+ sale_start_at: sale_start_at,
31
+ sale_end_at: sale_end_at,
32
+ price: listing.price,
33
+ sale_price: listing.sale_price
34
+ }
35
+ end
36
+
37
+ def deal_percentage
38
+ price = listing.price.to_f
39
+ sale_price = listing.sale_price.to_f
40
+ 100 - (sale_price / price * 100)
41
+ end
42
+
43
+ def sale_start_at
44
+ return DateTime.now if listing.sale_start_at.nil?
45
+ return DateTime.now if listing.sale_start_at < DateTime.now
46
+
47
+ listing.sale_start_at
48
+ end
49
+
50
+ def sale_end_at
51
+ month_later = DateTime.now + 1.month
52
+ return month_later if listing.sale_end_at.nil?
53
+ return month_later if listing.sale_end_at > month_later
54
+ return month_later if listing.sale_end_at < DateTime.now
55
+
56
+ listing.sale_end_at
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end