vulco_app/app/models/v_contact.rb
Nicolas Bally 35dc99e026 suite
2020-10-14 00:19:47 +02:00

62 lines
1.6 KiB
Ruby

class VContact < ApplicationRecord
def volumes
VolumePeriodique.where(:codemanaginn => self.codemanaginn, :codeindicateur => "MGI000000001").order("datereception DESC")
end
def self.update_mvt
vc_ids = VolumePeriodique.group(:codemanaginn).map{|a| a.codemanaginn}
VContact.where(:enabled => true).where("codemanaginn not in(?)", vc_ids).update_all(:nbr_mvt => 0)
return true
end
def self.update_caches(force = false)
@gen_past_time = Time.now
if !force
v_contacts = VContact.where(:enabled => true, :nbr_mvt => nil)
else
v_contacts = VContact.where(:enabled => true)
end
#v_contacts = v_contacts.limit(2)
v_contacts.each do |vc|
puts vc.codemanaginn
vc.cumul = vc.volumes.select("valeur","datereception").where("valeur > 0.0").sum(:valeur)
vc.cagnotte = vc.volumes.select("valeur","datereception").sum(:valeur)
vc.nbr_mvt = vc.volumes.count
vc.last_volume_at = vc.volumes.select("datereception").first.datereception if vc.volumes.first
vc.prime = (vc.ac_rate.to_f / 100.0) * vc.cagnotte
vc.save
end
to_delete = VContact.where("(last_volume_at is NULL and datecreation < ?) or (last_volume_at < ?)", (Date.today-5.year), (Date.today-5.year))
VolumePeriodique.where(:codemanaginn => to_delete.map{|a| a.codemanaginn}).destroy_all
to_delete.destroy_all
if @gen_past_time
return "Rendu en #{(Time.now - @gen_past_time).round(3)}s"
else
return false
end
end
end