Create new transaction (Jpype package)

gmeirelless's Avatar

gmeirelless

06 Apr, 2022 01:46 PM

Hello,
I hope you guys are doing well.

I`m using Jpype package via python to create a script to import transactions from my bank. I`m able to connect to Moneydance headless and read all objects and transactions correctly, so it`s working well (thanks @dalefurrow for that: https://github.com/dkfurrow/md_python_headless_demo). However, when I try to create a new transaction, no error is returned but anything is created in Moneydance.

Am I forgetting something? (All variables are initialized according to the type)

        new_txn = ParentTxn(accountBook)
        new_txn.setAccount(account)
        new_txn.setDescription(desc)
        new_txn.setMemo(memo)
        new_txn.setDateInt(transdate)
        new_txn.setTaxDateInt(transdate)
        new_txn.setDateEntered(transdate)
        new_txn.setClearedStatus(AbstractTxn.ClearedStatus.UNRECONCILED)
       
        txn_split = SplitTxn(new_txn)
        txn_split.setAmount(amount)
        txn_split.setParentAmount(1.0, amount)
        txn_split.setAccount(cat)
        txn_split.setClearedStatus(AbstractTxn.ClearedStatus.UNRECONCILED)
        new_txn.addSplit(txn_split)

        accountBook.getTransactionSet().addNewTxn(new_txn)
        accountBook.refreshAccountBalances()

Thanks experts!

All the best

  1. 1 Posted by Stuart Beesley ... on 06 Apr, 2022 02:00 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    (I won't comment on the use of JPype to update the MD dataset from outside MD)...

    You certainly MUST do this:
    new_txn.syncItem()

    I'm not sure you need to do this:
    accountBook.getTransactionSet().addNewTxn(new_txn)

    And I'm certain you do NOT need to do this:
    accountBook.refreshAccountBalances()

  2. 2 Posted by Stuart Beesley ... on 06 Apr, 2022 02:07 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    The odd thing is that .addNewTxn() actually does a .syncItem() so it may be that you have a different issue with your environment.. For example, if the sync engine is not running, then it may not be writing a .txn file to the ./safe/tiksync/out. NOTE: your dataset (trunk) does not get written out to disk until restart of MD when the .txn files are re-read, processed into memory and then a save to disk occurs.

  3. 3 Posted by Stuart Beesley ... on 06 Apr, 2022 02:11 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    I would also do a new_txn.setEditingMode() after ParentTxn() too.

  4. 4 Posted by Stuart Beesley ... on 06 Apr, 2022 02:12 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    FYI - Based on @dalefurrow's original scripts, I also played around with ways to launch MD - purely for 'fun'.. You can find these at the end of my site:
    https://yogi1967.github.io/MoneydancePythonScripts/

    (note: you play at your own (data) risk 😉 )

  5. 5 Posted by gmeirelless on 06 Apr, 2022 03:20 PM

    gmeirelless's Avatar

    Thanks for all the valuable tips, Stuart! Unfortunately I have no success. The strange part is that I don't receive no errors at all. Is there maybe some kind of commit for the transaction?

    Thanks again.

  6. 6 Posted by gmeirelless on 06 Apr, 2022 03:23 PM

    gmeirelless's Avatar

    Sorry I didn`t see your last post. Oh really? I love your scripts dude. I was using many of your content to learn how to create an extension. I will review it once again, thanks a lot!

  7. 7 Posted by Stuart Beesley ... on 06 Apr, 2022 03:38 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    I also just tried with your code... Oddly I could not get the split to stick.. So, here is some code that does work for me.... in Moneybot...

    from com.infinitekind.moneydance.model import Account, ParentTxn, SplitTxn, AbstractTxn
    from com.moneydance.apps.md.controller import Util
    from com.infinitekind.util import DateUtil
    
    MD_REF = moneydance
    book = MD_REF.getCurrentAccountBook()
    a_id = "77d3dc2b-9547-49ba-86ad-ceb0395cc592"
    c_id = "a858684e-f7cc-46e1-acc4-b1e4c501ecc0"
    acct = MD_REF.getCurrentAccountBook().getAccountByUUID(a_id)
    cat = MD_REF.getCurrentAccountBook().getAccountByUUID(c_id)
    print "Acct: %s" %(acct)
    print "Cat: %s" %(cat)
    desc = "Test Txn - hello"
    check = "1234"
    memo = "this is a memo"
    transdate = 20220203
    amount = -222
    rate = 1.0
    
    new_txn = ParentTxn.makeParentTxn(book, transdate, transdate,DateUtil.getUniqueCurrentTimeMillis(), check, acct, desc, memo, -1L, 30)
    txn_split = SplitTxn.makeSplitTxn(new_txn, amount, rate, cat, memo, -1L, 0)
    new_txn.addSplit(txn_split)
    
    print "------"
    print "TxnSet count before:", book.getTransactionSet().getTransactionCount()
    new_txn.syncItem()
    #book.getTransactionSet().addNewTxn(new_txn) 
    print new_txn.getSyncInfo().toMultilineHumanReadableString()
    print "------"
    print "Did I find txn in TxnSet?", new_txn in book.getTransactionSet()
    print "TxnSet count after:", book.getTransactionSet().getTransactionCount()
    print "------"
    

    So, please run in Moneybot first, to prove it works, and then try it out your way and see if it fails.... If it fails, then it's your 'environment'....

  8. 8 Posted by gmeirelless on 06 Apr, 2022 04:05 PM

    gmeirelless's Avatar

    Yes, indeed the problem should be in my approach. via Moneybot it worked perfectly.
    I reproduced the same code by Jpype, it does recognize the transaction in the execution, but when I open the moneydance it`s not there. Also, if I run the script again, the transaction is not found anymore and the counter starts back from 2.

    I will dig into this, thanks a lot for the help so far!
    ------
    TxnSet count before: 2
    0.acctid: 4a9888c9-3619-4c6a-b08b-ad26e11ac2cd
    0.desc: this is a memo
    0.id: e8227628-3b5a-4d24-a118-e3dcaa659ce6
    0.obj_type:
    0.oldid: -1
    0.pamt: -222
    0.samt: 222
    acctid: 7b459e38-ce6c-4d0c-8881-f22ffc48e575
    chk: CARD-282567677
    desc: Test Txn - hello
    dt: 20220406
    dtentered: 1649260720428
    id: 4bfbe0f9-c640-4dc6-a075-78f062338e3e
    memo: this is a memo
    obj_type: txn
    oldid: -1
    stat: x
    td: 20220406
    ts: 1649260720503

    ------
    Did I find txn in TxnSet? True
    TxnSet count after: 4
    ------

  9. 9 Posted by gmeirelless on 06 Apr, 2022 04:16 PM

    gmeirelless's Avatar

    Let me take advantage to kindly ask you some guidance. I have some development background but I`m a beginner in Python, the reason I`m trying like this, is that I could make the communication with the bank work in Python and it`s distributed in many files across my project.

    According to your experience, what`s the easiest way to put it inside moneydance? Can I compile that and run as a Moneybot script for example?

    Thanks a lot.

  10. 10 Posted by Stuart Beesley ... on 06 Apr, 2022 04:26 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    So I am certain it's the fact that the sync engine etc is not running.....

    When you run your code, does a .txn file get created? (Check both when doing in Moneybot and also through your method).

    In simple terms, when the GUI is loaded by calling .getUI(), MoneydanceGUI does lots of important stuff, including firing up the SyncManager and start background syncing. The BackgroundOpsThread also gets started...

    My guess is that the stuff you need is in there somewhere.

    Personally, I wouldn't be playing with this....

    Why not launch MD from the command line, and auto run an import, or python script, with auto quit afterwards. This way you get the full MD 'experience' and your updates will work properly....

  11. 11 Posted by Stuart Beesley ... on 06 Apr, 2022 04:28 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Are you asking whether you could write your Python (Jython) code to run inside Moneydance as a script - or extension? The answer is yes. The only issue will be access to your imported libraries.. Is this just for you, or to distribute to others?

  12. 12 Posted by Stuart Beesley ... on 06 Apr, 2022 04:31 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Refer this link to see how to import your own libraries:
    https://infinitekind.tenderapp.com/discussions/moneydance-developme...

  13. 13 Posted by gmeirelless on 06 Apr, 2022 04:32 PM

    gmeirelless's Avatar

    Yes, something like that, since I have this script 'working' in Python it will be great. It`s just for my personal use. I will review this link. Thanks mate

  14. 14 Posted by gmeirelless on 06 Apr, 2022 04:34 PM

    gmeirelless's Avatar

    About your suggestion to launch MD from the command line and auto run an import, seems interesting, do you have an example to share?

  15. 15 Posted by Stuart Beesley ... on 06 Apr, 2022 04:35 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    .

  16. 16 Posted by Stuart Beesley ... on 06 Apr, 2022 04:47 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Moneydance launch parameters:

    # Usage:
    # Parameter: '-d'                 is passed by default by this script and turns on MD DEBUG mode
    # Parameter: '-v'                 prints the current version
    # Parameter: datasetname          will open the specified dataset - specify the full path wrapped in "quotes"
    # Parameter: pythonscriptname.py  adds script to a list of scripts to execute (but this seems to then be ignored)
    # Parameter: importfilename       executes file import (mutually exclusive to datasetname option)
    # Parameter: '-invoke_and_quit=x' will pass along a string that you can to invoke an 'fmodule' (extension) and quit (not showing UI)
    #                                 executes Main.showURL(invokeAndQuitURI)
    #                                 e.g. 'moneydance:fmodule:test:test:customevent:magic'
    #                                 (e.g. my own extension with an id of test defines it's own command called 'test:customevent:magic'
    #                                 (there are other variations of this parameter and with ? instead of ':' for parameters.....
    # Parameter: '-invoke=x'          Same as -invoke_and_quit but does launch the UI first and doesn't quit...!
    
    # MD2021.2(3088): Adds the capability to set the encryption passphrase into an environment variable to bypass the popup question
    #                 Either: md_passphrase=  or  md_passphrase_[filename in lowercase format]
    
  17. 17 Posted by Stuart Beesley ... on 07 Apr, 2022 10:09 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    For fun/experiment only... To get the txns saved you need to call .saveCurrentAccount(). With Java/Jython, this works fine (as long as you also use .setCurrentBook() earlier on). But with JPype .setCurrentBook() never seems to work and thus .saveCurrentAccount() will not work... You can work around this with book.save() in JPype() and this will save the txn.

    Again, disclaimer.... I personally would not be updating MD this way..... Using an import file / extension would be best....

  18. 18 Posted by gmeirelless on 07 Apr, 2022 01:45 PM

    gmeirelless's Avatar

    Everything worked perfectly. Thanks a lot Stuart, you`re a Moneydance`s God! Also, thank you for all recommendation, everything is experimental since I`m getting experience as a Hobby. I use Moneydance since 2015 and having the opportunity to do 'useful things' as a way of personal study is amazing. Thank you again for all your help and available content.

  19. gmeirelless closed this discussion on 07 Apr, 2022 01:45 PM.

  20. Stuart Beesley (Mr Toolbox) re-opened this discussion on 07 Apr, 2022 04:51 PM

  21. 19 Posted by Stuart Beesley ... on 07 Apr, 2022 04:51 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    👍 😃

    Mr Toolbox

  22. gmeirelless closed this discussion on 10 Apr, 2022 09:11 AM.

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 02:26 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