diff --git a/lib/asp/entities/adresse.rb b/lib/asp/entities/adresse.rb deleted file mode 100644 index 533747adb..000000000 --- a/lib/asp/entities/adresse.rb +++ /dev/null @@ -1,58 +0,0 @@ -# frozen_string_literal: true - -module ASP - module Entities - class Adresse < Entity - attribute :codetypeadr, :string - attribute :codecominsee, :string - attribute :codeinseepays, :string - attribute :codepostalcedex, :string - - attribute :pointremise, :string - attribute :cpltdistribution, :string - - validates_presence_of %i[ - codetypeadr - codeinseepays - codepostalcedex - codecominsee - ] - - def fragment(xml) - xml.codetypeadr(codetypeadr) - xml.codeinseepays(codeinseepays) - xml.codepostalcedex(codepostalcedex) - xml.codecominsee(codecominsee) - end - - def self.from_payment_request(payment_request) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength - if payment_request.pfmp.rectified? - return new( - pointremise: payment_request.student.address_line1.slice(0, 38), # Max 38 characters - cpltdistribution: payment_request.student.address_line2&.slice(0, 38), # Max 38 characters - codetypeadr: ASP::Mappers::AdresseMapper::PRINCIPAL_ADDRESS_TYPE, - codeinseepays: InseeCountryCodeMapper.call(payment_request.student.address_country_code), - codepostalcedex: payment_request.student.address_postal_code, - codecominsee: payment_request.student.address_city_insee_code - ) - end - - if payment_request.student.lives_in_france? - super - else - establishment = payment_request.pfmp.establishment - - raise ASP::Errors::MissingEstablishmentCommuneCodeError if establishment.commune_code.blank? - raise ASP::Errors::MissingEstablishmentPostalCodeError if establishment.postal_code.blank? - - new( - codetypeadr: Mappers::AdresseMapper::PRINCIPAL_ADDRESS_TYPE, - codecominsee: establishment.commune_code, - codepostalcedex: establishment.postal_code, - codeinseepays: InseeCodes::FRANCE_INSEE_COUNTRY_CODE - ) - end - end - end - end -end diff --git a/lib/asp/entities/adresse/base.rb b/lib/asp/entities/adresse/base.rb new file mode 100644 index 000000000..f3a621550 --- /dev/null +++ b/lib/asp/entities/adresse/base.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module ASP + module Entities + module Adresse + class Base < Entity + attribute :codetypeadr, :string + attribute :codecominsee, :string + attribute :codeinseepays, :string + attribute :codepostalcedex, :string + + validates_presence_of %i[ + codetypeadr + codeinseepays + codepostalcedex + codecominsee + ] + + def fragment(xml) + xml.codetypeadr(codetypeadr) + xml.codeinseepays(codeinseepays) + xml.codepostalcedex(codepostalcedex) + xml.codecominsee(codecominsee) + end + end + end + end +end diff --git a/lib/asp/entities/adresse/etranger.rb b/lib/asp/entities/adresse/etranger.rb new file mode 100644 index 000000000..fbd8bf062 --- /dev/null +++ b/lib/asp/entities/adresse/etranger.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module ASP + module Entities + module Adresse + class Etranger < Base + # NOTE: for students living outside of France we take address attributes from the establishment + def fragment(xml) + establishment = payment_request.pfmp.establishment + + raise ASP::Errors::MissingEstablishmentCommuneCodeError if establishment.commune_code.blank? + raise ASP::Errors::MissingEstablishmentPostalCodeError if establishment.postal_code.blank? + + xml.codetypeadr(Mappers::AdresseMapper::PRINCIPAL_ADDRESS_TYPE) + xml.codecominsee(establishment.commune_code) + xml.codepostalcedex(establishment.postal_code) + xml.codeinseepays(InseeCodes::FRANCE_INSEE_COUNTRY_CODE) + end + end + end + end +end diff --git a/lib/asp/entities/adresse/indu.rb b/lib/asp/entities/adresse/indu.rb new file mode 100644 index 000000000..5bce86233 --- /dev/null +++ b/lib/asp/entities/adresse/indu.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module ASP + module Entities + module Adresse + class Indu < Base + attribute :pointremise, :string + attribute :cpltdistribution, :string + + validates_presence_of %i[ + pointremise + cpltdistribution + ] + + def fragment(xml) + xml.pointremise(payment_request.student.address_line1.slice(0, 38)) # Max 38 characters + xml.cpltdistribution(payment_request.student.address_line2&.slice(0, 38)) # Max 38 characters + xml.codetypeadr(ASP::Mappers::AdresseMapper::PRINCIPAL_ADDRESS_TYPE) + xml.codeinseepays(InseeCountryCodeMapper.call(payment_request.student.address_country_code)) + xml.codepostalcedex(payment_request.student.address_postal_code) + xml.codecominsee(payment_request.student.address_city_insee_code) + end + end + end + end +end diff --git a/lib/asp/entities/enregistrement.rb b/lib/asp/entities/enregistrement.rb index 1dd077363..eaa74e39f 100644 --- a/lib/asp/entities/enregistrement.rb +++ b/lib/asp/entities/enregistrement.rb @@ -25,7 +25,7 @@ def fragment(xml) def individu(xml) xml.natureindividu("P") PersPhysique.from_payment_request(payment_request).to_xml(xml) - xml.adressesindividu { Adresse.from_payment_request(payment_request).to_xml(xml) } + xml.adressesindividu { adresse_entity_class.from_payment_request(payment_request).to_xml(xml) } xml.listedossier { Dossier.from_payment_request(payment_request).to_xml(xml) } end diff --git a/lib/asp/entities/entity.rb b/lib/asp/entities/entity.rb index 28b109fe1..45d7d89c4 100644 --- a/lib/asp/entities/entity.rb +++ b/lib/asp/entities/entity.rb @@ -57,6 +57,14 @@ def to_xml(builder) end end end + + def adresse_entity_class + if payment_request.pfmp.rectified? + Adresse::Indu + else + payment_request.student.lives_in_france? ? Adresse::Base : Adresse::Etranger + end + end end end end diff --git a/lib/asp/entities/prestadoss.rb b/lib/asp/entities/prestadoss.rb index 9fac9f0f6..23deee790 100644 --- a/lib/asp/entities/prestadoss.rb +++ b/lib/asp/entities/prestadoss.rb @@ -31,7 +31,7 @@ def xml_root_args def fragment(xml) prestadoss_xml(xml) - xml.adressesprestadoss { Adresse.from_payment_request(payment_request).to_xml(xml) } + xml.adressesprestadoss { adresse_entity_class.from_payment_request(payment_request).to_xml(xml) } xml.coordpaiesprestadoss { CoordPaie.from_payment_request(payment_request).to_xml(xml) } xml.listeelementpaiement { ElementPaiement.from_payment_request(payment_request).to_xml(xml) } end