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