1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
   9 # Copyright (C) 1998-2002
 
  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 # Inventory invoicing module
 
  33 #======================================================================
 
  38   $main::lxdebug->enter_sub();
 
  40   my ($self, $myconfig, $form, $locale) = @_;
 
  42   $form->{duedate} = $form->{invdate} unless ($form->{duedate});
 
  45   my $dbh = $form->dbconnect($myconfig);
 
  47   my $query = qq|SELECT date '$form->{duedate}' - date '$form->{invdate}'
 
  50   my $sth = $dbh->prepare($query);
 
  51   $sth->execute || $form->dberror($query);
 
  53   ($form->{terms}) = $sth->fetchrow_array;
 
  61   my %oid = ('Pg'     => 'oid',
 
  64   # sort items by partsgroup
 
  65   for $i (1 .. $form->{rowcount}) {
 
  67     if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
 
  68       $form->format_string("partsgroup_$i");
 
  69       $partsgroup = $form->{"partsgroup_$i"};
 
  71     push @partsgroup, [$i, $partsgroup];
 
  83   foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
 
  86     if ($item->[1] ne $sameitem) {
 
  87       push(@{ $form->{description} }, qq|$item->[1]|);
 
  88       $sameitem = $item->[1];
 
  90       map { push(@{ $form->{$_} }, "") }
 
  91         qw(runningnumber number serialnumber bin partnotes qty unit deliverydate sellprice listprice netprice discount linetotal);
 
  94     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
 
  96     if ($form->{"qty_$i"} != 0) {
 
  98       # add number, description and qty to $form->{number}, ....
 
  99       push(@{ $form->{runningnumber} }, $i);
 
 100       push(@{ $form->{number} },        qq|$form->{"partnumber_$i"}|);
 
 101       push(@{ $form->{serialnumber} },  qq|$form->{"serialnumber_$i"}|);
 
 102       push(@{ $form->{bin} },           qq|$form->{"bin_$i"}|);
 
 103       push(@{ $form->{"partnotes"} },   qq|$form->{"partnotes_$i"}|);
 
 104       push(@{ $form->{description} },   qq|$form->{"description_$i"}|);
 
 105       push(@{ $form->{qty} },
 
 106            $form->format_amount($myconfig, $form->{"qty_$i"}));
 
 107       push(@{ $form->{unit} },         qq|$form->{"unit_$i"}|);
 
 108       push(@{ $form->{deliverydate} }, qq|$form->{"deliverydate_$i"}|);
 
 110       push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
 
 112       if ($form->{lizenzen}) {
 
 113         if ($form->{"licensenumber_$i"}) {
 
 115             qq|SELECT l.licensenumber, l.validuntil FROM license l WHERE l.id = $form->{"licensenumber_$i"}|;
 
 116           $sth = $dbh->prepare($query);
 
 117           $sth->execute || $form->dberror($query);
 
 119           ($licensenumber, $validuntil) = $sth->fetchrow_array;
 
 120           push(@{ $form->{licensenumber} }, $licensenumber);
 
 121           push(@{ $form->{validuntil} },
 
 122                $locale->date($myconfig, $validuntil, 0));
 
 123           $licensenumber = "Lizenz: " . $licensenumber;
 
 125           push(@{ $form->{licensenumber} }, $licensenumber);
 
 127           push(@{ $form->{licensenumber} }, "");
 
 128           push(@{ $form->{validuntil} },    "");
 
 133       push(@{ $form->{listprice} }, $form->{"listprice_$i"});
 
 135       my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
 
 136       my ($dec) = ($sellprice =~ /\.(\d+)/);
 
 138       my $decimalplaces = ($dec > 2) ? $dec : 2;
 
 142                             $sellprice * $form->parse_amount($myconfig,
 
 143                                                  $form->{"discount_$i"}) / 100,
 
 146       # keep a netprice as well, (sellprice - discount)
 
 147       $form->{"netprice_$i"} = $sellprice - $discount;
 
 148       push(@{ $form->{netprice} },
 
 149            ($form->{"netprice_$i"} != 0)
 
 150            ? $form->format_amount(
 
 151                                  $myconfig, $form->{"netprice_$i"},
 
 157         $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
 
 161         ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
 
 163       $linetotal = ($linetotal != 0) ? $linetotal : " ";
 
 165       push(@{ $form->{discount} }, $discount);
 
 167       $form->{total} += $linetotal;
 
 169       push(@{ $form->{linetotal} },
 
 170            $form->format_amount($myconfig, $linetotal, 2));
 
 172       @taxaccounts = split / /, $form->{"taxaccounts_$i"};
 
 176       map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
 
 178       if ($form->{taxincluded}) {
 
 181         $taxamount = $linetotal * $taxrate / (1 + $taxrate);
 
 182         $taxbase = $linetotal - $taxamount;
 
 184         $taxamount = $linetotal * $taxrate;
 
 185         $taxbase   = $linetotal;
 
 188       if ($form->round_amount($taxrate, 7) == 0) {
 
 189         if ($form->{taxincluded}) {
 
 190           foreach $item (@taxaccounts) {
 
 192               $form->round_amount($linetotal * $form->{"${item}_rate"} /
 
 193                                     (1 + abs($form->{"${item}_rate"})),
 
 196             $taxaccounts{$item} += $taxamount;
 
 197             $taxdiff            += $taxamount;
 
 199             $taxbase{$item} += $taxbase;
 
 201           $taxaccounts{ $taxaccounts[0] } += $taxdiff;
 
 203           foreach $item (@taxaccounts) {
 
 204             $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
 
 205             $taxbase{$item}     += $taxbase;
 
 209         foreach $item (@taxaccounts) {
 
 210           $taxaccounts{$item} +=
 
 211             $taxamount * $form->{"${item}_rate"} / $taxrate;
 
 212           $taxbase{$item} += $taxbase;
 
 216       if ($form->{"assembly_$i"}) {
 
 219         # get parts and push them onto the stack
 
 221         if ($form->{groupitems}) {
 
 223             qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
 
 225           $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
 
 228         $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
 
 231                     JOIN parts p ON (a.parts_id = p.id)
 
 232                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
 234                     AND a.id = '$form->{"id_$i"}'
 
 236         $sth = $dbh->prepare($query);
 
 237         $sth->execute || $form->dberror($query);
 
 239         while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 240           if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
 
 241             map { push(@{ $form->{$_} }, "") }
 
 242               qw(runningnumber number serialnumber unit qty bin sellprice listprice netprice discount linetotal);
 
 243             $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
 
 244             push(@{ $form->{description} }, $sameitem);
 
 247           map { $form->{"a_$_"} = $ref->{$_} } qw(partnumber description);
 
 248           $form->format_string("a_partnumber", "a_description");
 
 250           push(@{ $form->{description} },
 
 251                $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
 
 253                  . qq| -- $form->{"a_partnumber"}, $form->{"a_description"}|);
 
 254           map { push(@{ $form->{$_} }, "") }
 
 255             qw(number unit qty runningnumber serialnumber bin sellprice listprice netprice discount linetotal);
 
 263   foreach my $item (sort keys %taxaccounts) {
 
 264     if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
 
 265       push(@{ $form->{taxbase} },
 
 266            $form->format_amount($myconfig, $taxbase{$item}, 2));
 
 268       $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
 
 270       push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount));
 
 271       push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
 
 272       push(@{ $form->{taxrate} },
 
 273            $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
 
 274       push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
 
 278   for my $i (1 .. $form->{paidaccounts}) {
 
 279     if ($form->{"paid_$i"}) {
 
 280       push(@{ $form->{payment} }, $form->{"paid_$i"});
 
 281       my ($accno, $description) = split /--/, $form->{"AR_paid_$i"};
 
 282       push(@{ $form->{paymentaccount} }, $description);
 
 283       push(@{ $form->{paymentdate} },    $form->{"datepaid_$i"});
 
 284       push(@{ $form->{paymentsource} },  $form->{"source_$i"});
 
 286       $form->{paid} += $form->parse_amount($myconfig, $form->{"paid_$i"});
 
 290   $form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
 
 292     ($form->{taxincluded}) ? $form->{total} : $form->{total} + $tax;
 
 294     $form->format_amount($myconfig, $form->{invtotal} - $form->{paid}, 2);
 
 295   $form->{invtotal} = $form->format_amount($myconfig, $form->{invtotal}, 2);
 
 297   $form->{paid} = $form->format_amount($myconfig, $form->{paid}, 2);
 
 300   map { $form->{$_} = $myconfig->{$_} }
 
 301     (qw(company address tel fax signature businessnumber));
 
 302   $form->{username} = $myconfig->{name};
 
 306   $main::lxdebug->leave_sub();
 
 309 sub project_description {
 
 310   $main::lxdebug->enter_sub();
 
 312   my ($self, $dbh, $id) = @_;
 
 314   my $query = qq|SELECT p.description
 
 317   my $sth = $dbh->prepare($query);
 
 318   $sth->execute || $form->dberror($query);
 
 320   ($_) = $sth->fetchrow_array;
 
 324   $main::lxdebug->leave_sub();
 
 329 sub customer_details {
 
 330   $main::lxdebug->enter_sub();
 
 332   my ($self, $myconfig, $form) = @_;
 
 334   # connect to database
 
 335   my $dbh = $form->dbconnect($myconfig);
 
 337   # get contact id, set it if nessessary
 
 338   ($null, $form->{cp_id}) = split /--/, $form->{contact};
 
 341   if ($form->{cp_id}) {
 
 342     $contact = "and cp.cp_id = $form->{cp_id}";
 
 345   $taxincluded = $form->{taxincluded};
 
 347   # get rest for the customer
 
 348   my $query = qq|SELECT ct.*, cp.*, ct.notes as customernotes
 
 350                  LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
 
 351                  WHERE ct.id = $form->{customer_id} $contact order by cp.cp_id limit 1|;
 
 352   my $sth = $dbh->prepare($query);
 
 353   $sth->execute || $form->dberror($query);
 
 355   $ref = $sth->fetchrow_hashref(NAME_lc);
 
 356   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 358   $form->{taxincluded} = $taxincluded;
 
 363   $main::lxdebug->leave_sub();
 
 367   $main::lxdebug->enter_sub();
 
 369   my ($self, $myconfig, $form) = @_;
 
 371   # connect to database, turn off autocommit
 
 372   my $dbh = $form->dbconnect_noauto($myconfig);
 
 374   my ($query, $sth, $null, $project_id, $deliverydate);
 
 375   my $exchangerate = 0;
 
 377   ($null, $form->{employee_id}) = split /--/, $form->{employee};
 
 378   unless ($form->{employee_id}) {
 
 379     $form->get_employee($dbh);
 
 382   ($null, $form->{contact_id}) = split /--/, $form->{contact};
 
 383   $form->{contact_id} *= 1;
 
 385   ($null, $form->{department_id}) = split(/--/, $form->{department});
 
 386   $form->{department_id} *= 1;
 
 390     &reverse_invoice($dbh, $form);
 
 393     my $uid = rand() . time;
 
 395     $uid .= $form->{login};
 
 397     $uid = substr($uid, 2, 75);
 
 399     $query = qq|INSERT INTO ar (invnumber, employee_id)
 
 400                 VALUES ('$uid', $form->{employee_id})|;
 
 401     $dbh->do($query) || $form->dberror($query);
 
 403     $query = qq|SELECT a.id FROM ar a
 
 404                 WHERE a.invnumber = '$uid'|;
 
 405     $sth = $dbh->prepare($query);
 
 406     $sth->execute || $form->dberror($query);
 
 408     ($form->{id}) = $sth->fetchrow_array;
 
 412   map { $form->{$_} =~ s/\'/\'\'/g }
 
 413     (qw(invnumber shippingpoint shipvia notes intnotes message));
 
 415   my ($netamount, $invoicediff) = (0, 0);
 
 416   my ($amount, $linetotal, $lastincomeaccno);
 
 418   if ($form->{currency} eq $form->{defaultcurrency}) {
 
 419     $form->{exchangerate} = 1;
 
 422       $form->check_exchangerate($myconfig, $form->{currency},
 
 423                                 $form->{transdate}, 'buy');
 
 426   $form->{exchangerate} =
 
 429     : $form->parse_amount($myconfig, $form->{exchangerate});
 
 431   $form->{expense_inventory} = "";
 
 433   foreach my $i (1 .. $form->{rowcount}) {
 
 434     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
 
 436     if ($form->{"qty_$i"} != 0) {
 
 438       map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
 
 439         (qw(partnumber description unit));
 
 441       # undo discount formatting
 
 442       $form->{"discount_$i"} =
 
 443         $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
 
 445       my ($allocated, $taxrate) = (0, 0);
 
 448       # keep entered selling price
 
 450         $form->parse_amount($myconfig, $form->{"sellprice_$i"});
 
 452       my ($dec) = ($fxsellprice =~ /\.(\d+)/);
 
 454       my $decimalplaces = ($dec > 2) ? $dec : 2;
 
 458         $form->round_amount($fxsellprice * $form->{"discount_$i"},
 
 460       $form->{"sellprice_$i"} = $fxsellprice - $discount;
 
 463       map { $taxrate += $form->{"${_}_rate"} } split / /,
 
 464         $form->{"taxaccounts_$i"};
 
 466       # round linetotal to 2 decimal places
 
 468         $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
 
 470       if ($form->{taxincluded}) {
 
 471         $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
 
 472         $form->{"sellprice_$i"} =
 
 473           $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
 
 475         $taxamount = $linetotal * $taxrate;
 
 478       $netamount += $linetotal;
 
 480       if ($taxamount != 0) {
 
 482           $form->{amount}{ $form->{id} }{$_} +=
 
 483             $taxamount * $form->{"${_}_rate"} / $taxrate
 
 484         } split / /, $form->{"taxaccounts_$i"};
 
 487       # add amount to income, $form->{amount}{trans_id}{accno}
 
 489         $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
 
 492         $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
 
 493         $form->{exchangerate};
 
 494       $linetotal = $form->round_amount($linetotal, 2);
 
 496       # this is the difference from the inventory
 
 497       $invoicediff += ($amount - $linetotal);
 
 499       $form->{amount}{ $form->{id} }{ $form->{"income_accno_$i"} } +=
 
 502       $lastincomeaccno = $form->{"income_accno_$i"};
 
 504       # adjust and round sellprice
 
 505       $form->{"sellprice_$i"} =
 
 506         $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
 
 509       if ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"}) {
 
 511         # adjust parts onhand quantity
 
 513         if ($form->{"assembly_$i"}) {
 
 515           # do not update if assembly consists of all services
 
 516           $query = qq|SELECT sum(p.inventory_accno_id)
 
 518                       JOIN assembly a ON (a.parts_id = p.id)
 
 519                       WHERE a.id = $form->{"id_$i"}|;
 
 520           $sth = $dbh->prepare($query);
 
 521           $sth->execute || $form->dberror($query);
 
 523           if ($sth->fetchrow_array) {
 
 524             $form->update_balance($dbh, "parts", "onhand",
 
 525                                   qq|id = $form->{"id_$i"}|,
 
 526                                   $form->{"qty_$i"} * -1)
 
 527               unless $form->{shipped};
 
 531           # record assembly item as allocated
 
 532           &process_assembly($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
 
 534           $form->update_balance($dbh, "parts", "onhand",
 
 535                                 qq|id = $form->{"id_$i"}|,
 
 536                                 $form->{"qty_$i"} * -1)
 
 537             unless $form->{shipped};
 
 539           $allocated = &cogs($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
 
 543       $project_id = 'NULL';
 
 544       if ($form->{"projectnumber_$i"}) {
 
 545         $project_id = $form->{"projectnumber_$i"};
 
 548         ($form->{"deliverydate_$i"})
 
 549         ? qq|'$form->{"deliverydate_$i"}'|
 
 552       # save detail record in invoice table
 
 553       $query = qq|INSERT INTO invoice (trans_id, parts_id, description, qty,
 
 554                   sellprice, fxsellprice, discount, allocated, assemblyitem,
 
 555                   unit, deliverydate, project_id, serialnumber)
 
 556                   VALUES ($form->{id}, $form->{"id_$i"},
 
 557                   '$form->{"description_$i"}', $form->{"qty_$i"},
 
 558                   $form->{"sellprice_$i"}, $fxsellprice,
 
 559                   $form->{"discount_$i"}, $allocated, 'f',
 
 560                   '$form->{"unit_$i"}', $deliverydate, (SELECT id from project where projectnumber = '$project_id'),
 
 561                   '$form->{"serialnumber_$i"}')|;
 
 562       $dbh->do($query) || $form->dberror($query);
 
 564       if ($form->{lizenzen}) {
 
 565         if ($form->{"licensenumber_$i"}) {
 
 567             qq|SELECT i.id FROM invoice i WHERE i.trans_id=$form->{id} ORDER BY i.oid DESC LIMIT 1|;
 
 568           $sth = $dbh->prepare($query);
 
 569           $sth->execute || $form->dberror($query);
 
 571           ($invoice_row_id) = $sth->fetchrow_array;
 
 575             qq|INSERT INTO licenseinvoice (trans_id, license_id) VALUES ($invoice_row_id, $form->{"licensenumber_$i"})|;
 
 576           $dbh->do($query) || $form->dberror($query);
 
 583   $form->{datepaid} = $form->{invdate};
 
 585   # total payments, don't move we need it here
 
 586   for my $i (1 .. $form->{paidaccounts}) {
 
 587     $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
 
 588     $form->{paid} += $form->{"paid_$i"};
 
 589     $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
 
 592   my ($tax, $diff) = (0, 0);
 
 594   $netamount = $form->round_amount($netamount, 2);
 
 596   # figure out rounding errors for total amount vs netamount + taxes
 
 597   if ($form->{taxincluded}) {
 
 599     $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
 
 600     $diff += $amount - $netamount * $form->{exchangerate};
 
 601     $netamount = $amount;
 
 603     foreach my $item (split / /, $form->{taxaccounts}) {
 
 604       $amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
 
 605       $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
 
 606       $tax += $form->{amount}{ $form->{id} }{$item};
 
 607       $netamount -= $form->{amount}{ $form->{id} }{$item};
 
 610     $invoicediff += $diff;
 
 611     ######## this only applies to tax included
 
 612     if ($lastincomeaccno) {
 
 613       $form->{amount}{ $form->{id} }{$lastincomeaccno} += $invoicediff;
 
 617     $amount    = $form->round_amount($netamount * $form->{exchangerate}, 2);
 
 618     $diff      = $amount - $netamount * $form->{exchangerate};
 
 619     $netamount = $amount;
 
 620     foreach my $item (split / /, $form->{taxaccounts}) {
 
 621       $form->{amount}{ $form->{id} }{$item} =
 
 622         $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
 
 625                  $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate},
 
 628         $amount - $form->{amount}{ $form->{id} }{$item} *
 
 629         $form->{exchangerate};
 
 630       $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
 
 631       $tax += $form->{amount}{ $form->{id} }{$item};
 
 635   $form->{amount}{ $form->{id} }{ $form->{AR} } = $netamount + $tax;
 
 637     $form->round_amount($form->{paid} * $form->{exchangerate} + $diff, 2);
 
 640   $form->{amount}{ $form->{id} }{ $form->{AR} } *= -1;
 
 642   # update exchangerate
 
 643   if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
 
 644     $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate},
 
 645                                $form->{exchangerate}, 0);
 
 648   foreach my $trans_id (keys %{ $form->{amount} }) {
 
 649     foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
 
 650       next unless ($form->{expense_inventory} =~ /$accno/);
 
 652           ($form->{amount}{$trans_id}{$accno} =
 
 653            $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
 
 656         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
 
 658                     VALUES ($trans_id, (SELECT c.id FROM chart c
 
 659                                         WHERE c.accno = '$accno'),
 
 660                     $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
 
 661                     (SELECT taxkey_id  FROM chart WHERE accno = '$accno'))|;
 
 662         $dbh->do($query) || $form->dberror($query);
 
 663         $form->{amount}{$trans_id}{$accno} = 0;
 
 667     foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
 
 669           ($form->{amount}{$trans_id}{$accno} =
 
 670            $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
 
 673         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
 
 675                     VALUES ($trans_id, (SELECT id FROM chart
 
 676                                         WHERE accno = '$accno'),
 
 677                     $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
 
 678                     (SELECT taxkey_id  FROM chart WHERE accno = '$accno'))|;
 
 679         $dbh->do($query) || $form->dberror($query);
 
 684   # deduct payment differences from diff
 
 685   for my $i (1 .. $form->{paidaccounts}) {
 
 686     if ($form->{"paid_$i"} != 0) {
 
 688         $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
 
 689       $diff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
 
 693   # force AR entry if 0
 
 694   #  $form->{amount}{$form->{id}}{$form->{AR}} = 1 if ($form->{amount}{$form->{id}}{$form->{AR}} == 0);
 
 696   # record payments and offsetting AR
 
 697   for my $i (1 .. $form->{paidaccounts}) {
 
 699     if ($form->{"paid_$i"} != 0) {
 
 700       my ($accno) = split /--/, $form->{"AR_paid_$i"};
 
 701       $form->{"datepaid_$i"} = $form->{invdate}
 
 702         unless ($form->{"datepaid_$i"});
 
 703       $form->{datepaid} = $form->{"datepaid_$i"};
 
 707       if ($form->{currency} eq $form->{defaultcurrency}) {
 
 708         $form->{"exchangerate_$i"} = 1;
 
 711           $form->check_exchangerate($myconfig, $form->{currency},
 
 712                                     $form->{"datepaid_$i"}, 'buy');
 
 714         $form->{"exchangerate_$i"} =
 
 717           : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
 
 722         $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $diff,
 
 725       if ($form->{amount}{ $form->{id} }{ $form->{AR} } != 0) {
 
 726         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
 
 728                     VALUES ($form->{id}, (SELECT c.id FROM chart c
 
 729                                         WHERE c.accno = '$form->{AR}'),
 
 730                     $amount, '$form->{"datepaid_$i"}')|;
 
 731         $dbh->do($query) || $form->dberror($query);
 
 735       $form->{"paid_$i"} *= -1;
 
 737       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
 
 739                   VALUES ($form->{id}, (SELECT c.id FROM chart c
 
 740                                       WHERE c.accno = '$accno'),
 
 741                   $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
 
 742                   '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
 
 743       $dbh->do($query) || $form->dberror($query);
 
 745       # exchangerate difference
 
 746       $form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
 
 747         $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $diff;
 
 751         $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
 
 752         $form->{"exchangerate_$i"};
 
 754         $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
 
 757         $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
 
 763       # update exchange rate
 
 764       if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
 
 765         $form->update_exchangerate($dbh, $form->{currency},
 
 766                                    $form->{"datepaid_$i"},
 
 767                                    $form->{"exchangerate_$i"}, 0);
 
 772   # record exchange rate differences and gains/losses
 
 773   foreach my $accno (keys %{ $form->{fx} }) {
 
 774     foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
 
 776           ($form->{fx}{$accno}{$transdate} =
 
 777            $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
 
 781         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
 
 782                     transdate, cleared, fx_transaction)
 
 784                            (SELECT c.id FROM chart c
 
 785                             WHERE c.accno = '$accno'),
 
 786                     $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1')|;
 
 787         $dbh->do($query) || $form->dberror($query);
 
 792   $amount = $netamount + $tax;
 
 794   # set values which could be empty to 0
 
 796   $form->{taxincluded} *= 1;
 
 797   my $datepaid = ($form->{paid})    ? qq|'$form->{datepaid}'| : "NULL";
 
 798   my $duedate  = ($form->{duedate}) ? qq|'$form->{duedate}'|  : "NULL";
 
 800   # fill in subject if there is none
 
 801   $form->{subject} = qq|$form->{label} $form->{invnumber}|
 
 802     unless $form->{subject};
 
 804   # if there is a message stuff it into the intnotes
 
 805   my $cc  = "Cc: $form->{cc}\\r\n"   if $form->{cc};
 
 806   my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
 
 807   my $now = scalar localtime;
 
 808   $form->{intnotes} .= qq|\r
 
 809 \r| if $form->{intnotes};
 
 811   $form->{intnotes} .= qq|[email]\r
 
 814 $cc${bcc}Subject: $form->{subject}\r
 
 816 Message: $form->{message}\r| if $form->{message};
 
 819   $query = qq|UPDATE ar set
 
 820               invnumber = '$form->{invnumber}',
 
 821               ordnumber = '$form->{ordnumber}',
 
 822               quonumber = '$form->{quonumber}',
 
 823               cusordnumber = '$form->{cusordnumber}',
 
 824               transdate = '$form->{invdate}',
 
 825               customer_id = $form->{customer_id},
 
 827               netamount = $netamount,
 
 828               paid = $form->{paid},
 
 829               datepaid = $datepaid,
 
 832               shippingpoint = '$form->{shippingpoint}',
 
 833               shipvia = '$form->{shipvia}',
 
 834               terms = $form->{terms},
 
 835               notes = '$form->{notes}',
 
 836               intnotes = '$form->{intnotes}',
 
 837               taxincluded = '$form->{taxincluded}',
 
 838               curr = '$form->{currency}',
 
 839               department_id = $form->{department_id},
 
 840               employee_id = $form->{employee_id},
 
 841               cp_id = $form->{contact_id}
 
 842               WHERE id = $form->{id}
 
 844   $dbh->do($query) || $form->dberror($query);
 
 846   $form->{pago_total} = $amount;
 
 849   $form->{name} = $form->{customer};
 
 850   $form->{name} =~ s/--$form->{customer_id}//;
 
 851   $form->add_shipto($dbh, $form->{id});
 
 853   # save printed, emailed and queued
 
 854   $form->save_status($dbh);
 
 856   if ($form->{webdav}) {
 
 857     &webdav_folder($myconfig, $form);
 
 860   my $rc = $dbh->commit;
 
 863   $main::lxdebug->leave_sub();
 
 868 sub process_assembly {
 
 869   $main::lxdebug->enter_sub();
 
 871   my ($dbh, $form, $id, $totalqty) = @_;
 
 873   my $query = qq|SELECT a.parts_id, a.qty, p.assembly,
 
 874                  p.partnumber, p.description, p.unit,
 
 875                  p.inventory_accno_id, p.income_accno_id,
 
 878                  JOIN parts p ON (a.parts_id = p.id)
 
 880   my $sth = $dbh->prepare($query);
 
 881   $sth->execute || $form->dberror($query);
 
 883   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 887     $ref->{inventory_accno_id} *= 1;
 
 888     $ref->{expense_accno_id}   *= 1;
 
 890     map { $ref->{$_} =~ s/\'/\'\'/g } (qw(partnumber description unit));
 
 892     # multiply by number of assemblies
 
 893     $ref->{qty} *= $totalqty;
 
 895     if ($ref->{assembly}) {
 
 896       &process_assembly($dbh, $form, $ref->{parts_id}, $ref->{qty});
 
 899       if ($ref->{inventory_accno_id}) {
 
 900         $allocated = &cogs($dbh, $form, $ref->{parts_id}, $ref->{qty});
 
 904     # save detail record for individual assembly item in invoice table
 
 905     $query = qq|INSERT INTO invoice (trans_id, description, parts_id, qty,
 
 906                 sellprice, fxsellprice, allocated, assemblyitem, unit)
 
 908                 ($form->{id}, '$ref->{description}',
 
 909                 $ref->{parts_id}, $ref->{qty}, 0, 0, $allocated, 't',
 
 911     $dbh->do($query) || $form->dberror($query);
 
 917   $main::lxdebug->leave_sub();
 
 921   $main::lxdebug->enter_sub();
 
 923   my ($dbh, $form, $id, $totalqty) = @_;
 
 925   my $query = qq|SELECT i.id, i.trans_id, i.qty, i.allocated, i.sellprice,
 
 926                    (SELECT c.accno FROM chart c
 
 927                     WHERE p.inventory_accno_id = c.id) AS inventory_accno,
 
 928                    (SELECT c.accno FROM chart c
 
 929                     WHERE p.expense_accno_id = c.id) AS expense_accno
 
 930                   FROM invoice i, parts p
 
 931                   WHERE i.parts_id = p.id
 
 933                   AND (i.qty + i.allocated) < 0
 
 935   my $sth = $dbh->prepare($query);
 
 936   $sth->execute || $form->dberror($query);
 
 941   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 942     if (($qty = (($ref->{qty} * -1) - $ref->{allocated})) > $totalqty) {
 
 946     $form->update_balance($dbh, "invoice", "allocated", qq|id = $ref->{id}|,
 
 949     # total expenses and inventory
 
 950     # sellprice is the cost of the item
 
 951     $linetotal = $form->round_amount($ref->{sellprice} * $qty, 2);
 
 956       $form->{amount}{ $form->{id} }{ $ref->{expense_accno} } += -$linetotal;
 
 957       $form->{expense_inventory} .= " " . $ref->{expense_accno};
 
 960       $form->{amount}{ $form->{id} }{ $ref->{inventory_accno} } -= -$linetotal;
 
 961       $form->{expense_inventory} .= " " . $ref->{inventory_accno};
 
 967     last if (($totalqty -= $qty) <= 0);
 
 972   $main::lxdebug->leave_sub();
 
 977 sub reverse_invoice {
 
 978   $main::lxdebug->enter_sub();
 
 980   my ($dbh, $form) = @_;
 
 982   # reverse inventory items
 
 983   my $query = qq|SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.assembly,
 
 986                  JOIN parts p ON (i.parts_id = p.id)
 
 987                  WHERE i.trans_id = $form->{id}|;
 
 988   my $sth = $dbh->prepare($query);
 
 989   $sth->execute || $form->dberror($query);
 
 991   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 993     if ($ref->{inventory_accno_id} || $ref->{assembly}) {
 
 995       # if the invoice item is not an assemblyitem adjust parts onhand
 
 996       if (!$ref->{assemblyitem}) {
 
 998         # adjust onhand in parts table
 
 999         $form->update_balance($dbh, "parts", "onhand",
 
1000                               qq|id = $ref->{parts_id}|,
 
1004       # loop if it is an assembly
 
1005       next if ($ref->{assembly});
 
1007       # de-allocated purchases
 
1008       $query = qq|SELECT i.id, i.trans_id, i.allocated
 
1010                   WHERE i.parts_id = $ref->{parts_id}
 
1012                   ORDER BY i.trans_id DESC|;
 
1013       my $sth = $dbh->prepare($query);
 
1014       $sth->execute || $form->dberror($query);
 
1016       while (my $inhref = $sth->fetchrow_hashref(NAME_lc)) {
 
1018         if (($ref->{qty} - $inhref->{allocated}) > 0) {
 
1019           $qty = $inhref->{allocated};
 
1023         $form->update_balance($dbh, "invoice", "allocated",
 
1024                               qq|id = $inhref->{id}|,
 
1027         last if (($ref->{qty} -= $qty) <= 0);
 
1036   $query = qq|DELETE FROM acc_trans
 
1037               WHERE trans_id = $form->{id}|;
 
1038   $dbh->do($query) || $form->dberror($query);
 
1040   # delete invoice entries
 
1041   $query = qq|DELETE FROM invoice
 
1042               WHERE trans_id = $form->{id}|;
 
1043   $dbh->do($query) || $form->dberror($query);
 
1045   if ($form->{lizenzen}) {
 
1046     $query = qq|DELETE FROM licenseinvoice
 
1047               WHERE trans_id in (SELECT id FROM invoice WHERE trans_id = $form->{id})|;
 
1048     $dbh->do($query) || $form->dberror($query);
 
1051   $query = qq|DELETE FROM shipto
 
1052               WHERE trans_id = $form->{id}|;
 
1053   $dbh->do($query) || $form->dberror($query);
 
1055   $main::lxdebug->leave_sub();
 
1058 sub delete_invoice {
 
1059   $main::lxdebug->enter_sub();
 
1061   my ($self, $myconfig, $form, $spool) = @_;
 
1063   # connect to database
 
1064   my $dbh = $form->dbconnect_noauto($myconfig);
 
1066   &reverse_invoice($dbh, $form);
 
1069   my $query = qq|DELETE FROM ar
 
1070                  WHERE id = $form->{id}|;
 
1071   $dbh->do($query) || $form->dberror($query);
 
1073   # delete spool files
 
1074   $query = qq|SELECT s.spoolfile FROM status s
 
1075               WHERE s.trans_id = $form->{id}|;
 
1076   my $sth = $dbh->prepare($query);
 
1077   $sth->execute || $self->dberror($query);
 
1080   my @spoolfiles = ();
 
1082   while (($spoolfile) = $sth->fetchrow_array) {
 
1083     push @spoolfiles, $spoolfile;
 
1087   # delete status entries
 
1088   $query = qq|DELETE FROM status
 
1089               WHERE trans_id = $form->{id}|;
 
1090   $dbh->do($query) || $form->dberror($query);
 
1092   my $rc = $dbh->commit;
 
1096     foreach $spoolfile (@spoolfiles) {
 
1097       unlink "$spool/$spoolfile" if $spoolfile;
 
1101   $main::lxdebug->leave_sub();
 
1106 sub retrieve_invoice {
 
1107   $main::lxdebug->enter_sub();
 
1109   my ($self, $myconfig, $form) = @_;
 
1111   # connect to database
 
1112   my $dbh = $form->dbconnect_noauto($myconfig);
 
1118     # get default accounts and last invoice number
 
1119     $query = qq|SELECT (SELECT c.accno FROM chart c
 
1120                         WHERE d.inventory_accno_id = c.id) AS inventory_accno,
 
1121                        (SELECT c.accno FROM chart c
 
1122                         WHERE d.income_accno_id = c.id) AS income_accno,
 
1123                        (SELECT c.accno FROM chart c
 
1124                         WHERE d.expense_accno_id = c.id) AS expense_accno,
 
1125                        (SELECT c.accno FROM chart c
 
1126                         WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
1127                        (SELECT c.accno FROM chart c
 
1128                         WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
 
1129                 d.curr AS currencies
 
1132     $query = qq|SELECT (SELECT c.accno FROM chart c
 
1133                         WHERE d.inventory_accno_id = c.id) AS inventory_accno,
 
1134                        (SELECT c.accno FROM chart c
 
1135                         WHERE d.income_accno_id = c.id) AS income_accno,
 
1136                        (SELECT c.accno FROM chart c
 
1137                         WHERE d.expense_accno_id = c.id) AS expense_accno,
 
1138                        (SELECT c.accno FROM chart c
 
1139                         WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
1140                        (SELECT c.accno FROM chart c
 
1141                         WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
 
1142                 d.curr AS currencies, current_date AS invdate
 
1145   my $sth = $dbh->prepare($query);
 
1146   $sth->execute || $form->dberror($query);
 
1148   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
1149   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1155     $query = qq|SELECT a.invnumber, a.ordnumber, a.quonumber, a.cusordnumber,
 
1156                 a.transdate AS invdate, a.paid,
 
1157                 a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes,
 
1158                 a.duedate, a.taxincluded, a.curr AS currency,
 
1159                 a.employee_id, e.name AS employee
 
1161                 LEFT JOIN employee e ON (e.id = a.employee_id)
 
1162                 WHERE a.id = $form->{id}|;
 
1163     $sth = $dbh->prepare($query);
 
1164     $sth->execute || $form->dberror($query);
 
1166     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1167     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1170     $form->{exchangerate} =
 
1171       $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
 
1175     $query = qq|SELECT s.* FROM shipto s
 
1176                 WHERE s.trans_id = $form->{id}|;
 
1177     $sth = $dbh->prepare($query);
 
1178     $sth->execute || $form->dberror($query);
 
1180     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1181     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1184     # get printed, emailed
 
1185     $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
 
1187                 WHERE s.trans_id = $form->{id}|;
 
1188     $sth = $dbh->prepare($query);
 
1189     $sth->execute || $form->dberror($query);
 
1191     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1192       $form->{printed} .= "$ref->{formname} " if $ref->{printed};
 
1193       $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
 
1194       $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
 
1195         if $ref->{spoolfile};
 
1198     map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
 
1200     # retrieve individual items
 
1201     $query = qq|SELECT (SELECT c.accno FROM chart c
 
1202                        WHERE p.inventory_accno_id = c.id)
 
1204                        (SELECT c.accno FROM chart c
 
1205                        WHERE p.income_accno_id = c.id)
 
1207                        (SELECT c.accno FROM chart c
 
1208                        WHERE p.expense_accno_id = c.id)
 
1210                 i.description, i.qty, i.fxsellprice AS sellprice,
 
1211                 i.discount, i.parts_id AS id, i.unit, i.deliverydate,
 
1212                 i.project_id, pr.projectnumber, i.serialnumber,
 
1213                 p.partnumber, p.assembly, p.bin, p.notes AS partnotes, i.id AS invoice_pos,
 
1216                 JOIN parts p ON (i.parts_id = p.id)
 
1217                 LEFT JOIN project pr ON (i.project_id = pr.id)
 
1218                 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1219                 WHERE i.trans_id = $form->{id}
 
1220                 AND NOT i.assemblyitem = '1'
 
1222     $sth = $dbh->prepare($query);
 
1223     $sth->execute || $form->dberror($query);
 
1224     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1226       #set expense_accno=inventory_accno if they are different => bilanz
 
1228         ($ref->{expense_accno} != $ref->{inventory_accno})
 
1229         ? $ref->{inventory_accno}
 
1230         : $ref->{expense_accno};
 
1232       # get tax rates and description
 
1234         ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
 
1235       $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
 
1237                  WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
 
1239       $stw = $dbh->prepare($query);
 
1240       $stw->execute || $form->dberror($query);
 
1241       $ref->{taxaccounts} = "";
 
1242       while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
 
1244         #    if ($customertax{$ref->{accno}}) {
 
1245         $ref->{taxaccounts} .= "$ptr->{accno} ";
 
1246         if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
 
1247           $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
 
1248           $form->{"$ptr->{accno}_description"} = $ptr->{description};
 
1249           $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
 
1250           $form->{taxaccounts} .= "$ptr->{accno} ";
 
1255       if ($form->{lizenzen}) {
 
1256         $query = qq|SELECT l.licensenumber, l.id AS licenseid
 
1257                  FROM license l, licenseinvoice li
 
1258                  WHERE l.id = li.license_id AND li.trans_id = $ref->{invoice_pos}|;
 
1259         $stg = $dbh->prepare($query);
 
1260         $stg->execute || $form->dberror($query);
 
1261         ($licensenumber, $licenseid) = $stg->fetchrow_array();
 
1263           "<option value=\"$licenseid\">$licensenumber</option>";
 
1267       chop $ref->{taxaccounts};
 
1268       push @{ $form->{invoice_details} }, $ref;
 
1273     if ($form->{webdav}) {
 
1274       &webdav_folder($myconfig, $form);
 
1278   my $rc = $dbh->commit;
 
1281   $main::lxdebug->leave_sub();
 
1287   $main::lxdebug->enter_sub();
 
1289   my ($self, $myconfig, $form) = @_;
 
1291   # connect to database
 
1292   my $dbh = $form->dbconnect($myconfig);
 
1294   my $dateformat = $myconfig->{dateformat};
 
1295   $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
 
1299     ? "to_date('$form->{invdate}', '$dateformat')"
 
1302   $form->{customer_id} *= 1;
 
1305   my $query = qq|SELECT c.name AS customer, c.discount, c.creditlimit, c.terms,
 
1306                  c.email, c.cc, c.bcc, c.language,
 
1307                  c.street, c.zipcode, c.city, c.country,
 
1308                  $duedate + c.terms AS duedate, c.notes AS intnotes,
 
1309                  b.discount AS tradediscount, b.description AS business
 
1311                  LEFT JOIN business b ON (b.id = c.business_id)
 
1312                  WHERE c.id = $form->{customer_id}|;
 
1313   my $sth = $dbh->prepare($query);
 
1314   $sth->execute || $form->dberror($query);
 
1316   $ref = $sth->fetchrow_hashref(NAME_lc);
 
1318   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1321   $form->{creditremaining} = $form->{creditlimit};
 
1322   $query = qq|SELECT SUM(a.amount - a.paid)
 
1324               WHERE a.customer_id = $form->{customer_id}|;
 
1325   $sth = $dbh->prepare($query);
 
1326   $sth->execute || $form->dberror($query);
 
1328   ($form->{creditremaining}) -= $sth->fetchrow_array;
 
1332   $query = qq|SELECT o.amount,
 
1333                 (SELECT e.buy FROM exchangerate e
 
1334                  WHERE e.curr = o.curr
 
1335                  AND e.transdate = o.transdate)
 
1337               WHERE o.customer_id = $form->{customer_id}
 
1338               AND o.quotation = '0'
 
1339               AND o.closed = '0'|;
 
1340   $sth = $dbh->prepare($query);
 
1341   $sth->execute || $form->dberror($query);
 
1343   while (my ($amount, $exch) = $sth->fetchrow_array) {
 
1344     $exch = 1 unless $exch;
 
1345     $form->{creditremaining} -= $amount * $exch;
 
1349   $form->get_contacts($dbh, $form->{customer_id});
 
1350   ($null, $form->{cp_id}) = split /--/, $form->{contact};
 
1352   # get contact if selected
 
1353   if ($form->{contact} ne "--" && $form->{contact} ne "") {
 
1354     $form->get_contact($dbh, $form->{cp_id});
 
1357   # get shipto if we did not converted an order or invoice
 
1358   if (!$form->{shipto}) {
 
1359     map { delete $form->{$_} }
 
1360       qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
 
1362     $query = qq|SELECT s.* FROM shipto s
 
1363                 WHERE s.trans_id = $form->{customer_id}|;
 
1364     $sth = $dbh->prepare($query);
 
1365     $sth->execute || $form->dberror($query);
 
1367     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1368     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1372   # get taxes we charge for this customer
 
1373   $query = qq|SELECT c.accno
 
1375               JOIN customertax ct ON (ct.chart_id = c.id)
 
1376               WHERE ct.customer_id = $form->{customer_id}|;
 
1377   $sth = $dbh->prepare($query);
 
1378   $sth->execute || $form->dberror($query);
 
1380   my $customertax = ();
 
1381   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1382     $customertax{ $ref->{accno} } = 1;
 
1386   # setup last accounts used for this customer
 
1387   if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
 
1388     $query = qq|SELECT c.accno, c.description, c.link, c.category
 
1390                 JOIN acc_trans ac ON (ac.chart_id = c.id)
 
1391                 JOIN ar a ON (a.id = ac.trans_id)
 
1392                 WHERE a.customer_id = $form->{customer_id}
 
1393                 AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%')
 
1394                 AND a.id IN (SELECT max(a2.id) FROM ar a2
 
1395                              WHERE a2.customer_id = $form->{customer_id})|;
 
1396     $sth = $dbh->prepare($query);
 
1397     $sth->execute || $form->dberror($query);
 
1400     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1401       if ($ref->{category} eq 'I') {
 
1403         $form->{"AR_amount_$i"} = "$ref->{accno}--$ref->{description}";
 
1405       if ($ref->{category} eq 'A') {
 
1406         $form->{ARselected} = $form->{AR_1} =
 
1407           "$ref->{accno}--$ref->{description}";
 
1411     $form->{rowcount} = $i if ($i && !$form->{type});
 
1416   $main::lxdebug->leave_sub();
 
1420   $main::lxdebug->enter_sub();
 
1422   my ($self, $myconfig, $form) = @_;
 
1424   my $i = $form->{rowcount};
 
1426   my $where = "NOT p.obsolete = '1'";
 
1428   if ($form->{"partnumber_$i"}) {
 
1429     my $partnumber = $form->like(lc $form->{"partnumber_$i"});
 
1430     $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
 
1432   if ($form->{"description_$i"}) {
 
1433     my $description = $form->like(lc $form->{"description_$i"});
 
1434     $where .= " AND lower(p.description) LIKE '$description'";
 
1437   if ($form->{"partsgroup_$i"}) {
 
1438     my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
 
1439     $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
 
1442   if ($form->{"description_$i"}) {
 
1443     $where .= " ORDER BY p.description";
 
1445     $where .= " ORDER BY p.partnumber";
 
1448   # connect to database
 
1449   my $dbh = $form->dbconnect($myconfig);
 
1451   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
 
1453                         c1.accno AS inventory_accno,
 
1454                         c2.accno AS income_accno,
 
1455                         c3.accno AS expense_accno,
 
1456                  p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
 
1459                  LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
 
1460                  LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
 
1461                  LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
 
1462                  LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
 
1464   my $sth = $dbh->prepare($query);
 
1465   $sth->execute || $form->dberror($query);
 
1467   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1469     #set expense_accno=inventory_accno if they are different => bilanz
 
1471       ($ref->{expense_accno} != $ref->{inventory_accno})
 
1472       ? $ref->{inventory_accno}
 
1473       : $ref->{expense_accno};
 
1475     # get tax rates and description
 
1477       ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
 
1478     $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
 
1480               WHERE c.id=t.chart_id AND t.taxkey in (SELECT c2.taxkey_id from chart c2 where c2.accno = '$accno_id')
 
1482     $stw = $dbh->prepare($query);
 
1483     $stw->execute || $form->dberror($query);
 
1485     $ref->{taxaccounts} = "";
 
1486     while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
 
1488       #    if ($customertax{$ref->{accno}}) {
 
1489       $ref->{taxaccounts} .= "$ptr->{accno} ";
 
1490       if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
 
1491         $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
 
1492         $form->{"$ptr->{accno}_description"} = $ptr->{description};
 
1493         $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
 
1494         $form->{taxaccounts} .= "$ptr->{accno} ";
 
1500     chop $ref->{taxaccounts};
 
1502     push @{ $form->{item_list} }, $ref;
 
1504     if ($form->{lizenzen}) {
 
1505       if ($ref->{inventory_accno} > 0) {
 
1507           qq| SELECT l.* FROM license l WHERE l.parts_id = $ref->{id} AND NOT l.id IN (SELECT li.license_id FROM licenseinvoice li)|;
 
1508         $stw = $dbh->prepare($query);
 
1509         $stw->execute || $form->dberror($query);
 
1510         while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
 
1511           push @{ $form->{LIZENZEN}{ $ref->{id} } }, $ptr;
 
1521   $main::lxdebug->leave_sub();
 
1525   $main::lxdebug->enter_sub();
 
1527   my ($myconfig, $form) = @_;
 
1530     $path = "webdav/rechnungen/" . $form->{invnumber}, last SWITCH
 
1531       if ($form->{vc} eq "customer");
 
1532     $path = "webdav/einkaufsrechnungen/" . $form->{invnumber}, last SWITCH
 
1533       if ($form->{vc} eq "vendor");
 
1537     mkdir($path, 0770) or die "can't make directory $!\n";
 
1541       foreach $file (@files) {
 
1542         $file =~ /\/([^\/]*)$/;
 
1544         $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
 
1546         $link  = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
 
1547         $form->{WEBDAV}{$fname} = $link;
 
1552   $main::lxdebug->leave_sub();