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->{not_discountable} *= 1;
376 $form->{payment_id} *= 1;
383 $query = qq|SELECT p.sellprice, p.weight
385 WHERE p.id = $form->{id}|;
386 $sth = $dbh->prepare($query);
387 $sth->execute || $form->dberror($query);
388 my ($sellprice, $weight) = $sth->fetchrow_array;
391 # if item is part of an assembly adjust all assemblies
392 $query = qq|SELECT a.id, a.qty
394 WHERE a.parts_id = $form->{id}|;
395 $sth = $dbh->prepare($query);
396 $sth->execute || $form->dberror($query);
397 while (my ($id, $qty) = $sth->fetchrow_array) {
398 &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
402 if ($form->{item} ne 'service') {
404 # delete makemodel records
405 $query = qq|DELETE FROM makemodel
406 WHERE parts_id = $form->{id}|;
407 $dbh->do($query) || $form->dberror($query);
410 if ($form->{item} eq 'assembly') {
411 if ($form->{onhand} != 0) {
412 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
415 # delete assembly records
416 $query = qq|DELETE FROM assembly
417 WHERE id = $form->{id}|;
418 $dbh->do($query) || $form->dberror($query);
420 $form->{onhand} += $form->{stock};
424 $query = qq|DELETE FROM partstax
425 WHERE parts_id = $form->{id}|;
426 $dbh->do($query) || $form->dberror($query);
428 # delete translations
429 $query = qq|DELETE FROM translation
430 WHERE parts_id = $form->{id}|;
431 $dbh->do($query) || $form->dberror($query);
434 my $uid = rand() . time;
435 $uid .= $form->{login};
437 $query = qq|SELECT p.id FROM parts p
438 WHERE p.partnumber = '$form->{partnumber}'|;
439 $sth = $dbh->prepare($query);
440 $sth->execute || $form->dberror($query);
441 ($form->{id}) = $sth->fetchrow_array;
444 if ($form->{id} ne "") {
445 $main::lxdebug->leave_sub();
448 $query = qq|INSERT INTO parts (partnumber, description)
449 VALUES ('$uid', 'dummy')|;
450 $dbh->do($query) || $form->dberror($query);
452 $query = qq|SELECT p.id FROM parts p
453 WHERE p.partnumber = '$uid'|;
454 $sth = $dbh->prepare($query);
455 $sth->execute || $form->dberror($query);
457 ($form->{id}) = $sth->fetchrow_array;
460 $form->{orphaned} = 1;
461 $form->{onhand} = $form->{stock} if $form->{item} eq 'assembly';
462 if ($form->{partnumber} eq "" && $form->{inventory_accno} eq "") {
463 $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
465 if ($form->{partnumber} eq "" && $form->{inventory_accno} ne "") {
466 $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
470 my $partsgroup_id = 0;
472 if ($form->{partsgroup}) {
473 ($partsgroup, $partsgroup_id) = split /--/, $form->{partsgroup};
476 $query = qq|UPDATE parts SET
477 partnumber = '$form->{partnumber}',
478 description = '$form->{description}',
479 makemodel = '$form->{makemodel}',
480 alternate = '$form->{alternate}',
481 assembly = '$form->{assembly}',
482 listprice = $form->{listprice},
483 sellprice = $form->{sellprice},
484 lastcost = $form->{lastcost},
485 weight = $form->{weight},
486 priceupdate = $form->{priceupdate},
487 unit = '$form->{unit}',
488 notes = '$form->{notes}',
489 formel = '$form->{formel}',
491 bin = '$form->{bin}',
492 buchungsgruppen_id = '$form->{buchungsgruppen_id}',
493 payment_id = '$form->{payment_id}',
494 inventory_accno_id = (SELECT c.id FROM chart c
495 WHERE c.accno = '$form->{inventory_accno}'),
496 income_accno_id = (SELECT c.id FROM chart c
497 WHERE c.accno = '$form->{income_accno}'),
498 expense_accno_id = (SELECT c.id FROM chart c
499 WHERE c.accno = '$form->{expense_accno}'),
500 obsolete = '$form->{obsolete}',
501 image = '$form->{image}',
502 drawing = '$form->{drawing}',
503 shop = '$form->{shop}',
506 alu = '$form->{alu}',
507 not_discountable = '$form->{not_discountable}',
508 microfiche = '$form->{microfiche}',
509 partsgroup_id = $partsgroup_id
510 WHERE id = $form->{id}|;
511 $dbh->do($query) || $form->dberror($query);
513 # delete translation records
514 $query = qq|DELETE FROM translation
515 WHERE parts_id = $form->{id}|;
516 $dbh->do($query) || $form->dberror($query);
518 if ($form->{language_values} ne "") {
519 split /---\+\+\+---/,$form->{language_values};
521 my ($language_id, $translation, $longdescription) = split /--\+\+--/, $item;
522 if ($translation ne "") {
523 $query = qq|INSERT into translation (parts_id, language_id, translation, longdescription) VALUES
524 ($form->{id}, $language_id, | . $dbh->quote($translation) . qq|, | . $dbh->quote($longdescription) . qq| )|;
525 $dbh->do($query) || $form->dberror($query);
529 # delete price records
530 $query = qq|DELETE FROM prices
531 WHERE parts_id = $form->{id}|;
532 $dbh->do($query) || $form->dberror($query);
533 # insert price records only if different to sellprice
534 for my $i (1 .. $form->{price_rows}) {
535 if ($form->{"price_$i"} eq "0") {
536 $form->{"price_$i"} = $form->{sellprice};
539 ( $form->{"price_$i"}
540 || $form->{"klass_$i"}
541 || $form->{"pricegroup_id_$i"})
542 and $form->{"price_$i"} != $form->{sellprice}
544 $klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
545 $price = $form->parse_amount($myconfig, $form->{"price_$i"});
547 $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
548 $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price)
549 VALUES($form->{id},$pricegroup_id,$price)|;
550 $dbh->do($query) || $form->dberror($query);
554 # insert makemodel records
555 unless ($form->{item} eq 'service') {
556 for my $i (1 .. $form->{makemodel_rows}) {
557 if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
558 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
560 $query = qq|INSERT INTO makemodel (parts_id, make, model)
562 '$form->{"make_$i"}', '$form->{"model_$i"}')|;
563 $dbh->do($query) || $form->dberror($query);
569 foreach $item (split / /, $form->{taxaccounts}) {
570 if ($form->{"IC_tax_$item"}) {
571 $query = qq|INSERT INTO partstax (parts_id, chart_id)
575 WHERE c.accno = '$item'))|;
576 $dbh->do($query) || $form->dberror($query);
580 # add assembly records
581 if ($form->{item} eq 'assembly') {
583 for my $i (1 .. $form->{assembly_rows}) {
584 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
586 if ($form->{"qty_$i"} != 0) {
587 $form->{"bom_$i"} *= 1;
588 $query = qq|INSERT INTO assembly (id, parts_id, qty, bom)
589 VALUES ($form->{id}, $form->{"id_$i"},
590 $form->{"qty_$i"}, '$form->{"bom_$i"}')|;
591 $dbh->do($query) || $form->dberror($query);
595 # adjust onhand for the parts
596 if ($form->{onhand} != 0) {
597 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
603 my $shippingdate = "$a[5]-$a[4]-$a[3]";
605 $form->get_employee($dbh);
607 # add inventory record
608 $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
609 shippingdate, employee_id) VALUES (
610 0, $form->{id}, $form->{stock}, '$shippingdate',
611 $form->{employee_id})|;
612 $dbh->do($query) || $form->dberror($query);
616 #set expense_accno=inventory_accno if they are different => bilanz
618 ($form->{expense_accno} != $form->{inventory_accno})
619 ? $form->{inventory_accno}
620 : $form->{expense_accno};
622 # get tax rates and description
624 ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
625 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
627 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
629 $stw = $dbh->prepare($query);
631 $stw->execute || $form->dberror($query);
633 $form->{taxaccount} = "";
634 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
636 # if ($customertax{$ref->{accno}}) {
637 $form->{taxaccount} .= "$ptr->{accno} ";
638 if (!($form->{taxaccount2} =~ /$ptr->{accno}/)) {
639 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
640 $form->{"$ptr->{accno}_description"} = $ptr->{description};
641 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
642 $form->{taxaccount2} .= " $ptr->{accno} ";
648 my $rc = $dbh->commit;
651 $main::lxdebug->leave_sub();
656 sub update_assembly {
657 $main::lxdebug->enter_sub();
659 my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
661 my $query = qq|SELECT a.id, a.qty
663 WHERE a.parts_id = $id|;
664 my $sth = $dbh->prepare($query);
665 $sth->execute || $form->dberror($query);
667 while (my ($pid, $aqty) = $sth->fetchrow_array) {
668 &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
672 $query = qq|UPDATE parts
673 SET sellprice = sellprice +
674 $qty * ($form->{sellprice} - $sellprice),
676 $qty * ($form->{weight} - $weight)
678 $dbh->do($query) || $form->dberror($query);
680 $main::lxdebug->leave_sub();
683 sub retrieve_assemblies {
684 $main::lxdebug->enter_sub();
686 my ($self, $myconfig, $form) = @_;
688 # connect to database
689 my $dbh = $form->dbconnect($myconfig);
693 if ($form->{partnumber}) {
694 my $partnumber = $form->like(lc $form->{partnumber});
695 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
698 if ($form->{description}) {
699 my $description = $form->like(lc $form->{description});
700 $where .= " AND lower(p.description) LIKE '$description'";
702 $where .= " AND NOT p.obsolete = '1'";
704 # retrieve assembly items
705 my $query = qq|SELECT p.id, p.partnumber, p.description,
706 p.bin, p.onhand, p.rop,
707 (SELECT sum(p2.inventory_accno_id)
708 FROM parts p2, assembly a
709 WHERE p2.id = a.parts_id
710 AND a.id = p.id) AS inventory
715 my $sth = $dbh->prepare($query);
716 $sth->execute || $form->dberror($query);
718 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
719 push @{ $form->{assembly_items} }, $ref if $ref->{inventory};
725 $main::lxdebug->leave_sub();
728 sub restock_assemblies {
729 $main::lxdebug->enter_sub();
731 my ($self, $myconfig, $form) = @_;
733 # connect to database
734 my $dbh = $form->dbconnect_noauto($myconfig);
736 for my $i (1 .. $form->{rowcount}) {
738 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
740 if ($form->{"qty_$i"} != 0) {
741 &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
746 my $rc = $dbh->commit;
749 $main::lxdebug->leave_sub();
754 sub adjust_inventory {
755 $main::lxdebug->enter_sub();
757 my ($dbh, $form, $id, $qty) = @_;
759 my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
760 FROM parts p, assembly a
761 WHERE a.parts_id = p.id
763 my $sth = $dbh->prepare($query);
764 $sth->execute || $form->dberror($query);
766 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
768 my $allocate = $qty * $ref->{qty};
770 # is it a service item, then loop
771 $ref->{inventory_accno_id} *= 1;
772 next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
774 # adjust parts onhand
775 $form->update_balance($dbh, "parts", "onhand",
783 my $rc = $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $qty);
785 $main::lxdebug->leave_sub();
791 $main::lxdebug->enter_sub();
793 my ($self, $myconfig, $form) = @_;
795 # connect to database, turn off AutoCommit
796 my $dbh = $form->dbconnect_noauto($myconfig);
798 # first delete prices of pricegroup
799 my $query = qq|DELETE FROM prices
800 WHERE parts_id = $form->{id}|;
801 $dbh->do($query) || $form->dberror($query);
803 my $query = qq|DELETE FROM parts
804 WHERE id = $form->{id}|;
805 $dbh->do($query) || $form->dberror($query);
807 $query = qq|DELETE FROM partstax
808 WHERE parts_id = $form->{id}|;
809 $dbh->do($query) || $form->dberror($query);
811 # check if it is a part, assembly or service
812 if ($form->{item} ne 'service') {
813 $query = qq|DELETE FROM makemodel
814 WHERE parts_id = $form->{id}|;
815 $dbh->do($query) || $form->dberror($query);
818 if ($form->{item} eq 'assembly') {
821 $query = qq|DELETE FROM inventory
822 WHERE parts_id = $form->{id}|;
823 $dbh->do($query) || $form->dberror($query);
825 $query = qq|DELETE FROM assembly
826 WHERE id = $form->{id}|;
827 $dbh->do($query) || $form->dberror($query);
830 if ($form->{item} eq 'alternate') {
831 $query = qq|DELETE FROM alternate
832 WHERE id = $form->{id}|;
833 $dbh->do($query) || $form->dberror($query);
837 my $rc = $dbh->commit;
840 $main::lxdebug->leave_sub();
846 $main::lxdebug->enter_sub();
848 my ($self, $myconfig, $form) = @_;
850 my $i = $form->{assembly_rows};
854 if ($form->{"partnumber_$i"}) {
855 $var = $form->like(lc $form->{"partnumber_$i"});
856 $where .= " AND lower(p.partnumber) LIKE '$var'";
858 if ($form->{"description_$i"}) {
859 $var = $form->like(lc $form->{"description_$i"});
860 $where .= " AND lower(p.description) LIKE '$var'";
862 if ($form->{"partsgroup_$i"}) {
863 $var = $form->like(lc $form->{"partsgroup_$i"});
864 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
868 $where .= " AND NOT p.id = $form->{id}";
872 $where .= " ORDER BY p.partnumber";
874 $where .= " ORDER BY p.description";
877 # connect to database
878 my $dbh = $form->dbconnect($myconfig);
880 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
881 p.weight, p.onhand, p.unit,
884 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
886 my $sth = $dbh->prepare($query);
887 $sth->execute || $form->dberror($query);
889 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
890 push @{ $form->{item_list} }, $ref;
896 $main::lxdebug->leave_sub();
900 $main::lxdebug->enter_sub();
902 my ($self, $myconfig, $form) = @_;
910 foreach my $item (qw(partnumber drawing microfiche)) {
911 if ($form->{$item}) {
912 $var = $form->like(lc $form->{$item});
913 $where .= " AND lower(p.$item) LIKE '$var'";
917 # special case for description
918 if ($form->{description}) {
919 unless ( $form->{bought}
924 || $form->{quoted}) {
925 $var = $form->like(lc $form->{description});
926 $where .= " AND lower(p.description) LIKE '$var'";
930 # special case for serialnumber
931 if ($form->{l_serialnumber}) {
932 if ($form->{serialnumber}) {
933 $var = $form->like(lc $form->{serialnumber});
934 $where .= " AND lower(serialnumber) LIKE '$var'";
938 if ($form->{searchitems} eq 'part') {
939 $where .= " AND p.inventory_accno_id > 0";
941 if ($form->{searchitems} eq 'assembly') {
942 $form->{bought} = "";
943 $where .= " AND p.assembly = '1'";
945 if ($form->{searchitems} eq 'service') {
946 $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
948 # irrelevant for services
949 $form->{make} = $form->{model} = "";
952 # items which were never bought, sold or on an order
953 if ($form->{itemstatus} eq 'orphaned') {
954 $form->{onhand} = $form->{short} = 0;
955 $form->{bought} = $form->{sold} = 0;
956 $form->{onorder} = $form->{ordered} = 0;
957 $form->{rfq} = $form->{quoted} = 0;
959 $form->{transdatefrom} = $form->{transdateto} = "";
961 $where .= " AND p.onhand = 0
962 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
963 WHERE p.id = i.parts_id)
964 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
965 WHERE p.id = a.parts_id)
966 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
967 WHERE p.id = o.parts_id)";
970 if ($form->{itemstatus} eq 'active') {
971 $where .= " AND p.obsolete = '0'";
973 if ($form->{itemstatus} eq 'obsolete') {
974 $where .= " AND p.obsolete = '1'";
975 $form->{onhand} = $form->{short} = 0;
977 if ($form->{itemstatus} eq 'onhand') {
978 $where .= " AND p.onhand > 0";
980 if ($form->{itemstatus} eq 'short') {
981 $where .= " AND p.onhand < p.rop";
984 $var = $form->like(lc $form->{make});
985 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
986 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
988 if ($form->{model}) {
989 $var = $form->like(lc $form->{model});
990 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
991 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
993 if ($form->{partsgroup}) {
994 $var = $form->like(lc $form->{partsgroup});
995 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
997 if ($form->{l_soldtotal}) {
998 $where .= " AND p.id=i.parts_id AND i.qty >= 0";
1000 " GROUP BY p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
1002 if ($form->{top100}) {
1003 $limit = " LIMIT 100";
1007 if ($form->{revers} == 1) {
1008 $form->{desc} = " DESC";
1013 # connect to database
1014 my $dbh = $form->dbconnect($myconfig);
1016 my $sortorder = $form->{sort};
1017 $sortorder .= $form->{desc};
1018 $sortorder = $form->{sort} if $form->{sort};
1022 if ($form->{l_soldtotal}) {
1023 $form->{soldtotal} = 'soldtotal';
1025 qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
1026 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
1027 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
1033 $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
1034 p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
1035 p.priceupdate, p.image, p.drawing, p.microfiche,
1038 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1041 ORDER BY $sortorder|;
1044 # rebuild query for bought and sold items
1045 if ( $form->{bought}
1050 || $form->{quoted}) {
1052 my @a = qw(partnumber description bin priceupdate name);
1054 push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
1055 push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
1056 push @a, "quonumber" if ($form->{rfq} || $form->{quoted});
1061 if ($form->{bought} || $form->{sold}) {
1063 my $invwhere = "$where";
1064 $invwhere .= " AND i.assemblyitem = '0'";
1065 $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
1066 if $form->{transdatefrom};
1067 $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
1068 if $form->{transdateto};
1070 if ($form->{description}) {
1071 $var = $form->like(lc $form->{description});
1072 $invwhere .= " AND lower(i.description) LIKE '$var'";
1075 my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
1076 i.qty AS onhand, i.unit, p.bin, i.sellprice,
1077 p.listprice, p.lastcost, p.rop, p.weight,
1078 p.priceupdate, p.image, p.drawing, p.microfiche,
1080 a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
1081 ct.name, i.deliverydate|;
1083 if ($form->{bought}) {
1085 SELECT $flds, 'ir' AS module, '' AS type,
1088 JOIN parts p ON (p.id = i.parts_id)
1089 JOIN ap a ON (a.id = i.trans_id)
1090 JOIN vendor ct ON (a.vendor_id = ct.id)
1091 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1097 if ($form->{sold}) {
1099 SELECT $flds, 'is' AS module, '' AS type,
1102 JOIN parts p ON (p.id = i.parts_id)
1103 JOIN ar a ON (a.id = i.trans_id)
1104 JOIN customer ct ON (a.customer_id = ct.id)
1105 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1112 if ($form->{onorder} || $form->{ordered}) {
1113 my $ordwhere = "$where
1114 AND o.quotation = '0'";
1115 $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1116 if $form->{transdatefrom};
1117 $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
1118 if $form->{transdateto};
1120 if ($form->{description}) {
1121 $var = $form->like(lc $form->{description});
1122 $ordwhere .= " AND lower(oi.description) LIKE '$var'";
1126 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1127 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1128 p.listprice, p.lastcost, p.rop, p.weight,
1129 p.priceupdate, p.image, p.drawing, p.microfiche,
1131 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1134 if ($form->{ordered}) {
1136 SELECT $flds, 'oe' AS module, 'sales_order' AS type,
1137 (SELECT buy FROM exchangerate ex
1138 WHERE ex.curr = o.curr
1139 AND ex.transdate = o.transdate) AS exchangerate
1141 JOIN parts p ON (oi.parts_id = p.id)
1142 JOIN oe o ON (oi.trans_id = o.id)
1143 JOIN customer ct ON (o.customer_id = ct.id)
1144 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1146 AND o.customer_id > 0|;
1151 if ($form->{onorder}) {
1153 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1154 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1155 p.listprice, p.lastcost, p.rop, p.weight,
1156 p.priceupdate, p.image, p.drawing, p.microfiche,
1158 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1162 SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
1163 (SELECT sell FROM exchangerate ex
1164 WHERE ex.curr = o.curr
1165 AND ex.transdate = o.transdate) AS exchangerate
1167 JOIN parts p ON (oi.parts_id = p.id)
1168 JOIN oe o ON (oi.trans_id = o.id)
1169 JOIN vendor ct ON (o.vendor_id = ct.id)
1170 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1172 AND o.vendor_id > 0|;
1177 if ($form->{rfq} || $form->{quoted}) {
1178 my $quowhere = "$where
1179 AND o.quotation = '1'";
1180 $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1181 if $form->{transdatefrom};
1182 $quowhere .= " AND o.transdate <= '$form->{transdateto}'"
1183 if $form->{transdateto};
1185 if ($form->{description}) {
1186 $var = $form->like(lc $form->{description});
1187 $quowhere .= " AND lower(oi.description) LIKE '$var'";
1191 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1192 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1193 p.listprice, p.lastcost, p.rop, p.weight,
1194 p.priceupdate, p.image, p.drawing, p.microfiche,
1196 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1199 if ($form->{quoted}) {
1201 SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
1202 (SELECT buy FROM exchangerate ex
1203 WHERE ex.curr = o.curr
1204 AND ex.transdate = o.transdate) AS exchangerate
1206 JOIN parts p ON (oi.parts_id = p.id)
1207 JOIN oe o ON (oi.trans_id = o.id)
1208 JOIN customer ct ON (o.customer_id = ct.id)
1209 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1211 AND o.customer_id > 0|;
1218 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1219 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1220 p.listprice, p.lastcost, p.rop, p.weight,
1221 p.priceupdate, p.image, p.drawing, p.microfiche,
1223 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1227 SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
1228 (SELECT sell FROM exchangerate ex
1229 WHERE ex.curr = o.curr
1230 AND ex.transdate = o.transdate) AS exchangerate
1232 JOIN parts p ON (oi.parts_id = p.id)
1233 JOIN oe o ON (oi.trans_id = o.id)
1234 JOIN vendor ct ON (o.vendor_id = ct.id)
1235 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1237 AND o.vendor_id > 0|;
1242 ORDER BY $sortorder|;
1245 my $sth = $dbh->prepare($query);
1246 $sth->execute || $form->dberror($query);
1248 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1249 push @{ $form->{parts} }, $ref;
1254 # include individual items for assemblies
1255 if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1256 foreach $item (@{ $form->{parts} }) {
1257 push @assemblies, $item;
1258 $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1260 p.sellprice, p.listprice, p.lastcost,
1261 p.rop, p.weight, p.priceupdate,
1262 p.image, p.drawing, p.microfiche
1263 FROM parts p, assembly a
1264 WHERE p.id = a.parts_id
1265 AND a.id = $item->{id}|;
1267 $sth = $dbh->prepare($query);
1268 $sth->execute || $form->dberror($query);
1270 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1271 $ref->{assemblyitem} = 1;
1272 push @assemblies, $ref;
1276 push @assemblies, { id => $item->{id} };
1280 # copy assemblies to $form->{parts}
1281 @{ $form->{parts} } = @assemblies;
1285 $main::lxdebug->leave_sub();
1289 $main::lxdebug->enter_sub();
1291 my ($self, $myconfig, $form) = @_;
1293 my $where = '1 = 1';
1299 foreach my $item (qw(partnumber drawing microfiche make model)) {
1300 if ($form->{$item}) {
1301 $var = $form->like(lc $form->{$item});
1303 # make will build later Bugfix 145
1304 if ($item ne 'make') {
1305 $where .= " AND lower(p.$item) LIKE '$var'";
1310 # special case for description
1311 if ($form->{description}) {
1312 unless ( $form->{bought}
1317 || $form->{quoted}) {
1318 $var = $form->like(lc $form->{description});
1319 $where .= " AND lower(p.description) LIKE '$var'";
1323 # special case for serialnumber
1324 if ($form->{l_serialnumber}) {
1325 if ($form->{serialnumber}) {
1326 $var = $form->like(lc $form->{serialnumber});
1327 $where .= " AND lower(serialnumber) LIKE '$var'";
1332 # items which were never bought, sold or on an order
1333 if ($form->{itemstatus} eq 'orphaned') {
1334 $form->{onhand} = $form->{short} = 0;
1335 $form->{bought} = $form->{sold} = 0;
1336 $form->{onorder} = $form->{ordered} = 0;
1337 $form->{rfq} = $form->{quoted} = 0;
1339 $form->{transdatefrom} = $form->{transdateto} = "";
1341 $where .= " AND p.onhand = 0
1342 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
1343 WHERE p.id = i.parts_id)
1344 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
1345 WHERE p.id = a.parts_id)
1346 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
1347 WHERE p.id = o.parts_id)";
1350 if ($form->{itemstatus} eq 'active') {
1351 $where .= " AND p.obsolete = '0'";
1353 if ($form->{itemstatus} eq 'obsolete') {
1354 $where .= " AND p.obsolete = '1'";
1355 $form->{onhand} = $form->{short} = 0;
1357 if ($form->{itemstatus} eq 'onhand') {
1358 $where .= " AND p.onhand > 0";
1360 if ($form->{itemstatus} eq 'short') {
1361 $where .= " AND p.onhand < p.rop";
1363 if ($form->{make}) {
1364 $var = $form->like(lc $form->{make});
1365 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
1366 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
1368 if ($form->{model}) {
1369 $var = $form->like(lc $form->{model});
1370 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
1371 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
1373 if ($form->{partsgroup}) {
1374 $var = $form->like(lc $form->{partsgroup});
1375 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
1379 # connect to database
1380 my $dbh = $form->dbconnect_noauto($myconfig);
1382 if ($form->{"sellprice"} ne "") {
1384 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"});
1385 if ($form->{"sellprice_type"} eq "percent") {
1386 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1387 $update = "sellprice* $faktor";
1389 $update = "sellprice+$faktor";
1392 $query = qq|UPDATE parts set sellprice=$update WHERE id IN (SELECT p.id
1394 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1399 if ($form->{"listprice"} ne "") {
1401 my $faktor = $form->parse_amount($myconfig,$form->{"listprice"});
1402 if ($form->{"listprice_type"} eq "percent") {
1403 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1404 $update = "listprice* $faktor";
1406 $update = "listprice+$faktor";
1409 $query = qq|UPDATE parts set listprice=$update WHERE id IN (SELECT p.id
1411 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1420 for my $i (1 .. $form->{price_rows}) {
1425 if ($form->{"price_$i"} ne "") {
1427 my $faktor = $form->parse_amount($myconfig,$form->{"price_$i"});
1428 if ($form->{"pricegroup_type_$i"} eq "percent") {
1429 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1430 $update = "price* $faktor";
1432 $update = "price+$faktor";
1435 $query = qq|UPDATE prices set price=$update WHERE parts_id IN (SELECT p.id
1437 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1438 WHERE $where) AND pricegroup_id=$form->{"pricegroup_id_$i"}|;
1446 my $rc= $dbh->commit;
1448 $main::lxdebug->leave_sub();
1454 $main::lxdebug->enter_sub();
1456 my ($self, $module, $myconfig, $form) = @_;
1458 # connect to database
1459 my $dbh = $form->dbconnect($myconfig);
1462 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1463 p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1464 FROM chart c, parts p
1465 WHERE c.link LIKE '%$module%'
1466 AND p.id = $form->{id}
1469 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1470 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1471 FROM chart c, defaults d
1472 WHERE c.link LIKE '%$module%'
1476 my $sth = $dbh->prepare($query);
1477 $sth->execute || $form->dberror($query);
1478 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1479 foreach my $key (split /:/, $ref->{link}) {
1480 if ($key =~ /$module/) {
1481 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1482 || ($ref->{id} eq $ref->{income_accno_id})
1483 || ($ref->{id} eq $ref->{expense_accno_id})) {
1484 push @{ $form->{"${module}_links"}{$key} },
1485 { accno => $ref->{accno},
1486 description => $ref->{description},
1487 selected => "selected" };
1488 $form->{"${key}_default"} = "$ref->{accno}--$ref->{description}";
1490 push @{ $form->{"${module}_links"}{$key} },
1491 { accno => $ref->{accno},
1492 description => $ref->{description},
1500 # get buchungsgruppen
1501 $query = qq|SELECT id, description
1502 FROM buchungsgruppen|;
1503 $sth = $dbh->prepare($query);
1504 $sth->execute || $form->dberror($query);
1506 $form->{BUCHUNGSGRUPPEN} = [];
1507 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1508 push @{ $form->{BUCHUNGSGRUPPEN} }, $ref;
1513 $query = qq|SELECT id, description
1516 $sth = $dbh->prepare($query);
1517 $sth->execute || $form->dberror($query);
1519 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1520 push @{ $self->{payment_terms} }, $ref;
1525 $query = qq|SELECT weightunit
1527 $sth = $dbh->prepare($query);
1528 $sth->execute || $form->dberror($query);
1530 ($form->{weightunit}) = $sth->fetchrow_array;
1534 $query = qq|SELECT weightunit, current_date
1536 $sth = $dbh->prepare($query);
1537 $sth->execute || $form->dberror($query);
1539 ($form->{weightunit}, $form->{priceupdate}) = $sth->fetchrow_array;
1544 $main::lxdebug->leave_sub();
1547 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1549 $main::lxdebug->enter_sub();
1551 my ($self, $myconfig, $form, $sortorder) = @_;
1552 my $dbh = $form->dbconnect($myconfig);
1553 my $order = " p.partnumber";
1554 my $where = "1 = 1";
1556 if ($sortorder eq "all") {
1557 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1558 $where .= " AND p.description LIKE '%$form->{description}%'";
1560 if ($sortorder eq "partnumber") {
1561 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1562 $order = qq|p.$sortorder|;
1564 if ($sortorder eq "description") {
1565 $where .= " AND p.description LIKE '%$form->{description}%'";
1570 qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
1571 my $sth = $dbh->prepare($query);
1572 $sth->execute || $self->dberror($query);
1574 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1575 if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1578 $form->{"id_$j"} = $ref->{id};
1579 $form->{"partnumber_$j"} = $ref->{partnumber};
1580 $form->{"description_$j"} = $ref->{description};
1581 $form->{"unit_$j"} = $ref->{unit};
1582 $form->{"sellprice_$j"} = $ref->{sellprice};
1583 $form->{"soldtotal_$j"} = get_soldtotal($dbh, $ref->{id});
1590 $main::lxdebug->leave_sub();
1595 # gets sum of sold part with part_id
1597 $main::lxdebug->enter_sub();
1599 my ($dbh, $id) = @_;
1602 qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
1604 my $sth = $dbh->prepare($query);
1605 $sth->execute || $form->dberror($query);
1608 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1610 $sum = $ref->{totalsold};
1614 if ($sum eq undef) {
1618 $main::lxdebug->leave_sub();
1621 } #end get_soldtotal
1624 $main::lxdebug->enter_sub();
1626 my ($self, $myconfig, $form) = @_;
1627 my $i = $form->{rowcount};
1628 my $where = "NOT p.obsolete = '1'";
1630 if ($form->{"partnumber_$i"}) {
1631 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1632 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1634 if ($form->{"description_$i"}) {
1635 my $description = $form->like(lc $form->{"description_$i"});
1636 $where .= " AND lower(p.description) LIKE '$description'";
1639 if ($form->{"partsgroup_$i"}) {
1640 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1641 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1644 if ($form->{"description_$i"}) {
1645 $where .= " ORDER BY description";
1647 $where .= " ORDER BY partnumber";
1650 # connect to database
1651 my $dbh = $form->dbconnect($myconfig);
1653 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1655 c1.accno AS inventory_accno,
1656 c2.accno AS income_accno,
1657 c3.accno AS expense_accno,
1658 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1661 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1662 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1663 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1664 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1666 my $sth = $dbh->prepare($query);
1667 $sth->execute || $form->dberror($query);
1669 #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1671 # get tax rates and description
1672 #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
1673 #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1674 # FROM chart c, tax t
1675 # WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1677 # $stw = $dbh->prepare($query);
1678 #$stw->execute || $form->dberror($query);
1680 #$ref->{taxaccounts} = "";
1681 #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1683 # $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1684 # $form->{"$ptr->{accno}_description"} = $ptr->{description};
1685 # $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1686 # $form->{taxaccounts} .= "$ptr->{accno} ";
1687 # $ref->{taxaccounts} .= "$ptr->{accno} ";
1692 #chop $ref->{taxaccounts};
1694 push @{ $form->{item_list} }, $ref;
1700 $main::lxdebug->leave_sub();
1703 sub retrieve_languages {
1704 $main::lxdebug->enter_sub();
1706 my ($self, $myconfig, $form) = @_;
1708 # connect to database
1709 my $dbh = $form->dbconnect($myconfig);
1712 $where .= "tr.parts_id=$form->{id}";
1716 if ($form->{language_values} ne "") {
1717 $query = qq|SELECT l.id, l.description, tr.translation, tr.longdescription
1718 FROM language l LEFT OUTER JOIN translation tr ON (tr.language_id=l.id AND $where)|;
1720 $query = qq|SELECT l.id, l.description
1723 my $sth = $dbh->prepare($query);
1724 $sth->execute || $form->dberror($query);
1726 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1727 push(@{$languages}, $ref);
1733 $main::lxdebug->leave_sub();