Merge branch 'currency'
[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 | .
59     qq|FROM invoice i | .
60     qq|JOIN ar on (i.trans_id = ar.id) | .
61     qq|JOIN parts p on (i.parts_id = p.id) | .
62     qq|LEFT JOIN partsgroup pg on (p.partsgroup_id = pg.id) | .
63     qq|LEFT JOIN customer ct on (ct.id = ar.customer_id) | .
64     qq|LEFT JOIN business b on (ct.business_id = b.id) | .
65     qq|LEFT JOIN employee e ON (ar.employee_id = e.id) | .
66     qq|LEFT JOIN employee e2 ON (ar.salesman_id = e2.id) |;
67
68   my $where = "1 = 1";
69
70   # if employee can only see his own invoices, make sure this also holds for sales report
71   # limits by employees (Bearbeiter), not salesmen!
72   if (!$main::auth->assert('sales_all_edit', 1)) {
73     $where .= " AND ar.employee_id = (select id from employee where login= ?)";
74     push (@values, $form->{login});
75   }
76
77   # Stornierte Rechnungen und Stornorechnungen in invoice rausfiltern
78   # was ist mit Gutschriften?
79   $where .= " AND ar.storno is not true ";
80
81   # Bestandteile von Erzeugnissen herausfiltern
82   $where .= " AND i.assemblyitem is not true ";
83
84   # filter allowed parameters for mainsort and subsort as passed by POST
85   my @databasefields = qw(description customername country partsgroup business salesman month);
86   my ($mainsort) = grep { /^$form->{mainsort}$/ } @databasefields;
87   my ($subsort) = grep { /^$form->{subsort}$/ } @databasefields;
88   die "illegal parameter for mainsort or subsort" unless $mainsort and $subsort;
89
90   my $sortorder;
91   # sorting by month is a special case, we don't want to sort alphabetically by
92   # month name, so we also extract a numerical month in the from YYYYMM to sort
93   # by in case of month sorting
94   # Sorting by month, using description as an example:
95   # Sorting with month as mainsort: ORDER BY nummonth,description,ar.transdate,ar.invnumber
96   # Sorting with month as subsort:  ORDER BY description,nummonth,ar.transdate,ar.invnumber
97   if ($form->{mainsort} eq 'month') {
98     $sortorder .= "nummonth,"
99   } else {
100     $sortorder .= $mainsort . ",";
101   };
102   if ($form->{subsort} eq 'month') {
103     $sortorder .= "nummonth,"
104   } else {
105     $sortorder .= $subsort . ",";
106   };
107   $sortorder .= 'ar.transdate,ar.invnumber';  # Default sorting order after mainsort und subsort
108
109   if ($form->{customer_id}) {
110     $where .= " AND ar.customer_id = ?";
111     push(@values, $form->{customer_id});
112   };
113   if ($form->{customernumber}) {
114     $where .= qq| AND ct.customernumber = ? |;
115     push(@values, $form->{customernumber});
116   }
117   if ($form->{partnumber}) {
118     $where .= qq| AND (p.partnumber ILIKE ?)|;
119     push(@values, '%' . $form->{partnumber} . '%');
120   }
121   if ($form->{partsgroup_id}) {
122     $where .= qq| AND (pg.id = ?)|;
123     push(@values, $form->{partsgroup_id});
124   }
125   if ($form->{country}) {
126     $where .= qq| AND (ct.country ILIKE ?)|;
127     push(@values, '%' . $form->{country} . '%');
128   }
129
130   # when filtering for parts by description we probably want to filter by the description of the part as per the master data
131   # invoice.description may differ due to manually changing the description in the invoice or because of translations of the description
132   # at least in the translation case we probably want the report to also include translated articles, so we have to filter via parts.description
133   if ($form->{description}) {
134     $where .= qq| AND (p.description ILIKE ?)|;
135     push(@values, '%' . $form->{description} . '%');
136   }
137   if ($form->{transdatefrom}) {
138     $where .= " AND ar.transdate >= ?";
139     push(@values, $form->{transdatefrom});
140   }
141   if ($form->{transdateto}) {
142     $where .= " AND ar.transdate <= ?";
143     push(@values, $form->{transdateto});
144   }
145   if ($form->{department}) {
146     my ($null, $department_id) = split /--/, $form->{department};
147     $where .= " AND ar.department_id = ?";
148     push(@values, $department_id);
149   }
150   if ($form->{employee_id}) {
151     $where .= " AND ar.employee_id = ?";
152     push @values, conv_i($form->{employee_id});
153   }
154
155   if ($form->{salesman_id}) {
156     $where .= " AND ar.salesman_id = ?";
157     push @values, conv_i($form->{salesman_id});
158   }
159   if ($form->{project_id}) {
160     $where .=
161       qq|AND ((ar.globalproject_id = ?) OR EXISTS | .
162       qq|  (SELECT * FROM invoice i | .
163       qq|   WHERE i.project_id = ? AND i.trans_id = ar.id))|;
164     push(@values, $form->{"project_id"}, $form->{"project_id"});
165   }
166   if ($form->{business_id}) {
167     $where .= qq| AND ct.business_id = ? |;
168     push(@values, $form->{"business_id"});
169   }
170
171   my ($cvar_where_ct, @cvar_values_ct) = CVar->build_filter_query('module'    => 'CT',
172                                                                   'trans_id_field' => 'ct.id',
173                                                                   'filter'         => $form);
174
175   if ($cvar_where_ct) {
176     $where .= qq| AND ($cvar_where_ct)|;
177     push @values, @cvar_values_ct;
178   }
179
180
181   my ($cvar_where_ic, @cvar_values_ic) = CVar->build_filter_query('module'         => 'IC',
182                                                                   'trans_id_field' => 'p.id',
183                                                                   'filter'         => $form);
184
185   if ($cvar_where_ic) {
186     $where .= qq| AND ($cvar_where_ic)|;
187     push @values, @cvar_values_ic;
188   }
189
190   $query .= " WHERE $where ORDER BY $sortorder "; # LIMIT 5000";
191
192   my @result = selectall_hashref_query($form, $dbh, $query, @values);
193
194   $form->{AR} = [ @result ];
195
196   $main::lxdebug->leave_sub();
197 }
198
199 1;
200