item_builder 0.1.4 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/lib/item_builder.rb +2 -1
  3. data/lib/item_builder/get_quantity_service.rb +141 -0
  4. data/lib/item_builder/modes.rb +1 -0
  5. data/lib/item_builder/modes/active_service.rb +8 -1
  6. data/lib/item_builder/modes/price/base.rb +26 -0
  7. data/lib/item_builder/modes/price/blibli_service.rb +26 -0
  8. data/lib/item_builder/modes/price/bukalapak_service.rb +60 -0
  9. data/lib/item_builder/modes/price/jd_service.rb +30 -0
  10. data/lib/item_builder/modes/price/sale_price_policy.rb +40 -0
  11. data/lib/item_builder/modes/price/shopify_service.rb +26 -0
  12. data/lib/item_builder/modes/price/zalora_service.rb +26 -0
  13. data/lib/item_builder/modes/price_service.rb +37 -6
  14. data/lib/item_builder/modes/quantity/base.rb +75 -0
  15. data/lib/item_builder/modes/quantity/blibli_service.rb +51 -0
  16. data/lib/item_builder/modes/quantity/lazada_service.rb +14 -0
  17. data/lib/item_builder/modes/quantity/shopee_service.rb +14 -0
  18. data/lib/item_builder/modes/quantity/zalora_service.rb +14 -0
  19. data/lib/item_builder/modes/quantity_service.rb +44 -1
  20. data/lib/item_builder/modes/update/base.rb +30 -0
  21. data/lib/item_builder/modes/update/blibli_service.rb +11 -0
  22. data/lib/item_builder/modes/update/bukalapak_service.rb +11 -0
  23. data/lib/item_builder/modes/update/jd_service.rb +11 -0
  24. data/lib/item_builder/modes/update/lazada_service.rb +11 -0
  25. data/lib/item_builder/modes/update/shopify_service.rb +11 -0
  26. data/lib/item_builder/modes/update/tokopedia_service.rb +62 -0
  27. data/lib/item_builder/modes/update/zalora_service.rb +11 -0
  28. data/lib/item_builder/modes/update_service.rb +56 -0
  29. data/lib/item_builder/version.rb +1 -1
  30. metadata +26 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 149dad9c254c3b2318bf2b34aac1cb8788028b5006a01722d1d119f794e95963
4
- data.tar.gz: 40bf302606d6d08a0fae8be2b3eee65ac0126c88cb0883cd88a883aea7a93459
3
+ metadata.gz: bce91d8493302cb16b40cb46855412d7b1cf9378ac39791c7806379c00419602
4
+ data.tar.gz: 0ba73ba00283255f10b25541ae4d3c8a998135fe7d6059341a187fc164ebfe73
5
5
  SHA512:
6
- metadata.gz: 89afe48d4af43f64d63cfd0f9237979ba60e60842e6a4444003dc3f9b8f4df4834c3d38fbd2bcc1d763f3ab68dcc1ab2f02b683e87dfe28dd939e45b074c8c05
7
- data.tar.gz: a2b6664b091533401982e7edcb337c4f32a4f877c7dcebdfba6cf4ea034da69cec72dd0526732684381a35cce49b2f57427c71e928b5df58e200502022f699fb
6
+ metadata.gz: 8145858b7f105fb0dfb0ab7fc58e982e4efa0e4f095b03b33112800d3d8bd8a09f25e223121ca307f399a737dd855a0e3b75ab50c6dbc2ebfd2d9a21721f2adb
7
+ data.tar.gz: 97088a338d35d90b3cf2b8ccd80acaf7039beea0db0c13dbaf2e4cd32de14b9d5faca7aed082d2e4ff50ba01c7d35c71aec2bfe7ddedeb297a4106c8eab9b503
@@ -22,7 +22,8 @@ module ItemBuilder
22
22
  price: ItemBuilder::Modes::PriceService,
23
23
  quantity: ItemBuilder::Modes::QuantityService,
