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?Boolean

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/tiny_builder/quantity_counter.rb', line 87

def bundle?
  product = MasterProduct.find_by("variants.id": variant.id)
  product.variants.find{|v| v["id"] == variant.id}["bundle_id"].present?
end

#bundle_variantsObject



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

def bundle_variants
  @bundle_variants ||= search_bundle_variants
end

#get_bundle_wh_space(item_variant_id) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/tiny_builder/quantity_counter.rb', line 66

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



42
43
44
45
46
47
48
49
# File 'lib/tiny_builder/quantity_counter.rb', line 42

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

  wh_space.promotions.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



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

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



35
36
37
38
39
40
# File 'lib/tiny_builder/quantity_counter.rb', line 35

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



81
82
83
84
85
# File 'lib/tiny_builder/quantity_counter.rb', line 81

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
# File 'lib/tiny_builder/quantity_counter.rb', line 27

def warehouse_stock
  if bundle?
    qty_bundle 
  else 
    qty_default(warehouse_space)
  end
end