Class: Dscf::Marketplace::QuotationItem

Inherits:
ApplicationRecord show all
Defined in:
app/models/dscf/marketplace/quotation_item.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_associations(_auth_object = nil) ⇒ Object



33
34
35
# File 'app/models/dscf/marketplace/quotation_item.rb', line 33

def self.ransackable_associations(_auth_object = nil)
  %w[quotation rfq_item product unit]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object

Ransack configuration for secure filtering



29
30
31
# File 'app/models/dscf/marketplace/quotation_item.rb', line 29

def self.ransackable_attributes(_auth_object = nil)
  %w[id quotation_id rfq_item_id product_id unit_id quantity unit_price subtotal notes created_at updated_at]
end

Instance Method Details

#can_create_order?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/dscf/marketplace/quotation_item.rb', line 81

def can_create_order?
  quotation.sent? || quotation.accepted?
end

#matches_rfq_request?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
# File 'app/models/dscf/marketplace/quotation_item.rb', line 75

def matches_rfq_request?
  return false unless rfq_item

  product_id == rfq_item.product_id && unit_id == rfq_item.unit_id
end

#price_per_base_unitObject



69
70
71
72
73
# File 'app/models/dscf/marketplace/quotation_item.rb', line 69

def price_per_base_unit
  return nil unless unit_price && quantity && quantity > 0

  unit_price / quantity
end

#product_nameObject



37
38
39
# File 'app/models/dscf/marketplace/quotation_item.rb', line 37

def product_name
  product&.name
end

#product_skuObject



41
42
43
# File 'app/models/dscf/marketplace/quotation_item.rb', line 41

def product_sku
  product&.sku
end

#quantity_differenceObject



57
58
59
60
61
# File 'app/models/dscf/marketplace/quotation_item.rb', line 57

def quantity_difference
  return nil unless quantity && rfq_requested_quantity

  quantity - rfq_requested_quantity
end

#quantity_difference_percentageObject



63
64
65
66
67
# File 'app/models/dscf/marketplace/quotation_item.rb', line 63

def quantity_difference_percentage
  return nil unless quantity && rfq_requested_quantity && rfq_requested_quantity > 0

  ((quantity_difference.to_f / rfq_requested_quantity) * 100).round(2)
end

#rfq_requested_quantityObject



53
54
55
# File 'app/models/dscf/marketplace/quotation_item.rb', line 53

def rfq_requested_quantity
  rfq_item&.quantity
end

#subtotalObject

Override subtotal getter to calculate if not set



113
114
115
116
117
118
119
120
121
# File 'app/models/dscf/marketplace/quotation_item.rb', line 113

def subtotal
  stored_value = super
  # If we have a stored value (including 0), return it
  return stored_value unless stored_value.nil?
  # If no stored value but we have unit_price and quantity, calculate it
  return (unit_price * quantity).round(2) if unit_price && quantity
  # Otherwise return nil
  nil
end

#subtotal=(value) ⇒ Object

Override subtotal setter to allow validation to work



106
107
108
109
# File 'app/models/dscf/marketplace/quotation_item.rb', line 106

def subtotal=(value)
  # Allow any value to be set for validation purposes
  super
end

#unit_codeObject



49
50
51
# File 'app/models/dscf/marketplace/quotation_item.rb', line 49

def unit_code
  unit&.code
end

#unit_nameObject



45
46
47
# File 'app/models/dscf/marketplace/quotation_item.rb', line 45

def unit_name
  unit&.name
end