class Dscf::Marketplace::QuotationItem
def self.ransackable_associations(_auth_object = nil)
def self.ransackable_associations(_auth_object = nil) %w[quotation rfq_item product unit] end
def self.ransackable_attributes(_auth_object = nil)
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
def calculate_subtotal
def calculate_subtotal return unless unit_price && quantity self.subtotal = (unit_price * quantity).round(2) end
def calculated_subtotal
def calculated_subtotal return subtotal if subtotal.present? return nil unless unit_price && quantity (unit_price * quantity).round(2) end
def can_create_order?
def can_create_order? quotation.sent? || quotation.accepted? end
def matches_rfq_request?
def matches_rfq_request? return false unless rfq_item product_id == rfq_item.product_id && unit_id == rfq_item.unit_id end
def price_per_base_unit
def price_per_base_unit return nil unless unit_price && quantity && quantity > 0 unit_price / quantity end
def product_matches_rfq_item
def product_matches_rfq_item return unless rfq_item_id && product_id unless product_id == rfq_item.product_id errors.add(:product_id, "must match the product in the RFQ item") end end
def product_name
def product_name product&.name end
def product_sku
def product_sku product&.sku end
def quantity_difference
def quantity_difference return nil unless quantity && rfq_requested_quantity quantity - rfq_requested_quantity end
def quantity_difference_percentage
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
def rfq_requested_quantity
def rfq_requested_quantity rfq_item&.quantity end
def should_recalculate_subtotal?
def should_recalculate_subtotal? unit_price_changed? || quantity_changed? end
def should_validate_subtotal?
def should_validate_subtotal? # Only validate if subtotal is explicitly set and we have unit_price and quantity unit_price.present? && quantity.present? && subtotal.present? && subtotal_changed? end
def subtotal
def subtotal stored_value = super # If we have a stored value (including 0), return it # Only calculate if it's nil and we have the required fields return stored_value unless stored_value.nil? return nil unless unit_price && quantity (unit_price * quantity).round(2) end
def subtotal=(value)
def subtotal=(value) # Allow any value to be set for validation purposes super end
def subtotal_matches_calculation
def subtotal_matches_calculation return unless should_validate_subtotal? # Calculate what the subtotal should be based on current unit_price and quantity expected_subtotal = (unit_price * quantity).round(2) # If subtotal is stored and doesn't match, that's an error # If subtotal is nil, we'll let the before_save callback handle it if subtotal.present? && subtotal.round(2) != expected_subtotal errors.add(:subtotal, "must equal unit_price * quantity") end end
def unit_code
def unit_code unit&.code end
def unit_compatible_with_product
def unit_compatible_with_product return unless product_id && unit_id unless product.unit_id == unit_id errors.add(:unit_id, "must match the product's base unit") end end
def unit_name
def unit_name unit&.name end