77854ff4cca9cda1443ee11310f74cdcb2a4a795
[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 List::Util qw(sum);
12
13 sub auth { 'general_ledger' }
14
15 sub name { 'gl_transaction' }
16
17 sub description_config { t8('GL search') }
18
19 sub description_field { t8('GL search') }
20
21 sub query_autocomplete {
22   my ($self, %params) = @_;
23
24   my $limit = $::form->{limit} || 40; # max number of results per type (AR/AP/GL)
25   my $term  = $::form->{term}  || '';
26
27   my $descriptionquery = { ilike => '%' . $term . '%' };
28   my $referencequery   = { ilike => '%' . $term . '%' };
29   my $apinvnumberquery = { ilike => '%' . $term . '%' };
30   my $namequery        = { ilike => '%' . $term . '%' };
31   my $arinvnumberquery = { ilike => '%' . $term       };
32   # ar match is more restrictive. Left fuzzy beginning so it also matches "Storno zu $INVNUMBER"
33   # and numbers like 000123 if you only enter 123.
34   # When used in quicksearch short numbers like 1 or 11 won't match because of the
35   # ajax autocomplete minlimit of 3 characters
36
37   my (@glfilter, @arfilter, @apfilter);
38
39   push( @glfilter, (or => [ description => $descriptionquery, reference => $referencequery ] ) );
40   push( @arfilter, (or => [ invnumber   => $arinvnumberquery, name      => $namequery ] ) );
41   push( @apfilter, (or => [ invnumber   => $apinvnumberquery, name      => $namequery ] ) );
42
43   my $gls = SL::DB::Manager::GLTransaction->get_all(  query => [ @glfilter ], limit => $limit, sort_by => 'transdate DESC');
44   my $ars = SL::DB::Manager::Invoice->get_all(        query => [ @arfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'customer' ]);
45   my $aps = SL::DB::Manager::PurchaseInvoice->get_all(query => [ @apfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'vendor' ]);
46
47   # use the sum of all credit amounts as the "amount" of the gl transaction
48   foreach my $gl ( @$gls ) {
49     $gl->{'amount'} = sum map { $_->amount if $_->amount > 0 } @{$gl->transactions};
50   };
51
52   my $gldata = [
53     map(
54       {
55         {
56            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
57            label     => $_->abbreviation. ": " . $_->description . " " . $_->reference . " " . $::form->format_amount(\%::myconfig, $_->{'amount'},2). " (" . $_->transdate->to_lxoffice . ")" ,
58            id        => 'gl.pl?action=edit&id=' . $_->id,
59         }
60       }
61       @{$gls}
62     ),
63   ];
64
65   my $ardata = [
66     map(
67       {
68         {
69            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
70            label     => $_->abbreviation . ": " . $_->invnumber . "   " . $_->customer->name . " " . $::form->format_amount(\%::myconfig, $_->amount,2)  . " (" . $_->transdate->to_lxoffice . ")" ,
71            id        => ($_->invoice ? "is" : "ar" ) . '.pl?action=edit&id=' . $_->id,
72         }
73       }
74       @{$ars}
75     ),
76   ];
77
78   my $apdata = [
79     map(
80       {
81         {
82            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
83            label     => $_->abbreviation . ": " . $_->invnumber . " " . $_->vendor->name . " " . $::form->format_amount(\%::myconfig, $_->amount,2)  . " (" . $_->transdate->to_lxoffice . ")" ,
84            value     => "",
85            id        => ($_->invoice ? "ir" : "ap" ) . '.pl?action=edit&id=' . $_->id,
86         }
87       }
88       @{$aps}
89     ),
90   ];
91
92   my $data;
93   push(@{$data},@{$gldata});
94   push(@{$data},@{$ardata});
95   push(@{$data},@{$apdata});
96
97   @$data = reverse sort { $a->{'transdate'} cmp $b->{'transdate'} } @$data;
98
99   $data;
100 }
101
102 sub select_autocomplete {
103   $::form->{id}
104 }
105
106 sub do_search {
107   my ($self) = @_;
108
109   my $results = $self->query_autocomplete;
110
111   return @$results == 1
112     ? $results->[0]{id}
113     : undef;
114 }
115
116 # TODO: result overview page
117
118 1;