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.



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

def initialize(args)
  @variant = args[:variant]
  @warehouse_space =  args[:warehouse_space]
  @listing =  args[:listing]
  @inventory_v2 = args[:inventory_v2]
end

Instance Attribute Details

#inventory_v2Object (readonly)

Returns the value of attribute inventory_v2.



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

def inventory_v2
  @inventory_v2
end

#listingObject (readonly)

Returns the value of attribute listing.



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

def listing
  @listing
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.



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

def warehouse_space
  @warehouse_space
end

Instance Method Details

#bundle_variantsObject



80
81
82
# File 'lib/tiny_builder/quantity_counter.rb', line 80

def bundle_variants
  @bundle_variants ||= search_bundle_variants
end

#get_bundle_wh_space(item_variant_id) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/tiny_builder/quantity_counter.rb', line 69

def get_bundle_wh_space(item_variant_id)
  if inventory_v2
    MongoWarehouseSpace.find_by(item_variant_id: item_variant_id, warehouse_id: warehouse_space&.old_warehouse_id)
  else
    WarehouseSpace.find_by(
      item_variant_id: item_variant_id,
      warehouse_id: warehouse_space&.warehouse_id
    )
  end
end

#locked_qty(wh_space) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/tiny_builder/quantity_counter.rb', line 45

def locked_qty(wh_space)
  return 0 unless inventory_v2
  return 0 unless wh_space

  wh_space.promo_list.select do |promo|
    promo["local_id"] == listing.local_id
  end.sum{ |promo| promo["quantity"] }
end

#performObject



21
22
23
24
25
# File 'lib/tiny_builder/quantity_counter.rb', line 21

def perform
  return 0 unless listing.active
  
  warehouse_stock
end

#qty_bundleObject



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

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

#qty_default(wh_space) ⇒ Object



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

def qty_default(wh_space)
  return [(wh_space&.quantity || 0), 0].max unless inventory_v2

  qty = (wh_space&.available_quantity || 0) + locked_qty(wh_space)
  [qty, 0].max
end

#search_bundle_variantsObject



84
85
86
87
88
# File 'lib/tiny_builder/quantity_counter.rb', line 84

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



27
28
29
30
31
32
33
34
35
36
# File 'lib/tiny_builder/quantity_counter.rb', line 27

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