24
24
  simple: ItemBuilder::Modes::SimpleService,
25
- active: ItemBuilder::Modes::ActiveService
25
+ active: ItemBuilder::Modes::ActiveService,
26
+ update: ItemBuilder::Modes::UpdateService
26
27
  }
27
28
  end
28
29
  end
@@ -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
@@ -5,6 +5,7 @@ require 'item_builder/modes/price_service'
5
5
  require 'item_builder/modes/quantity_service'
6
6
  require 'item_builder/modes/simple_service'
7
7
  require 'item_builder/modes/active_service'
8
+ require 'item_builder/modes/update_service'
8
9
  module ItemBuilder
9
10
  module Modes
10
11
  attr_reader :listing
@@ -12,9 +12,16 @@ module ItemBuilder
12
12
 
13
13
  def to_h
14
14
  {
15
- active: listing.active
15
+ active: listing.active,
16
+ active_variant: active_variant.count
16
17
  }
17
18
  end
19
+
20
+ def active_variant
21
+ VariantListing
22
+ .where(local_item_id: listing.local_item_id,
23
+ active: 1)
24
+ end
18
25
  end
19
26
  end
20
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,22 +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
23
  to_h.merge(base)
11
24
  end
12
25
 
13
26
  def to_h
14
- {
15
- price: listing.price,
16
- sale_price: listing.sale_price,
17
- sale_start_at: listing.sale_start_at,
18
- sale_end_at: listing.sale_end_at
19
- }
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
47
+ end
48
+
49
+ def channel_name
50
+ PRICE_CHANNEL[listing.channel_id].to_s
20
51
  end
21
52
  end
