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 #======================================================================
 
  31 # backend code for customers and vendors
 
  34 #   DS. 2000-07-04  Created
 
  36 #======================================================================
 
  43   $main::lxdebug->enter_sub();
 
  45   my ( $self, $myconfig, $form ) = @_;
 
  47   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
  49   my $dbh   = $form->dbconnect($myconfig);
 
  51     qq|SELECT ct.*, b.id AS business, cp.* | .
 
  53     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
  54     qq|LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) | .
 
  55     qq|WHERE (ct.id = ?) | .
 
  56     qq|ORDER BY cp.cp_id LIMIT 1|;
 
  57   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
 
  59   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
  61   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
  64   if ( $form->{salesman_id} ) {
 
  66       qq|SELECT ct.name AS salesman | .
 
  70       selectrow_query($form, $dbh, $query, $form->{salesman_id});
 
  73   # check if it is orphaned
 
  74   my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
 
  78     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
 
  79     qq|WHERE ct.id = ? | .
 
  83     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
 
  85   my ($dummy) = selectrow_query($form, $dbh, $query, $form->{id}, $form->{id});
 
  86   $form->{status} = "orphaned" unless ($dummy);
 
  90   $main::lxdebug->leave_sub();
 
  93 sub populate_drop_down_boxes {
 
  94   $main::lxdebug->enter_sub();
 
  96   my ($self, $myconfig, $form, $provided_dbh) = @_;
 
  98   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
 
 101   $query = qq|SELECT id, description FROM business ORDER BY id|;
 
 102   $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
 
 106     qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
 
 107     qq|FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
 
 108   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 111   $query  = qq|SELECT cp_id, cp_name FROM contacts WHERE cp_cv_id = ?|;
 
 112   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 115   $query = qq|SELECT id, description FROM language ORDER BY id|;
 
 116   $form->{languages} = selectall_hashref_query($form, $dbh, $query);
 
 119   $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
 
 120   $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
 
 122   $dbh->disconnect() unless ($provided_dbh);
 
 124   $main::lxdebug->leave_sub();
 
 127 sub query_titles_and_greetings {
 
 128   $main::lxdebug->enter_sub();
 
 130   my ( $self, $myconfig, $form ) = @_;
 
 133   my $dbh = $form->dbconnect($myconfig);
 
 136     qq|SELECT DISTINCT(cp_greeting) | .
 
 138     qq|WHERE cp_greeting ~ '[a-zA-Z]' | .
 
 139     qq|ORDER BY cp_greeting|;
 
 140   $form->{GREETINGS} = [ selectall_array_query($form, $dbh, $query) ];
 
 143     qq|SELECT DISTINCT(greeting) | .
 
 145     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 147     qq|SELECT DISTINCT(greeting) | .
 
 149     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 150     qq|ORDER BY greeting|;
 
 152   map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
 
 153   $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
 
 156     qq|SELECT DISTINCT(cp_title) | .
 
 158     qq|WHERE cp_title ~ '[a-zA-Z]'|;
 
 159   $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
 
 162     qq|SELECT DISTINCT(cp_abteilung) | .
 
 164     qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
 
 165   $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
 
 168   $main::lxdebug->leave_sub();
 
 172   $main::lxdebug->enter_sub();
 
 174   my ( $self, $myconfig, $form ) = @_;
 
 176   # set pricegroup to default
 
 177   $form->{klass} = 0 unless ($form->{klass});
 
 179   # connect to database
 
 180   my $dbh = $form->dbconnect_noauto($myconfig);
 
 183     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 184     if ( $form->{"selected_cp_${_}"} );
 
 185        } qw(title greeting abteilung) );
 
 186   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 187   if ( $form->{"selected_company_greeting"} );
 
 189   # assign value discount, terms, creditlimit
 
 190   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 191   $form->{discount} /= 100;
 
 192   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 194   my ( $query, $sth, $f_id );
 
 197     $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
 
 198     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 200     if (($f_id ne $form->{id}) && ($f_id ne "")) {
 
 201       $main::lxdebug->leave_sub();
 
 206     if (!$form->{customernumber} && $form->{business}) {
 
 207       $form->{customernumber} =
 
 208         $form->update_business($myconfig, $form->{business}, $dbh);
 
 210     if (!$form->{customernumber}) {
 
 211       $form->{customernumber} =
 
 212         $form->update_defaults($myconfig, "customernumber", $dbh);
 
 215     $query  = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
 
 216     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 218       $main::lxdebug->leave_sub();
 
 222     $query = qq|SELECT nextval('id')|;
 
 223     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 225     $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
 
 226     do_query($form, $dbh, $query, $form->{id});
 
 229   $query = qq|UPDATE customer SET | .
 
 230     qq|customernumber = ?, | .
 
 233     qq|department_1 = ?, | .
 
 234     qq|department_2 = ?, | .
 
 248     qq|creditlimit = ?, | .
 
 250     qq|business_id = ?, | .
 
 251     qq|taxnumber = ?, | .
 
 254     qq|account_number = ?, | .
 
 255     qq|bank_code = ?, | .
 
 260     qq|salesman_id = ?, | .
 
 261     qq|language_id = ?, | .
 
 262     qq|payment_id = ?, | .
 
 263     qq|taxzone_id = ?, | .
 
 264     qq|user_password = ?, | .
 
 265     qq|c_vendor_id = ?, | .
 
 269     $form->{customernumber},
 
 272     $form->{department_1},
 
 273     $form->{department_2},
 
 287     $form->{creditlimit},
 
 288     conv_i($form->{terms}),
 
 289     conv_i($form->{business}),
 
 293     $form->{account_number},
 
 296     $form->{obsolete} ? 't' : 'f',
 
 299     conv_i($form->{salesman_id}),
 
 300     conv_i($form->{language_id}),
 
 301     conv_i($form->{payment_id}),
 
 302     conv_i($form->{taxzone_id}, 0),
 
 303     $form->{user_password},
 
 304     $form->{c_vendor_id},
 
 305     conv_i($form->{klass}),
 
 308   do_query( $form, $dbh, $query, @values );
 
 311   if ( $form->{cp_id} ) {
 
 312     $query = qq|UPDATE contacts SET | .
 
 313       qq|cp_greeting = ?, | .
 
 315       qq|cp_givenname = ?, | .
 
 318       qq|cp_phone1 = ?, | .
 
 319       qq|cp_phone2 = ?, | .
 
 320       qq|cp_abteilung = ?, | .
 
 322       qq|cp_mobile1 = ?, | .
 
 323       qq|cp_mobile2 = ?, | .
 
 324       qq|cp_satphone = ?, | .
 
 325       qq|cp_satfax = ?, | .
 
 326       qq|cp_project = ?, | .
 
 327       qq|cp_privatphone = ?, | .
 
 328       qq|cp_privatemail = ?, | .
 
 329       qq|cp_birthday = ? | .
 
 332       $form->{cp_greeting},
 
 334       $form->{cp_givenname},
 
 339       $form->{cp_abteilung},
 
 343       $form->{cp_satphone},
 
 346       $form->{cp_privatphone},
 
 347       $form->{cp_privatemail},
 
 348       $form->{cp_birthday},
 
 351   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 353       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
 
 354       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 355       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 357       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 360       $form->{cp_greeting},
 
 362       $form->{cp_givenname},
 
 367       $form->{cp_abteilung},
 
 371       $form->{cp_satphone},
 
 374       $form->{cp_privatphone},
 
 375       $form->{cp_privatemail},
 
 379   do_query( $form, $dbh, $query, @values ) if ($query);
 
 382   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 384   $rc = $dbh->commit();
 
 387   $main::lxdebug->leave_sub();
 
 392   $main::lxdebug->enter_sub();
 
 394   my ( $self, $myconfig, $form ) = @_;
 
 396   $form->{taxzone_id} *= 1;
 
 397   # connect to database
 
 398   my $dbh = $form->dbconnect_noauto($myconfig);
 
 401     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 402     if ( $form->{"selected_cp_${_}"} );
 
 403        } qw(title greeting abteilung) );
 
 404   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 405   if ( $form->{"selected_company_greeting"} );
 
 407   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 408   $form->{discount} /= 100;
 
 409   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 414     $query = qq|DELETE FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
 
 415     do_query($form, $dbh, $query, $form->{id});
 
 418     $query = qq|SELECT nextval('id')|;
 
 419     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 421     $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
 
 422     do_query($form, $dbh, $query, $form->{id});
 
 424     if ( !$form->{vendornumber} ) {
 
 425       $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
 
 430     qq|UPDATE vendor SET | .
 
 431     qq|  vendornumber = ?, | .
 
 433     qq|  greeting = ?, | .
 
 434     qq|  department_1 = ?, | .
 
 435     qq|  department_2 = ?, | .
 
 440     qq|  homepage = ?, | .
 
 449     qq|  discount = ?, | .
 
 450     qq|  creditlimit = ?, | .
 
 451     qq|  business_id = ?, | .
 
 452     qq|  taxnumber = ?, | .
 
 453     qq|  sic_code = ?, | .
 
 454     qq|  language = ?, | .
 
 455     qq|  account_number = ?, | .
 
 456     qq|  bank_code = ?, | .
 
 458     qq|  obsolete = ?, | .
 
 460     qq|  payment_id = ?, | .
 
 461     qq|  taxzone_id = ?, | .
 
 462     qq|  language_id = ?, | .
 
 463     qq|  username = ?, | .
 
 464     qq|  user_password = ?, | .
 
 465     qq|  v_customer_id = ? | .
 
 468     $form->{vendornumber},
 
 471     $form->{department_1},
 
 472     $form->{department_2},
 
 485     conv_i($form->{terms}),
 
 487     $form->{creditlimit},
 
 488     conv_i($form->{business}),
 
 492     $form->{account_number},
 
 495     $form->{obsolete} ? 't' : 'f',
 
 497     conv_i($form->{payment_id}),
 
 498     conv_i($form->{taxzone_id}, 0),
 
 499     conv_i( $form->{language_id}),
 
 501     $form->{user_password},
 
 502     conv_i($form->{v_customer_id}),
 
 505   do_query($form, $dbh, $query, @values);
 
 508   if ( $form->{cp_id} ) {
 
 509     $query = qq|UPDATE contacts SET | .
 
 510       qq|cp_greeting = ?, | .
 
 512       qq|cp_givenname = ?, | .
 
 515       qq|cp_phone1 = ?, | .
 
 516       qq|cp_phone2 = ?, | .
 
 517       qq|cp_abteilung = ?, | .
 
 519       qq|cp_mobile1 = ?, | .
 
 520       qq|cp_mobile2 = ?, | .
 
 521       qq|cp_satphone = ?, | .
 
 522       qq|cp_satfax = ?, | .
 
 523       qq|cp_project = ?, | .
 
 524       qq|cp_privatphone = ?, | .
 
 525       qq|cp_privatemail = ?, | .
 
 526       qq|cp_birthday = ? | .
 
 529       $form->{cp_greeting},
 
 531       $form->{cp_givenname},
 
 536       $form->{cp_abteilung},
 
 540       $form->{cp_satphone},
 
 543       $form->{cp_privatphone},
 
 544       $form->{cp_privatemail},
 
 545       $form->{cp_birthday},
 
 548   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 550       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
 
 551       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 552       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 554       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 557       $form->{cp_greeting},
 
 559       $form->{cp_givenname},
 
 564       $form->{cp_abteilung},
 
 568       $form->{cp_satphone},
 
 571       $form->{cp_privatphone},
 
 572       $form->{cp_privatemail},
 
 576   do_query($form, $dbh, $query, @values) if ($query);
 
 579   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 581   $rc = $dbh->commit();
 
 584   $main::lxdebug->leave_sub();
 
 589   $main::lxdebug->enter_sub();
 
 591   my ( $self, $myconfig, $form ) = @_;
 
 592   # connect to database
 
 593   my $dbh = $form->dbconnect($myconfig);
 
 596   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 597   my $query = qq|DELETE FROM $cv WHERE id = ?|;
 
 598   do_query($form, $dbh, $query, $form->{id});
 
 602   $main::lxdebug->leave_sub();
 
 606   $main::lxdebug->enter_sub();
 
 608   my ( $self, $myconfig, $form ) = @_;
 
 610   # connect to database
 
 611   my $dbh = $form->dbconnect($myconfig);
 
 613   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 618   my %allowed_sort_columns =
 
 619     map({ $_, 1 } qw(id customernumber name address contact phone fax email
 
 620                      taxnumber sic_code business invnumber ordnumber quonumber));
 
 622     $form->{sort} && $allowed_sort_columns{$form->{sort}} ?
 
 623     $form->{sort} : "name";
 
 624   $form->{sort} = $sortorder;
 
 625   $sortorder = "country,city,street" if ($sortorder eq "address");
 
 627   if ($form->{"${cv}number"}) {
 
 628     $where .= " AND ct.${cv}number ILIKE ?";
 
 629     push(@values, '%' . $form->{"${cv}number"} . '%');
 
 632   foreach my $key (qw(name contact email)) {
 
 634       $where .= " AND ct.$key ILIKE ?";
 
 635       push(@values, '%' . $form->{$key} . '%');
 
 639   if ( $form->{status} eq 'orphaned' ) {
 
 641       qq| AND ct.id NOT IN | .
 
 642       qq|   (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
 
 643     if ($cv eq 'customer') {
 
 645         qq| AND ct.id NOT IN | .
 
 646         qq| (SELECT a.customer_id FROM ar a, customer cv | .
 
 647         qq|  WHERE cv.id = a.customer_id)|;
 
 649     if ($cv eq 'vendor') {
 
 651         qq| AND ct.id NOT IN | .
 
 652         qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
 
 653         qq|  WHERE cv.id = a.vendor_id)|;
 
 655     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
 
 658   if ($form->{obsolete} eq "Y") {
 
 659     $where .= qq| AND obsolete|;
 
 660   } elsif ($form->{obsolete} eq "N") {
 
 661     $where .= qq| AND NOT obsolete|;
 
 664   if ($form->{business_id}) {
 
 665     $where .= qq| AND (business_id = ?)|;
 
 666     push(@values, conv_i($form->{business_id}));
 
 670     qq|SELECT ct.*, b.description AS business | .
 
 672     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 675   my @saved_values = @values;
 
 676   # redo for invoices, orders and quotations
 
 677   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
 
 678     my ($ar, $union, $module);
 
 681     if ($form->{l_invnumber}) {
 
 682       my $ar = $cv eq 'customer' ? 'ar' : 'ap';
 
 683       my $module = $ar eq 'ar' ? 'is' : 'ir';
 
 686         qq|SELECT ct.*, b.description AS business, | .
 
 687         qq|  a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
 
 688         qq|  '$module' AS module, 'invoice' AS formtype, | .
 
 689         qq|  (a.amount = a.paid) AS closed | .
 
 691         qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
 
 692         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 693         qq|WHERE $where AND (a.invoice = '1')|;
 
 698     if ( $form->{l_ordnumber} ) {
 
 699       if ($union eq "UNION") {
 
 700         push(@values, @saved_values);
 
 704         qq|SELECT ct.*, b.description AS business,| .
 
 705         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 706         qq|  'oe' AS module, 'order' AS formtype, o.closed | .
 
 708         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 709         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 710         qq|WHERE $where AND (o.quotation = '0')|;
 
 715     if ( $form->{l_quonumber} ) {
 
 716       if ($union eq "UNION") {
 
 717         push(@values, @saved_values);
 
 721         qq|SELECT ct.*, b.description AS business, | .
 
 722         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 723         qq|  'oe' AS module, 'quotation' AS formtype, o.closed | .
 
 725         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 726         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 727         qq|WHERE $where AND (o.quotation = '1')|;
 
 731   $query .= qq| ORDER BY $sortorder|;
 
 733   $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
 
 734   map({ my $ref = $_; $ref->{address} = join(" ", map({ $ref->{$_} } qw(street zipcode city country))); }
 
 737   $main::lxdebug->leave_sub();
 
 741   $main::lxdebug->enter_sub();
 
 743   my ( $self, $myconfig, $form ) = @_;
 
 744   my $dbh   = $form->dbconnect($myconfig);
 
 746     qq|SELECT * FROM contacts c | .
 
 747     qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
 
 748   my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
 
 749   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 751   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 756   $main::lxdebug->leave_sub();
 
 760   $main::lxdebug->enter_sub();
 
 762   my ( $self, $myconfig, $form ) = @_;
 
 763   my $dbh   = $form->dbconnect($myconfig);
 
 764   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 765   my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
 
 767   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 769   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 774   $main::lxdebug->leave_sub();
 
 778   $main::lxdebug->enter_sub();
 
 780   my ( $self, $myconfig, $form ) = @_;
 
 781   my $dbh = $form->dbconnect($myconfig);
 
 783   my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
 
 784   my $db = $form->{db} eq "customer" ? "customer" : "vendor";
 
 786   my $where = " WHERE 1=1 ";
 
 789   if ($form->{shipto_id} && ($arap eq "ar")) {
 
 790     $where .= "AND ${arap}.shipto_id = ?";
 
 791     push(@values, $form->{shipto_id});
 
 793     $where .= "AND ${arap}.${db}_id = ?";
 
 794     push(@values, $form->{id});
 
 798     $where .= "AND ${arap}.transdate >= ?";
 
 799     push(@values, conv_date($form->{from}));
 
 802     $where .= "AND ${arap}.transdate <= ?";
 
 803     push(@values, conv_date($form->{to}));
 
 806     qq|SELECT s.shiptoname, i.qty, | .
 
 807     qq|  ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
 
 808     qq|  i.description, i.unit, i.sellprice | .
 
 810     qq|LEFT JOIN shipto s ON | .
 
 812      ? qq|(ar.shipto_id = s.shipto_id) |
 
 813      : qq|(ap.id = s.trans_id) |) .
 
 814     qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
 
 815     qq|LEFT join parts p ON (p.id = i.parts_id) | .
 
 817     qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
 
 819   $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
 
 823   $main::lxdebug->leave_sub();