1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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 #======================================================================
33 #======================================================================
43 sub invoice_transactions {
44 $main::lxdebug->enter_sub();
46 my ($self, $myconfig, $form) = @_;
49 my $dbh = $form->get_standard_dbh($myconfig);
54 qq|SELECT ct.id as customerid, ct.name as customername,ct.customernumber,ct.country,ar.invnumber,ar.id,ar.transdate,p.partnumber,pg.partsgroup,i.parts_id,i.qty,i.price_factor,i.discount,i.description as description,i.lastcost,i.sellprice,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 | .
56 qq|JOIN ar on (i.trans_id = ar.id) | .
57 qq|JOIN parts p on (i.parts_id = p.id) | .
58 qq|LEFT JOIN partsgroup pg on (p.partsgroup_id = pg.id) | .
59 qq|LEFT JOIN customer ct on (ct.id = ar.customer_id) | .
60 qq|LEFT JOIN business b on (ct.business_id = b.id) | .
61 qq|LEFT JOIN employee e ON (ar.employee_id = e.id) | .
62 qq|LEFT JOIN employee e2 ON (ar.salesman_id = e2.id) |;
66 # if employee can only see his own invoices, make sure this also holds for sales report
67 # limits by employees (Bearbeiter), not salesmen!
68 if (!$main::auth->assert('sales_all_edit', 1)) {
69 $where .= " AND ar.employee_id = (select id from employee where login= ?)";
70 push (@values, $form->{login});
73 # Stornierte Rechnungen und Stornorechnungen in invoice rausfiltern
74 # was ist mit Gutschriften?
75 $where .= " AND ar.storno is not true ";
77 # Bestandteile von Erzeugnissen herausfiltern
78 $where .= " AND i.assemblyitem is not true ";
81 # sorting by month is a special case:
82 # Sorting by month, using salesman as an example:
83 # Sorting with month as mainsort: ORDER BY month,salesman,ar.transdate,ar.invnumber
84 # Sorting with month as subsort: ORDER BY salesman,ar.transdate,month,ar.invnumber
85 if ($form->{mainsort} eq 'month') {
86 $sortorder .= "ar.transdate,month,"
88 $sortorder .= $form->{mainsort} . ",";
90 if ($form->{subsort} eq 'month') {
91 $sortorder .= "ar.transdate,month,"
93 $sortorder .= $form->{subsort} . ",";
95 $sortorder .= 'ar.transdate,' unless $form->{subsort} eq 'month';
96 $sortorder .= 'ar.invnumber';
98 # $sortorder =~ s/month/ar.transdate/;
100 if ($form->{customer_id}) {
101 $where .= " AND ar.customer_id = ?";
102 push(@values, $form->{customer_id});
104 if ($form->{customernumber}) {
105 $where .= qq| AND ct.customernumber = ? |;
106 push(@values, $form->{customernumber});
108 if ($form->{partnumber}) {
109 $where .= qq| AND (p.partnumber ILIKE ?)|;
110 push(@values, '%' . $form->{partnumber} . '%');
112 if ($form->{partsgroup_id}) {
113 $where .= qq| AND (pg.id = ?)|;
114 push(@values, $form->{partsgroup_id});
116 if ($form->{country}) {
117 $where .= qq| AND (ct.country ILIKE ?)|;
118 push(@values, '%' . $form->{country} . '%');
120 # nimmt man description am Besten aus invoice oder parts?
121 if ($form->{description}) {
122 $where .= qq| AND (i.description ILIKE ?)|;
123 push(@values, '%' . $form->{description} . '%');
125 if ($form->{transdatefrom}) {
126 $where .= " AND ar.transdate >= ?";
127 push(@values, $form->{transdatefrom});
129 if ($form->{transdateto}) {
130 $where .= " AND ar.transdate <= ?";
131 push(@values, $form->{transdateto});
133 if ($form->{department}) {
134 my ($null, $department_id) = split /--/, $form->{department};
135 $where .= " AND ar.department_id = ?";
136 push(@values, $department_id);
138 if ($form->{employee_id}) {
139 $where .= " AND ar.employee_id = ?";
140 push @values, conv_i($form->{employee_id});
143 if ($form->{salesman_id}) {
144 $where .= " AND ar.salesman_id = ?";
145 push @values, conv_i($form->{salesman_id});
147 if ($form->{project_id}) {
149 qq|AND ((ar.globalproject_id = ?) OR EXISTS | .
150 qq| (SELECT * FROM invoice i | .
151 qq| WHERE i.project_id = ? AND i.trans_id = ar.id))|;
152 push(@values, $form->{"project_id"}, $form->{"project_id"});
154 if ($form->{business_id}) {
155 $where .= qq| AND ct.business_id = ? |;
156 push(@values, $form->{"business_id"});
159 my ($cvar_where_ct, @cvar_values_ct) = CVar->build_filter_query('module' => 'CT',
160 'trans_id_field' => 'ct.id',
163 if ($cvar_where_ct) {
164 $where .= qq| AND ($cvar_where_ct)|;
165 push @values, @cvar_values_ct;
169 my ($cvar_where_ic, @cvar_values_ic) = CVar->build_filter_query('module' => 'IC',
170 'trans_id_field' => 'p.id',
173 if ($cvar_where_ic) {
174 $where .= qq| AND ($cvar_where_ic)|;
175 push @values, @cvar_values_ic;
178 $query .= " WHERE $where ORDER BY $sortorder "; # LIMIT 5000";
180 my @result = selectall_hashref_query($form, $dbh, $query, @values);
182 $form->{AR} = [ @result ];
184 $main::lxdebug->leave_sub();