This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Copying Payee to Memo field

Tags: None
(comma "," separated)
User avatar
psychonaut
Registered Member
Posts
27
Karma
1
OS

Copying Payee to Memo field

Thu Jan 09, 2020 1:15 pm
I recently started using KMyMoney. For some of my bank accounts, the CSV transaction logs I imported did not have separate fields for the payee and the payment description; there was one field that contained both types of information, with no clear separation between them. (Certainly nothing that a regular expression would have been able to reliably split.) I chose to import this field as "Payee/Description", not realizing that this would result in KMyMoney creating a separate payee for almost every transaction. I know that I can now merge these payees in the "Payees" view, but I think this will end up replacing the payment descriptions for the individual transactions. Is there any way that I can automatically copy the value of the Payee field to the Memo field for all transactions of a given account? From looking at the XML file, I could probably hack together a short program that does this, though if there is a way of doing this from within KMyMoney I would prefer to take that safer route.
User avatar
ipwizard
KDE Developer
Posts
1359
Karma
6
OS

Re: Copying Payee to Memo field

Thu Jan 09, 2020 2:33 pm
Your script is the best bet you could get at this point. This functionality is not available in KMyMoney. Use the merge feature to get rid of the multiple payees. I think you can do that with a multiple/all in one shot.


ipwizard, proud to be a member of the KMyMoney forum since its beginning. :-D
openSuSE Leap 15.4 64bit, KF5
User avatar
psychonaut
Registered Member
Posts
27
Karma
1
OS

Re: Copying Payee to Memo field

Mon Jan 13, 2020 11:36 am
I've started on the script but it seems this may be more complicated than I thought. In my kmy file, I have some transactions with KEYVALUEPAIR elements that contain escaped XML:

Code: Select all
  <TRANSACTION commodity="EUR" postdate="2019-01-14" entrydate="2020-01-06" id="T000000000000000037" memo="">
   <SPLITS>
    <SPLIT payee="P000724" price="1/1" action="" shares="-1000/1" number="" reconciledate="" id="S0001" memo="" bankid="2019-01-11-98574112-0" reconcileflag="0" account="A000065" value="-1000/1"/>
    <SPLIT payee="P000724" price="1/1" action="" shares="1000/1" number="" reconciledate="" id="S0002" memo="" bankid="2019-01-14-160284286-0" reconcileflag="1" account="A000072" value="1000/1">
     <KEYVALUEPAIRS>
      <PAIR value="S0001" key="kmm-match-split"/>
      <PAIR value="&amp;lt;!DOCTYPE MATCH>&#xa;&amp;lt;CONTAINER>&#xa; &amp;lt;TRANSACTION commodity=&quot;EUR&quot; postdate=&quot;2019-01-14&quot; entrydate=&quot;2020-01-06&quot; id=&quot;&quot; memo=&quot;&quot;>&#xa;  &amp;lt;SPLITS>&#xa;   &amp;lt;SPLIT payee=&quot;P000684&quot; price=&quot;1/1&quot; action=&quot;&quot; shares=&quot;1000/1&quot; number=&quot;&quot; reconciledate=&quot;&quot; id=&quot;S0001&quot; memo=&quot;&quot; bankid=&quot;2019-01-14-160284286-0&quot; reconcileflag=&quot;0&quot; account=&quot;A000072&quot; value=&quot;1000/1&quot;/>&#xa;  &amp;lt;/SPLITS>&#xa;  &amp;lt;KEYVALUEPAIRS>&#xa;   &amp;lt;PAIR value=&quot;true&quot; key=&quot;Imported&quot;/>&#xa;  &amp;lt;/KEYVALUEPAIRS>&#xa; &amp;lt;/TRANSACTION>&#xa;&amp;lt;/CONTAINER>&#xa;" key="kmm-matched-tx"/>
      <PAIR value="2019-01-11" key="kmm-orig-postdate"/>
     </KEYVALUEPAIRS>
    </SPLIT>
   </SPLITS>
  </TRANSACTION>


If I'm copying the payee name into the memo field, do I also need to make sure I do it inside these embedded XML snippets?
User avatar
ipwizard
KDE Developer
Posts
1359
Karma
6
OS

Re: Copying Payee to Memo field

Mon Jan 13, 2020 12:12 pm
The embedded XML structures are 'matched transactions'. Once you accept them, they will be gone. So you should do that as a first step before you massage the XML.


ipwizard, proud to be a member of the KMyMoney forum since its beginning. :-D
openSuSE Leap 15.4 64bit, KF5
User avatar
psychonaut
Registered Member
Posts
27
Karma
1
OS

Re: Copying Payee to Memo field

Thu Jan 16, 2020 9:45 pm
Thanks! Here's the script in case anyone wants it:
Code: Select all
#!/usr/bin/gawk -f

# This script takes a KMyMoney XML file and copies the name of the
# payee into the memo field if it is empty.

/<PAYEE / {
    name=gensub(/.*name="([^"]+)".*/, "\\1", 1)
    name=gensub(/&/, "\\\\&", "g", name)
    id=gensub(/.*id="([^"]+)".*/, "\\1", 1)
    payee[id]=name
}

{
    if (match($0, /payee="[^"]+".* memo=""/) || match($0, /memo="".* payee="[^"]+"/)) {
        id=gensub(/.*payee="([^"]+)".*/, "\\1", 1)
        if (id in payee) {
            print gensub(/memo=""/, "memo=\"" payee[id] "\"", 1)
        }
        else {
            print "Unknown payee ID: " id > "/dev/stderr"
            exit 1
        }
    }
    else
        print $0
}

Last edited by psychonaut on Fri Jan 17, 2020 9:20 am, edited 1 time in total.
User avatar
ipwizard
KDE Developer
Posts
1359
Karma
6
OS

Re: Copying Payee to Memo field

Fri Jan 17, 2020 6:21 am
Please keep in mind that the order of attributes in an XML element is not guaranteed. An expression like
if (match($0, /payee="[^"]+".* memo=""/)) {

may therefore fail in the future.


ipwizard, proud to be a member of the KMyMoney forum since its beginning. :-D
openSuSE Leap 15.4 64bit, KF5
User avatar
psychonaut
Registered Member
Posts
27
Karma
1
OS

Re: Copying Payee to Memo field

Fri Jan 17, 2020 9:21 am
ipwizard wrote:Please keep in mind that the order of attributes in an XML element is not guaranteed.

Thanks; I've updated the code in my post to address this issue.


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft