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' ]);
48 # use the sum of all credit amounts as the "amount" of the gl transaction
49 foreach my $gl ( @$gls ) {
50 $gl->{'amount'} = sum map { $_->amount if $_->amount > 0 } @{$gl->transactions};
57 transdate => DateTime->from_object(object => $_->transdate)->ymd(),
58 label => $_->abbreviation. ": " . $_->description . " " . $_->reference . " " . $::form->format_amount(\%::myconfig, $_->{'amount'},2). " (" . $_->transdate->to_lxoffice . ")" ,
59 id => 'gl.pl?action=edit&id=' . $_->id,
70 transdate => DateTime->from_object(object => $_->transdate)->ymd(),
71 label => $_->abbreviation . ": " . $_->invnumber . " " . $_->customer->name . " " . $::form->format_amount(\%::myconfig, $_->amount,2) . " (" . $_->transdate->to_lxoffice . ")" ,
72 id => ($_->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 id => ($_->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'} cmp $b->{'transdate'} } @$data;
103 sub select_autocomplete {
110 my $results = $self->query_autocomplete;
112 return @$results == 1
117 # TODO: result overview page