Skip to content

Commit

Permalink
don't merge: payment script
Browse files Browse the repository at this point in the history
  • Loading branch information
freesteph committed Apr 4, 2024
1 parent dc94cc6 commit 85bb326
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Student < ApplicationRecord

scope :without_ribs, -> { where.missing(:rib) }

scope :lives_in_france, -> { where(address_country_code: %w[100 99100]) }

scope :asp_ready, lambda {
where(biological_sex: [1, 2])
.where.not(address_postal_code: nil)
Expand Down
82 changes: 82 additions & 0 deletions lib/tasks/payments.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

OUTSIDE_CONTRACT = [
%w[0382170C 2472000831],
%w[0690652J 2472340732],
%w[0690652J 2472340733],
%w[0690652J 2412010122],
%w[0690652J 2412344721],
%w[0690652J 2412344722],
%w[0690652J 2412521921],
%w[0690652J 2412521922],
%w[0690652J 2412543422],
%w[0691875N 2473360333],
%w[0691875N 2473360531],
%w[0691875N 2473360632],
%w[0691875N 2413361521],
%w[0691875N 2413361522],
%w[0442083A 2473121131],
%w[0442083A 2473121332],
%w[0442083A 2473121333],
%w[0442227G 2403320511],
%w[0910838S 2473000433],
%w[0910838S 2473121432]
].freeze

PRIVATE_HEALTH_ESTABLISHMENTS = %w[0541769E 0010212A 0930075B].freeze

class Student
scope :lives_in_france, -> { where(address_country_code: %w[100 99100]) }
end

def select_perfect_pfmps
perfect = []

Pfmp
.joins(Pfmp.most_recent_transition_join)
.joins(:payment_requests, :schooling, :establishment, student: :rib)
.where(start_date: Aplypro::SCHOOL_YEAR_RANGE, end_date: Aplypro::SCHOOL_YEAR_RANGE)
.order(end_date: :asc)
.merge(Schooling.with_attributive_decisions)
.merge(Student.lives_in_france)
.where.not("establishments.uai": PRIVATE_HEALTH_ESTABLISHMENTS)
.in_state(:validated)
.preload(:schooling, student: :rib)
.find_in_batches
.with_index do |pfmps, index|
puts "looking for perfect PFMPs in batch number #{index} (have #{perfect.count} perfect PFMPs so far)"

ready = pfmps
.select(&:valid?)
.reject { |pfmp| outside_contract?(pfmp) }
.reject { |pfmp| needs_abrogated_da?(pfmp) }
.each { |pfmp| grab_missing_status(pfmp) }
.select { |pfmp| pfmp.payment_requests.last.can_transition_to?(:ready) }

perfect.concat(ready)

break if perfect.count > 1000
end

puts "all good! pfmps: #{perfect.count}"
end

def outside_contract?(pfmp)
OUTSIDE_CONTRACT.any? { |uai, mef| pfmp.establishment.uai == uai && pfmp.mef.code == mef }
end

def grab_missing_status(pfmp)
return if pfmp.schooling.status.present?

puts "fetching student information again..."
FetchStudentInformationJob.perform_now(pfmp.schooling)
puts "done."
end

def needs_abrogated_da?(pfmp)
pfmp.student.pfmps.joins(:mef).select(:"mefs.id").distinct.count > 1 &&
pfmp.student.schoolings.joins(:classe).select(:"establishment_id").distinct.count > 1

# # plusieurs schoolings même MEF dans le même étab = plusieurs DA = bon quand même
# pfmp.student.attributive_decisions.uniq.many?
end

0 comments on commit 85bb326

Please sign in to comment.