update cash balance after adding split transaction

H Yi's Avatar

H Yi

30 May, 2022 04:40 AM

The ofx file download from my financial institute uses "Buy" as the action. I would like to change it to "BuyXfr". I used the following code to do it:

amount = txn.getOtherTxn(0).getAmount()
incSplit = SplitTxn(txn)
incSplit.setAmount(-amount)
incSplit.setParameter(AbstractTxn.TAG_INVST_SPLIT_TYPE, AbstractTxn.TAG_INVST_SPLIT_INC)
txn.setInvestTxnType(InvestTxnType.BUY_XFER)
txn.addSplit(incSplit)
The "action" and "transfer" fields have been updated properly. However, the "Cash Balance" remains as the original value. I had to highlight the transaction, and press "enter" key, then "Cash Balance" changed to "0". What did I do wrong to prevent "Cash Balance" from updating automatically?

Thanks

  1. 1 Posted by Stuart Beesley ... on 30 May, 2022 04:53 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    I haven’t debugged your code, but a BuyXfr would need an account/category to transfer the cash in/out. I don’t see you setting this in the above. So I can properly understand what you are trying to do, can you highlight a transaction, right click, show record details. Can you do this before and after a successful change and post those details?

  2. 2 Posted by Stuart Beesley ... on 30 May, 2022 05:13 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Also, I'm a bit confused as why you are setting the "invest.splittype" parameter to "inc" when you said this is/was a Buy transaction? For example, here is a Buy and a BuyXfr

    Buy that reduces cash balance

    0.acctid: 2005787b-f161-4dea-b360-9dc192822b78
    0.desc: Desc
    0.id: 35b6283b-6b6d-4c19-b5c9-c8c0fcda6368
    0.invest.splittype: sec
    0.obj_type: 
    0.pamt: -99999
    0.samt: 10100000
    acctid: a5f09061-2c42-4a18-805f-7cf82deb0614
    chk: cheque
    desc: Desc
    dt: 20220501
    dtentered: 1653886506642
    id: 037f3491-2d02-42ab-8602-468f9ec89b88
    invest.txntype: buy
    memo: memo
    obj_type: txn
    oldid: -1
    td: 20220501
    ts: 1653886558566
    xfer_type: xfrtp_buysell
    

    BuyXfr that does not reduce cash balance, but transfers the cash in from elsewhere

    0.acctid: 2005787b-f161-4dea-b360-9dc192822b78
    0.desc: Desc2
    0.id: d6fd1587-3b81-44b3-89af-7a99ababfe76
    0.invest.splittype: sec
    0.obj_type: 
    0.pamt: -119158468
    0.samt: 20200000
    0.tags: 
    1.acctid: 083e4a09-b633-42ae-877a-110f7db54bc0
    1.desc: Desc2
    1.id: 8c79ef59-e025-4cd2-b0b0-71abd8251159
    1.invest.splittype: xfr
    1.obj_type: 
    1.pamt: 119158468
    1.samt: -119158468
    acctid: a5f09061-2c42-4a18-805f-7cf82deb0614
    chk: cheque2
    desc: Desc2
    dt: 20220502
    dtentered: 1653886667124
    id: 8f8e2a5e-8bc9-4cdd-bd32-32db87a28168
    invest.txntype: buyx
    memo: memo2
    obj_type: txn
    tags: 
    td: 20220502
    ts: 1653886694355
    xfer_type: xfrtp_buysellxfr
    
    A BuyXfr will always have an additional split...

    So, please can you post the txn details (screenshots of before and after and the show record details before and after)?

    Tip, the com.infinitekind.moneydance.model.InvestFields class might be your friend here:

    https://infinitekind.com/dev/apidoc/com/infinitekind/moneydance/model/InvestFields.html
    
    fields = InvestFields()
    fields.setFieldStatus(parentTxn)  # Just do this on Parent transactions
    ...make changes to fields
    fields.storeFields(parentTxn)
    fields.syncItem()
    

    This might also help:

    print parentTxn.getSyncInfo().toMultilineHumanReadableString()
    
  3. 3 Posted by H Yi on 30 May, 2022 05:42 AM

    H Yi's Avatar

    Stuart,

    Thanks a lot for your feedback. The BuyX should be the correct type as transactions are stock shares directly from the employer.

    Here is the original "Buy" transaction:

    0.acctid: 48f3085f-893e-4341-ae78-a6ac110eb833
    0.desc: 
    0.id: 65c32061-c718-4d2c-bcdc-8db169b76550
    0.invest.splittype: sec
    0.obj_type: 
    0.pamt: -14542
    0.samt: 8029818
    acctid: 9a0c2d8f-10e8-4a5e-ba6a-38337bf60eb7
    chk: 
    desc: 
    dt: 20220527
    dtentered: 1653888120975
    id: 0e9cd320-c06a-4440-a1f4-216790c44ba0
    invest.txntype: buy
    memo: 
    obj_type: txn
    ol.match-status: y
    ol.match-type: 0
    ol.orig-memo: 
    ol.orig-payee: 
    ol_fitid_1: 97276066E1537532279S4
    oldid: -1
    td: 20220527
    ts: 1653888120975
    xfer_type: xfrtp_buysell
    
    After running the script
    0.acctid: 48f3085f-893e-4341-ae78-a6ac110eb833
    0.desc: 
    0.id: 65c32061-c718-4d2c-bcdc-8db169b76550
    0.invest.splittype: sec
    0.obj_type: 
    0.pamt: -14542
    0.samt: 8029818
    acctid: 9a0c2d8f-10e8-4a5e-ba6a-38337bf60eb7
    chk: 
    desc: 
    dt: 20220527
    dtentered: 1653888120975
    id: 0e9cd320-c06a-4440-a1f4-216790c44ba0
    invest.txntype: buyx
    memo: 
    obj_type: txn
    ol.match-status: y
    ol.match-type: 0
    ol.orig-memo: 
    ol.orig-payee: 
    ol_fitid_1: 97276066E1537532279S4
    oldid: -1
    td: 20220527
    ts: 1653888225604
    xfer_type: xfrtp_buysellxfr
    
    After pressing "enter"
    0.acctid: 48f3085f-893e-4341-ae78-a6ac110eb833
    0.desc: 
    0.id: 65c32061-c718-4d2c-bcdc-8db169b76550
    0.invest.splittype: sec
    0.obj_type: 
    0.pamt: -14542
    0.samt: 8029818
    1.acctid: 58100c4b-d4d5-4db8-adaf-ed1a256cdfe2
    1.desc: 
    1.id: d1478cba-ce77-411d-ab8a-60f8ef5bb2c5
    1.invest.splittype: xfr
    1.obj_type: 
    1.pamt: 14542
    1.samt: -14542
    acctid: 9a0c2d8f-10e8-4a5e-ba6a-38337bf60eb7
    chk: 
    desc: 
    dt: 20220527
    dtentered: 1653888120975
    id: 0e9cd320-c06a-4440-a1f4-216790c44ba0
    invest.txntype: buyx
    memo: 
    obj_type: txn
    ol.match-status: y
    ol.match-type: 0
    ol.orig-memo: 
    ol.orig-payee: 
    ol_fitid_1: 97276066E1537532279S4
    oldid: -1
    td: 20220527
    ts: 1653888266843
    xfer_type: xfrtp_buysellxfr
    
    Besides ts changes, which are irrelevant for the discussion. It seems that script did just its job somewhat correctly, except newly added split transaction didn't get recognized until "enter" is pressed from keyboard. What could cause that?
  4. 4 Posted by Stuart Beesley ... on 30 May, 2022 06:06 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Sorry, but the script is not working... You are not adding the new split line...

  5. 5 Posted by Stuart Beesley ... on 30 May, 2022 06:10 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    All your script is actually doing is changing this:
    invest.txntype: buyx

    When you then go to press enter, MD knows the data is wrong and is doing all the work...

    Your script needs to add a splitTxn for the actual xfr (and not change the buy split). Best best is to use the InvestFields class

  6. 6 Posted by Stuart Beesley ... on 30 May, 2022 06:25 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    .

  7. 7 Posted by Stuart Beesley ... on 30 May, 2022 06:33 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    .

  8. 8 Posted by Stuart Beesley ... on 30 May, 2022 06:46 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Last update:.....Do something like this (not tested) on the ParentTxn:

    from com.infinitekind.moneydance.model import InvestFields, ParentTxn
    
    if not isinstance(txn, ParentTxn): continue
    fields = InvestFields()
    fields.setFieldStatus(txn)
    fields.txnType = InvestTxnType.BUY_XFER
    fields.hasXfrAcct = True
    fields.xfrAcct = THE_ACCOUNT_OBJECT_FOR_TXFR_ACCT_HERE
    fields.storeFields(txn)
    txn.syncItem()
    

    I presume you already have a reference to THE_ACCOUNT_OBJECT_FOR_TXFR_ACCT_HERE (uuid: 58100c4b-d4d5-4db8-adaf-ed1a256cdfe2)? If not, and if the xfr account never changes, then for now you can do once this at the beginning before you iterate the txns:

    xfrAcct = moneydance.getCurrentAccountBook().getAccountByUUID("58100c4b-d4d5-4db8-adaf-ed1a256cdfe2")
    

    and use:

    fields.xfrAcct = xfrAcct
    
  9. 9 Posted by H Yi on 30 May, 2022 11:48 AM

    H Yi's Avatar

    Stuart,

    Thanks a lot for the suggestion. Your code works like a charm!

  10. 10 Posted by Stuart Beesley ... on 30 May, 2022 12:15 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Kool! 👍 😃

    The InvestFields is a very useful and easy class to use and handles the complexity of the underlying record format….

  11. 11 Posted by H Yi on 30 May, 2022 02:21 PM

    H Yi's Avatar

    Thanks for the tip! You're a lifesaver!

  12. System closed this discussion on 29 Aug, 2022 02:30 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

27 Sep, 2022 12:49 PM
27 Sep, 2022 12:37 PM
27 Sep, 2022 12:23 PM
27 Sep, 2022 10:40 AM
27 Sep, 2022 07:22 AM