113 lines
2.4 KiB
Ruby
113 lines
2.4 KiB
Ruby
class PVolucompteur < ApplicationRecord
|
|
|
|
has_many :p_vol_excepts
|
|
accepts_nested_attributes_for :p_vol_excepts, allow_destroy: true
|
|
|
|
#validates_associated :p_vol_excepts
|
|
|
|
validates :p_truck_id, :presence => true
|
|
|
|
belongs_to :p_ship_tour_truck
|
|
#has_one :p_ship_tour, :through => :p_ship_tour_truck
|
|
attr_accessor :write_why, :check_delta
|
|
validates :start_vol, :presence => true
|
|
validates :end_vol, :presence => true
|
|
|
|
#validates :delta_cause, :presence => true, :if => :delta_needed?
|
|
before_validation do
|
|
|
|
#errors.add(:end_vol, 'Volucompteur :'+self.p_ship_tour_truck.volume_total_th_ok.to_f.to_s+" AAA "+self.prov_vol.to_s)
|
|
|
|
nbr_volucompteurs = 0
|
|
|
|
if self.id
|
|
nbr_volucompteurs = self.p_ship_tour_truck.p_volucompteurs.where("id != ?",self.id).count
|
|
elsif
|
|
nbr_volucompteurs = self.p_ship_tour_truck.p_volucompteurs.count
|
|
end
|
|
|
|
if nbr_volucompteurs > 0
|
|
errors.add(:end_vol, 'Le volumètre existe déjà')
|
|
end
|
|
|
|
|
|
|
|
|
|
errors.add(:end_vol, 'Vous devez justifier la différence entière') if !justif_ok?
|
|
|
|
true
|
|
end
|
|
|
|
|
|
def prov_vol
|
|
self.p_ship_tour_truck.volume_total_th_ok.to_f
|
|
end
|
|
|
|
def justif_ok?
|
|
puts "JJJJ"
|
|
puts total_justif.to_f
|
|
puts "JJJJ"
|
|
puts delta_calc_ok.to_f
|
|
puts "DDD"
|
|
#sdffsdfs = sdfsfd
|
|
|
|
if (total_justif.to_f) == delta_calc_ok.to_f
|
|
true
|
|
else
|
|
false
|
|
end
|
|
end
|
|
|
|
def total_justif
|
|
r = 0.0
|
|
|
|
self.p_vol_excepts.each do |pve|
|
|
#puts "TEST"
|
|
#puts pve.qte.to_f
|
|
#puts "AAA"
|
|
r += pve.qte.to_f if !pve.marked_for_destruction?
|
|
end
|
|
|
|
|
|
return r
|
|
end
|
|
|
|
after_validation do
|
|
if self.end_vol.to_f < self.start_vol.to_f
|
|
errors.add(:end_vol, 'doit être supérieur ou égal à la valeur de départ')
|
|
end
|
|
|
|
if delta_needed? and !justif_ok?
|
|
self.write_why = true
|
|
errors.add(:end_vol, 'Le volumètre ne correspond pas aux quantités (delta : '+delta_calc_ok.to_s+')')
|
|
end
|
|
|
|
true
|
|
end
|
|
|
|
def delta_needed?
|
|
if true
|
|
|
|
if delta_calc_ok != 0.0
|
|
true
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
def volume_used
|
|
self.end_vol.to_f - self.start_vol.to_f
|
|
end
|
|
|
|
def delta_calc
|
|
self.volume_used.to_f - self.p_ship_tour_truck.volume_total_th.to_f
|
|
end
|
|
|
|
def delta_calc_ok
|
|
self.volume_used.to_f - self.prov_vol.to_f
|
|
end
|
|
|
|
|
|
|
|
end
|