epic-ts
[kivitendo-erp.git] / SL / DB / Manager / Chart.pm
1 package SL::DB::Manager::Chart;
2
3 use strict;
4
5 use SL::DB::Helper::Manager;
6 use base qw(SL::DB::Helper::Manager);
7
8 use SL::DB::Helper::Sorted;
9 use SL::DB::Helper::Paginated;
10 use SL::DB::Helper::Filtered;
11 use SL::MoreCommon qw(listify);
12 use DateTime;
13 use SL::DBUtils;
14 use Data::Dumper;
15
16 sub object_class { 'SL::DB::Chart' }
17
18 __PACKAGE__->make_manager_methods;
19
20 __PACKAGE__->add_filter_specs(
21   type => sub {
22     my ($key, $value) = @_;
23     return __PACKAGE__->type_filter($value);
24   },
25   category => sub {
26     my ($key, $value) = @_;
27     return __PACKAGE__->category_filter($value);
28   },
29   selected_category => sub {
30     my ($key, $value) = @_;
31     return __PACKAGE__->selected_category_filter($value);
32   },
33   all => sub {
34     my ($key, $value) = @_;
35     return or => [ map { $_ => $value } qw(accno description) ]
36   },
37   booked => sub {
38     my ($key, $value) = @_;
39     return __PACKAGE__->booked_filter($value);
40   },
41 );
42
43 sub booked_filter {
44   my ($class, $booked) = @_;
45
46   $booked //= 0;
47   my @filter;
48
49   if ( $booked ) {
50      push @filter, ( id => [ \"SELECT distinct chart_id FROM acc_trans" ] );
51   };
52
53   return @filter;
54 }
55
56 sub selected_category_filter {
57   my ($class, $selected_categories) = @_;
58
59   my @selected_categories = @$selected_categories;
60
61   # if no category is selected, there is no filter and thus all charts of all
62   # categories are displayed, which is what we want.
63
64   return (category => \@$selected_categories);
65 }
66
67 sub type_filter {
68   my ($class, $type) = @_;
69
70   # filter by link or several defined custom types
71   # special types:
72   # bank, guv, balance
73
74   return () unless $type;
75
76   if ('HASH' eq ref $type) {
77     # this is to make selection like type => { AR_paid => 1, AP_paid => 1 } work
78     $type = [ grep { $type->{$_} } keys %$type ];
79   }
80
81   my @types = grep { $_ } listify($type);
82   my @filter;
83
84   for my $type (@types) {
85     if ( $type eq 'bank' ) {
86      push @filter, ( id => [ \"SELECT chart_id FROM bank_accounts" ] );
87     } elsif ( $type eq 'guv' ) {
88      push @filter, ( category => [ 'I', 'E' ] );
89     } elsif ( $type eq 'balance' ) {
90      push @filter, ( category => [ 'A', 'Q', 'L' ] );
91     } else {
92       push @filter, $class->link_filter($type);
93     };
94   };
95
96   return @filter > 2 ? (or => \@filter) : @filter;
97 }
98
99 sub category_filter {
100   my ($class, $category) = @_;
101
102   return () unless $category;
103
104   # filter for chart_picker if a category filter was passed via params
105
106   if ( ref $category eq 'HASH' ) {
107     # this is to make a selection like category => { I => 1, E => 1 } work
108     $category = [ grep { $category->{$_} } keys %$category ];
109   }
110
111   my @categories = grep { $_ } listify($category);
112
113   return (category => \@categories);
114 }
115
116 sub link_filter {
117   my ($class, $link) = @_;
118
119   return (or => [ link => $link,
120                   link => { like => "${link}:\%"    },
121                   link => { like => "\%:${link}"    },
122                   link => { like => "\%:${link}:\%" } ]);
123 }
124
125 sub cache_taxkeys {
126   my ($self, %params) = @_;
127
128   my $date  = $params{date} || DateTime->today;
129   my $cache = $::request->cache('::SL::DB::Chart::get_active_taxkey')->{$date} //= {};
130
131   require SL::DB::TaxKey;
132   my $tks = SL::DB::Manager::TaxKey->get_all;
133   my %tks_by_id = map { $_->id => $_ } @$tks;
134
135   my $rows = selectall_hashref_query($::form, $::form->get_standard_dbh, <<"", $date);
136     SELECT DISTINCT ON (chart_id) chart_id, startdate, id
137     FROM taxkeys
138     WHERE startdate < ?
139     ORDER BY chart_id, startdate DESC;
140
141   for (@$rows) {
142     $cache->{$_->{chart_id}} = $tks_by_id{$_->{id}};
143   }
144 }
145
146 sub _sort_spec {
147   (
148     default  => [ 'accno', 1 ],
149     # columns  => {
150     #   SIMPLE => 'ALL',
151     # },
152     nulls    => {},
153   );
154 }
155
156 1;
157
158 __END__
159
160 =pod
161
162 =encoding utf8
163
164 =head1 NAME
165
166 SL::DB::Manager::Chart - Manager class for the model for the C<chart> table
167
168 =head1 FUNCTIONS
169
170 =over 4
171
172 =item C<link_filter $link>
173
174 Returns a query builder filter that matches charts whose 'C<link>'
175 field contains C<$link>. Matching is done so that the exact value of
176 C<$link> matches but not if C<$link> is only a substring of a
177 match. Therefore C<$link = 'AR'> will match the column content 'C<AR>'
178 or 'C<AR_paid:AR>' but not 'C<AR_amount>'.
179
180 =back
181
182 =head1 BUGS
183
184 Nothing here yet.
185
186 =head1 AUTHOR
187
188 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
189
190 G. Richardson E<lt>information@kivitendo-premium.deE<gt>
191
192 =cut