FiBu Schellsuche in Headerzeile
[kivitendo-erp.git] / SL / Controller / GL.pm
1 package SL::Controller::GL;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
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
12 __PACKAGE__->run_before('check_auth');
13
14 sub action_quicksearch {
15
16   my ($self, %params) = @_;
17   
18   my $limit = $::form->{limit} || 40; # max number of results per type (AR/AP/GL)
19   my $term  = $::form->{term}  || '';
20   
21   my $descriptionquery = { ilike => '%' . $term . '%' };
22   my $referencequery   = { ilike => '%' . $term . '%' };
23   my $apinvnumberquery = { ilike => '%' . $term . '%' };
24   my $namequery        = { ilike => '%' . $term . '%' };
25   my $arinvnumberquery = { ilike => '%' . $term       };
26   # ar match is more restrictive. Left fuzzy beginning so it also matches "Storno zu $INVNUMBER"
27   # and numbers like 000123 if you only enter 123.
28   # When used in quicksearch short numbers like 1 or 11 won't match because of the
29   # ajax autocomplete minlimit of 3 characters
30
31   my (@glfilter, @arfilter, @apfilter);
32
33   push( @glfilter, (or => [ description => $descriptionquery, reference => $referencequery ] ) );
34   push( @arfilter, (or => [ invnumber   => $arinvnumberquery, name      => $namequery ] ) );
35   push( @apfilter, (or => [ invnumber   => $apinvnumberquery, name      => $namequery ] ) );
36
37   my $gls = SL::DB::Manager::GLTransaction->get_all(  query => [ @glfilter ], limit => $limit, sort_by => 'transdate DESC');
38   my $ars = SL::DB::Manager::Invoice->get_all(        query => [ @arfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'customer' ]);
39   my $aps = SL::DB::Manager::PurchaseInvoice->get_all(query => [ @apfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'vendor' ]);
40
41   # calculate an amount to be displayed for gl transaction
42   foreach my $gl ( @$gls ) {
43     my $amount = 0;
44     my $acc_trans_lines = SL::DB::Manager::AccTransaction->get_all(query => [ trans_id => $gl->id ]);
45     foreach my $acc_trans_line ( @$acc_trans_lines ) {
46       $amount += $acc_trans_line->amount if $acc_trans_line->amount > 0 ;
47     };
48     $gl->{'amount'} = $amount;
49   };
50
51   my $gldata = [
52     map(
53       {
54         {
55            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
56            label     => $_->abbreviation. ": " . $_->description . " " . $_->reference . " " . $::form->format_amount(\%::myconfig, $_->{'amount'},2). " (" . $_->transdate->to_lxoffice . ")" ,
57            value     => '',
58            url       => '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            value     => "",
72            url       => ($_->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            url       => ($_->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_sort'} cmp $b->{'transdate_sort'} } @$data;
99
100   $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
101 }
102
103 sub check_auth {
104   $::auth->assert('general_ledger');
105 }
106
107 1;