1 package SL::Controller::TopQuickSearch::GLTransaction;
 
   4 use parent qw(Rose::Object);
 
   6 use SL::DB::GLTransaction;
 
   8 use SL::DB::PurchaseInvoice;
 
   9 use SL::DB::AccTransaction;
 
  10 use SL::Locale::String qw(t8);
 
  11 use SL::DBUtils qw(like);
 
  12 use List::Util qw(sum);
 
  14 sub auth { 'general_ledger|gl_transactions|ap_transactions|ar_transactions' }
 
  16 sub name { 'gl_transaction' }
 
  18 sub description_config { t8('GL search') }
 
  20 sub description_field { t8('GL search') }
 
  22 sub query_autocomplete {
 
  23   my ($self, %params) = @_;
 
  25   my $limit = $::form->{limit} || 40; # max number of results per type (AR/AP/GL)
 
  26   my $term  = $::form->{term}  || '';
 
  28   my $descriptionquery = { ilike => like($term) };
 
  29   my $referencequery   = { ilike => like($term) };
 
  30   my $apinvnumberquery = { ilike => like($term) };
 
  31   my $namequery        = { ilike => like($term) };
 
  32   my $arinvnumberquery = { ilike => '%' . SL::Util::trim($term) };
 
  33   # ar match is more restrictive. Left fuzzy beginning so it also matches "Storno zu $INVNUMBER"
 
  34   # and numbers like 000123 if you only enter 123.
 
  35   # When used in quicksearch short numbers like 1 or 11 won't match because of the
 
  36   # ajax autocomplete minlimit of 3 characters
 
  38   my (@glfilter, @arfilter, @apfilter);
 
  40   push( @glfilter, (or => [ description => $descriptionquery, reference => $referencequery ] ) );
 
  41   push( @arfilter, (or => [ invnumber   => $arinvnumberquery, name      => $namequery ] ) );
 
  42   push( @apfilter, (or => [ invnumber   => $apinvnumberquery, name      => $namequery ] ) );
 
  44   my $gls = SL::DB::Manager::GLTransaction->get_all(  query => [ @glfilter ], limit => $limit, sort_by => 'transdate DESC');
 
  45   my $ars = SL::DB::Manager::Invoice->get_all(        query => [ @arfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'customer' ]);
 
  46   my $aps = SL::DB::Manager::PurchaseInvoice->get_all(query => [ @apfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'vendor' ]);
 
  52            transdate => $_->transdate->ymd(''), # only used for sorting
 
  53            label     => $_->oneline_summary,
 
  55            id        => 'gl.pl?action=edit&id=' . $_->id,
 
  66            transdate => $_->transdate->ymd(''),
 
  67            label     => $_->oneline_summary,
 
  69            id        => ($_->invoice ? "is" : "ar" ) . '.pl?action=edit&id=' . $_->id,
 
  80            transdate => $_->transdate->ymd(''),
 
  81            label     => $_->oneline_summary,
 
  83            id        => ($_->invoice ? "ir" : "ap" ) . '.pl?action=edit&id=' . $_->id,
 
  91   push(@{$data},@{$gldata});
 
  92   push(@{$data},@{$ardata});
 
  93   push(@{$data},@{$apdata});
 
  95   @$data = reverse sort { $a->{'transdate'} cmp $b->{'transdate'} } @$data;
 
 100 sub select_autocomplete {
 
 107   my $results = $self->query_autocomplete;
 
 109   return @$results == 1
 
 114 # TODO: result overview page