dab0a6284c287bcad5995b5245cdf0d85b4b5c13
[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   # 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};
51   };
52
53   my $gldata = [
54     map(
55       {
56         {
57            transdate => $_->transdate->to_kivitendo,
58            label     => $_->oneline_summary,
59            value     => '',
60            url       => 'gl.pl?action=edit&id=' . $_->id,
61         }
62       }
63       @{$gls}
64     ),
65   ];
66
67   my $ardata = [
68     map(
69       {
70         {
71            transdate => $_->transdate->to_kivitendo,
72            label     => $_->oneline_summary,
73            value     => "",
74            url       => ($_->invoice ? "is" : "ar" ) . '.pl?action=edit&id=' . $_->id,
75         }
76       }
77       @{$ars}
78     ),
79   ];
80
81   my $apdata = [
82     map(
83       {
84         {
85            transdate => $_->transdate->to_kivitendo,
86            label     => $_->oneline_summary,
87            value     => "",
88            id        => ($_->invoice ? "ir" : "ap" ) . '.pl?action=edit&id=' . $_->id,
89         }
90       }
91       @{$aps}
92     ),
93   ];
94
95   my $data;
96   push(@{$data},@{$gldata});
97   push(@{$data},@{$ardata});
98   push(@{$data},@{$apdata});
99
100   @$data = reverse sort { $a->{'transdate'} cmp $b->{'transdate'} } @$data;
101
102   $data;
103 }
104
105 sub select_autocomplete {
106   $::form->{id}
107 }
108
109 sub do_search {
110   my ($self) = @_;
111
112   my $results = $self->query_autocomplete;
113
114   return @$results == 1
115     ? $results->[0]{id}
116     : undef;
117 }
118
119 # TODO: result overview page
120
121 1;