epic-s6ts
[kivitendo-erp.git] / SL / Controller / TopQuickSearch / GLTransaction.pm
1 package SL::Controller::TopQuickSearch::GLTransaction;
2
3 use strict;
4 use parent qw(Rose::Object);
5
6 use SL::DB::GLTransaction;
7 use SL::DB::Invoice;
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);
13
14 sub auth { 'general_ledger|gl_transactions|ap_transactions|ar_transactions' }
15
16 sub name { 'gl_transaction' }
17
18 sub description_config { t8('GL search') }
19
20 sub description_field { t8('GL search') }
21
22 sub query_autocomplete {
23   my ($self, %params) = @_;
24
25   my $limit = $::form->{limit} || 40; # max number of results per type (AR/AP/GL)
26   my $term  = $::form->{term}  || '';
27
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
37
38   my (@glfilter, @arfilter, @apfilter);
39
40   push( @glfilter, (or => [ description => $descriptionquery, reference => $referencequery ] ) );
41   push( @arfilter, (or => [ invnumber   => $arinvnumberquery, name      => $namequery ] ) );
42   push( @apfilter, (or => [ invnumber   => $apinvnumberquery, name      => $namequery ] ) );
43
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' ]);
47
48   my $gldata = [
49     map(
50       {
51         {
52            transdate => $_->transdate->ymd(''), # only used for sorting
53            label     => $_->oneline_summary,
54            value     => '',
55            id        => 'gl.pl?action=edit&id=' . $_->id,
56         }
57       }
58       @{$gls}
59     ),
60   ];
61
62   my $ardata = [
63     map(
64       {
65         {
66            transdate => $_->transdate->ymd(''),
67            label     => $_->oneline_summary,
68            value     => "",
69            id        => ($_->invoice ? "is" : "ar" ) . '.pl?action=edit&id=' . $_->id,
70         }
71       }
72       @{$ars}
73     ),
74   ];
75
76   my $apdata = [
77     map(
78       {
79         {
80            transdate => $_->transdate->ymd(''),
81            label     => $_->oneline_summary,
82            value     => "",
83            id        => ($_->invoice ? "ir" : "ap" ) . '.pl?action=edit&id=' . $_->id,
84         }
85       }
86       @{$aps}
87     ),
88   ];
89
90   my $data;
91   push(@{$data},@{$gldata});
92   push(@{$data},@{$ardata});
93   push(@{$data},@{$apdata});
94
95   @$data = reverse sort { $a->{'transdate'} cmp $b->{'transdate'} } @$data;
96
97   $data;
98 }
99
100 sub select_autocomplete {
101   $::form->{id}
102 }
103
104 sub do_search {
105   my ($self) = @_;
106
107   my $results = $self->query_autocomplete;
108
109   return @$results == 1
110     ? $results->[0]{id}
111     : undef;
112 }
113
114 # TODO: result overview page
115
116 1;