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., 51 Franklin Street, Fifth Floor, Boston,
30 #======================================================================
34 #======================================================================
44 sub invoice_transactions {
45 $main::lxdebug->enter_sub();
47 my ($self, $myconfig, $form) = @_;
50 my $dbh = $form->get_standard_dbh($myconfig);
54 # default usage: always use parts.description for (sub-)totalling and in header and subheader lines
55 # but use invoice.description in article mode
56 # so we extract both versions in our query and later overwrite the description in article mode
59 qq|SELECT ct.id as customerid, ct.name as customername,ct.customernumber,ct.country,ar.invnumber,ar.shipvia,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, ar.taxincluded | .
60 qq|, COALESCE(er.buy, 1) | .
62 qq|RIGHT JOIN ar on (i.trans_id = ar.id) | .
63 qq|JOIN parts p on (i.parts_id = p.id) | .
64 qq|LEFT JOIN exchangerate er on (er.transdate = ar.transdate and ar.currency_id = er.currency_id) | .
65 qq|LEFT JOIN partsgroup pg on (p.partsgroup_id = pg.id) | .
66 qq|LEFT JOIN customer ct on (ct.id = ar.customer_id) | .
67 qq|LEFT JOIN business b on (ct.business_id = b.id) | .
68 qq|LEFT JOIN employee e ON (ar.employee_id = e.id) | .
69 qq|LEFT JOIN employee e2 ON (ar.salesman_id = e2.id) |;
73 # if employee can only see his own invoices, make sure this also holds for sales report
74 # limits by employees (Bearbeiter), not salesmen!
75 if (!$main::auth->assert('sales_all_edit', 1)) {
76 $where .= " AND ar.employee_id = (select id from employee where login= ?)";
77 push (@values, $::myconfig{login});
80 # Stornierte Rechnungen und Stornorechnungen in invoice rausfiltern
81 # was ist mit Gutschriften?
82 $where .= " AND ar.storno is not true ";
84 # Bestandteile von Erzeugnissen herausfiltern
85 $where .= " AND i.assemblyitem is not true ";
87 # filter allowed parameters for mainsort and subsort as passed by POST
88 my @databasefields = qw(description customername country partsgroup business salesman month shipvia);
89 my ($mainsort) = grep { /^$form->{mainsort}$/ } @databasefields;
90 my ($subsort) = grep { /^$form->{subsort}$/ } @databasefields;
91 die "illegal parameter for mainsort or subsort" unless $mainsort and $subsort;
94 # sorting by month is a special case, we don't want to sort alphabetically by
95 # month name, so we also extract a numerical month in the from YYYYMM to sort
96 # by in case of month sorting
97 # Sorting by month, using description as an example:
98 # Sorting with month as mainsort: ORDER BY nummonth,description,ar.transdate,ar.invnumber
99 # Sorting with month as subsort: ORDER BY description,nummonth,ar.transdate,ar.invnumber
100 if ($form->{mainsort} eq 'month') {
101 $sortorder .= "nummonth,"
103 $sortorder .= $mainsort . ",";
105 if ($form->{subsort} eq 'month') {
106 $sortorder .= "nummonth,"
108 $sortorder .= $subsort . ",";
110 $sortorder .= 'ar.transdate,ar.invnumber'; # Default sorting order after mainsort und subsort
112 if ($form->{customer_id}) {
113 $where .= " AND ar.customer_id = ?";
114 push(@values, $form->{customer_id});
115 } elsif ($form->{customer}) {
116 $where .= " AND ct.name ILIKE ?";
117 push(@values, like($form->{customer}));
119 if ($form->{customernumber}) {
120 $where .= qq| AND ct.customernumber = ? |;
121 push(@values, $form->{customernumber});
123 if ($form->{partnumber}) {
124 $where .= qq| AND (p.partnumber ILIKE ?)|;
125 push(@values, like($form->{partnumber}));
127 if ($form->{partsgroup_id}) {
128 $where .= qq| AND (pg.id = ?)|;
129 push(@values, $form->{partsgroup_id});
131 if ($form->{country}) {
132 $where .= qq| AND (ct.country ILIKE ?)|;
133 push(@values, like($form->{country}));
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, like($form->{description}));
143 if ($form->{transdatefrom}) {
144 $where .= " AND ar.transdate >= ?";
145 push(@values, $form->{transdatefrom});
147 if ($form->{transdateto}) {
148 $where .= " AND ar.transdate <= ?";
149 push(@values, $form->{transdateto});
151 if ($form->{department_id}) {
152 $where .= " AND ar.department_id = ?";
153 push @values, conv_i($form->{department_id});
155 if ($form->{employee_id}) {
156 $where .= " AND ar.employee_id = ?";
157 push @values, conv_i($form->{employee_id});
160 if ($form->{salesman_id}) {
161 $where .= " AND ar.salesman_id = ?";
162 push @values, conv_i($form->{salesman_id});
164 if ($form->{project_id}) {
166 qq|AND ((ar.globalproject_id = ?) OR EXISTS | .
167 qq| (SELECT * FROM invoice i | .
168 qq| WHERE i.project_id = ? AND i.trans_id = ar.id))|;
169 push(@values, $form->{"project_id"}, $form->{"project_id"});
171 if ($form->{business_id}) {
172 $where .= qq| AND ct.business_id = ? |;
173 push(@values, $form->{"business_id"});
176 my ($cvar_where_ct, @cvar_values_ct) = CVar->build_filter_query('module' => 'CT',
177 'trans_id_field' => 'ct.id',
180 if ($cvar_where_ct) {
181 $where .= qq| AND ($cvar_where_ct)|;
182 push @values, @cvar_values_ct;
186 my ($cvar_where_ic, @cvar_values_ic) = CVar->build_filter_query('module' => 'IC',
187 'trans_id_field' => 'p.id',
190 if ($cvar_where_ic) {
191 $where .= qq| AND ($cvar_where_ic)|;
192 push @values, @cvar_values_ic;
195 $query .= " WHERE $where ORDER BY $sortorder "; # LIMIT 5000";
197 my @result = selectall_hashref_query($form, $dbh, $query, @values);
199 $form->{AR} = [ @result ];
201 $main::lxdebug->leave_sub();