8eead24de39cdd10848e3bee14fb7865a31034cd
[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' }
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 => 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,
60         }
61       }
62       @{$gls}
63     ),
64   ];
65
66   my $ardata = [
67     map(
68       {
69         {
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,
73         }
74       }
75       @{$ars}
76     ),
77   ];
78
79   my $apdata = [
80     map(
81       {
82         {
83            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
84            label     => $_->abbreviation . ": " . $_->invnumber . " " . $_->vendor->name . " " . $::form->format_amount(\%::myconfig, $_->amount,2)  . " (" . $_->transdate->to_lxoffice . ")" ,
85            value     => "",
86            id        => ($_->invoice ? "ir" : "ap" ) . '.pl?action=edit&id=' . $_->id,
87         }
88       }
89       @{$aps}
90     ),
91   ];
92
93   my $data;
94   push(@{$data},@{$gldata});
95   push(@{$data},@{$ardata});
96   push(@{$data},@{$apdata});
97
98   @$data = reverse sort { $a->{'transdate'} cmp $b->{'transdate'} } @$data;
99
100   $data;
101 }
102
103 sub select_autocomplete {
104   $::form->{id}
105 }
106
107 sub do_search {
108   my ($self) = @_;
109
110   my $results = $self->query_autocomplete;
111
112   return @$results == 1
113     ? $results->[0]{id}
114     : undef;
115 }
116
117 # TODO: result overview page
118
119 1;