Verkaufsbericht - exchangerate nicht mehr als Subselect
[kivitendo-erp.git] / SL / VK.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
30 #
31 # Sold Items report
32 #
33 #======================================================================
34
35 package VK;
36
37 use SL::DBUtils;
38 use SL::IO;
39 use SL::MoreCommon;
40
41 use strict;
42
43 sub invoice_transactions {
44   $main::lxdebug->enter_sub();
45
46   my ($self, $myconfig, $form) = @_;
47
48   # connect to database
49   my $dbh = $form->get_standard_dbh($myconfig);
50
51   my @values;
52
53   # default usage: always use parts.description for (sub-)totalling and in header and subheader lines
54   # but use invoice.description in article mode
55   # so we extract both versions in our query and later overwrite the description in article mode
56
57   my $query =
58     qq|SELECT ct.id as customerid, ct.name as customername,ct.customernumber,ct.country,ar.invnumber,ar.id,ar.transdate,p.partnumber,p.description as description, pg.partsgroup,i.parts_id,i.qty,i.price_factor,i.discount,i.description as invoice_description,i.lastcost,i.sellprice,i.fxsellprice,i.marge_total,i.marge_percent,i.unit,b.description as business,e.name as employee,e2.name as salesman, to_char(ar.transdate,'Month') as month, to_char(ar.transdate, 'YYYYMM') as nummonth, p.unit as parts_unit, p.weight, ar.taxincluded | .
59     qq|, COALESCE(er.buy, 1) | .
60     qq|FROM invoice i | .
61     qq|RIGHT JOIN ar on (i.trans_id = ar.id) | .
62     qq|JOIN parts p on (i.parts_id = p.id) | .
63     qq|LEFT JOIN exchangerate er on (er.transdate = ar.transdate and ar.currency_id = er.currency_id) | .
64     qq|LEFT JOIN partsgroup pg on (p.partsgroup_id = pg.id) | .
65     qq|LEFT JOIN customer ct on (ct.id = ar.customer_id) | .
66     qq|LEFT JOIN business b on (ct.business_id = b.id) | .
67     qq|LEFT JOIN employee e ON (ar.employee_id = e.id) | .
68     qq|LEFT JOIN employee e2 ON (ar.salesman_id = e2.id) |;
69
70   my $where = "1 = 1";
71
72   # if employee can only see his own invoices, make sure this also holds for sales report
73   # limits by employees (Bearbeiter), not salesmen!
74   if (!$main::auth->assert('sales_all_edit', 1)) {
75     $where .= " AND ar.employee_id = (select id from employee where login= ?)";
76     push (@values, $::myconfig{login});
77   }
78
79   # Stornierte Rechnungen und Stornorechnungen in invoice rausfiltern
80   # was ist mit Gutschriften?
81   $where .= " AND ar.storno is not true ";
82
83   # Bestandteile von Erzeugnissen herausfiltern
84   $where .= " AND i.assemblyitem is not true ";
85
86   # filter allowed parameters for mainsort and subsort as passed by POST
87   my @databasefields = qw(description customername country partsgroup business salesman month);
88   my ($mainsort) = grep { /^$form->{mainsort}$/ } @databasefields;
89   my ($subsort) = grep { /^$form->{subsort}$/ } @databasefields;
90   die "illegal parameter for mainsort or subsort" unless $mainsort and $subsort;
91
92   my $sortorder;
93   # sorting by month is a special case, we don't want to sort alphabetically by
94   # month name, so we also extract a numerical month in the from YYYYMM to sort
95   # by in case of month sorting
96   # Sorting by month, using description as an example:
97   # Sorting with month as mainsort: ORDER BY nummonth,description,ar.transdate,ar.invnumber
98   # Sorting with month as subsort:  ORDER BY description,nummonth,ar.transdate,ar.invnumber
99   if ($form->{mainsort} eq 'month') {
100     $sortorder .= "nummonth,"
101   } else {
102     $sortorder .= $mainsort . ",";
103   };
104   if ($form->{subsort} eq 'month') {
105     $sortorder .= "nummonth,"
106   } else {
107     $sortorder .= $subsort . ",";
108   };
109   $sortorder .= 'ar.transdate,ar.invnumber';  # Default sorting order after mainsort und subsort
110
111   if ($form->{customer_id}) {
112     $where .= " AND ar.customer_id = ?";
113     push(@values, $form->{customer_id});
114   } elsif ($form->{customer}) {
115     $where .= " AND ct.name ILIKE ?";
116     push(@values, like($form->{customer}));
117   }
118   if ($form->{customernumber}) {
119     $where .= qq| AND ct.customernumber = ? |;
120     push(@values, $form->{customernumber});
121   }
122   if ($form->{partnumber}) {
123     $where .= qq| AND (p.partnumber ILIKE ?)|;
124     push(@values, like($form->{partnumber}));
125   }
126   if ($form->{partsgroup_id}) {
127     $where .= qq| AND (pg.id = ?)|;
128     push(@values, $form->{partsgroup_id});
129   }
130   if ($form->{country}) {
131     $where .= qq| AND (ct.country ILIKE ?)|;
132     push(@values, like($form->{country}));
133   }
134
135   # when filtering for parts by description we probably want to filter by the description of the part as per the master data
136   # invoice.description may differ due to manually changing the description in the invoice or because of translations of the description
137   # at least in the translation case we probably want the report to also include translated articles, so we have to filter via parts.description
138   if ($form->{description}) {
139     $where .= qq| AND (p.description ILIKE ?)|;
140     push(@values, like($form->{description}));
141   }
142   if ($form->{transdatefrom}) {
143     $where .= " AND ar.transdate >= ?";
144     push(@values, $form->{transdatefrom});
145   }
146   if ($form->{transdateto}) {
147     $where .= " AND ar.transdate <= ?";
148     push(@values, $form->{transdateto});
149   }
150   if ($form->{department}) {
151     my ($null, $department_id) = split /--/, $form->{department};
152     $where .= " AND ar.department_id = ?";
153     push(@values, $department_id);
154   }
155   if ($form->{employee_id}) {
156     $where .= " AND ar.employee_id = ?";
157     push @values, conv_i($form->{employee_id});
158   }
159
160   if ($form->{salesman_id}) {
161     $where .= " AND ar.salesman_id = ?";
162     push @values, conv_i($form->{salesman_id});
163   }
164   if ($form->{project_id}) {
165     $where .=
166       qq|AND ((ar.globalproject_id = ?) OR EXISTS | .
167       qq|  (SELECT * FROM invoice i | .
168       qq|   WHERE i.project_id = ? AND i.trans_id = ar.id))|;
169     push(@values, $form->{"project_id"}, $form->{"project_id"});
170   }
171   if ($form->{business_id}) {
172     $where .= qq| AND ct.business_id = ? |;
173     push(@values, $form->{"business_id"});
174   }
175
176   my ($cvar_where_ct, @cvar_values_ct) = CVar->build_filter_query('module'    => 'CT',
177                                                                   'trans_id_field' => 'ct.id',
178                                                                   'filter'         => $form);
179
180   if ($cvar_where_ct) {
181     $where .= qq| AND ($cvar_where_ct)|;
182     push @values, @cvar_values_ct;
183   }
184
185
186   my ($cvar_where_ic, @cvar_values_ic) = CVar->build_filter_query('module'         => 'IC',
187                                                                   'trans_id_field' => 'p.id',
188                                                                   'filter'         => $form);
189
190   if ($cvar_where_ic) {
191     $where .= qq| AND ($cvar_where_ic)|;
192     push @values, @cvar_values_ic;
193   }
194
195   $query .= " WHERE $where ORDER BY $sortorder "; # LIMIT 5000";
196
197   my @result = selectall_hashref_query($form, $dbh, $query, @values);
198
199   $form->{AR} = [ @result ];
200
201   $main::lxdebug->leave_sub();
202 }
203
204 1;
205