Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility of extracting address fields ? #64

Closed
zuzu59 opened this issue Oct 14, 2023 · 9 comments
Closed

Possibility of extracting address fields ? #64

zuzu59 opened this issue Oct 14, 2023 · 9 comments

Comments

@zuzu59
Copy link

zuzu59 commented Oct 14, 2023

Good morning,

First of all, a BIG thank you for writing this superb lib (camt_parser) to process these CAMT XML files which are really painful.

Among all the libs I tested, only yours, written in Ruby, worked well but above all can be modified very easily to have the columns you want for extracting the CAMT file !

In order to simplify the life of our financial secretary, who is not a computer scientist, I created a very small WEB service that is very simple to use:

https://github.com/zuzu59/zcamt2csv

This works great, except we need to be able to extract the transaction proxy address columns:


                       <Dbtr>
                            <Nm>Toto, Tutu</Nm>
                            <PstlAdr>
                                <StrtNm>Avenue de Milan</StrtNm>
                                <BldgNb>26</BldgNb>
                                <PstCd>1007</PstCd>
                                <TwnNm>Lausanne</TwnNm>
                                <Ctry>CH</Ctry>
                            </PstlAdr>
                        </Dbtr>

and


                        <Cdtr>
                            <Nm>Association MakeRevolution</Nm>
                            <PstlAdr>
                                <AdrLine>Chemin du Closel 2</AdrLine>
                                <AdrLine>1020 Renens VD</AdrLine>
                            </PstlAdr>
                        </Cdtr>

And this is where my limits in Ruby are exceeded !

Would it be possible, please, to add in your camt_parser lib the possibility of extracting address fields ?

THANKS
Sincerely
Ch. Zufferey

@tobischo
Copy link
Member

tobischo commented Oct 14, 2023

Good morning Christian,

thank you for your very kind and polite request. 😃
Definitely the best one I have received on Github so far.

Yes, it is definitely possible to extend camt_parser accordingly.

Based on the code you linked to, you are currently using name and iban from the transactions object.

It provides the convenience of automatically selecting the respective creditor or debitor that would probably be of interest.
Based on whether the entry is a credit or a debit it selects the Creditor or Debitor

The creditor or debitor allow access through attr_reader to an underlying XML data object which would already expose the data without any changes that have to be made to camt_parser:

statement.entries.each do |entry|
  related_party = entry.transactions[0].credit? ? entry.transactions[0].debitor : entry.transactions[0].creditor

  street_name = related_party.xml_data.xpath('PstlAdr/StrtNm/text()').text

  # ...
end

So it would be possible for you to use that approach to get to the data.
However, I also see the value in extending camt_parser here and providing convenience functions for the correspondding access.

Right now I would imagine a convenient access like:

address = entry.transactions[0].postal_address
# for unstructured address data
address.lines
# => ["Chemin du Closel 2", "1020 Renens VD"]
# for structured address data
address.street_name
# => "Avenue de Milan"
address.building_number
# => "26"
address.postal_code
# => "1007"
address.town_name
# => "Lausanne"
address.country
# => "CH"

With that all the data would then be conveniently accessible, and also distinctly per related party by going through creditor and debitor if needed.

Would you have anything else in mind here or is this already sufficient for your use case?

Best,
Tobias

@tobischo
Copy link
Member

Hi Christian,

please have a look at #65

This adds what I had in mind based on your request. Your feedback would be appreciated.

Best,
Tobias

@zuzu59
Copy link
Author

zuzu59 commented Oct 14, 2023

Good evening,

Sorry for the late reply but I was out walking all day with my family!

And thank you for responding so quickly, BRAVO for the support and responsiveness!

I studied the code:

https://github.com/viafintech/camt_parser/blob/feature/64-add-postal-address-support/lib/camt_parser/general/postal_address.rb

It seems correct to me and meets my expectations.

I'll try modifying my Gemfile to work with the branch:

https://github.com/viafintech/camt_parser/tree/feature/64-add-postal-address-support