22
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
+ 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
@@ -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
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes.rb'
4
+ module ItemBuilder
5
+ module Modes
6
+ module Update
7
+ class Base
8
+ attr_reader :listing
9
+ attr_reader :item_listing
10
+
11
+ def initialize(listing, item_listing)
12
+ raise 'listing is not set' if listing.nil?
13
+
14
+ raise 'item listing is not set' if item_listing.nil?
15
+
16
+ @listing = listing
17
+ @item_listing = item_listing
18
+ end
19
+
20
+ def base
21
+ {
22
+ name: listing.name,
23
+ minimum_order: listing.minimum_order,
24
+ package_weight: listing.package_weight
25
+ }
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes/update/base'
4
+ module ItemBuilder
5
+ module Modes
6
+ module Update
7
+ class BlibliService < Base
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes/update/base'
4
+ module ItemBuilder
5
+ module Modes
6
+ module Update
7
+ class BukalapakService < Base
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes/update/base'
4
+ module ItemBuilder
5
+ module Modes
6
+ module Update
7
+ class JdService < Base
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes/update/base'
4
+ module ItemBuilder
5
+ module Modes
6
+ module Update
7
+ class LazadaService < Base
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes/update/base'
4
+ module ItemBuilder
5
+ module Modes
6
+ module Update
7
+ class ShopifyService < Base
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes/update/base'
4
+ module ItemBuilder
5
+ module Modes
6
+ module Update
7
+ class TokopediaService < Base
8
+ def perform
9
+ tokopedia_attr.merge(base)
10
+ end
11
+
12
+ def tokopedia_attr
13
+ {
14
+ condition: (listing.new_condition ? 'NEW' : 'USED')
15
+ }
16
+ end
17
+
18
+ def possible_variant_options
19
+ item_listing.item_listing_custom_fields
20
+ .where.not(value: '') # prevent unselected/empty options
21
+ .group_by(&:tokopedia_custom_field_id)
22
+ .each_with_index.map {|variant_custom_fields, index1|
23
+ variant_custom_field_sample = variant_custom_fields.first
24
+ selected_tokopedia_custom_field = tokopedia_variant(variant_custom_field_sample)
25
+ {
26
+ v: variant_custom_field_sample.tokopedia.local_id.to_i,
27
+ vu: selected_tokopedia_custom_field['unit_id'],
28
+ pos: index1 + 1,
29
+ opt: variant_custom_fields.pluck(:value).uniq.map {|custom_field_value|
30
+ # handle variant with manual value (without options)
31
+ if variant_custom_field_sample.tokopedia.input_type == 'text'
32
+ {
33
+ value: custom_field_value,
34
+ t_id: custom_field_value.bytes.sum, # set value act as option id
35
+ cstm: ''
36
+ }
37
+ else
38
+ custom_field_option = variant_custom_field_sample.tokopedia.options
39
+ .find_by(name: custom_field_value)
40
+ option_value =
41
+ if selected_tokopedia_custom_field['unit_id'] == 0
42
+ custom_field_option.name
43
+ else
44
+ custom_field_option.name
45
+ .split(" #{selected_tokopedia_custom_field['unit']}")
46
+ .first
47
+ end
48
+ {
49
+ vuv: custom_field_option.local_id, # variant unit value id
50
+ value: option_value, # original option value
51
+ t_id: custom_field_option.id,
52
+ cstm: ''
53
+ }
54
+ end
55
+ }
56
+ }
57
+ }
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes/update/base'
4
+ module ItemBuilder
5
+ module Modes
6
+ module Update
7
+ class ZaloraService < Base
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes.rb'
4
+ require 'item_builder/modes/update/blibli_service'
5
+ require 'item_builder/modes/update/bukalapak_service'
6
+ require 'item_builder/modes/update/lazada_service'
7
+ require 'item_builder/modes/update/zalora_service'
8
+ require 'item_builder/modes/update/shopify_service'
9
+ require 'item_builder/modes/update/jd_service'
10
+ require 'item_builder/modes/update/tokopedia_service'
11
+ module ItemBuilder
12
+ module Modes
13
+ class UpdateService
14
+ include Modes
15
+
16
+ UPDATE_CHANNEL = {}.tap do |hash|
17
+ hash[2] = :Shopify
18
+ hash[3] = :Lazada
19
+ hash[4] = :Bhinneka
20
+ hash[5] = :Blanja
21
+ hash[9] = :Blibli
22
+ hash[11] = :Bukalapak
23
+ hash[12] = :Shopee
24
+ hash[13] = :Zalora
25
+ hash[15] = :Tokopedia
26
+ hash[16] = :Jd
27
+ end.freeze
28
+
29
+ def perform
30
+ to_h.merge(base)
31
+ end
32
+
33
+ def to_h
34
+ simple.merge(update)
35
+ end
36
+
37
+ def update
38
+ class_name = "ItemBuilder::Modes::Update::#{channel_name}Service"
39
+ update_channel_service = class_name.constantize
40
+ update_channel_service.new(listing, item_listing).perform
41
+ end
42
+
43
+ def channel_name
44
+ UPDATE_CHANNEL[listing.channel_id].to_s
45
+ end
46
+
47
+ def simple
48
+ SimpleService.new(listing: listing).to_h
49
+ end
50
+
51
+ def item_listing
52
+ @item_listing ||= ItemListing.find(listing.channel_association_id)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ItemBuilder
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.9'
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.4
4
+ version: 0.1.9
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-05 00:00:00.000000000 Z
11
+ date: 2020-08-05 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,12 +116,34 @@ 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
138
+ - lib/item_builder/modes/update/base.rb
139
+ - lib/item_builder/modes/update/blibli_service.rb
140
+ - lib/item_builder/modes/update/bukalapak_service.rb
141
+ - lib/item_builder/modes/update/jd_service.rb
142
+ - lib/item_builder/modes/update/lazada_service.rb
143
+ - lib/item_builder/modes/update/shopify_service.rb
144
+ - lib/item_builder/modes/update/tokopedia_service.rb
145
+ - lib/item_builder/modes/update/zalora_service.rb
146
+ - lib/item_builder/modes/update_service.rb
125
147
  - lib/item_builder/version.rb
126
148
  homepage:
127
149
  licenses: []