def add_salary(self, salary): ''' Need to write splits to a transaction in account Expenses.Salaries ''' trans1 = gnucash.Transaction(self.book) trans1.BeginEdit() split1 = gnucash.Split(self.book) split1.SetParent(trans1) trans1.SetCurrency(self.currency) # It fails the first time, then succeeds, else split1.SetParent(trans1) # it just crashes num = gnucash.GncNumeric(100*salary.gross_for_tax, 100) split1.SetAccount(self.acc_exp_salaries) split1.SetValue(num) num = gnucash.GncNumeric(100*salary.calc_salary(), 100) split2 = gnucash.Split(self.book) split2.SetParent(trans1) split2.SetAccount(self.acc_bank) split2.SetValue(gnucash.GncNumeric.neg(num)) num = gnucash.GncNumeric(100*salary.ni_employee,100) split3 = gnucash.Split(self.book) split3.SetParent(trans1) split3.SetAccount(self.acct_liab_NI) split3.SetValue(gnucash.GncNumeric.neg(num)) #logging.debug("Doing NICS %s",salary.ni_employee) split4 = gnucash.Split(self.book) split4.SetParent(trans1) split4.SetAccount(self.acct_liab_PAYE) num = gnucash.GncNumeric(100*salary.paye,100) split4.SetValue(gnucash.GncNumeric.neg(num)) split5 = gnucash.Split(self.book) split5.SetParent(trans1) split5.SetAccount(self.acct_liab_student_loan) num = gnucash.GncNumeric(100*salary.student_loan,100) split5.SetValue(gnucash.GncNumeric.neg(num)) trans1.SetDescription("Mike Evans") trans1.SetDate(datetime.date.today().day,datetime.date.today().month,datetime.date.today().year) #DONE get this from datetime.date.today() as day month year trans1.CommitEdit() logging.debug("Done transaction") self.session.save() return
def __transaction__str__(self): """__str__ method for Transaction class""" from gnucash import Transaction import time self = Transaction(instance=self) fmt_tuple = ('Date:', time.ctime(self.GetDate()), 'Description:', self.GetDescription(), 'Notes:', self.GetNotes()) transaction_str = "{0:6}{1:25} {2:14}{3:40} {4:7}{5:40}".format( *all_as_classwithcutting__format__(*fmt_tuple)) transaction_str += "\n" splits_str = "" for n, split in enumerate(self.GetSplitList()): if not (type(split) == gnucash.Split): split = gnucash.Split(instance=split) transaction_flag = split.getflag("PRINT_TRANSACTION") split.setflag("PRINT_TRANSACTION", False) splits_str += "[{0:>2}] ".format(str(n)) splits_str += str(split) splits_str += "\n" split.setflag("PRINT_TRANSACTION", transaction_flag) return transaction_str + splits_str
# this is not the right choice tx.SetCurrency(DEFAULT_CCY) for grandchild in child: # Splits (also called legs) if grandchild.tag == 'split': splAccount = unidecode(unicode(grandchild.attrib['account'])) # This is empty string for imbalances in MoneyGuru splAmountStr = grandchild.attrib['amount'].split()[-1] if not grandchild.attrib['amount'] == '0.00' and \ not grandchild.attrib['amount'].startswith(DEFAULT_CCY.get_mnemonic()): logging.warn("Leg of transaction \"" + txDescription + "\" on account \"" + splAccount + "\" for date " + child.attrib['date'] + " has been added in " + DEFAULT_CCY.get_mnemonic() + " but in MoneyGuru is of " + grandchild.attrib['amount']) amount = int(Decimal(splAmountStr) * DEFAULT_CCY.get_fraction()) spl = gnucash.Split(session.book) spl.SetParent(tx) if "" != splAccount: spl.SetAccount(session.book.get_root_account().lookup_by_name(splAccount)) spl.SetValue(gnucash.GncNumeric(amount, DEFAULT_CCY.get_fraction())) spl.SetAmount(gnucash.GncNumeric(amount, DEFAULT_CCY.get_fraction())) if 'reconciliation_date' in grandchild.attrib.keys(): recYear, recMonth, recDay = map(int, grandchild.attrib['reconciliation_date'].split('-')) recDate = datetime.datetime(day=recDay, month=recMonth, year=recYear) spl.SetDateReconciledSecs(int(time.mktime(recDate.timetuple()))) spl.SetReconcile('y') if memo != None: spl.SetMemo(memo) else:
def new_transaction(request, key): accounts = misc_functions.get_accounts_by_webapp_key(key) if len(accounts) != 1: raise ValueError('Can only create transactions for 1 account at a time.') src_account = accounts[0] choices = forms.AccountChoices(accounts) new_tx_form = forms.NewTransactionForm(choices, request.POST) if not new_tx_form.is_valid(): raise ValueError(new_tx_form.errors) txinfo = new_tx_form.cleaned_data txinfo['amount'] = Decimal(txinfo['amount']) global set_home_for_gnucash_api if not set_home_for_gnucash_api: # Bad gnucash depends on $HOME (this dir needs to be writable by the webserver) os.environ['HOME'] = os.path.abspath(os.path.join( os.path.dirname(os.path.dirname(__file__)), 'gnucash_api_home')) set_home_for_gnucash_api = True import gnucash from gnucash_scripts import common # make sure we can begin a session Lock.check_can_obtain() # begin GnuCash API session session = gnucash.Session(settings.GNUCASH_CONN_STRING) try: book = session.book USD = book.get_table().lookup('ISO4217', 'USD') root = book.get_root_account() imbalance = common.get_account_by_path(root, 'Imbalance-USD') acct = common.get_account_by_guid(root, src_account.guid) opposing_acct = common.get_account_by_guid(root, txinfo['opposing_account']) gnc_amount = common.decimal_to_gnc_numeric(Decimal(txinfo['amount'])) # From example script 'test_imbalance_transaction.py' trans = gnucash.Transaction(book) trans.BeginEdit() trans.SetCurrency(USD) trans.SetDescription(str(txinfo['tx_desc'])) trans.SetDate( txinfo['post_date'].day, txinfo['post_date'].month, txinfo['post_date'].year) split1 = gnucash.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: split2 = gnucash.Split(book) split2.SetParent(trans) split2.SetAccount(opposing_acct) split2.SetValue(gnc_amount.neg()) split2.SetAmount(gnc_amount.neg()) split2.SetReconcile('c') trans.CommitEdit() finally: session.end() session.destroy() dest_url = request.META.get('HTTP_REFERER') if not dest_url: dest_url = reverse('money_views.views.account', kwargs={'key': key}) return redirect(dest_url)