コード例 #1
0
    def test_payeevalidation(self):
        transaction = Transaction()
        transaction.ledger = self.ledger1
        account, _ = Account.objects.get_or_create(name='account',
                                                   ledger=self.ledger1)
        transaction.account = account
        payee, _ = Payee.objects.get_or_create(name='payee',
                                               ledger=self.ledger1)
        transaction.payee = payee
        transaction.date = timezone.now()
        transaction.amount = Decimal('1000')
        self.assertIsNone(transaction.save())

        transaction = Transaction()
        transaction.ledger = self.ledger1
        account, _ = Account.objects.get_or_create(name='account',
                                                   ledger=self.ledger1)
        transaction.account = account
        payee, _ = Payee.objects.get_or_create(name='payee',
                                               ledger=self.ledger2)
        transaction.payee = payee
        transaction.date = timezone.now()
        transaction.amount = Decimal('1000')

        with self.assertRaises(ValueError):
            transaction.save()
コード例 #2
0
 def test_getbalance(self):
     self.assertEqual(self.account.get_balance(), Decimal('0.00'))
     t = Transaction()
     t.ledger = self.ledger
     t.payee, _ = Payee.objects.get_or_create(name='', ledger=self.ledger)
     t.account = self.account
     t.date = timezone.now()
     t.amount = Decimal('1234.56')
     t.save()
     self.assertEqual(self.account.get_balance(), Decimal('1234.56'))
コード例 #3
0
 def test_name(self):
     transaction = Transaction()
     transaction.ledger = self.ledger1
     account, _ = Account.objects.get_or_create(name='account',
                                                ledger=self.ledger1)
     transaction.account = account
     payee, _ = Payee.objects.get_or_create(name='payee',
                                            ledger=self.ledger1)
     transaction.payee = payee
     transaction.date = timezone.now()
     transaction.amount = Decimal('1000')
     transaction.save()
     self.assertEqual(str(transaction), transaction.memo)
コード例 #4
0
    def test_balance(self):
        t = Transaction()
        t.ledger = self.ledger
        t.payee, _ = Payee.objects.get_or_create(name='', ledger=self.ledger)
        t.account, _ = Account.objects.get_or_create(name='Cuenta',
                                                     ledger=self.ledger)
        t.date = timezone.now()
        t.amount = Decimal('1234.56')
        t.save()

        self.assertEqual(self.ledger.balance, Decimal('1234.56'))
        self.assertEqual(self.ledger.get_balance(), Decimal('1234.56'))
        self.assertEqual(self.ledger.balance, Decimal('1234.56'))
        balance = self.ledger.balance
        self.ledger.set_balance()
        self.assertEqual(self.ledger.balance, balance)
        self.assertEqual(self.ledger.get_balance(), balance)
コード例 #5
0
    def test_transaction_new_object_multi2_all_fields_correct(self):
        """ Testing custom save() behaves as expected."""

        test_input = [("01-0101", 5), ("01-0102", -5)]
        new_obj = Transaction(user=self.user, date=self.date, value=0)
        new_obj.save(test_input)
        self.assertEqual(new_obj.date, self.date)
        self.assertEqual(new_obj.value, Decimal(5))
        self.assertEqual(new_obj.note, "")
        self.assertEqual(new_obj.source, "")
        self.assertEqual(new_obj.user, self.user)
        self.assertEqual(new_obj.is_balanced, True)
        self.assertEqual(new_obj.lines.count(), 2)
        self.assertEqual(new_obj.lines.first().account, self.a1)
        self.assertEqual(new_obj.lines.first().value, 5)
        self.assertEqual(new_obj.lines.first().note, "")
        self.assertEqual(new_obj.lines.last().account, self.a2)
        self.assertEqual(new_obj.lines.last().value, -5)
        self.assertEqual(new_obj.lines.last().note, "")
