e79f304cbc3ef61ca5ce73083804f5d3ce42cc33
[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 use List::Util qw(sum);
12
13 __PACKAGE__->run_before('check_auth');
14
15 sub action_quicksearch {
16
17   my ($self, %params) = @_;
18
19   my $limit = $::form->{limit} || 40; # max number of results per type (AR/AP/GL)
20   my $term  = $::form->{term}  || '';
21
22   my $descriptionquery = { ilike => '%' . $term . '%' };
23   my $referencequery   = { ilike => '%' . $term . '%' };
24   my $apinvnumberquery = { ilike => '%' . $term . '%' };
25   my $namequery        = { ilike => '%' . $term . '%' };
26   my $arinvnumberquery = { ilike => '%' . $term       };
27   # ar match is more restrictive. Left fuzzy beginning so it also matches "Storno zu $INVNUMBER"
28   # and numbers like 000123 if you only enter 123.
29   # When used in quicksearch short numbers like 1 or 11 won't match because of the
30   # ajax autocomplete minlimit of 3 characters
31
32   my (@glfilter, @arfilter, @apfilter);
33
34   push( @glfilter, (or => [ description => $descriptionquery, reference => $referencequery ] ) );
35   push( @arfilter, (or => [ invnumber   => $arinvnumberquery, name      => $namequery ] ) );
36   push( @apfilter, (or => [ invnumber   => $apinvnumberquery, name      => $namequery ] ) );
37
38   my $gls = SL::DB::Manager::GLTransaction->get_all(  query => [ @glfilter ], limit => $limit, sort_by => 'transdate DESC');
39   my $ars = SL::DB::Manager::Invoice->get_all(        query => [ @arfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'customer' ]);
40   my $aps = SL::DB::Manager::PurchaseInvoice->get_all(query => [ @apfilter ], limit => $limit, sort_by => 'transdate DESC', with_objects => [ 'vendor' ]);
41
42   # use the sum of all credit amounts as the "amount" of the gl transaction
43   foreach my $gl ( @$gls ) {
44     $gl->{'amount'} = sum map { $_->amount if $_->amount > 0 } @{$gl->transactions};
45   };
46
47   my $gldata = [
48     map(
49       {
50         {
51            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
52            label     => $_->abbreviation. ": " . $_->description . " " . $_->reference . " " . $::form->format_amount(\%::myconfig, $_->{'amount'},2). " (" . $_->transdate->to_lxoffice . ")" ,
53            value     => '',
54            url       => 'gl.pl?action=edit&id=' . $_->id,
55         }
56       }
57       @{$gls}
58     ),
59   ];
60
61   my $ardata = [
62     map(
63       {
64         {
65            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
66            label     => $_->abbreviation . ": " . $_->invnumber . "   " . $_->customer->name . " " . $::form->format_amount(\%::myconfig, $_->amount,2)  . " (" . $_->transdate->to_lxoffice . ")" ,
67            value     => "",
68            url       => ($_->invoice ? "is" : "ar" ) . '.pl?action=edit&id=' . $_->id,
69         }
70       }
71       @{$ars}
72     ),
73   ];
74
75   my $apdata = [
76     map(
77       {
78         {
79            transdate => DateTime->from_object(object => $_->transdate)->ymd(),
80            label     => $_->abbreviation . ": " . $_->invnumber . " " . $_->vendor->name . " " . $::form->format_amount(\%::myconfig, $_->amount,2)  . " (" . $_->transdate->to_lxoffice . ")" ,
81            value     => "",
82            url       => ($_->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   $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
97 }
98
99 sub check_auth {
100   $::auth->assert('general_ledger');
101 }
102
103 1;