module Comee module Core class SupplierInvoiceItem < ApplicationRecord before_save { self.total_price = unit_price * quantity } after_save :update_invoice_price belongs_to :purchase_order_item belongs_to :supplier_invoice belongs_to :unit validates :quantity, :unit_price, presence: true, numericality: {greater_than: 0} validates :total_price, numericality: {greater_than_or_equal_to: 0, allow_nil: true} def update_invoice_price supplier_invoice.total_price = supplier_invoice.supplier_invoice_items.sum(:total_price) supplier_invoice.save! end end end end