8f2cfb35b10f5feb46724cb697ca5ea325d265fb
[kivitendo-erp.git] / SL / IC.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
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.
21 #
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 #======================================================================
30 #
31 # Inventory Control backend
32 #
33 #======================================================================
34
35 package IC;
36 use Data::Dumper;
37
38 sub get_part {
39   $main::lxdebug->enter_sub();
40
41   my ($self, $myconfig, $form) = @_;
42
43   # connect to db
44   my $dbh = $form->dbconnect($myconfig);
45
46   my $query = qq|SELECT p.*,
47                  c1.accno AS inventory_accno,
48                  c2.accno AS income_accno,
49                  c3.accno AS expense_accno,
50                  pg.partsgroup
51                  FROM parts p
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);
60
61   # copy to $form variables
62   map { $form->{$_} = $ref->{$_} } (keys %{$ref});
63
64   $sth->finish;
65
66   my %oid = ('Pg'     => 'a.oid',
67              'Oracle' => 'a.rowid');
68
69   # part or service item
70   $form->{item} = ($form->{inventory_accno}) ? 'part' : 'service';
71   if ($form->{assembly}) {
72     $form->{item} = 'assembly';
73
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,
77                 pg.partsgroup
78                 FROM parts p
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}}|;
83
84     $sth = $dbh->prepare($query);
85     $sth->execute || $form->dberror($query);
86
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};
92       }
93     }
94     $sth->finish;
95
96   }
97
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};
104
105   # get prices
106   $query =
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|;
110
111   $sth = $dbh->prepare($query);
112   $sth->execute || $form->dberror($query);
113
114   @pricegroups          = ();
115   @pricegroups_not_used = ();
116
117   #for pricegroups
118   my $i = 1;
119   while (
120          ($form->{"klass_$i"}, $form->{"pricegroup_id_$i"},
121           $form->{"price_$i"}, $form->{"pricegroup_$i"})
122          = $sth->fetchrow_array
123     ) {
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"};
128     $i++;
129   }
130
131   $sth->finish;
132
133   # get pricegroups
134   $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
135
136   $pkq = $dbh->prepare($query);
137   $pkq->execute || $form->dberror($query);
138   while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
139     push @{ $form->{PRICEGROUPS} }, $pkr;
140   }
141   $pkq->finish;
142
143   #find not used pricegroups
144   while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
145     my $insert = 0;
146     foreach $item (@pricegroups) {
147       if ($item eq $tmp->{id}) {
148
149         #drop
150         $insert = 1;
151       }
152     }
153     if ($insert == 0) {
154       push @pricegroups_not_used, $tmp;
155     }
156   }
157
158   # if not used pricegroups are avaible
159   if (@pricegroups_not_used) {
160
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}";
168       $i++;
169     }
170   }
171
172   #correct rows
173   $form->{price_rows} = $i - 1;
174
175   unless ($form->{item} eq 'service') {
176
177     # get makes
178     if ($form->{makemodel}) {
179       $query = qq|SELECT m.make, m.model FROM makemodel m
180                   WHERE m.parts_id = $form->{id}|;
181
182       $sth = $dbh->prepare($query);
183       $sth->execute || $form->dberror($query);
184
185       my $i = 1;
186       while (($form->{"make_$i"}, $form->{"model_$i"}) = $sth->fetchrow_array)
187       {
188         $i++;
189       }
190       $sth->finish;
191       $form->{makemodel_rows} = $i - 1;
192
193     }
194   }
195
196   # now get accno for taxes
197   $query = qq|SELECT c.accno
198               FROM chart c, partstax pt
199               WHERE pt.chart_id = c.id
200               AND pt.parts_id = $form->{id}|;
201
202   $sth = $dbh->prepare($query);
203   $sth->execute || $form->dberror($query);
204
205   while (($key) = $sth->fetchrow_array) {
206     $form->{amount}{$key} = $key;
207   }
208
209   $sth->finish;
210
211   # is it an orphan
212   $query = qq|SELECT i.parts_id
213               FROM invoice i
214               WHERE i.parts_id = $form->{id}
215             UNION
216               SELECT o.parts_id
217               FROM orderitems o
218               WHERE o.parts_id = $form->{id}
219             UNION
220               SELECT a.parts_id
221               FROM assembly a
222               WHERE a.parts_id = $form->{id}|;
223   $sth = $dbh->prepare($query);
224   $sth->execute || $form->dberror($query);
225
226   ($form->{orphaned}) = $sth->fetchrow_array;
227   $form->{orphaned} = !$form->{orphaned};
228   $sth->finish;
229
230   $dbh->disconnect;
231
232   $main::lxdebug->leave_sub();
233 }
234
235 sub get_pricegroups {
236   $main::lxdebug->enter_sub();
237
238   my ($self, $myconfig, $form) = @_;
239   my $dbh                  = $form->dbconnect($myconfig);
240   my $i                    = 1;
241   my @pricegroups_not_used = ();
242
243   # get pricegroups
244   my $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
245
246   my $pkq = $dbh->prepare($query);
247   $pkq->execute || $form->dberror($query);
248   while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
249     push @{ $form->{PRICEGROUPS} }, $pkr;
250   }
251   $pkq->finish;
252
253   #find not used pricegroups
254   while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
255     push @pricegroups_not_used, $tmp;
256   }
257
258   # if not used pricegroups are avaible
259   if (@pricegroups_not_used) {
260
261     foreach $name (@pricegroups_not_used) {
262       $form->{"klass_$i"} = "$name->{id}";
263       $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
264       $form->{"price_$i"} =
265         $form->format_amount($myconfig, $form->{"price_$i"}, 5);
266       $form->{"pricegroup_id_$i"} = "$name->{id}";
267       $form->{"pricegroup_$i"}    = "$name->{pricegroup}";
268       $i++;
269     }
270   }
271
272   #correct rows
273   $form->{price_rows} = $i - 1;
274
275   $dbh->disconnect;
276
277   $main::lxdebug->leave_sub();
278 }
279
280 sub save {
281   $main::lxdebug->enter_sub();
282
283   my ($self, $myconfig, $form) = @_;
284
285   if ($form->{eur} && ($form->{item} ne 'service')) {
286     $form->{IC} = $form->{IC_expense};
287   }
288
289   ($form->{inventory_accno}) = split(/--/, $form->{IC});
290   ($form->{expense_accno})   = split(/--/, $form->{IC_expense});
291   ($form->{income_accno})    = split(/--/, $form->{IC_income});
292
293   # connect to database, turn off AutoCommit
294   my $dbh = $form->dbconnect_noauto($myconfig);
295
296   # save the part
297   # make up a unique handle and store in partnumber field
298   # then retrieve the record based on the unique handle to get the id
299   # replace the partnumber field with the actual variable
300   # add records for makemodel
301
302   # if there is a $form->{id} then replace the old entry
303   # delete all makemodel entries and add the new ones
304
305   # escape '
306   map { $form->{$_} =~ s/\'/\'\'/g } qw(partnumber description notes unit);
307
308   # undo amount formatting
309   map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) }
310     qw(rop weight listprice sellprice gv lastcost stock);
311
312   # set date to NULL if nothing entered
313   $form->{priceupdate} =
314     ($form->{priceupdate}) ? qq|'$form->{priceupdate}'| : "NULL";
315
316   $form->{makemodel} = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
317
318   $form->{alternate} = 0;
319   $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
320   $form->{obsolete} *= 1;
321   $form->{shop}     *= 1;
322   $form->{onhand}   *= 1;
323   $form->{ve}       *= 1;
324   $form->{ge}       *= 1;
325
326   my ($query, $sth);
327
328   if ($form->{id}) {
329
330     # get old price
331     $query = qq|SELECT p.sellprice, p.weight
332                 FROM parts p
333                 WHERE p.id = $form->{id}|;
334     $sth = $dbh->prepare($query);
335     $sth->execute || $form->dberror($query);
336     my ($sellprice, $weight) = $sth->fetchrow_array;
337     $sth->finish;
338
339     # if item is part of an assembly adjust all assemblies
340     $query = qq|SELECT a.id, a.qty
341                 FROM assembly a
342                 WHERE a.parts_id = $form->{id}|;
343     $sth = $dbh->prepare($query);
344     $sth->execute || $form->dberror($query);
345     while (my ($id, $qty) = $sth->fetchrow_array) {
346       &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
347     }
348     $sth->finish;
349
350     if ($form->{item} ne 'service') {
351
352       # delete makemodel records
353       $query = qq|DELETE FROM makemodel
354                   WHERE parts_id = $form->{id}|;
355       $dbh->do($query) || $form->dberror($query);
356     }
357
358     if ($form->{item} eq 'assembly') {
359       if ($form->{onhand} != 0) {
360         &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
361       }
362
363       # delete assembly records
364       $query = qq|DELETE FROM assembly
365                   WHERE id = $form->{id}|;
366       $dbh->do($query) || $form->dberror($query);
367
368       $form->{onhand} += $form->{stock};
369     }
370
371     # delete tax records
372     $query = qq|DELETE FROM partstax
373                 WHERE parts_id = $form->{id}|;
374     $dbh->do($query) || $form->dberror($query);
375
376   } else {
377     my $uid = rand() . time;
378     $uid .= $form->{login};
379
380     $query = qq|SELECT p.id FROM parts p
381                 WHERE p.partnumber = '$form->{partnumber}'|;
382     $sth = $dbh->prepare($query);
383     $sth->execute || $form->dberror($query);
384     ($form->{id}) = $sth->fetchrow_array;
385     $sth->finish;
386
387     if ($form->{id} ne "") {
388       $main::lxdebug->leave_sub();
389       return 3;
390     }
391     $query = qq|INSERT INTO parts (partnumber, description)
392                 VALUES ('$uid', 'dummy')|;
393     $dbh->do($query) || $form->dberror($query);
394
395     $query = qq|SELECT p.id FROM parts p
396                 WHERE p.partnumber = '$uid'|;
397     $sth = $dbh->prepare($query);
398     $sth->execute || $form->dberror($query);
399
400     ($form->{id}) = $sth->fetchrow_array;
401     $sth->finish;
402
403     $form->{orphaned} = 1;
404     $form->{onhand} = $form->{stock} if $form->{item} eq 'assembly';
405     if ($form->{partnumber} eq "" && $form->{inventory_accno} eq "") {
406       $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
407     }
408     if ($form->{partnumber} eq "" && $form->{inventory_accno} ne "") {
409       $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
410     }
411
412   }
413   my $partsgroup_id = 0;
414
415   if ($form->{partsgroup}) {
416     ($partsgroup, $partsgroup_id) = split /--/, $form->{partsgroup};
417   }
418
419   $query = qq|UPDATE parts SET
420               partnumber = '$form->{partnumber}',
421               description = '$form->{description}',
422               makemodel = '$form->{makemodel}',
423               alternate = '$form->{alternate}',
424               assembly = '$form->{assembly}',
425               listprice = $form->{listprice},
426               sellprice = $form->{sellprice},
427               lastcost = $form->{lastcost},
428               weight = $form->{weight},
429               priceupdate = $form->{priceupdate},
430               unit = '$form->{unit}',
431               notes = '$form->{notes}',
432               rop = $form->{rop},
433               bin = '$form->{bin}',
434               inventory_accno_id = (SELECT c.id FROM chart c
435                                     WHERE c.accno = '$form->{inventory_accno}'),
436               income_accno_id = (SELECT c.id FROM chart c
437                                  WHERE c.accno = '$form->{income_accno}'),
438               expense_accno_id = (SELECT c.id FROM chart c
439                                   WHERE c.accno = '$form->{expense_accno}'),
440               obsolete = '$form->{obsolete}',
441               image = '$form->{image}',
442               drawing = '$form->{drawing}',
443               shop = '$form->{shop}',
444               ve = '$form->{ve}',
445               gv = '$form->{gv}',
446               microfiche = '$form->{microfiche}',
447               partsgroup_id = $partsgroup_id
448               WHERE id = $form->{id}|;
449   $dbh->do($query) || $form->dberror($query);
450
451   # delete price records
452   $query = qq|DELETE FROM prices
453               WHERE parts_id = $form->{id}|;
454   $dbh->do($query) || $form->dberror($query);
455
456   # insert price records only if different to sellprice
457   for my $i (1 .. $form->{price_rows}) {
458     if ($form->{"price_$i"} eq "0") {
459       $form->{"price_$i"} = $form->{sellprice};
460     }
461     if (
462         (   $form->{"price_$i"}
463          || $form->{"klass_$i"}
464          || $form->{"pricegroup_id_$i"})
465         and $form->{"price_$i"} != $form->{sellprice}
466       ) {
467       $klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
468       $price = $form->parse_amount($myconfig, $form->{"price_$i"});
469       $pricegroup_id =
470         $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
471       $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price)
472                   VALUES($form->{id},$pricegroup_id,$price)|;
473       $dbh->do($query) || $form->dberror($query);
474     }
475   }
476
477   # insert makemodel records
478   unless ($form->{item} eq 'service') {
479     for my $i (1 .. $form->{makemodel_rows}) {
480       if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
481         map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
482
483         $query = qq|INSERT INTO makemodel (parts_id, make, model)
484                     VALUES ($form->{id},
485                     '$form->{"make_$i"}', '$form->{"model_$i"}')|;
486         $dbh->do($query) || $form->dberror($query);
487       }
488     }
489   }
490
491   # insert taxes
492   foreach $item (split / /, $form->{taxaccounts}) {
493     if ($form->{"IC_tax_$item"}) {
494       $query = qq|INSERT INTO partstax (parts_id, chart_id)
495                   VALUES ($form->{id},
496                           (SELECT c.id
497                            FROM chart c
498                            WHERE c.accno = '$item'))|;
499       $dbh->do($query) || $form->dberror($query);
500     }
501   }
502
503   # add assembly records
504   if ($form->{item} eq 'assembly') {
505
506     for my $i (1 .. $form->{assembly_rows}) {
507       $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
508
509       if ($form->{"qty_$i"} != 0) {
510         $form->{"bom_$i"} *= 1;
511         $query = qq|INSERT INTO assembly (id, parts_id, qty, bom)
512                     VALUES ($form->{id}, $form->{"id_$i"},
513                     $form->{"qty_$i"}, '$form->{"bom_$i"}')|;
514         $dbh->do($query) || $form->dberror($query);
515       }
516     }
517
518     # adjust onhand for the parts
519     if ($form->{onhand} != 0) {
520       &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
521     }
522
523     @a = localtime;
524     $a[5] += 1900;
525     $a[4]++;
526     my $shippingdate = "$a[5]-$a[4]-$a[3]";
527
528     $form->get_employee($dbh);
529
530     # add inventory record
531     $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
532                 shippingdate, employee_id) VALUES (
533                 0, $form->{id}, $form->{stock}, '$shippingdate',
534                 $form->{employee_id})|;
535     $dbh->do($query) || $form->dberror($query);
536
537   }
538
539   #set expense_accno=inventory_accno if they are different => bilanz
540   $vendor_accno =
541     ($form->{expense_accno} != $form->{inventory_accno})
542     ? $form->{inventory_accno}
543     : $form->{expense_accno};
544
545   # get tax rates and description
546   $accno_id =
547     ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
548   $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
549               FROM chart c, tax t
550               WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
551               ORDER BY c.accno|;
552   $stw = $dbh->prepare($query);
553
554   $stw->execute || $form->dberror($query);
555
556   $form->{taxaccount} = "";
557   while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
558
559     #    if ($customertax{$ref->{accno}}) {
560     $form->{taxaccount} .= "$ptr->{accno} ";
561     if (!($form->{taxaccount2} =~ /$ptr->{accno}/)) {
562       $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
563       $form->{"$ptr->{accno}_description"} = $ptr->{description};
564       $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
565       $form->{taxaccount2} .= " $ptr->{accno} ";
566     }
567
568   }
569
570   # commit
571   my $rc = $dbh->commit;
572   $dbh->disconnect;
573
574   $main::lxdebug->leave_sub();
575
576   return $rc;
577 }
578
579 sub update_assembly {
580   $main::lxdebug->enter_sub();
581
582   my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
583
584   my $query = qq|SELECT a.id, a.qty
585                  FROM assembly a
586                  WHERE a.parts_id = $id|;
587   my $sth = $dbh->prepare($query);
588   $sth->execute || $form->dberror($query);
589
590   while (my ($pid, $aqty) = $sth->fetchrow_array) {
591     &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
592   }
593   $sth->finish;
594
595   $query = qq|UPDATE parts
596               SET sellprice = sellprice +
597                   $qty * ($form->{sellprice} - $sellprice),
598                   weight = weight +
599                   $qty * ($form->{weight} - $weight)
600               WHERE id = $id|;
601   $dbh->do($query) || $form->dberror($query);
602
603   $main::lxdebug->leave_sub();
604 }
605
606 sub retrieve_assemblies {
607   $main::lxdebug->enter_sub();
608
609   my ($self, $myconfig, $form) = @_;
610
611   # connect to database
612   my $dbh = $form->dbconnect($myconfig);
613
614   my $where = '1 = 1';
615
616   if ($form->{partnumber}) {
617     my $partnumber = $form->like(lc $form->{partnumber});
618     $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
619   }
620
621   if ($form->{description}) {
622     my $description = $form->like(lc $form->{description});
623     $where .= " AND lower(p.description) LIKE '$description'";
624   }
625   $where .= " AND NOT p.obsolete = '1'";
626
627   # retrieve assembly items
628   my $query = qq|SELECT p.id, p.partnumber, p.description,
629                  p.bin, p.onhand, p.rop,
630                    (SELECT sum(p2.inventory_accno_id)
631                     FROM parts p2, assembly a
632                     WHERE p2.id = a.parts_id
633                     AND a.id = p.id) AS inventory
634                  FROM parts p
635                  WHERE $where
636                  AND assembly = '1'|;
637
638   my $sth = $dbh->prepare($query);
639   $sth->execute || $form->dberror($query);
640
641   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
642     push @{ $form->{assembly_items} }, $ref if $ref->{inventory};
643   }
644   $sth->finish;
645
646   $dbh->disconnect;
647
648   $main::lxdebug->leave_sub();
649 }
650
651 sub restock_assemblies {
652   $main::lxdebug->enter_sub();
653
654   my ($self, $myconfig, $form) = @_;
655
656   # connect to database
657   my $dbh = $form->dbconnect_noauto($myconfig);
658
659   for my $i (1 .. $form->{rowcount}) {
660
661     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
662
663     if ($form->{"qty_$i"} != 0) {
664       &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
665     }
666
667   }
668
669   my $rc = $dbh->commit;
670   $dbh->disconnect;
671
672   $main::lxdebug->leave_sub();
673
674   return $rc;
675 }
676
677 sub adjust_inventory {
678   $main::lxdebug->enter_sub();
679
680   my ($dbh, $form, $id, $qty) = @_;
681
682   my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
683                  FROM parts p, assembly a
684                  WHERE a.parts_id = p.id
685                  AND a.id = $id|;
686   my $sth = $dbh->prepare($query);
687   $sth->execute || $form->dberror($query);
688
689   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
690
691     my $allocate = $qty * $ref->{qty};
692
693     # is it a service item, then loop
694     $ref->{inventory_accno_id} *= 1;
695     next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
696
697     # adjust parts onhand
698     $form->update_balance($dbh, "parts", "onhand",
699                           qq|id = $ref->{id}|,
700                           $allocate * -1);
701   }
702
703   $sth->finish;
704
705   # update assembly
706   my $rc = $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $qty);
707
708   $main::lxdebug->leave_sub();
709
710   return $rc;
711 }
712
713 sub delete {
714   $main::lxdebug->enter_sub();
715
716   my ($self, $myconfig, $form) = @_;
717
718   # connect to database, turn off AutoCommit
719   my $dbh = $form->dbconnect_noauto($myconfig);
720
721   # first delete prices of pricegroup 
722   my $query = qq|DELETE FROM prices
723            WHERE parts_id = $form->{id}|;
724   $dbh->do($query) || $form->dberror($query);
725
726   my $query = qq|DELETE FROM parts
727                  WHERE id = $form->{id}|;
728   $dbh->do($query) || $form->dberror($query);
729
730   $query = qq|DELETE FROM partstax
731               WHERE parts_id = $form->{id}|;
732   $dbh->do($query) || $form->dberror($query);
733
734   # check if it is a part, assembly or service
735   if ($form->{item} ne 'service') {
736     $query = qq|DELETE FROM makemodel
737                 WHERE parts_id = $form->{id}|;
738     $dbh->do($query) || $form->dberror($query);
739   }
740
741   if ($form->{item} eq 'assembly') {
742
743     # delete inventory
744     $query = qq|DELETE FROM inventory
745                 WHERE parts_id = $form->{id}|;
746     $dbh->do($query) || $form->dberror($query);
747
748     $query = qq|DELETE FROM assembly
749                 WHERE id = $form->{id}|;
750     $dbh->do($query) || $form->dberror($query);
751   }
752
753   if ($form->{item} eq 'alternate') {
754     $query = qq|DELETE FROM alternate
755                 WHERE id = $form->{id}|;
756     $dbh->do($query) || $form->dberror($query);
757   }
758
759   # commit
760   my $rc = $dbh->commit;
761   $dbh->disconnect;
762
763   $main::lxdebug->leave_sub();
764
765   return $rc;
766 }
767
768 sub assembly_item {
769   $main::lxdebug->enter_sub();
770
771   my ($self, $myconfig, $form) = @_;
772
773   my $i = $form->{assembly_rows};
774   my $var;
775   my $where = "1 = 1";
776
777   if ($form->{"partnumber_$i"}) {
778     $var = $form->like(lc $form->{"partnumber_$i"});
779     $where .= " AND lower(p.partnumber) LIKE '$var'";
780   }
781   if ($form->{"description_$i"}) {
782     $var = $form->like(lc $form->{"description_$i"});
783     $where .= " AND lower(p.description) LIKE '$var'";
784   }
785   if ($form->{"partsgroup_$i"}) {
786     $var = $form->like(lc $form->{"partsgroup_$i"});
787     $where .= " AND lower(pg.partsgroup) LIKE '$var'";
788   }
789
790   if ($form->{id}) {
791     $where .= " AND NOT p.id = $form->{id}";
792   }
793
794   if ($partnumber) {
795     $where .= " ORDER BY p.partnumber";
796   } else {
797     $where .= " ORDER BY p.description";
798   }
799
800   # connect to database
801   my $dbh = $form->dbconnect($myconfig);
802
803   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
804                  p.weight, p.onhand, p.unit,
805                  pg.partsgroup
806                  FROM parts p
807                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
808                  WHERE $where|;
809   my $sth = $dbh->prepare($query);
810   $sth->execute || $form->dberror($query);
811
812   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
813     push @{ $form->{item_list} }, $ref;
814   }
815
816   $sth->finish;
817   $dbh->disconnect;
818
819   $main::lxdebug->leave_sub();
820 }
821
822 sub all_parts {
823   $main::lxdebug->enter_sub();
824
825   my ($self, $myconfig, $form) = @_;
826
827   my $where = '1 = 1';
828   my $var;
829
830   my $group;
831   my $limit;
832
833   foreach my $item (qw(partnumber drawing microfiche)) {
834     if ($form->{$item}) {
835       $var = $form->like(lc $form->{$item});
836       $where .= " AND lower(p.$item) LIKE '$var'";
837     }
838   }
839
840   # special case for description
841   if ($form->{description}) {
842     unless (   $form->{bought}
843             || $form->{sold}
844             || $form->{onorder}
845             || $form->{ordered}
846             || $form->{rfq}
847             || $form->{quoted}) {
848       $var = $form->like(lc $form->{description});
849       $where .= " AND lower(p.description) LIKE '$var'";
850     }
851   }
852
853   # special case for serialnumber
854   if ($form->{l_serialnumber}) {
855     if ($form->{serialnumber}) {
856       $var = $form->like(lc $form->{serialnumber});
857       $where .= " AND lower(serialnumber) LIKE '$var'";
858     }
859   }
860
861   if ($form->{searchitems} eq 'part') {
862     $where .= " AND p.inventory_accno_id > 0";
863   }
864   if ($form->{searchitems} eq 'assembly') {
865     $form->{bought} = "";
866     $where .= " AND p.assembly = '1'";
867   }
868   if ($form->{searchitems} eq 'service') {
869     $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
870
871     # irrelevant for services
872     $form->{make} = $form->{model} = "";
873   }
874
875   # items which were never bought, sold or on an order
876   if ($form->{itemstatus} eq 'orphaned') {
877     $form->{onhand}  = $form->{short}   = 0;
878     $form->{bought}  = $form->{sold}    = 0;
879     $form->{onorder} = $form->{ordered} = 0;
880     $form->{rfq}     = $form->{quoted}  = 0;
881
882     $form->{transdatefrom} = $form->{transdateto} = "";
883
884     $where .= " AND p.onhand = 0
885                 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
886                                  WHERE p.id = i.parts_id)
887                 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
888                                  WHERE p.id = a.parts_id)
889                 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
890                                  WHERE p.id = o.parts_id)";
891   }
892
893   if ($form->{itemstatus} eq 'active') {
894     $where .= " AND p.obsolete = '0'";
895   }
896   if ($form->{itemstatus} eq 'obsolete') {
897     $where .= " AND p.obsolete = '1'";
898     $form->{onhand} = $form->{short} = 0;
899   }
900   if ($form->{itemstatus} eq 'onhand') {
901     $where .= " AND p.onhand > 0";
902   }
903   if ($form->{itemstatus} eq 'short') {
904     $where .= " AND p.onhand < p.rop";
905   }
906   if ($form->{make}) {
907     $var = $form->like(lc $form->{make});
908     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
909                            FROM makemodel m WHERE lower(m.make) LIKE '$var')";
910   }
911   if ($form->{model}) {
912     $var = $form->like(lc $form->{model});
913     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
914                            FROM makemodel m WHERE lower(m.model) LIKE '$var')";
915   }
916   if ($form->{partsgroup}) {
917     $var = $form->like(lc $form->{partsgroup});
918     $where .= " AND lower(pg.partsgroup) LIKE '$var'";
919   }
920   if ($form->{l_soldtotal}) {
921     $where .= " AND p.id=i.parts_id AND  i.qty >= 0";
922     $group =
923       " GROUP BY  p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
924   }
925   if ($form->{top100}) {
926     $limit = " LIMIT 100";
927   }
928
929   # tables revers?
930   if ($form->{revers} == 1) {
931     $form->{desc} = " DESC";
932   } else {
933     $form->{desc} = "";
934   }
935
936   # connect to database
937   my $dbh = $form->dbconnect($myconfig);
938
939   my $sortorder = $form->{sort};
940   $sortorder .= $form->{desc};
941   $sortorder = $form->{sort} if $form->{sort};
942
943   my $query = "";
944
945   if ($form->{l_soldtotal}) {
946     $form->{soldtotal} = 'soldtotal';
947     $query =
948       qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
949                 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
950                 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
951                 WHERE $where
952                 $group
953                 ORDER BY $sortorder
954                 $limit|;
955   } else {
956     $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
957                  p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
958                  p.priceupdate, p.image, p.drawing, p.microfiche,
959                  pg.partsgroup
960                  FROM parts p
961                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
962                  WHERE $where
963                  $group
964                  ORDER BY $sortorder|;
965   }
966
967   # rebuild query for bought and sold items
968   if (   $form->{bought}
969       || $form->{sold}
970       || $form->{onorder}
971       || $form->{ordered}
972       || $form->{rfq}
973       || $form->{quoted}) {
974
975     my @a = qw(partnumber description bin priceupdate name);
976
977     push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
978     push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
979     push @a, "quonumber" if ($form->{rfq}     || $form->{quoted});
980
981     my $union = "";
982     $query = "";
983
984     if ($form->{bought} || $form->{sold}) {
985
986       my $invwhere = "$where";
987       $invwhere .= " AND i.assemblyitem = '0'";
988       $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
989         if $form->{transdatefrom};
990       $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
991         if $form->{transdateto};
992
993       if ($form->{description}) {
994         $var = $form->like(lc $form->{description});
995         $invwhere .= " AND lower(i.description) LIKE '$var'";
996       }
997
998       my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
999                     i.qty AS onhand, i.unit, p.bin, i.sellprice,
1000                     p.listprice, p.lastcost, p.rop, p.weight,
1001                     p.priceupdate, p.image, p.drawing, p.microfiche,
1002                     pg.partsgroup,
1003                     a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
1004                     ct.name, i.deliverydate|;
1005
1006       if ($form->{bought}) {
1007         $query = qq|
1008                     SELECT $flds, 'ir' AS module, '' AS type,
1009                     1 AS exchangerate
1010                     FROM invoice i
1011                     JOIN parts p ON (p.id = i.parts_id)
1012                     JOIN ap a ON (a.id = i.trans_id)
1013                     JOIN vendor ct ON (a.vendor_id = ct.id)
1014                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1015                     WHERE $invwhere|;
1016         $union = "
1017                   UNION";
1018       }
1019
1020       if ($form->{sold}) {
1021         $query .= qq|$union
1022                      SELECT $flds, 'is' AS module, '' AS type,
1023                      1 As exchangerate
1024                      FROM invoice i
1025                      JOIN parts p ON (p.id = i.parts_id)
1026                      JOIN ar a ON (a.id = i.trans_id)
1027                      JOIN customer ct ON (a.customer_id = ct.id)
1028                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1029                      WHERE $invwhere|;
1030         $union = "
1031                   UNION";
1032       }
1033     }
1034
1035     if ($form->{onorder} || $form->{ordered}) {
1036       my $ordwhere = "$where
1037                      AND o.quotation = '0'";
1038       $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1039         if $form->{transdatefrom};
1040       $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
1041         if $form->{transdateto};
1042
1043       if ($form->{description}) {
1044         $var = $form->like(lc $form->{description});
1045         $ordwhere .= " AND lower(oi.description) LIKE '$var'";
1046       }
1047
1048       $flds =
1049         qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1050                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1051                  p.listprice, p.lastcost, p.rop, p.weight,
1052                  p.priceupdate, p.image, p.drawing, p.microfiche,
1053                  pg.partsgroup,
1054                  '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1055                  ct.name|;
1056
1057       if ($form->{ordered}) {
1058         $query .= qq|$union
1059                      SELECT $flds, 'oe' AS module, 'sales_order' AS type,
1060                     (SELECT buy FROM exchangerate ex
1061                      WHERE ex.curr = o.curr
1062                      AND ex.transdate = o.transdate) AS exchangerate
1063                      FROM orderitems oi
1064                      JOIN parts p ON (oi.parts_id = p.id)
1065                      JOIN oe o ON (oi.trans_id = o.id)
1066                      JOIN customer ct ON (o.customer_id = ct.id)
1067                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1068                      WHERE $ordwhere
1069                      AND o.customer_id > 0|;
1070         $union = "
1071                   UNION";
1072       }
1073
1074       if ($form->{onorder}) {
1075         $flds =
1076           qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1077                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1078                    p.listprice, p.lastcost, p.rop, p.weight,
1079                    p.priceupdate, p.image, p.drawing, p.microfiche,
1080                    pg.partsgroup,
1081                    '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1082                    ct.name|;
1083
1084         $query .= qq|$union
1085                     SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
1086                     (SELECT sell FROM exchangerate ex
1087                      WHERE ex.curr = o.curr
1088                      AND ex.transdate = o.transdate) AS exchangerate
1089                     FROM orderitems oi
1090                     JOIN parts p ON (oi.parts_id = p.id)
1091                     JOIN oe o ON (oi.trans_id = o.id)
1092                     JOIN vendor ct ON (o.vendor_id = ct.id)
1093                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1094                     WHERE $ordwhere
1095                     AND o.vendor_id > 0|;
1096       }
1097
1098     }
1099
1100     if ($form->{rfq} || $form->{quoted}) {
1101       my $quowhere = "$where
1102                      AND o.quotation = '1'";
1103       $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1104         if $form->{transdatefrom};
1105       $quowhere .= " AND o.transdate <= '$form->{transdateto}'"
1106         if $form->{transdateto};
1107
1108       if ($form->{description}) {
1109         $var = $form->like(lc $form->{description});
1110         $quowhere .= " AND lower(oi.description) LIKE '$var'";
1111       }
1112
1113       $flds =
1114         qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1115                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1116                  p.listprice, p.lastcost, p.rop, p.weight,
1117                  p.priceupdate, p.image, p.drawing, p.microfiche,
1118                  pg.partsgroup,
1119                  '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1120                  ct.name|;
1121
1122       if ($form->{quoted}) {
1123         $query .= qq|$union
1124                      SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
1125                     (SELECT buy FROM exchangerate ex
1126                      WHERE ex.curr = o.curr
1127                      AND ex.transdate = o.transdate) AS exchangerate
1128                      FROM orderitems oi
1129                      JOIN parts p ON (oi.parts_id = p.id)
1130                      JOIN oe o ON (oi.trans_id = o.id)
1131                      JOIN customer ct ON (o.customer_id = ct.id)
1132                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1133                      WHERE $quowhere
1134                      AND o.customer_id > 0|;
1135         $union = "
1136                   UNION";
1137       }
1138
1139       if ($form->{rfq}) {
1140         $flds =
1141           qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1142                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1143                    p.listprice, p.lastcost, p.rop, p.weight,
1144                    p.priceupdate, p.image, p.drawing, p.microfiche,
1145                    pg.partsgroup,
1146                    '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1147                    ct.name|;
1148
1149         $query .= qq|$union
1150                     SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
1151                     (SELECT sell FROM exchangerate ex
1152                      WHERE ex.curr = o.curr
1153                      AND ex.transdate = o.transdate) AS exchangerate
1154                     FROM orderitems oi
1155                     JOIN parts p ON (oi.parts_id = p.id)
1156                     JOIN oe o ON (oi.trans_id = o.id)
1157                     JOIN vendor ct ON (o.vendor_id = ct.id)
1158                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1159                     WHERE $quowhere
1160                     AND o.vendor_id > 0|;
1161       }
1162
1163     }
1164     $query .= qq|
1165                  ORDER BY $sortorder|;
1166
1167   }
1168   my $sth = $dbh->prepare($query);
1169   $sth->execute || $form->dberror($query);
1170
1171   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1172     push @{ $form->{parts} }, $ref;
1173   }
1174
1175   $sth->finish;
1176
1177   # include individual items for assemblies
1178   if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1179     foreach $item (@{ $form->{parts} }) {
1180       push @assemblies, $item;
1181       $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1182                   p.unit, p.bin,
1183                   p.sellprice, p.listprice, p.lastcost,
1184                   p.rop, p.weight, p.priceupdate,
1185                   p.image, p.drawing, p.microfiche
1186                   FROM parts p, assembly a
1187                   WHERE p.id = a.parts_id
1188                   AND a.id = $item->{id}|;
1189
1190       $sth = $dbh->prepare($query);
1191       $sth->execute || $form->dberror($query);
1192
1193       while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1194         $ref->{assemblyitem} = 1;
1195         push @assemblies, $ref;
1196       }
1197       $sth->finish;
1198
1199       push @assemblies, { id => $item->{id} };
1200
1201     }
1202
1203     # copy assemblies to $form->{parts}
1204     @{ $form->{parts} } = @assemblies;
1205   }
1206
1207   $dbh->disconnect;
1208   $main::lxdebug->leave_sub();
1209 }
1210
1211 sub create_links {
1212   $main::lxdebug->enter_sub();
1213
1214   my ($self, $module, $myconfig, $form) = @_;
1215
1216   # connect to database
1217   my $dbh = $form->dbconnect($myconfig);
1218
1219   if ($form->{id}) {
1220     $query = qq|SELECT c.accno, c.description, c.link, c.id,
1221                         p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1222                         FROM chart c, parts p
1223                         WHERE c.link LIKE '%$module%'
1224                         AND p.id = $form->{id}
1225                         ORDER BY c.accno|;
1226   } else {
1227     $query = qq|SELECT c.accno, c.description, c.link, c.id,
1228                 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1229                 FROM chart c, defaults d
1230                 WHERE c.link LIKE '%$module%'
1231                 ORDER BY c.accno|;
1232   }
1233
1234   my $sth = $dbh->prepare($query);
1235   $sth->execute || $form->dberror($query);
1236   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1237     foreach my $key (split /:/, $ref->{link}) {
1238       if ($key =~ /$module/) {
1239         if (   ($ref->{id} eq $ref->{inventory_accno_id})
1240             || ($ref->{id} eq $ref->{income_accno_id})
1241             || ($ref->{id} eq $ref->{expense_accno_id})) {
1242           push @{ $form->{"${module}_links"}{$key} },
1243             { accno       => $ref->{accno},
1244               description => $ref->{description},
1245               selected    => "selected" };
1246             } else {
1247           push @{ $form->{"${module}_links"}{$key} },
1248             { accno       => $ref->{accno},
1249               description => $ref->{description},
1250               selected    => "" };
1251         }
1252       }
1253     }
1254   }
1255   $sth->finish;
1256
1257   if ($form->{id}) {
1258     $query = qq|SELECT weightunit
1259                 FROM defaults|;
1260     $sth = $dbh->prepare($query);
1261     $sth->execute || $form->dberror($query);
1262
1263     ($form->{weightunit}) = $sth->fetchrow_array;
1264     $sth->finish;
1265
1266   } else {
1267     $query = qq|SELECT weightunit, current_date
1268                 FROM defaults|;
1269     $sth = $dbh->prepare($query);
1270     $sth->execute || $form->dberror($query);
1271
1272     ($form->{weightunit}, $form->{priceupdate}) = $sth->fetchrow_array;
1273     $sth->finish;
1274   }
1275
1276   $dbh->disconnect;
1277   $main::lxdebug->leave_sub();
1278 }
1279
1280 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1281 sub get_parts {
1282   $main::lxdebug->enter_sub();
1283
1284   my ($self, $myconfig, $form, $sortorder) = @_;
1285   my $dbh   = $form->dbconnect($myconfig);
1286   my $order = " p.partnumber";
1287   my $where = "1 = 1";
1288
1289   if ($sortorder eq "all") {
1290     $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1291     $where .= " AND p.description LIKE '%$form->{description}%'";
1292   } else {
1293     if ($sortorder eq "partnumber") {
1294       $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1295       $order = qq|p.$sortorder|;
1296     }
1297     if ($sortorder eq "description") {
1298       $where .= " AND p.description LIKE '%$form->{description}%'";
1299     }
1300   }
1301
1302   my $query =
1303     qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
1304   my $sth = $dbh->prepare($query);
1305   $sth->execute || $self->dberror($query);
1306   my $j = 0;
1307   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1308     if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1309     } else {
1310       $j++;
1311       $form->{"id_$j"}          = $ref->{id};
1312       $form->{"partnumber_$j"}  = $ref->{partnumber};
1313       $form->{"description_$j"} = $ref->{description};
1314       $form->{"unit_$j"}        = $ref->{unit};
1315       $form->{"sellprice_$j"}   = $ref->{sellprice};
1316       $form->{"soldtotal_$j"}   = get_soldtotal($dbh, $ref->{id});
1317     }    #fi
1318   }    #while
1319   $form->{rows} = $j;
1320   $sth->finish;
1321   $dbh->disconnect;
1322
1323   $main::lxdebug->leave_sub();
1324
1325   return $self;
1326 }    #end get_parts()
1327
1328 # gets sum of sold part with part_id
1329 sub get_soldtotal {
1330   $main::lxdebug->enter_sub();
1331
1332   my ($dbh, $id) = @_;
1333
1334   my $query =
1335     qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
1336
1337   my $sth = $dbh->prepare($query);
1338   $sth->execute || $form->dberror($query);
1339
1340   my $sum = 0;
1341   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1342
1343     $sum = $ref->{totalsold};
1344   }    #while
1345   $sth->finish;
1346
1347   if ($sum eq undef) {
1348     $sum = 0;
1349   }    #fi
1350
1351   $main::lxdebug->leave_sub();
1352
1353   return $sum;
1354 }    #end get_soldtotal
1355
1356 sub retrieve_item {
1357   $main::lxdebug->enter_sub();
1358
1359   my ($self, $myconfig, $form) = @_;
1360   my $i     = $form->{rowcount};
1361   my $where = "NOT p.obsolete = '1'";
1362
1363   if ($form->{"partnumber_$i"}) {
1364     my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1365     $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1366   }
1367   if ($form->{"description_$i"}) {
1368     my $description = $form->like(lc $form->{"description_$i"});
1369     $where .= " AND lower(p.description) LIKE '$description'";
1370   }
1371
1372   if ($form->{"partsgroup_$i"}) {
1373     my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1374     $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1375   }
1376
1377   if ($form->{"description_$i"}) {
1378     $where .= " ORDER BY description";
1379   } else {
1380     $where .= " ORDER BY partnumber";
1381   }
1382
1383   # connect to database
1384   my $dbh = $form->dbconnect($myconfig);
1385
1386   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1387                         p.listprice,
1388                         c1.accno AS inventory_accno,
1389                         c2.accno AS income_accno,
1390                         c3.accno AS expense_accno,
1391                  p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1392                  pg.partsgroup
1393                  FROM parts p
1394                  LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1395                  LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1396                  LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1397                  LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1398                  WHERE $where|;
1399   my $sth = $dbh->prepare($query);
1400   $sth->execute || $form->dberror($query);
1401
1402   #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1403
1404   # get tax rates and description
1405   #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
1406   #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1407   #           FROM chart c, tax t
1408   #           WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1409   #           ORDER BY accno|;
1410   # $stw = $dbh->prepare($query);
1411   #$stw->execute || $form->dberror($query);
1412
1413   #$ref->{taxaccounts} = "";
1414   #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1415
1416   #   $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1417   #  $form->{"$ptr->{accno}_description"} = $ptr->{description};
1418   #   $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1419   #   $form->{taxaccounts} .= "$ptr->{accno} ";
1420   #   $ref->{taxaccounts} .= "$ptr->{accno} ";
1421
1422   #}
1423
1424   #$stw->finish;
1425   #chop $ref->{taxaccounts};
1426
1427   push @{ $form->{item_list} }, $ref;
1428
1429   #}
1430   $sth->finish;
1431   $dbh->disconnect;
1432
1433   $main::lxdebug->leave_sub();
1434 }
1435
1436 1;