1 package SL::Controller::GL;
 
   4 use parent qw(SL::Controller::Base);
 
   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 __PACKAGE__->run_before('check_auth');
 
  15 sub action_quicksearch {
 
  17   my ($self, %params) = @_;
 
  19   my $limit = $::form->{limit} || 40; # max number of results per type (AR/AP/GL)
 
  20   my $term  = $::form->{term}  || '';
 
  22   my $descriptionquery = { ilike => '%' . $term . '%' };
 
  23   my $referencequery   = { ilike => '%' . $term . '%' };
 
  24   my $apinvnumberquery = { ilike => '%' . $term . '%' };
 
  25   my $namequery        = { ilike => '%' . $term . '%' };
 
  26   my $arinvnumberquery = { ilike => '%' . $term       };
 
  27   # ar match is more restrictive. Left fuzzy beginning so it also matches "Storno zu $INVNUMBER"
 
  28   # and numbers like 000123 if you only enter 123.
 
  29   # When used in quicksearch short numbers like 1 or 11 won't match because of the
 
  30   # ajax autocomplete minlimit of 3 characters
 
  32   my (@glfilter, @arfilter, @apfilter);
 
  34   push( @glfilter, (or => [ description => $descriptionquery, reference => $referencequery ] ) );
 
  35   push( @arfilter, (or => [ invnumber   => $arinvnumberquery, name      => $namequery ] ) );
 
  36   push( @apfilter, (or => [ invnumber   => $apinvnumberquery, name      => $namequery ] ) );
 
  38   my $gls = SL::DB::Manager::GLTransaction->get_all(  query => [ @glfilter ], limit => $limit, sort_by => 'transdate DESC');
 
  39   my $ars = SL::DB::Manager::Invoice->get_all(        query => [ @arfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'customer' ]);
 
  40   my $aps = SL::DB::Manager::PurchaseInvoice->get_all(query => [ @apfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'vendor' ]);
 
  42   # use the sum of all credit amounts as the "amount" of the gl transaction
 
  43   foreach my $gl ( @$gls ) {
 
  44     $gl->{'amount'} = sum map { $_->amount if $_->amount > 0 } @{$gl->transactions};
 
  51            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
 
  52            label     => $_->abbreviation. ": " . $_->description . " " . $_->reference . " " . $::form->format_amount(\%::myconfig, $_->{'amount'},2). " (" . $_->transdate->to_lxoffice . ")" ,
 
  54            url       => 'gl.pl?action=edit&id=' . $_->id,
 
  65            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
 
  66            label     => $_->abbreviation . ": " . $_->invnumber . "   " . $_->customer->name . " " . $::form->format_amount(\%::myconfig, $_->amount,2)  . " (" . $_->transdate->to_lxoffice . ")" ,
 
  68            url       => ($_->invoice ? "is" : "ar" ) . '.pl?action=edit&id=' . $_->id,
 
  79            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
 
  80            label     => $_->abbreviation . ": " . $_->invnumber . " " . $_->vendor->name . " " . $::form->format_amount(\%::myconfig, $_->amount,2)  . " (" . $_->transdate->to_lxoffice . ")" ,
 
  82            url       => ($_->invoice ? "ir" : "ap" ) . '.pl?action=edit&id=' . $_->id,
 
  90   push(@{$data},@{$gldata});
 
  91   push(@{$data},@{$ardata});
 
  92   push(@{$data},@{$apdata});
 
  94   @$data = reverse sort { $a->{'transdate'} cmp $b->{'transdate'} } @$data;
 
  96   $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
 
 100   $::auth->assert('general_ledger');