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 List::Util qw(sum);
 
  13 sub auth { 'general_ledger' }
 
  15 sub name { 'gl_transaction' }
 
  17 sub description_config { t8('GL search') }
 
  19 sub description_field { t8('GL search') }
 
  21 sub query_autocomplete {
 
  22   my ($self, %params) = @_;
 
  24   my $limit = $::form->{limit} || 40; # max number of results per type (AR/AP/GL)
 
  25   my $term  = $::form->{term}  || '';
 
  27   my $descriptionquery = { ilike => '%' . $term . '%' };
 
  28   my $referencequery   = { ilike => '%' . $term . '%' };
 
  29   my $apinvnumberquery = { ilike => '%' . $term . '%' };
 
  30   my $namequery        = { ilike => '%' . $term . '%' };
 
  31   my $arinvnumberquery = { ilike => '%' . $term       };
 
  32   # ar match is more restrictive. Left fuzzy beginning so it also matches "Storno zu $INVNUMBER"
 
  33   # and numbers like 000123 if you only enter 123.
 
  34   # When used in quicksearch short numbers like 1 or 11 won't match because of the
 
  35   # ajax autocomplete minlimit of 3 characters
 
  37   my (@glfilter, @arfilter, @apfilter);
 
  39   push( @glfilter, (or => [ description => $descriptionquery, reference => $referencequery ] ) );
 
  40   push( @arfilter, (or => [ invnumber   => $arinvnumberquery, name      => $namequery ] ) );
 
  41   push( @apfilter, (or => [ invnumber   => $apinvnumberquery, name      => $namequery ] ) );
 
  43   my $gls = SL::DB::Manager::GLTransaction->get_all(  query => [ @glfilter ], limit => $limit, sort_by => 'transdate DESC');
 
  44   my $ars = SL::DB::Manager::Invoice->get_all(        query => [ @arfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'customer' ]);
 
  45   my $aps = SL::DB::Manager::PurchaseInvoice->get_all(query => [ @apfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'vendor' ]);
 
  47   # use the sum of all credit amounts as the "amount" of the gl transaction
 
  48   foreach my $gl ( @$gls ) {
 
  49     $gl->{'amount'} = sum map { $_->amount if $_->amount > 0 } @{$gl->transactions};
 
  56            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
 
  57            label     => $_->abbreviation. ": " . $_->description . " " . $_->reference . " " . $::form->format_amount(\%::myconfig, $_->{'amount'},2). " (" . $_->transdate->to_lxoffice . ")" ,
 
  58            id        => 'gl.pl?action=edit&id=' . $_->id,
 
  69            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
 
  70            label     => $_->abbreviation . ": " . $_->invnumber . "   " . $_->customer->name . " " . $::form->format_amount(\%::myconfig, $_->amount,2)  . " (" . $_->transdate->to_lxoffice . ")" ,
 
  71            id        => ($_->invoice ? "is" : "ar" ) . '.pl?action=edit&id=' . $_->id,
 
  82            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
 
  83            label     => $_->abbreviation . ": " . $_->invnumber . " " . $_->vendor->name . " " . $::form->format_amount(\%::myconfig, $_->amount,2)  . " (" . $_->transdate->to_lxoffice . ")" ,
 
  85            id        => ($_->invoice ? "ir" : "ap" ) . '.pl?action=edit&id=' . $_->id,
 
  93   push(@{$data},@{$gldata});
 
  94   push(@{$data},@{$ardata});
 
  95   push(@{$data},@{$apdata});
 
  97   @$data = reverse sort { $a->{'transdate'} cmp $b->{'transdate'} } @$data;
 
 102 sub select_autocomplete {
 
 109   my $results = $self->query_autocomplete;
 
 111   return @$results == 1
 
 116 # TODO: result overview page