コード例 #6
0
class TestTransactionCheckIsBalanced(TestCase):
    """ Test a bunch of random object types and formats for outcomes. """
    def setUp(self):
        self.user = User.objects.create_user('test_staff_user',
                                             '*****@*****.**', '1234')
        self.user.is_staff = True
        self.user.save()
        self.a1 = Account(element='01', number='0101', name='Test Account 1')
        self.a1.save()
        self.a2 = Account(element='01', number='0102', name='Test Account 2')
        self.a2.save()

        t1_value = Decimal(10)
        lines = (self.a1, self.a2, t1_value)
        self.t1 = Transaction(date=date(2017, 6, 16), value=0, user=self.user)
        self.t1.save(lines=lines)

    def test_transaction_check_is_balanced_fails(self):
        broken_line = self.t1.lines.first()
        broken_line.value = 10000
        broken_line.save()
        self.assertEqual(self.t1.is_balanced, False)
コード例 #7
0
    def save_transaction(self, kwargs, live=True):
        """ Creates self instance with necessary `Transaction` and `Lines`
        based upon kwargs provided.

        @@TODO still should be pre-save signal, though this is simple.

        `account` and `accounts` DON'T include tb account.
        This is inferred from cls/self and OBJECT_SETTINGS.

        `lines` are complete and should balance to zero.

        Minimum required fields:
        {
          'user':
          '******':
          'account':
          'value':
        or
          'accounts'
        }

        Note: if `JournalEntry` must define:
        {
          'account_DR':
          'account_CR':
          'value':
        or
          'lines'
        }

        where:
            accounts = [
              (account, val),
              (account, val),
              (account, val)]
              **tb account not included, added later**
            lines = [
              (account_DR, val),
              (account_DR, val),
              (account_DR, val, notes),
              (account_CR, val, notes),
              (account_CR, val)]

        Note: see `Transaction` documentation regarding correct use of `lines`
        or simplified case of providing (`ac_DR`, `ac_CR`, `value`).

        Example usage of `.save_transaction`:

        # simple
        new_journalentry = JournalEntry()
        new_journalentry.save_transaction(kwargs)

        # with entity:
        kwargs['invoice_number'] = 'abc123'
        kwargs['relation'] = Creditor.objects.get(code="GUI")
        new_creditorinvoice = CreditorInvoice()
        new_creditorinvoice.save_transaction(kwargs)
        """
        lines, trans_kwargs, obj_kwargs = self.make_dicts(kwargs)

        if 'relation' in obj_kwargs:
            relation = obj_kwargs.pop('relation')

        # Note: self(**obj_kwargs) does not work in this case.
        for key in obj_kwargs:
            setattr(self, key, kwargs[key])

        # @@ TODO not sure why relation has to be removed and then re-added
        # but it works now after bit of agony and confusion. Explore this.
        if 'relation' in locals():
            self.relation = relation

        self_transaction = Transaction(**trans_kwargs)

        try:
            if live:
                self_transaction.save(lines=lines)
                self.transaction = self_transaction
                self.save(force_insert=True)
                return self
            else:
                print("Pass!: {}: {}".format(self, obj_kwargs))
                return kwargs
        except Exception as e:
            print("Error {}. Keys provided: {}".format(e,
                                                       ", ".join(obj_kwargs)))
            try:
                # Delete transaction if created so not to pollute.
                # @@ TODO save signals
                self_transaction.delete()
            except NameError:
                pass

            return "Error {}: {}".format(e, ", ".join(kwargs))
