2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: traitements
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# instructeur_email :string
|
|
|
|
# motivation :string
|
2021-08-18 11:19:19 +02:00
|
|
|
# process_expired :boolean
|
2020-08-06 16:35:45 +02:00
|
|
|
# processed_at :datetime
|
|
|
|
# state :string
|
|
|
|
# dossier_id :bigint
|
|
|
|
#
|
2020-07-02 11:02:50 +02:00
|
|
|
class Traitement < ApplicationRecord
|
2020-07-20 17:04:05 +02:00
|
|
|
belongs_to :dossier, optional: false
|
2020-07-06 22:13:00 +02:00
|
|
|
|
2021-11-04 19:05:04 +01:00
|
|
|
scope :en_construction, -> { where(state: Dossier.states.fetch(:en_construction)) }
|
|
|
|
scope :en_instruction, -> { where(state: Dossier.states.fetch(:en_instruction)) }
|
|
|
|
scope :termine, -> { where(state: Dossier::TERMINE) }
|
|
|
|
|
2021-06-16 15:13:36 +02:00
|
|
|
scope :for_traitement_time_stats, -> (procedure) do
|
2021-06-16 10:51:56 +02:00
|
|
|
includes(:dossier)
|
2021-11-04 19:05:04 +01:00
|
|
|
.termine
|
2021-06-16 10:51:56 +02:00
|
|
|
.where(dossier: procedure.dossiers)
|
|
|
|
.where.not('dossiers.en_construction_at' => nil, :processed_at => nil)
|
|
|
|
.order(:processed_at)
|
|
|
|
end
|
|
|
|
|
2021-11-19 12:36:03 +01:00
|
|
|
scope :termine_close_to_expiration, -> do
|
|
|
|
joins(dossier: :procedure)
|
|
|
|
.termine
|
|
|
|
.where(process_expired: true)
|
|
|
|
.where('dossiers.state' => Dossier::TERMINE)
|
|
|
|
.where("traitements.processed_at + (procedures.duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: Dossier::INTERVAL_BEFORE_EXPIRATION })
|
|
|
|
end
|
|
|
|
|
2021-05-26 18:17:07 +02:00
|
|
|
def self.count_dossiers_termines_by_month(groupe_instructeurs)
|
2021-05-26 15:41:45 +02:00
|
|
|
last_traitements_per_dossier = Traitement
|
|
|
|
.select('max(traitements.processed_at) as processed_at')
|
2021-11-04 19:05:04 +01:00
|
|
|
.termine
|
2021-06-17 21:57:52 +02:00
|
|
|
.where(dossier: Dossier.state_termine.where(groupe_instructeur: groupe_instructeurs))
|
2021-05-26 15:41:45 +02:00
|
|
|
.group(:dossier_id)
|
|
|
|
.to_sql
|
|
|
|
|
|
|
|
sql = <<~EOF
|
|
|
|
select date_trunc('month', r1.processed_at) as month, count(r1.processed_at)
|
|
|
|
from (#{last_traitements_per_dossier}) as r1
|
|
|
|
group by date_trunc('month', r1.processed_at)
|
2021-06-01 15:31:08 +02:00
|
|
|
order by month desc
|
2021-05-26 15:41:45 +02:00
|
|
|
EOF
|
|
|
|
|
|
|
|
ActiveRecord::Base.connection.execute(sql)
|
|
|
|
end
|
2020-07-02 11:02:50 +02:00
|
|
|
end
|