1 package SL::DB::Manager::Chart;
5 use SL::DB::Helper::Manager;
6 use base qw(SL::DB::Helper::Manager);
8 use SL::DB::Helper::Sorted;
9 use SL::DB::Helper::Paginated;
10 use SL::DB::Helper::Filtered;
11 use SL::MoreCommon qw(listify);
18 sub object_class { 'SL::DB::Chart' }
20 __PACKAGE__->make_manager_methods;
22 __PACKAGE__->add_filter_specs(
24 my ($key, $value) = @_;
25 return __PACKAGE__->type_filter($value);
28 my ($key, $value) = @_;
29 return __PACKAGE__->category_filter($value);
31 selected_category => sub {
32 my ($key, $value) = @_;
33 return __PACKAGE__->selected_category_filter($value);
36 my ($key, $value) = @_;
37 return or => [ map { $_ => $value } qw(accno description) ]
40 my ($key, $value) = @_;
41 return __PACKAGE__->booked_filter($value);
44 my ($key, $value) = @_;
45 return __PACKAGE__->invalid_filter($value);
50 my ($class, $status) = @_;
52 croak "Wrong call, need status invalid, all or valid, got:" . $status unless $status =~ m/invalid|all|valid/;
56 if ($status eq 'all') {
57 push @filter, ( id => [ \"SELECT id FROM chart" ] );
58 } elsif ($status eq 'valid') {
59 push @filter, ( id => [ \"SELECT id FROM chart WHERE NOT invalid" ] );
60 } elsif ($status eq 'invalid') {
61 push @filter, ( id => [ \"SELECT id FROM chart WHERE invalid" ] );
62 } else { die "Wrong state for invalid_filter"; }
69 my ($class, $booked) = @_;
75 push @filter, ( id => [ \"SELECT distinct chart_id FROM acc_trans" ] );
81 sub selected_category_filter {
82 my ($class, $selected_categories) = @_;
84 my @selected_categories = @$selected_categories;
86 # if no category is selected, there is no filter and thus all charts of all
87 # categories are displayed, which is what we want.
89 return (category => \@$selected_categories);
93 my ($class, $type) = @_;
95 # filter by link or several defined custom types
99 return () unless $type;
101 if ('HASH' eq ref $type) {
102 # this is to make selection like type => { AR_paid => 1, AP_paid => 1 } work
103 $type = [ grep { $type->{$_} } keys %$type ];
106 my @types = grep { $_ } listify($type);
109 for my $type (@types) {
110 if ( $type eq 'bank' ) {
111 push @filter, ( id => [ \"SELECT chart_id FROM bank_accounts" ] );
112 } elsif ( $type eq 'guv' ) {
113 push @filter, ( category => [ 'I', 'E' ] );
114 } elsif ( $type eq 'balance' ) {
115 push @filter, ( category => [ 'A', 'Q', 'L' ] );
117 push @filter, $class->link_filter($type);
121 return @filter > 2 ? (or => \@filter) : @filter;
124 sub category_filter {
125 my ($class, $category) = @_;
127 return () unless $category;
129 # filter for chart_picker if a category filter was passed via params
131 if ( ref $category eq 'HASH' ) {
132 # this is to make a selection like category => { I => 1, E => 1 } work
133 $category = [ grep { $category->{$_} } keys %$category ];
136 my @categories = grep { $_ } listify($category);
138 return (category => \@categories);
142 my ($class, $link) = @_;
144 return (or => [ link => $link,
145 link => { like => "${link}:\%" },
146 link => { like => "\%:${link}" },
147 link => { like => "\%:${link}:\%" } ]);
151 my ($self, %params) = @_;
153 my $date = $params{date} || DateTime->today;
154 my $cache = $::request->cache('::SL::DB::Chart::get_active_taxkey')->{$date} //= {};
156 require SL::DB::TaxKey;
157 my $tks = SL::DB::Manager::TaxKey->get_all;
158 my %tks_by_id = map { $_->id => $_ } @$tks;
160 my $rows = selectall_hashref_query($::form, $::form->get_standard_dbh, <<"", $date);
161 SELECT DISTINCT ON (chart_id) chart_id, startdate, id
164 ORDER BY chart_id, startdate DESC;
167 $cache->{$_->{chart_id}} = $tks_by_id{$_->{id}};
173 default => [ 'accno', 1 ],
191 SL::DB::Manager::Chart - Manager class for the model for the C<chart> table
197 =item C<link_filter $link>
199 Returns a query builder filter that matches charts whose 'C<link>'
200 field contains C<$link>. Matching is done so that the exact value of
201 C<$link> matches but not if C<$link> is only a substring of a
202 match. Therefore C<$link = 'AR'> will match the column content 'C<AR>'
203 or 'C<AR_paid:AR>' but not 'C<AR_amount>'.
213 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
215 G. Richardson E<lt>information@kivitendo-premium.deE<gt>