def createRandomTransactions(book, accountA, accountB): split = Split(book) currency = book.get_table().lookup("CURRENCY", "EUR") print("Create 100 random transactions") for i in range(100): trans = Transaction(book) trans.BeginEdit() trans.SetCurrency(currency) trans.SetDate(randint(1, 28), randint(1, 12), randint(1900, 2000)) trans.SetDescription("Transaction " + str(i)) value = randint(0, 10000) value1 = GncNumeric(value, 100) value2 = GncNumeric(-value, 100) split1 = Split(book) split1.SetValue(value1) split1.SetAccount(accountA) split1.SetMemo("A" + str(i)) split1.SetParent(trans) split2 = Split(book) split2.SetValue(value2) split2.SetAccount(accountB) split2.SetMemo("B" + str(i)) split2.SetParent(trans) trans.CommitEdit()
def write_transactions(self, transactions): for transaction in transactions: tx = Transaction(self.book) tx.BeginEdit() tx.SetCurrency(self.currency) tx.SetDateEnteredTS(datetime.datetime.now()) tx.SetDatePostedTS(transaction.datetime) tx.SetDescription(transaction.description) tx.SetNotes(transaction.note) for split in transaction.splits: sp = Split(self.book) sp.SetParent(tx) sp.SetAccount(GnucashBook.lookup_account(self, split.account)) sp.SetMemo(split.memo) amount = int( Decimal(split.amount) * self.currency.get_fraction()) sp.SetValue(GncNumeric(amount, self.currency.get_fraction())) sp.SetAmount(GncNumeric(amount, self.currency.get_fraction())) tx.CommitEdit()
txinfo['amount']) # From example script 'test_imbalance_transaction.py' trans = Transaction(book) trans.BeginEdit() trans.SetCurrency(USD) trans.SetDescription(str(txinfo['description'])) trans.SetDate(txinfo['date'].day, txinfo['date'].month, txinfo['date'].year) split1 = Split(book) split1.SetParent(trans) split1.SetAccount(acct) if txinfo.has_key('memo'): split1.SetMemo(str(txinfo['memo'])) # The docs say both of these are needed: # http://svn.gnucash.org/docs/HEAD/group__Transaction.html split1.SetValue(gnc_amount) split1.SetAmount(gnc_amount) split1.SetReconcile('c') if opposing_acct != None: debug_print( 'Categorizing transaction %s as %s' % (get_transaction_string(txinfo), opposing_acct_path)) split2 = Split(book) split2.SetParent(trans) split2.SetAccount(opposing_acct) split2.SetValue(gnc_amount.neg())
gtx.SetDateEnteredTS(today) gtx.SetDatePostedTS(tx['date']) # or another datetime object for the transaction's "register date" gtx.SetDescription(str(tx['label'])) amount = tx['amount'] * 100 sp1 = Split(book) # First half of transaction sp1.SetParent(gtx) # The lookup string needs to match your account path exactly. sp1.SetAccount(dest_acc) # amount is an int (no $ or .), so $5.23 becomes amount=523 sp1.SetValue(GncNumeric(amount, 100)) # Assuming you only have one split # For multiple splits, you need to make sure the totals all balance out. sp1.SetAmount(GncNumeric(amount, 100)) sp1.SetMemo(str(tx['label'] + ' - Destiny')) sp2 = Split(book) # Need a balancing split sp2.SetParent(gtx) sp2.SetAccount(src_acc) sp2.SetValue(sp1.GetValue().neg()) sp2.SetAmount(sp1.GetValue().neg()) sp2.SetMemo(str(tx['label'] + ' - Source')) gtx.CommitEdit() # Finish editing transaction session.save() session.end() session.destroy()
# From example script 'test_imbalance_transaction.py' trans = Transaction(book) trans.BeginEdit() trans.SetCurrency(USD) trans.SetDescription(txinfo['description']) trans.SetDate( txinfo['date'].day, txinfo['date'].month, txinfo['date'].year) split1 = Split(book) split1.SetParent(trans) split1.SetAccount(acct) if txinfo.has_key('memo'): split1.SetMemo(txinfo['memo']) # The docs say both of these are needed: # http://svn.gnucash.org/docs/HEAD/group__Transaction.html split1.SetValue(gnc_amount) split1.SetAmount(gnc_amount) split1.SetReconcile('c') if opposing_acct != None: debug_print('Categorizing transaction %s as %s' % (get_transaction_string(txinfo), opposing_acct_path)) split2 = Split(book) split2.SetParent(trans) split2.SetAccount(opposing_acct) split2.SetValue(gnc_amount.neg()) split2.SetAmount(gnc_amount.neg()) split2.SetReconcile('c')
def create_gnc_trade_txs(self, tx1:dict, tx2:dict): """ Create and load Gnucash transactions to the Gnucash file :param tx1: first transaction :param tx2: matching transaction if a switch :return: nil """ self.logger.print_info('create_gnc_trade_txs()', BLUE) # create a gnucash Tx gtx = Transaction(self.book) # gets a guid on construction gtx.BeginEdit() gtx.SetCurrency(self.currency) gtx.SetDate(tx1[TRADE_DAY], tx1[TRADE_MTH], tx1[TRADE_YR]) # self.dbg.print_info("gtx date = {}".format(gtx.GetDate()), BLUE) self.logger.print_info("tx1[DESC] = {}".format(tx1[DESC]), YELLOW) gtx.SetDescription(tx1[DESC]) # create the ASSET split for the Tx spl_ast = Split(self.book) spl_ast.SetParent(gtx) # set the account, value, and units of the Asset split spl_ast.SetAccount(tx1[ACCT]) spl_ast.SetValue(GncNumeric(tx1[GROSS], 100)) spl_ast.SetAmount(GncNumeric(tx1[UNITS], 10000)) if tx1[SWITCH]: # create the second ASSET split for the Tx spl_ast2 = Split(self.book) spl_ast2.SetParent(gtx) # set the Account, Value, and Units of the second ASSET split spl_ast2.SetAccount(tx2[ACCT]) spl_ast2.SetValue(GncNumeric(tx2[GROSS], 100)) spl_ast2.SetAmount(GncNumeric(tx2[UNITS], 10000)) # set Actions for the splits spl_ast2.SetAction("Buy" if tx1[UNITS] < 0 else "Sell") spl_ast.SetAction("Buy" if tx1[UNITS] > 0 else "Sell") # combine Notes for the Tx and set Memos for the splits gtx.SetNotes(tx1[NOTES] + " | " + tx2[NOTES]) spl_ast.SetMemo(tx1[NOTES]) spl_ast2.SetMemo(tx2[NOTES]) else: # the second split is for a REVENUE account spl_rev = Split(self.book) spl_rev.SetParent(gtx) # set the Account, Value and Reconciled of the REVENUE split spl_rev.SetAccount(tx1[REVENUE]) rev_gross = tx1[GROSS] * -1 # self.dbg.print_info("revenue gross = {}".format(rev_gross)) spl_rev.SetValue(GncNumeric(rev_gross, 100)) spl_rev.SetReconcile(CREC) # set Notes for the Tx gtx.SetNotes(tx1[NOTES]) # set Action for the ASSET split action = FEE if FEE in tx1[DESC] else ("Sell" if tx1[UNITS] < 0 else DIST) self.logger.print_info("action = {}".format(action)) spl_ast.SetAction(action) # ROLL BACK if something went wrong and the two splits DO NOT balance if not gtx.GetImbalanceValue().zero_p(): self.logger.print_error("Gnc tx IMBALANCE = {}!! Roll back transaction changes!" .format(gtx.GetImbalanceValue().to_string())) gtx.RollbackEdit() return if self.mode == PROD: self.logger.print_info("Mode = {}: Commit transaction changes.\n".format(self.mode), GREEN) gtx.CommitEdit() else: self.logger.print_info("Mode = {}: Roll back transaction changes!\n".format(self.mode), RED) gtx.RollbackEdit()