Merge branch 'master' of vc.linet-services.de:public/lx-office-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   my $query =
54     qq|SELECT cus.name,cus.customernumber,ar.invnumber,ar.id,ar.transdate,p.partnumber,i.parts_id,i.qty,i.price_factor,i.discount,i.description,i.lastcost,i.sellprice,i.marge_total,i.marge_percent,i.unit | .
55     qq|FROM invoice i | .  
56     qq|join ar on (i.trans_id = ar.id) | .
57     qq|join parts p on (i.parts_id = p.id) | .
58     qq|join customer cus on (cus.id = ar.customer_id) |;
59
60   my $where = "1 = 1";
61
62   # Stornierte Rechnungen und Stornorechnungen in invoice rausfiltern
63   $where .= " AND ar.storno is not true ";
64
65   # Bestandteile von Erzeugnissen herausfiltern
66   $where .= " AND i.assemblyitem is not true ";
67
68   my $sortorder = "cus.name,i.parts_id,ar.transdate";
69   if ($form->{sortby} eq 'artikelsort') {
70     $sortorder = "i.parts_id,cus.name,ar.transdate";
71   };
72
73   if ($form->{customer_id}) {
74     $where .= " AND ar.customer_id = ?";
75     push(@values, $form->{customer_id});
76   };
77   if ($form->{customernumber}) {
78     $where .= qq| AND cus.customernumber = ? |;
79     push(@values, $form->{customernumber});
80   }
81   if ($form->{partnumber}) {
82     $where .= qq| AND (p.partnumber ILIKE ?)|;
83     push(@values, '%' . $form->{partnumber} . '%');
84   }
85   # nimmt man description am Besten aus invoice oder parts?
86   if ($form->{description}) {
87     $where .= qq| AND (i.description ILIKE ?)|;
88     push(@values, '%' . $form->{description} . '%');
89   }
90   if ($form->{transdatefrom}) {
91     $where .= " AND ar.transdate >= ?";
92     push(@values, $form->{transdatefrom});
93   }
94   if ($form->{transdateto}) {
95     $where .= " AND ar.transdate <= ?";
96     push(@values, $form->{transdateto});
97   }
98   if ($form->{department}) {
99     my ($null, $department_id) = split /--/, $form->{department};
100     $where .= " AND ar.department_id = ?";
101     push(@values, $department_id);
102   }
103   if ($form->{project_id}) {
104     $where .=
105       qq|AND ((ar.globalproject_id = ?) OR EXISTS | .
106       qq|  (SELECT * FROM invoice i | .
107       qq|   WHERE i.project_id = ? AND i.trans_id = ar.id))|;
108     push(@values, $form->{"project_id"}, $form->{"project_id"});
109   }
110
111   $query .= " WHERE $where ORDER BY $sortorder";
112
113   my @result = selectall_hashref_query($form, $dbh, $query, @values);
114
115   $form->{AR} = [ @result ];
116
117   $main::lxdebug->leave_sub();
118 }
119
120 1;
121