1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  17 # This program is free software; you can redistribute it and/or modify
 
  18 # it under the terms of the GNU General Public License as published by
 
  19 # the Free Software Foundation; either version 2 of the License, or
 
  20 # (at your option) any later version.
 
  22 # This program is distributed in the hope that it will be useful,
 
  23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  25 # GNU General Public License for more details.
 
  26 # You should have received a copy of the GNU General Public License
 
  27 # along with this program; if not, write to the Free Software
 
  28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  29 #======================================================================
 
  31 # Inventory Control backend
 
  33 #======================================================================
 
  39   $main::lxdebug->enter_sub();
 
  41   my ($self, $myconfig, $form) = @_;
 
  44   my $dbh = $form->dbconnect($myconfig);
 
  46   my $query = qq|SELECT p.*,
 
  47                  c1.accno AS inventory_accno,
 
  48                  c2.accno AS income_accno,
 
  49                  c3.accno AS expense_accno,
 
  52                  LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
 
  53                  LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
 
  54                  LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
 
  55                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
  56                  WHERE p.id = $form->{id}|;
 
  57   my $sth = $dbh->prepare($query);
 
  58   $sth->execute || $form->dberror($query);
 
  59   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
  61   # copy to $form variables
 
  62   map { $form->{$_} = $ref->{$_} } (keys %{$ref});
 
  66   my %oid = ('Pg'     => 'a.oid',
 
  67              'Oracle' => 'a.rowid');
 
  69   # part or service item
 
  70   $form->{item} = ($form->{inventory_accno}) ? 'part' : 'service';
 
  71   if ($form->{assembly}) {
 
  72     $form->{item} = 'assembly';
 
  74     # retrieve assembly items
 
  75     $query = qq|SELECT p.id, p.partnumber, p.description,
 
  76                 p.sellprice, p.weight, a.qty, a.bom, p.unit,
 
  79                 JOIN assembly a ON (a.parts_id = p.id)
 
  80                 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
  81                 WHERE a.id = $form->{id}
 
  82                 ORDER BY $oid{$myconfig->{dbdriver}}|;
 
  84     $sth = $dbh->prepare($query);
 
  85     $sth->execute || $form->dberror($query);
 
  87     $form->{assembly_rows} = 0;
 
  88     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
  89       $form->{assembly_rows}++;
 
  90       foreach my $key (keys %{$ref}) {
 
  91         $form->{"${key}_$form->{assembly_rows}"} = $ref->{$key};
 
  98   # setup accno hash for <option checked> {amount} is used in create_links
 
  99   $form->{amount}{IC}         = $form->{inventory_accno};
 
 100   $form->{amount}{IC_income}  = $form->{income_accno};
 
 101   $form->{amount}{IC_sale}    = $form->{income_accno};
 
 102   $form->{amount}{IC_expense} = $form->{expense_accno};
 
 103   $form->{amount}{IC_cogs}    = $form->{expense_accno};
 
 107     qq|SELECT p.parts_id, p.pricegroup_id, p.price, (SELECT pg.pricegroup FROM pricegroup pg WHERE pg.id=p.pricegroup_id) AS pricegroup FROM prices p
 
 108               WHERE parts_id = $form->{id}
 
 109               ORDER by pricegroup|;
 
 111   $sth = $dbh->prepare($query);
 
 112   $sth->execute || $form->dberror($query);
 
 115   @pricegroups_not_used = ();
 
 120          ($form->{"klass_$i"}, $form->{"pricegroup_id_$i"},
 
 121           $form->{"price_$i"}, $form->{"pricegroup_$i"})
 
 122          = $sth->fetchrow_array
 
 124     $form->{"price_$i"} = $form->round_amount($form->{"price_$i"}, 5);
 
 125     $form->{"price_$i"} =
 
 126       $form->format_amount($myconfig, $form->{"price_$i"}, 5);
 
 127     push @pricegroups, $form->{"pricegroup_id_$i"};
 
 134   $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
 
 136   $pkq = $dbh->prepare($query);
 
 137   $pkq->execute || $form->dberror($query);
 
 138   while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
 
 139     push @{ $form->{PRICEGROUPS} }, $pkr;
 
 143   #find not used pricegroups
 
 144   while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
 
 146     foreach $item (@pricegroups) {
 
 147       if ($item eq $tmp->{id}) {
 
 154       push @pricegroups_not_used, $tmp;
 
 158   # if not used pricegroups are avaible
 
 159   if (@pricegroups_not_used) {
 
 161     foreach $name (@pricegroups_not_used) {
 
 162       $form->{"klass_$i"} = "$name->{id}";
 
 163       $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
 
 164       $form->{"price_$i"} =
 
 165         $form->format_amount($myconfig, $form->{"price_$i"}, 5);
 
 166       $form->{"pricegroup_id_$i"} = "$name->{id}";
 
 167       $form->{"pricegroup_$i"}    = "$name->{pricegroup}";
 
 173   $form->{price_rows} = $i - 1;
 
 175   unless ($form->{item} eq 'service') {
 
 178     if ($form->{makemodel}) {
 
 179       $query = qq|SELECT m.make, m.model FROM makemodel m
 
 180                   WHERE m.parts_id = $form->{id}|;
 
 182       $sth = $dbh->prepare($query);
 
 183       $sth->execute || $form->dberror($query);
 
 186       while (($form->{"make_$i"}, $form->{"model_$i"}) = $sth->fetchrow_array)
 
 191       $form->{makemodel_rows} = $i - 1;
 
 197   $form->{language_values} = "";
 
 198   $query = qq|SELECT language_id, translation FROM translation WHERE parts_id = $form->{id}|;
 
 199   $trq = $dbh->prepare($query);
 
 200   $trq->execute || $form->dberror($query);
 
 201   while ($tr = $trq->fetchrow_hashref(NAME_lc)) {
 
 202     $form->{language_values} .= "---+++---".$tr->{language_id}."--++--".$tr->{translation};
 
 206   # now get accno for taxes
 
 207   $query = qq|SELECT c.accno
 
 208               FROM chart c, partstax pt
 
 209               WHERE pt.chart_id = c.id
 
 210               AND pt.parts_id = $form->{id}|;
 
 212   $sth = $dbh->prepare($query);
 
 213   $sth->execute || $form->dberror($query);
 
 215   while (($key) = $sth->fetchrow_array) {
 
 216     $form->{amount}{$key} = $key;
 
 222   $query = qq|SELECT i.parts_id
 
 224               WHERE i.parts_id = $form->{id}
 
 228               WHERE o.parts_id = $form->{id}
 
 232               WHERE a.parts_id = $form->{id}|;
 
 233   $sth = $dbh->prepare($query);
 
 234   $sth->execute || $form->dberror($query);
 
 236   ($form->{orphaned}) = $sth->fetchrow_array;
 
 237   $form->{orphaned} = !$form->{orphaned};
 
 240   $form->{"unit_changeable"} = 1;
 
 241   foreach my $table (qw(invoice assembly orderitems inventory license)) {
 
 242     $query = "SELECT COUNT(*) FROM $table WHERE parts_id = ?";
 
 243     my ($count) = $dbh->selectrow_array($query, undef, $form->{"id"});
 
 244     $form->dberror($query . " (" . $form->{"id"} . ")") if ($dbh->err);
 
 247       $form->{"unit_changeable"} = 0;
 
 254   $main::lxdebug->leave_sub();
 
 257 sub get_pricegroups {
 
 258   $main::lxdebug->enter_sub();
 
 260   my ($self, $myconfig, $form) = @_;
 
 261   my $dbh                  = $form->dbconnect($myconfig);
 
 263   my @pricegroups_not_used = ();
 
 266   my $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
 
 268   my $pkq = $dbh->prepare($query);
 
 269   $pkq->execute || $form->dberror($query);
 
 270   while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
 
 271     push @{ $form->{PRICEGROUPS} }, $pkr;
 
 275   #find not used pricegroups
 
 276   while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
 
 277     push @pricegroups_not_used, $tmp;
 
 280   # if not used pricegroups are avaible
 
 281   if (@pricegroups_not_used) {
 
 283     foreach $name (@pricegroups_not_used) {
 
 284       $form->{"klass_$i"} = "$name->{id}";
 
 285       $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
 
 286       $form->{"price_$i"} =
 
 287         $form->format_amount($myconfig, $form->{"price_$i"}, 5);
 
 288       $form->{"pricegroup_id_$i"} = "$name->{id}";
 
 289       $form->{"pricegroup_$i"}    = "$name->{pricegroup}";
 
 295   $form->{price_rows} = $i - 1;
 
 299   $main::lxdebug->leave_sub();
 
 302 sub retrieve_buchungsgruppen {
 
 303   $main::lxdebug->enter_sub();
 
 305   my ($self, $myconfig, $form) = @_;
 
 309   my $dbh = $form->dbconnect($myconfig);
 
 311   # get buchungsgruppen
 
 312   $query = qq|SELECT id, description
 
 313               FROM buchungsgruppen|;
 
 314   $sth = $dbh->prepare($query);
 
 315   $sth->execute || $form->dberror($query);
 
 317   $form->{BUCHUNGSGRUPPEN} = [];
 
 318   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 319     push(@{ $form->{BUCHUNGSGRUPPEN} }, $ref);
 
 323   $main::lxdebug->leave_sub();
 
 327   $main::lxdebug->enter_sub();
 
 329   my ($self, $myconfig, $form) = @_;
 
 330   $form->{IC_expense} = "1000";
 
 331   $form->{IC_income} = "2000";
 
 333   if ($form->{item} ne 'service') {
 
 334     $form->{IC} = $form->{IC_expense};
 
 337   ($form->{inventory_accno}) = split(/--/, $form->{IC});
 
 338   ($form->{expense_accno})   = split(/--/, $form->{IC_expense});
 
 339   ($form->{income_accno})    = split(/--/, $form->{IC_income});
 
 341   # connect to database, turn off AutoCommit
 
 342   my $dbh = $form->dbconnect_noauto($myconfig);
 
 345   # make up a unique handle and store in partnumber field
 
 346   # then retrieve the record based on the unique handle to get the id
 
 347   # replace the partnumber field with the actual variable
 
 348   # add records for makemodel
 
 350   # if there is a $form->{id} then replace the old entry
 
 351   # delete all makemodel entries and add the new ones
 
 354   map { $form->{$_} =~ s/\'/\'\'/g } qw(partnumber description notes unit);
 
 356   # undo amount formatting
 
 357   map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) }
 
 358     qw(rop weight listprice sellprice gv lastcost stock);
 
 360   # set date to NULL if nothing entered
 
 361   $form->{priceupdate} =
 
 362     ($form->{priceupdate}) ? qq|'$form->{priceupdate}'| : "NULL";
 
 364   $form->{makemodel} = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
 
 366   $form->{alternate} = 0;
 
 367   $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
 
 368   $form->{obsolete} *= 1;
 
 370   $form->{onhand}   *= 1;
 
 373   $form->{buchungsgruppen_id}       *= 1;
 
 374   $form->{not_discountable}       *= 1;
 
 375   $form->{payment_id}       *= 1;
 
 382     $query = qq|SELECT p.sellprice, p.weight
 
 384                 WHERE p.id = $form->{id}|;
 
 385     $sth = $dbh->prepare($query);
 
 386     $sth->execute || $form->dberror($query);
 
 387     my ($sellprice, $weight) = $sth->fetchrow_array;
 
 390     # if item is part of an assembly adjust all assemblies
 
 391     $query = qq|SELECT a.id, a.qty
 
 393                 WHERE a.parts_id = $form->{id}|;
 
 394     $sth = $dbh->prepare($query);
 
 395     $sth->execute || $form->dberror($query);
 
 396     while (my ($id, $qty) = $sth->fetchrow_array) {
 
 397       &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
 
 401     if ($form->{item} ne 'service') {
 
 403       # delete makemodel records
 
 404       $query = qq|DELETE FROM makemodel
 
 405                   WHERE parts_id = $form->{id}|;
 
 406       $dbh->do($query) || $form->dberror($query);
 
 409     if ($form->{item} eq 'assembly') {
 
 410       if ($form->{onhand} != 0) {
 
 411         &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
 
 414       # delete assembly records
 
 415       $query = qq|DELETE FROM assembly
 
 416                   WHERE id = $form->{id}|;
 
 417       $dbh->do($query) || $form->dberror($query);
 
 419       $form->{onhand} += $form->{stock};
 
 423     $query = qq|DELETE FROM partstax
 
 424                 WHERE parts_id = $form->{id}|;
 
 425     $dbh->do($query) || $form->dberror($query);
 
 427     # delete translations
 
 428     $query = qq|DELETE FROM translation
 
 429                 WHERE parts_id = $form->{id}|;
 
 430     $dbh->do($query) || $form->dberror($query);
 
 433     my $uid = rand() . time;
 
 434     $uid .= $form->{login};
 
 436     $query = qq|SELECT p.id FROM parts p
 
 437                 WHERE p.partnumber = '$form->{partnumber}'|;
 
 438     $sth = $dbh->prepare($query);
 
 439     $sth->execute || $form->dberror($query);
 
 440     ($form->{id}) = $sth->fetchrow_array;
 
 443     if ($form->{id} ne "") {
 
 444       $main::lxdebug->leave_sub();
 
 447     $query = qq|INSERT INTO parts (partnumber, description)
 
 448                 VALUES ('$uid', 'dummy')|;
 
 449     $dbh->do($query) || $form->dberror($query);
 
 451     $query = qq|SELECT p.id FROM parts p
 
 452                 WHERE p.partnumber = '$uid'|;
 
 453     $sth = $dbh->prepare($query);
 
 454     $sth->execute || $form->dberror($query);
 
 456     ($form->{id}) = $sth->fetchrow_array;
 
 459     $form->{orphaned} = 1;
 
 460     $form->{onhand} = $form->{stock} if $form->{item} eq 'assembly';
 
 461     if ($form->{partnumber} eq "" && $form->{inventory_accno} eq "") {
 
 462       $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
 
 464     if ($form->{partnumber} eq "" && $form->{inventory_accno} ne "") {
 
 465       $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
 
 469   my $partsgroup_id = 0;
 
 471   if ($form->{partsgroup}) {
 
 472     ($partsgroup, $partsgroup_id) = split /--/, $form->{partsgroup};
 
 475   $query = qq|UPDATE parts SET
 
 476               partnumber = '$form->{partnumber}',
 
 477               description = '$form->{description}',
 
 478               makemodel = '$form->{makemodel}',
 
 479               alternate = '$form->{alternate}',
 
 480               assembly = '$form->{assembly}',
 
 481               listprice = $form->{listprice},
 
 482               sellprice = $form->{sellprice},
 
 483               lastcost = $form->{lastcost},
 
 484               weight = $form->{weight},
 
 485               priceupdate = $form->{priceupdate},
 
 486               unit = '$form->{unit}',
 
 487               notes = '$form->{notes}',
 
 488               formel = '$form->{formel}',
 
 490               bin = '$form->{bin}',
 
 491               buchungsgruppen_id = '$form->{buchungsgruppen_id}',
 
 492               payment_id = '$form->{payment_id}',
 
 493               inventory_accno_id = (SELECT c.id FROM chart c
 
 494                                     WHERE c.accno = '$form->{inventory_accno}'),
 
 495               income_accno_id = (SELECT c.id FROM chart c
 
 496                                  WHERE c.accno = '$form->{income_accno}'),
 
 497               expense_accno_id = (SELECT c.id FROM chart c
 
 498                                   WHERE c.accno = '$form->{expense_accno}'),
 
 499               obsolete = '$form->{obsolete}',
 
 500               image = '$form->{image}',
 
 501               drawing = '$form->{drawing}',
 
 502               shop = '$form->{shop}',
 
 505               not_discountable = '$form->{not_discountable}',
 
 506               microfiche = '$form->{microfiche}',
 
 507               partsgroup_id = $partsgroup_id
 
 508               WHERE id = $form->{id}|;
 
 509   $dbh->do($query) || $form->dberror($query);
 
 511   # delete translation records
 
 512   $query = qq|DELETE FROM translation
 
 513               WHERE parts_id = $form->{id}|;
 
 514   $dbh->do($query) || $form->dberror($query);
 
 516   if ($form->{language_values} ne "") {
 
 517     split /---\+\+\+---/,$form->{language_values};
 
 519       my ($language_id, $translation, $longdescription) = split /--\+\+--/, $item;
 
 520       if ($translation ne "") {
 
 521         $query = qq|INSERT into translation (parts_id, language_id, translation, longdescription) VALUES
 
 522                     ($form->{id}, $language_id, | . $dbh->quote($translation) . qq|, | . $dbh->quote($longdescription) . qq| )|;
 
 523         $dbh->do($query) || $form->dberror($query);
 
 527   # delete price records
 
 528   $query = qq|DELETE FROM prices
 
 529               WHERE parts_id = $form->{id}|;
 
 530   $dbh->do($query) || $form->dberror($query);
 
 531   # insert price records only if different to sellprice
 
 532   for my $i (1 .. $form->{price_rows}) {
 
 533     if ($form->{"price_$i"} eq "0") {
 
 534       $form->{"price_$i"} = $form->{sellprice};
 
 537         (   $form->{"price_$i"}
 
 538          || $form->{"klass_$i"}
 
 539          || $form->{"pricegroup_id_$i"})
 
 540         and $form->{"price_$i"} != $form->{sellprice}
 
 542       $klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
 
 543       $price = $form->parse_amount($myconfig, $form->{"price_$i"});
 
 545         $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
 
 546       $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price)
 
 547                   VALUES($form->{id},$pricegroup_id,$price)|;
 
 548       $dbh->do($query) || $form->dberror($query);
 
 552   # insert makemodel records
 
 553   unless ($form->{item} eq 'service') {
 
 554     for my $i (1 .. $form->{makemodel_rows}) {
 
 555       if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
 
 556         map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
 
 558         $query = qq|INSERT INTO makemodel (parts_id, make, model)
 
 560                     '$form->{"make_$i"}', '$form->{"model_$i"}')|;
 
 561         $dbh->do($query) || $form->dberror($query);
 
 567   foreach $item (split / /, $form->{taxaccounts}) {
 
 568     if ($form->{"IC_tax_$item"}) {
 
 569       $query = qq|INSERT INTO partstax (parts_id, chart_id)
 
 573                            WHERE c.accno = '$item'))|;
 
 574       $dbh->do($query) || $form->dberror($query);
 
 578   # add assembly records
 
 579   if ($form->{item} eq 'assembly') {
 
 581     for my $i (1 .. $form->{assembly_rows}) {
 
 582       $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
 
 584       if ($form->{"qty_$i"} != 0) {
 
 585         $form->{"bom_$i"} *= 1;
 
 586         $query = qq|INSERT INTO assembly (id, parts_id, qty, bom)
 
 587                     VALUES ($form->{id}, $form->{"id_$i"},
 
 588                     $form->{"qty_$i"}, '$form->{"bom_$i"}')|;
 
 589         $dbh->do($query) || $form->dberror($query);
 
 593     # adjust onhand for the parts
 
 594     if ($form->{onhand} != 0) {
 
 595       &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
 
 601     my $shippingdate = "$a[5]-$a[4]-$a[3]";
 
 603     $form->get_employee($dbh);
 
 605     # add inventory record
 
 606     $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
 
 607                 shippingdate, employee_id) VALUES (
 
 608                 0, $form->{id}, $form->{stock}, '$shippingdate',
 
 609                 $form->{employee_id})|;
 
 610     $dbh->do($query) || $form->dberror($query);
 
 614   #set expense_accno=inventory_accno if they are different => bilanz
 
 616     ($form->{expense_accno} != $form->{inventory_accno})
 
 617     ? $form->{inventory_accno}
 
 618     : $form->{expense_accno};
 
 620   # get tax rates and description
 
 622     ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
 
 623   $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
 
 625               WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
 
 627   $stw = $dbh->prepare($query);
 
 629   $stw->execute || $form->dberror($query);
 
 631   $form->{taxaccount} = "";
 
 632   while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
 
 634     #    if ($customertax{$ref->{accno}}) {
 
 635     $form->{taxaccount} .= "$ptr->{accno} ";
 
 636     if (!($form->{taxaccount2} =~ /$ptr->{accno}/)) {
 
 637       $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
 
 638       $form->{"$ptr->{accno}_description"} = $ptr->{description};
 
 639       $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
 
 640       $form->{taxaccount2} .= " $ptr->{accno} ";
 
 646   my $rc = $dbh->commit;
 
 649   $main::lxdebug->leave_sub();
 
 654 sub update_assembly {
 
 655   $main::lxdebug->enter_sub();
 
 657   my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
 
 659   my $query = qq|SELECT a.id, a.qty
 
 661                  WHERE a.parts_id = $id|;
 
 662   my $sth = $dbh->prepare($query);
 
 663   $sth->execute || $form->dberror($query);
 
 665   while (my ($pid, $aqty) = $sth->fetchrow_array) {
 
 666     &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
 
 670   $query = qq|UPDATE parts
 
 671               SET sellprice = sellprice +
 
 672                   $qty * ($form->{sellprice} - $sellprice),
 
 674                   $qty * ($form->{weight} - $weight)
 
 676   $dbh->do($query) || $form->dberror($query);
 
 678   $main::lxdebug->leave_sub();
 
 681 sub retrieve_assemblies {
 
 682   $main::lxdebug->enter_sub();
 
 684   my ($self, $myconfig, $form) = @_;
 
 686   # connect to database
 
 687   my $dbh = $form->dbconnect($myconfig);
 
 691   if ($form->{partnumber}) {
 
 692     my $partnumber = $form->like(lc $form->{partnumber});
 
 693     $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
 
 696   if ($form->{description}) {
 
 697     my $description = $form->like(lc $form->{description});
 
 698     $where .= " AND lower(p.description) LIKE '$description'";
 
 700   $where .= " AND NOT p.obsolete = '1'";
 
 702   # retrieve assembly items
 
 703   my $query = qq|SELECT p.id, p.partnumber, p.description,
 
 704                  p.bin, p.onhand, p.rop,
 
 705                    (SELECT sum(p2.inventory_accno_id)
 
 706                     FROM parts p2, assembly a
 
 707                     WHERE p2.id = a.parts_id
 
 708                     AND a.id = p.id) AS inventory
 
 713   my $sth = $dbh->prepare($query);
 
 714   $sth->execute || $form->dberror($query);
 
 716   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 717     push @{ $form->{assembly_items} }, $ref if $ref->{inventory};
 
 723   $main::lxdebug->leave_sub();
 
 726 sub restock_assemblies {
 
 727   $main::lxdebug->enter_sub();
 
 729   my ($self, $myconfig, $form) = @_;
 
 731   # connect to database
 
 732   my $dbh = $form->dbconnect_noauto($myconfig);
 
 734   for my $i (1 .. $form->{rowcount}) {
 
 736     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
 
 738     if ($form->{"qty_$i"} != 0) {
 
 739       &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
 
 744   my $rc = $dbh->commit;
 
 747   $main::lxdebug->leave_sub();
 
 752 sub adjust_inventory {
 
 753   $main::lxdebug->enter_sub();
 
 755   my ($dbh, $form, $id, $qty) = @_;
 
 757   my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
 
 758                  FROM parts p, assembly a
 
 759                  WHERE a.parts_id = p.id
 
 761   my $sth = $dbh->prepare($query);
 
 762   $sth->execute || $form->dberror($query);
 
 764   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 766     my $allocate = $qty * $ref->{qty};
 
 768     # is it a service item, then loop
 
 769     $ref->{inventory_accno_id} *= 1;
 
 770     next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
 
 772     # adjust parts onhand
 
 773     $form->update_balance($dbh, "parts", "onhand",
 
 781   my $rc = $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $qty);
 
 783   $main::lxdebug->leave_sub();
 
 789   $main::lxdebug->enter_sub();
 
 791   my ($self, $myconfig, $form) = @_;
 
 793   # connect to database, turn off AutoCommit
 
 794   my $dbh = $form->dbconnect_noauto($myconfig);
 
 796   # first delete prices of pricegroup 
 
 797   my $query = qq|DELETE FROM prices
 
 798            WHERE parts_id = $form->{id}|;
 
 799   $dbh->do($query) || $form->dberror($query);
 
 801   my $query = qq|DELETE FROM parts
 
 802                  WHERE id = $form->{id}|;
 
 803   $dbh->do($query) || $form->dberror($query);
 
 805   $query = qq|DELETE FROM partstax
 
 806               WHERE parts_id = $form->{id}|;
 
 807   $dbh->do($query) || $form->dberror($query);
 
 809   # check if it is a part, assembly or service
 
 810   if ($form->{item} ne 'service') {
 
 811     $query = qq|DELETE FROM makemodel
 
 812                 WHERE parts_id = $form->{id}|;
 
 813     $dbh->do($query) || $form->dberror($query);
 
 816   if ($form->{item} eq 'assembly') {
 
 819     $query = qq|DELETE FROM inventory
 
 820                 WHERE parts_id = $form->{id}|;
 
 821     $dbh->do($query) || $form->dberror($query);
 
 823     $query = qq|DELETE FROM assembly
 
 824                 WHERE id = $form->{id}|;
 
 825     $dbh->do($query) || $form->dberror($query);
 
 828   if ($form->{item} eq 'alternate') {
 
 829     $query = qq|DELETE FROM alternate
 
 830                 WHERE id = $form->{id}|;
 
 831     $dbh->do($query) || $form->dberror($query);
 
 835   my $rc = $dbh->commit;
 
 838   $main::lxdebug->leave_sub();
 
 844   $main::lxdebug->enter_sub();
 
 846   my ($self, $myconfig, $form) = @_;
 
 848   my $i = $form->{assembly_rows};
 
 852   if ($form->{"partnumber_$i"}) {
 
 853     $var = $form->like(lc $form->{"partnumber_$i"});
 
 854     $where .= " AND lower(p.partnumber) LIKE '$var'";
 
 856   if ($form->{"description_$i"}) {
 
 857     $var = $form->like(lc $form->{"description_$i"});
 
 858     $where .= " AND lower(p.description) LIKE '$var'";
 
 860   if ($form->{"partsgroup_$i"}) {
 
 861     $var = $form->like(lc $form->{"partsgroup_$i"});
 
 862     $where .= " AND lower(pg.partsgroup) LIKE '$var'";
 
 866     $where .= " AND NOT p.id = $form->{id}";
 
 870     $where .= " ORDER BY p.partnumber";
 
 872     $where .= " ORDER BY p.description";
 
 875   # connect to database
 
 876   my $dbh = $form->dbconnect($myconfig);
 
 878   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
 
 879                  p.weight, p.onhand, p.unit,
 
 882                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
 884   my $sth = $dbh->prepare($query);
 
 885   $sth->execute || $form->dberror($query);
 
 887   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 888     push @{ $form->{item_list} }, $ref;
 
 894   $main::lxdebug->leave_sub();
 
 898   $main::lxdebug->enter_sub();
 
 900   my ($self, $myconfig, $form) = @_;
 
 908   foreach my $item (qw(partnumber drawing microfiche)) {
 
 909     if ($form->{$item}) {
 
 910       $var = $form->like(lc $form->{$item});
 
 911       $where .= " AND lower(p.$item) LIKE '$var'";
 
 915   # special case for description
 
 916   if ($form->{description}) {
 
 917     unless (   $form->{bought}
 
 922             || $form->{quoted}) {
 
 923       $var = $form->like(lc $form->{description});
 
 924       $where .= " AND lower(p.description) LIKE '$var'";
 
 928   # special case for serialnumber
 
 929   if ($form->{l_serialnumber}) {
 
 930     if ($form->{serialnumber}) {
 
 931       $var = $form->like(lc $form->{serialnumber});
 
 932       $where .= " AND lower(serialnumber) LIKE '$var'";
 
 936   if ($form->{searchitems} eq 'part') {
 
 937     $where .= " AND p.inventory_accno_id > 0";
 
 939   if ($form->{searchitems} eq 'assembly') {
 
 940     $form->{bought} = "";
 
 941     $where .= " AND p.assembly = '1'";
 
 943   if ($form->{searchitems} eq 'service') {
 
 944     $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
 
 946     # irrelevant for services
 
 947     $form->{make} = $form->{model} = "";
 
 950   # items which were never bought, sold or on an order
 
 951   if ($form->{itemstatus} eq 'orphaned') {
 
 952     $form->{onhand}  = $form->{short}   = 0;
 
 953     $form->{bought}  = $form->{sold}    = 0;
 
 954     $form->{onorder} = $form->{ordered} = 0;
 
 955     $form->{rfq}     = $form->{quoted}  = 0;
 
 957     $form->{transdatefrom} = $form->{transdateto} = "";
 
 959     $where .= " AND p.onhand = 0
 
 960                 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
 
 961                                  WHERE p.id = i.parts_id)
 
 962                 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
 
 963                                  WHERE p.id = a.parts_id)
 
 964                 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
 
 965                                  WHERE p.id = o.parts_id)";
 
 968   if ($form->{itemstatus} eq 'active') {
 
 969     $where .= " AND p.obsolete = '0'";
 
 971   if ($form->{itemstatus} eq 'obsolete') {
 
 972     $where .= " AND p.obsolete = '1'";
 
 973     $form->{onhand} = $form->{short} = 0;
 
 975   if ($form->{itemstatus} eq 'onhand') {
 
 976     $where .= " AND p.onhand > 0";
 
 978   if ($form->{itemstatus} eq 'short') {
 
 979     $where .= " AND p.onhand < p.rop";
 
 982     $var = $form->like(lc $form->{make});
 
 983     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
 
 984                            FROM makemodel m WHERE lower(m.make) LIKE '$var')";
 
 986   if ($form->{model}) {
 
 987     $var = $form->like(lc $form->{model});
 
 988     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
 
 989                            FROM makemodel m WHERE lower(m.model) LIKE '$var')";
 
 991   if ($form->{partsgroup}) {
 
 992     $var = $form->like(lc $form->{partsgroup});
 
 993     $where .= " AND lower(pg.partsgroup) LIKE '$var'";
 
 995   if ($form->{l_soldtotal}) {
 
 996     $where .= " AND p.id=i.parts_id AND  i.qty >= 0";
 
 998       " GROUP BY  p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
 
1000   if ($form->{top100}) {
 
1001     $limit = " LIMIT 100";
 
1005   if ($form->{revers} == 1) {
 
1006     $form->{desc} = " DESC";
 
1011   # connect to database
 
1012   my $dbh = $form->dbconnect($myconfig);
 
1014   my $sortorder = $form->{sort};
 
1015   $sortorder .= $form->{desc};
 
1016   $sortorder = $form->{sort} if $form->{sort};
 
1020   if ($form->{l_soldtotal}) {
 
1021     $form->{soldtotal} = 'soldtotal';
 
1023       qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
 
1024                 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
 
1025                 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
 
1031     $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
 
1032                  p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
 
1033                  p.priceupdate, p.image, p.drawing, p.microfiche,
 
1036                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1039                  ORDER BY $sortorder|;
 
1042   # rebuild query for bought and sold items
 
1043   if (   $form->{bought}
 
1048       || $form->{quoted}) {
 
1050     my @a = qw(partnumber description bin priceupdate name);
 
1052     push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
 
1053     push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
 
1054     push @a, "quonumber" if ($form->{rfq}     || $form->{quoted});
 
1059     if ($form->{bought} || $form->{sold}) {
 
1061       my $invwhere = "$where";
 
1062       $invwhere .= " AND i.assemblyitem = '0'";
 
1063       $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
 
1064         if $form->{transdatefrom};
 
1065       $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
 
1066         if $form->{transdateto};
 
1068       if ($form->{description}) {
 
1069         $var = $form->like(lc $form->{description});
 
1070         $invwhere .= " AND lower(i.description) LIKE '$var'";
 
1073       my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
 
1074                     i.qty AS onhand, i.unit, p.bin, i.sellprice,
 
1075                     p.listprice, p.lastcost, p.rop, p.weight,
 
1076                     p.priceupdate, p.image, p.drawing, p.microfiche,
 
1078                     a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
 
1079                     ct.name, i.deliverydate|;
 
1081       if ($form->{bought}) {
 
1083                     SELECT $flds, 'ir' AS module, '' AS type,
 
1086                     JOIN parts p ON (p.id = i.parts_id)
 
1087                     JOIN ap a ON (a.id = i.trans_id)
 
1088                     JOIN vendor ct ON (a.vendor_id = ct.id)
 
1089                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1095       if ($form->{sold}) {
 
1097                      SELECT $flds, 'is' AS module, '' AS type,
 
1100                      JOIN parts p ON (p.id = i.parts_id)
 
1101                      JOIN ar a ON (a.id = i.trans_id)
 
1102                      JOIN customer ct ON (a.customer_id = ct.id)
 
1103                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1110     if ($form->{onorder} || $form->{ordered}) {
 
1111       my $ordwhere = "$where
 
1112                      AND o.quotation = '0'";
 
1113       $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
 
1114         if $form->{transdatefrom};
 
1115       $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
 
1116         if $form->{transdateto};
 
1118       if ($form->{description}) {
 
1119         $var = $form->like(lc $form->{description});
 
1120         $ordwhere .= " AND lower(oi.description) LIKE '$var'";
 
1124         qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
 
1125                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
 
1126                  p.listprice, p.lastcost, p.rop, p.weight,
 
1127                  p.priceupdate, p.image, p.drawing, p.microfiche,
 
1129                  '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
 
1132       if ($form->{ordered}) {
 
1134                      SELECT $flds, 'oe' AS module, 'sales_order' AS type,
 
1135                     (SELECT buy FROM exchangerate ex
 
1136                      WHERE ex.curr = o.curr
 
1137                      AND ex.transdate = o.transdate) AS exchangerate
 
1139                      JOIN parts p ON (oi.parts_id = p.id)
 
1140                      JOIN oe o ON (oi.trans_id = o.id)
 
1141                      JOIN customer ct ON (o.customer_id = ct.id)
 
1142                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1144                      AND o.customer_id > 0|;
 
1149       if ($form->{onorder}) {
 
1151           qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
 
1152                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
 
1153                    p.listprice, p.lastcost, p.rop, p.weight,
 
1154                    p.priceupdate, p.image, p.drawing, p.microfiche,
 
1156                    '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
 
1160                     SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
 
1161                     (SELECT sell FROM exchangerate ex
 
1162                      WHERE ex.curr = o.curr
 
1163                      AND ex.transdate = o.transdate) AS exchangerate
 
1165                     JOIN parts p ON (oi.parts_id = p.id)
 
1166                     JOIN oe o ON (oi.trans_id = o.id)
 
1167                     JOIN vendor ct ON (o.vendor_id = ct.id)
 
1168                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1170                     AND o.vendor_id > 0|;
 
1175     if ($form->{rfq} || $form->{quoted}) {
 
1176       my $quowhere = "$where
 
1177                      AND o.quotation = '1'";
 
1178       $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'"
 
1179         if $form->{transdatefrom};
 
1180       $quowhere .= " AND o.transdate <= '$form->{transdateto}'"
 
1181         if $form->{transdateto};
 
1183       if ($form->{description}) {
 
1184         $var = $form->like(lc $form->{description});
 
1185         $quowhere .= " AND lower(oi.description) LIKE '$var'";
 
1189         qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
 
1190                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
 
1191                  p.listprice, p.lastcost, p.rop, p.weight,
 
1192                  p.priceupdate, p.image, p.drawing, p.microfiche,
 
1194                  '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
 
1197       if ($form->{quoted}) {
 
1199                      SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
 
1200                     (SELECT buy FROM exchangerate ex
 
1201                      WHERE ex.curr = o.curr
 
1202                      AND ex.transdate = o.transdate) AS exchangerate
 
1204                      JOIN parts p ON (oi.parts_id = p.id)
 
1205                      JOIN oe o ON (oi.trans_id = o.id)
 
1206                      JOIN customer ct ON (o.customer_id = ct.id)
 
1207                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1209                      AND o.customer_id > 0|;
 
1216           qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
 
1217                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
 
1218                    p.listprice, p.lastcost, p.rop, p.weight,
 
1219                    p.priceupdate, p.image, p.drawing, p.microfiche,
 
1221                    '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
 
1225                     SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
 
1226                     (SELECT sell FROM exchangerate ex
 
1227                      WHERE ex.curr = o.curr
 
1228                      AND ex.transdate = o.transdate) AS exchangerate
 
1230                     JOIN parts p ON (oi.parts_id = p.id)
 
1231                     JOIN oe o ON (oi.trans_id = o.id)
 
1232                     JOIN vendor ct ON (o.vendor_id = ct.id)
 
1233                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1235                     AND o.vendor_id > 0|;
 
1240                  ORDER BY $sortorder|;
 
1243   my $sth = $dbh->prepare($query);
 
1244   $sth->execute || $form->dberror($query);
 
1246   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1247     push @{ $form->{parts} }, $ref;
 
1252   # include individual items for assemblies
 
1253   if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
 
1254     foreach $item (@{ $form->{parts} }) {
 
1255       push @assemblies, $item;
 
1256       $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
 
1258                   p.sellprice, p.listprice, p.lastcost,
 
1259                   p.rop, p.weight, p.priceupdate,
 
1260                   p.image, p.drawing, p.microfiche
 
1261                   FROM parts p, assembly a
 
1262                   WHERE p.id = a.parts_id
 
1263                   AND a.id = $item->{id}|;
 
1265       $sth = $dbh->prepare($query);
 
1266       $sth->execute || $form->dberror($query);
 
1268       while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1269         $ref->{assemblyitem} = 1;
 
1270         push @assemblies, $ref;
 
1274       push @assemblies, { id => $item->{id} };
 
1278     # copy assemblies to $form->{parts}
 
1279     @{ $form->{parts} } = @assemblies;
 
1283   $main::lxdebug->leave_sub();
 
1287   $main::lxdebug->enter_sub();
 
1289   my ($self, $myconfig, $form) = @_;
 
1291   my $where = '1 = 1';
 
1297   foreach my $item (qw(partnumber drawing microfiche make model)) {
 
1298     if ($form->{$item}) {
 
1299       $var = $form->like(lc $form->{$item});
 
1301       # make will build later Bugfix 145
 
1302       if ($item ne 'make') {
 
1303         $where .= " AND lower(p.$item) LIKE '$var'";
 
1308   # special case for description
 
1309   if ($form->{description}) {
 
1310     unless (   $form->{bought}
 
1315             || $form->{quoted}) {
 
1316       $var = $form->like(lc $form->{description});
 
1317       $where .= " AND lower(p.description) LIKE '$var'";
 
1321   # special case for serialnumber
 
1322   if ($form->{l_serialnumber}) {
 
1323     if ($form->{serialnumber}) {
 
1324       $var = $form->like(lc $form->{serialnumber});
 
1325       $where .= " AND lower(serialnumber) LIKE '$var'";
 
1330   # items which were never bought, sold or on an order
 
1331   if ($form->{itemstatus} eq 'orphaned') {
 
1332     $form->{onhand}  = $form->{short}   = 0;
 
1333     $form->{bought}  = $form->{sold}    = 0;
 
1334     $form->{onorder} = $form->{ordered} = 0;
 
1335     $form->{rfq}     = $form->{quoted}  = 0;
 
1337     $form->{transdatefrom} = $form->{transdateto} = "";
 
1339     $where .= " AND p.onhand = 0
 
1340                 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
 
1341                                  WHERE p.id = i.parts_id)
 
1342                 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
 
1343                                  WHERE p.id = a.parts_id)
 
1344                 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
 
1345                                  WHERE p.id = o.parts_id)";
 
1348   if ($form->{itemstatus} eq 'active') {
 
1349     $where .= " AND p.obsolete = '0'";
 
1351   if ($form->{itemstatus} eq 'obsolete') {
 
1352     $where .= " AND p.obsolete = '1'";
 
1353     $form->{onhand} = $form->{short} = 0;
 
1355   if ($form->{itemstatus} eq 'onhand') {
 
1356     $where .= " AND p.onhand > 0";
 
1358   if ($form->{itemstatus} eq 'short') {
 
1359     $where .= " AND p.onhand < p.rop";
 
1361   if ($form->{make}) {
 
1362     $var = $form->like(lc $form->{make});
 
1363     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
 
1364                            FROM makemodel m WHERE lower(m.make) LIKE '$var')";
 
1366   if ($form->{model}) {
 
1367     $var = $form->like(lc $form->{model});
 
1368     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
 
1369                            FROM makemodel m WHERE lower(m.model) LIKE '$var')";
 
1371   if ($form->{partsgroup}) {
 
1372     $var = $form->like(lc $form->{partsgroup});
 
1373     $where .= " AND lower(pg.partsgroup) LIKE '$var'";
 
1377   # connect to database
 
1378   my $dbh = $form->dbconnect_noauto($myconfig);
 
1380   if ($form->{"sellprice"} ne "") {
 
1382     my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"});
 
1383     if ($form->{"sellprice_type"} eq "percent") {
 
1384       my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
 
1385       $update = "sellprice* $faktor";
 
1387       $update = "sellprice+$faktor";
 
1390     $query = qq|UPDATE parts set sellprice=$update WHERE id IN (SELECT p.id
 
1392                   LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1397   if ($form->{"listprice"} ne "") {
 
1399     my $faktor = $form->parse_amount($myconfig,$form->{"listprice"});
 
1400     if ($form->{"listprice_type"} eq "percent") {
 
1401       my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
 
1402       $update = "listprice* $faktor";
 
1404       $update = "listprice+$faktor";
 
1407     $query = qq|UPDATE parts set listprice=$update WHERE id IN (SELECT p.id
 
1409                   LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1418   for my $i (1 .. $form->{price_rows}) {
 
1423     if ($form->{"price_$i"} ne "") {
 
1425       my $faktor = $form->parse_amount($myconfig,$form->{"price_$i"});
 
1426       if ($form->{"pricegroup_type_$i"} eq "percent") {
 
1427         my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
 
1428         $update = "price* $faktor";
 
1430         $update = "price+$faktor";
 
1433       $query = qq|UPDATE prices set price=$update WHERE parts_id IN (SELECT p.id
 
1435                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
1436                     WHERE $where) AND pricegroup_id=$form->{"pricegroup_id_$i"}|;
 
1444   my $rc= $dbh->commit;
 
1446   $main::lxdebug->leave_sub();
 
1452   $main::lxdebug->enter_sub();
 
1454   my ($self, $module, $myconfig, $form) = @_;
 
1456   # connect to database
 
1457   my $dbh = $form->dbconnect($myconfig);
 
1460     $query = qq|SELECT c.accno, c.description, c.link, c.id,
 
1461                         p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
 
1462                         FROM chart c, parts p
 
1463                         WHERE c.link LIKE '%$module%'
 
1464                         AND p.id = $form->{id}
 
1467     $query = qq|SELECT c.accno, c.description, c.link, c.id,
 
1468                 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
 
1469                 FROM chart c, defaults d
 
1470                 WHERE c.link LIKE '%$module%'
 
1474   my $sth = $dbh->prepare($query);
 
1475   $sth->execute || $form->dberror($query);
 
1476   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1477     foreach my $key (split /:/, $ref->{link}) {
 
1478       if ($key =~ /$module/) {
 
1479         if (   ($ref->{id} eq $ref->{inventory_accno_id})
 
1480             || ($ref->{id} eq $ref->{income_accno_id})
 
1481             || ($ref->{id} eq $ref->{expense_accno_id})) {
 
1482           push @{ $form->{"${module}_links"}{$key} },
 
1483             { accno       => $ref->{accno},
 
1484               description => $ref->{description},
 
1485               selected    => "selected" };
 
1486           $form->{"${key}_default"} = "$ref->{accno}--$ref->{description}";
 
1488           push @{ $form->{"${module}_links"}{$key} },
 
1489             { accno       => $ref->{accno},
 
1490               description => $ref->{description},
 
1498   # get buchungsgruppen
 
1499   $query = qq|SELECT id, description
 
1500               FROM buchungsgruppen|;
 
1501   $sth = $dbh->prepare($query);
 
1502   $sth->execute || $form->dberror($query);
 
1504   $form->{BUCHUNGSGRUPPEN} = [];
 
1505   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1506     push @{ $form->{BUCHUNGSGRUPPEN} }, $ref;
 
1511   $query = qq|SELECT id, description
 
1514   $sth = $dbh->prepare($query);
 
1515   $sth->execute || $form->dberror($query);
 
1517   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1518     push @{ $self->{payment_terms} }, $ref;
 
1523     $query = qq|SELECT weightunit
 
1525     $sth = $dbh->prepare($query);
 
1526     $sth->execute || $form->dberror($query);
 
1528     ($form->{weightunit}) = $sth->fetchrow_array;
 
1532     $query = qq|SELECT weightunit, current_date
 
1534     $sth = $dbh->prepare($query);
 
1535     $sth->execute || $form->dberror($query);
 
1537     ($form->{weightunit}, $form->{priceupdate}) = $sth->fetchrow_array;
 
1542   $main::lxdebug->leave_sub();
 
1545 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
 
1547   $main::lxdebug->enter_sub();
 
1549   my ($self, $myconfig, $form, $sortorder) = @_;
 
1550   my $dbh   = $form->dbconnect($myconfig);
 
1551   my $order = " p.partnumber";
 
1552   my $where = "1 = 1";
 
1554   if ($sortorder eq "all") {
 
1555     $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
 
1556     $where .= " AND p.description LIKE '%$form->{description}%'";
 
1558     if ($sortorder eq "partnumber") {
 
1559       $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
 
1560       $order = qq|p.$sortorder|;
 
1562     if ($sortorder eq "description") {
 
1563       $where .= " AND p.description LIKE '%$form->{description}%'";
 
1568     qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
 
1569   my $sth = $dbh->prepare($query);
 
1570   $sth->execute || $self->dberror($query);
 
1572   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1573     if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
 
1576       $form->{"id_$j"}          = $ref->{id};
 
1577       $form->{"partnumber_$j"}  = $ref->{partnumber};
 
1578       $form->{"description_$j"} = $ref->{description};
 
1579       $form->{"unit_$j"}        = $ref->{unit};
 
1580       $form->{"sellprice_$j"}   = $ref->{sellprice};
 
1581       $form->{"soldtotal_$j"}   = get_soldtotal($dbh, $ref->{id});
 
1588   $main::lxdebug->leave_sub();
 
1593 # gets sum of sold part with part_id
 
1595   $main::lxdebug->enter_sub();
 
1597   my ($dbh, $id) = @_;
 
1600     qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
 
1602   my $sth = $dbh->prepare($query);
 
1603   $sth->execute || $form->dberror($query);
 
1606   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1608     $sum = $ref->{totalsold};
 
1612   if ($sum eq undef) {
 
1616   $main::lxdebug->leave_sub();
 
1619 }    #end get_soldtotal
 
1622   $main::lxdebug->enter_sub();
 
1624   my ($self, $myconfig, $form) = @_;
 
1625   my $i     = $form->{rowcount};
 
1626   my $where = "NOT p.obsolete = '1'";
 
1628   if ($form->{"partnumber_$i"}) {
 
1629     my $partnumber = $form->like(lc $form->{"partnumber_$i"});
 
1630     $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
 
1632   if ($form->{"description_$i"}) {
 
1633     my $description = $form->like(lc $form->{"description_$i"});
 
1634     $where .= " AND lower(p.description) LIKE '$description'";
 
1637   if ($form->{"partsgroup_$i"}) {
 
1638     my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
 
1639     $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
 
1642   if ($form->{"description_$i"}) {
 
1643     $where .= " ORDER BY description";
 
1645     $where .= " ORDER BY partnumber";
 
1648   # connect to database
 
1649   my $dbh = $form->dbconnect($myconfig);
 
1651   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
 
1653                         c1.accno AS inventory_accno,
 
1654                         c2.accno AS income_accno,
 
1655                         c3.accno AS expense_accno,
 
1656                  p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
 
1659                  LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
 
1660                  LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
 
1661                  LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
 
1662                  LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
 
1664   my $sth = $dbh->prepare($query);
 
1665   $sth->execute || $form->dberror($query);
 
1667   #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1669   # get tax rates and description
 
1670   #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
 
1671   #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
 
1672   #           FROM chart c, tax t
 
1673   #           WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
 
1675   # $stw = $dbh->prepare($query);
 
1676   #$stw->execute || $form->dberror($query);
 
1678   #$ref->{taxaccounts} = "";
 
1679   #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
 
1681   #   $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
 
1682   #  $form->{"$ptr->{accno}_description"} = $ptr->{description};
 
1683   #   $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
 
1684   #   $form->{taxaccounts} .= "$ptr->{accno} ";
 
1685   #   $ref->{taxaccounts} .= "$ptr->{accno} ";
 
1690   #chop $ref->{taxaccounts};
 
1692   push @{ $form->{item_list} }, $ref;
 
1698   $main::lxdebug->leave_sub();
 
1701 sub retrieve_languages {
 
1702   $main::lxdebug->enter_sub();
 
1704   my ($self, $myconfig, $form) = @_;
 
1706   # connect to database
 
1707   my $dbh = $form->dbconnect($myconfig);
 
1710     $where .= "tr.parts_id=$form->{id}";
 
1714   if ($form->{language_values} ne "") {
 
1715   $query = qq|SELECT l.id, l.description, tr.translation, tr.longdescription
 
1716                  FROM language l LEFT OUTER JOIN translation tr ON (tr.language_id=l.id AND $where)|;
 
1718   $query = qq|SELECT l.id, l.description
 
1721   my $sth = $dbh->prepare($query);
 
1722   $sth->execute || $form->dberror($query);
 
1724   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1725     push(@{$languages}, $ref);
 
1731   $main::lxdebug->leave_sub();
 
1736 sub follow_account_chain {
 
1737   $main::lxdebug->enter_sub();
 
1739   my ($self, $form, $dbh, $transdate, $accno_id, $accno) = @_;
 
1741   my @visited_accno_ids = ($accno_id);
 
1746     "SELECT c.new_chart_id, date($transdate) >= c.valid_from AS is_valid, " .
 
1749     "LEFT JOIN chart cnew ON c.new_chart_id = cnew.id " .
 
1750     "WHERE (c.id = ?) AND NOT c.new_chart_id ISNULL AND (c.new_chart_id > 0)";
 
1751   $sth = $dbh->prepare($query);
 
1754     $sth->execute($accno_id) || $form->dberror($query . " ($accno_id)");
 
1755     $ref = $sth->fetchrow_hashref();
 
1756     last unless ($ref && $ref->{"is_valid"} &&
 
1757                  !grep({ $_ == $ref->{"new_chart_id"} } @visited_accno_ids));
 
1758     $accno_id = $ref->{"new_chart_id"};
 
1759     $accno = $ref->{"accno"};
 
1760     push(@visited_accno_ids, $accno_id);
 
1763   $main::lxdebug->leave_sub();
 
1765   return ($accno_id, $accno);
 
1768 sub retrieve_accounts {
 
1769   $main::lxdebug->enter_sub();
 
1771   my ($self, $myconfig, $form, $parts_id, $index, $copy_accnos) = @_;
 
1773   my ($query, $sth, $dbh);
 
1775   return $main::lxdebug->leave_sub() if (!defined($form->{"taxzone_id"}));
 
1777   $dbh = $form->dbconnect($myconfig);
 
1780   if ($form->{type} eq "invoice") {
 
1781     if (($form->{vc} eq "vendor") || !$form->{deliverydate}) {
 
1782       $transdate = $form->{invdate};
 
1784       $transdate = $form->{deliverydate};
 
1786   } elsif ($form->{type} eq "credit_note") {
 
1787     $transdate = $form->{invdate};
 
1789     $transdate = $form->{transdate};
 
1792   if ($transdate eq "") {
 
1793     $transdate = "current_date";
 
1795     $transdate = $dbh->quote($transdate);
 
1800     "  p.inventory_accno_id AS is_part, " .
 
1801     "  bg.inventory_accno_id, " .
 
1802     "  bg.income_accno_id_$form->{taxzone_id} AS income_accno_id, " .
 
1803     "  bg.expense_accno_id_$form->{taxzone_id} AS expense_accno_id, " .
 
1804     "  c1.accno AS inventory_accno, " .
 
1805     "  c2.accno AS income_accno, " .
 
1806     "  c3.accno AS expense_accno " .
 
1808     "LEFT JOIN buchungsgruppen bg ON p.buchungsgruppen_id = bg.id " .
 
1809     "LEFT JOIN chart c1 ON bg.inventory_accno_id = c1.id " .
 
1810     "LEFT JOIN chart c2 ON bg.income_accno_id_$form->{taxzone_id} = c2.id " .
 
1811     "LEFT JOIN chart c3 ON bg.expense_accno_id_$form->{taxzone_id} = c3.id " .
 
1813   $sth = $dbh->prepare($query);
 
1814   $sth->execute($parts_id) || $form->dberror($query . " ($parts_id)");
 
1815   my $ref = $sth->fetchrow_hashref();
 
1818 #   $main::lxdebug->message(0, "q $query");
 
1822     return $lxdebug->leave_sub();
 
1825   $ref->{"inventory_accno_id"} = undef unless ($ref->{"is_part"});
 
1828   foreach my $type (qw(inventory income expense)) {
 
1829     next unless ($ref->{"${type}_accno_id"});
 
1830     ($accounts{"${type}_accno_id"}, $accounts{"${type}_accno"}) =
 
1831       $self->follow_account_chain($form, $dbh, $transdate,
 
1832                                   $ref->{"${type}_accno_id"},
 
1833                                   $ref->{"${type}_accno"});
 
1836   map({ $form->{"${_}_accno_$index"} = $accounts{"${_}_accno"} }
 
1837       qw(inventory income expense));
 
1839   my $inc_exp = $form->{"vc"} eq "customer" ? "income" : "expense";
 
1840   my $accno_id = $accounts{"${inc_exp}_accno_id"};
 
1843     "SELECT c.accno, t.taxdescription AS description, t.rate, t.taxnumber " .
 
1845     "LEFT JOIN chart c ON c.id = t.chart_id " .
 
1847     "  (SELECT tk.tax_id " .
 
1848     "   FROM taxkeys tk " .
 
1849     "   WHERE tk.chart_id = $accno_id AND startdate <= $transdate " .
 
1850     "   ORDER BY startdate DESC LIMIT 1) ";
 
1851   $sth = $dbh->prepare($query);
 
1852   $sth->execute() || $form->dberror($query);
 
1853   $ref = $sth->fetchrow_hashref();
 
1858     $main::lxdebug->leave_sub();
 
1862   $form->{"taxaccounts_$index"} = $ref->{"accno"};
 
1863   if ($form->{"taxaccounts"} !~ /$ref->{accno}/) {
 
1864     $form->{"taxaccounts"} .= "$ref->{accno} ";
 
1866   map({ $form->{"$ref->{accno}_${_}"} = $ref->{$_}; }
 
1867       qw(rate description taxnumber));
 
1869 #   $main::lxdebug->message(0, "formvars: rate " . $form->{"$ref->{accno}_rate"} .
 
1870 #                           " description " . $form->{"$ref->{accno}_description"} .
 
1871 #                           " taxnumber " . $form->{"$ref->{accno}_taxnumber"} .
 
1872 #                           " || taxaccounts_$index " . $form->{"taxaccounts_$index"} .
 
1873 #                           " || taxaccounts " . $form->{"taxaccounts"});
 
1877   $main::lxdebug->leave_sub();