strictness.
[kivitendo-erp.git] / SL / Taxkeys.pm
1 package Taxkeys;
2
3 use Memoize;
4
5 use SL::DBUtils;
6
7 use strict;
8
9 sub new {
10   my $type = shift;
11
12   my $self = {};
13
14   bless $self, $type;
15
16   return $self->_init();
17 }
18
19 sub DESTROY {
20   my $self = shift;
21
22   $self->_finish_statements();
23 }
24
25 sub _init {
26   my $self = shift;
27
28   $self->{handles} = { };
29   $self->{queries} = { };
30
31   memoize 'get_tax_info';
32   memoize 'get_full_tax_info';
33
34   return $self;
35 }
36
37 sub _finish_statements {
38   $main::lxdebug->enter_sub();
39
40   my $self = shift;
41
42   foreach my $idx (keys %{ $self->{handles} }) {
43     $self->{handles}->{$idx}->finish();
44     delete $self->{handles}->{$idx};
45   }
46
47   $main::lxdebug->leave_sub();
48 }
49
50 sub get_tax_info {
51   $main::lxdebug->enter_sub();
52
53   my $self     = shift;
54   my %params   = @_;
55
56   Common::check_params(\%params, qw(transdate taxkey));
57
58   my $myconfig = \%main::myconfig;
59   my $form     = $main::form;
60
61   if (!$self->{handles}->{get_tax_info}) {
62     $self->{queries}->{get_tax_info} = qq|
63       SELECT t.rate AS taxrate, t.taxnumber, t.taxdescription, t.chart_id AS taxchart_id,
64         c.accno AS taxaccno, c.description AS taxaccount
65       FROM taxkeys tk
66       LEFT JOIN tax t   ON (tk.tax_id  = t.id)
67       LEFT JOIN chart c ON (t.chart_id = c.id)
68       WHERE tk.id =
69         (SELECT id
70          FROM taxkeys
71          WHERE (taxkey_id = ?)
72            AND (startdate <= ?)
73          ORDER BY startdate DESC
74          LIMIT 1)
75 |;
76
77     $self->{handles}->{get_tax_info} = prepare_query($form, $params{dbh} || $form->get_standard_dbh($myconfig), $self->{queries}->{get_tax_info});
78   }
79
80   my $sth = $self->{handles}->{get_tax_info};
81   do_statement($form, $sth, $self->{queries}->{get_tax_info}, $params{taxkey}, $params{transdate});
82
83   my $ref = $sth->fetchrow_hashref() || { };
84
85   $main::lxdebug->leave_sub();
86
87   return $ref;
88 }
89
90 sub get_full_tax_info {
91   $main::lxdebug->enter_sub();
92
93   my $self     = shift;
94   my %params   = @_;
95
96   Common::check_params(\%params, qw(transdate));
97
98   my $myconfig = \%main::myconfig;
99   my $form     = $main::form;
100
101   my %tax_info     = (
102     'taxkeys'      => { },
103     'taxchart_ids' => { },
104     );
105
106   my @all_taxkeys = map { $_->{taxkey} } (selectall_hashref_query($form, $form->get_standard_dbh(), qq|SELECT DISTINCT taxkey FROM tax WHERE taxkey IS NOT NULL|));
107
108   foreach my $taxkey (@all_taxkeys) {
109     my $ref = $self->get_tax_info('transdate' => $params{transdate}, 'taxkey' => $taxkey);
110
111     $tax_info{taxkeys}->{$taxkey}            = $ref;
112     $tax_info{accnos}->{$ref->{taxchart_id}} = $ref if ($ref->{taxchart_id});
113   }
114
115   $main::lxdebug->leave_sub();
116
117   return %tax_info;
118 }
119
120 1;