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.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 | .
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 ";
82 # sorting by month is a special case, we don't want to sort alphabetically by
83 # month name, so we also extract a numerical month in the from YYYYMM to sort
84 # by in case of month sorting
85 # Sorting by month, using description as an example:
86 # Sorting with month as mainsort: ORDER BY nummonth,description,ar.transdate,ar.invnumber
87 # Sorting with month as subsort: ORDER BY description,nummonth,ar.transdate,ar.invnumber
88 if ($form->{mainsort} eq 'month') {
89 $sortorder .= "nummonth,"
91 $sortorder .= $form->{mainsort} . ",";
93 if ($form->{subsort} eq 'month') {
94 $sortorder .= "nummonth,"
96 $sortorder .= $form->{subsort} . ",";
98 $sortorder .= 'ar.transdate,ar.invnumber'; # Default sorting order after mainsort und subsort
101 if ($form->{customer_id}) {
102 $where .= " AND ar.customer_id = ?";
103 push(@values, $form->{customer_id});
105 if ($form->{customernumber}) {
106 $where .= qq| AND ct.customernumber = ? |;
107 push(@values, $form->{customernumber});
109 if ($form->{partnumber}) {
110 $where .= qq| AND (p.partnumber ILIKE ?)|;
111 push(@values, '%' . $form->{partnumber} . '%');
113 if ($form->{partsgroup_id}) {
114 $where .= qq| AND (pg.id = ?)|;
115 push(@values, $form->{partsgroup_id});
117 if ($form->{country}) {
118 $where .= qq| AND (ct.country ILIKE ?)|;
119 push(@values, '%' . $form->{country} . '%');
121 # nimmt man description am Besten aus invoice oder parts?
122 if ($form->{description}) {
123 $where .= qq| AND (i.description ILIKE ?)|;
124 push(@values, '%' . $form->{description} . '%');
126 if ($form->{transdatefrom}) {
127 $where .= " AND ar.transdate >= ?";
128 push(@values, $form->{transdatefrom});
130 if ($form->{transdateto}) {
131 $where .= " AND ar.transdate <= ?";
132 push(@values, $form->{transdateto});
134 if ($form->{department}) {
135 my ($null, $department_id) = split /--/, $form->{department};
136 $where .= " AND ar.department_id = ?";
137 push(@values, $department_id);
139 if ($form->{employee_id}) {
140 $where .= " AND ar.employee_id = ?";
141 push @values, conv_i($form->{employee_id});
144 if ($form->{salesman_id}) {
145 $where .= " AND ar.salesman_id = ?";
146 push @values, conv_i($form->{salesman_id});
148 if ($form->{project_id}) {
150 qq|AND ((ar.globalproject_id = ?) OR EXISTS | .
151 qq| (SELECT * FROM invoice i | .
152 qq| WHERE i.project_id = ? AND i.trans_id = ar.id))|;
153 push(@values, $form->{"project_id"}, $form->{"project_id"});
155 if ($form->{business_id}) {
156 $where .= qq| AND ct.business_id = ? |;
157 push(@values, $form->{"business_id"});
160 my ($cvar_where_ct, @cvar_values_ct) = CVar->build_filter_query('module' => 'CT',
161 'trans_id_field' => 'ct.id',
164 if ($cvar_where_ct) {
165 $where .= qq| AND ($cvar_where_ct)|;
166 push @values, @cvar_values_ct;
170 my ($cvar_where_ic, @cvar_values_ic) = CVar->build_filter_query('module' => 'IC',
171 'trans_id_field' => 'p.id',
174 if ($cvar_where_ic) {
175 $where .= qq| AND ($cvar_where_ic)|;
176 push @values, @cvar_values_ic;
179 $query .= " WHERE $where ORDER BY $sortorder "; # LIMIT 5000";
181 my @result = selectall_hashref_query($form, $dbh, $query, @values);
183 $form->{AR} = [ @result ];
185 $main::lxdebug->leave_sub();