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);
 
  16 sub object_class { 'SL::DB::Chart' }
 
  18 __PACKAGE__->make_manager_methods;
 
  20 __PACKAGE__->add_filter_specs(
 
  22     my ($key, $value) = @_;
 
  23     return __PACKAGE__->type_filter($value);
 
  26     my ($key, $value) = @_;
 
  27     return __PACKAGE__->category_filter($value);
 
  29   selected_category => sub {
 
  30     my ($key, $value) = @_;
 
  31     return __PACKAGE__->selected_category_filter($value);
 
  34     my ($key, $value) = @_;
 
  35     return or => [ map { $_ => $value } qw(accno description) ]
 
  38     my ($key, $value) = @_;
 
  39     return __PACKAGE__->booked_filter($value);
 
  44   my ($class, $booked) = @_;
 
  50      push @filter, ( id => [ \"SELECT distinct chart_id FROM acc_trans" ] );
 
  56 sub selected_category_filter {
 
  57   my ($class, $selected_categories) = @_;
 
  59   my @selected_categories = @$selected_categories;
 
  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.
 
  64   return (category => \@$selected_categories);
 
  68   my ($class, $type) = @_;
 
  70   # filter by link or several defined custom types
 
  74   return () unless $type;
 
  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 ];
 
  81   my @types = grep { $_ } listify($type);
 
  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' ] );
 
  92       push @filter, $class->link_filter($type);
 
  96   return @filter > 2 ? (or => \@filter) : @filter;
 
 100   my ($class, $category) = @_;
 
 102   return () unless $category;
 
 104   # filter for chart_picker if a category filter was passed via params
 
 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 ];
 
 111   my @categories = grep { $_ } listify($category);
 
 113   return (category => \@categories);
 
 117   my ($class, $link) = @_;
 
 119   return (or => [ link => $link,
 
 120                   link => { like => "${link}:\%"    },
 
 121                   link => { like => "\%:${link}"    },
 
 122                   link => { like => "\%:${link}:\%" } ]);
 
 126   my ($self, %params) = @_;
 
 128   my $date  = $params{date} || DateTime->today;
 
 129   my $cache = $::request->cache('::SL::DB::Chart::get_active_taxkey')->{$date} //= {};
 
 131   require SL::DB::TaxKey;
 
 132   my $tks = SL::DB::Manager::TaxKey->get_all;
 
 133   my %tks_by_id = map { $_->id => $_ } @$tks;
 
 135   my $rows = selectall_hashref_query($::form, $::form->get_standard_dbh, <<"", $date);
 
 136     SELECT DISTINCT ON (chart_id) chart_id, startdate, id
 
 139     ORDER BY chart_id, startdate DESC;
 
 142     $cache->{$_->{chart_id}} = $tks_by_id{$_->{id}};
 
 148     default  => [ 'accno', 1 ],
 
 166 SL::DB::Manager::Chart - Manager class for the model for the C<chart> table
 
 172 =item C<link_filter $link>
 
 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>'.
 
 188 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
 
 190 G. Richardson E<lt>information@kivitendo-premium.deE<gt>