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 model)) {
908 if ($form->{$item}) {
909 $var = $form->like(lc $form->{$item});
911 # make will build later Bugfix 145
912 if ($item ne 'make') {
913 $where .= " AND lower(p.$item) LIKE '$var'";
918 # special case for description
919 if ($form->{description}) {
920 unless ( $form->{bought}
925 || $form->{quoted}) {
926 $var = $form->like(lc $form->{description});
927 $where .= " AND lower(p.description) LIKE '$var'";
931 # special case for serialnumber
932 if ($form->{l_serialnumber}) {
933 if ($form->{serialnumber}) {
934 $var = $form->like(lc $form->{serialnumber});
935 $where .= " AND lower(serialnumber) LIKE '$var'";
939 if ($form->{searchitems} eq 'part') {
940 $where .= " AND p.inventory_accno_id > 0";
942 if ($form->{searchitems} eq 'assembly') {
943 $form->{bought} = "";
944 $where .= " AND p.assembly = '1'";
946 if ($form->{searchitems} eq 'service') {
947 $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
949 # irrelevant for services
950 $form->{make} = $form->{model} = "";
953 # items which were never bought, sold or on an order
954 if ($form->{itemstatus} eq 'orphaned') {
955 $form->{onhand} = $form->{short} = 0;
956 $form->{bought} = $form->{sold} = 0;
957 $form->{onorder} = $form->{ordered} = 0;
958 $form->{rfq} = $form->{quoted} = 0;
960 $form->{transdatefrom} = $form->{transdateto} = "";
962 $where .= " AND p.onhand = 0
963 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
964 WHERE p.id = i.parts_id)
965 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
966 WHERE p.id = a.parts_id)
967 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
968 WHERE p.id = o.parts_id)";
971 if ($form->{itemstatus} eq 'active') {
972 $where .= " AND p.obsolete = '0'";
974 if ($form->{itemstatus} eq 'obsolete') {
975 $where .= " AND p.obsolete = '1'";
976 $form->{onhand} = $form->{short} = 0;
978 if ($form->{itemstatus} eq 'onhand') {
979 $where .= " AND p.onhand > 0";
981 if ($form->{itemstatus} eq 'short') {
982 $where .= " AND p.onhand < p.rop";
985 $var = $form->like(lc $form->{make});
986 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
987 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
989 if ($form->{model}) {
990 $var = $form->like(lc $form->{model});
991 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
992 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
994 if ($form->{partsgroup}) {
995 $var = $form->like(lc $form->{partsgroup});
996 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
998 if ($form->{l_soldtotal}) {
999 $where .= " AND p.id=i.parts_id AND i.qty >= 0";
1001 " GROUP BY p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
1003 if ($form->{top100}) {
1004 $limit = " LIMIT 100";
1008 if ($form->{revers} == 1) {
1009 $form->{desc} = " DESC";
1014 # connect to database
1015 my $dbh = $form->dbconnect($myconfig);
1017 my $sortorder = $form->{sort};
1018 $sortorder .= $form->{desc};
1019 $sortorder = $form->{sort} if $form->{sort};
1023 if ($form->{l_soldtotal}) {
1024 $form->{soldtotal} = 'soldtotal';
1026 qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
1027 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
1028 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
1034 $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
1035 p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
1036 p.priceupdate, p.image, p.drawing, p.microfiche,
1039 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1042 ORDER BY $sortorder|;
1045 # rebuild query for bought and sold items
1046 if ( $form->{bought}
1051 || $form->{quoted}) {
1053 my @a = qw(partnumber description bin priceupdate name);
1055 push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
1056 push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
1057 push @a, "quonumber" if ($form->{rfq} || $form->{quoted});
1062 if ($form->{bought} || $form->{sold}) {
1064 my $invwhere = "$where";
1065 $invwhere .= " AND i.assemblyitem = '0'";
1066 $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
1067 if $form->{transdatefrom};
1068 $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
1069 if $form->{transdateto};
1071 if ($form->{description}) {
1072 $var = $form->like(lc $form->{description});
1073 $invwhere .= " AND lower(i.description) LIKE '$var'";
1076 my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
1077 i.qty AS onhand, i.unit, p.bin, i.sellprice,
1078 p.listprice, p.lastcost, p.rop, p.weight,
1079 p.priceupdate, p.image, p.drawing, p.microfiche,
1081 a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
1084 if ($form->{bought}) {
1086 SELECT $flds, 'ir' AS module, '' AS type,
1089 JOIN parts p ON (p.id = i.parts_id)
1090 JOIN ap a ON (a.id = i.trans_id)
1091 JOIN vendor ct ON (a.vendor_id = ct.id)
1092 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1098 if ($form->{sold}) {
1100 SELECT $flds, 'is' AS module, '' AS type,
1103 JOIN parts p ON (p.id = i.parts_id)
1104 JOIN ar a ON (a.id = i.trans_id)
1105 JOIN customer ct ON (a.customer_id = ct.id)
1106 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1113 if ($form->{onorder} || $form->{ordered}) {
1114 my $ordwhere = "$where
1115 AND o.quotation = '0'";
1116 $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1117 if $form->{transdatefrom};
1118 $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
1119 if $form->{transdateto};
1121 if ($form->{description}) {
1122 $var = $form->like(lc $form->{description});
1123 $ordwhere .= " AND lower(oi.description) LIKE '$var'";
1127 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1128 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1129 p.listprice, p.lastcost, p.rop, p.weight,
1130 p.priceupdate, p.image, p.drawing, p.microfiche,
1132 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1135 if ($form->{ordered}) {
1137 SELECT $flds, 'oe' AS module, 'sales_order' AS type,
1138 (SELECT buy FROM exchangerate ex
1139 WHERE ex.curr = o.curr
1140 AND ex.transdate = o.transdate) AS exchangerate
1142 JOIN parts p ON (oi.parts_id = p.id)
1143 JOIN oe o ON (oi.trans_id = o.id)
1144 JOIN customer ct ON (o.customer_id = ct.id)
1145 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1147 AND o.customer_id > 0|;
1152 if ($form->{onorder}) {
1154 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1155 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1156 p.listprice, p.lastcost, p.rop, p.weight,
1157 p.priceupdate, p.image, p.drawing, p.microfiche,
1159 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1163 SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
1164 (SELECT sell FROM exchangerate ex
1165 WHERE ex.curr = o.curr
1166 AND ex.transdate = o.transdate) AS exchangerate
1168 JOIN parts p ON (oi.parts_id = p.id)
1169 JOIN oe o ON (oi.trans_id = o.id)
1170 JOIN vendor ct ON (o.vendor_id = ct.id)
1171 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1173 AND o.vendor_id > 0|;
1178 if ($form->{rfq} || $form->{quoted}) {
1179 my $quowhere = "$where
1180 AND o.quotation = '1'";
1181 $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1182 if $form->{transdatefrom};
1183 $quowhere .= " AND o.transdate <= '$form->{transdateto}'"
1184 if $form->{transdateto};
1186 if ($form->{description}) {
1187 $var = $form->like(lc $form->{description});
1188 $quowhere .= " AND lower(oi.description) LIKE '$var'";
1192 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1193 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1194 p.listprice, p.lastcost, p.rop, p.weight,
1195 p.priceupdate, p.image, p.drawing, p.microfiche,
1197 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1200 if ($form->{quoted}) {
1202 SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
1203 (SELECT buy FROM exchangerate ex
1204 WHERE ex.curr = o.curr
1205 AND ex.transdate = o.transdate) AS exchangerate
1207 JOIN parts p ON (oi.parts_id = p.id)
1208 JOIN oe o ON (oi.trans_id = o.id)
1209 JOIN customer ct ON (o.customer_id = ct.id)
1210 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1212 AND o.customer_id > 0|;
1219 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1220 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1221 p.listprice, p.lastcost, p.rop, p.weight,
1222 p.priceupdate, p.image, p.drawing, p.microfiche,
1224 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1228 SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
1229 (SELECT sell FROM exchangerate ex
1230 WHERE ex.curr = o.curr
1231 AND ex.transdate = o.transdate) AS exchangerate
1233 JOIN parts p ON (oi.parts_id = p.id)
1234 JOIN oe o ON (oi.trans_id = o.id)
1235 JOIN vendor ct ON (o.vendor_id = ct.id)
1236 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1238 AND o.vendor_id > 0|;
1243 ORDER BY $sortorder|;
1246 my $sth = $dbh->prepare($query);
1247 $sth->execute || $form->dberror($query);
1249 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1250 push @{ $form->{parts} }, $ref;
1255 # include individual items for assemblies
1256 if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1257 foreach $item (@{ $form->{parts} }) {
1258 push @assemblies, $item;
1259 $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1261 p.sellprice, p.listprice, p.lastcost,
1262 p.rop, p.weight, p.priceupdate,
1263 p.image, p.drawing, p.microfiche
1264 FROM parts p, assembly a
1265 WHERE p.id = a.parts_id
1266 AND a.id = $item->{id}|;
1268 $sth = $dbh->prepare($query);
1269 $sth->execute || $form->dberror($query);
1271 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1272 $ref->{assemblyitem} = 1;
1273 push @assemblies, $ref;
1277 push @assemblies, { id => $item->{id} };
1281 # copy assemblies to $form->{parts}
1282 @{ $form->{parts} } = @assemblies;
1286 $main::lxdebug->leave_sub();
1290 $main::lxdebug->enter_sub();
1292 my ($self, $myconfig, $form) = @_;
1294 my $where = '1 = 1';
1300 foreach my $item (qw(partnumber drawing microfiche make model)) {
1301 if ($form->{$item}) {
1302 $var = $form->like(lc $form->{$item});
1304 # make will build later Bugfix 145
1305 if ($item ne 'make') {
1306 $where .= " AND lower(p.$item) LIKE '$var'";
1311 # special case for description
1312 if ($form->{description}) {
1313 unless ( $form->{bought}
1318 || $form->{quoted}) {
1319 $var = $form->like(lc $form->{description});
1320 $where .= " AND lower(p.description) LIKE '$var'";
1324 # special case for serialnumber
1325 if ($form->{l_serialnumber}) {
1326 if ($form->{serialnumber}) {
1327 $var = $form->like(lc $form->{serialnumber});
1328 $where .= " AND lower(serialnumber) LIKE '$var'";
1333 # items which were never bought, sold or on an order
1334 if ($form->{itemstatus} eq 'orphaned') {
1335 $form->{onhand} = $form->{short} = 0;
1336 $form->{bought} = $form->{sold} = 0;
1337 $form->{onorder} = $form->{ordered} = 0;
1338 $form->{rfq} = $form->{quoted} = 0;
1340 $form->{transdatefrom} = $form->{transdateto} = "";
1342 $where .= " AND p.onhand = 0
1343 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
1344 WHERE p.id = i.parts_id)
1345 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
1346 WHERE p.id = a.parts_id)
1347 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
1348 WHERE p.id = o.parts_id)";
1351 if ($form->{itemstatus} eq 'active') {
1352 $where .= " AND p.obsolete = '0'";
1354 if ($form->{itemstatus} eq 'obsolete') {
1355 $where .= " AND p.obsolete = '1'";
1356 $form->{onhand} = $form->{short} = 0;
1358 if ($form->{itemstatus} eq 'onhand') {
1359 $where .= " AND p.onhand > 0";
1361 if ($form->{itemstatus} eq 'short') {
1362 $where .= " AND p.onhand < p.rop";
1364 if ($form->{make}) {
1365 $var = $form->like(lc $form->{make});
1366 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
1367 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
1369 if ($form->{model}) {
1370 $var = $form->like(lc $form->{model});
1371 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
1372 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
1374 if ($form->{partsgroup}) {
1375 $var = $form->like(lc $form->{partsgroup});
1376 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
1380 # connect to database
1381 my $dbh = $form->dbconnect_noauto($myconfig);
1383 if ($form->{"sellprice"} ne "") {
1385 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"});
1386 if ($form->{"sellprice_type"} eq "percent") {
1387 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1388 $update = "sellprice* $faktor";
1390 $update = "sellprice+$faktor";
1393 $query = qq|UPDATE parts set sellprice=$update WHERE id IN (SELECT p.id
1395 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1400 if ($form->{"listprice"} ne "") {
1402 my $faktor = $form->parse_amount($myconfig,$form->{"listprice"});
1403 if ($form->{"listprice_type"} eq "percent") {
1404 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1405 $update = "listprice* $faktor";
1407 $update = "listprice+$faktor";
1410 $query = qq|UPDATE parts set listprice=$update WHERE id IN (SELECT p.id
1412 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1421 for my $i (1 .. $form->{price_rows}) {
1426 if ($form->{"price_$i"} ne "") {
1428 my $faktor = $form->parse_amount($myconfig,$form->{"price_$i"});
1429 if ($form->{"pricegroup_type_$i"} eq "percent") {
1430 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1431 $update = "price* $faktor";
1433 $update = "price+$faktor";
1436 $query = qq|UPDATE prices set price=$update WHERE parts_id IN (SELECT p.id
1438 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1439 WHERE $where) AND pricegroup_id=$form->{"pricegroup_id_$i"}|;
1447 my $rc= $dbh->commit;
1449 $main::lxdebug->leave_sub();
1455 $main::lxdebug->enter_sub();
1457 my ($self, $module, $myconfig, $form) = @_;
1459 # connect to database
1460 my $dbh = $form->dbconnect($myconfig);
1463 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1464 p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1465 FROM chart c, parts p
1466 WHERE c.link LIKE '%$module%'
1467 AND p.id = $form->{id}
1470 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1471 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1472 FROM chart c, defaults d
1473 WHERE c.link LIKE '%$module%'
1477 my $sth = $dbh->prepare($query);
1478 $sth->execute || $form->dberror($query);
1479 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1480 foreach my $key (split /:/, $ref->{link}) {
1481 if ($key =~ /$module/) {
1482 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1483 || ($ref->{id} eq $ref->{income_accno_id})
1484 || ($ref->{id} eq $ref->{expense_accno_id})) {
1485 push @{ $form->{"${module}_links"}{$key} },
1486 { accno => $ref->{accno},
1487 description => $ref->{description},
1488 selected => "selected" };
1489 $form->{"${key}_default"} = "$ref->{accno}--$ref->{description}";
1491 push @{ $form->{"${module}_links"}{$key} },
1492 { accno => $ref->{accno},
1493 description => $ref->{description},
1501 # get buchungsgruppen
1502 $query = qq|SELECT id, description
1503 FROM buchungsgruppen|;
1504 $sth = $dbh->prepare($query);
1505 $sth->execute || $form->dberror($query);
1507 $form->{BUCHUNGSGRUPPEN} = [];
1508 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1509 push @{ $form->{BUCHUNGSGRUPPEN} }, $ref;
1514 $query = qq|SELECT id, adr_description, adr_code
1516 $sth = $dbh->prepare($query);
1517 $sth->execute || $form->dberror($query);
1521 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1522 push @{ $form->{ADR} }, $ref;
1527 $query = qq|SELECT id, description
1530 $sth = $dbh->prepare($query);
1531 $sth->execute || $form->dberror($query);
1533 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1534 push @{ $self->{payment_terms} }, $ref;
1539 $query = qq|SELECT weightunit
1541 $sth = $dbh->prepare($query);
1542 $sth->execute || $form->dberror($query);
1544 ($form->{weightunit}) = $sth->fetchrow_array;
1548 $query = qq|SELECT weightunit, current_date
1550 $sth = $dbh->prepare($query);
1551 $sth->execute || $form->dberror($query);
1553 ($form->{weightunit}, $form->{priceupdate}) = $sth->fetchrow_array;
1558 $main::lxdebug->leave_sub();
1561 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1563 $main::lxdebug->enter_sub();
1565 my ($self, $myconfig, $form, $sortorder) = @_;
1566 my $dbh = $form->dbconnect($myconfig);
1567 my $order = " p.partnumber";
1568 my $where = "1 = 1";
1570 if ($sortorder eq "all") {
1571 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1572 $where .= " AND p.description LIKE '%$form->{description}%'";
1574 if ($sortorder eq "partnumber") {
1575 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1576 $order = qq|p.$sortorder|;
1578 if ($sortorder eq "description") {
1579 $where .= " AND p.description LIKE '%$form->{description}%'";
1584 qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
1585 my $sth = $dbh->prepare($query);
1586 $sth->execute || $self->dberror($query);
1588 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1589 if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1592 $form->{"id_$j"} = $ref->{id};
1593 $form->{"partnumber_$j"} = $ref->{partnumber};
1594 $form->{"description_$j"} = $ref->{description};
1595 $form->{"unit_$j"} = $ref->{unit};
1596 $form->{"sellprice_$j"} = $ref->{sellprice};
1597 $form->{"soldtotal_$j"} = get_soldtotal($dbh, $ref->{id});
1604 $main::lxdebug->leave_sub();
1609 # gets sum of sold part with part_id
1611 $main::lxdebug->enter_sub();
1613 my ($dbh, $id) = @_;
1616 qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
1618 my $sth = $dbh->prepare($query);
1619 $sth->execute || $form->dberror($query);
1622 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1624 $sum = $ref->{totalsold};
1628 if ($sum eq undef) {
1632 $main::lxdebug->leave_sub();
1635 } #end get_soldtotal
1638 $main::lxdebug->enter_sub();
1640 my ($self, $myconfig, $form) = @_;
1641 my $i = $form->{rowcount};
1642 my $where = "NOT p.obsolete = '1'";
1644 if ($form->{"partnumber_$i"}) {
1645 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1646 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1648 if ($form->{"description_$i"}) {
1649 my $description = $form->like(lc $form->{"description_$i"});
1650 $where .= " AND lower(p.description) LIKE '$description'";
1653 if ($form->{"partsgroup_$i"}) {
1654 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1655 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1658 if ($form->{"description_$i"}) {
1659 $where .= " ORDER BY description";
1661 $where .= " ORDER BY partnumber";
1664 # connect to database
1665 my $dbh = $form->dbconnect($myconfig);
1667 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1669 c1.accno AS inventory_accno,
1670 c2.accno AS income_accno,
1671 c3.accno AS expense_accno,
1672 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1675 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1676 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1677 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1678 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1680 my $sth = $dbh->prepare($query);
1681 $sth->execute || $form->dberror($query);
1683 #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1685 # get tax rates and description
1686 #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
1687 #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1688 # FROM chart c, tax t
1689 # WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1691 # $stw = $dbh->prepare($query);
1692 #$stw->execute || $form->dberror($query);
1694 #$ref->{taxaccounts} = "";
1695 #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1697 # $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1698 # $form->{"$ptr->{accno}_description"} = $ptr->{description};
1699 # $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1700 # $form->{taxaccounts} .= "$ptr->{accno} ";
1701 # $ref->{taxaccounts} .= "$ptr->{accno} ";
1706 #chop $ref->{taxaccounts};
1708 push @{ $form->{item_list} }, $ref;
1714 $main::lxdebug->leave_sub();
1717 sub retrieve_languages {
1718 $main::lxdebug->enter_sub();
1720 my ($self, $myconfig, $form) = @_;
1722 # connect to database
1723 my $dbh = $form->dbconnect($myconfig);
1726 $where .= "tr.parts_id=$form->{id}";
1730 if ($form->{language_values} ne "") {
1731 $query = qq|SELECT l.id, l.description, tr.translation, tr.longdescription
1732 FROM language l LEFT OUTER JOIN translation tr ON (tr.language_id=l.id AND $where)|;
1734 $query = qq|SELECT l.id, l.description
1737 my $sth = $dbh->prepare($query);
1738 $sth->execute || $form->dberror($query);
1740 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1741 push(@{$languages}, $ref);
1747 $main::lxdebug->leave_sub();