But since the Ruby language is very new to me, it will take me a little time.
I will keep you informed of my tests soon.

THANKS
Sincerely
Ch. Zufferey

@tobischo
Copy link
Member

Good evening as well,

no explanation necessary. Fortunately text patiently waits for people to read it. 😄
I would not have expected a response over the weekend.

Considering that you will need to probably deal with cases where address lines as well as the individual fields are present, I would assume that you might want to get the address into a single column in your CSV file?

If so, I might have a suggestion on how to do this in Ruby and could provide you with a pull request or a code snippet based on your code.

Best,
Tobias

@zuzu59
Copy link
Author

zuzu59 commented Oct 14, 2023

Good evening,

So, as expected, I spent a lot of time to successfully create my Docker container (Gemfile) with your test repository (modification of my Gemfile):

https://github.com/viafintech/camt_parser/tree/feature/64-add-postal-address-support

But finally I got there \o/

Except that I have problems using the 'postal_address' fields!

Nothing to do, I tried everything in every direction, I must not have understood how to use them.

My test code, lines 32 and 33, can be found here:

https://github.com/zuzu59/zcamt2csv/blob/master/app/app.rb

Could you please point out my mistake?

And yes, I would like to be able to retrieve the 'postal_address' fields in detail in order to be able to create a db with the addresses of the principals

thanks again
Sincerely
Ch. Zufferey

@tobischo
Copy link
Member

tobischo commented Oct 15, 2023

Good morning Christian,

line 32 is how it would have to be used.

The way it is currently implemented is that

  • postal_address would only be null if the PstlAdr field does not exist in the XML content
  • lines on the postal_address object is only set if the XML content contains AdrLine fields as in your second example
                       <Cdtr>
                           <Nm>Association MakeRevolution</Nm>
                           <PstlAdr>
                               <AdrLine>Chemin du Closel 2</AdrLine>
                               <AdrLine>1020 Renens VD</AdrLine>
                           </PstlAdr>
                       </Cdtr>

Here is an example that should work to just get it into the file somehow independent of whether the address data would be provided structured or unstructured.

statement.entries.each do |entry|
  address = nil
  postal_address = entry.transactions[0].postal_address
  if postal_address != nil
    address = [
      postal_address.lines,
      street_name,
      building_number,
      postal_code,
      town_name,
      country
    ].flatten.join(' ')
  end

  csv << [
    entry.value_date,
    entry.amount,
    entry.sign,
    entry.additional_information,
    entry.transactions[0].remittance_information,
    address,
    entry.transactions[0].transaction_id,
    entry.transactions[0].name,
    entry.transactions[0].iban,
  ]
end

If this does not work for you, I would ask you to provide an anonymized example of an XML file, so that I may take a look at whether I got something wrong about where the address is found in your XML file(s).

Best,
Tobias

@zuzu59
Copy link
Author

zuzu59 commented Oct 16, 2023

Hello,

So, I used your sample code to retrieve the address fields and it works fine. I had to make some small corrections because the 'postal_address' table was missing in the concatenation of the 'address' variable.

I abandoned the idea of being able to retrieve the details of the address fields because the bank does a bit of nonsense with these addresses at the CAMT level and it was not usable.

So that's going well, I have everything I need to be able to work well and I can therefore validate the pull request :-)

I am quite surprised, not knowing the Ruby language, to have managed relatively easily to use your dev branch (feature/64-add-postal-address-support) to validate the tests on my code. A former developer who recently retired, I know quite a few computer languages, but unfortunately not Ruby.

So I thank you very much for very quickly adding this new possibility of retrieving the address field in camt_parser!

thanks again
Sincerely
Ch. Zufferey

@tobischo
Copy link
Member

Good evening,

happy to hear that!
Then I'll merge the branch tomorrow and release a new gem version so that you do not have to rely on the feature branch.

Best,
Tobias

@tobischo
Copy link
Member

Good morning,

I released it in v2.16.0.
I would recommend that you update the Gemfile to use that version instead of the branch

Best,
Tobias

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants