item_builder 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce2ebd51f97f3d37ae9a8028f885108f544b0f970431bf47f762e0c0a80a7911
4
- data.tar.gz: 7f472d800918402e3b1fbe86a055e446e1a0a6fe6ebe7d268779c6aea203adaa
3
+ metadata.gz: 18a2372f40168e3e56f7412d031413a0b279cdf26230ce2245c4e77b69c6b3fb
4
+ data.tar.gz: 7d5a072c248a21cee2777a9da17b016d08deccf9bb0ca4e6acd1695f42f6a877
5
5
  SHA512:
6
- metadata.gz: dc967441831e0adb69cb77d9fcaa4be7a13faf6f1058f89f443004676aba085a32e8e8fe0d263ae5da11768cd78dff2671fad093d93345a592a6cfd9e8e67b22
7
- data.tar.gz: 70366f1bb87d059e34537a1723b5a6bfd04b6bfac9d964920c66a4beb1c31af404e195860e83eba93d9a2a30740f670b8bb12a107ed5f8e644fa31baf3c58d06
6
+ metadata.gz: f98e7fd12ab95bc9d7bd0d2b583f550bcdf6cdf2e5254e35baf63c90d562e99e1828732e540a101a76facced7540f2049264c77209c8b675dc3ebed13d05dfad
7
+ data.tar.gz: ef31996f4fcd1e475555219084dda218eaec248a3b6824f2faeb705a420fb3e031775ea4e1478594b27106c5b10a4b26996251c5d0192f0e824ffa1e65f70a49
@@ -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
@@ -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,49 @@
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
+ local_product['value']['items'].each do |item|
14
+ total_quantity(item) if item['item_sku'] == listing.local_id
15
+ end
16
+ end
17
+
18
+ def total_quantity(item)
19
+ # Make stock 0
20
+ # if local reserved quantity is bigger than available quantity
21
+ if item['reserved_stock_level2'] > available_quantity
22
+ 0
23
+ else
24
+ item['available_stock_level2'] + item['reserved_stock_level2']
25
+ end
26
+ end
27
+
28
+ # Find local product based on id
29
+ # @return [Hash] Local Blibli product
30
+ def local_product
31
+ @local_product ||= JSON.parse(apigateway_post)
32
+ end
33
+
34
+ private
35
+
36
+ def request
37
+ {
38
+ "gdnSku": listing.local_id
39
+ }
40
+ end
41
+
42
+ def url
43
+ url = ENV['API_GATEWAY_URL'] || 'apigateway.forstok.com'
44
+ url + '/blibli/item'
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes/quantity/base'
4
+ module ItemBuilder
5
+ module Modes
6
+ module Quantity
7
+ class LazadaService < Base
8
+ def perform
9
+ available_quantity + reserved_stock.to_i
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes/quantity/base'
4
+ module ItemBuilder
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
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes/quantity/base'
4
+ module ItemBuilder
5
+ module Modes
6
+ module Quantity
7
+ class ZaloraService < Base
8
+ def perform
9
+ available_quantity + reserved_stock.to_i
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -2,24 +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
24
  to_h.merge(base)
12
25
  end
13
26
 
14
27
  def to_h
15
28
  {
16
- quantity: warehouse.quantity
29
+ quantity: qty
17
30
  }
18
31
  end
19
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
+
20
59
  def warehouse
21
60
  WarehouseSpace.find_by(item_variant_id: listing.variant_id)
22
61
  end
62
+
63
+ def variant
64
+ @variant ||= Variant.find(listing.variant_id)
65
+ end
23
66
  end
24
67
  end
25
68
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ItemBuilder
4
- VERSION = '0.1.5'
4
+ VERSION = '0.1.6'
5
5
  end
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.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - okaaryanata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-08 00:00:00.000000000 Z
11
+ date: 2020-07-07 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.7
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.7
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,16 @@ 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
122
123
  - lib/item_builder/modes/price_service.rb
124
+ - lib/item_builder/modes/quantity/base.rb
125
+ - lib/item_builder/modes/quantity/blibli_service.rb
126
+ - lib/item_builder/modes/quantity/lazada_service.rb
127
+ - lib/item_builder/modes/quantity/shopee_service.rb
128
+ - lib/item_builder/modes/quantity/zalora_service.rb
123
129
  - lib/item_builder/modes/quantity_service.rb
124
130
  - lib/item_builder/modes/simple_service.rb
125
131
  - lib/item_builder/version.rb