1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
31 # Inventory Control backend
33 #======================================================================
39 $main::lxdebug->enter_sub();
41 my ($self, $myconfig, $form) = @_;
44 my $dbh = $form->dbconnect($myconfig);
46 my $query = qq|SELECT p.*,
47 c1.accno AS inventory_accno,
48 c2.accno AS income_accno,
49 c3.accno AS expense_accno,
52 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
53 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
54 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
55 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
56 WHERE p.id = $form->{id}|;
57 my $sth = $dbh->prepare($query);
58 $sth->execute || $form->dberror($query);
59 my $ref = $sth->fetchrow_hashref(NAME_lc);
61 # copy to $form variables
62 map { $form->{$_} = $ref->{$_} } (keys %{$ref});
66 my %oid = ('Pg' => 'a.oid',
67 'Oracle' => 'a.rowid');
69 # part or service item
70 $form->{item} = ($form->{inventory_accno}) ? 'part' : 'service';
71 if ($form->{assembly}) {
72 $form->{item} = 'assembly';
74 # retrieve assembly items
75 $query = qq|SELECT p.id, p.partnumber, p.description,
76 p.sellprice, p.weight, a.qty, a.bom, p.unit,
79 JOIN assembly a ON (a.parts_id = p.id)
80 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
81 WHERE a.id = $form->{id}
82 ORDER BY $oid{$myconfig->{dbdriver}}|;
84 $sth = $dbh->prepare($query);
85 $sth->execute || $form->dberror($query);
87 $form->{assembly_rows} = 0;
88 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
89 $form->{assembly_rows}++;
90 foreach my $key (keys %{$ref}) {
91 $form->{"${key}_$form->{assembly_rows}"} = $ref->{$key};
98 # setup accno hash for <option checked> {amount} is used in create_links
99 $form->{amount}{IC} = $form->{inventory_accno};
100 $form->{amount}{IC_income} = $form->{income_accno};
101 $form->{amount}{IC_sale} = $form->{income_accno};
102 $form->{amount}{IC_expense} = $form->{expense_accno};
103 $form->{amount}{IC_cogs} = $form->{expense_accno};
107 qq|SELECT p.parts_id, p.pricegroup_id, p.price, (SELECT pg.pricegroup FROM pricegroup pg WHERE pg.id=p.pricegroup_id) AS pricegroup FROM prices p
108 WHERE parts_id = $form->{id}
109 ORDER by pricegroup|;
111 $sth = $dbh->prepare($query);
112 $sth->execute || $form->dberror($query);
115 @pricegroups_not_used = ();
120 ($form->{"klass_$i"}, $form->{"pricegroup_id_$i"},
121 $form->{"price_$i"}, $form->{"pricegroup_$i"})
122 = $sth->fetchrow_array
124 $form->{"price_$i"} = $form->round_amount($form->{"price_$i"}, 5);
125 $form->{"price_$i"} =
126 $form->format_amount($myconfig, $form->{"price_$i"}, 5);
127 push @pricegroups, $form->{"pricegroup_id_$i"};
134 $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
136 $pkq = $dbh->prepare($query);
137 $pkq->execute || $form->dberror($query);
138 while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
139 push @{ $form->{PRICEGROUPS} }, $pkr;
143 #find not used pricegroups
144 while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
146 foreach $item (@pricegroups) {
147 if ($item eq $tmp->{id}) {
154 push @pricegroups_not_used, $tmp;
158 # if not used pricegroups are avaible
159 if (@pricegroups_not_used) {
161 foreach $name (@pricegroups_not_used) {
162 $form->{"klass_$i"} = "$name->{id}";
163 $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
164 $form->{"price_$i"} =
165 $form->format_amount($myconfig, $form->{"price_$i"}, 5);
166 $form->{"pricegroup_id_$i"} = "$name->{id}";
167 $form->{"pricegroup_$i"} = "$name->{pricegroup}";
173 $form->{price_rows} = $i - 1;
175 unless ($form->{item} eq 'service') {
178 if ($form->{makemodel}) {
179 $query = qq|SELECT m.make, m.model FROM makemodel m
180 WHERE m.parts_id = $form->{id}|;
182 $sth = $dbh->prepare($query);
183 $sth->execute || $form->dberror($query);
186 while (($form->{"make_$i"}, $form->{"model_$i"}) = $sth->fetchrow_array)
191 $form->{makemodel_rows} = $i - 1;
197 $form->{language_values} = "";
198 $query = qq|SELECT language_id, translation FROM translation WHERE parts_id = $form->{id}|;
199 $trq = $dbh->prepare($query);
200 $trq->execute || $form->dberror($query);
201 while ($tr = $trq->fetchrow_hashref(NAME_lc)) {
202 $form->{language_values} .= "---+++---".$tr->{language_id}."--++--".$tr->{translation};
206 # now get accno for taxes
207 $query = qq|SELECT c.accno
208 FROM chart c, partstax pt
209 WHERE pt.chart_id = c.id
210 AND pt.parts_id = $form->{id}|;
212 $sth = $dbh->prepare($query);
213 $sth->execute || $form->dberror($query);
215 while (($key) = $sth->fetchrow_array) {
216 $form->{amount}{$key} = $key;
222 $query = qq|SELECT i.parts_id
224 WHERE i.parts_id = $form->{id}
228 WHERE o.parts_id = $form->{id}
232 WHERE a.parts_id = $form->{id}|;
233 $sth = $dbh->prepare($query);
234 $sth->execute || $form->dberror($query);
236 ($form->{orphaned}) = $sth->fetchrow_array;
237 $form->{orphaned} = !$form->{orphaned};
240 $form->{"unit_changeable"} = 1;
241 foreach my $table (qw(invoice assembly orderitems inventory license)) {
242 $query = "SELECT COUNT(*) FROM $table WHERE parts_id = ?";
243 my ($count) = $dbh->selectrow_array($query, undef, $form->{"id"});
244 $form->dberror($query . " (" . $form->{"id"} . ")") if ($dbh->err);
247 $form->{"unit_changeable"} = 0;
254 $main::lxdebug->leave_sub();
257 sub get_pricegroups {
258 $main::lxdebug->enter_sub();
260 my ($self, $myconfig, $form) = @_;
261 my $dbh = $form->dbconnect($myconfig);
263 my @pricegroups_not_used = ();
266 my $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
268 my $pkq = $dbh->prepare($query);
269 $pkq->execute || $form->dberror($query);
270 while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
271 push @{ $form->{PRICEGROUPS} }, $pkr;
275 #find not used pricegroups
276 while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
277 push @pricegroups_not_used, $tmp;
280 # if not used pricegroups are avaible
281 if (@pricegroups_not_used) {
283 foreach $name (@pricegroups_not_used) {
284 $form->{"klass_$i"} = "$name->{id}";
285 $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
286 $form->{"price_$i"} =
287 $form->format_amount($myconfig, $form->{"price_$i"}, 5);
288 $form->{"pricegroup_id_$i"} = "$name->{id}";
289 $form->{"pricegroup_$i"} = "$name->{pricegroup}";
295 $form->{price_rows} = $i - 1;
299 $main::lxdebug->leave_sub();
302 sub retrieve_buchungsgruppen {
303 $main::lxdebug->enter_sub();
305 my ($self, $myconfig, $form) = @_;
309 my $dbh = $form->dbconnect($myconfig);
311 # get buchungsgruppen
312 $query = qq|SELECT id, description
313 FROM buchungsgruppen|;
314 $sth = $dbh->prepare($query);
315 $sth->execute || $form->dberror($query);
317 $form->{BUCHUNGSGRUPPEN} = [];
318 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
319 push(@{ $form->{BUCHUNGSGRUPPEN} }, $ref);
323 $main::lxdebug->leave_sub();
327 $main::lxdebug->enter_sub();
329 my ($self, $myconfig, $form) = @_;
330 $form->{IC_expense} = "1000";
331 $form->{IC_income} = "2000";
333 if ($form->{item} ne 'service') {
334 $form->{IC} = $form->{IC_expense};
337 ($form->{inventory_accno}) = split(/--/, $form->{IC});
338 ($form->{expense_accno}) = split(/--/, $form->{IC_expense});
339 ($form->{income_accno}) = split(/--/, $form->{IC_income});
341 # connect to database, turn off AutoCommit
342 my $dbh = $form->dbconnect_noauto($myconfig);
345 # make up a unique handle and store in partnumber field
346 # then retrieve the record based on the unique handle to get the id
347 # replace the partnumber field with the actual variable
348 # add records for makemodel
350 # if there is a $form->{id} then replace the old entry
351 # delete all makemodel entries and add the new ones
354 map { $form->{$_} =~ s/\'/\'\'/g } qw(partnumber description notes unit);
356 # undo amount formatting
357 map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) }
358 qw(rop weight listprice sellprice gv lastcost stock);
360 # set date to NULL if nothing entered
361 $form->{priceupdate} =
362 ($form->{priceupdate}) ? qq|'$form->{priceupdate}'| : "NULL";
364 $form->{makemodel} = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
366 $form->{alternate} = 0;
367 $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
368 $form->{obsolete} *= 1;
370 $form->{onhand} *= 1;
373 $form->{buchungsgruppen_id} *= 1;
374 $form->{not_discountable} *= 1;
375 $form->{payment_id} *= 1;
382 $query = qq|SELECT p.sellprice, p.weight
384 WHERE p.id = $form->{id}|;
385 $sth = $dbh->prepare($query);
386 $sth->execute || $form->dberror($query);
387 my ($sellprice, $weight) = $sth->fetchrow_array;
390 # if item is part of an assembly adjust all assemblies
391 $query = qq|SELECT a.id, a.qty
393 WHERE a.parts_id = $form->{id}|;
394 $sth = $dbh->prepare($query);
395 $sth->execute || $form->dberror($query);
396 while (my ($id, $qty) = $sth->fetchrow_array) {
397 &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
401 if ($form->{item} ne 'service') {
403 # delete makemodel records
404 $query = qq|DELETE FROM makemodel
405 WHERE parts_id = $form->{id}|;
406 $dbh->do($query) || $form->dberror($query);
409 if ($form->{item} eq 'assembly') {
410 if ($form->{onhand} != 0) {
411 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
414 # delete assembly records
415 $query = qq|DELETE FROM assembly
416 WHERE id = $form->{id}|;
417 $dbh->do($query) || $form->dberror($query);
419 $form->{onhand} += $form->{stock};
423 $query = qq|DELETE FROM partstax
424 WHERE parts_id = $form->{id}|;
425 $dbh->do($query) || $form->dberror($query);
427 # delete translations
428 $query = qq|DELETE FROM translation
429 WHERE parts_id = $form->{id}|;
430 $dbh->do($query) || $form->dberror($query);
433 my $uid = rand() . time;
434 $uid .= $form->{login};
436 $query = qq|SELECT p.id FROM parts p
437 WHERE p.partnumber = '$form->{partnumber}'|;
438 $sth = $dbh->prepare($query);
439 $sth->execute || $form->dberror($query);
440 ($form->{id}) = $sth->fetchrow_array;
443 if ($form->{id} ne "") {
444 $main::lxdebug->leave_sub();
447 $query = qq|INSERT INTO parts (partnumber, description)
448 VALUES ('$uid', 'dummy')|;
449 $dbh->do($query) || $form->dberror($query);
451 $query = qq|SELECT p.id FROM parts p
452 WHERE p.partnumber = '$uid'|;
453 $sth = $dbh->prepare($query);
454 $sth->execute || $form->dberror($query);
456 ($form->{id}) = $sth->fetchrow_array;
459 $form->{orphaned} = 1;
460 $form->{onhand} = $form->{stock} if $form->{item} eq 'assembly';
461 if ($form->{partnumber} eq "" && $form->{inventory_accno} eq "") {
462 $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
464 if ($form->{partnumber} eq "" && $form->{inventory_accno} ne "") {
465 $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
469 my $partsgroup_id = 0;
471 if ($form->{partsgroup}) {
472 ($partsgroup, $partsgroup_id) = split /--/, $form->{partsgroup};
475 $query = qq|UPDATE parts SET
476 partnumber = '$form->{partnumber}',
477 description = '$form->{description}',
478 makemodel = '$form->{makemodel}',
479 alternate = '$form->{alternate}',
480 assembly = '$form->{assembly}',
481 listprice = $form->{listprice},
482 sellprice = $form->{sellprice},
483 lastcost = $form->{lastcost},
484 weight = $form->{weight},
485 priceupdate = $form->{priceupdate},
486 unit = '$form->{unit}',
487 notes = '$form->{notes}',
488 formel = '$form->{formel}',
490 bin = '$form->{bin}',
491 buchungsgruppen_id = '$form->{buchungsgruppen_id}',
492 payment_id = '$form->{payment_id}',
493 inventory_accno_id = (SELECT c.id FROM chart c
494 WHERE c.accno = '$form->{inventory_accno}'),
495 income_accno_id = (SELECT c.id FROM chart c
496 WHERE c.accno = '$form->{income_accno}'),
497 expense_accno_id = (SELECT c.id FROM chart c
498 WHERE c.accno = '$form->{expense_accno}'),
499 obsolete = '$form->{obsolete}',
500 image = '$form->{image}',
501 drawing = '$form->{drawing}',
502 shop = '$form->{shop}',
505 ean = '$form->{ean}',
506 not_discountable = '$form->{not_discountable}',
507 microfiche = '$form->{microfiche}',
508 partsgroup_id = $partsgroup_id
509 WHERE id = $form->{id}|;
510 $dbh->do($query) || $form->dberror($query);
512 # delete translation records
513 $query = qq|DELETE FROM translation
514 WHERE parts_id = $form->{id}|;
515 $dbh->do($query) || $form->dberror($query);
517 if ($form->{language_values} ne "") {
518 split /---\+\+\+---/,$form->{language_values};
520 my ($language_id, $translation, $longdescription) = split /--\+\+--/, $item;
521 if ($translation ne "") {
522 $query = qq|INSERT into translation (parts_id, language_id, translation, longdescription) VALUES
523 ($form->{id}, $language_id, | . $dbh->quote($translation) . qq|, | . $dbh->quote($longdescription) . qq| )|;
524 $dbh->do($query) || $form->dberror($query);
528 # delete price records
529 $query = qq|DELETE FROM prices
530 WHERE parts_id = $form->{id}|;
531 $dbh->do($query) || $form->dberror($query);
532 # insert price records only if different to sellprice
533 for my $i (1 .. $form->{price_rows}) {
534 if ($form->{"price_$i"} eq "0") {
535 $form->{"price_$i"} = $form->{sellprice};
538 ( $form->{"price_$i"}
539 || $form->{"klass_$i"}
540 || $form->{"pricegroup_id_$i"})
541 and $form->{"price_$i"} != $form->{sellprice}
543 $klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
544 $price = $form->parse_amount($myconfig, $form->{"price_$i"});
546 $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
547 $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price)
548 VALUES($form->{id},$pricegroup_id,$price)|;
549 $dbh->do($query) || $form->dberror($query);
553 # insert makemodel records
554 unless ($form->{item} eq 'service') {
555 for my $i (1 .. $form->{makemodel_rows}) {
556 if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
557 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
559 $query = qq|INSERT INTO makemodel (parts_id, make, model)
561 '$form->{"make_$i"}', '$form->{"model_$i"}')|;
562 $dbh->do($query) || $form->dberror($query);
568 foreach $item (split / /, $form->{taxaccounts}) {
569 if ($form->{"IC_tax_$item"}) {
570 $query = qq|INSERT INTO partstax (parts_id, chart_id)
574 WHERE c.accno = '$item'))|;
575 $dbh->do($query) || $form->dberror($query);
579 # add assembly records
580 if ($form->{item} eq 'assembly') {
582 for my $i (1 .. $form->{assembly_rows}) {
583 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
585 if ($form->{"qty_$i"} != 0) {
586 $form->{"bom_$i"} *= 1;
587 $query = qq|INSERT INTO assembly (id, parts_id, qty, bom)
588 VALUES ($form->{id}, $form->{"id_$i"},
589 $form->{"qty_$i"}, '$form->{"bom_$i"}')|;
590 $dbh->do($query) || $form->dberror($query);
594 # adjust onhand for the parts
595 if ($form->{onhand} != 0) {
596 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
602 my $shippingdate = "$a[5]-$a[4]-$a[3]";
604 $form->get_employee($dbh);
606 # add inventory record
607 $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
608 shippingdate, employee_id) VALUES (
609 0, $form->{id}, $form->{stock}, '$shippingdate',
610 $form->{employee_id})|;
611 $dbh->do($query) || $form->dberror($query);
615 #set expense_accno=inventory_accno if they are different => bilanz
617 ($form->{expense_accno} != $form->{inventory_accno})
618 ? $form->{inventory_accno}
619 : $form->{expense_accno};
621 # get tax rates and description
623 ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
624 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
626 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
628 $stw = $dbh->prepare($query);
630 $stw->execute || $form->dberror($query);
632 $form->{taxaccount} = "";
633 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
635 # if ($customertax{$ref->{accno}}) {
636 $form->{taxaccount} .= "$ptr->{accno} ";
637 if (!($form->{taxaccount2} =~ /$ptr->{accno}/)) {
638 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
639 $form->{"$ptr->{accno}_description"} = $ptr->{description};
640 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
641 $form->{taxaccount2} .= " $ptr->{accno} ";
647 my $rc = $dbh->commit;
650 $main::lxdebug->leave_sub();
655 sub update_assembly {
656 $main::lxdebug->enter_sub();
658 my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
660 my $query = qq|SELECT a.id, a.qty
662 WHERE a.parts_id = $id|;
663 my $sth = $dbh->prepare($query);
664 $sth->execute || $form->dberror($query);
666 while (my ($pid, $aqty) = $sth->fetchrow_array) {
667 &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
671 $query = qq|UPDATE parts
672 SET sellprice = sellprice +
673 $qty * ($form->{sellprice} - $sellprice),
675 $qty * ($form->{weight} - $weight)
677 $dbh->do($query) || $form->dberror($query);
679 $main::lxdebug->leave_sub();
682 sub retrieve_assemblies {
683 $main::lxdebug->enter_sub();
685 my ($self, $myconfig, $form) = @_;
687 # connect to database
688 my $dbh = $form->dbconnect($myconfig);
692 if ($form->{partnumber}) {
693 my $partnumber = $form->like(lc $form->{partnumber});
694 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
697 if ($form->{description}) {
698 my $description = $form->like(lc $form->{description});
699 $where .= " AND lower(p.description) LIKE '$description'";
701 $where .= " AND NOT p.obsolete = '1'";
703 # retrieve assembly items
704 my $query = qq|SELECT p.id, p.partnumber, p.description,
705 p.bin, p.onhand, p.rop,
706 (SELECT sum(p2.inventory_accno_id)
707 FROM parts p2, assembly a
708 WHERE p2.id = a.parts_id
709 AND a.id = p.id) AS inventory
714 my $sth = $dbh->prepare($query);
715 $sth->execute || $form->dberror($query);
717 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
718 push @{ $form->{assembly_items} }, $ref if $ref->{inventory};
724 $main::lxdebug->leave_sub();
727 sub restock_assemblies {
728 $main::lxdebug->enter_sub();
730 my ($self, $myconfig, $form) = @_;
732 # connect to database
733 my $dbh = $form->dbconnect_noauto($myconfig);
735 for my $i (1 .. $form->{rowcount}) {
737 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
739 if ($form->{"qty_$i"} != 0) {
740 &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
745 my $rc = $dbh->commit;
748 $main::lxdebug->leave_sub();
753 sub adjust_inventory {
754 $main::lxdebug->enter_sub();
756 my ($dbh, $form, $id, $qty) = @_;
758 my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
759 FROM parts p, assembly a
760 WHERE a.parts_id = p.id
762 my $sth = $dbh->prepare($query);
763 $sth->execute || $form->dberror($query);
765 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
767 my $allocate = $qty * $ref->{qty};
769 # is it a service item, then loop
770 $ref->{inventory_accno_id} *= 1;
771 next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
773 # adjust parts onhand
774 $form->update_balance($dbh, "parts", "onhand",
782 my $rc = $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $qty);
784 $main::lxdebug->leave_sub();
790 $main::lxdebug->enter_sub();
792 my ($self, $myconfig, $form) = @_;
794 # connect to database, turn off AutoCommit
795 my $dbh = $form->dbconnect_noauto($myconfig);
797 # first delete prices of pricegroup
798 my $query = qq|DELETE FROM prices
799 WHERE parts_id = $form->{id}|;
800 $dbh->do($query) || $form->dberror($query);
802 my $query = qq|DELETE FROM parts
803 WHERE id = $form->{id}|;
804 $dbh->do($query) || $form->dberror($query);
806 $query = qq|DELETE FROM partstax
807 WHERE parts_id = $form->{id}|;
808 $dbh->do($query) || $form->dberror($query);
810 # check if it is a part, assembly or service
811 if ($form->{item} ne 'service') {
812 $query = qq|DELETE FROM makemodel
813 WHERE parts_id = $form->{id}|;
814 $dbh->do($query) || $form->dberror($query);
817 if ($form->{item} eq 'assembly') {
820 $query = qq|DELETE FROM inventory
821 WHERE parts_id = $form->{id}|;
822 $dbh->do($query) || $form->dberror($query);
824 $query = qq|DELETE FROM assembly
825 WHERE id = $form->{id}|;
826 $dbh->do($query) || $form->dberror($query);
829 if ($form->{item} eq 'alternate') {
830 $query = qq|DELETE FROM alternate
831 WHERE id = $form->{id}|;
832 $dbh->do($query) || $form->dberror($query);
836 my $rc = $dbh->commit;
839 $main::lxdebug->leave_sub();
845 $main::lxdebug->enter_sub();
847 my ($self, $myconfig, $form) = @_;
849 my $i = $form->{assembly_rows};
853 if ($form->{"partnumber_$i"}) {
854 $var = $form->like(lc $form->{"partnumber_$i"});
855 $where .= " AND lower(p.partnumber) LIKE '$var'";
857 if ($form->{"description_$i"}) {
858 $var = $form->like(lc $form->{"description_$i"});
859 $where .= " AND lower(p.description) LIKE '$var'";
861 if ($form->{"partsgroup_$i"}) {
862 $var = $form->like(lc $form->{"partsgroup_$i"});
863 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
867 $where .= " AND NOT p.id = $form->{id}";
871 $where .= " ORDER BY p.partnumber";
873 $where .= " ORDER BY p.description";
876 # connect to database
877 my $dbh = $form->dbconnect($myconfig);
879 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
880 p.weight, p.onhand, p.unit,
883 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
885 my $sth = $dbh->prepare($query);
886 $sth->execute || $form->dberror($query);
888 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
889 push @{ $form->{item_list} }, $ref;
895 $main::lxdebug->leave_sub();
899 $main::lxdebug->enter_sub();
901 my ($self, $myconfig, $form) = @_;
909 foreach my $item (qw(partnumber drawing microfiche)) {
910 if ($form->{$item}) {
911 $var = $form->like(lc $form->{$item});
912 $where .= " AND lower(p.$item) LIKE '$var'";
916 # special case for description
917 if ($form->{description}) {
918 unless ( $form->{bought}
923 || $form->{quoted}) {
924 $var = $form->like(lc $form->{description});
925 $where .= " AND lower(p.description) LIKE '$var'";
929 # special case for serialnumber
930 if ($form->{l_serialnumber}) {
931 if ($form->{serialnumber}) {
932 $var = $form->like(lc $form->{serialnumber});
933 $where .= " AND lower(serialnumber) LIKE '$var'";
938 $var = $form->like(lc $form->{ean});
939 $where .= " AND lower(ean) LIKE '$var'";
942 if ($form->{searchitems} eq 'part') {
943 $where .= " AND p.inventory_accno_id > 0";
945 if ($form->{searchitems} eq 'assembly') {
946 $form->{bought} = "";
947 $where .= " AND p.assembly = '1'";
949 if ($form->{searchitems} eq 'service') {
950 $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
952 # irrelevant for services
953 $form->{make} = $form->{model} = "";
956 # items which were never bought, sold or on an order
957 if ($form->{itemstatus} eq 'orphaned') {
958 $form->{onhand} = $form->{short} = 0;
959 $form->{bought} = $form->{sold} = 0;
960 $form->{onorder} = $form->{ordered} = 0;
961 $form->{rfq} = $form->{quoted} = 0;
963 $form->{transdatefrom} = $form->{transdateto} = "";
965 $where .= " AND p.onhand = 0
966 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
967 WHERE p.id = i.parts_id)
968 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
969 WHERE p.id = a.parts_id)
970 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
971 WHERE p.id = o.parts_id)";
974 if ($form->{itemstatus} eq 'active') {
975 $where .= " AND p.obsolete = '0'";
977 if ($form->{itemstatus} eq 'obsolete') {
978 $where .= " AND p.obsolete = '1'";
979 $form->{onhand} = $form->{short} = 0;
981 if ($form->{itemstatus} eq 'onhand') {
982 $where .= " AND p.onhand > 0";
984 if ($form->{itemstatus} eq 'short') {
985 $where .= " AND p.onhand < p.rop";
988 $var = $form->like(lc $form->{make});
989 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
990 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
992 if ($form->{model}) {
993 $var = $form->like(lc $form->{model});
994 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
995 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
997 if ($form->{partsgroup}) {
998 $var = $form->like(lc $form->{partsgroup});
999 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
1001 if ($form->{l_soldtotal}) {
1002 $where .= " AND p.id=i.parts_id AND i.qty >= 0";
1004 " GROUP BY p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
1006 if ($form->{top100}) {
1007 $limit = " LIMIT 100";
1011 if ($form->{revers} == 1) {
1012 $form->{desc} = " DESC";
1017 # connect to database
1018 my $dbh = $form->dbconnect($myconfig);
1020 my $sortorder = $form->{sort};
1021 $sortorder .= $form->{desc};
1022 $sortorder = $form->{sort} if $form->{sort};
1026 if ($form->{l_soldtotal}) {
1027 $form->{soldtotal} = 'soldtotal';
1029 qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
1030 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
1031 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
1037 $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
1038 p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
1039 p.priceupdate, p.image, p.drawing, p.microfiche,
1042 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1045 ORDER BY $sortorder|;
1048 # rebuild query for bought and sold items
1049 if ( $form->{bought}
1054 || $form->{quoted}) {
1056 my @a = qw(partnumber description bin priceupdate name);
1058 push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
1059 push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
1060 push @a, "quonumber" if ($form->{rfq} || $form->{quoted});
1065 if ($form->{bought} || $form->{sold}) {
1067 my $invwhere = "$where";
1068 $invwhere .= " AND i.assemblyitem = '0'";
1069 $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
1070 if $form->{transdatefrom};
1071 $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
1072 if $form->{transdateto};
1074 if ($form->{description}) {
1075 $var = $form->like(lc $form->{description});
1076 $invwhere .= " AND lower(i.description) LIKE '$var'";
1079 my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
1080 i.qty AS onhand, i.unit, p.bin, i.sellprice,
1081 p.listprice, p.lastcost, p.rop, p.weight,
1082 p.priceupdate, p.image, p.drawing, p.microfiche,
1084 a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
1085 ct.name, i.deliverydate|;
1087 if ($form->{bought}) {
1089 SELECT $flds, 'ir' AS module, '' AS type,
1092 JOIN parts p ON (p.id = i.parts_id)
1093 JOIN ap a ON (a.id = i.trans_id)
1094 JOIN vendor ct ON (a.vendor_id = ct.id)
1095 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1101 if ($form->{sold}) {
1103 SELECT $flds, 'is' AS module, '' AS type,
1106 JOIN parts p ON (p.id = i.parts_id)
1107 JOIN ar a ON (a.id = i.trans_id)
1108 JOIN customer ct ON (a.customer_id = ct.id)
1109 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1116 if ($form->{onorder} || $form->{ordered}) {
1117 my $ordwhere = "$where
1118 AND o.quotation = '0'";
1119 $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1120 if $form->{transdatefrom};
1121 $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
1122 if $form->{transdateto};
1124 if ($form->{description}) {
1125 $var = $form->like(lc $form->{description});
1126 $ordwhere .= " AND lower(oi.description) LIKE '$var'";
1130 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1131 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1132 p.listprice, p.lastcost, p.rop, p.weight,
1133 p.priceupdate, p.image, p.drawing, p.microfiche,
1135 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1138 if ($form->{ordered}) {
1140 SELECT $flds, 'oe' AS module, 'sales_order' AS type,
1141 (SELECT buy FROM exchangerate ex
1142 WHERE ex.curr = o.curr
1143 AND ex.transdate = o.transdate) AS exchangerate
1145 JOIN parts p ON (oi.parts_id = p.id)
1146 JOIN oe o ON (oi.trans_id = o.id)
1147 JOIN customer ct ON (o.customer_id = ct.id)
1148 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1150 AND o.customer_id > 0|;
1155 if ($form->{onorder}) {
1157 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1158 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1159 p.listprice, p.lastcost, p.rop, p.weight,
1160 p.priceupdate, p.image, p.drawing, p.microfiche,
1162 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1166 SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
1167 (SELECT sell FROM exchangerate ex
1168 WHERE ex.curr = o.curr
1169 AND ex.transdate = o.transdate) AS exchangerate
1171 JOIN parts p ON (oi.parts_id = p.id)
1172 JOIN oe o ON (oi.trans_id = o.id)
1173 JOIN vendor ct ON (o.vendor_id = ct.id)
1174 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1176 AND o.vendor_id > 0|;
1181 if ($form->{rfq} || $form->{quoted}) {
1182 my $quowhere = "$where
1183 AND o.quotation = '1'";
1184 $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1185 if $form->{transdatefrom};
1186 $quowhere .= " AND o.transdate <= '$form->{transdateto}'"
1187 if $form->{transdateto};
1189 if ($form->{description}) {
1190 $var = $form->like(lc $form->{description});
1191 $quowhere .= " AND lower(oi.description) LIKE '$var'";
1195 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1196 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1197 p.listprice, p.lastcost, p.rop, p.weight,
1198 p.priceupdate, p.image, p.drawing, p.microfiche,
1200 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1203 if ($form->{quoted}) {
1205 SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
1206 (SELECT buy FROM exchangerate ex
1207 WHERE ex.curr = o.curr
1208 AND ex.transdate = o.transdate) AS exchangerate
1210 JOIN parts p ON (oi.parts_id = p.id)
1211 JOIN oe o ON (oi.trans_id = o.id)
1212 JOIN customer ct ON (o.customer_id = ct.id)
1213 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1215 AND o.customer_id > 0|;
1222 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1223 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1224 p.listprice, p.lastcost, p.rop, p.weight,
1225 p.priceupdate, p.image, p.drawing, p.microfiche,
1227 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1231 SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
1232 (SELECT sell FROM exchangerate ex
1233 WHERE ex.curr = o.curr
1234 AND ex.transdate = o.transdate) AS exchangerate
1236 JOIN parts p ON (oi.parts_id = p.id)
1237 JOIN oe o ON (oi.trans_id = o.id)
1238 JOIN vendor ct ON (o.vendor_id = ct.id)
1239 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1241 AND o.vendor_id > 0|;
1246 ORDER BY $sortorder|;
1249 my $sth = $dbh->prepare($query);
1250 $sth->execute || $form->dberror($query);
1252 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1253 push @{ $form->{parts} }, $ref;
1258 # include individual items for assemblies
1259 if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1260 foreach $item (@{ $form->{parts} }) {
1261 push @assemblies, $item;
1262 $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1264 p.sellprice, p.listprice, p.lastcost,
1265 p.rop, p.weight, p.priceupdate,
1266 p.image, p.drawing, p.microfiche
1267 FROM parts p, assembly a
1268 WHERE p.id = a.parts_id
1269 AND a.id = $item->{id}|;
1271 $sth = $dbh->prepare($query);
1272 $sth->execute || $form->dberror($query);
1274 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1275 $ref->{assemblyitem} = 1;
1276 push @assemblies, $ref;
1280 push @assemblies, { id => $item->{id} };
1284 # copy assemblies to $form->{parts}
1285 @{ $form->{parts} } = @assemblies;
1289 $main::lxdebug->leave_sub();
1293 $main::lxdebug->enter_sub();
1295 my ($self, $myconfig, $form) = @_;
1297 my $where = '1 = 1';
1303 foreach my $item (qw(partnumber drawing microfiche make model)) {
1304 if ($form->{$item}) {
1305 $var = $form->like(lc $form->{$item});
1307 # make will build later Bugfix 145
1308 if ($item ne 'make') {
1309 $where .= " AND lower(p.$item) LIKE '$var'";
1314 # special case for description
1315 if ($form->{description}) {
1316 unless ( $form->{bought}
1321 || $form->{quoted}) {
1322 $var = $form->like(lc $form->{description});
1323 $where .= " AND lower(p.description) LIKE '$var'";
1327 # special case for serialnumber
1328 if ($form->{l_serialnumber}) {
1329 if ($form->{serialnumber}) {
1330 $var = $form->like(lc $form->{serialnumber});
1331 $where .= " AND lower(serialnumber) LIKE '$var'";
1336 # items which were never bought, sold or on an order
1337 if ($form->{itemstatus} eq 'orphaned') {
1338 $form->{onhand} = $form->{short} = 0;
1339 $form->{bought} = $form->{sold} = 0;
1340 $form->{onorder} = $form->{ordered} = 0;
1341 $form->{rfq} = $form->{quoted} = 0;
1343 $form->{transdatefrom} = $form->{transdateto} = "";
1345 $where .= " AND p.onhand = 0
1346 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
1347 WHERE p.id = i.parts_id)
1348 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
1349 WHERE p.id = a.parts_id)
1350 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
1351 WHERE p.id = o.parts_id)";
1354 if ($form->{itemstatus} eq 'active') {
1355 $where .= " AND p.obsolete = '0'";
1357 if ($form->{itemstatus} eq 'obsolete') {
1358 $where .= " AND p.obsolete = '1'";
1359 $form->{onhand} = $form->{short} = 0;
1361 if ($form->{itemstatus} eq 'onhand') {
1362 $where .= " AND p.onhand > 0";
1364 if ($form->{itemstatus} eq 'short') {
1365 $where .= " AND p.onhand < p.rop";
1367 if ($form->{make}) {
1368 $var = $form->like(lc $form->{make});
1369 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
1370 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
1372 if ($form->{model}) {
1373 $var = $form->like(lc $form->{model});
1374 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
1375 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
1377 if ($form->{partsgroup}) {
1378 $var = $form->like(lc $form->{partsgroup});
1379 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
1383 # connect to database
1384 my $dbh = $form->dbconnect_noauto($myconfig);
1386 if ($form->{"sellprice"} ne "") {
1388 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"});
1389 if ($form->{"sellprice_type"} eq "percent") {
1390 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1391 $update = "sellprice* $faktor";
1393 $update = "sellprice+$faktor";
1396 $query = qq|UPDATE parts set sellprice=$update WHERE id IN (SELECT p.id
1398 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1403 if ($form->{"listprice"} ne "") {
1405 my $faktor = $form->parse_amount($myconfig,$form->{"listprice"});
1406 if ($form->{"listprice_type"} eq "percent") {
1407 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1408 $update = "listprice* $faktor";
1410 $update = "listprice+$faktor";
1413 $query = qq|UPDATE parts set listprice=$update WHERE id IN (SELECT p.id
1415 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1424 for my $i (1 .. $form->{price_rows}) {
1429 if ($form->{"price_$i"} ne "") {
1431 my $faktor = $form->parse_amount($myconfig,$form->{"price_$i"});
1432 if ($form->{"pricegroup_type_$i"} eq "percent") {
1433 my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1434 $update = "price* $faktor";
1436 $update = "price+$faktor";
1439 $query = qq|UPDATE prices set price=$update WHERE parts_id IN (SELECT p.id
1441 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1442 WHERE $where) AND pricegroup_id=$form->{"pricegroup_id_$i"}|;
1450 my $rc= $dbh->commit;
1452 $main::lxdebug->leave_sub();
1458 $main::lxdebug->enter_sub();
1460 my ($self, $module, $myconfig, $form) = @_;
1462 # connect to database
1463 my $dbh = $form->dbconnect($myconfig);
1466 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1467 p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1468 FROM chart c, parts p
1469 WHERE c.link LIKE '%$module%'
1470 AND p.id = $form->{id}
1473 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1474 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1475 FROM chart c, defaults d
1476 WHERE c.link LIKE '%$module%'
1480 my $sth = $dbh->prepare($query);
1481 $sth->execute || $form->dberror($query);
1482 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1483 foreach my $key (split /:/, $ref->{link}) {
1484 if ($key =~ /$module/) {
1485 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1486 || ($ref->{id} eq $ref->{income_accno_id})
1487 || ($ref->{id} eq $ref->{expense_accno_id})) {
1488 push @{ $form->{"${module}_links"}{$key} },
1489 { accno => $ref->{accno},
1490 description => $ref->{description},
1491 selected => "selected" };
1492 $form->{"${key}_default"} = "$ref->{accno}--$ref->{description}";
1494 push @{ $form->{"${module}_links"}{$key} },
1495 { accno => $ref->{accno},
1496 description => $ref->{description},
1504 # get buchungsgruppen
1505 $query = qq|SELECT id, description
1506 FROM buchungsgruppen|;
1507 $sth = $dbh->prepare($query);
1508 $sth->execute || $form->dberror($query);
1510 $form->{BUCHUNGSGRUPPEN} = [];
1511 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1512 push @{ $form->{BUCHUNGSGRUPPEN} }, $ref;
1517 $query = qq|SELECT id, description
1520 $sth = $dbh->prepare($query);
1521 $sth->execute || $form->dberror($query);
1523 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1524 push @{ $self->{payment_terms} }, $ref;
1529 $query = qq|SELECT current_date FROM defaults|;
1530 $sth = $dbh->prepare($query);
1531 $sth->execute || $form->dberror($query);
1533 ($form->{priceupdate}) = $sth->fetchrow_array;
1538 $main::lxdebug->leave_sub();
1541 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1543 $main::lxdebug->enter_sub();
1545 my ($self, $myconfig, $form, $sortorder) = @_;
1546 my $dbh = $form->dbconnect($myconfig);
1547 my $order = " p.partnumber";
1548 my $where = "1 = 1";
1550 if ($sortorder eq "all") {
1551 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1552 $where .= " AND p.description LIKE '%$form->{description}%'";
1554 if ($sortorder eq "partnumber") {
1555 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1556 $order = qq|p.$sortorder|;
1558 if ($sortorder eq "description") {
1559 $where .= " AND p.description LIKE '%$form->{description}%'";
1564 qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
1565 my $sth = $dbh->prepare($query);
1566 $sth->execute || $self->dberror($query);
1568 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1569 if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1572 $form->{"id_$j"} = $ref->{id};
1573 $form->{"partnumber_$j"} = $ref->{partnumber};
1574 $form->{"description_$j"} = $ref->{description};
1575 $form->{"unit_$j"} = $ref->{unit};
1576 $form->{"sellprice_$j"} = $ref->{sellprice};
1577 $form->{"soldtotal_$j"} = get_soldtotal($dbh, $ref->{id});
1584 $main::lxdebug->leave_sub();
1589 # gets sum of sold part with part_id
1591 $main::lxdebug->enter_sub();
1593 my ($dbh, $id) = @_;
1596 qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
1598 my $sth = $dbh->prepare($query);
1599 $sth->execute || $form->dberror($query);
1602 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1604 $sum = $ref->{totalsold};
1608 if ($sum eq undef) {
1612 $main::lxdebug->leave_sub();
1615 } #end get_soldtotal
1618 $main::lxdebug->enter_sub();
1620 my ($self, $myconfig, $form) = @_;
1621 my $i = $form->{rowcount};
1622 my $where = "NOT p.obsolete = '1'";
1624 if ($form->{"partnumber_$i"}) {
1625 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1626 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1628 if ($form->{"description_$i"}) {
1629 my $description = $form->like(lc $form->{"description_$i"});
1630 $where .= " AND lower(p.description) LIKE '$description'";
1633 if ($form->{"partsgroup_$i"}) {
1634 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1635 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1638 if ($form->{"description_$i"}) {
1639 $where .= " ORDER BY description";
1641 $where .= " ORDER BY partnumber";
1644 # connect to database
1645 my $dbh = $form->dbconnect($myconfig);
1647 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1649 c1.accno AS inventory_accno,
1650 c2.accno AS income_accno,
1651 c3.accno AS expense_accno,
1652 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1655 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1656 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1657 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1658 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1660 my $sth = $dbh->prepare($query);
1661 $sth->execute || $form->dberror($query);
1663 #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1665 # get tax rates and description
1666 #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
1667 #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1668 # FROM chart c, tax t
1669 # WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1671 # $stw = $dbh->prepare($query);
1672 #$stw->execute || $form->dberror($query);
1674 #$ref->{taxaccounts} = "";
1675 #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1677 # $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1678 # $form->{"$ptr->{accno}_description"} = $ptr->{description};
1679 # $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1680 # $form->{taxaccounts} .= "$ptr->{accno} ";
1681 # $ref->{taxaccounts} .= "$ptr->{accno} ";
1686 #chop $ref->{taxaccounts};
1688 push @{ $form->{item_list} }, $ref;
1694 $main::lxdebug->leave_sub();
1697 sub retrieve_languages {
1698 $main::lxdebug->enter_sub();
1700 my ($self, $myconfig, $form) = @_;
1702 # connect to database
1703 my $dbh = $form->dbconnect($myconfig);
1706 $where .= "tr.parts_id=$form->{id}";
1710 if ($form->{language_values} ne "") {
1711 $query = qq|SELECT l.id, l.description, tr.translation, tr.longdescription
1712 FROM language l LEFT OUTER JOIN translation tr ON (tr.language_id=l.id AND $where)|;
1714 $query = qq|SELECT l.id, l.description
1717 my $sth = $dbh->prepare($query);
1718 $sth->execute || $form->dberror($query);
1720 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1721 push(@{$languages}, $ref);
1727 $main::lxdebug->leave_sub();
1732 sub follow_account_chain {
1733 $main::lxdebug->enter_sub();
1735 my ($self, $form, $dbh, $transdate, $accno_id, $accno) = @_;
1737 my @visited_accno_ids = ($accno_id);
1742 "SELECT c.new_chart_id, date($transdate) >= c.valid_from AS is_valid, " .
1745 "LEFT JOIN chart cnew ON c.new_chart_id = cnew.id " .
1746 "WHERE (c.id = ?) AND NOT c.new_chart_id ISNULL AND (c.new_chart_id > 0)";
1747 $sth = $dbh->prepare($query);
1750 $sth->execute($accno_id) || $form->dberror($query . " ($accno_id)");
1751 $ref = $sth->fetchrow_hashref();
1752 last unless ($ref && $ref->{"is_valid"} &&
1753 !grep({ $_ == $ref->{"new_chart_id"} } @visited_accno_ids));
1754 $accno_id = $ref->{"new_chart_id"};
1755 $accno = $ref->{"accno"};
1756 push(@visited_accno_ids, $accno_id);
1759 $main::lxdebug->leave_sub();
1761 return ($accno_id, $accno);
1764 sub retrieve_accounts {
1765 $main::lxdebug->enter_sub();
1767 my ($self, $myconfig, $form, $parts_id, $index, $copy_accnos) = @_;
1769 my ($query, $sth, $dbh);
1771 $form->{"taxzone_id"} *= 1;
1773 $dbh = $form->dbconnect($myconfig);
1776 if ($form->{type} eq "invoice") {
1777 if (($form->{vc} eq "vendor") || !$form->{deliverydate}) {
1778 $transdate = $form->{invdate};
1780 $transdate = $form->{deliverydate};
1782 } elsif ($form->{type} eq "credit_note") {
1783 $transdate = $form->{invdate};
1785 $transdate = $form->{transdate};
1788 if ($transdate eq "") {
1789 $transdate = "current_date";
1791 $transdate = $dbh->quote($transdate);
1796 " p.inventory_accno_id AS is_part, " .
1797 " bg.inventory_accno_id, " .
1798 " bg.income_accno_id_$form->{taxzone_id} AS income_accno_id, " .
1799 " bg.expense_accno_id_$form->{taxzone_id} AS expense_accno_id, " .
1800 " c1.accno AS inventory_accno, " .
1801 " c2.accno AS income_accno, " .
1802 " c3.accno AS expense_accno " .
1804 "LEFT JOIN buchungsgruppen bg ON p.buchungsgruppen_id = bg.id " .
1805 "LEFT JOIN chart c1 ON bg.inventory_accno_id = c1.id " .
1806 "LEFT JOIN chart c2 ON bg.income_accno_id_$form->{taxzone_id} = c2.id " .
1807 "LEFT JOIN chart c3 ON bg.expense_accno_id_$form->{taxzone_id} = c3.id " .
1809 $sth = $dbh->prepare($query);
1810 $sth->execute($parts_id) || $form->dberror($query . " ($parts_id)");
1811 my $ref = $sth->fetchrow_hashref();
1814 # $main::lxdebug->message(0, "q $query");
1818 return $main::lxdebug->leave_sub();
1821 $ref->{"inventory_accno_id"} = undef unless ($ref->{"is_part"});
1824 foreach my $type (qw(inventory income expense)) {
1825 next unless ($ref->{"${type}_accno_id"});
1826 ($accounts{"${type}_accno_id"}, $accounts{"${type}_accno"}) =
1827 $self->follow_account_chain($form, $dbh, $transdate,
1828 $ref->{"${type}_accno_id"},
1829 $ref->{"${type}_accno"});
1832 map({ $form->{"${_}_accno_$index"} = $accounts{"${_}_accno"} }
1833 qw(inventory income expense));
1835 my $inc_exp = $form->{"vc"} eq "customer" ? "income" : "expense";
1836 my $accno_id = $accounts{"${inc_exp}_accno_id"};
1839 "SELECT c.accno, t.taxdescription AS description, t.rate, t.taxnumber " .
1841 "LEFT JOIN chart c ON c.id = t.chart_id " .
1843 " (SELECT tk.tax_id " .
1844 " FROM taxkeys tk " .
1845 " WHERE tk.chart_id = $accno_id AND startdate <= $transdate " .
1846 " ORDER BY startdate DESC LIMIT 1) ";
1847 $sth = $dbh->prepare($query);
1848 $sth->execute() || $form->dberror($query);
1849 $ref = $sth->fetchrow_hashref();
1854 $main::lxdebug->leave_sub();
1858 $form->{"taxaccounts_$index"} = $ref->{"accno"};
1859 if ($form->{"taxaccounts"} !~ /$ref->{accno}/) {
1860 $form->{"taxaccounts"} .= "$ref->{accno} ";
1862 map({ $form->{"$ref->{accno}_${_}"} = $ref->{$_}; }
1863 qw(rate description taxnumber));
1865 # $main::lxdebug->message(0, "formvars: rate " . $form->{"$ref->{accno}_rate"} .
1866 # " description " . $form->{"$ref->{accno}_description"} .
1867 # " taxnumber " . $form->{"$ref->{accno}_taxnumber"} .
1868 # " || taxaccounts_$index " . $form->{"taxaccounts_$index"} .
1869 # " || taxaccounts " . $form->{"taxaccounts"});
1871 $main::lxdebug->leave_sub();