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