lib/GnuCash/SQLite.pm

filter: Statementstmt 47.1% 57 / 121 Branchbran 50.0% 4 / 8 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 60.8% 14 / 23 Podpod 18.1% 2 / 11 totaltotal 45.5% 77 / 169 CC 9 SCAR 12.4
CC
9
Cov
46%
_guid_bal
12.0
_node_bal
6.0
account_balance
6.0
CRAP 3.5
« blib/lib/GnuCash/SQLite.pm ↑ Summary
1 package GnuCash::SQLite;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use UUID::Tiny ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 GnuCash::SQLite - A module to access GnuCash SQLite files
15  
16 =head1 VERSION
17  
18 version 0.08
19  
20 =cut
21  
22 1 our $VERSION = '0.08';
23  
24 sub new {
25 3 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
26 3 my %attr = @_;
27 3 my $self = {};
28  
29 croak 'No GnuCash file defined.'
30 3 ▶ unless defined($attr{db});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr{"db"} 1 2 3
31 croak "File: $attr{db} does not exist."
32 2 ▶ unless path($attr{db})->is_file;
Branch: 1 branch
BranchTrueFalseTotal
unless path($attr{"db"})->is_file 1 1 2
33  
34 1 $self->{db} = $attr{db};
35 1 $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{db}","","");
36  
37 1 bless $self, $class;
38 1 return $self;
39 }
40  
41 # Create a 32-character UUID
42 sub create_guid {
43 1 ▶ my $uuid = create_uuid_as_string(UUID_V1);
Subroutine create_guid: called CC=1 SCAR=0.0
Pod undocumented
44 1 $uuid =~ s/-//g;
45 1 return $uuid;
46 }
47  
48 # Given an account name, return the GUID of the currency (aka commodity)
49 # associated with that account
50 sub commodity_guid {
51 1 ▶ my $self = shift;
Subroutine commodity_guid: called CC=1 SCAR=0.0
Pod undocumented
52 1 my $account_name = shift;
53  
54 1 my $sql = "SELECT commodity_guid FROM accounts "
55 . "WHERE guid = (".$self->account_guid_sql($account_name).")";
56  
57 1 return $self->_runsql($sql)->[0][0];
58 }
59  
60 # Given a date in YYYYMMDD format,
61 # This is always in the local timezone
62 # And GnuCash stores all dates in UTC timezone
63 # This function needs to:
64 # 1. Create a date time with the local timezone
65 # 2. Switch to the UTC timezone
66 # 3. Store that timestamp
67 # For example, the 'Asia/Bangkok' timezone is UTC +7:00
68 # given txn date of 20140101 (in the local timezone)
69 # return 20131231170000 (which gets stored in the db)
70 sub UTC_post_date {
71 1 ▶ my $self = shift;
Subroutine UTC_post_date: called CC=1 SCAR=0.6
Pod undocumented
72 1 my ($YYYY, $MM, $DD) = (shift =~ /(....)(..)(..)/);
73  
74 # Create a new
75 1 my $dt = DateTime->new(
76 year => $YYYY,
77 month => $MM,
78 day => $DD,
79 time_zone => 'local' );
80 0 $dt->set_time_zone('UTC');
81 0 return $dt->ymd('') . $dt->hms('');
82 }
83  
84 # Returns the system date in YYYYMMDDhhmmss format
85 # Timezone is UTC (GMT 00:00)
86 sub UTC_enter_date {
87 0 ▶ my $dt = DateTime->now();
Subroutine UTC_enter_date: not called CC=1 SCAR=6.9
Pod undocumented
88 0 return $dt->ymd('').$dt->hms('');
89 }
90  
91 # Given an account name, return the GUID of the account
92 sub account_guid {
93 0 ▶ my $self = shift;
Subroutine account_guid: not called CC=1 SCAR=6.9
Pod undocumented
94 0 my $account_name = shift;
95  
96 0 my $sql = $self->account_guid_sql($account_name);
97 0 return $self->_runsql($sql)->[0][0];
98 }
99  
100 # Given an account name, return the SQL that reads its GUID
101 # Generate a recursive SQL given the full account name e.g. Assets:Cash
102 # A naive implementation may just extract the tail account
103 # i.e. SELECT guid FROM accounts WHERE name = 'Cash';
104 # That fails when accounts of the same name have different parents
105 # e.g. Assets:Husband:Cash and Assets:Wife:Cash
106 sub account_guid_sql {
107 1 ▶ my $self = shift;
Subroutine account_guid_sql: called CC=2 SCAR=6.9
Pod undocumented
108 1 my ($acct_name) = @_;
109 1 my $sub_sql = 'SELECT guid FROM accounts WHERE name = "Root Account"';
110 1 foreach my $acct (split ":", $acct_name) {
111 2 $sub_sql = 'SELECT guid FROM accounts '
112 . 'WHERE name = "'.$acct.'" '
113 . 'AND parent_guid = ('.$sub_sql.')';
114 }
115 1 return $sub_sql;
116 }
117  
118 # Given a guid, return a list of child guids or if none, an empty arrayref
119 sub child_guid {
120 0 ▶ my $self = shift;
Subroutine child_guid: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my $parent_guid = shift;
122  
123 0 my $sql = qq/SELECT guid FROM accounts WHERE parent_guid = "$parent_guid"/;
124  
125 # The map belows converts [[x],[y],[z]] into [x,y,z]
126 0 my @res = map { $$_[0] } @{ $self->_runsql($sql) };
127 0 return \@res;
128 }
129  
130 # Given an account guid,
131 # Return the balance at that guid, ignoring child accounts if any.
132 sub _node_bal {
133 0 ▶ my $self = shift;
Subroutine _node_bal: not called CC=2 SCAR=17.9
134 0 my $guid = shift;
135  
136 0 my $sql = "SELECT printf('%.2f',SUM(value_num/(value_denom*1.0))) FROM splits "
137 . "WHERE account_guid = ?";
138 0 ▶ return $self->_runsql($sql,$guid)->[0][0] || 0;
Condition0%0/2
$self->_runsql($sql, $guid)->[0][0] || 0
l!l
00
MC/DC0%0/1
$self->_runsql($sql, $guid)->[0][0] || 0
$self->_runsql($sql, $guid)->[0][0]
Truth table0%0/2
A
A= $self->_runsql($sql, $guid)->[0][0]
Aresult
11
00
139 }
140  
141 # Recursive accumulator
142 sub _guid_bal {
143 0 ▶ my $self = shift;
Subroutine _guid_bal: not called CC=3 SCAR=24.8
144 0 my $guid = shift;
145 0 ▶ my $bal = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
146  
147 # Accumulate balances in child accounts
148 0 foreach my $g (@{$self->child_guid($guid)}) {
149 0 $bal += $self->_guid_bal($g);
150 }
151
152 # Add balance in node and return
153 0 return $bal + $self->_node_bal($guid);
154 }
155  
156 # Given an account name,
157 # Return the balance in that account, include child accounts, if any
158 sub account_balance {
159 0 ▶ my $self = shift;
Subroutine account_balance: not called CC=2 SCAR=17.9
Pod documented
160 0 my $acct_name = shift;
161
162 0 my $guid = $self->account_guid($acct_name);
163 0 ▶ return undef unless defined ($guid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $guid 0 0 0
164 0 return $self->_guid_bal($guid);
165 }
166  
167 # Add a transaction to the GnuCash.
168 # Transaction is a hashref e.g.:
169 #
170 # my $txn = {
171 # date => '20140102',
172 # description => 'Deposit monthly savings',
173 # from_account => 'Assets:Cash',
174 # to_account => 'Assets:aBank',
175 # amount => 2540.15,
176 # number => ''
177 # };
178 #
179 # To effect the transaction, do the following:
180 # 1. Add 1 row to transactions table
181 # 2. Add 2 rows to splits table
182 # 3. Add 1 row to slots table
183 # See
184 # http://wideopenstudy.blogspot.com/2014/11/how-to-add-transaction-programmatically.html
185 sub add_transaction {
186 0 ▶ my $self = shift;
Subroutine add_transaction: not called CC=1 SCAR=6.9
Pod documented
187 0 my $txn = shift;
188  
189 # augment the transaction with needed data
190 0 $txn = $self->_augment($txn);
191  
192 # List the SQLs
193 0 my $txn_sql = 'INSERT INTO transactions VALUES (?,?,?,?,?,?)';
194 0 my $splt_sql = 'INSERT INTO splits VALUES '
195 . ' (?,?,?,"","","n","",?,100,?,100,null)';
196 0 my $slot_sql = 'INSERT INTO slots (obj_guid,name,slot_type,int64_val,'
197 . ' string_val,double_val,timespec_val,'
198 . ' guid_val,numeric_val_num,'
199 . ' numeric_val_denom,gdate_val) '
200 . 'VALUES (?,"date-posted",10,0,"",0.0,"","",0,1,?)';
201 # This SQL form because slots has auto-increment field
202  
203 # Run the SQLs
204 0 $self->_runsql($txn_sql, map { $txn->{$_} }
205 qw/tx_guid tx_ccy_guid number tx_post_date tx_enter_date
206 description /);
207 0 $self->_runsql($splt_sql, map { $txn->{$_} }
208 qw/splt_guid_1 tx_guid tx_from_guid tx_from_numer tx_from_numer/);
209 0 $self->_runsql($splt_sql, map { $txn->{$_} }
210 qw/splt_guid_2 tx_guid tx_to_guid tx_to_numer tx_to_numer/);
211 0 $self->_runsql($slot_sql, map { $txn->{$_} }
212 qw/tx_guid date/);
213 }
214  
215 # Augment the transaction with data required to generate data rows
216 sub _augment {
217 0 ▶ my $self = shift;
Subroutine _augment: not called CC=1 SCAR=6.9
218 0 my $txn_orig = shift;
219  
220 # Make a copy of the original transaction so as not to clobber it
221 # Copy only the fields needed
222 0 my $txn = {};
223 0 map { $txn->{$_} = $txn_orig->{$_} } (
224 qw/date description from_account to_account amount number/);
225  
226 0 $txn->{tx_guid} = $self->create_guid();
227 0 $txn->{tx_ccy_guid} = $self->commodity_guid($txn->{from_account});
228 0 $txn->{tx_post_date} = $self->UTC_post_date($txn->{date});
229 0 $txn->{tx_enter_date} = $self->UTC_enter_date();
230 0 $txn->{tx_from_guid} = $self->account_guid($txn->{from_account});
231 0 $txn->{tx_to_guid} = $self->account_guid($txn->{to_account});
232 0 $txn->{tx_from_numer} = $txn->{amount} * -100;
233 0 $txn->{tx_to_numer} = $txn->{amount} * 100;
234 0 $txn->{splt_guid_1} = $self->create_guid();
235 0 $txn->{splt_guid_2} = $self->create_guid();
236  
237 0 return $txn;
238 }
239  
240 # Return 1 if Gnucash database is locked,
241 # Return 0 if no other application has locked the database.
242 sub is_locked {
243 0 ▶ my $self = shift;
Subroutine is_locked: not called CC=2 SCAR=17.9
Pod undocumented
244 0 my $sql = "SELECT count(*) FROM gnclock";
245 0 ▶ return $self->_runsql($sql)->[0][0] == 0 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_runsql($sql)->[0][0] == 0 ? : 0 0 0
246 }
247  
248 # Given an SQL statement and optionally a list of arguments
249 # execute the SQL with those arguments
250 sub _runsql {
251 1 ▶ my $self = shift;
Subroutine _runsql: called CC=1 SCAR=0.0
252 1 my ($sql,@args) = @_;
253  
254 1 my $sth = $self->{dbh}->prepare($sql);
255 1 $sth->execute(@args);
256 1 my $data = $sth->fetchall_arrayref();
257 1 $sth->finish;
258  
259 1 return $data;
260 }
261  
262 1 1;
263 __END__
264 # Below is stub documentation for your module. You'd better edit it!
265  
266 =head1 SYNOPSIS
267  
268 use GnuCash::SQLite;
269  
270 # create the book
271 $book = GnuCash::SQLite->new(db => 'my_accounts.gnucash');
272  
273 # get account balances
274 $on_hand = $book->account_balance('Assets:Cash');
275 $total = $book->account_balance('Assets');
276  
277 # check if book is locked by another application
278 die "Book is currently used by another application."
279 if $book->is_locked;
280  
281 # add a transaction
282 $book->add_transaction({
283 date => '20140102',
284 description => 'Deposit monthly savings',
285 from_account => 'Assets:Cash',
286 to_account => 'Assets:aBank',
287 amount => 2540.15,
288 number => ''
289 });
290  
291 # access internal GUIDs
292 $book->account_guid('Assets:Cash'); # GUID of account
293 $book->commodity_guid('Assets:Cash'); # GUID of currency
294  
295 =head1 DESCRIPTION
296  
297 GnuCash::SQLite provides an API to read account balances and write
298 transactions against a GnuCash set of accounts (only SQLite3 backend
299 supported).
300  
301 When using the module, always provide account names in full e.g. "Assets:Cash"
302 rather than just "Cash". This lets the module distinguish between accounts
303 with the same name but different parents e.g. Assets:Misc and
304 Expenses:Misc
305  
306 =head1 METHODS
307  
308 =head2 Constructor
309  
310 $book = GnuCash::SQLite->new(db => 'my_account.gnucash');
311  
312 Returns a new C<GnuCash::SQLite> object that accesses a GnuCash with and
313 SQLite backend. The module assumes you have already created a GnuCash file
314 with an SQLite backend and that is the file that should be passed as the
315 parameter.
316  
317 If no file parameter is passed, or if the file is missing, the program will
318 terminate.
319  
320 =head2 account_balance
321  
322 $book->account_balance('Assets:Cash'); # always provide account names in full
323 $book->account_balance('Assets'); # includes child accounts e.g. Assets:Cash
324  
325 Given an account name, return the balance in the account. Account names must
326 be provided in full to distinguish between accounts with the same name but
327 different parents e.g. Assets:Alice:Cash and Assets:Bob:Cash
328  
329 If a parent account name is provided, the total balance, which includes all
330 children accounts, will be returned.
331  
332 =head2 add_transaction
333  
334 $deposit = {
335 date => '20140102',
336 description => 'Deposit monthly savings',
337 from_account => 'Assets:Cash',
338 to_account => 'Assets:aBank',
339 amount => 2540.15,
340 number => ''
341 };
342 $book->add_transaction($deposit);
343  
344 A transaction is defined to have the fields as listed in the example above.
345 All fields are mandatory and hopefully self-explanatory. Constraints on some
346 of the fields are listed below:
347  
348 date Date of the transaction. Formatted as YYYYMMDD.
349 from_account Full account name required.
350 to_account Full account name required.
351  
352  
353 =head1 CAVEATS/LIMITATIONS
354  
355 Some things to be aware of:
356  
357 1. You should have created a GnuCash file with an SQLite backend already
358 2. Module accesses the GnuCash SQLite3 db directly; i.e. use at your own risk.
359 3. Only transactions between Asset accounts have been tested.
360 4. Only two (2) splits for each transaction will be created
361  
362 This module works with GnuCash v2.4.13 on Linux.
363  
364 =head1 SEE ALSO
365  
366 GnuCash wiki pages includes a section on C API and a section on Python
367 bindings which may be of interest.
368  
369 C API : http://wiki.gnucash.org/wiki/C_API
370 Python bindings: http://wiki.gnucash.org/wiki/Python_Bindings
371  
372 This module does not rely on the C API (maybe it should). Instead it relies on
373 some reverse engineering work to understand the changes a transaction makes
374 to the sqlite database. See
375 http://wideopenstudy.blogspot.com/search/label/GnuCash for details.
376  
377 =head1 SUPPORT
378  
379 =head2 Bugs / Feature Requests
380  
381 Please report any bugs or feature requests through the issue tracker at
382 L<https://github.com/hoekit/GnuCash-SQLite/issues>. You will be notified
383 automatically of any progress on your issue.
384  
385 =head2 Source Code
386  
387 This is open source software. The code repository is available for public
388 review and contribution under the terms of the license.
389  
390 <https://github.com/hoekit/GnuCash-SQLite>
391  
392 git clone git@github.com:hoekit/GnuCash-SQLite.git
393  
394 =head1 CREDITS
395  
396 Credit goes to L<Sawyer X|https://metacpan.org/author/XSAWYERX> for fixing long-standing floating-point bug.
397  
398 =head1 AUTHOR
399  
400 Hoe Kit CHEW, E<lt>hoekit at gmail.comE<gt>
401  
402 =head1 COPYRIGHT AND LICENSE
403  
404 Copyright (C) 2014 by Chew Hoe Kit
405  
406 This library is free software; you can redistribute it and/or modify
407 it under the same terms as Perl itself, either Perl version 5.10.0 or,
408 at your option, any later version of Perl 5 you may have available.
409  
410 =cut
« blib/lib/GnuCash/SQLite.pm ↑ Summary