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);
 
  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
 
  58     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, ar.taxincluded | .
 
  59     qq|, COALESCE((SELECT e.buy FROM exchangerate e WHERE e.transdate = ar.transdate and ar.currency_id = e.currency_id),1) as exchangerate | .
 
  61     qq|JOIN ar on (i.trans_id = ar.id) | .
 
  62     qq|JOIN parts p on (i.parts_id = p.id) | .
 
  63     qq|LEFT JOIN partsgroup pg on (p.partsgroup_id = pg.id) | .
 
  64     qq|LEFT JOIN customer ct on (ct.id = ar.customer_id) | .
 
  65     qq|LEFT JOIN business b on (ct.business_id = b.id) | .
 
  66     qq|LEFT JOIN employee e ON (ar.employee_id = e.id) | .
 
  67     qq|LEFT JOIN employee e2 ON (ar.salesman_id = e2.id) |;
 
  71   # if employee can only see his own invoices, make sure this also holds for sales report
 
  72   # limits by employees (Bearbeiter), not salesmen!
 
  73   if (!$main::auth->assert('sales_all_edit', 1)) {
 
  74     $where .= " AND ar.employee_id = (select id from employee where login= ?)";
 
  75     push (@values, $::myconfig{login});
 
  78   # Stornierte Rechnungen und Stornorechnungen in invoice rausfiltern
 
  79   # was ist mit Gutschriften?
 
  80   $where .= " AND ar.storno is not true ";
 
  82   # Bestandteile von Erzeugnissen herausfiltern
 
  83   $where .= " AND i.assemblyitem is not true ";
 
  85   # filter allowed parameters for mainsort and subsort as passed by POST
 
  86   my @databasefields = qw(description customername country partsgroup business salesman month);
 
  87   my ($mainsort) = grep { /^$form->{mainsort}$/ } @databasefields;
 
  88   my ($subsort) = grep { /^$form->{subsort}$/ } @databasefields;
 
  89   die "illegal parameter for mainsort or subsort" unless $mainsort and $subsort;
 
  92   # sorting by month is a special case, we don't want to sort alphabetically by
 
  93   # month name, so we also extract a numerical month in the from YYYYMM to sort
 
  94   # by in case of month sorting
 
  95   # Sorting by month, using description as an example:
 
  96   # Sorting with month as mainsort: ORDER BY nummonth,description,ar.transdate,ar.invnumber
 
  97   # Sorting with month as subsort:  ORDER BY description,nummonth,ar.transdate,ar.invnumber
 
  98   if ($form->{mainsort} eq 'month') {
 
  99     $sortorder .= "nummonth,"
 
 101     $sortorder .= $mainsort . ",";
 
 103   if ($form->{subsort} eq 'month') {
 
 104     $sortorder .= "nummonth,"
 
 106     $sortorder .= $subsort . ",";
 
 108   $sortorder .= 'ar.transdate,ar.invnumber';  # Default sorting order after mainsort und subsort
 
 110   if ($form->{customer_id}) {
 
 111     $where .= " AND ar.customer_id = ?";
 
 112     push(@values, $form->{customer_id});
 
 113   } elsif ($form->{customer}) {
 
 114     $where .= " AND ct.name ILIKE ?";
 
 115     push(@values, $form->like($form->{customer}));
 
 117   if ($form->{customernumber}) {
 
 118     $where .= qq| AND ct.customernumber = ? |;
 
 119     push(@values, $form->{customernumber});
 
 121   if ($form->{partnumber}) {
 
 122     $where .= qq| AND (p.partnumber ILIKE ?)|;
 
 123     push(@values, '%' . $form->{partnumber} . '%');
 
 125   if ($form->{partsgroup_id}) {
 
 126     $where .= qq| AND (pg.id = ?)|;
 
 127     push(@values, $form->{partsgroup_id});
 
 129   if ($form->{country}) {
 
 130     $where .= qq| AND (ct.country ILIKE ?)|;
 
 131     push(@values, '%' . $form->{country} . '%');
 
 134   # when filtering for parts by description we probably want to filter by the description of the part as per the master data
 
 135   # invoice.description may differ due to manually changing the description in the invoice or because of translations of the description
 
 136   # at least in the translation case we probably want the report to also include translated articles, so we have to filter via parts.description
 
 137   if ($form->{description}) {
 
 138     $where .= qq| AND (p.description ILIKE ?)|;
 
 139     push(@values, '%' . $form->{description} . '%');
 
 141   if ($form->{transdatefrom}) {
 
 142     $where .= " AND ar.transdate >= ?";
 
 143     push(@values, $form->{transdatefrom});
 
 145   if ($form->{transdateto}) {
 
 146     $where .= " AND ar.transdate <= ?";
 
 147     push(@values, $form->{transdateto});
 
 149   if ($form->{department}) {
 
 150     my ($null, $department_id) = split /--/, $form->{department};
 
 151     $where .= " AND ar.department_id = ?";
 
 152     push(@values, $department_id);
 
 154   if ($form->{employee_id}) {
 
 155     $where .= " AND ar.employee_id = ?";
 
 156     push @values, conv_i($form->{employee_id});
 
 159   if ($form->{salesman_id}) {
 
 160     $where .= " AND ar.salesman_id = ?";
 
 161     push @values, conv_i($form->{salesman_id});
 
 163   if ($form->{project_id}) {
 
 165       qq|AND ((ar.globalproject_id = ?) OR EXISTS | .
 
 166       qq|  (SELECT * FROM invoice i | .
 
 167       qq|   WHERE i.project_id = ? AND i.trans_id = ar.id))|;
 
 168     push(@values, $form->{"project_id"}, $form->{"project_id"});
 
 170   if ($form->{business_id}) {
 
 171     $where .= qq| AND ct.business_id = ? |;
 
 172     push(@values, $form->{"business_id"});
 
 175   my ($cvar_where_ct, @cvar_values_ct) = CVar->build_filter_query('module'    => 'CT',
 
 176                                                                   'trans_id_field' => 'ct.id',
 
 179   if ($cvar_where_ct) {
 
 180     $where .= qq| AND ($cvar_where_ct)|;
 
 181     push @values, @cvar_values_ct;
 
 185   my ($cvar_where_ic, @cvar_values_ic) = CVar->build_filter_query('module'         => 'IC',
 
 186                                                                   'trans_id_field' => 'p.id',
 
 189   if ($cvar_where_ic) {
 
 190     $where .= qq| AND ($cvar_where_ic)|;
 
 191     push @values, @cvar_values_ic;
 
 194   $query .= " WHERE $where ORDER BY $sortorder "; # LIMIT 5000";
 
 196   my @result = selectall_hashref_query($form, $dbh, $query, @values);
 
 198   $form->{AR} = [ @result ];
 
 200   $main::lxdebug->leave_sub();