epic-s6ts
[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, c.accno as 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   # Lieferdatum (deliverydate) ist entscheidend für den Steuersatz
82   do_statement($form, $sth, $self->{queries}->{get_tax_info}, $params{taxkey}, $params{deliverydate} || $params{transdate});
83
84   my $ref = $sth->fetchrow_hashref() || { };
85
86   $main::lxdebug->leave_sub();
87
88   return $ref;
89 }
90
91 sub get_full_tax_info {
92   $main::lxdebug->enter_sub();
93
94   my $self     = shift;
95   my %params   = @_;
96
97   Common::check_params(\%params, qw(transdate));
98
99   my $myconfig = \%main::myconfig;
100   my $form     = $main::form;
101
102   my %tax_info     = (
103     'taxkeys'      => { },
104     'taxchart_ids' => { },
105     );
106
107   my @all_taxkeys = map { $_->{taxkey} } (selectall_hashref_query($form, $form->get_standard_dbh(), qq|SELECT DISTINCT taxkey FROM tax WHERE taxkey IS NOT NULL|));
108
109   foreach my $taxkey (@all_taxkeys) {
110     my $ref = $self->get_tax_info('transdate' => $params{transdate}, 'taxkey' => $taxkey, 'deliverydate' => $params{deliverydate});
111
112     $tax_info{taxkeys}->{$taxkey}            = $ref;
113     $tax_info{accnos}->{$ref->{taxchart_id}} = $ref if ($ref->{taxchart_id});
114   }
115
116   $main::lxdebug->leave_sub();
117
118   return %tax_info;
119 }
120
121 1;