Summary
Cotrugli is an experimental accounting application written in GNU-APL that uses PostgreSQL to manage its database.
At this writing, Seventh Month 2021, it will create a trial balance and allow one to adjust that that trial balance using an application program interface (API). Users well versed in GNU-APL can enter and adjust that trial balance by using the API.
There is much work to be done and I am looking for volunteers.
Data Model
Cotrugli stores its data in a PostgreSQL database and uses GNU APL's library 5 SQL workspace. At the start of a session one should make the connection to PostgreSQL and store the database handle against need.
database_handle←ctrgl_sql_connect server username dbname passwordThe data model consists of six tables:
Configuration
The configuration table is a collection of name -- value pairs used throughout the system
Journal
Accountants use Journals to group entries generated by specific transaction cycles. Examples of such cycles are Sales, Cash Receipts, and Cash Disbursements. In hand posted systems each journal was a spreadsheet organized to simplify entry.
Although several journals are included in the examples the API supports only the General Journal.
Fields in the Journal table are:
- Code
- Name
Company
Cotrugli allows recording the transactions of multiple companies and this table defines those companies.
Fields in the Company table are:
- Code
- Name
Periods
Accounting transactions are stored by period and trial balances are prepared as of the end of a period. Each company has its own set of periods.
Fields in the Periods table are:
- Company code
- Period
- Begin date
- End date
Chart of accounts
A chart of accounts is a list of the categories that can be used to accumulate the totals of transactions. Each company has a separate chart of accounts. Each account is defined with four data:
Account numbers are used to provide a logical order to the accounts and title describe what should be included in an account.
An account should be one of three types:
The basic accounting equation is Assets are equal to Liabilities plus Equity and a Balance Sheet displays that requirement. That is it first lists and totals the assets, then lists and totals the liabilities, then lists and totals the equity accounts and finally totals liabilities and equity.
Retained Earnings is a special equity account that is posted as the final document of the year with the total of the income accounts. The income accounts are then reset to zero.
The balance in each account is displayed as either a positive or negative number based on where it falls in the basic accounting equation.
Assets show positive balances when the debits exceed the credits. Liabilities and Equity show positive balance when the credits exceed the debits.
For income accounts revenue (which increases equity) shows a positive credit balance, while expense (which decrease equity) show a positive debit balance.
This leads us to the account's sign type. It is one of 'd' for debit or 'c' for credit.
These two flags should therefore be set as follows
Accounting Equation | acct_type | sign_type |
---|---|---|
Asset | b | d |
Liability | b | c |
Equity | b | c |
Retained earnings | r | c |
Revenue | i | c |
Expense | i | d |
Document
A document is the basic building block of cotrugli and should be used to record each transaction (a check or an invoice for example) each document consists of a header that has information about the transaction and a body which records the financial effect of the transaction. Cotrugli is a double entry accounting system. That is each document must contain at lease two lines and any amount is identified as either a debit or a credit. The total of the debit lines must equal the total of the credit lines.
There are in fact two database tables document and doc_lines. Document fields are:
- doc_id
- company
- journal
- name
- doc_date
- description
- period
Doc_lines fields are:
- doc_id
- line_no
- acct_no
- debit
- credit
- company
Reports
There is a group of functions which return a report. Cotrugli uses APL Library's wp workspace for reports and these function return a wp instance. wp∆txt∆assemble returns a text report suitable for display in a terminal window while wp∆html∆assemble returns an HTML page.
Reporting functions begin with 'ctrgl_open' and can be listed with
)fns ctrgl_open ctrgl_open_account ctrgl_open_chart ctrgl_open_company ctrgl_open_config ctrgl_open_journals ctrgl_open_periods ctrgl_open_tbctrgl_open_config
Function returns a report of configuration name--value pairs
⍞←wp∆txt∆assemble ctrgl_open_config database_handlectrgl_open_company
Functions returns a report of defined companies
⍞←wp∆txt∆assemble ctrgl_open_company database_handlectrgl_open_periods
Function prepares a workpaper showing the periods defined for a company.
⍞←wp∆txt∆assemble database_handle ctrgl_open_periods company_codectrgl_open_journals
Function prepares a workpaper showing the defined periods.
⍞←wp∆txt∆assemble ctrgl_open_journals database_handlectrgl_open_chart
Function returns a workpaper showing a company's chart of accounts.
⍞←wp∆txt∆assemble database_handle ctrgl_open_chart companyctrgl_open_tb
Function returns a trial balance workpaper
⍞←wp∆txt∆assemble database_handle ctrgl_open_tb company periodctrgl_open_account
Function returns a report showing the transactions that have been posted to an account.
⍞←wp∆txt∆assemble database_handle ctrgl_open_account company period acct_noMaintenance
Config table
ctrgl_config_post
Function to post a name -- value pair to the config table.
database_handle ctrgl_config_post name valueCompany table
ctrgl_company_post
Function posts a company to the company table
database_handle ctrgl_company_post company_code company_namePeriod table
ctrgl_period_post
Function posts a period to the period table.
database_handle ctrgl_period_post company period begin_date end_dateJournal table
ctrgl_jrnl_post
Function to create or update a journal.
database_handle ctrgl_jrnl_post journal_code journal_nameChart of Accounts
ctrgl_chart_fetch
Function returns a record from the chart of accounts.
acct←database_handle ctrgl_chart_fetch company acct_noctrgl_chart_post
Function posts an account to the chart of accounts. Existing account will be overwritten.
database_handle ctrgl_chart_post company acct_no title acct_type sign_typeDocument
ctrgl_doc_init
Function creates a document from a vector of company, journal, name, date, description and period.
doc←ctrgl_doc_init doc_hd company journal name date description periodctrgl_doc_debit
Function adds or replaces a debit line to a document. The descision to add or replace is based on the account number.
doc←doc ctrgl_doc_debit acct_no debitctrgl_doc_credit
Function adds or replaces a credit line to a document.
doc←doc ctrgl_doc_credit acct_no creditctrgl_doc_newLine
Function appends a new line or replaces and old line (based on the account account number) in a document. It is called by ctrgl_doc_debit and ctrgl_doc_credit.
A document line is a vector of doc_id (assigned by the system), line number, account number, debit, and credit.
ctrgl_doc_newLine may be used in lieu of ctrgl_doc_debit or ctrgl_doc_credit. ctrgl_doc_post will populate the doc_id fields in the database.
doc←doc ctrgl_doc_newLine 0 line_no acct_no debit creditctrgl_doc_delLine
Function removes the line in the document for the given account number.
doc←doc ctrgl_doc_delLine acct_noctrgl_doc_post
Function posts a document to the database.
database_handle ctrgl_doc_post docctrgl_doc_show
Function returns the document as a text vector suitable for printing. The function looks up the account titles in the database and a handle must be supplied.
⍞←database_handle ctrgl_doc_show doc