Kosmetik: Perltidy-Lauf nach den Einstellungen in doc/programmierrichtlinien.txt...
[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   my $query = qq|DELETE FROM parts
722                  WHERE id = $form->{id}|;
723   $dbh->do($query) || $form->dberror($query);
724
725   $query = qq|DELETE FROM partstax
726               WHERE parts_id = $form->{id}|;
727   $dbh->do($query) || $form->dberror($query);
728
729   # check if it is a part, assembly or service
730   if ($form->{item} ne 'service') {
731     $query = qq|DELETE FROM makemodel
732                 WHERE parts_id = $form->{id}|;
733     $dbh->do($query) || $form->dberror($query);
734   }
735
736   if ($form->{item} eq 'assembly') {
737
738     # delete inventory
739     $query = qq|DELETE FROM inventory
740                 WHERE parts_id = $form->{id}|;
741     $dbh->do($query) || $form->dberror($query);
742
743     $query = qq|DELETE FROM assembly
744                 WHERE id = $form->{id}|;
745     $dbh->do($query) || $form->dberror($query);
746   }
747
748   if ($form->{item} eq 'alternate') {
749     $query = qq|DELETE FROM alternate
750                 WHERE id = $form->{id}|;
751     $dbh->do($query) || $form->dberror($query);
752   }
753
754   # commit
755   my $rc = $dbh->commit;
756   $dbh->disconnect;
757
758   $main::lxdebug->leave_sub();
759
760   return $rc;
761 }
762
763 sub assembly_item {
764   $main::lxdebug->enter_sub();
765
766   my ($self, $myconfig, $form) = @_;
767
768   my $i = $form->{assembly_rows};
769   my $var;
770   my $where = "1 = 1";
771
772   if ($form->{"partnumber_$i"}) {
773     $var = $form->like(lc $form->{"partnumber_$i"});
774     $where .= " AND lower(p.partnumber) LIKE '$var'";
775   }
776   if ($form->{"description_$i"}) {
777     $var = $form->like(lc $form->{"description_$i"});
778     $where .= " AND lower(p.description) LIKE '$var'";
779   }
780   if ($form->{"partsgroup_$i"}) {
781     $var = $form->like(lc $form->{"partsgroup_$i"});
782     $where .= " AND lower(pg.partsgroup) LIKE '$var'";
783   }
784
785   if ($form->{id}) {
786     $where .= " AND NOT p.id = $form->{id}";
787   }
788
789   if ($partnumber) {
790     $where .= " ORDER BY p.partnumber";
791   } else {
792     $where .= " ORDER BY p.description";
793   }
794
795   # connect to database
796   my $dbh = $form->dbconnect($myconfig);
797
798   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
799                  p.weight, p.onhand, p.unit,
800                  pg.partsgroup
801                  FROM parts p
802                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
803                  WHERE $where|;
804   my $sth = $dbh->prepare($query);
805   $sth->execute || $form->dberror($query);
806
807   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
808     push @{ $form->{item_list} }, $ref;
809   }
810
811   $sth->finish;
812   $dbh->disconnect;
813
814   $main::lxdebug->leave_sub();
815 }
816
817 sub all_parts {
818   $main::lxdebug->enter_sub();
819
820   my ($self, $myconfig, $form) = @_;
821
822   my $where = '1 = 1';
823   my $var;
824
825   my $group;
826   my $limit;
827
828   foreach my $item (qw(partnumber drawing microfiche make model)) {
829     if ($form->{$item}) {
830       $var = $form->like(lc $form->{$item});
831
832       # make will build later Bugfix 145
833       if ($item ne 'make') {
834         $where .= " AND lower(p.$item) LIKE '$var'";
835       }
836     }
837   }
838
839   # special case for description
840   if ($form->{description}) {
841     unless (   $form->{bought}
842             || $form->{sold}
843             || $form->{onorder}
844             || $form->{ordered}
845             || $form->{rfq}
846             || $form->{quoted}) {
847       $var = $form->like(lc $form->{description});
848       $where .= " AND lower(p.description) LIKE '$var'";
849     }
850   }
851
852   # special case for serialnumber
853   if ($form->{l_serialnumber}) {
854     if ($form->{serialnumber}) {
855       $var = $form->like(lc $form->{serialnumber});
856       $where .= " AND lower(serialnumber) LIKE '$var'";
857     }
858   }
859
860   if ($form->{searchitems} eq 'part') {
861     $where .= " AND p.inventory_accno_id > 0";
862   }
863   if ($form->{searchitems} eq 'assembly') {
864     $form->{bought} = "";
865     $where .= " AND p.assembly = '1'";
866   }
867   if ($form->{searchitems} eq 'service') {
868     $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
869
870     # irrelevant for services
871     $form->{make} = $form->{model} = "";
872   }
873
874   # items which were never bought, sold or on an order
875   if ($form->{itemstatus} eq 'orphaned') {
876     $form->{onhand}  = $form->{short}   = 0;
877     $form->{bought}  = $form->{sold}    = 0;
878     $form->{onorder} = $form->{ordered} = 0;
879     $form->{rfq}     = $form->{quoted}  = 0;
880
881     $form->{transdatefrom} = $form->{transdateto} = "";
882
883     $where .= " AND p.onhand = 0
884                 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
885                                  WHERE p.id = i.parts_id)
886                 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
887                                  WHERE p.id = a.parts_id)
888                 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
889                                  WHERE p.id = o.parts_id)";
890   }
891
892   if ($form->{itemstatus} eq 'active') {
893     $where .= " AND p.obsolete = '0'";
894   }
895   if ($form->{itemstatus} eq 'obsolete') {
896     $where .= " AND p.obsolete = '1'";
897     $form->{onhand} = $form->{short} = 0;
898   }
899   if ($form->{itemstatus} eq 'onhand') {
900     $where .= " AND p.onhand > 0";
901   }
902   if ($form->{itemstatus} eq 'short') {
903     $where .= " AND p.onhand < p.rop";
904   }
905   if ($form->{make}) {
906     $var = $form->like(lc $form->{make});
907     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
908                            FROM makemodel m WHERE lower(m.make) LIKE '$var')";
909   }
910   if ($form->{model}) {
911     $var = $form->like(lc $form->{model});
912     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
913                            FROM makemodel m WHERE lower(m.model) LIKE '$var')";
914   }
915   if ($form->{partsgroup}) {
916     $var = $form->like(lc $form->{partsgroup});
917     $where .= " AND lower(pg.partsgroup) LIKE '$var'";
918   }
919   if ($form->{l_soldtotal}) {
920     $where .= " AND p.id=i.parts_id AND  i.qty >= 0";
921     $group =
922       " GROUP BY  p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
923   }
924   if ($form->{top100}) {
925     $limit = " LIMIT 100";
926   }
927
928   # tables revers?
929   if ($form->{revers} == 1) {
930     $form->{desc} = " DESC";
931   } else {
932     $form->{desc} = "";
933   }
934
935   # connect to database
936   my $dbh = $form->dbconnect($myconfig);
937
938   my $sortorder = $form->{sort};
939   $sortorder .= $form->{desc};
940   $sortorder = $form->{sort} if $form->{sort};
941
942   my $query = "";
943
944   if ($form->{l_soldtotal}) {
945     $form->{soldtotal} = 'soldtotal';
946     $query =
947       qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
948                 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
949                 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
950                 WHERE $where
951                 $group
952                 ORDER BY $sortorder
953                 $limit|;
954   } else {
955     $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
956                  p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
957                  p.priceupdate, p.image, p.drawing, p.microfiche,
958                  pg.partsgroup
959                  FROM parts p
960                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
961                  WHERE $where
962                  $group
963                  ORDER BY $sortorder|;
964   }
965
966   # rebuild query for bought and sold items
967   if (   $form->{bought}
968       || $form->{sold}
969       || $form->{onorder}
970       || $form->{ordered}
971       || $form->{rfq}
972       || $form->{quoted}) {
973
974     my @a = qw(partnumber description bin priceupdate name);
975
976     push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
977     push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
978     push @a, "quonumber" if ($form->{rfq}     || $form->{quoted});
979
980     my $union = "";
981     $query = "";
982
983     if ($form->{bought} || $form->{sold}) {
984
985       my $invwhere = "$where";
986       $invwhere .= " AND i.assemblyitem = '0'";
987       $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
988         if $form->{transdatefrom};
989       $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
990         if $form->{transdateto};
991
992       if ($form->{description}) {
993         $var = $form->like(lc $form->{description});
994         $invwhere .= " AND lower(i.description) LIKE '$var'";
995       }
996
997       my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
998                     i.qty AS onhand, i.unit, p.bin, i.sellprice,
999                     p.listprice, p.lastcost, p.rop, p.weight,
1000                     p.priceupdate, p.image, p.drawing, p.microfiche,
1001                     pg.partsgroup,
1002                     a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
1003                     ct.name|;
1004
1005       if ($form->{bought}) {
1006         $query = qq|
1007                     SELECT $flds, 'ir' AS module, '' AS type,
1008                     1 AS exchangerate
1009                     FROM invoice i
1010                     JOIN parts p ON (p.id = i.parts_id)
1011                     JOIN ap a ON (a.id = i.trans_id)
1012                     JOIN vendor ct ON (a.vendor_id = ct.id)
1013                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1014                     WHERE $invwhere|;
1015         $union = "
1016                   UNION";
1017       }
1018
1019       if ($form->{sold}) {
1020         $query .= qq|$union
1021                      SELECT $flds, 'is' AS module, '' AS type,
1022                      1 As exchangerate
1023                      FROM invoice i
1024                      JOIN parts p ON (p.id = i.parts_id)
1025                      JOIN ar a ON (a.id = i.trans_id)
1026                      JOIN customer ct ON (a.customer_id = ct.id)
1027                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1028                      WHERE $invwhere|;
1029         $union = "
1030                   UNION";
1031       }
1032     }
1033
1034     if ($form->{onorder} || $form->{ordered}) {
1035       my $ordwhere = "$where
1036                      AND o.quotation = '0'";
1037       $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1038         if $form->{transdatefrom};
1039       $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
1040         if $form->{transdateto};
1041
1042       if ($form->{description}) {
1043         $var = $form->like(lc $form->{description});
1044         $ordwhere .= " AND lower(oi.description) LIKE '$var'";
1045       }
1046
1047       $flds =
1048         qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1049                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1050                  p.listprice, p.lastcost, p.rop, p.weight,
1051                  p.priceupdate, p.image, p.drawing, p.microfiche,
1052                  pg.partsgroup,
1053                  '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1054                  ct.name|;
1055
1056       if ($form->{ordered}) {
1057         $query .= qq|$union
1058                      SELECT $flds, 'oe' AS module, 'sales_order' AS type,
1059                     (SELECT buy FROM exchangerate ex
1060                      WHERE ex.curr = o.curr
1061                      AND ex.transdate = o.transdate) AS exchangerate
1062                      FROM orderitems oi
1063                      JOIN parts p ON (oi.parts_id = p.id)
1064                      JOIN oe o ON (oi.trans_id = o.id)
1065                      JOIN customer ct ON (o.customer_id = ct.id)
1066                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1067                      WHERE $ordwhere
1068                      AND o.customer_id > 0|;
1069         $union = "
1070                   UNION";
1071       }
1072
1073       if ($form->{onorder}) {
1074         $flds =
1075           qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1076                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1077                    p.listprice, p.lastcost, p.rop, p.weight,
1078                    p.priceupdate, p.image, p.drawing, p.microfiche,
1079                    pg.partsgroup,
1080                    '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1081                    ct.name|;
1082
1083         $query .= qq|$union
1084                     SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
1085                     (SELECT sell FROM exchangerate ex
1086                      WHERE ex.curr = o.curr
1087                      AND ex.transdate = o.transdate) AS exchangerate
1088                     FROM orderitems oi
1089                     JOIN parts p ON (oi.parts_id = p.id)
1090                     JOIN oe o ON (oi.trans_id = o.id)
1091                     JOIN vendor ct ON (o.vendor_id = ct.id)
1092                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1093                     WHERE $ordwhere
1094                     AND o.vendor_id > 0|;
1095       }
1096
1097     }
1098
1099     if ($form->{rfq} || $form->{quoted}) {
1100       my $quowhere = "$where
1101                      AND o.quotation = '1'";
1102       $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1103         if $form->{transdatefrom};
1104       $quowhere .= " AND o.transdate <= '$form->{transdateto}'"
1105         if $form->{transdateto};
1106
1107       if ($form->{description}) {
1108         $var = $form->like(lc $form->{description});
1109         $quowhere .= " AND lower(oi.description) LIKE '$var'";
1110       }
1111
1112       $flds =
1113         qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1114                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1115                  p.listprice, p.lastcost, p.rop, p.weight,
1116                  p.priceupdate, p.image, p.drawing, p.microfiche,
1117                  pg.partsgroup,
1118                  '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1119                  ct.name|;
1120
1121       if ($form->{quoted}) {
1122         $query .= qq|$union
1123                      SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
1124                     (SELECT buy FROM exchangerate ex
1125                      WHERE ex.curr = o.curr
1126                      AND ex.transdate = o.transdate) AS exchangerate
1127                      FROM orderitems oi
1128                      JOIN parts p ON (oi.parts_id = p.id)
1129                      JOIN oe o ON (oi.trans_id = o.id)
1130                      JOIN customer ct ON (o.customer_id = ct.id)
1131                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1132                      WHERE $quowhere
1133                      AND o.customer_id > 0|;
1134         $union = "
1135                   UNION";
1136       }
1137
1138       if ($form->{rfq}) {
1139         $flds =
1140           qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1141                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1142                    p.listprice, p.lastcost, p.rop, p.weight,
1143                    p.priceupdate, p.image, p.drawing, p.microfiche,
1144                    pg.partsgroup,
1145                    '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1146                    ct.name|;
1147
1148         $query .= qq|$union
1149                     SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
1150                     (SELECT sell FROM exchangerate ex
1151                      WHERE ex.curr = o.curr
1152                      AND ex.transdate = o.transdate) AS exchangerate
1153                     FROM orderitems oi
1154                     JOIN parts p ON (oi.parts_id = p.id)
1155                     JOIN oe o ON (oi.trans_id = o.id)
1156                     JOIN vendor ct ON (o.vendor_id = ct.id)
1157                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1158                     WHERE $quowhere
1159                     AND o.vendor_id > 0|;
1160       }
1161
1162     }
1163     $query .= qq|
1164                  ORDER BY $sortorder|;
1165
1166   }
1167   my $sth = $dbh->prepare($query);
1168   $sth->execute || $form->dberror($query);
1169
1170   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1171     push @{ $form->{parts} }, $ref;
1172   }
1173
1174   $sth->finish;
1175
1176   # include individual items for assemblies
1177   if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1178     foreach $item (@{ $form->{parts} }) {
1179       push @assemblies, $item;
1180       $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1181                   p.unit, p.bin,
1182                   p.sellprice, p.listprice, p.lastcost,
1183                   p.rop, p.weight, p.priceupdate,
1184                   p.image, p.drawing, p.microfiche
1185                   FROM parts p, assembly a
1186                   WHERE p.id = a.parts_id
1187                   AND a.id = $item->{id}|;
1188
1189       $sth = $dbh->prepare($query);
1190       $sth->execute || $form->dberror($query);
1191
1192       while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1193         $ref->{assemblyitem} = 1;
1194         push @assemblies, $ref;
1195       }
1196       $sth->finish;
1197
1198       push @assemblies, { id => $item->{id} };
1199
1200     }
1201
1202     # copy assemblies to $form->{parts}
1203     @{ $form->{parts} } = @assemblies;
1204   }
1205
1206   $dbh->disconnect;
1207   $main::lxdebug->leave_sub();
1208 }
1209
1210 sub create_links {
1211   $main::lxdebug->enter_sub();
1212
1213   my ($self, $module, $myconfig, $form) = @_;
1214
1215   # connect to database
1216   my $dbh = $form->dbconnect($myconfig);
1217
1218   if ($form->{id}) {
1219     $query = qq|SELECT c.accno, c.description, c.link, c.id,
1220                         p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1221                         FROM chart c, parts p
1222                         WHERE c.link LIKE '%$module%'
1223                         AND p.id = $form->{id}
1224                         ORDER BY c.accno|;
1225   } else {
1226     $query = qq|SELECT c.accno, c.description, c.link, c.id,
1227                 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1228                 FROM chart c, defaults d
1229                 WHERE c.link LIKE '%$module%'
1230                 ORDER BY c.accno|;
1231   }
1232
1233   my $sth = $dbh->prepare($query);
1234   $sth->execute || $form->dberror($query);
1235   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1236     foreach my $key (split /:/, $ref->{link}) {
1237       if ($key =~ /$module/) {
1238         if (   ($ref->{id} eq $ref->{inventory_accno_id})
1239             || ($ref->{id} eq $ref->{income_accno_id})
1240             || ($ref->{id} eq $ref->{expense_accno_id})) {
1241           push @{ $form->{"${module}_links"}{$key} },
1242             { accno       => $ref->{accno},
1243               description => $ref->{description},
1244               selected    => "selected" };
1245             } else {
1246           push @{ $form->{"${module}_links"}{$key} },
1247             { accno       => $ref->{accno},
1248               description => $ref->{description},
1249               selected    => "" };
1250         }
1251       }
1252     }
1253   }
1254   $sth->finish;
1255
1256   if ($form->{id}) {
1257     $query = qq|SELECT weightunit
1258                 FROM defaults|;
1259     $sth = $dbh->prepare($query);
1260     $sth->execute || $form->dberror($query);
1261
1262     ($form->{weightunit}) = $sth->fetchrow_array;
1263     $sth->finish;
1264
1265   } else {
1266     $query = qq|SELECT weightunit, current_date
1267                 FROM defaults|;
1268     $sth = $dbh->prepare($query);
1269     $sth->execute || $form->dberror($query);
1270
1271     ($form->{weightunit}, $form->{priceupdate}) = $sth->fetchrow_array;
1272     $sth->finish;
1273   }
1274
1275   $dbh->disconnect;
1276   $main::lxdebug->leave_sub();
1277 }
1278
1279 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1280 sub get_parts {
1281   $main::lxdebug->enter_sub();
1282
1283   my ($self, $myconfig, $form, $sortorder) = @_;
1284   my $dbh   = $form->dbconnect($myconfig);
1285   my $order = " p.partnumber";
1286   my $where = "1 = 1";
1287
1288   if ($sortorder eq "all") {
1289     $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1290     $where .= " AND p.description LIKE '%$form->{description}%'";
1291   } else {
1292     if ($sortorder eq "partnumber") {
1293       $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1294       $order = qq|p.$sortorder|;
1295     }
1296     if ($sortorder eq "description") {
1297       $where .= " AND p.description LIKE '%$form->{description}%'";
1298     }
1299   }
1300
1301   my $query =
1302     qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
1303   my $sth = $dbh->prepare($query);
1304   $sth->execute || $self->dberror($query);
1305   my $j = 0;
1306   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1307     if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1308     } else {
1309       $j++;
1310       $form->{"id_$j"}          = $ref->{id};
1311       $form->{"partnumber_$j"}  = $ref->{partnumber};
1312       $form->{"description_$j"} = $ref->{description};
1313       $form->{"unit_$j"}        = $ref->{unit};
1314       $form->{"sellprice_$j"}   = $ref->{sellprice};
1315       $form->{"soldtotal_$j"}   = get_soldtotal($dbh, $ref->{id});
1316     }    #fi
1317   }    #while
1318   $form->{rows} = $j;
1319   $sth->finish;
1320   $dbh->disconnect;
1321
1322   $main::lxdebug->leave_sub();
1323
1324   return $self;
1325 }    #end get_parts()
1326
1327 # gets sum of sold part with part_id
1328 sub get_soldtotal {
1329   $main::lxdebug->enter_sub();
1330
1331   my ($dbh, $id) = @_;
1332
1333   my $query =
1334     qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
1335
1336   my $sth = $dbh->prepare($query);
1337   $sth->execute || $form->dberror($query);
1338
1339   my $sum = 0;
1340   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1341
1342     $sum = $ref->{totalsold};
1343   }    #while
1344   $sth->finish;
1345
1346   if ($sum eq undef) {
1347     $sum = 0;
1348   }    #fi
1349
1350   $main::lxdebug->leave_sub();
1351
1352   return $sum;
1353 }    #end get_soldtotal
1354
1355 sub retrieve_item {
1356   $main::lxdebug->enter_sub();
1357
1358   my ($self, $myconfig, $form) = @_;
1359   my $i     = $form->{rowcount};
1360   my $where = "NOT p.obsolete = '1'";
1361
1362   if ($form->{"partnumber_$i"}) {
1363     my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1364     $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1365   }
1366   if ($form->{"description_$i"}) {
1367     my $description = $form->like(lc $form->{"description_$i"});
1368     $where .= " AND lower(p.description) LIKE '$description'";
1369   }
1370
1371   if ($form->{"partsgroup_$i"}) {
1372     my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1373     $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1374   }
1375
1376   if ($form->{"description_$i"}) {
1377     $where .= " ORDER BY description";
1378   } else {
1379     $where .= " ORDER BY partnumber";
1380   }
1381
1382   # connect to database
1383   my $dbh = $form->dbconnect($myconfig);
1384
1385   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1386                         p.listprice,
1387                         c1.accno AS inventory_accno,
1388                         c2.accno AS income_accno,
1389                         c3.accno AS expense_accno,
1390                  p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1391                  pg.partsgroup
1392                  FROM parts p
1393                  LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1394                  LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1395                  LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1396                  LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1397                  WHERE $where|;
1398   my $sth = $dbh->prepare($query);
1399   $sth->execute || $form->dberror($query);
1400
1401   #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1402
1403   # get tax rates and description
1404   #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
1405   #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1406   #           FROM chart c, tax t
1407   #           WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1408   #           ORDER BY accno|;
1409   # $stw = $dbh->prepare($query);
1410   #$stw->execute || $form->dberror($query);
1411
1412   #$ref->{taxaccounts} = "";
1413   #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1414
1415   #   $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1416   #  $form->{"$ptr->{accno}_description"} = $ptr->{description};
1417   #   $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1418   #   $form->{taxaccounts} .= "$ptr->{accno} ";
1419   #   $ref->{taxaccounts} .= "$ptr->{accno} ";
1420
1421   #}
1422
1423   #$stw->finish;
1424   #chop $ref->{taxaccounts};
1425
1426   push @{ $form->{item_list} }, $ref;
1427
1428   #}
1429   $sth->finish;
1430   $dbh->disconnect;
1431
1432   $main::lxdebug->leave_sub();
1433 }
1434
1435 1;