コード例 #8
0
class TestBankLineQueryset(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('test_staff_user',
                                             '*****@*****.**', '1234')
        self.user.is_staff = True
        self.user.save()

        self.a1 = Account(element='01',
                          number='0101',
                          name='Test Bank Account 1')
        self.a1.save()
        self.a2 = Account(element='15',
                          number='0501',
                          name='Test Expenses Account 1')
        self.a2.save()
        self.ba = BankAccount(account=self.a1, bank='CBA')
        self.ba.save()

        # banktransacion created, transaction matched
        t1_value = Decimal(10)
        lines = (self.a1, self.a2, t1_value)
        self.t1 = Transaction(date=date(2017, 6, 16),
                              value=0,
                              user=self.user,
                              source="{}".format(BankAccount.__module__))
        self.t1.save(lines=lines)

        self.b1 = BankLine(date=date(2017, 6, 16),
                           value=t1_value,
                           bank_account=self.ba,
                           line_dump='Test Transaction 1',
                           description='Test Transaction 1')
        self.b1.save()

        self.e1 = BankEntry.objects.create(transaction=self.t1,
                                           bank_line=self.b1)

        # banktransacion created only
        self.b2 = BankLine(date=date(2017, 6, 16),
                           value=Decimal(20),
                           bank_account=self.ba,
                           line_dump='Test Transaction 2',
                           description='Test Transaction 2')
        self.b2.save()
        self.b3 = BankLine(date=date(2017, 6, 16),
                           value=Decimal(30),
                           bank_account=self.ba,
                           line_dump='Test Transaction 3',
                           description='Test Transaction 3')
        self.b3.save()

    def test_banktransaction_queryset_reconcilied_passes(self):
        reconciled_obj_list = BankLine.objects.reconciled()
        self.assertEqual(reconciled_obj_list.count(), 1)
        self.assertIn(self.b1, reconciled_obj_list)
        self.assertNotIn(self.b2, reconciled_obj_list)
        self.assertNotIn(self.b3, reconciled_obj_list)

    def test_banktransaction_queryset_unreconcilied_passes(self):
        unreconciled_obj_list = BankLine.objects.unreconciled()
        self.assertEqual(unreconciled_obj_list.count(), 2)
        self.assertNotIn(self.b1, unreconciled_obj_list)
        self.assertIn(self.b2, unreconciled_obj_list)
        self.assertIn(self.b3, unreconciled_obj_list)
コード例 #9
0
    def setUp(self):
        self.user = User.objects.create_user('test_staff_user',
                                             '*****@*****.**', '1234')
        self.user.is_staff = True
        self.user.save()

        # create accounts
        self.a1 = Account.objects.create(element='01',
                                         number='0150',
                                         name='a1')
        self.a2 = Account.objects.create(element='01',
                                         number='0100',
                                         name='a2')

        # create creditors objects
        entity = Entity.objects.create(code='a', name='a')
        self.creditor = Creditor.objects.create(entity=entity)

        # create bank_reconciliation objects
        self.ba = BankAccount(account=self.a1, bank='CBA')
        self.ba.save()

        # banktransacion created, transaction matched
        # Payment 1
        t1_value = Decimal(350)
        lines = (self.a1, self.a2, t1_value)
        self.t1 = Transaction(date=date(2017, 6, 16),
                              value=0,
                              user=self.user,
                              source="{}".format(BankAccount.__module__))
        self.t1.save(lines=lines)
        self.b1 = BankLine(date=date(2017, 6, 16),
                           value=t1_value,
                           bank_account=self.ba,
                           line_dump='Test Transaction 1',
                           description='Test Transaction 1')
        self.b1.save()
        self.e1 = BankEntry.objects.create(transaction=self.t1,
                                           bank_line=self.b1)

        self.p1 = CreditorPayment(relation=self.creditor,
                                  bank_entry=self.e1,
                                  user=self.user)
        self.p1.save()

        # Payment 2
        t2_value = Decimal(20)
        lines = (self.a1, self.a2, t2_value)
        self.t2 = Transaction(date=date(2017, 6, 16),
                              value=0,
                              user=self.user,
                              source="{}".format(BankAccount.__module__))
        self.t2.save(lines=lines)
        self.b2 = BankLine(date=date(2017, 6, 16),
                           value=t2_value,
                           bank_account=self.ba,
                           line_dump='Test Transaction 2',
                           description='Test Transaction 2')
        self.b2.save()
        self.e2 = BankEntry.objects.create(transaction=self.t2,
                                           bank_line=self.b2)
        self.p2 = CreditorPayment(relation=self.creditor,
                                  bank_entry=self.e2,
                                  user=self.user)
        self.p2.save()

        for x in range(1, 6):
            new_transaction = Transaction(user=self.user,
                                          date=date(2017, 5, 2) -
                                          timedelta(days=x * 30),
                                          source=get_source(CreditorInvoice))
            new_transaction.save(lines=(self.a1, self.a2, 100.00))
            new_invoice = CreditorInvoice(relation=self.creditor,
                                          transaction=new_transaction,
                                          invoice_number=x)
            new_invoice.save()
コード例 #10
0
class TestCreditorPaymentMatchInvoiceMethod(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('test_staff_user',
                                             '*****@*****.**', '1234')
        self.user.is_staff = True
        self.user.save()

        # create accounts
        self.a1 = Account.objects.create(element='01',
                                         number='0150',
                                         name='a1')
        self.a2 = Account.objects.create(element='01',
                                         number='0100',
                                         name='a2')

        # create creditors objects
        entity = Entity.objects.create(code='a', name='a')
        self.creditor = Creditor.objects.create(entity=entity)

        # create bank_reconciliation objects
        self.ba = BankAccount(account=self.a1, bank='CBA')
        self.ba.save()

        # banktransacion created, transaction matched
        # Payment 1
        t1_value = Decimal(350)
        lines = (self.a1, self.a2, t1_value)
        self.t1 = Transaction(date=date(2017, 6, 16),
                              value=0,
                              user=self.user,
                              source="{}".format(BankAccount.__module__))
        self.t1.save(lines=lines)
        self.b1 = BankLine(date=date(2017, 6, 16),
                           value=t1_value,
                           bank_account=self.ba,
                           line_dump='Test Transaction 1',
                           description='Test Transaction 1')
        self.b1.save()
        self.e1 = BankEntry.objects.create(transaction=self.t1,
                                           bank_line=self.b1)

        self.p1 = CreditorPayment(relation=self.creditor,
                                  bank_entry=self.e1,
                                  user=self.user)
        self.p1.save()

        # Payment 2
        t2_value = Decimal(20)
        lines = (self.a1, self.a2, t2_value)
        self.t2 = Transaction(date=date(2017, 6, 16),
                              value=0,
                              user=self.user,
                              source="{}".format(BankAccount.__module__))
        self.t2.save(lines=lines)
        self.b2 = BankLine(date=date(2017, 6, 16),
                           value=t2_value,
                           bank_account=self.ba,
                           line_dump='Test Transaction 2',
                           description='Test Transaction 2')
        self.b2.save()
        self.e2 = BankEntry.objects.create(transaction=self.t2,
                                           bank_line=self.b2)
        self.p2 = CreditorPayment(relation=self.creditor,
                                  bank_entry=self.e2,
                                  user=self.user)
        self.p2.save()

        for x in range(1, 6):
            new_transaction = Transaction(user=self.user,
                                          date=date(2017, 5, 2) -
                                          timedelta(days=x * 30),
                                          source=get_source(CreditorInvoice))
            new_transaction.save(lines=(self.a1, self.a2, 100.00))
            new_invoice = CreditorInvoice(relation=self.creditor,
                                          transaction=new_transaction,
                                          invoice_number=x)
            new_invoice.save()

    def test_match_invoices_simple_case1(self):
        self.p1.match_invoices()
        i1 = CreditorInvoice.objects.get(relation=self.creditor,
                                         invoice_number='1')
        self.assertEqual(i1.unpaid, Decimal('100.00'))

    def test_match_invoices_simple_case2(self):
        self.p1.match_invoices()
        i2 = CreditorInvoice.objects.get(relation=self.creditor,
                                         invoice_number='2')
        self.assertEqual(i2.unpaid, Decimal('50.00'))

    def test_match_invoices_simple_case3(self):
        self.p1.match_invoices()
        i3 = CreditorInvoice.objects.get(relation=self.creditor,
                                         invoice_number='3')
        self.assertEqual(i3.unpaid, Decimal('0.00'))

    def test_match_invoices_simple_case4(self):
        self.p1.match_invoices()
        i4 = CreditorInvoice.objects.get(relation=self.creditor,
                                         invoice_number='4')
        self.assertEqual(i4.unpaid, Decimal('0.00'))

    def test_match_invoices_simple_case5(self):
        self.p1.match_invoices()
        i5 = CreditorInvoice.objects.get(relation=self.creditor,
                                         invoice_number='5')
        self.assertEqual(i5.unpaid, Decimal('0.00'))

    def test_match_invoices_2nd_payment_case(self):
        """ Second payment on the same invoice """
        self.p1.match_invoices()
        self.p2.match_invoices()
        i2 = CreditorInvoice.objects.get(relation=self.creditor,
                                         invoice_number='2')
        self.assertEqual(i2.unpaid, Decimal('30.00'))

    def test_match_invoices_dup_payment_case(self):
        """ Second payment on the same invoice """
        self.p1.match_invoices()
        self.p2.match_invoices()
        self.p2.match_invoices()
        i2 = CreditorInvoice.objects.get(relation=self.creditor,
                                         invoice_number='2')
        self.assertEqual(i2.unpaid, Decimal('30.00'))
コード例 #11
0
class TestLineQueryset(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('test_staff_user',
                                             '*****@*****.**', '1234')
        self.user.is_staff = True
        self.user.save()

        self.a1 = Account.objects.create(element='01',
                                         number='0101',
                                         name='Test Bank Account 1')
        self.a2 = Account.objects.create(element='15',
                                         number='0501',
                                         name='Test Expenses Account 1')

        self.v1, self.v2, self.v3 = Decimal(10), Decimal(-15), Decimal(5)
        self.year = settings.FINANCIAL_YEARS['F16']

        # this year
        self.d1 = date(2016, 6, 16)
        self.t1 = Transaction(date=self.d1, value=0, user=self.user)
        l = [
            (self.a1, self.v1),
            (self.a1, self.v2),
            (self.a2, self.v3),
        ]
        self.t1.save(lines=l)
        self.l1 = Line.objects.get(transaction=self.t1,
                                   account=self.a1,
                                   value=self.v1)
        self.l2 = Line.objects.get(transaction=self.t1,
                                   account=self.a1,
                                   value=self.v2)
        self.l3 = Line.objects.get(transaction=self.t1,
                                   account=self.a2,
                                   value=self.v3)

        # last year
        self.d2 = date(2015, 6, 16)
        self.tl1 = Transaction(date=date(2015, 6, 16), value=0, user=self.user)
        l = [
            (self.a1, self.v1),
            (self.a1, self.v2),
            (self.a2, self.v3),
        ]
        self.tl1.save(lines=l)
        self.ll1 = Line.objects.get(transaction=self.tl1,
                                    account=self.a1,
                                    value=self.v1)
        self.ll2 = Line.objects.get(transaction=self.tl1,
                                    account=self.a1,
                                    value=self.v2)
        self.ll3 = Line.objects.get(transaction=self.tl1,
                                    account=self.a2,
                                    value=self.v3)

        # next year
        self.tn1 = Transaction(date=date(2017, 6, 16), value=0, user=self.user)
        l = [
            (self.a1, self.v1),
            (self.a1, self.v2),
            (self.a2, self.v3),
        ]
        self.tn1.save(lines=l)
        self.ln1 = Line.objects.get(transaction=self.tn1,
                                    account=self.a1,
                                    value=self.v1)
        self.ln2 = Line.objects.get(transaction=self.tn1,
                                    account=self.a1,
                                    value=self.v2)
        self.ln3 = Line.objects.get(transaction=self.tn1,
                                    account=self.a2,
                                    value=self.v3)

    def test_line_queryset_day1_passes(self):

        obj_list = Line.objects.day(self.d1)

        self.assertEqual(obj_list.count(), 3)
        self.assertIn(self.l1, obj_list)
        self.assertIn(self.l2, obj_list)
        self.assertIn(self.l3, obj_list)
        self.assertNotIn(self.ll1, obj_list)
        self.assertNotIn(self.ll2, obj_list)
        self.assertNotIn(self.ll3, obj_list)
        self.assertNotIn(self.ln1, obj_list)
        self.assertNotIn(self.ln2, obj_list)
        self.assertNotIn(self.ln3, obj_list)

    def test_line_queryset_month_passes(self):

        obj_list = Line.objects.month('2016-06')

        self.assertEqual(obj_list.count(), 3)
        self.assertIn(self.l1, obj_list)
        self.assertIn(self.l2, obj_list)
        self.assertIn(self.l3, obj_list)
        self.assertNotIn(self.ll1, obj_list)
        self.assertNotIn(self.ll2, obj_list)
        self.assertNotIn(self.ll3, obj_list)
        self.assertNotIn(self.ln1, obj_list)
        self.assertNotIn(self.ln2, obj_list)
        self.assertNotIn(self.ln3, obj_list)

    def test_line_queryset_month_none_passes(self):

        obj_list = Line.objects.month('2016-05')

        self.assertEqual(obj_list.count(), 0)
        self.assertNotIn(self.l1, obj_list)
        self.assertNotIn(self.l2, obj_list)
        self.assertNotIn(self.l3, obj_list)
        self.assertNotIn(self.ll1, obj_list)
        self.assertNotIn(self.ll2, obj_list)
        self.assertNotIn(self.ll3, obj_list)
        self.assertNotIn(self.ln1, obj_list)
        self.assertNotIn(self.ln2, obj_list)
        self.assertNotIn(self.ln3, obj_list)

    def test_line_queryset_fyear_last_range_passes(self):

        obj_list = Line.objects.fyear('F15')

        self.assertEqual(obj_list.count(), 3)
        self.assertNotIn(self.l1, obj_list)
        self.assertNotIn(self.l2, obj_list)
        self.assertNotIn(self.l3, obj_list)
        self.assertIn(self.ll1, obj_list)
        self.assertIn(self.ll2, obj_list)
        self.assertIn(self.ll3, obj_list)
        self.assertNotIn(self.ln1, obj_list)
        self.assertNotIn(self.ln2, obj_list)
        self.assertNotIn(self.ln3, obj_list)

    def test_line_queryset_fyear_next_range_passes(self):

        obj_list = Line.objects.fyear('F17')

        self.assertEqual(obj_list.count(), 3)
        self.assertNotIn(self.l1, obj_list)
        self.assertNotIn(self.l2, obj_list)
        self.assertNotIn(self.l3, obj_list)
        self.assertNotIn(self.ll1, obj_list)
        self.assertNotIn(self.ll2, obj_list)
        self.assertNotIn(self.ll3, obj_list)
        self.assertIn(self.ln1, obj_list)
        self.assertIn(self.ln2, obj_list)
        self.assertIn(self.ln3, obj_list)

    def test_line_queryset_date_range1_passes(self):

        obj_list = Line.objects.range(date(2016, 6, 15), date(2016, 6, 17))

        self.assertEqual(obj_list.count(), 3)
        self.assertIn(self.l1, obj_list)
        self.assertIn(self.l2, obj_list)
        self.assertIn(self.l3, obj_list)
        self.assertNotIn(self.ll1, obj_list)
        self.assertNotIn(self.ll2, obj_list)
        self.assertNotIn(self.ll3, obj_list)
        self.assertNotIn(self.ln1, obj_list)
        self.assertNotIn(self.ln2, obj_list)
        self.assertNotIn(self.ln3, obj_list)

    def test_line_queryset_date_range2_passes(self):

        obj_list = Line.objects.range(date(2016, 6, 15), date(2016, 6, 16))

        self.assertEqual(obj_list.count(), 3)
        self.assertIn(self.l1, obj_list)
        self.assertIn(self.l2, obj_list)
        self.assertIn(self.l3, obj_list)
        self.assertNotIn(self.ll1, obj_list)
        self.assertNotIn(self.ll2, obj_list)
        self.assertNotIn(self.ll3, obj_list)
        self.assertNotIn(self.ln1, obj_list)
        self.assertNotIn(self.ln2, obj_list)
        self.assertNotIn(self.ln3, obj_list)

    def test_line_queryset_date_range3_passes(self):

        obj_list = Line.objects.range(date(2016, 6, 16), date(2016, 6, 17))

        self.assertEqual(obj_list.count(), 3)
        self.assertIn(self.l1, obj_list)
        self.assertIn(self.l2, obj_list)
        self.assertIn(self.l3, obj_list)
        self.assertNotIn(self.ll1, obj_list)
        self.assertNotIn(self.ll2, obj_list)
        self.assertNotIn(self.ll3, obj_list)
        self.assertNotIn(self.ln1, obj_list)
        self.assertNotIn(self.ln2, obj_list)
        self.assertNotIn(self.ln3, obj_list)

    def test_line_queryset_date_range4_passes(self):

        obj_list = Line.objects.range(date(2016, 6, 16), date(2016, 6, 16))

        self.assertEqual(obj_list.count(), 3)
        self.assertIn(self.l1, obj_list)
        self.assertIn(self.l2, obj_list)
        self.assertIn(self.l3, obj_list)
        self.assertNotIn(self.ll1, obj_list)
        self.assertNotIn(self.ll2, obj_list)
        self.assertNotIn(self.ll3, obj_list)
        self.assertNotIn(self.ln1, obj_list)
        self.assertNotIn(self.ln2, obj_list)
        self.assertNotIn(self.ln3, obj_list)