class PShipTourTruck < ApplicationRecord belongs_to :p_ship_tour belongs_to :p_truck belongs_to :p_driver has_many :p_ship_spaces, :dependent => :destroy accepts_nested_attributes_for :p_ship_spaces, allow_destroy: true has_many :p_documents validates :p_truck_id, :presence => true #validates :comment, :presence => true belongs_to :p_ship_tour_truck has_many :p_ship_tour_trucks, :dependent => :destroy has_many :p_sheet_lines, :through => :p_ship_tour_truck_sheet_lines has_many :p_ship_tour_truck_sheet_lines, :dependent => :destroy accepts_nested_attributes_for :p_ship_tour_truck_sheet_lines, allow_destroy: true has_many :p_volucompteurs, :dependent => :destroy accepts_nested_attributes_for :p_volucompteurs, allow_destroy: true validates_associated :p_volucompteurs before_validation do #errors.add(:created_at, 'ShipTourTruck :'+self.volume_total_th_ok.to_f.to_s) end def member_label self.p_truck.name end def ok? if !self.sheet_lines_oks? return false elsif !self.volucompteur_ok? return false else return true end end def volucompteur_ok? if (!self.tracteur_id and !self.p_volucompteurs.first) or (!self.tracteur_id and !self.p_volucompteurs.first.valid?) return false else return true end end def sheet_lines_oks? r = true self.p_ship_tour_truck_sheet_lines.each do |pss| if !pss.ok? r = false break end end return r end after_validation do self.p_volucompteurs.each do |child| # child.valid? end end def volume_total_th var = 0.0 volumes_th.each do |index, value| var += value.to_f end return var end def volume_total_th_ok var = 0.0 self.volumes_th_ok.each do |index, value| var += value.to_f end return var end def volumes_th var = {} self.p_sheet_lines.each do |p_sheet_line| var[p_sheet_line.p_product_id.to_s] = var[p_sheet_line.p_product_id.to_s].to_f + p_sheet_line.qte end self.p_ship_tour_trucks.each do |p_ship_tour_truck| p_ship_tour_truck.p_sheet_lines.each do |p_sheet_line| var[p_sheet_line.p_product_id.to_s] = var[p_sheet_line.p_product_id.to_s].to_f + p_sheet_line.qte end end return var end def volumes_th_ok var = {} self.p_ship_tour_truck_sheet_lines.each do |p_ship_tour_truck_sheet_line| var[p_ship_tour_truck_sheet_line.p_sheet_line.p_product_id.to_s] = var[p_ship_tour_truck_sheet_line.p_sheet_line.p_product_id.to_s].to_f + p_ship_tour_truck_sheet_line.qte_shiped.to_f puts "CCCC"+p_ship_tour_truck_sheet_line.qte_shiped.to_f.to_s end self.p_ship_tour_trucks.each do |p_ship_tour_truck| p_ship_tour_truck.p_ship_tour_truck_sheet_lines.each do |p_ship_tour_truck_sheet_line| var[p_ship_tour_truck_sheet_line.p_sheet_line.p_product_id.to_s] = var[p_ship_tour_truck_sheet_line.p_sheet_line.p_product_id.to_s].to_f + p_ship_tour_truck_sheet_line.qte_shiped.to_f puts "CCCC"+p_ship_tour_truck_sheet_line.qte_shiped.to_f.to_s end end return var end before_validation do self.set_remorques end after_create do self.set_remorques end def set_remorques # dfdsf = sdffsd if self.tracteur_id and self.p_ship_tour t = self.p_ship_tour.p_ship_tour_trucks.where(:p_truck_id => self.tracteur_id).first if t self.p_ship_tour_truck_id = t.id end end #self.remorque = self.p_truck.remorque if self.p_truck end end