#! /usr/local/bin/apl --script ⍝ ******************************************************************** ⍝ APL workspace provies a bookkeeping system for a single company ⍝ Copyright (C) 2024 Bill Daly ⍝ This program is free software: you can redistribute it and/or modify ⍝ it under the terms of the GNU General Public License as published by ⍝ the Free Software Foundation, either version 3 of the License, or ⍝ (at your option) any later version. ⍝ This program is distributed in the hope that it will be useful, ⍝ but WITHOUT ANY WARRANTY; without even the implied warranty of ⍝ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ⍝ GNU General Public License for more details. ⍝ You should have received a copy of the GNU General Public License ⍝ along with this program. If not, see . ⍝ ******************************************************************** )copy_once 5 SQL )copy_once 5 DALY/wp )copy_once 3 DALY/date )copy_once 5 DALY/cl ⍝ ******************************************************************** ⍝ Integrity checking ⍝ ******************************************************************** ∇b←handle ctrgl_check_account acct_no;co;acct;cmd ⍝ Function confirms an acct is defined in the accounts table.The ⍝ right argument is the account number. →(~b←utl∆stringp acct_no)/0 ⍝ accounts as numbers not acceptable. cmd←'SELECT acct_no from accounts where acct_no = ''',acct_no,'''' b←0≠1↑⍴cmd SQL∆Select[handle] ⍬ ∇ ∇b←handle ctrgl_check_period pd;cmd ⍝ Function confirms that the period is defined. The right argument ⍝ is a nested vector of company and period. cmd←'SELECT period from periods where period = ''',pd,'''' b←0≠1↑⍴cmd SQL∆Select[handle] args ∇ ∇b←handle ctrgl_check_journal jrnl;cmd ⍝ Function confirms that a journal is defined. cmd←'SELECT jrnl FROM journal where jrnl = ''',jrnl,'''' b←0≠1↑⍴cmd SQL∆Select[handle] '' ∇ ∇id←handle ctrgl_check_doc head;cmd;co;nm;pd ⍝ Function returns the document id. Documents are ⍝ defined by the journal, name, and period. jr←1⊃head ◊ nm←2⊃head ◊ pd←3⊃head cmd←'SELECT doc_id FROM document WHERE journal = ? and name = ? and period = ?' id←⍬⍴cmd SQL∆Select[handle] co jr nm pd ∇ ⍝ ******************************************************************** ⍝ Functions to implement the sql interface ⍝ ******************************************************************** ∇handle←ctrgl_sql_connect args;constr ⍝ Function to connect to the postgres server. Right argument is a ⍝ vector of host, user, dbname, and password. constr←'host=',(1⊃args),' user=',(2⊃args),' dbname=',(3⊃args),' password=',4⊃args handle←'postgresql' SQL∆Connect constr ∇ ∇ctrgl_sql_disconnect handle ⍝ Function to disconnect from the postgres server SQL∆Disconnect handle ∇ ∇ctrgl_sql_rollback handle ⍝ Functions rolls back the current sql transaction SQL∆Rollback handle ∇ ∇rs←ctrgl_sql_escape_quotes txt;loc ⍝ Function returns the text with single quotes escaped for ⍝ postgresql rs←txt →(∧/~txt='''')/0 ⍝ Nothing to do loc←+/1,∧\''''≠txt rs←(loc↑rs),'''',ctrgl_sql_escape_quotes loc↓txt ∇ ∇data←handle ctrgl_sql_tb period;cmd ⍝ Function returns a trial balance array. Right argument is a nested ⍝ vector of company and period. cmd←'SELECT acct, title, ' cmd←cmd,'CASE WHEN dr > cr then dr - cr else 0 end as debit, ' cmd←cmd,'CASE WHEN cr > dr then cr - dr else 0 end as credit, ' cmd←cmd,'acct_type, sign_type from tb join accounts ' cmd←cmd,'on tb.co = accounts.company and tb.acct = accounts.acct_no ' cmd←cmd,'where co = ''',company, ''' and period =''',period,''' order by acct' data←cmd SQL∆Select[handle] '' ⍎(1=⍴⍴data)/'data←0 6⍴data' ∇ ⍝ ******************************************************************** ⍝ Maintain the config table ⍝ ******************************************************************** ∇ handle ctrgl_config_post args;cmd;name;value;rs ⍝ Function to post a name -- value pair to the config table. name←1⊃args ◊ value←2⊃args cmd←'SELECT EXISTS(SELECT trim(name) FROM config WHERE name = ''',name,''')' →('t'=''⍴⊃rs←cmd SQL∆Select[handle] '')/replace insert: cmd←'INSERT INTO config (name,value) VALUES (?,?)' cmd SQL∆Exec[handle] name value →0 replace: cmd←'UPDATE config SET value = ? WHERE name = ?' cmd SQL∆Exec[handle] value name →0 ∇ ⍝ ******************************************************************** ⍝ Maintain the period table ⍝ ******************************************************************** ∇ msg←cth ctrgl_period_post_editchecks pd;name;begin;end;ye;dtest;bv ⍝ Function to post a period to the database. (name begin end ye)←pd msg←'' ⍎(~utl∆stringp name)/'msg←''The period name must be a character string. ◊ →0''' dtest←(⊂date∆US) date∆parse ¨ begin end ⍎(∨/bv)/'msg←',((bv←utl∆stringp ¨ dtest)/dtest),' is not a date ◊ →0' ⍎(~