GnuCash c935c2f+
Loading...
Searching...
No Matches
test_business.py
1from unittest import main
2
3from datetime import datetime, timezone, timedelta
4
5from gnucash import Account, \
6 ACCT_TYPE_RECEIVABLE, ACCT_TYPE_INCOME, ACCT_TYPE_BANK, \
7 GncNumeric
8from gnucash.gnucash_business import Vendor, Employee, Customer, Job, Invoice, Entry
9
10from test_book import BookSession
11
13 def setUp(self):
14 BookSession.setUp(self)
15
16 self.today = datetime.today()
17
18 self.bank = Account(self.book)
19 self.bank.SetType(ACCT_TYPE_BANK)
20 self.bank.SetCommodity(self.currencycurrency)
21 self.income = Account(self.book)
22 self.income.SetType(ACCT_TYPE_INCOME)
23 self.income.SetCommodity(self.currencycurrency)
24 self.receivable = Account(self.book)
25 self.receivable.SetType(ACCT_TYPE_RECEIVABLE)
26 self.receivable.SetCommodity(self.currencycurrency)
27
28 self.customer = Customer(self.book,'CustomerID',self.currencycurrency)
29 self.vendor = Vendor(self.book,'VendorID',self.currencycurrency)
30 self.employee = Employee(self.book,'EmployeeID',self.currencycurrency)
31 self.job = Job(self.book,'JobID',self.customer)
32
33 self.invoice = Invoice(self.book,'InvoiceID',self.currencycurrency,self.customer)
34 self.invoice.SetDateOpened(self.today)
35 entry = Entry(self.book)
36 entry.SetDate(self.today)
37 entry.SetDescription("Some income")
38 entry.SetQuantity(GncNumeric(1))
39 entry.SetInvAccount(self.income)
40 entry.SetInvPrice(GncNumeric(100))
41 self.invoice.AddEntry(entry)
42
43 self.invoice.PostToAccount(self.receivable,
44 self.today, self.today, "", True, False)
45
47 def test_equal(self):
48 self.assertTrue( self.vendor.Equal( self.vendor.GetVendor() ) )
49 self.assertTrue( self.customer.Equal( self.job.GetOwner() ) )
50 self.assertTrue( self.customer.Equal( self.invoice.GetOwner() ) )
51
52 def test_employee_name(self):
53 NAME = 'John Doe'
54 self.assertEqual( '', self.employee.GetUsername() )
55 self.employee.SetUsername(NAME)
56 self.assertEqual( NAME, self.employee.GetUsername() )
57
58 def test_post(self):
59 utc_offset = datetime.now().astimezone().utcoffset()
60 now = datetime.now().astimezone()
61 neutral_time = (now + utc_offset).astimezone(timezone.utc).replace(hour=10, minute=59, second=0, microsecond=0)
62 if utc_offset > timedelta(hours=13):
63 neutral_time -= utc_offset - timedelta(hours=13);
64 if utc_offset < timedelta(hours=-10):
65 neutral_time += timedelta(hours=-10) - utc_offset
66 self.assertEqual(neutral_time,
67 self.invoice.GetDatePosted().astimezone(timezone.utc))
68 self.assertTrue( self.invoice.IsPosted() )
69
70 def test_owner(self):
71 OWNER = self.invoice.GetOwner()
72 self.assertTrue( self.customer.Equal( OWNER ) )
73
74 def test_commodities(self):
75 self.assertTrue( self.currencycurrency.equal( self.customer.GetCommoditiesList()[0] ) )
76
78 """
79 Test that you can get the posted transaction from a posted invoice and that you can get the invoice back from the transaction.
80 """
81 posted_transaction = self.invoice.GetPostedTxn()
82 self.assertTrue( posted_transaction != None )
83 invoice_from_transaction = posted_transaction.GetInvoiceFromTxn()
84 self.assertTrue( invoice_from_transaction != None and invoice_from_transaction.GetID() == self.invoice.GetID() )
85
87 """
88 Test that you can pay an invoice into a transfer account (this also tests conversion of Python lists to GList).
89 """
90 self.receivablereceivable.RecomputeBalance()
91 self.assertTrue( int(self.receivablereceivable.GetBalance().to_double()) == 100 )
92 lots = [self.invoice.GetPostedLot()]
93 self.customer.ApplyPayment(None, lots, self.receivablereceivable, self.bankbank, GncNumeric(100), GncNumeric(1), self.invoice.GetDatePosted(), "Paid into bank account.", "1234", False)
94 self.receivablereceivable.RecomputeBalance()
95 self.assertTrue( self.receivablereceivable.GetBalance().to_fraction().numerator == 0 )
96
97if __name__ == '__main__':
98 main()