def alter(account, date, dr_difference, cr_difference): Transaction.objects.filter( journal_entry__date__gt=date, account=account).update( current_dr=none_for_zero( zero_for_none(F('current_dr')) + zero_for_none(dr_difference)), current_cr=none_for_zero( zero_for_none(F('current_cr')) + zero_for_none(cr_difference)))
def get_day_opening(self, before_date=None): if not before_date: before_date = datetime.date.today() transactions = Transaction.objects.filter(account=self, journal_entry__date__lt=before_date).order_by( '-journal_entry__id', '-journal_entry__date')[:1] if len(transactions) > 0: return zero_for_none(transactions[0].current_dr) - zero_for_none(transactions[0].current_cr) return self.opening_dr - self.opening_cr
def get_day_opening(self, before_date=None): if not before_date: before_date = datetime.date.today() transactions = Transaction.objects.filter( account=self, journal_entry__date__lt=before_date).order_by( '-journal_entry__id', '-journal_entry__date')[:1] if len(transactions) > 0: return zero_for_none(transactions[0].current_dr) - zero_for_none( transactions[0].current_cr) return self.opening_dr - self.opening_cr
def _transaction_delete(sender, instance, **kwargs): transaction = instance # cancel out existing dr_amount and cr_amount from account's current_dr and current_cr if transaction.dr_amount: transaction.account.current_dr -= transaction.dr_amount if transaction.cr_amount: transaction.account.current_cr -= transaction.cr_amount alter(transaction.account, transaction.journal_entry.date, float(zero_for_none(transaction.dr_amount)) * -1, float(zero_for_none(transaction.cr_amount)) * -1) transaction.account.save()
def set_transactions(model, date, *args): args = [arg for arg in args if arg is not None] journal_entry, created = JournalEntry.objects.get_or_create( content_type=ContentType.objects.get_for_model(model), model_id=model.id, defaults={ 'date': date }) for arg in args: matches = journal_entry.transactions.filter(account=arg[1]) diff = 0 if not matches: transaction = Transaction() else: transaction = matches[0] diff = zero_for_none(transaction.cr_amount) diff -= zero_for_none(transaction.dr_amount) if arg[0] == 'dr': transaction.dr_amount = float(arg[2]) transaction.cr_amount = None diff += float(arg[2]) elif arg[0] == 'cr': transaction.cr_amount = float(arg[2]) transaction.dr_amount = None diff -= float(arg[2]) elif arg[0] == 'ob': transaction.dr_amount = float(arg[2]) transaction.cr_amount = None diff = 0 else: raise Exception('Transactions can only be either "dr" or "cr".') transaction.account = arg[1] if isinstance(transaction.account.current_balance, unicode): transaction.account.current_balance = float(transaction.account.current_balance) transaction.account.current_balance += diff transaction.current_balance = transaction.account.current_balance transaction.account.save() journal_entry.transactions.add(transaction, bulk=False) alter(transaction.account, date, diff)
def alter(account, date, diff): Transaction.objects.filter(journal_entry__date__gt=date, account=account).update( current_balance=none_for_zero(zero_for_none(F('current_balance')) + zero_for_none(diff)))
def get_balance(self): return zero_for_none(self.current_dr) - zero_for_none(self.current_cr)
def set_transactions(submodel, date, *args): if isinstance(date, unicode): date = datetime.datetime.strptime(date, '%Y-%m-%d') journal_entry, created = JournalEntry.objects.get_or_create( content_type=ContentType.objects.get_for_model(submodel), object_id=submodel.id, defaults={'date': date}) for arg in args: # transaction = Transaction(account=arg[1], dr_amount=arg[2]) matches = journal_entry.transactions.filter(account=arg[1]) if not matches: transaction = Transaction() transaction.account = arg[1] if arg[0] == 'dr': transaction.dr_amount = float(zero_for_none(arg[2])) transaction.cr_amount = None transaction.account.current_dr = none_for_zero( zero_for_none(transaction.account.current_dr) + transaction.dr_amount) alter(arg[1], date, float(arg[2]), 0) if arg[0] == 'cr': transaction.cr_amount = float(zero_for_none(arg[2])) transaction.dr_amount = None transaction.account.current_cr = none_for_zero( zero_for_none(transaction.account.current_cr) + transaction.cr_amount) alter(arg[1], date, 0, float(arg[2])) transaction.current_dr = none_for_zero( zero_for_none( transaction.account.get_dr_amount( date + datetime.timedelta(days=1))) + zero_for_none(transaction.dr_amount)) transaction.current_cr = none_for_zero( zero_for_none( transaction.account.get_cr_amount( date + datetime.timedelta(days=1))) + zero_for_none(transaction.cr_amount)) else: transaction = matches[0] transaction.account = arg[1] # cancel out existing dr_amount and cr_amount from current_dr and current_cr # if transaction.dr_amount: # transaction.current_dr -= transaction.dr_amount # transaction.account.current_dr -= transaction.dr_amount # # if transaction.cr_amount: # transaction.current_cr -= transaction.cr_amount # transaction.account.current_cr -= transaction.cr_amount # save new dr_amount and add it to current_dr/cr if arg[0] == 'dr': dr_difference = float(arg[2]) - zero_for_none( transaction.dr_amount) cr_difference = zero_for_none(transaction.cr_amount) * -1 alter(arg[1], transaction.journal_entry.date, dr_difference, cr_difference) transaction.dr_amount = float(arg[2]) transaction.cr_amount = None else: cr_difference = float(arg[2]) - zero_for_none( transaction.cr_amount) dr_difference = zero_for_none(transaction.dr_amount) * -1 alter(arg[1], transaction.journal_entry.date, dr_difference, cr_difference) transaction.cr_amount = float(arg[2]) transaction.dr_amount = None transaction.current_dr = none_for_zero( zero_for_none(transaction.current_dr) + dr_difference) transaction.current_cr = none_for_zero( zero_for_none(transaction.current_cr) + cr_difference) transaction.account.current_dr = none_for_zero( zero_for_none(transaction.account.current_dr) + dr_difference) transaction.account.current_cr = none_for_zero( zero_for_none(transaction.account.current_cr) + cr_difference) # the following code lies outside if,else block, inside for loop transaction.account.save() try: journal_entry.transactions.add(transaction, bulk=False) except TypeError: # for Django <1.9 journal_entry.transactions.add(transaction)
def set_transactions(submodel, date, *args): if isinstance(date, unicode): date = datetime.datetime.strptime(date, '%Y-%m-%d') journal_entry, created = JournalEntry.objects.get_or_create( content_type=ContentType.objects.get_for_model(submodel), object_id=submodel.id, defaults={ 'date': date }) for arg in args: # transaction = Transaction(account=arg[1], dr_amount=arg[2]) matches = journal_entry.transactions.filter(account=arg[1]) if not matches: transaction = Transaction() transaction.account = arg[1] if arg[0] == 'dr': transaction.dr_amount = float(zero_for_none(arg[2])) transaction.cr_amount = None transaction.account.current_dr = none_for_zero( zero_for_none(transaction.account.current_dr) + transaction.dr_amount) alter(arg[1], date, float(arg[2]), 0) if arg[0] == 'cr': transaction.cr_amount = float(zero_for_none(arg[2])) transaction.dr_amount = None transaction.account.current_cr = none_for_zero( zero_for_none(transaction.account.current_cr) + transaction.cr_amount) alter(arg[1], date, 0, float(arg[2])) transaction.current_dr = none_for_zero( zero_for_none(transaction.account.get_dr_amount(date + datetime.timedelta(days=1))) + zero_for_none(transaction.dr_amount)) transaction.current_cr = none_for_zero( zero_for_none(transaction.account.get_cr_amount(date + datetime.timedelta(days=1))) + zero_for_none(transaction.cr_amount)) else: transaction = matches[0] transaction.account = arg[1] # cancel out existing dr_amount and cr_amount from current_dr and current_cr # if transaction.dr_amount: # transaction.current_dr -= transaction.dr_amount # transaction.account.current_dr -= transaction.dr_amount # # if transaction.cr_amount: # transaction.current_cr -= transaction.cr_amount # transaction.account.current_cr -= transaction.cr_amount # save new dr_amount and add it to current_dr/cr if arg[0] == 'dr': dr_difference = float(arg[2]) - zero_for_none(transaction.dr_amount) cr_difference = zero_for_none(transaction.cr_amount) * -1 alter(arg[1], transaction.journal_entry.date, dr_difference, cr_difference) transaction.dr_amount = float(arg[2]) transaction.cr_amount = None else: cr_difference = float(arg[2]) - zero_for_none(transaction.cr_amount) dr_difference = zero_for_none(transaction.dr_amount) * -1 alter(arg[1], transaction.journal_entry.date, dr_difference, cr_difference) transaction.cr_amount = float(arg[2]) transaction.dr_amount = None transaction.current_dr = none_for_zero(zero_for_none(transaction.current_dr) + dr_difference) transaction.current_cr = none_for_zero(zero_for_none(transaction.current_cr) + cr_difference) transaction.account.current_dr = none_for_zero( zero_for_none(transaction.account.current_dr) + dr_difference) transaction.account.current_cr = none_for_zero( zero_for_none(transaction.account.current_cr) + cr_difference) # the following code lies outside if,else block, inside for loop transaction.account.save() try: journal_entry.transactions.add(transaction, bulk=False) except TypeError: # for Django <1.9 journal_entry.transactions.add(transaction)
def alter(account, date, dr_difference, cr_difference): Transaction.objects.filter(journal_entry__date__gt=date, account=account).update( current_dr=none_for_zero(zero_for_none(F('current_dr')) + zero_for_none(dr_difference)), current_cr=none_for_zero(zero_for_none(F('current_cr')) + zero_for_none(cr_difference)))