Class: TinyBuilder::QuantityCounter

Inherits:
Object
  • Object
show all
Includes:
AllocatedStock
Defined in:
lib/tiny_builder/quantity_counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AllocatedStock

#allocated_stock_active?, #count_allocated_stock, #host, #one_alloc_rsvd_stock, #params

Constructor Details

#initialize(args) ⇒ QuantityCounter

Returns a new instance of QuantityCounter.



13
14
15
16
17
18
# File 'lib/tiny_builder/quantity_counter.rb', line 13

def initialize(args)
  @variant = args[:variant]
  @stock_alloc = args[:stock_alloc]
  @associated_stock_allocs = args[:associated_stock_allocs]
  @warehouse_space =  args[:warehouse_space]
end

Instance Attribute Details

#associated_stock_allocsObject (readonly)

Returns the value of attribute associated_stock_allocs.



11
12
13
# File 'lib/tiny_builder/quantity_counter.rb', line 11

def associated_stock_allocs
  @associated_stock_allocs
end

#stock_allocObject (readonly)

Returns the value of attribute stock_alloc.



10
11
12
# File 'lib/tiny_builder/quantity_counter.rb', line 10

def stock_alloc
  @stock_alloc
end

#variantObject (readonly)

Returns the value of attribute variant.



9
10
11
# File 'lib/tiny_builder/quantity_counter.rb', line 9

def variant
  @variant
end

#warehouse_spaceObject (readonly)

Returns the value of attribute warehouse_space.



12
13
14
# File 'lib/tiny_builder/quantity_counter.rb', line 12

def warehouse_space
  @warehouse_space
end

Instance Method Details

#bundle_variantsObject



71
72
73
# File 'lib/tiny_builder/quantity_counter.rb', line 71

def bundle_variants
  @bundle_variants ||= search_bundle_variants
end

#performObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tiny_builder/quantity_counter.rb', line 20

def perform
  if allocated_stock_active?
    # yang masuk di kondisi ini,
    # artinya akun tersebut ada allocated stock yang aktif
    count_allocated_stock
  elsif associated_stock_allocs.present?
    # yang masuk di kondisi ini,
    # artinya akun tersebut tidak ada allocated stock yang aktif,
    # namun ada allocated stock yg aktif dari channel lain
    warehouse_stock - count_existing_alloc_stock + count_alloc_rsvd_stock
  else
    # yang masuk di kondisi ini,
    # artinya tidak ada allocated stock di item tersebut
    warehouse_stock
  end
end

#qty_bundleObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tiny_builder/quantity_counter.rb', line 52

def qty_bundle
  if bundle_variants.present?
    qty_list = []
    bundle_variants.each do |bv|
      wh_space = WarehouseSpace.find_by(
        item_variant_id: bv[:master_variant_id],
        warehouse_id: warehouse_space&.warehouse_id
      )
      qty = wh_space.quantity if wh_space.present?
      qty ||= 0
      qty = qty / bv[:bundle_attribute][:quantity]
      qty_list.push(qty)
    end
    [qty_list.min, 0].sort[1]
  else
    qty_default
  end
end

#qty_defaultObject



48
49
50
# File 'lib/tiny_builder/quantity_counter.rb', line 48

def qty_default
  [(warehouse_space&.quantity || 0), 0].max
end

#search_bundle_variantsObject



75
76
77
78
79
# File 'lib/tiny_builder/quantity_counter.rb', line 75

def search_bundle_variants
  bundle = MasterProduct.where("bundle_variants.master_variant_id": variant.id).last
  return [] unless bundle
  bundle.bundle_variants.select { |bv| !bv[:main] }
end

#warehouse_stockObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/tiny_builder/quantity_counter.rb', line 37

def warehouse_stock
  case variant.config.downcase
  when 'default'
    qty_default
  when 'free gift', 'combo'
    qty_bundle
  else
    raise "config not recognize => #{variant.config}"
  end
end