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       # get pricegroup_id and save ist
 
 315       ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_drag_$i"};
 
 318       # save detail record in orderitems table
 
 319       $query = qq|INSERT INTO orderitems (|;
 
 320       $query .= "id, " if $form->{"orderitems_id_$i"};
 
 321       $query .= qq|trans_id, parts_id, description, qty, sellprice, discount,
 
 322                    unit, reqdate, project_id, serialnumber, ship, pricegroup_id)
 
 324       $query .= qq|$form->{"orderitems_id_$i"},|
 
 325         if $form->{"orderitems_id_$i"};
 
 326       $query .= qq|$form->{id}, $form->{"id_$i"},
 
 327                    '$form->{"description_$i"}', $form->{"qty_$i"},
 
 328                    $fxsellprice, $form->{"discount_$i"},
 
 329                    '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
 
 330                    '$form->{"serialnumber_$i"}', $form->{"ship_$i"},
 
 332       $dbh->do($query) || $form->dberror($query);
 
 334       $form->{"sellprice_$i"} = $fxsellprice;
 
 335       $form->{"discount_$i"} *= 100;
 
 339   # set values which could be empty
 
 340   map { $form->{$_} *= 1 }
 
 341     qw(vendor_id customer_id taxincluded closed quotation);
 
 343   $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
 
 347   map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
 
 349   $amount = $form->round_amount($netamount + $tax, 2);
 
 350   $netamount = $form->round_amount($netamount, 2);
 
 352   if ($form->{currency} eq $form->{defaultcurrency}) {
 
 353     $form->{exchangerate} = 1;
 
 356       $form->check_exchangerate($myconfig,
 
 359                                 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
 
 362   $form->{exchangerate} =
 
 365     : $form->parse_amount($myconfig, $form->{exchangerate});
 
 369   # fill in subject if there is none
 
 370   if ($form->{type} =~ /_order$/) {
 
 372     $form->{subject} = qq|$form->{label} $form->{ordnumber}|
 
 373       unless $form->{subject};
 
 376     $form->{subject} = qq|$form->{label} $form->{quonumber}|
 
 377       unless $form->{subject};
 
 380   # if there is a message stuff it into the intnotes
 
 381   my $cc  = "Cc: $form->{cc}\\r\n"   if $form->{cc};
 
 382   my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
 
 383   my $now = scalar localtime;
 
 384   $form->{intnotes} .= qq|\r
 
 385 \r| if $form->{intnotes};
 
 387   $form->{intnotes} .= qq|[email]\r
 
 390 $cc${bcc}Subject: $form->{subject}\r
 
 392 Message: $form->{message}\r| if $form->{message};
 
 394   ($null, $form->{department_id}) = split(/--/, $form->{department});
 
 395   $form->{department_id} *= 1;
 
 398   $query = qq|UPDATE oe set
 
 399               ordnumber = '$form->{ordnumber}',
 
 400               quonumber = '$form->{quonumber}',
 
 401               cusordnumber = '$form->{cusordnumber}',
 
 402               transdate = '$form->{transdate}',
 
 403               vendor_id = $form->{vendor_id},
 
 404               customer_id = $form->{customer_id},
 
 406               netamount = $netamount,
 
 408               taxincluded = '$form->{taxincluded}',
 
 409               shippingpoint = '$form->{shippingpoint}',
 
 410               shipvia = '$form->{shipvia}',
 
 411               notes = '$form->{notes}',
 
 412               intnotes = '$form->{intnotes}',
 
 413               curr = '$form->{currency}',
 
 414               closed = '$form->{closed}',
 
 415               quotation = '$quotation',
 
 416               department_id = $form->{department_id},
 
 417               employee_id = $form->{employee_id},
 
 418               cp_id = $form->{contact_id}
 
 419               WHERE id = $form->{id}|;
 
 420   $dbh->do($query) || $form->dberror($query);
 
 422   $form->{ordtotal} = $amount;
 
 424   if ($form->{webdav}) {
 
 425     &webdav_folder($myconfig, $form);
 
 429   $form->{name} = $form->{ $form->{vc} };
 
 430   $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
 
 431   $form->add_shipto($dbh, $form->{id});
 
 433   # save printed, emailed, queued
 
 434   $form->save_status($dbh);
 
 436   if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
 
 437     if ($form->{vc} eq 'customer') {
 
 438       $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
 
 439                                  $form->{exchangerate}, 0);
 
 441     if ($form->{vc} eq 'vendor') {
 
 442       $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
 
 443                                  0, $form->{exchangerate});
 
 447   if ($form->{type} =~ /_order$/) {
 
 450     &adj_onhand($dbh, $form, $ml * -1);
 
 451     &adj_inventory($dbh, $myconfig, $form);
 
 454   my $rc = $dbh->commit;
 
 457   $main::lxdebug->leave_sub();
 
 463   $main::lxdebug->enter_sub();
 
 465   my ($self, $myconfig, $form, $spool) = @_;
 
 467   # connect to database
 
 468   my $dbh = $form->dbconnect_noauto($myconfig);
 
 471   my $query = qq|SELECT s.spoolfile FROM status s
 
 472                  WHERE s.trans_id = $form->{id}|;
 
 473   $sth = $dbh->prepare($query);
 
 474   $sth->execute || $self->dberror($query);
 
 479   while (($spoolfile) = $sth->fetchrow_array) {
 
 480     push @spoolfiles, $spoolfile;
 
 484   $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
 
 485               WHERE o.trans_id = $form->{id}|;
 
 486   $sth = $dbh->prepare($query);
 
 487   $sth->execute || $self->dberror($query);
 
 489   while (my ($id, $ship) = $sth->fetchrow_array) {
 
 490     $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
 
 495   $query = qq|DELETE FROM inventory
 
 496               WHERE oe_id = $form->{id}|;
 
 497   $dbh->do($query) || $form->dberror($query);
 
 499   # delete status entries
 
 500   $query = qq|DELETE FROM status
 
 501               WHERE trans_id = $form->{id}|;
 
 502   $dbh->do($query) || $form->dberror($query);
 
 505   $query = qq|DELETE FROM oe
 
 506               WHERE id = $form->{id}|;
 
 507   $dbh->do($query) || $form->dberror($query);
 
 509   # delete individual entries
 
 510   $query = qq|DELETE FROM orderitems
 
 511               WHERE trans_id = $form->{id}|;
 
 512   $dbh->do($query) || $form->dberror($query);
 
 514   $query = qq|DELETE FROM shipto
 
 515               WHERE trans_id = $form->{id}|;
 
 516   $dbh->do($query) || $form->dberror($query);
 
 518   my $rc = $dbh->commit;
 
 522     foreach $spoolfile (@spoolfiles) {
 
 523       unlink "$spool/$spoolfile" if $spoolfile;
 
 527   $main::lxdebug->leave_sub();
 
 533   $main::lxdebug->enter_sub();
 
 535   my ($self, $myconfig, $form) = @_;
 
 537   # connect to database
 
 538   my $dbh = $form->dbconnect_noauto($myconfig);
 
 544     # get default accounts and last order number
 
 545     $query = qq|SELECT (SELECT c.accno FROM chart c
 
 546                         WHERE d.inventory_accno_id = c.id) AS inventory_accno,
 
 547                        (SELECT c.accno FROM chart c
 
 548                         WHERE d.income_accno_id = c.id) AS income_accno,
 
 549                        (SELECT c.accno FROM chart c
 
 550                         WHERE d.expense_accno_id = c.id) AS expense_accno,
 
 551                        (SELECT c.accno FROM chart c
 
 552                         WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
 553                        (SELECT c.accno FROM chart c
 
 554                         WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
 
 558     $query = qq|SELECT (SELECT c.accno FROM chart c
 
 559                         WHERE d.inventory_accno_id = c.id) AS inventory_accno,
 
 560                        (SELECT c.accno FROM chart c
 
 561                         WHERE d.income_accno_id = c.id) AS income_accno,
 
 562                        (SELECT c.accno FROM chart c
 
 563                         WHERE d.expense_accno_id = c.id) AS expense_accno,
 
 564                        (SELECT c.accno FROM chart c
 
 565                         WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
 566                        (SELECT c.accno FROM chart c
 
 567                         WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
 
 568                 d.curr AS currencies,
 
 569                 current_date AS transdate, current_date AS reqdate
 
 572   my $sth = $dbh->prepare($query);
 
 573   $sth->execute || $form->dberror($query);
 
 575   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 576   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 579   ($form->{currency}) = split /:/, $form->{currencies};
 
 584     $query = qq|SELECT o.cp_id,o.ordnumber, o.transdate, o.reqdate,
 
 585                 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
 
 586                 o.curr AS currency, e.name AS employee, o.employee_id,
 
 587                 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
 
 588                 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
 
 589                 d.description AS department
 
 591                 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
 
 592                 LEFT JOIN employee e ON (o.employee_id = e.id)
 
 593                 LEFT JOIN department d ON (o.department_id = d.id)
 
 594                 WHERE o.id = $form->{id}|;
 
 595     $sth = $dbh->prepare($query);
 
 596     $sth->execute || $form->dberror($query);
 
 598     $ref = $sth->fetchrow_hashref(NAME_lc);
 
 599     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 602     $query = qq|SELECT s.* FROM shipto s
 
 603                 WHERE s.trans_id = $form->{id}|;
 
 604     $sth = $dbh->prepare($query);
 
 605     $sth->execute || $form->dberror($query);
 
 607     $ref = $sth->fetchrow_hashref(NAME_lc);
 
 608     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 611     # get printed, emailed and queued
 
 612     $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
 
 614                 WHERE s.trans_id = $form->{id}|;
 
 615     $sth = $dbh->prepare($query);
 
 616     $sth->execute || $form->dberror($query);
 
 618     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 619       $form->{printed} .= "$ref->{formname} " if $ref->{printed};
 
 620       $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
 
 621       $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
 
 622         if $ref->{spoolfile};
 
 625     map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
 
 627     my %oid = ('Pg'     => 'oid',
 
 628                'Oracle' => 'rowid');
 
 630     # retrieve individual items
 
 631     $query = qq|SELECT o.id AS orderitems_id,
 
 632                 c1.accno AS inventory_accno,
 
 633                 c2.accno AS income_accno,
 
 634                 c3.accno AS expense_accno,
 
 635                 p.partnumber, p.assembly, o.description, o.qty,
 
 636                 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes,
 
 637                 o.reqdate, o.project_id, o.serialnumber, o.ship,
 
 639                 pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
 
 641                 JOIN parts p ON (o.parts_id = p.id)
 
 642                 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
 
 643                 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
 
 644                 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
 
 645                 LEFT JOIN project pr ON (o.project_id = pr.id)
 
 646                 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
 647                 WHERE o.trans_id = $form->{id}
 
 648                 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
 
 649     $sth = $dbh->prepare($query);
 
 650     $sth->execute || $form->dberror($query);
 
 652     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 654       #set expense_accno=inventory_accno if they are different => bilanz
 
 656         ($ref->{expense_accno} != $ref->{inventory_accno})
 
 657         ? $ref->{inventory_accno}
 
 658         : $ref->{expense_accno};
 
 660       # get tax rates and description
 
 662         ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
 
 663       $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
 
 665                  WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
 
 667       $stw = $dbh->prepare($query);
 
 668       $stw->execute || $form->dberror($query);
 
 669       $ref->{taxaccounts} = "";
 
 670       while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
 
 672         #    if ($customertax{$ref->{accno}}) {
 
 673         $ref->{taxaccounts} .= "$ptr->{accno} ";
 
 674         if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
 
 675           $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
 
 676           $form->{"$ptr->{accno}_description"} = $ptr->{description};
 
 677           $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
 
 678           $form->{taxaccounts} .= "$ptr->{accno} ";
 
 683       chop $ref->{taxaccounts};
 
 684       push @{ $form->{form_details} }, $ref;
 
 692     $form->lastname_used($dbh, $myconfig, $form->{vc})
 
 693       unless $form->{"$form->{vc}_id"};
 
 697   $form->{exchangerate} =
 
 698     $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
 
 699                             ($form->{vc} eq 'customer') ? "buy" : "sell");
 
 701   if ($form->{webdav}) {
 
 702     &webdav_folder($myconfig, $form);
 
 705   my $rc = $dbh->commit;
 
 708   $main::lxdebug->leave_sub();
 
 714   $main::lxdebug->enter_sub();
 
 716   my ($self, $myconfig, $form) = @_;
 
 718   # connect to database
 
 719   my $dbh = $form->dbconnect($myconfig);
 
 727   my %oid = ('Pg'     => 'oid',
 
 728              'Oracle' => 'rowid');
 
 730   # sort items by partsgroup
 
 731   for $i (1 .. $form->{rowcount}) {
 
 733     if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
 
 734       $form->format_string("partsgroup_$i");
 
 735       $partsgroup = $form->{"partsgroup_$i"};
 
 737     push @partsgroup, [$i, $partsgroup];
 
 740   # if there is a warehouse limit picking
 
 741   if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
 
 743     # run query to check for inventory
 
 744     $query = qq|SELECT sum(i.qty) AS qty
 
 747                 AND i.warehouse_id = ?|;
 
 748     $sth = $dbh->prepare($query) || $form->dberror($query);
 
 750     for $i (1 .. $form->{rowcount}) {
 
 751       $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
 
 753       ($qty) = $sth->fetchrow_array;
 
 756       $form->{"qty_$i"} = 0 if $qty == 0;
 
 758       if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
 
 759         $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
 
 765   foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
 
 768     if ($item->[1] ne $sameitem) {
 
 769       push(@{ $form->{description} }, qq|$item->[1]|);
 
 770       $sameitem = $item->[1];
 
 772       map { push(@{ $form->{$_} }, "") }
 
 773         qw(runningnumber number qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount linetotal);
 
 776     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
 
 778     if ($form->{"qty_$i"} != 0) {
 
 780       # add number, description and qty to $form->{number}, ....
 
 781       push(@{ $form->{runningnumber} }, $i);
 
 782       push(@{ $form->{number} },        qq|$form->{"partnumber_$i"}|);
 
 783       push(@{ $form->{description} },   qq|$form->{"description_$i"}|);
 
 784       push(@{ $form->{qty} },
 
 785            $form->format_amount($myconfig, $form->{"qty_$i"}));
 
 786       push(@{ $form->{ship} },
 
 787            $form->format_amount($myconfig, $form->{"ship_$i"}));
 
 788       push(@{ $form->{unit} },         qq|$form->{"unit_$i"}|);
 
 789       push(@{ $form->{bin} },          qq|$form->{"bin_$i"}|);
 
 790       push(@{ $form->{"partnotes"} },  qq|$form->{"partnotes_$i"}|);
 
 791       push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
 
 792       push(@{ $form->{reqdate} },      qq|$form->{"reqdate_$i"}|);
 
 794       push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
 
 796       push(@{ $form->{listprice} }, $form->{"listprice_$i"});
 
 798       my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
 
 799       my ($dec) = ($sellprice =~ /\.(\d+)/);
 
 801       my $decimalplaces = ($dec > 2) ? $dec : 2;
 
 805                             $sellprice * $form->parse_amount($myconfig,
 
 806                                                  $form->{"discount_$i"}) / 100,
 
 809       # keep a netprice as well, (sellprice - discount)
 
 810       $form->{"netprice_$i"} = $sellprice - $discount;
 
 813         $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
 
 815       push(@{ $form->{netprice} },
 
 816            ($form->{"netprice_$i"} != 0)
 
 817            ? $form->format_amount(
 
 818                                  $myconfig, $form->{"netprice_$i"},
 
 825         ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
 
 827       $linetotal = ($linetotal != 0) ? $linetotal : " ";
 
 829       push(@{ $form->{discount} }, $discount);
 
 831       $form->{ordtotal} += $linetotal;
 
 833       push(@{ $form->{linetotal} },
 
 834            $form->format_amount($myconfig, $linetotal, 2));
 
 836       my ($taxamount, $taxbase);
 
 839       map { $taxrate += $form->{"${_}_rate"} } split / /,
 
 840         $form->{"taxaccounts_$i"};
 
 842       if ($form->{taxincluded}) {
 
 845         $taxamount = $linetotal * $taxrate / (1 + $taxrate);
 
 846         $taxbase = $linetotal / (1 + $taxrate);
 
 848         $taxamount = $linetotal * $taxrate;
 
 849         $taxbase   = $linetotal;
 
 852       if ($taxamount != 0) {
 
 853         foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
 
 854           $taxaccounts{$item} +=
 
 855             $taxamount * $form->{"${item}_rate"} / $taxrate;
 
 856           $taxbase{$item} += $taxbase;
 
 860       if ($form->{"assembly_$i"}) {
 
 863         # get parts and push them onto the stack
 
 865         if ($form->{groupitems}) {
 
 867             qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
 
 869           $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
 
 872         $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
 
 875                     JOIN parts p ON (a.parts_id = p.id)
 
 876                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
 878                     AND a.id = '$form->{"id_$i"}'
 
 880         $sth = $dbh->prepare($query);
 
 881         $sth->execute || $form->dberror($query);
 
 883         while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 884           if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
 
 885             map { push(@{ $form->{$_} }, "") }
 
 886               qw(runningnumber ship bin serialnumber number unit bin qty reqdate sellprice listprice netprice discount linetotal);
 
 887             $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
 
 888             push(@{ $form->{description} }, $sameitem);
 
 891           push(@{ $form->{description} },
 
 892                $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
 
 894                  . qq|, $ref->{partnumber}, $ref->{description}|);
 
 896           map { push(@{ $form->{$_} }, "") }
 
 897             qw(number unit qty runningnumber ship bin serialnumber reqdate sellprice listprice netprice discount linetotal);
 
 907   foreach $item (sort keys %taxaccounts) {
 
 908     if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
 
 909       push(@{ $form->{taxbase} },
 
 910            $form->format_amount($myconfig, $taxbase{$item}, 2));
 
 912       $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
 
 914       push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
 
 915       push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
 
 916       push(@{ $form->{taxrate} },
 
 917            $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
 
 918       push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
 
 922   $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
 
 924     ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
 
 927   $form->{quototal} = $form->{ordtotal} =
 
 928     $form->format_amount($myconfig, $form->{ordtotal}, 2);
 
 931   map { $form->{$_} = $myconfig->{$_} }
 
 932     (qw(company address tel fax signature businessnumber));
 
 933   $form->{username} = $myconfig->{name};
 
 937   $main::lxdebug->leave_sub();
 
 940 sub project_description {
 
 941   $main::lxdebug->enter_sub();
 
 943   my ($self, $dbh, $id) = @_;
 
 945   my $query = qq|SELECT p.description
 
 948   my $sth = $dbh->prepare($query);
 
 949   $sth->execute || $form->dberror($query);
 
 951   ($_) = $sth->fetchrow_array;
 
 955   $main::lxdebug->leave_sub();
 
 961   $main::lxdebug->enter_sub();
 
 963   my ($self, $myconfig, $form) = @_;
 
 965   my $dbh = $form->dbconnect($myconfig);
 
 968   my $query = qq|SELECT id, description
 
 971   my $sth = $dbh->prepare($query);
 
 972   $sth->execute || $form->dberror($query);
 
 974   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 975     push @{ $form->{all_warehouses} }, $ref;
 
 981   $main::lxdebug->leave_sub();
 
 985   $main::lxdebug->enter_sub();
 
 987   my ($self, $myconfig, $form) = @_;
 
 989   my ($null, $warehouse_id) = split /--/, $form->{warehouse};
 
 993   ($null, $employee_id) = split /--/, $form->{employee};
 
 995   my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
 
 997   my $dbh = $form->dbconnect_noauto($myconfig);
 
1003   $query = qq|SELECT o.serialnumber, o.ship
 
1005               WHERE o.trans_id = ?
 
1008   $sth = $dbh->prepare($query) || $form->dberror($query);
 
1010   $query = qq|SELECT sum(i.qty)
 
1012               WHERE i.parts_id = ?
 
1013               AND i.warehouse_id = ?|;
 
1014   $wth = $dbh->prepare($query) || $form->dberror($query);
 
1016   for my $i (1 .. $form->{rowcount} - 1) {
 
1019       (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
 
1021       : $form->{"ship_$i"};
 
1023     if ($warehouse_id && $form->{type} eq 'ship_order') {
 
1025       $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
 
1027       ($qty) = $wth->fetchrow_array;
 
1038       $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
 
1039                   qty, oe_id, orderitems_id, shippingdate, employee_id)
 
1040                   VALUES ($form->{"id_$i"}, $warehouse_id,
 
1041                   $ship, $form->{"id"},
 
1042                   $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
 
1044       $dbh->do($query) || $form->dberror($query);
 
1046       # add serialnumber, ship to orderitems
 
1047       $sth->execute($form->{id}, $form->{"orderitems_id_$i"})
 
1049       ($serialnumber, $ship) = $sth->fetchrow_array;
 
1052       $serialnumber .= " " if $serialnumber;
 
1053       $serialnumber .= qq|$form->{"serialnumber_$i"}|;
 
1054       $ship += $form->{"ship_$i"};
 
1056       $query = qq|UPDATE orderitems SET
 
1057                   serialnumber = '$serialnumber',
 
1059                   WHERE trans_id = $form->{id}
 
1060                   AND id = $form->{"orderitems_id_$i"}|;
 
1061       $dbh->do($query) || $form->dberror($query);
 
1063       # update order with ship via
 
1064       $query = qq|UPDATE oe SET
 
1065                   shippingpoint = '$form->{shippingpoint}',
 
1066                   shipvia = '$form->{shipvia}'
 
1067                   WHERE id = $form->{id}|;
 
1068       $dbh->do($query) || $form->dberror($query);
 
1070       # update onhand for parts
 
1071       $form->update_balance($dbh, "parts", "onhand",
 
1072                             qq|id = $form->{"id_$i"}|,
 
1073                             $form->{"ship_$i"} * $ml);
 
1078   my $rc = $dbh->commit;
 
1081   $main::lxdebug->leave_sub();
 
1087   $main::lxdebug->enter_sub();
 
1089   my ($dbh, $form, $ml) = @_;
 
1091   my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
 
1093                  JOIN parts p ON (p.id = oi.parts_id)
 
1094                  WHERE oi.trans_id = $form->{id}|;
 
1095   my $sth = $dbh->prepare($query);
 
1096   $sth->execute || $form->dberror($query);
 
1098   $query = qq|SELECT sum(p.inventory_accno_id)
 
1100               JOIN assembly a ON (a.parts_id = p.id)
 
1102   my $ath = $dbh->prepare($query) || $form->dberror($query);
 
1106   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1108     if ($ref->{inventory_accno_id} || $ref->{assembly}) {
 
1110       # do not update if assembly consists of all services
 
1111       if ($ref->{assembly}) {
 
1112         $ath->execute($ref->{parts_id}) || $form->dberror($query);
 
1114         ($ispa) = $sth->fetchrow_array;
 
1121       # adjust onhand in parts table
 
1122       $form->update_balance($dbh, "parts", "onhand",
 
1123                             qq|id = $ref->{parts_id}|,
 
1124                             $ref->{ship} * $ml);
 
1130   $main::lxdebug->leave_sub();
 
1134   $main::lxdebug->enter_sub();
 
1136   my ($dbh, $myconfig, $form) = @_;
 
1138   my %oid = ('Pg'     => 'oid',
 
1139              'Oracle' => 'rowid');
 
1141   # increase/reduce qty in inventory table
 
1142   my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
 
1144                  WHERE oi.trans_id = $form->{id}|;
 
1145   my $sth = $dbh->prepare($query);
 
1146   $sth->execute || $form->dberror($query);
 
1148   $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
 
1149                      (SELECT SUM(qty) FROM inventory
 
1150                       WHERE oe_id = $form->{id}
 
1151                       AND orderitems_id = ?) AS total
 
1153               WHERE oe_id = $form->{id}
 
1154               AND orderitems_id = ?|;
 
1155   my $ith = $dbh->prepare($query) || $form->dberror($query);
 
1158   my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
 
1160   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1162     $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
 
1164     while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
 
1166       if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
 
1167         $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
 
1169         $form->update_balance($dbh, "inventory", "qty",
 
1170                               qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
 
1179   # delete inventory entries if qty = 0
 
1180   $query = qq|DELETE FROM inventory
 
1181               WHERE oe_id = $form->{id}
 
1183   $dbh->do($query) || $form->dberror($query);
 
1185   $main::lxdebug->leave_sub();
 
1189   $main::lxdebug->enter_sub();
 
1191   my ($self, $myconfig, $form) = @_;
 
1193   my ($null, $warehouse_id) = split /--/, $form->{warehouse};
 
1196   my $dbh = $form->dbconnect($myconfig);
 
1198   my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
 
1201                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1202                  WHERE p.onhand > 0|;
 
1204   if ($form->{partnumber}) {
 
1205     $var = $form->like(lc $form->{partnumber});
 
1207                  AND lower(p.partnumber) LIKE '$var'";
 
1209   if ($form->{description}) {
 
1210     $var = $form->like(lc $form->{description});
 
1212                  AND lower(p.description) LIKE '$var'";
 
1214   if ($form->{partsgroup}) {
 
1215     $var = $form->like(lc $form->{partsgroup});
 
1217                  AND lower(pg.partsgroup) LIKE '$var'";
 
1220   $sth = $dbh->prepare($query);
 
1221   $sth->execute || $form->dberror($query);
 
1223   $query = qq|SELECT sum(i.qty), w.description, w.id
 
1225               LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
 
1226               WHERE i.parts_id = ?
 
1227               AND NOT i.warehouse_id = $warehouse_id
 
1228               GROUP BY w.description, w.id|;
 
1229   $wth = $dbh->prepare($query) || $form->dberror($query);
 
1231   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1233     $wth->execute($ref->{id}) || $form->dberror;
 
1235     while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
 
1236       push @{ $form->{all_inventory} },
 
1237         { 'id'           => $ref->{id},
 
1238           'partnumber'   => $ref->{partnumber},
 
1239           'description'  => $ref->{description},
 
1240           'partsgroup'   => $ref->{partsgroup},
 
1242           'warehouse_id' => $warehouse_id,
 
1243           'warehouse'    => $warehouse }
 
1253   @{ $form->{all_inventory} } =
 
1254     sort { $a->{ $form->{sort} } cmp $b->{ $form->{sort} } }
 
1255     @{ $form->{all_inventory} };
 
1257   $main::lxdebug->leave_sub();
 
1259   return @{ $form->{all_inventory} };
 
1263   $main::lxdebug->enter_sub();
 
1265   my ($self, $myconfig, $form) = @_;
 
1267   my $dbh = $form->dbconnect_noauto($myconfig);
 
1269   my $query = qq|INSERT INTO inventory
 
1270                  (warehouse_id, parts_id, qty, shippingdate, employee_id)
 
1271                  VALUES (?, ?, ?, ?, ?)|;
 
1272   $sth = $dbh->prepare($query) || $form->dberror($query);
 
1274   $form->get_employee($dbh);
 
1279   $shippingdate = "$a[5]-$a[4]-$a[3]";
 
1281   for my $i (1 .. $form->{rowcount}) {
 
1282     $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
 
1284     $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
 
1289       $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty,
 
1290                     $shippingdate, $form->{employee_id})
 
1296       $sth->execute($form->{"warehouse_id_$i"},
 
1297                     $form->{"id_$i"}, $qty * -1, $shippingdate,
 
1298                     $form->{employee_id})
 
1305   my $rc = $dbh->commit;
 
1308   $main::lxdebug->leave_sub();
 
1314   $main::lxdebug->enter_sub();
 
1316   my ($myconfig, $form) = @_;
 
1319     $path = "webdav/angebote/" . $form->{quonumber}, last SWITCH
 
1320       if ($form->{type} eq "sales_quotation");
 
1321     $path = "webdav/bestellungen/" . $form->{ordnumber}, last SWITCH
 
1322       if ($form->{type} eq "sales_order");
 
1323     $path = "webdav/anfragen/" . $form->{quonumber}, last SWITCH
 
1324       if ($form->{type} eq "request_quotation");
 
1325     $path = "webdav/lieferantenbestellungen/" . $form->{ordnumber}, last SWITCH
 
1326       if ($form->{type} eq "purchase_order");
 
1330     mkdir($path, 0770) or die "can't make directory $!\n";
 
1334       foreach $file (@files) {
 
1335         $file =~ /\/([^\/]*)$/;
 
1337         $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
 
1339         $link  = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
 
1340         $form->{WEBDAV}{$fname} = $link;
 
1345   $main::lxdebug->leave_sub();