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) 1999-2003
 
  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 #======================================================================
 
  38   $main::lxdebug->enter_sub();
 
  40   my ($self, $myconfig, $form) = @_;
 
  43   my $dbh = $form->dbconnect($myconfig);
 
  46   my $ordnumber = 'ordnumber';
 
  48   my ($null, $department_id) = split /--/, $form->{department};
 
  50   my $department = " AND o.department_id = $department_id" if $department_id;
 
  52   my $rate = ($form->{vc} eq 'customer') ? 'buy' : 'sell';
 
  54   if ($form->{type} =~ /_quotation$/) {
 
  56     $ordnumber = 'quonumber';
 
  59   my $number = $form->like(lc $form->{$ordnumber});
 
  60   my $name   = $form->like(lc $form->{ $form->{vc} });
 
  62   my $query = qq|SELECT o.id, o.ordnumber, o.transdate, o.reqdate,
 
  63                  o.amount, ct.name, o.netamount, o.$form->{vc}_id,
 
  64                  ex.$rate AS exchangerate,
 
  65                  o.closed, o.quonumber, o.shippingpoint, o.shipvia,
 
  68                  JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
 
  69                  LEFT JOIN employee e ON (o.employee_id = e.id)
 
  70                  LEFT JOIN exchangerate ex ON (ex.curr = o.curr
 
  71                                                AND ex.transdate = o.transdate)
 
  72                  WHERE o.quotation = '$quotation'
 
  75   # build query if type eq (ship|receive)_order
 
  76   if ($form->{type} =~ /(ship|receive)_order/) {
 
  77     my ($warehouse, $warehouse_id) = split /--/, $form->{warehouse};
 
  79     $query = qq|SELECT DISTINCT ON (o.id) o.id, o.ordnumber, o.transdate,
 
  80                  o.reqdate, o.amount, ct.name, o.netamount, o.$form->{vc}_id,
 
  81                  ex.$rate AS exchangerate,
 
  82                  o.closed, o.quonumber, o.shippingpoint, o.shipvia,
 
  85                  JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
 
  86                  JOIN orderitems oi ON (oi.trans_id = o.id)
 
  87                  JOIN parts p ON (p.id = oi.parts_id)|;
 
  89     if ($warehouse_id && $form->{type} eq 'ship_order') {
 
  91                  JOIN inventory i ON (oi.parts_id = i.parts_id)
 
  96                  LEFT JOIN employee e ON (o.employee_id = e.id)
 
  97                  LEFT JOIN exchangerate ex ON (ex.curr = o.curr
 
  98                                                AND ex.transdate = o.transdate)
 
  99                  WHERE o.quotation = '0'
 
 100                  AND (p.inventory_accno_id > 0 OR p.assembly = '1')
 
 101                  AND oi.qty <> oi.ship
 
 104     if ($warehouse_id && $form->{type} eq 'ship_order') {
 
 106                  AND i.warehouse_id = $warehouse_id
 
 107                  AND i.qty >= (oi.qty - oi.ship)
 
 113   if ($form->{"$form->{vc}_id"}) {
 
 114     $query .= qq| AND o.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
 
 116     if ($form->{ $form->{vc} }) {
 
 117       $query .= " AND lower(ct.name) LIKE '$name'";
 
 120   if (!$form->{open} && !$form->{closed}) {
 
 121     $query .= " AND o.id = 0";
 
 122   } elsif (!($form->{open} && $form->{closed})) {
 
 123     $query .= ($form->{open}) ? " AND o.closed = '0'" : " AND o.closed = '1'";
 
 126   my $sortorder = join ', ',
 
 127     ("o.id", $form->sort_columns(transdate, $ordnumber, name));
 
 128   $sortorder = $form->{sort} unless $sortorder;
 
 130   $query .= " AND lower($ordnumber) LIKE '$number'" if $form->{$ordnumber};
 
 131   $query .= " AND o.transdate >= '$form->{transdatefrom}'"
 
 132     if $form->{transdatefrom};
 
 133   $query .= " AND o.transdate <= '$form->{transdateto}'"
 
 134     if $form->{transdateto};
 
 135   $query .= " ORDER by $sortorder";
 
 137   my $sth = $dbh->prepare($query);
 
 138   $sth->execute || $form->dberror($query);
 
 141   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 142     $ref->{exchangerate} = 1 unless $ref->{exchangerate};
 
 143     push @{ $form->{OE} }, $ref if $ref->{id} != $id{ $ref->{id} };
 
 144     $id{ $ref->{id} } = $ref->{id};
 
 150   $main::lxdebug->leave_sub();
 
 154   $main::lxdebug->enter_sub();
 
 156   my ($self, $myconfig, $form) = @_;
 
 158   # connect to database, turn off autocommit
 
 159   my $dbh = $form->dbconnect_noauto($myconfig);
 
 161   my ($query, $sth, $null);
 
 162   my $exchangerate = 0;
 
 164   ($null, $form->{employee_id}) = split /--/, $form->{employee};
 
 165   unless ($form->{employee_id}) {
 
 166     $form->get_employee($dbh);
 
 169   ($null, $form->{contact_id}) = split /--/, $form->{contact};
 
 170   $form->{contact_id} *= 1;
 
 172   my $ml = ($form->{type} eq 'sales_order') ? 1 : -1;
 
 176     &adj_onhand($dbh, $form, $ml) if $form->{type} =~ /_order$/;
 
 178     $query = qq|DELETE FROM orderitems
 
 179                 WHERE trans_id = $form->{id}|;
 
 180     $dbh->do($query) || $form->dberror($query);
 
 182     $query = qq|DELETE FROM shipto
 
 183                 WHERE trans_id = $form->{id}|;
 
 184     $dbh->do($query) || $form->dberror($query);
 
 188     my $uid = rand() . time;
 
 190     $uid .= $form->{login};
 
 192     $uid = substr($uid, 2, 75);
 
 194     $query = qq|INSERT INTO oe (ordnumber, employee_id)
 
 195                 VALUES ('$uid', $form->{employee_id})|;
 
 196     $dbh->do($query) || $form->dberror($query);
 
 198     $query = qq|SELECT o.id FROM oe o
 
 199                 WHERE o.ordnumber = '$uid'|;
 
 200     $sth = $dbh->prepare($query);
 
 201     $sth->execute || $form->dberror($query);
 
 203     ($form->{id}) = $sth->fetchrow_array;
 
 207   map { $form->{$_} =~ s/\'/\'\'/g }
 
 208     qw(ordnumber quonumber shippingpoint shipvia notes intnotes message);
 
 223   for my $i (1 .. $form->{rowcount}) {
 
 226       $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"})
 
 229     if ($form->{"qty_$i"}) {
 
 231       map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
 
 232         qw(partnumber description unit);
 
 234       # set values to 0 if nothing entered
 
 235       $form->{"discount_$i"} =
 
 236         $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
 
 238       $form->{"sellprice_$i"} =
 
 239         $form->parse_amount($myconfig, $form->{"sellprice_$i"});
 
 240       $fxsellprice = $form->{"sellprice_$i"};
 
 242       my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
 
 244       my $decimalplaces = ($dec > 2) ? $dec : 2;
 
 247         $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
 
 249       $form->{"sellprice_$i"} =
 
 250         $form->round_amount($form->{"sellprice_$i"} - $discount,
 
 253       $form->{"inventory_accno_$i"} *= 1;
 
 254       $form->{"expense_accno_$i"}   *= 1;
 
 257         $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
 
 259       @taxaccounts = split / /, $form->{"taxaccounts_$i"};
 
 263       map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
 
 265       if ($form->{taxincluded}) {
 
 266         $taxamount = $linetotal * $taxrate / (1 + $taxrate);
 
 267         $taxbase   = $linetotal - $taxamount;
 
 269         # we are not keeping a natural price, do not round
 
 270         $form->{"sellprice_$i"} =
 
 271           $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
 
 273         $taxamount = $linetotal * $taxrate;
 
 274         $taxbase   = $linetotal;
 
 277       if ($form->round_amount($taxrate, 7) == 0) {
 
 278         if ($form->{taxincluded}) {
 
 279           foreach $item (@taxaccounts) {
 
 281               $form->round_amount($linetotal * $form->{"${item}_rate"} /
 
 282                                     (1 + abs($form->{"${item}_rate"})),
 
 285             $taxaccounts{$item} += $taxamount;
 
 286             $taxdiff            += $taxamount;
 
 288             $taxbase{$item} += $taxbase;
 
 290           $taxaccounts{ $taxaccounts[0] } += $taxdiff;
 
 292           foreach $item (@taxaccounts) {
 
 293             $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
 
 294             $taxbase{$item}     += $taxbase;
 
 298         foreach $item (@taxaccounts) {
 
 299           $taxaccounts{$item} +=
 
 300             $taxamount * $form->{"${item}_rate"} / $taxrate;
 
 301           $taxbase{$item} += $taxbase;
 
 305       $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
 
 307       $project_id = 'NULL';
 
 308       if ($form->{"projectnumber_$i"}) {
 
 309         $project_id = $form->{"projectnumber_$i"};
 
 312         ($form->{"reqdate_$i"}) ? qq|'$form->{"reqdate_$i"}'| : "NULL";
 
 314       # save detail record in orderitems table
 
 315       $query = qq|INSERT INTO orderitems (|;
 
 316       $query .= "id, " if $form->{"orderitems_id_$i"};
 
 317       $query .= qq|trans_id, parts_id, description, qty, sellprice, discount,
 
 318                    unit, reqdate, project_id, serialnumber, ship)
 
 320       $query .= qq|$form->{"orderitems_id_$i"},|
 
 321         if $form->{"orderitems_id_$i"};
 
 322       $query .= qq|$form->{id}, $form->{"id_$i"},
 
 323                    '$form->{"description_$i"}', $form->{"qty_$i"},
 
 324                    $fxsellprice, $form->{"discount_$i"},
 
 325                    '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
 
 326                    '$form->{"serialnumber_$i"}', $form->{"ship_$i"})|;
 
 327       $dbh->do($query) || $form->dberror($query);
 
 329       $form->{"sellprice_$i"} = $fxsellprice;
 
 330       $form->{"discount_$i"} *= 100;
 
 334   # set values which could be empty
 
 335   map { $form->{$_} *= 1 }
 
 336     qw(vendor_id customer_id taxincluded closed quotation);
 
 338   $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
 
 342   map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
 
 344   $amount = $form->round_amount($netamount + $tax, 2);
 
 345   $netamount = $form->round_amount($netamount, 2);
 
 347   if ($form->{currency} eq $form->{defaultcurrency}) {
 
 348     $form->{exchangerate} = 1;
 
 351       $form->check_exchangerate($myconfig,
 
 354                                 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
 
 357   $form->{exchangerate} =
 
 360     : $form->parse_amount($myconfig, $form->{exchangerate});
 
 364   # fill in subject if there is none
 
 365   if ($form->{type} =~ /_order$/) {
 
 367     $form->{subject} = qq|$form->{label} $form->{ordnumber}|
 
 368       unless $form->{subject};
 
 371     $form->{subject} = qq|$form->{label} $form->{quonumber}|
 
 372       unless $form->{subject};
 
 375   # if there is a message stuff it into the intnotes
 
 376   my $cc  = "Cc: $form->{cc}\\r\n"   if $form->{cc};
 
 377   my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
 
 378   my $now = scalar localtime;
 
 379   $form->{intnotes} .= qq|\r
 
 380 \r| if $form->{intnotes};
 
 382   $form->{intnotes} .= qq|[email]\r
 
 385 $cc${bcc}Subject: $form->{subject}\r
 
 387 Message: $form->{message}\r| if $form->{message};
 
 389   ($null, $form->{department_id}) = split(/--/, $form->{department});
 
 390   $form->{department_id} *= 1;
 
 393   $query = qq|UPDATE oe set
 
 394               ordnumber = '$form->{ordnumber}',
 
 395               quonumber = '$form->{quonumber}',
 
 396               cusordnumber = '$form->{cusordnumber}',
 
 397               transdate = '$form->{transdate}',
 
 398               vendor_id = $form->{vendor_id},
 
 399               customer_id = $form->{customer_id},
 
 401               netamount = $netamount,
 
 403               taxincluded = '$form->{taxincluded}',
 
 404               shippingpoint = '$form->{shippingpoint}',
 
 405               shipvia = '$form->{shipvia}',
 
 406               notes = '$form->{notes}',
 
 407               intnotes = '$form->{intnotes}',
 
 408               curr = '$form->{currency}',
 
 409               closed = '$form->{closed}',
 
 410               quotation = '$quotation',
 
 411               department_id = $form->{department_id},
 
 412               employee_id = $form->{employee_id},
 
 413               cp_id = $form->{contact_id}
 
 414               WHERE id = $form->{id}|;
 
 415   $dbh->do($query) || $form->dberror($query);
 
 417   $form->{ordtotal} = $amount;
 
 419   if ($form->{webdav}) {
 
 420     &webdav_folder($myconfig, $form);
 
 424   $form->{name} = $form->{ $form->{vc} };
 
 425   $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
 
 426   $form->add_shipto($dbh, $form->{id});
 
 428   # save printed, emailed, queued
 
 429   $form->save_status($dbh);
 
 431   if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
 
 432     if ($form->{vc} eq 'customer') {
 
 433       $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
 
 434                                  $form->{exchangerate}, 0);
 
 436     if ($form->{vc} eq 'vendor') {
 
 437       $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
 
 438                                  0, $form->{exchangerate});
 
 442   if ($form->{type} =~ /_order$/) {
 
 445     &adj_onhand($dbh, $form, $ml * -1);
 
 446     &adj_inventory($dbh, $myconfig, $form);
 
 449   my $rc = $dbh->commit;
 
 452   $main::lxdebug->leave_sub();
 
 458   $main::lxdebug->enter_sub();
 
 460   my ($self, $myconfig, $form, $spool) = @_;
 
 462   # connect to database
 
 463   my $dbh = $form->dbconnect_noauto($myconfig);
 
 466   my $query = qq|SELECT s.spoolfile FROM status s
 
 467                  WHERE s.trans_id = $form->{id}|;
 
 468   $sth = $dbh->prepare($query);
 
 469   $sth->execute || $self->dberror($query);
 
 474   while (($spoolfile) = $sth->fetchrow_array) {
 
 475     push @spoolfiles, $spoolfile;
 
 479   $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
 
 480               WHERE o.trans_id = $form->{id}|;
 
 481   $sth = $dbh->prepare($query);
 
 482   $sth->execute || $self->dberror($query);
 
 484   while (my ($id, $ship) = $sth->fetchrow_array) {
 
 485     $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
 
 490   $query = qq|DELETE FROM inventory
 
 491               WHERE oe_id = $form->{id}|;
 
 492   $dbh->do($query) || $form->dberror($query);
 
 494   # delete status entries
 
 495   $query = qq|DELETE FROM status
 
 496               WHERE trans_id = $form->{id}|;
 
 497   $dbh->do($query) || $form->dberror($query);
 
 500   $query = qq|DELETE FROM oe
 
 501               WHERE id = $form->{id}|;
 
 502   $dbh->do($query) || $form->dberror($query);
 
 504   # delete individual entries
 
 505   $query = qq|DELETE FROM orderitems
 
 506               WHERE trans_id = $form->{id}|;
 
 507   $dbh->do($query) || $form->dberror($query);
 
 509   $query = qq|DELETE FROM shipto
 
 510               WHERE trans_id = $form->{id}|;
 
 511   $dbh->do($query) || $form->dberror($query);
 
 513   my $rc = $dbh->commit;
 
 517     foreach $spoolfile (@spoolfiles) {
 
 518       unlink "$spool/$spoolfile" if $spoolfile;
 
 522   $main::lxdebug->leave_sub();
 
 528   $main::lxdebug->enter_sub();
 
 530   my ($self, $myconfig, $form) = @_;
 
 532   # connect to database
 
 533   my $dbh = $form->dbconnect_noauto($myconfig);
 
 539     # get default accounts and last order number
 
 540     $query = qq|SELECT (SELECT c.accno FROM chart c
 
 541                         WHERE d.inventory_accno_id = c.id) AS inventory_accno,
 
 542                        (SELECT c.accno FROM chart c
 
 543                         WHERE d.income_accno_id = c.id) AS income_accno,
 
 544                        (SELECT c.accno FROM chart c
 
 545                         WHERE d.expense_accno_id = c.id) AS expense_accno,
 
 546                        (SELECT c.accno FROM chart c
 
 547                         WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
 548                        (SELECT c.accno FROM chart c
 
 549                         WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
 
 553     $query = qq|SELECT (SELECT c.accno FROM chart c
 
 554                         WHERE d.inventory_accno_id = c.id) AS inventory_accno,
 
 555                        (SELECT c.accno FROM chart c
 
 556                         WHERE d.income_accno_id = c.id) AS income_accno,
 
 557                        (SELECT c.accno FROM chart c
 
 558                         WHERE d.expense_accno_id = c.id) AS expense_accno,
 
 559                        (SELECT c.accno FROM chart c
 
 560                         WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
 561                        (SELECT c.accno FROM chart c
 
 562                         WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
 
 563                 d.curr AS currencies,
 
 564                 current_date AS transdate, current_date AS reqdate
 
 567   my $sth = $dbh->prepare($query);
 
 568   $sth->execute || $form->dberror($query);
 
 570   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 571   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 574   ($form->{currency}) = split /:/, $form->{currencies};
 
 579     $query = qq|SELECT o.cp_id,o.ordnumber, o.transdate, o.reqdate,
 
 580                 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
 
 581                 o.curr AS currency, e.name AS employee, o.employee_id,
 
 582                 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
 
 583                 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
 
 584                 d.description AS department
 
 586                 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
 
 587                 LEFT JOIN employee e ON (o.employee_id = e.id)
 
 588                 LEFT JOIN department d ON (o.department_id = d.id)
 
 589                 WHERE o.id = $form->{id}|;
 
 590     $sth = $dbh->prepare($query);
 
 591     $sth->execute || $form->dberror($query);
 
 593     $ref = $sth->fetchrow_hashref(NAME_lc);
 
 594     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 597     $query = qq|SELECT s.* FROM shipto s
 
 598                 WHERE s.trans_id = $form->{id}|;
 
 599     $sth = $dbh->prepare($query);
 
 600     $sth->execute || $form->dberror($query);
 
 602     $ref = $sth->fetchrow_hashref(NAME_lc);
 
 603     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 606     # get printed, emailed and queued
 
 607     $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
 
 609                 WHERE s.trans_id = $form->{id}|;
 
 610     $sth = $dbh->prepare($query);
 
 611     $sth->execute || $form->dberror($query);
 
 613     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 614       $form->{printed} .= "$ref->{formname} " if $ref->{printed};
 
 615       $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
 
 616       $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
 
 617         if $ref->{spoolfile};
 
 620     map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
 
 622     my %oid = ('Pg'     => 'oid',
 
 623                'Oracle' => 'rowid');
 
 625     # retrieve individual items
 
 626     $query = qq|SELECT o.id AS orderitems_id,
 
 627                 c1.accno AS inventory_accno,
 
 628                 c2.accno AS income_accno,
 
 629                 c3.accno AS expense_accno,
 
 630                 p.partnumber, p.assembly, o.description, o.qty,
 
 631                 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes,
 
 632                 o.reqdate, o.project_id, o.serialnumber, o.ship,
 
 636                 JOIN parts p ON (o.parts_id = p.id)
 
 637                 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
 
 638                 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
 
 639                 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
 
 640                 LEFT JOIN project pr ON (o.project_id = pr.id)
 
 641                 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
 642                 WHERE o.trans_id = $form->{id}
 
 643                 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
 
 644     $sth = $dbh->prepare($query);
 
 645     $sth->execute || $form->dberror($query);
 
 647     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 649       #set expense_accno=inventory_accno if they are different => bilanz
 
 651         ($ref->{expense_accno} != $ref->{inventory_accno})
 
 652         ? $ref->{inventory_accno}
 
 653         : $ref->{expense_accno};
 
 655       # get tax rates and description
 
 657         ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
 
 658       $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
 
 660                  WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
 
 662       $stw = $dbh->prepare($query);
 
 663       $stw->execute || $form->dberror($query);
 
 664       $ref->{taxaccounts} = "";
 
 665       while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
 
 667         #    if ($customertax{$ref->{accno}}) {
 
 668         $ref->{taxaccounts} .= "$ptr->{accno} ";
 
 669         if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
 
 670           $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
 
 671           $form->{"$ptr->{accno}_description"} = $ptr->{description};
 
 672           $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
 
 673           $form->{taxaccounts} .= "$ptr->{accno} ";
 
 678       chop $ref->{taxaccounts};
 
 679       push @{ $form->{form_details} }, $ref;
 
 687     $form->lastname_used($dbh, $myconfig, $form->{vc})
 
 688       unless $form->{"$form->{vc}_id"};
 
 692   $form->{exchangerate} =
 
 693     $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
 
 694                             ($form->{vc} eq 'customer') ? "buy" : "sell");
 
 696   if ($form->{webdav}) {
 
 697     &webdav_folder($myconfig, $form);
 
 700   my $rc = $dbh->commit;
 
 703   $main::lxdebug->leave_sub();
 
 709   $main::lxdebug->enter_sub();
 
 711   my ($self, $myconfig, $form) = @_;
 
 713   # connect to database
 
 714   my $dbh = $form->dbconnect($myconfig);
 
 722   my %oid = ('Pg'     => 'oid',
 
 723              'Oracle' => 'rowid');
 
 725   # sort items by partsgroup
 
 726   for $i (1 .. $form->{rowcount}) {
 
 728     if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
 
 729       $form->format_string("partsgroup_$i");
 
 730       $partsgroup = $form->{"partsgroup_$i"};
 
 732     push @partsgroup, [$i, $partsgroup];
 
 735   # if there is a warehouse limit picking
 
 736   if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
 
 738     # run query to check for inventory
 
 739     $query = qq|SELECT sum(i.qty) AS qty
 
 742                 AND i.warehouse_id = ?|;
 
 743     $sth = $dbh->prepare($query) || $form->dberror($query);
 
 745     for $i (1 .. $form->{rowcount}) {
 
 746       $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
 
 748       ($qty) = $sth->fetchrow_array;
 
 751       $form->{"qty_$i"} = 0 if $qty == 0;
 
 753       if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
 
 754         $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
 
 760   foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
 
 763     if ($item->[1] ne $sameitem) {
 
 764       push(@{ $form->{description} }, qq|$item->[1]|);
 
 765       $sameitem = $item->[1];
 
 767       map { push(@{ $form->{$_} }, "") }
 
 768         qw(runningnumber number qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount linetotal);
 
 771     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
 
 773     if ($form->{"qty_$i"} != 0) {
 
 775       # add number, description and qty to $form->{number}, ....
 
 776       push(@{ $form->{runningnumber} }, $i);
 
 777       push(@{ $form->{number} },        qq|$form->{"partnumber_$i"}|);
 
 778       push(@{ $form->{description} },   qq|$form->{"description_$i"}|);
 
 779       push(@{ $form->{qty} },
 
 780            $form->format_amount($myconfig, $form->{"qty_$i"}));
 
 781       push(@{ $form->{ship} },
 
 782            $form->format_amount($myconfig, $form->{"ship_$i"}));
 
 783       push(@{ $form->{unit} },         qq|$form->{"unit_$i"}|);
 
 784       push(@{ $form->{bin} },          qq|$form->{"bin_$i"}|);
 
 785       push(@{ $form->{"partnotes"} },  qq|$form->{"partnotes_$i"}|);
 
 786       push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
 
 787       push(@{ $form->{reqdate} },      qq|$form->{"reqdate_$i"}|);
 
 789       push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
 
 791       push(@{ $form->{listprice} }, $form->{"listprice_$i"});
 
 793       my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
 
 794       my ($dec) = ($sellprice =~ /\.(\d+)/);
 
 796       my $decimalplaces = ($dec > 2) ? $dec : 2;
 
 800                             $sellprice * $form->parse_amount($myconfig,
 
 801                                                  $form->{"discount_$i"}) / 100,
 
 804       # keep a netprice as well, (sellprice - discount)
 
 805       $form->{"netprice_$i"} = $sellprice - $discount;
 
 808         $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
 
 810       push(@{ $form->{netprice} },
 
 811            ($form->{"netprice_$i"} != 0)
 
 812            ? $form->format_amount(
 
 813                                  $myconfig, $form->{"netprice_$i"},
 
 820         ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
 
 822       $linetotal = ($linetotal != 0) ? $linetotal : " ";
 
 824       push(@{ $form->{discount} }, $discount);
 
 826       $form->{ordtotal} += $linetotal;
 
 828       push(@{ $form->{linetotal} },
 
 829            $form->format_amount($myconfig, $linetotal, 2));
 
 831       my ($taxamount, $taxbase);
 
 834       map { $taxrate += $form->{"${_}_rate"} } split / /,
 
 835         $form->{"taxaccounts_$i"};
 
 837       if ($form->{taxincluded}) {
 
 840         $taxamount = $linetotal * $taxrate / (1 + $taxrate);
 
 841         $taxbase = $linetotal / (1 + $taxrate);
 
 843         $taxamount = $linetotal * $taxrate;
 
 844         $taxbase   = $linetotal;
 
 847       if ($taxamount != 0) {
 
 848         foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
 
 849           $taxaccounts{$item} +=
 
 850             $taxamount * $form->{"${item}_rate"} / $taxrate;
 
 851           $taxbase{$item} += $taxbase;
 
 855       if ($form->{"assembly_$i"}) {
 
 858         # get parts and push them onto the stack
 
 860         if ($form->{groupitems}) {
 
 862             qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
 
 864           $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
 
 867         $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
 
 870                     JOIN parts p ON (a.parts_id = p.id)
 
 871                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
 873                     AND a.id = '$form->{"id_$i"}'
 
 875         $sth = $dbh->prepare($query);
 
 876         $sth->execute || $form->dberror($query);
 
 878         while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 879           if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
 
 880             map { push(@{ $form->{$_} }, "") }
 
 881               qw(runningnumber ship bin serialnumber number unit bin qty reqdate sellprice listprice netprice discount linetotal);
 
 882             $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
 
 883             push(@{ $form->{description} }, $sameitem);
 
 886           push(@{ $form->{description} },
 
 887                $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
 
 889                  . qq|, $ref->{partnumber}, $ref->{description}|);
 
 891           map { push(@{ $form->{$_} }, "") }
 
 892             qw(number unit qty runningnumber ship bin serialnumber reqdate sellprice listprice netprice discount linetotal);
 
 902   foreach $item (sort keys %taxaccounts) {
 
 903     if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
 
 904       push(@{ $form->{taxbase} },
 
 905            $form->format_amount($myconfig, $taxbase{$item}, 2));
 
 907       $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
 
 909       push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
 
 910       push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
 
 911       push(@{ $form->{taxrate} },
 
 912            $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
 
 913       push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
 
 917   $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
 
 919     ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
 
 922   $form->{quototal} = $form->{ordtotal} =
 
 923     $form->format_amount($myconfig, $form->{ordtotal}, 2);
 
 926   map { $form->{$_} = $myconfig->{$_} }
 
 927     (qw(company address tel fax signature businessnumber));
 
 928   $form->{username} = $myconfig->{name};
 
 932   $main::lxdebug->leave_sub();
 
 935 sub project_description {
 
 936   $main::lxdebug->enter_sub();
 
 938   my ($self, $dbh, $id) = @_;
 
 940   my $query = qq|SELECT p.description
 
 943   my $sth = $dbh->prepare($query);
 
 944   $sth->execute || $form->dberror($query);
 
 946   ($_) = $sth->fetchrow_array;
 
 950   $main::lxdebug->leave_sub();
 
 956   $main::lxdebug->enter_sub();
 
 958   my ($self, $myconfig, $form) = @_;
 
 960   my $dbh = $form->dbconnect($myconfig);
 
 963   my $query = qq|SELECT id, description
 
 966   my $sth = $dbh->prepare($query);
 
 967   $sth->execute || $form->dberror($query);
 
 969   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 970     push @{ $form->{all_warehouses} }, $ref;
 
 976   $main::lxdebug->leave_sub();
 
 980   $main::lxdebug->enter_sub();
 
 982   my ($self, $myconfig, $form) = @_;
 
 984   my ($null, $warehouse_id) = split /--/, $form->{warehouse};
 
 988   ($null, $employee_id) = split /--/, $form->{employee};
 
 990   my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
 
 992   my $dbh = $form->dbconnect_noauto($myconfig);
 
 998   $query = qq|SELECT o.serialnumber, o.ship
 
1000               WHERE o.trans_id = ?
 
1003   $sth = $dbh->prepare($query) || $form->dberror($query);
 
1005   $query = qq|SELECT sum(i.qty)
 
1007               WHERE i.parts_id = ?
 
1008               AND i.warehouse_id = ?|;
 
1009   $wth = $dbh->prepare($query) || $form->dberror($query);
 
1011   for my $i (1 .. $form->{rowcount} - 1) {
 
1014       (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
 
1016       : $form->{"ship_$i"};
 
1018     if ($warehouse_id && $form->{type} eq 'ship_order') {
 
1020       $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
 
1022       ($qty) = $wth->fetchrow_array;
 
1033       $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
 
1034                   qty, oe_id, orderitems_id, shippingdate, employee_id)
 
1035                   VALUES ($form->{"id_$i"}, $warehouse_id,
 
1036                   $ship, $form->{"id"},
 
1037                   $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
 
1039       $dbh->do($query) || $form->dberror($query);
 
1041       # add serialnumber, ship to orderitems
 
1042       $sth->execute($form->{id}, $form->{"orderitems_id_$i"})
 
1044       ($serialnumber, $ship) = $sth->fetchrow_array;
 
1047       $serialnumber .= " " if $serialnumber;
 
1048       $serialnumber .= qq|$form->{"serialnumber_$i"}|;
 
1049       $ship += $form->{"ship_$i"};
 
1051       $query = qq|UPDATE orderitems SET
 
1052                   serialnumber = '$serialnumber',
 
1054                   WHERE trans_id = $form->{id}
 
1055                   AND id = $form->{"orderitems_id_$i"}|;
 
1056       $dbh->do($query) || $form->dberror($query);
 
1058       # update order with ship via
 
1059       $query = qq|UPDATE oe SET
 
1060                   shippingpoint = '$form->{shippingpoint}',
 
1061                   shipvia = '$form->{shipvia}'
 
1062                   WHERE id = $form->{id}|;
 
1063       $dbh->do($query) || $form->dberror($query);
 
1065       # update onhand for parts
 
1066       $form->update_balance($dbh, "parts", "onhand",
 
1067                             qq|id = $form->{"id_$i"}|,
 
1068                             $form->{"ship_$i"} * $ml);
 
1073   my $rc = $dbh->commit;
 
1076   $main::lxdebug->leave_sub();
 
1082   $main::lxdebug->enter_sub();
 
1084   my ($dbh, $form, $ml) = @_;
 
1086   my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
 
1088                  JOIN parts p ON (p.id = oi.parts_id)
 
1089                  WHERE oi.trans_id = $form->{id}|;
 
1090   my $sth = $dbh->prepare($query);
 
1091   $sth->execute || $form->dberror($query);
 
1093   $query = qq|SELECT sum(p.inventory_accno_id)
 
1095               JOIN assembly a ON (a.parts_id = p.id)
 
1097   my $ath = $dbh->prepare($query) || $form->dberror($query);
 
1101   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1103     if ($ref->{inventory_accno_id} || $ref->{assembly}) {
 
1105       # do not update if assembly consists of all services
 
1106       if ($ref->{assembly}) {
 
1107         $ath->execute($ref->{parts_id}) || $form->dberror($query);
 
1109         ($ispa) = $sth->fetchrow_array;
 
1116       # adjust onhand in parts table
 
1117       $form->update_balance($dbh, "parts", "onhand",
 
1118                             qq|id = $ref->{parts_id}|,
 
1119                             $ref->{ship} * $ml);
 
1125   $main::lxdebug->leave_sub();
 
1129   $main::lxdebug->enter_sub();
 
1131   my ($dbh, $myconfig, $form) = @_;
 
1133   my %oid = ('Pg'     => 'oid',
 
1134              'Oracle' => 'rowid');
 
1136   # increase/reduce qty in inventory table
 
1137   my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
 
1139                  WHERE oi.trans_id = $form->{id}|;
 
1140   my $sth = $dbh->prepare($query);
 
1141   $sth->execute || $form->dberror($query);
 
1143   $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
 
1144                      (SELECT SUM(qty) FROM inventory
 
1145                       WHERE oe_id = $form->{id}
 
1146                       AND orderitems_id = ?) AS total
 
1148               WHERE oe_id = $form->{id}
 
1149               AND orderitems_id = ?|;
 
1150   my $ith = $dbh->prepare($query) || $form->dberror($query);
 
1153   my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
 
1155   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1157     $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
 
1159     while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
 
1161       if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
 
1162         $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
 
1164         $form->update_balance($dbh, "inventory", "qty",
 
1165                               qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
 
1174   # delete inventory entries if qty = 0
 
1175   $query = qq|DELETE FROM inventory
 
1176               WHERE oe_id = $form->{id}
 
1178   $dbh->do($query) || $form->dberror($query);
 
1180   $main::lxdebug->leave_sub();
 
1184   $main::lxdebug->enter_sub();
 
1186   my ($self, $myconfig, $form) = @_;
 
1188   my ($null, $warehouse_id) = split /--/, $form->{warehouse};
 
1191   my $dbh = $form->dbconnect($myconfig);
 
1193   my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
 
1196                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1197                  WHERE p.onhand > 0|;
 
1199   if ($form->{partnumber}) {
 
1200     $var = $form->like(lc $form->{partnumber});
 
1202                  AND lower(p.partnumber) LIKE '$var'";
 
1204   if ($form->{description}) {
 
1205     $var = $form->like(lc $form->{description});
 
1207                  AND lower(p.description) LIKE '$var'";
 
1209   if ($form->{partsgroup}) {
 
1210     $var = $form->like(lc $form->{partsgroup});
 
1212                  AND lower(pg.partsgroup) LIKE '$var'";
 
1215   $sth = $dbh->prepare($query);
 
1216   $sth->execute || $form->dberror($query);
 
1218   $query = qq|SELECT sum(i.qty), w.description, w.id
 
1220               LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
 
1221               WHERE i.parts_id = ?
 
1222               AND NOT i.warehouse_id = $warehouse_id
 
1223               GROUP BY w.description, w.id|;
 
1224   $wth = $dbh->prepare($query) || $form->dberror($query);
 
1226   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1228     $wth->execute($ref->{id}) || $form->dberror;
 
1230     while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
 
1231       push @{ $form->{all_inventory} },
 
1232         { 'id'           => $ref->{id},
 
1233           'partnumber'   => $ref->{partnumber},
 
1234           'description'  => $ref->{description},
 
1235           'partsgroup'   => $ref->{partsgroup},
 
1237           'warehouse_id' => $warehouse_id,
 
1238           'warehouse'    => $warehouse }
 
1248   @{ $form->{all_inventory} } =
 
1249     sort { $a->{ $form->{sort} } cmp $b->{ $form->{sort} } }
 
1250     @{ $form->{all_inventory} };
 
1252   $main::lxdebug->leave_sub();
 
1254   return @{ $form->{all_inventory} };
 
1258   $main::lxdebug->enter_sub();
 
1260   my ($self, $myconfig, $form) = @_;
 
1262   my $dbh = $form->dbconnect_noauto($myconfig);
 
1264   my $query = qq|INSERT INTO inventory
 
1265                  (warehouse_id, parts_id, qty, shippingdate, employee_id)
 
1266                  VALUES (?, ?, ?, ?, ?)|;
 
1267   $sth = $dbh->prepare($query) || $form->dberror($query);
 
1269   $form->get_employee($dbh);
 
1274   $shippingdate = "$a[5]-$a[4]-$a[3]";
 
1276   for my $i (1 .. $form->{rowcount}) {
 
1277     $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
 
1279     $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
 
1284       $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty,
 
1285                     $shippingdate, $form->{employee_id})
 
1291       $sth->execute($form->{"warehouse_id_$i"},
 
1292                     $form->{"id_$i"}, $qty * -1, $shippingdate,
 
1293                     $form->{employee_id})
 
1300   my $rc = $dbh->commit;
 
1303   $main::lxdebug->leave_sub();
 
1309   $main::lxdebug->enter_sub();
 
1311   my ($myconfig, $form) = @_;
 
1314     $path = "webdav/angebote/" . $form->{quonumber}, last SWITCH
 
1315       if ($form->{type} eq "sales_quotation");
 
1316     $path = "webdav/bestellungen/" . $form->{ordnumber}, last SWITCH
 
1317       if ($form->{type} eq "sales_order");
 
1318     $path = "webdav/anfragen/" . $form->{quonumber}, last SWITCH
 
1319       if ($form->{type} eq "request_quotation");
 
1320     $path = "webdav/lieferantenbestellungen/" . $form->{ordnumber}, last SWITCH
 
1321       if ($form->{type} eq "purchase_order");
 
1325     mkdir($path, 0770) or die "can't make directory $!\n";
 
1329       foreach $file (@files) {
 
1330         $file =~ /\/([^\/]*)$/;
 
1332         $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
 
1334         $link  = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
 
1335         $form->{WEBDAV}{$fname} = $link;
 
1340   $main::lxdebug->leave_sub();