]> wagnertech.de Git - mfinanz.git/blob - SL/Controller/TopQuickSearch/GLTransaction.pm
Merge branch 'master' of http://wagnertech.de/git/mfinanz
[mfinanz.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 $arinvnumber_left_query  = { ilike => '%' . SL::Util::trim($term) };
33   my $arinvnumber_right_query = { ilike => SL::Util::trim($term) . '%' };
34   # ar match is more restrictive. Left fuzzy beginning so it also matches "Storno zu $INVNUMBER"
35   # and numbers like 000123 if you only enter 123. Right beginning searches from the begin.
36
37   my (@glfilter, @arfilter, @apfilter);
38
39   push( @glfilter, (or => [ description => $descriptionquery, reference => $referencequery ] ) );
40   push( @arfilter, (or => [ invnumber   => $arinvnumber_left_query, invnumber => $arinvnumber_right_query, 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   my $gldata = [
48     map(
49       {
50         {
51            transdate => $_->transdate->ymd(''), # only used for sorting
52            label     => $_->oneline_summary,
53            value     => '',
54            id        => 'gl.pl?action=edit&id=' . $_->id,
55         }
56       }
57       @{$gls}
58     ),
59   ];
60
61   my $ardata = [
62     map(
63       {
64         {
65            transdate => $_->transdate->ymd(''),
66            label     => $_->oneline_summary,
67            value     => "",
68            id        => ($_->invoice ? "is" : "ar" ) . '.pl?action=edit&id=' . $_->id,
69         }
70       }
71       @{$ars}
72     ),
73   ];
74
75   my $apdata = [
76     map(
77       {
78         {
79            transdate => $_->transdate->ymd(''),
80            label     => $_->oneline_summary,
81            value     => "",
82            id        => ($_->invoice ? "ir" : "ap" ) . '.pl?action=edit&id=' . $_->id,
83         }
84       }
85       @{$aps}
86     ),
87   ];
88
89   my $data;
90   push(@{$data},@{$gldata});
91   push(@{$data},@{$ardata});
92   push(@{$data},@{$apdata});
93
94   @$data = reverse sort { $a->{'transdate'} cmp $b->{'transdate'} } @$data;
95
96   $data;
97 }
98
99 sub select_autocomplete {
100   $::form->{id}
101 }
102
103 sub do_search {
104   my ($self) = @_;
105
106   my $results = $self->query_autocomplete;
107
108   return @$results == 1
109     ? $results->[0]{id}
110     : undef;
111 }
112
113 # TODO: result overview page
114
115 1;