class PFournisseurOrder < ApplicationRecord belongs_to :p_fournisseur has_many :p_product_brut_stocks, :dependent => :destroy accepts_nested_attributes_for :p_product_brut_stocks before_save do tot = 0.0 tot_ttc = 0.0 tot_ok = 0.0 tot_ttc_ok = 0.0 self.p_product_brut_stocks.all.each do |ppbs| tot += ppbs.tot tot_ttc += ppbs.tot_ttc tot_ok += ppbs.tot_ok tot_ttc_ok += ppbs.tot_ttc_ok end self.a_ok_total_ttc = tot_ttc_ok self.a_ok_total_ht = tot_ok self.a_ok_tva = (tot_ok*0.2).round(2) self.a_total_ttc = tot_ttc self.a_total_ht = tot self.a_tva = (tot*0.2).round(2) end end