File Coverage

blib/lib/Business/AU/Ledger/Database.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Business::AU::Ledger::Database;
2              
3 1     1   646 use Business::AU::Ledger::Database::Payment;
  0            
  0            
4             use Business::AU::Ledger::Database::Receipt;
5              
6             use Log::Dispatch;
7             use Log::Dispatch::DBI;
8              
9             use Moose;
10              
11             has last_insert_id => (is => 'rw', isa => 'Int');
12             has logger => (is => 'rw', isa => 'Log::Dispatch');
13             has payment => (is => 'rw', isa => 'Business::AU::Ledger::Database::Payment');
14             has receipt => (is => 'rw', isa => 'Business::AU::Ledger::Database::Receipt');
15             has simple => (is => 'rw', isa => 'DBIx::Simple');
16              
17             use namespace::autoclean;
18              
19             our $VERSION = '0.88';
20              
21             # -----------------------------------------------
22              
23             sub BUILD
24             {
25             my($self) = @_;
26              
27             $self -> logger(Log::Dispatch -> new);
28             $self -> logger -> add
29             (
30             Log::Dispatch::DBI -> new
31             (
32             dbh => $self -> simple -> dbh,
33             min_level => 'info',
34             name => 'Ledger',
35             )
36             );
37             $self -> payment(Business::AU::Ledger::Database::Payment -> new(db => $self, simple => $self -> simple) );
38             $self -> receipt(Business::AU::Ledger::Database::Receipt -> new(db => $self, simple => $self -> simple) );
39              
40             return $self;
41              
42             } # End of BUILD.
43              
44             # -----------------------------------------------
45              
46             sub get_last_insert_id
47             {
48             my($self, $table_name) = @_;
49              
50             $self -> last_insert_id($self -> simple -> dbh -> last_insert_id(undef, undef, $table_name, undef) );
51              
52             } # End of get_last_insert_id.
53              
54             # -----------------------------------------------
55              
56             sub get_month_name
57             {
58             my($self, $number) = @_;
59             my($month) = $self -> simple -> query('select name from months where id = ?', $number) -> hash;
60              
61             $self -> log(__PACKAGE__ . ". Leaving get_month_name: $number => $$month{'name'}");
62              
63             return $$month{'name'};
64              
65             } # End of get_month_name.
66              
67             # -----------------------------------------------
68              
69             sub get_month_number
70             {
71             my($self, $name) = @_;
72             my($month) = $self -> simple -> query('select id from months where name = ?', $name) -> hash;
73              
74             $self -> log(__PACKAGE__ . ". Leaving get_month_number. $name => $$month{'id'}");
75              
76             return $$month{'id'};
77              
78             } # End of get_month_number.
79              
80             # -----------------------------------------------
81              
82             sub get_months
83             {
84             my($self, $number) = @_;
85             my $month = $self -> simple -> query('select * from months') -> hashes;
86              
87             $self -> log(__PACKAGE__ . '. Leaving get_months');
88              
89             return $month;
90              
91             } # End of get_months.
92              
93             # -----------------------------------------------
94              
95             sub log
96             {
97             my($self, $s) = @_;
98              
99             $self -> logger -> log(level => 'info', message => $s ? $s : '');
100              
101             } # End of log.
102              
103             # -----------------------------------------------
104              
105             sub validate_month
106             {
107             my($self, $month_name) = @_;
108             my($name) = ucfirst lc $month_name;
109             my(@month) = $self -> simple -> query('select code, name from months') -> hashes;
110             my($ok) = '';
111              
112             for (@month)
113             {
114             if ( ($name eq $$_{'code'}) || ($name eq $$_{'name'}) )
115             {
116             $ok = $$_{'name'};
117              
118             last;
119             }
120             }
121              
122             $self -> log(__PACKAGE__ . ". Leaving validate_month. $month_name => $ok");
123              
124             return $ok;
125              
126             } # End of validate_month.
127              
128             # --------------------------------------------------
129              
130             __PACKAGE__ -> meta -> make_immutable;
131              
132             1;