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);
 
  12 __PACKAGE__->run_before('check_auth');
 
  14 sub action_quicksearch {
 
  16   my ($self, %params) = @_;
 
  18   my $limit = $::form->{limit} || 40; # max number of results per type (AR/AP/GL)
 
  19   my $term  = $::form->{term}  || '';
 
  21   my $descriptionquery = { ilike => '%' . $term . '%' };
 
  22   my $referencequery   = { ilike => '%' . $term . '%' };
 
  23   my $apinvnumberquery = { ilike => '%' . $term . '%' };
 
  24   my $namequery        = { ilike => '%' . $term . '%' };
 
  25   my $arinvnumberquery = { ilike => '%' . $term       };
 
  26   # ar match is more restrictive. Left fuzzy beginning so it also matches "Storno zu $INVNUMBER"
 
  27   # and numbers like 000123 if you only enter 123.
 
  28   # When used in quicksearch short numbers like 1 or 11 won't match because of the
 
  29   # ajax autocomplete minlimit of 3 characters
 
  31   my (@glfilter, @arfilter, @apfilter);
 
  33   push( @glfilter, (or => [ description => $descriptionquery, reference => $referencequery ] ) );
 
  34   push( @arfilter, (or => [ invnumber   => $arinvnumberquery, name      => $namequery ] ) );
 
  35   push( @apfilter, (or => [ invnumber   => $apinvnumberquery, name      => $namequery ] ) );
 
  37   my $gls = SL::DB::Manager::GLTransaction->get_all(  query => [ @glfilter ], limit => $limit, sort_by => 'transdate DESC');
 
  38   my $ars = SL::DB::Manager::Invoice->get_all(        query => [ @arfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'customer' ]);
 
  39   my $aps = SL::DB::Manager::PurchaseInvoice->get_all(query => [ @apfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'vendor' ]);
 
  41   # calculate an amount to be displayed for gl transaction
 
  42   foreach my $gl ( @$gls ) {
 
  44     my $acc_trans_lines = SL::DB::Manager::AccTransaction->get_all(query => [ trans_id => $gl->id ]);
 
  45     foreach my $acc_trans_line ( @$acc_trans_lines ) {
 
  46       $amount += $acc_trans_line->amount if $acc_trans_line->amount > 0 ;
 
  48     $gl->{'amount'} = $amount;
 
  55            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
 
  56            label     => $_->abbreviation. ": " . $_->description . " " . $_->reference . " " . $::form->format_amount(\%::myconfig, $_->{'amount'},2). " (" . $_->transdate->to_lxoffice . ")" ,
 
  58            url       => '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 . ")" ,
 
  72            url       => ($_->invoice ? "is" : "ar" ) . '.pl?action=edit&id=' . $_->id,
 
  83            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
 
  84            label     => $_->abbreviation . ": " . $_->invnumber . " " . $_->vendor->name . " " . $::form->format_amount(\%::myconfig, $_->amount,2)  . " (" . $_->transdate->to_lxoffice . ")" ,
 
  86            url       => ($_->invoice ? "ir" : "ap" ) . '.pl?action=edit&id=' . $_->id,
 
  94   push(@{$data},@{$gldata});
 
  95   push(@{$data},@{$ardata});
 
  96   push(@{$data},@{$apdata});
 
  98   @$data = reverse sort { $a->{'transdate_sort'} cmp $b->{'transdate_sort'} } @$data;
 
 100   $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
 
 104   $::auth->assert('general_ledger');