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;
374 $form->{buchungsgruppen_id} *= 1;
375 $form->{adr_id} *= 1;
376 $form->{not_discountable} *= 1;
377 $form->{payment_id} *= 1;
384 $query = qq|SELECT p.sellprice, p.weight
386 WHERE p.id = $form->{id}|;
387 $sth = $dbh->prepare($query);
388 $sth->execute || $form->dberror($query);
389 my ($sellprice, $weight) = $sth->fetchrow_array;
392 # if item is part of an assembly adjust all assemblies
393 $query = qq|SELECT a.id, a.qty
395 WHERE a.parts_id = $form->{id}|;
396 $sth = $dbh->prepare($query);
397 $sth->execute || $form->dberror($query);
398 while (my ($id, $qty) = $sth->fetchrow_array) {
399 &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
403 if ($form->{item} ne 'service') {
405 # delete makemodel records
406 $query = qq|DELETE FROM makemodel
407 WHERE parts_id = $form->{id}|;
408 $dbh->do($query) || $form->dberror($query);
411 if ($form->{item} eq 'assembly') {
412 if ($form->{onhand} != 0) {
413 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
416 # delete assembly records
417 $query = qq|DELETE FROM assembly
418 WHERE id = $form->{id}|;
419 $dbh->do($query) || $form->dberror($query);
421 $form->{onhand} += $form->{stock};
425 $query = qq|DELETE FROM partstax
426 WHERE parts_id = $form->{id}|;
427 $dbh->do($query) || $form->dberror($query);
429 # delete translations
430 $query = qq|DELETE FROM translation
431 WHERE parts_id = $form->{id}|;
432 $dbh->do($query) || $form->dberror($query);
435 my $uid = rand() . time;
436 $uid .= $form->{login};
438 $query = qq|SELECT p.id FROM parts p
439 WHERE p.partnumber = '$form->{partnumber}'|;
440 $sth = $dbh->prepare($query);
441 $sth->execute || $form->dberror($query);
442 ($form->{id}) = $sth->fetchrow_array;
445 if ($form->{id} ne "") {
446 $main::lxdebug->leave_sub();
449 $query = qq|INSERT INTO parts (partnumber, description)
450 VALUES ('$uid', 'dummy')|;
451 $dbh->do($query) || $form->dberror($query);
453 $query = qq|SELECT p.id FROM parts p
454 WHERE p.partnumber = '$uid'|;
455 $sth = $dbh->prepare($query);
456 $sth->execute || $form->dberror($query);
458 ($form->{id}) = $sth->fetchrow_array;
461 $form->{orphaned} = 1;
462 $form->{onhand} = $form->{stock} if $form->{item} eq 'assembly';
463 if ($form->{partnumber} eq "" && $form->{inventory_accno} eq "") {
464 $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
466 if ($form->{partnumber} eq "" && $form->{inventory_accno} ne "") {
467 $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
471 my $partsgroup_id = 0;
473 if ($form->{partsgroup}) {
474 ($partsgroup, $partsgroup_id) = split /--/, $form->{partsgroup};
477 $query = qq|UPDATE parts SET
478 partnumber = '$form->{partnumber}',
479 description = '$form->{description}',
480 makemodel = '$form->{makemodel}',
481 alternate = '$form->{alternate}',
482 assembly = '$form->{assembly}',
483 listprice = $form->{listprice},
484 sellprice = $form->{sellprice},
485 lastcost = $form->{lastcost},
486 weight = $form->{weight},
487 priceupdate = $form->{priceupdate},
488 unit = '$form->{unit}',
489 notes = '$form->{notes}',
490 formel = '$form->{formel}',
492 bin = '$form->{bin}',
493 buchungsgruppen_id = '$form->{buchungsgruppen_id}',
494 adr_id = '$form->{adr_id}',
495 payment_id = '$form->{payment_id}',
496 inventory_accno_id = (SELECT c.id FROM chart c
497 WHERE c.accno = '$form->{inventory_accno}'),
498 income_accno_id = (SELECT c.id FROM chart c
499 WHERE c.accno = '$form->{income_accno}'),
500 expense_accno_id = (SELECT c.id FROM chart c
501 WHERE c.accno = '$form->{expense_accno}'),
502 obsolete = '$form->{obsolete}',
503 image = '$form->{image}',
504 drawing = '$form->{drawing}',
505 shop = '$form->{shop}',
508 alu = '$form->{alu}',
509 not_discountable = '$form->{not_discountable}',
510 microfiche = '$form->{microfiche}',
511 partsgroup_id = $partsgroup_id
512 WHERE id = $form->{id}|;
513 $dbh->do($query) || $form->dberror($query);
515 # delete translation records
516 $query = qq|DELETE FROM translation
517 WHERE parts_id = $form->{id}|;
518 $dbh->do($query) || $form->dberror($query);
520 if ($form->{language_values} ne "") {
521 split /---\+\+\+---/,$form->{language_values};
523 my ($language_id, $translation, $longdescription) = split /--\+\+--/, $item;
524 if ($translation ne "") {
525 $query = qq|INSERT into translation (parts_id, language_id, translation, longdescription) VALUES
526 ($form->{id}, $language_id, | . $dbh->quote($translation) . qq|, | . $dbh->quote($longdescription) . qq| )|;
527 $dbh->do($query) || $form->dberror($query);
531 # delete price records
532 $query = qq|DELETE FROM prices
533 WHERE parts_id = $form->{id}|;
534 $dbh->do($query) || $form->dberror($query);
535 # insert price records only if different to sellprice
536 for my $i (1 .. $form->{price_rows}) {
537 if ($form->{"price_$i"} eq "0") {
538 $form->{"price_$i"} = $form->{sellprice};
541 ( $form->{"price_$i"}
542 || $form->{"klass_$i"}
543 || $form->{"pricegroup_id_$i"})
544 and $form->{"price_$i"} != $form->{sellprice}
546 $klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
547 $price = $form->parse_amount($myconfig, $form->{"price_$i"});
549 $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
550 $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price)
551 VALUES($form->{id},$pricegroup_id,$price)|;
552 $dbh->do($query) || $form->dberror($query);
556 # insert makemodel records
557 unless ($form->{item} eq 'service') {
558 for my $i (1 .. $form->{makemodel_rows}) {
559 if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
560 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
562 $query = qq|INSERT INTO makemodel (parts_id, make, model)
564 '$form->{"make_$i"}', '$form->{"model_$i"}')|;
565 $dbh->do($query) || $form->dberror($query);
571 foreach $item (split / /, $form->{taxaccounts}) {
572 if ($form->{"IC_tax_$item"}) {
573 $query = qq|INSERT INTO partstax (parts_id, chart_id)
577 WHERE c.accno = '$item'))|;
578 $dbh->do($query) || $form->dberror($query);
582 # add assembly records
583 if ($form->{item} eq 'assembly') {
585 for my $i (1 .. $form->{assembly_rows}) {
586 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
588 if ($form->{"qty_$i"} != 0) {
589 $form->{"bom_$i"} *= 1;
590 $query = qq|INSERT INTO assembly (id, parts_id, qty, bom)
591 VALUES ($form->{id}, $form->{"id_$i"},
592 $form->{"qty_$i"}, '$form->{"bom_$i"}')|;
593 $dbh->do($query) || $form->dberror($query);
597 # adjust onhand for the parts
598 if ($form->{onhand} != 0) {
599 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
605 my $shippingdate = "$a[5]-$a[4]-$a[3]";
607 $form->get_employee($dbh);
609 # add inventory record
610 $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
611 shippingdate, employee_id) VALUES (
612 0, $form->{id}, $form->{stock}, '$shippingdate',
613 $form->{employee_id})|;
614 $dbh->do($query) || $form->dberror($query);
618 #set expense_accno=inventory_accno if they are different => bilanz
620 ($form->{expense_accno} != $form->{inventory_accno})
621 ? $form->{inventory_accno}
622 : $form->{expense_accno};
624 # get tax rates and description
626 ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
627 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
629 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
631 $stw = $dbh->prepare($query);
633 $stw->execute || $form->dberror($query);
635 $form->{taxaccount} = "";
636 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
638 # if ($customertax{$ref->{accno}}) {
639 $form->{taxaccount} .= "$ptr->{accno} ";
640 if (!($form->{taxaccount2} =~ /$ptr->{accno}/)) {
641 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
642 $form->{"$ptr->{accno}_description"} = $ptr->{description};
643 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
644 $form->{taxaccount2} .= " $ptr->{accno} ";
650 my $rc = $dbh->commit;
653 $main::lxdebug->leave_sub();
658 sub update_assembly {
659 $main::lxdebug->enter_sub();
661 my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
663 my $query = qq|SELECT a.id, a.qty
665 WHERE a.parts_id = $id|;
666 my $sth = $dbh->prepare($query);
667 $sth->execute || $form->dberror($query);
669 while (my ($pid, $aqty) = $sth->fetchrow_array) {
670 &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
674 $query = qq|UPDATE parts
675 SET sellprice = sellprice +
676 $qty * ($form->{sellprice} - $sellprice),
678 $qty * ($form->{weight} - $weight)
680 $dbh->do($query) || $form->dberror($query);
682 $main::lxdebug->leave_sub();
685 sub retrieve_assemblies {
686 $main::lxdebug->enter_sub();
688 my ($self, $myconfig, $form) = @_;
690 # connect to database
691 my $dbh = $form->dbconnect($myconfig);
695 if ($form->{partnumber}) {
696 my $partnumber = $form->like(lc $form->{partnumber});
697 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
700 if ($form->{description}) {
701 my $description = $form->like(lc $form->{description});
702 $where .= " AND lower(p.description) LIKE '$description'";
704 $where .= " AND NOT p.obsolete = '1'";
706 # retrieve assembly items
707 my $query = qq|SELECT p.id, p.partnumber, p.description,
708 p.bin, p.onhand, p.rop,
709 (SELECT sum(p2.inventory_accno_id)
710 FROM parts p2, assembly a
711 WHERE p2.id = a.parts_id
712 AND a.id = p.id) AS inventory
717 my $sth = $dbh->prepare($query);
718 $sth->execute || $form->dberror($query);
720 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
721 push @{ $form->{assembly_items} }, $ref if $ref->{inventory};
727 $main::lxdebug->leave_sub();
730 sub restock_assemblies {
731 $main::lxdebug->enter_sub();
733 my ($self, $myconfig, $form) = @_;
735 # connect to database
736 my $dbh = $form->dbconnect_noauto($myconfig);
738 for my $i (1 .. $form->{rowcount}) {
740 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
742 if ($form->{"qty_$i"} != 0) {
743 &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
748 my $rc = $dbh->commit;
751 $main::lxdebug->leave_sub();
756 sub adjust_inventory {
757 $main::lxdebug->enter_sub();
759 my ($dbh, $form, $id, $qty) = @_;
761 my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
762 FROM parts p, assembly a
763 WHERE a.parts_id = p.id
765 my $sth = $dbh->prepare($query);
766 $sth->execute || $form->dberror($query);
768 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
770 my $allocate = $qty * $ref->{qty};
772 # is it a service item, then loop
773 $ref->{inventory_accno_id} *= 1;
774 next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
776 # adjust parts onhand
777 $form->update_balance($dbh, "parts", "onhand",
785 my $rc = $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $qty);
787 $main::lxdebug->leave_sub();
793 $main::lxdebug->enter_sub();
795 my ($self, $myconfig, $form) = @_;
797 # connect to database, turn off AutoCommit
798 my $dbh = $form->dbconnect_noauto($myconfig);
800 my $query = qq|DELETE FROM parts
801 WHERE id = $form->{id}|;
802 $dbh->do($query) || $form->dberror($query);
804 $query = qq|DELETE FROM partstax
805 WHERE parts_id = $form->{id}|;
806 $dbh->do($query) || $form->dberror($query);
808 # check if it is a part, assembly or service
809 if ($form->{item} ne 'service') {
810 $query = qq|DELETE FROM makemodel
811 WHERE parts_id = $form->{id}|;
812 $dbh->do($query) || $form->dberror($query);
815 if ($form->{item} eq 'assembly') {
818 $query = qq|DELETE FROM inventory
819 WHERE parts_id = $form->{id}|;
820 $dbh->do($query) || $form->dberror($query);
822 $query = qq|DELETE FROM assembly
823 WHERE id = $form->{id}|;
824 $dbh->do($query) || $form->dberror($query);
827 if ($form->{item} eq 'alternate') {
828 $query = qq|DELETE FROM alternate
829 WHERE id = $form->{id}|;
830 $dbh->do($query) || $form->dberror($query);
834 my $rc = $dbh->commit;
837 $main::lxdebug->leave_sub();
843 $main::lxdebug->enter_sub();
845 my ($self, $myconfig, $form) = @_;
847 my $i = $form->{assembly_rows};
851 if ($form->{"partnumber_$i"}) {
852 $var = $form->like(lc $form->{"partnumber_$i"});
853 $where .= " AND lower(p.partnumber) LIKE '$var'";
855 if ($form->{"description_$i"}) {
856 $var = $form->like(lc $form->{"description_$i"});
857 $where .= " AND lower(p.description) LIKE '$var'";
859 if ($form->{"partsgroup_$i"}) {
860 $var = $form->like(lc $form->{"partsgroup_$i"});
861 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
865 $where .= " AND NOT p.id = $form->{id}";
869 $where .= " ORDER BY p.partnumber";
871 $where .= " ORDER BY p.description";
874 # connect to database
875 my $dbh = $form->dbconnect($myconfig);
877 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
878 p.weight, p.onhand, p.unit,
881 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
883 my $sth = $dbh->prepare($query);
884 $sth->execute || $form->dberror($query);
886 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
887 push @{ $form->{item_list} }, $ref;
893 $main::lxdebug->leave_sub();
897 $main::lxdebug->enter_sub();
899 my ($self, $myconfig, $form) = @_;
907 foreach my $item (qw(partnumber drawing microfiche make)) {
908 if ($form->{$item}) {
909 $var = $form->like(lc $form->{$item});
911 # make will build later Bugfix 145
912 # model will build later too - Bugfix 331
913 if ($item ne 'make') {
914 $where .= " AND lower(p.$item) LIKE '$var'";
919 # special case for description
920 if ($form->{description}) {
921 unless ( $form->{bought}
926 || $form->{quoted}) {
927 $var = $form->like(lc $form->{description});
928 $where .= " AND lower(p.description) LIKE '$var'";
932 # special case for serialnumber
933 if ($form->{l_serialnumber}) {
934 if ($form->{serialnumber}) {
935 $var = $form->like(lc $form->{serialnumber});
936 $where .= " AND lower(serialnumber) LIKE '$var'";
940 if ($form->{searchitems} eq 'part') {
941 $where .= " AND p.inventory_accno_id > 0";
943 if ($form->{searchitems} eq 'assembly') {
944 $form->{bought} = "";
945 $where .= " AND p.assembly = '1'";
947 if ($form->{searchitems} eq 'service') {
948 $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
950 # irrelevant for services
951 $form->{make} = $form->{model} = "";
954 # items which were never bought, sold or on an order
955 if ($form->{itemstatus} eq 'orphaned') {
956 $form->{onhand} = $form->{short} = 0;
957 $form->{bought} = $form->{sold} = 0;
958 $form->{onorder} = $form->{ordered} = 0;
959 $form->{rfq} = $form->{quoted} = 0;
961 $form->{transdatefrom} = $form->{transdateto} = "";
963 $where .= " AND p.onhand = 0
964 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
965 WHERE p.id = i.parts_id)
966 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
967 WHERE p.id = a.parts_id)
968 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
969 WHERE p.id = o.parts_id)";
972 if ($form->{itemstatus} eq 'active') {
973 $where .= " AND p.obsolete = '0'";
975 if ($form->{itemstatus} eq 'obsolete') {
976 $where .= " AND p.obsolete = '1'";
977 $form->{onhand} = $form->{short} = 0;
979 if ($form->{itemstatus} eq 'onhand') {
980 $where .= " AND p.onhand > 0";
982 if ($form->{itemstatus} eq 'short') {
983 $where .= " AND p.onhand < p.rop";
986 $var = $form->like(lc $form->{make});
987 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
988 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
990 if ($form->{model}) {
991 $var = $form->like(lc $form->{model});
992 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
993 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
995 if ($form->{partsgroup}) {
996 $var = $form->like(lc $form->{partsgroup});
997 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
999 if ($form->{l_soldtotal}) {
1000 $where .= " AND p.id=i.parts_id AND i.qty >= 0";
1002 " GROUP BY p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
1004 if ($form->{top100}) {
1005 $limit = " LIMIT 100";
1009 if ($form->{revers} == 1) {
1010 $form->{desc} = " DESC";
1015 # connect to database
1016 my $dbh = $form->dbconnect($myconfig);
1018 my $sortorder = $form->{sort};
1019 $sortorder .= $form->{desc};
1020 $sortorder = $form->{sort} if $form->{sort};
1024 if ($form->{l_soldtotal}) {
1025 $form->{soldtotal} = 'soldtotal';
1027 qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
1028 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
1029 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
1035 $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
1036 p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
1037 p.priceupdate, p.image, p.drawing, p.microfiche,
1040 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1043 ORDER BY $sortorder|;
1046 # rebuild query for bought and sold items
1047 if ( $form->{bought}
1052 || $form->{quoted}) {
1054 my @a = qw(partnumber description bin priceupdate name);
1056 push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
1057 push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
1058 push @a, "quonumber" if ($form->{rfq} || $form->{quoted});
1063 if ($form->{bought} || $form->{sold}) {
1065 my $invwhere = "$where";
1066 $invwhere .= " AND i.assemblyitem = '0'";
1067 $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
1068 if $form->{transdatefrom};
1069 $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
1070 if $form->{transdateto};
1072 if ($form->{description}) {
1073 $var = $form->like(lc $form->{description});
1074 $invwhere .= " AND lower(i.description) LIKE '$var'";
1077 my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
1078 i.qty AS onhand, i.unit, p.bin, i.sellprice,
1079 p.listprice, p.lastcost, p.rop, p.weight,
1080 p.priceupdate, p.image, p.drawing, p.microfiche,
1082 a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
1085 if ($form->{bought}) {
1087 SELECT $flds, 'ir' AS module, '' AS type,
1090 JOIN parts p ON (p.id = i.parts_id)
1091 JOIN ap a ON (a.id = i.trans_id)
1092 JOIN vendor ct ON (a.vendor_id = ct.id)
1093 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1099 if ($form->{sold}) {
1101 SELECT $flds, 'is' AS module, '' AS type,
1104 JOIN parts p ON (p.id = i.parts_id)
1105 JOIN ar a ON (a.id = i.trans_id)
1106 JOIN customer ct ON (a.customer_id = ct.id)
1107 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1114 if ($form->{onorder} || $form->{ordered}) {
1115 my $ordwhere = "$where
1116 AND o.quotation = '0'";
1117 $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1118 if $form->{transdatefrom};
1119 $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
1120 if $form->{transdateto};
1122 if ($form->{description}) {
1123 $var = $form->like(lc $form->{description});
1124 $ordwhere .= " AND lower(oi.description) LIKE '$var'";
1128 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1129 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1130 p.listprice, p.lastcost, p.rop, p.weight,
1131 p.priceupdate, p.image, p.drawing, p.microfiche,
1133 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1136 if ($form->{ordered}) {
1138 SELECT $flds, 'oe' AS module, 'sales_order' AS type,
1139 (SELECT buy FROM exchangerate ex
1140 WHERE ex.curr = o.curr
1141 AND ex.transdate = o.transdate) AS exchangerate
1143 JOIN parts p ON (oi.parts_id = p.id)
1144 JOIN oe o ON (oi.trans_id = o.id)
1145 JOIN customer ct ON (o.customer_id = ct.id)
1146 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1148 AND o.customer_id > 0|;
1153 if ($form->{onorder}) {
1155 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1156 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1157 p.listprice, p.lastcost, p.rop, p.weight,
1158 p.priceupdate, p.image, p.drawing, p.microfiche,
1160 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1164 SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
1165 (SELECT sell FROM exchangerate ex
1166 WHERE ex.curr = o.curr
1167 AND ex.transdate = o.transdate) AS exchangerate
1169 JOIN parts p ON (oi.parts_id = p.id)
1170 JOIN oe o ON (oi.trans_id = o.id)
1171 JOIN vendor ct ON (o.vendor_id = ct.id)
1172 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1174 AND o.vendor_id > 0|;
1179 if ($form->{rfq} || $form->{quoted}) {
1180 my $quowhere = "$where
1181 AND o.quotation = '1'";
1182 $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1183 if $form->{transdatefrom};
1184 $quowhere .= " AND o.transdate <= '$form->{transdateto}'"
1185 if $form->{transdateto};
1187 if ($form->{description}) {
1188 $var = $form->like(lc $form->{description});
1189 $quowhere .= " AND lower(oi.description) LIKE '$var'";
1193 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1194 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1195 p.listprice, p.lastcost, p.rop, p.weight,
1196 p.priceupdate, p.image, p.drawing, p.microfiche,
1198 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1201 if ($form->{quoted}) {
1203 SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
1204 (SELECT buy FROM exchangerate ex
1205 WHERE ex.curr = o.curr
1206 AND ex.transdate = o.transdate) AS exchangerate
1208 JOIN parts p ON (oi.parts_id = p.id)
1209 JOIN oe o ON (oi.trans_id = o.id)
1210 JOIN customer ct ON (o.customer_id = ct.id)
1211 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1213 AND o.customer_id > 0|;
1220 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1221 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1222 p.listprice, p.lastcost, p.rop, p.weight,
1223 p.priceupdate, p.image, p.drawing, p.microfiche,
1225 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1229 SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
1230 (SELECT sell FROM exchangerate ex
1231 WHERE ex.curr = o.curr
1232 AND ex.transdate = o.transdate) AS exchangerate
1234 JOIN parts p ON (oi.parts_id = p.id)
1235 JOIN oe o ON (oi.trans_id = o.id)
1236 JOIN vendor ct ON (o.vendor_id = ct.id)
1237 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1239 AND o.vendor_id > 0|;
1244 ORDER BY $sortorder|;
1247 my $sth = $dbh->prepare($query);
1248 $sth->execute || $form->dberror($query);
1250 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1251 push @{ $form->{parts} }, $ref;
1256 # include individual items for assemblies
1257 if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1258 foreach $item (@{ $form->{parts} }) {
1259 push @assemblies, $item;
1260 $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1262 p.sellprice, p.listprice, p.lastcost,
1263 p.rop, p.weight, p.priceupdate,
1264 p.image, p.drawing, p.microfiche
1265 FROM parts p, assembly a
1266 WHERE p.id = a.parts_id
1267 AND a.id = $item->{id}|;
1269 $sth = $dbh->prepare($query);
1270 $sth->execute || $form->dberror($query);
1272 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1273 $ref->{assemblyitem} = 1;
1274 push @assemblies, $ref;
1278 push @assemblies, { id => $item->{id} };
1282 # copy assemblies to $form->{parts}
1283 @{ $form->{parts} } = @assemblies;
1287 $main::lxdebug->leave_sub();
1291 $main::lxdebug->enter_sub();
1293 my ($self, $myconfig, $form) = @_;
1295 my $where = '1 = 1';
1301 foreach my $item (qw(partnumber drawing microfiche make model)) {
1302 if ($form->{$item}) {
1303 $var = $form->like(lc $form->{$item});
1305 # make will build later Bugfix 145
1306 if ($item ne 'make') {
1307 $where .= " AND lower(p.$item) LIKE '$var'";
1312 # special case for description
1313 if ($form->{description}) {
1314 unless ( $form->{bought}
1319 || $form->{quoted}) {
1320 $var = $form->like(lc $form->{description});
1321 $where .= " AND lower(p.description) LIKE '$var'";
1325 # special case for serialnumber
1326 if ($form->{l_serialnumber}) {
1327 if ($form->{serialnumber}) {
1328 $var = $form->like(lc $form->{serialnumber});
1329 $where .= " AND lower(serialnumber) LIKE '$var'";
1334 # items which were never bought, sold or on an order
1335 if ($form->{itemstatus} eq 'orphaned') {
1336 $form->{onhand} = $form->{short} = 0;
1337 $form->{bought} = $form->{sold} = 0;
1338 $form->{onorder} = $form->{ordered} = 0;
1339 $form->{rfq} = $form->{quoted} = 0;
1341 $form->{transdatefrom} = $form->{transdateto} = "";
1343 $where .= " AND p.onhand = 0
1344 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
1345 WHERE p.id = i.parts_id)
1346 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
1347 WHERE p.id = a.parts_id)
1348 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
1349 WHERE p.id = o.parts_id)";
1352 if ($form->{itemstatus} eq 'active') {
1353 $where .= " AND p.obsolete = '0'";
1355 if ($form->{itemstatus} eq 'obsolete') {
1356 $where .= " AND p.obsolete = '1'";
1357 $form->{onhand} = $form->{short} = 0;
1359 if ($form->{itemstatus} eq 'onhand') {
1360 $where .= " AND p.onhand > 0";
1362 if ($form->{itemstatus} eq 'short') {
1363 $where .= " AND p.onhand < p.rop";
1365 if ($form->{make}) {
1366 $var = $form->like(lc $form->{make});
1367 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
1368 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
1370 if ($form->{model}) {
1371 $var = $form->like(lc $form->{model});
1372 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
1373 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
1375 if ($form->{partsgroup}) {
1376 $var = $form->like(lc $form->{partsgroup});
1377 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
1381 # connect to database
1382 my $dbh = $form->dbconnect_noauto($myconfig);
1384 if ($form->{"sellprice"} ne "") {
1386 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"});
1387 if ($form->{"sellprice_type"} eq "percent") {
1388 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1389 $update = "sellprice* $faktor";
1391 $update = "sellprice+$faktor";
1394 $query = qq|UPDATE parts set sellprice=$update WHERE id IN (SELECT p.id
1396 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1401 if ($form->{"listprice"} ne "") {
1403 my $faktor = $form->parse_amount($myconfig,$form->{"listprice"});
1404 if ($form->{"listprice_type"} eq "percent") {
1405 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1406 $update = "listprice* $faktor";
1408 $update = "listprice+$faktor";
1411 $query = qq|UPDATE parts set listprice=$update WHERE id IN (SELECT p.id
1413 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1422 for my $i (1 .. $form->{price_rows}) {
1427 if ($form->{"price_$i"} ne "") {
1429 my $faktor = $form->parse_amount($myconfig,$form->{"price_$i"});
1430 if ($form->{"pricegroup_type_$i"} eq "percent") {
1431 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1432 $update = "price* $faktor";
1434 $update = "price+$faktor";
1437 $query = qq|UPDATE prices set price=$update WHERE parts_id IN (SELECT p.id
1439 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1440 WHERE $where) AND pricegroup_id=$form->{"pricegroup_id_$i"}|;
1448 my $rc= $dbh->commit;
1450 $main::lxdebug->leave_sub();
1456 $main::lxdebug->enter_sub();
1458 my ($self, $module, $myconfig, $form) = @_;
1460 # connect to database
1461 my $dbh = $form->dbconnect($myconfig);
1464 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1465 p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1466 FROM chart c, parts p
1467 WHERE c.link LIKE '%$module%'
1468 AND p.id = $form->{id}
1471 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1472 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1473 FROM chart c, defaults d
1474 WHERE c.link LIKE '%$module%'
1478 my $sth = $dbh->prepare($query);
1479 $sth->execute || $form->dberror($query);
1480 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1481 foreach my $key (split /:/, $ref->{link}) {
1482 if ($key =~ /$module/) {
1483 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1484 || ($ref->{id} eq $ref->{income_accno_id})
1485 || ($ref->{id} eq $ref->{expense_accno_id})) {
1486 push @{ $form->{"${module}_links"}{$key} },
1487 { accno => $ref->{accno},
1488 description => $ref->{description},
1489 selected => "selected" };
1490 $form->{"${key}_default"} = "$ref->{accno}--$ref->{description}";
1492 push @{ $form->{"${module}_links"}{$key} },
1493 { accno => $ref->{accno},
1494 description => $ref->{description},
1502 # get buchungsgruppen
1503 $query = qq|SELECT id, description
1504 FROM buchungsgruppen|;
1505 $sth = $dbh->prepare($query);
1506 $sth->execute || $form->dberror($query);
1508 $form->{BUCHUNGSGRUPPEN} = [];
1509 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1510 push @{ $form->{BUCHUNGSGRUPPEN} }, $ref;
1515 $query = qq|SELECT id, adr_description, adr_code
1517 $sth = $dbh->prepare($query);
1518 $sth->execute || $form->dberror($query);
1522 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1523 push @{ $form->{ADR} }, $ref;
1528 $query = qq|SELECT id, description
1531 $sth = $dbh->prepare($query);
1532 $sth->execute || $form->dberror($query);
1534 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1535 push @{ $self->{payment_terms} }, $ref;
1540 $query = qq|SELECT weightunit
1542 $sth = $dbh->prepare($query);
1543 $sth->execute || $form->dberror($query);
1545 ($form->{weightunit}) = $sth->fetchrow_array;
1549 $query = qq|SELECT weightunit, current_date
1551 $sth = $dbh->prepare($query);
1552 $sth->execute || $form->dberror($query);
1554 ($form->{weightunit}, $form->{priceupdate}) = $sth->fetchrow_array;
1559 $main::lxdebug->leave_sub();
1562 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1564 $main::lxdebug->enter_sub();
1566 my ($self, $myconfig, $form, $sortorder) = @_;
1567 my $dbh = $form->dbconnect($myconfig);
1568 my $order = " p.partnumber";
1569 my $where = "1 = 1";
1571 if ($sortorder eq "all") {
1572 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1573 $where .= " AND p.description LIKE '%$form->{description}%'";
1575 if ($sortorder eq "partnumber") {
1576 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1577 $order = qq|p.$sortorder|;
1579 if ($sortorder eq "description") {
1580 $where .= " AND p.description LIKE '%$form->{description}%'";
1585 qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
1586 my $sth = $dbh->prepare($query);
1587 $sth->execute || $self->dberror($query);
1589 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1590 if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1593 $form->{"id_$j"} = $ref->{id};
1594 $form->{"partnumber_$j"} = $ref->{partnumber};
1595 $form->{"description_$j"} = $ref->{description};
1596 $form->{"unit_$j"} = $ref->{unit};
1597 $form->{"sellprice_$j"} = $ref->{sellprice};
1598 $form->{"soldtotal_$j"} = get_soldtotal($dbh, $ref->{id});
1605 $main::lxdebug->leave_sub();
1610 # gets sum of sold part with part_id
1612 $main::lxdebug->enter_sub();
1614 my ($dbh, $id) = @_;
1617 qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
1619 my $sth = $dbh->prepare($query);
1620 $sth->execute || $form->dberror($query);
1623 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1625 $sum = $ref->{totalsold};
1629 if ($sum eq undef) {
1633 $main::lxdebug->leave_sub();
1636 } #end get_soldtotal
1639 $main::lxdebug->enter_sub();
1641 my ($self, $myconfig, $form) = @_;
1642 my $i = $form->{rowcount};
1643 my $where = "NOT p.obsolete = '1'";
1645 if ($form->{"partnumber_$i"}) {
1646 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1647 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1649 if ($form->{"description_$i"}) {
1650 my $description = $form->like(lc $form->{"description_$i"});
1651 $where .= " AND lower(p.description) LIKE '$description'";
1654 if ($form->{"partsgroup_$i"}) {
1655 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1656 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1659 if ($form->{"description_$i"}) {
1660 $where .= " ORDER BY description";
1662 $where .= " ORDER BY partnumber";
1665 # connect to database
1666 my $dbh = $form->dbconnect($myconfig);
1668 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1670 c1.accno AS inventory_accno,
1671 c2.accno AS income_accno,
1672 c3.accno AS expense_accno,
1673 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1676 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1677 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1678 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1679 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1681 my $sth = $dbh->prepare($query);
1682 $sth->execute || $form->dberror($query);
1684 #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1686 # get tax rates and description
1687 #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
1688 #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1689 # FROM chart c, tax t
1690 # WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1692 # $stw = $dbh->prepare($query);
1693 #$stw->execute || $form->dberror($query);
1695 #$ref->{taxaccounts} = "";
1696 #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1698 # $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1699 # $form->{"$ptr->{accno}_description"} = $ptr->{description};
1700 # $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1701 # $form->{taxaccounts} .= "$ptr->{accno} ";
1702 # $ref->{taxaccounts} .= "$ptr->{accno} ";
1707 #chop $ref->{taxaccounts};
1709 push @{ $form->{item_list} }, $ref;
1715 $main::lxdebug->leave_sub();
1718 sub retrieve_languages {
1719 $main::lxdebug->enter_sub();
1721 my ($self, $myconfig, $form) = @_;
1723 # connect to database
1724 my $dbh = $form->dbconnect($myconfig);
1727 $where .= "tr.parts_id=$form->{id}";
1731 if ($form->{language_values} ne "") {
1732 $query = qq|SELECT l.id, l.description, tr.translation, tr.longdescription
1733 FROM language l LEFT OUTER JOIN translation tr ON (tr.language_id=l.id AND $where)|;
1735 $query = qq|SELECT l.id, l.description
1738 my $sth = $dbh->prepare($query);
1739 $sth->execute || $form->dberror($query);
1741 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1742 push(@{$languages}, $ref);
1748 $main::lxdebug->leave_sub();