Locale-Anpassung nach Entfernen von ADR.
[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   # get translations
197   $form->{language_values} = "";
198   $query = qq|SELECT language_id, translation FROM translation WHERE parts_id = $form->{id}|;
199   $trq = $dbh->prepare($query);
200   $trq->execute || $form->dberror($query);
201   while ($tr = $trq->fetchrow_hashref(NAME_lc)) {
202     $form->{language_values} .= "---+++---".$tr->{language_id}."--++--".$tr->{translation};
203   }
204   $trq->finish;
205
206   # now get accno for taxes
207   $query = qq|SELECT c.accno
208               FROM chart c, partstax pt
209               WHERE pt.chart_id = c.id
210               AND pt.parts_id = $form->{id}|;
211
212   $sth = $dbh->prepare($query);
213   $sth->execute || $form->dberror($query);
214
215   while (($key) = $sth->fetchrow_array) {
216     $form->{amount}{$key} = $key;
217   }
218
219   $sth->finish;
220
221   # is it an orphan
222   $query = qq|SELECT i.parts_id
223               FROM invoice i
224               WHERE i.parts_id = $form->{id}
225             UNION
226               SELECT o.parts_id
227               FROM orderitems o
228               WHERE o.parts_id = $form->{id}
229             UNION
230               SELECT a.parts_id
231               FROM assembly a
232               WHERE a.parts_id = $form->{id}|;
233   $sth = $dbh->prepare($query);
234   $sth->execute || $form->dberror($query);
235
236   ($form->{orphaned}) = $sth->fetchrow_array;
237   $form->{orphaned} = !$form->{orphaned};
238   $sth->finish;
239
240   $form->{"unit_changeable"} = 1;
241   foreach my $table (qw(invoice assembly orderitems inventory license)) {
242     $query = "SELECT COUNT(*) FROM $table WHERE parts_id = ?";
243     my ($count) = $dbh->selectrow_array($query, undef, $form->{"id"});
244     $form->dberror($query . " (" . $form->{"id"} . ")") if ($dbh->err);
245
246     if ($count) {
247       $form->{"unit_changeable"} = 0;
248       last;
249     }
250   }
251
252   $dbh->disconnect;
253
254   $main::lxdebug->leave_sub();
255 }
256
257 sub get_pricegroups {
258   $main::lxdebug->enter_sub();
259
260   my ($self, $myconfig, $form) = @_;
261   my $dbh                  = $form->dbconnect($myconfig);
262   my $i                    = 1;
263   my @pricegroups_not_used = ();
264
265   # get pricegroups
266   my $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
267
268   my $pkq = $dbh->prepare($query);
269   $pkq->execute || $form->dberror($query);
270   while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
271     push @{ $form->{PRICEGROUPS} }, $pkr;
272   }
273   $pkq->finish;
274
275   #find not used pricegroups
276   while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
277     push @pricegroups_not_used, $tmp;
278   }
279
280   # if not used pricegroups are avaible
281   if (@pricegroups_not_used) {
282
283     foreach $name (@pricegroups_not_used) {
284       $form->{"klass_$i"} = "$name->{id}";
285       $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
286       $form->{"price_$i"} =
287         $form->format_amount($myconfig, $form->{"price_$i"}, 5);
288       $form->{"pricegroup_id_$i"} = "$name->{id}";
289       $form->{"pricegroup_$i"}    = "$name->{pricegroup}";
290       $i++;
291     }
292   }
293
294   #correct rows
295   $form->{price_rows} = $i - 1;
296
297   $dbh->disconnect;
298
299   $main::lxdebug->leave_sub();
300 }
301
302 sub retrieve_buchungsgruppen {
303   $main::lxdebug->enter_sub();
304
305   my ($self, $myconfig, $form) = @_;
306
307   my ($query, $sth);
308
309   my $dbh = $form->dbconnect($myconfig);
310
311   # get buchungsgruppen
312   $query = qq|SELECT id, description
313               FROM buchungsgruppen|;
314   $sth = $dbh->prepare($query);
315   $sth->execute || $form->dberror($query);
316
317   $form->{BUCHUNGSGRUPPEN} = [];
318   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
319     push(@{ $form->{BUCHUNGSGRUPPEN} }, $ref);
320   }
321   $sth->finish;
322
323   $main::lxdebug->leave_sub();
324 }
325
326 sub save {
327   $main::lxdebug->enter_sub();
328
329   my ($self, $myconfig, $form) = @_;
330   $form->{IC_expense} = "1000";
331   $form->{IC_income} = "2000";
332
333   if ($form->{item} ne 'service') {
334     $form->{IC} = $form->{IC_expense};
335   }
336
337   ($form->{inventory_accno}) = split(/--/, $form->{IC});
338   ($form->{expense_accno})   = split(/--/, $form->{IC_expense});
339   ($form->{income_accno})    = split(/--/, $form->{IC_income});
340
341   # connect to database, turn off AutoCommit
342   my $dbh = $form->dbconnect_noauto($myconfig);
343
344   # save the part
345   # make up a unique handle and store in partnumber field
346   # then retrieve the record based on the unique handle to get the id
347   # replace the partnumber field with the actual variable
348   # add records for makemodel
349
350   # if there is a $form->{id} then replace the old entry
351   # delete all makemodel entries and add the new ones
352
353   # escape '
354   map { $form->{$_} =~ s/\'/\'\'/g } qw(partnumber description notes unit);
355
356   # undo amount formatting
357   map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) }
358     qw(rop weight listprice sellprice gv lastcost stock);
359
360   # set date to NULL if nothing entered
361   $form->{priceupdate} =
362     ($form->{priceupdate}) ? qq|'$form->{priceupdate}'| : "NULL";
363
364   $form->{makemodel} = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
365
366   $form->{alternate} = 0;
367   $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
368   $form->{obsolete} *= 1;
369   $form->{shop}     *= 1;
370   $form->{onhand}   *= 1;
371   $form->{ve}       *= 1;
372   $form->{ge}       *= 1;
373   $form->{alu}       *= 1;
374   $form->{buchungsgruppen_id}       *= 1;
375   $form->{not_discountable}       *= 1;
376   $form->{payment_id}       *= 1;
377
378   my ($query, $sth);
379
380   if ($form->{id}) {
381
382     # get old price
383     $query = qq|SELECT p.sellprice, p.weight
384                 FROM parts p
385                 WHERE p.id = $form->{id}|;
386     $sth = $dbh->prepare($query);
387     $sth->execute || $form->dberror($query);
388     my ($sellprice, $weight) = $sth->fetchrow_array;
389     $sth->finish;
390
391     # if item is part of an assembly adjust all assemblies
392     $query = qq|SELECT a.id, a.qty
393                 FROM assembly a
394                 WHERE a.parts_id = $form->{id}|;
395     $sth = $dbh->prepare($query);
396     $sth->execute || $form->dberror($query);
397     while (my ($id, $qty) = $sth->fetchrow_array) {
398       &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
399     }
400     $sth->finish;
401
402     if ($form->{item} ne 'service') {
403
404       # delete makemodel records
405       $query = qq|DELETE FROM makemodel
406                   WHERE parts_id = $form->{id}|;
407       $dbh->do($query) || $form->dberror($query);
408     }
409
410     if ($form->{item} eq 'assembly') {
411       if ($form->{onhand} != 0) {
412         &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
413       }
414
415       # delete assembly records
416       $query = qq|DELETE FROM assembly
417                   WHERE id = $form->{id}|;
418       $dbh->do($query) || $form->dberror($query);
419
420       $form->{onhand} += $form->{stock};
421     }
422
423     # delete tax records
424     $query = qq|DELETE FROM partstax
425                 WHERE parts_id = $form->{id}|;
426     $dbh->do($query) || $form->dberror($query);
427
428     # delete translations
429     $query = qq|DELETE FROM translation
430                 WHERE parts_id = $form->{id}|;
431     $dbh->do($query) || $form->dberror($query);
432
433   } else {
434     my $uid = rand() . time;
435     $uid .= $form->{login};
436
437     $query = qq|SELECT p.id FROM parts p
438                 WHERE p.partnumber = '$form->{partnumber}'|;
439     $sth = $dbh->prepare($query);
440     $sth->execute || $form->dberror($query);
441     ($form->{id}) = $sth->fetchrow_array;
442     $sth->finish;
443
444     if ($form->{id} ne "") {
445       $main::lxdebug->leave_sub();
446       return 3;
447     }
448     $query = qq|INSERT INTO parts (partnumber, description)
449                 VALUES ('$uid', 'dummy')|;
450     $dbh->do($query) || $form->dberror($query);
451
452     $query = qq|SELECT p.id FROM parts p
453                 WHERE p.partnumber = '$uid'|;
454     $sth = $dbh->prepare($query);
455     $sth->execute || $form->dberror($query);
456
457     ($form->{id}) = $sth->fetchrow_array;
458     $sth->finish;
459
460     $form->{orphaned} = 1;
461     $form->{onhand} = $form->{stock} if $form->{item} eq 'assembly';
462     if ($form->{partnumber} eq "" && $form->{inventory_accno} eq "") {
463       $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
464     }
465     if ($form->{partnumber} eq "" && $form->{inventory_accno} ne "") {
466       $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
467     }
468
469   }
470   my $partsgroup_id = 0;
471
472   if ($form->{partsgroup}) {
473     ($partsgroup, $partsgroup_id) = split /--/, $form->{partsgroup};
474   }
475
476   $query = qq|UPDATE parts SET
477               partnumber = '$form->{partnumber}',
478               description = '$form->{description}',
479               makemodel = '$form->{makemodel}',
480               alternate = '$form->{alternate}',
481               assembly = '$form->{assembly}',
482               listprice = $form->{listprice},
483               sellprice = $form->{sellprice},
484               lastcost = $form->{lastcost},
485               weight = $form->{weight},
486               priceupdate = $form->{priceupdate},
487               unit = '$form->{unit}',
488               notes = '$form->{notes}',
489               formel = '$form->{formel}',
490               rop = $form->{rop},
491               bin = '$form->{bin}',
492               buchungsgruppen_id = '$form->{buchungsgruppen_id}',
493               payment_id = '$form->{payment_id}',
494               inventory_accno_id = (SELECT c.id FROM chart c
495                                     WHERE c.accno = '$form->{inventory_accno}'),
496               income_accno_id = (SELECT c.id FROM chart c
497                                  WHERE c.accno = '$form->{income_accno}'),
498               expense_accno_id = (SELECT c.id FROM chart c
499                                   WHERE c.accno = '$form->{expense_accno}'),
500               obsolete = '$form->{obsolete}',
501               image = '$form->{image}',
502               drawing = '$form->{drawing}',
503               shop = '$form->{shop}',
504               ve = '$form->{ve}',
505               gv = '$form->{gv}',
506               alu = '$form->{alu}',
507               not_discountable = '$form->{not_discountable}',
508               microfiche = '$form->{microfiche}',
509               partsgroup_id = $partsgroup_id
510               WHERE id = $form->{id}|;
511   $dbh->do($query) || $form->dberror($query);
512
513   # delete translation records
514   $query = qq|DELETE FROM translation
515               WHERE parts_id = $form->{id}|;
516   $dbh->do($query) || $form->dberror($query);
517
518   if ($form->{language_values} ne "") {
519     split /---\+\+\+---/,$form->{language_values};
520     foreach $item (@_) {
521       my ($language_id, $translation, $longdescription) = split /--\+\+--/, $item;
522       if ($translation ne "") {
523         $query = qq|INSERT into translation (parts_id, language_id, translation, longdescription) VALUES
524                     ($form->{id}, $language_id, | . $dbh->quote($translation) . qq|, | . $dbh->quote($longdescription) . qq| )|;
525         $dbh->do($query) || $form->dberror($query);
526       }
527     }
528   }
529   # delete price records
530   $query = qq|DELETE FROM prices
531               WHERE parts_id = $form->{id}|;
532   $dbh->do($query) || $form->dberror($query);
533   # insert price records only if different to sellprice
534   for my $i (1 .. $form->{price_rows}) {
535     if ($form->{"price_$i"} eq "0") {
536       $form->{"price_$i"} = $form->{sellprice};
537     }
538     if (
539         (   $form->{"price_$i"}
540          || $form->{"klass_$i"}
541          || $form->{"pricegroup_id_$i"})
542         and $form->{"price_$i"} != $form->{sellprice}
543       ) {
544       $klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
545       $price = $form->parse_amount($myconfig, $form->{"price_$i"});
546       $pricegroup_id =
547         $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
548       $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price)
549                   VALUES($form->{id},$pricegroup_id,$price)|;
550       $dbh->do($query) || $form->dberror($query);
551     }
552   }
553
554   # insert makemodel records
555   unless ($form->{item} eq 'service') {
556     for my $i (1 .. $form->{makemodel_rows}) {
557       if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
558         map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
559
560         $query = qq|INSERT INTO makemodel (parts_id, make, model)
561                     VALUES ($form->{id},
562                     '$form->{"make_$i"}', '$form->{"model_$i"}')|;
563         $dbh->do($query) || $form->dberror($query);
564       }
565     }
566   }
567
568   # insert taxes
569   foreach $item (split / /, $form->{taxaccounts}) {
570     if ($form->{"IC_tax_$item"}) {
571       $query = qq|INSERT INTO partstax (parts_id, chart_id)
572                   VALUES ($form->{id},
573                           (SELECT c.id
574                            FROM chart c
575                            WHERE c.accno = '$item'))|;
576       $dbh->do($query) || $form->dberror($query);
577     }
578   }
579
580   # add assembly records
581   if ($form->{item} eq 'assembly') {
582
583     for my $i (1 .. $form->{assembly_rows}) {
584       $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
585
586       if ($form->{"qty_$i"} != 0) {
587         $form->{"bom_$i"} *= 1;
588         $query = qq|INSERT INTO assembly (id, parts_id, qty, bom)
589                     VALUES ($form->{id}, $form->{"id_$i"},
590                     $form->{"qty_$i"}, '$form->{"bom_$i"}')|;
591         $dbh->do($query) || $form->dberror($query);
592       }
593     }
594
595     # adjust onhand for the parts
596     if ($form->{onhand} != 0) {
597       &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
598     }
599
600     @a = localtime;
601     $a[5] += 1900;
602     $a[4]++;
603     my $shippingdate = "$a[5]-$a[4]-$a[3]";
604
605     $form->get_employee($dbh);
606
607     # add inventory record
608     $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
609                 shippingdate, employee_id) VALUES (
610                 0, $form->{id}, $form->{stock}, '$shippingdate',
611                 $form->{employee_id})|;
612     $dbh->do($query) || $form->dberror($query);
613
614   }
615
616   #set expense_accno=inventory_accno if they are different => bilanz
617   $vendor_accno =
618     ($form->{expense_accno} != $form->{inventory_accno})
619     ? $form->{inventory_accno}
620     : $form->{expense_accno};
621
622   # get tax rates and description
623   $accno_id =
624     ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
625   $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
626               FROM chart c, tax t
627               WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
628               ORDER BY c.accno|;
629   $stw = $dbh->prepare($query);
630
631   $stw->execute || $form->dberror($query);
632
633   $form->{taxaccount} = "";
634   while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
635
636     #    if ($customertax{$ref->{accno}}) {
637     $form->{taxaccount} .= "$ptr->{accno} ";
638     if (!($form->{taxaccount2} =~ /$ptr->{accno}/)) {
639       $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
640       $form->{"$ptr->{accno}_description"} = $ptr->{description};
641       $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
642       $form->{taxaccount2} .= " $ptr->{accno} ";
643     }
644
645   }
646
647   # commit
648   my $rc = $dbh->commit;
649   $dbh->disconnect;
650
651   $main::lxdebug->leave_sub();
652
653   return $rc;
654 }
655
656 sub update_assembly {
657   $main::lxdebug->enter_sub();
658
659   my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
660
661   my $query = qq|SELECT a.id, a.qty
662                  FROM assembly a
663                  WHERE a.parts_id = $id|;
664   my $sth = $dbh->prepare($query);
665   $sth->execute || $form->dberror($query);
666
667   while (my ($pid, $aqty) = $sth->fetchrow_array) {
668     &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
669   }
670   $sth->finish;
671
672   $query = qq|UPDATE parts
673               SET sellprice = sellprice +
674                   $qty * ($form->{sellprice} - $sellprice),
675                   weight = weight +
676                   $qty * ($form->{weight} - $weight)
677               WHERE id = $id|;
678   $dbh->do($query) || $form->dberror($query);
679
680   $main::lxdebug->leave_sub();
681 }
682
683 sub retrieve_assemblies {
684   $main::lxdebug->enter_sub();
685
686   my ($self, $myconfig, $form) = @_;
687
688   # connect to database
689   my $dbh = $form->dbconnect($myconfig);
690
691   my $where = '1 = 1';
692
693   if ($form->{partnumber}) {
694     my $partnumber = $form->like(lc $form->{partnumber});
695     $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
696   }
697
698   if ($form->{description}) {
699     my $description = $form->like(lc $form->{description});
700     $where .= " AND lower(p.description) LIKE '$description'";
701   }
702   $where .= " AND NOT p.obsolete = '1'";
703
704   # retrieve assembly items
705   my $query = qq|SELECT p.id, p.partnumber, p.description,
706                  p.bin, p.onhand, p.rop,
707                    (SELECT sum(p2.inventory_accno_id)
708                     FROM parts p2, assembly a
709                     WHERE p2.id = a.parts_id
710                     AND a.id = p.id) AS inventory
711                  FROM parts p
712                  WHERE $where
713                  AND assembly = '1'|;
714
715   my $sth = $dbh->prepare($query);
716   $sth->execute || $form->dberror($query);
717
718   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
719     push @{ $form->{assembly_items} }, $ref if $ref->{inventory};
720   }
721   $sth->finish;
722
723   $dbh->disconnect;
724
725   $main::lxdebug->leave_sub();
726 }
727
728 sub restock_assemblies {
729   $main::lxdebug->enter_sub();
730
731   my ($self, $myconfig, $form) = @_;
732
733   # connect to database
734   my $dbh = $form->dbconnect_noauto($myconfig);
735
736   for my $i (1 .. $form->{rowcount}) {
737
738     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
739
740     if ($form->{"qty_$i"} != 0) {
741       &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
742     }
743
744   }
745
746   my $rc = $dbh->commit;
747   $dbh->disconnect;
748
749   $main::lxdebug->leave_sub();
750
751   return $rc;
752 }
753
754 sub adjust_inventory {
755   $main::lxdebug->enter_sub();
756
757   my ($dbh, $form, $id, $qty) = @_;
758
759   my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
760                  FROM parts p, assembly a
761                  WHERE a.parts_id = p.id
762                  AND a.id = $id|;
763   my $sth = $dbh->prepare($query);
764   $sth->execute || $form->dberror($query);
765
766   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
767
768     my $allocate = $qty * $ref->{qty};
769
770     # is it a service item, then loop
771     $ref->{inventory_accno_id} *= 1;
772     next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
773
774     # adjust parts onhand
775     $form->update_balance($dbh, "parts", "onhand",
776                           qq|id = $ref->{id}|,
777                           $allocate * -1);
778   }
779
780   $sth->finish;
781
782   # update assembly
783   my $rc = $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $qty);
784
785   $main::lxdebug->leave_sub();
786
787   return $rc;
788 }
789
790 sub delete {
791   $main::lxdebug->enter_sub();
792
793   my ($self, $myconfig, $form) = @_;
794
795   # connect to database, turn off AutoCommit
796   my $dbh = $form->dbconnect_noauto($myconfig);
797
798   # first delete prices of pricegroup 
799   my $query = qq|DELETE FROM prices
800            WHERE parts_id = $form->{id}|;
801   $dbh->do($query) || $form->dberror($query);
802
803   my $query = qq|DELETE FROM parts
804                  WHERE id = $form->{id}|;
805   $dbh->do($query) || $form->dberror($query);
806
807   $query = qq|DELETE FROM partstax
808               WHERE parts_id = $form->{id}|;
809   $dbh->do($query) || $form->dberror($query);
810
811   # check if it is a part, assembly or service
812   if ($form->{item} ne 'service') {
813     $query = qq|DELETE FROM makemodel
814                 WHERE parts_id = $form->{id}|;
815     $dbh->do($query) || $form->dberror($query);
816   }
817
818   if ($form->{item} eq 'assembly') {
819
820     # delete inventory
821     $query = qq|DELETE FROM inventory
822                 WHERE parts_id = $form->{id}|;
823     $dbh->do($query) || $form->dberror($query);
824
825     $query = qq|DELETE FROM assembly
826                 WHERE id = $form->{id}|;
827     $dbh->do($query) || $form->dberror($query);
828   }
829
830   if ($form->{item} eq 'alternate') {
831     $query = qq|DELETE FROM alternate
832                 WHERE id = $form->{id}|;
833     $dbh->do($query) || $form->dberror($query);
834   }
835
836   # commit
837   my $rc = $dbh->commit;
838   $dbh->disconnect;
839
840   $main::lxdebug->leave_sub();
841
842   return $rc;
843 }
844
845 sub assembly_item {
846   $main::lxdebug->enter_sub();
847
848   my ($self, $myconfig, $form) = @_;
849
850   my $i = $form->{assembly_rows};
851   my $var;
852   my $where = "1 = 1";
853
854   if ($form->{"partnumber_$i"}) {
855     $var = $form->like(lc $form->{"partnumber_$i"});
856     $where .= " AND lower(p.partnumber) LIKE '$var'";
857   }
858   if ($form->{"description_$i"}) {
859     $var = $form->like(lc $form->{"description_$i"});
860     $where .= " AND lower(p.description) LIKE '$var'";
861   }
862   if ($form->{"partsgroup_$i"}) {
863     $var = $form->like(lc $form->{"partsgroup_$i"});
864     $where .= " AND lower(pg.partsgroup) LIKE '$var'";
865   }
866
867   if ($form->{id}) {
868     $where .= " AND NOT p.id = $form->{id}";
869   }
870
871   if ($partnumber) {
872     $where .= " ORDER BY p.partnumber";
873   } else {
874     $where .= " ORDER BY p.description";
875   }
876
877   # connect to database
878   my $dbh = $form->dbconnect($myconfig);
879
880   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
881                  p.weight, p.onhand, p.unit,
882                  pg.partsgroup
883                  FROM parts p
884                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
885                  WHERE $where|;
886   my $sth = $dbh->prepare($query);
887   $sth->execute || $form->dberror($query);
888
889   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
890     push @{ $form->{item_list} }, $ref;
891   }
892
893   $sth->finish;
894   $dbh->disconnect;
895
896   $main::lxdebug->leave_sub();
897 }
898
899 sub all_parts {
900   $main::lxdebug->enter_sub();
901
902   my ($self, $myconfig, $form) = @_;
903
904   my $where = '1 = 1';
905   my $var;
906
907   my $group;
908   my $limit;
909
910   foreach my $item (qw(partnumber drawing microfiche)) {
911     if ($form->{$item}) {
912       $var = $form->like(lc $form->{$item});
913       $where .= " AND lower(p.$item) LIKE '$var'";
914     }
915   }
916
917   # special case for description
918   if ($form->{description}) {
919     unless (   $form->{bought}
920             || $form->{sold}
921             || $form->{onorder}
922             || $form->{ordered}
923             || $form->{rfq}
924             || $form->{quoted}) {
925       $var = $form->like(lc $form->{description});
926       $where .= " AND lower(p.description) LIKE '$var'";
927     }
928   }
929
930   # special case for serialnumber
931   if ($form->{l_serialnumber}) {
932     if ($form->{serialnumber}) {
933       $var = $form->like(lc $form->{serialnumber});
934       $where .= " AND lower(serialnumber) LIKE '$var'";
935     }
936   }
937
938   if ($form->{searchitems} eq 'part') {
939     $where .= " AND p.inventory_accno_id > 0";
940   }
941   if ($form->{searchitems} eq 'assembly') {
942     $form->{bought} = "";
943     $where .= " AND p.assembly = '1'";
944   }
945   if ($form->{searchitems} eq 'service') {
946     $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
947
948     # irrelevant for services
949     $form->{make} = $form->{model} = "";
950   }
951
952   # items which were never bought, sold or on an order
953   if ($form->{itemstatus} eq 'orphaned') {
954     $form->{onhand}  = $form->{short}   = 0;
955     $form->{bought}  = $form->{sold}    = 0;
956     $form->{onorder} = $form->{ordered} = 0;
957     $form->{rfq}     = $form->{quoted}  = 0;
958
959     $form->{transdatefrom} = $form->{transdateto} = "";
960
961     $where .= " AND p.onhand = 0
962                 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
963                                  WHERE p.id = i.parts_id)
964                 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
965                                  WHERE p.id = a.parts_id)
966                 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
967                                  WHERE p.id = o.parts_id)";
968   }
969
970   if ($form->{itemstatus} eq 'active') {
971     $where .= " AND p.obsolete = '0'";
972   }
973   if ($form->{itemstatus} eq 'obsolete') {
974     $where .= " AND p.obsolete = '1'";
975     $form->{onhand} = $form->{short} = 0;
976   }
977   if ($form->{itemstatus} eq 'onhand') {
978     $where .= " AND p.onhand > 0";
979   }
980   if ($form->{itemstatus} eq 'short') {
981     $where .= " AND p.onhand < p.rop";
982   }
983   if ($form->{make}) {
984     $var = $form->like(lc $form->{make});
985     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
986                            FROM makemodel m WHERE lower(m.make) LIKE '$var')";
987   }
988   if ($form->{model}) {
989     $var = $form->like(lc $form->{model});
990     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
991                            FROM makemodel m WHERE lower(m.model) LIKE '$var')";
992   }
993   if ($form->{partsgroup}) {
994     $var = $form->like(lc $form->{partsgroup});
995     $where .= " AND lower(pg.partsgroup) LIKE '$var'";
996   }
997   if ($form->{l_soldtotal}) {
998     $where .= " AND p.id=i.parts_id AND  i.qty >= 0";
999     $group =
1000       " GROUP BY  p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
1001   }
1002   if ($form->{top100}) {
1003     $limit = " LIMIT 100";
1004   }
1005
1006   # tables revers?
1007   if ($form->{revers} == 1) {
1008     $form->{desc} = " DESC";
1009   } else {
1010     $form->{desc} = "";
1011   }
1012
1013   # connect to database
1014   my $dbh = $form->dbconnect($myconfig);
1015
1016   my $sortorder = $form->{sort};
1017   $sortorder .= $form->{desc};
1018   $sortorder = $form->{sort} if $form->{sort};
1019
1020   my $query = "";
1021
1022   if ($form->{l_soldtotal}) {
1023     $form->{soldtotal} = 'soldtotal';
1024     $query =
1025       qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
1026                 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
1027                 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
1028                 WHERE $where
1029                 $group
1030                 ORDER BY $sortorder
1031                 $limit|;
1032   } else {
1033     $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
1034                  p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
1035                  p.priceupdate, p.image, p.drawing, p.microfiche,
1036                  pg.partsgroup
1037                  FROM parts p
1038                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1039                  WHERE $where
1040                  $group
1041                  ORDER BY $sortorder|;
1042   }
1043
1044   # rebuild query for bought and sold items
1045   if (   $form->{bought}
1046       || $form->{sold}
1047       || $form->{onorder}
1048       || $form->{ordered}
1049       || $form->{rfq}
1050       || $form->{quoted}) {
1051
1052     my @a = qw(partnumber description bin priceupdate name);
1053
1054     push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
1055     push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
1056     push @a, "quonumber" if ($form->{rfq}     || $form->{quoted});
1057
1058     my $union = "";
1059     $query = "";
1060
1061     if ($form->{bought} || $form->{sold}) {
1062
1063       my $invwhere = "$where";
1064       $invwhere .= " AND i.assemblyitem = '0'";
1065       $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
1066         if $form->{transdatefrom};
1067       $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
1068         if $form->{transdateto};
1069
1070       if ($form->{description}) {
1071         $var = $form->like(lc $form->{description});
1072         $invwhere .= " AND lower(i.description) LIKE '$var'";
1073       }
1074
1075       my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
1076                     i.qty AS onhand, i.unit, p.bin, i.sellprice,
1077                     p.listprice, p.lastcost, p.rop, p.weight,
1078                     p.priceupdate, p.image, p.drawing, p.microfiche,
1079                     pg.partsgroup,
1080                     a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
1081                     ct.name, i.deliverydate|;
1082
1083       if ($form->{bought}) {
1084         $query = qq|
1085                     SELECT $flds, 'ir' AS module, '' AS type,
1086                     1 AS exchangerate
1087                     FROM invoice i
1088                     JOIN parts p ON (p.id = i.parts_id)
1089                     JOIN ap a ON (a.id = i.trans_id)
1090                     JOIN vendor ct ON (a.vendor_id = ct.id)
1091                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1092                     WHERE $invwhere|;
1093         $union = "
1094                   UNION";
1095       }
1096
1097       if ($form->{sold}) {
1098         $query .= qq|$union
1099                      SELECT $flds, 'is' AS module, '' AS type,
1100                      1 As exchangerate
1101                      FROM invoice i
1102                      JOIN parts p ON (p.id = i.parts_id)
1103                      JOIN ar a ON (a.id = i.trans_id)
1104                      JOIN customer ct ON (a.customer_id = ct.id)
1105                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1106                      WHERE $invwhere|;
1107         $union = "
1108                   UNION";
1109       }
1110     }
1111
1112     if ($form->{onorder} || $form->{ordered}) {
1113       my $ordwhere = "$where
1114                      AND o.quotation = '0'";
1115       $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1116         if $form->{transdatefrom};
1117       $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
1118         if $form->{transdateto};
1119
1120       if ($form->{description}) {
1121         $var = $form->like(lc $form->{description});
1122         $ordwhere .= " AND lower(oi.description) LIKE '$var'";
1123       }
1124
1125       $flds =
1126         qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1127                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1128                  p.listprice, p.lastcost, p.rop, p.weight,
1129                  p.priceupdate, p.image, p.drawing, p.microfiche,
1130                  pg.partsgroup,
1131                  '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1132                  ct.name|;
1133
1134       if ($form->{ordered}) {
1135         $query .= qq|$union
1136                      SELECT $flds, 'oe' AS module, 'sales_order' AS type,
1137                     (SELECT buy FROM exchangerate ex
1138                      WHERE ex.curr = o.curr
1139                      AND ex.transdate = o.transdate) AS exchangerate
1140                      FROM orderitems oi
1141                      JOIN parts p ON (oi.parts_id = p.id)
1142                      JOIN oe o ON (oi.trans_id = o.id)
1143                      JOIN customer ct ON (o.customer_id = ct.id)
1144                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1145                      WHERE $ordwhere
1146                      AND o.customer_id > 0|;
1147         $union = "
1148                   UNION";
1149       }
1150
1151       if ($form->{onorder}) {
1152         $flds =
1153           qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1154                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1155                    p.listprice, p.lastcost, p.rop, p.weight,
1156                    p.priceupdate, p.image, p.drawing, p.microfiche,
1157                    pg.partsgroup,
1158                    '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1159                    ct.name|;
1160
1161         $query .= qq|$union
1162                     SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
1163                     (SELECT sell FROM exchangerate ex
1164                      WHERE ex.curr = o.curr
1165                      AND ex.transdate = o.transdate) AS exchangerate
1166                     FROM orderitems oi
1167                     JOIN parts p ON (oi.parts_id = p.id)
1168                     JOIN oe o ON (oi.trans_id = o.id)
1169                     JOIN vendor ct ON (o.vendor_id = ct.id)
1170                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1171                     WHERE $ordwhere
1172                     AND o.vendor_id > 0|;
1173       }
1174
1175     }
1176
1177     if ($form->{rfq} || $form->{quoted}) {
1178       my $quowhere = "$where
1179                      AND o.quotation = '1'";
1180       $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1181         if $form->{transdatefrom};
1182       $quowhere .= " AND o.transdate <= '$form->{transdateto}'"
1183         if $form->{transdateto};
1184
1185       if ($form->{description}) {
1186         $var = $form->like(lc $form->{description});
1187         $quowhere .= " AND lower(oi.description) LIKE '$var'";
1188       }
1189
1190       $flds =
1191         qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1192                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1193                  p.listprice, p.lastcost, p.rop, p.weight,
1194                  p.priceupdate, p.image, p.drawing, p.microfiche,
1195                  pg.partsgroup,
1196                  '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1197                  ct.name|;
1198
1199       if ($form->{quoted}) {
1200         $query .= qq|$union
1201                      SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
1202                     (SELECT buy FROM exchangerate ex
1203                      WHERE ex.curr = o.curr
1204                      AND ex.transdate = o.transdate) AS exchangerate
1205                      FROM orderitems oi
1206                      JOIN parts p ON (oi.parts_id = p.id)
1207                      JOIN oe o ON (oi.trans_id = o.id)
1208                      JOIN customer ct ON (o.customer_id = ct.id)
1209                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1210                      WHERE $quowhere
1211                      AND o.customer_id > 0|;
1212         $union = "
1213                   UNION";
1214       }
1215
1216       if ($form->{rfq}) {
1217         $flds =
1218           qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1219                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1220                    p.listprice, p.lastcost, p.rop, p.weight,
1221                    p.priceupdate, p.image, p.drawing, p.microfiche,
1222                    pg.partsgroup,
1223                    '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1224                    ct.name|;
1225
1226         $query .= qq|$union
1227                     SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
1228                     (SELECT sell FROM exchangerate ex
1229                      WHERE ex.curr = o.curr
1230                      AND ex.transdate = o.transdate) AS exchangerate
1231                     FROM orderitems oi
1232                     JOIN parts p ON (oi.parts_id = p.id)
1233                     JOIN oe o ON (oi.trans_id = o.id)
1234                     JOIN vendor ct ON (o.vendor_id = ct.id)
1235                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1236                     WHERE $quowhere
1237                     AND o.vendor_id > 0|;
1238       }
1239
1240     }
1241     $query .= qq|
1242                  ORDER BY $sortorder|;
1243
1244   }
1245   my $sth = $dbh->prepare($query);
1246   $sth->execute || $form->dberror($query);
1247
1248   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1249     push @{ $form->{parts} }, $ref;
1250   }
1251
1252   $sth->finish;
1253
1254   # include individual items for assemblies
1255   if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1256     foreach $item (@{ $form->{parts} }) {
1257       push @assemblies, $item;
1258       $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1259                   p.unit, p.bin,
1260                   p.sellprice, p.listprice, p.lastcost,
1261                   p.rop, p.weight, p.priceupdate,
1262                   p.image, p.drawing, p.microfiche
1263                   FROM parts p, assembly a
1264                   WHERE p.id = a.parts_id
1265                   AND a.id = $item->{id}|;
1266
1267       $sth = $dbh->prepare($query);
1268       $sth->execute || $form->dberror($query);
1269
1270       while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1271         $ref->{assemblyitem} = 1;
1272         push @assemblies, $ref;
1273       }
1274       $sth->finish;
1275
1276       push @assemblies, { id => $item->{id} };
1277
1278     }
1279
1280     # copy assemblies to $form->{parts}
1281     @{ $form->{parts} } = @assemblies;
1282   }
1283
1284   $dbh->disconnect;
1285   $main::lxdebug->leave_sub();
1286 }
1287
1288 sub update_prices {
1289   $main::lxdebug->enter_sub();
1290
1291   my ($self, $myconfig, $form) = @_;
1292
1293   my $where = '1 = 1';
1294   my $var;
1295
1296   my $group;
1297   my $limit;
1298
1299   foreach my $item (qw(partnumber drawing microfiche make model)) {
1300     if ($form->{$item}) {
1301       $var = $form->like(lc $form->{$item});
1302
1303       # make will build later Bugfix 145
1304       if ($item ne 'make') {
1305         $where .= " AND lower(p.$item) LIKE '$var'";
1306       }
1307     }
1308   }
1309
1310   # special case for description
1311   if ($form->{description}) {
1312     unless (   $form->{bought}
1313             || $form->{sold}
1314             || $form->{onorder}
1315             || $form->{ordered}
1316             || $form->{rfq}
1317             || $form->{quoted}) {
1318       $var = $form->like(lc $form->{description});
1319       $where .= " AND lower(p.description) LIKE '$var'";
1320     }
1321   }
1322
1323   # special case for serialnumber
1324   if ($form->{l_serialnumber}) {
1325     if ($form->{serialnumber}) {
1326       $var = $form->like(lc $form->{serialnumber});
1327       $where .= " AND lower(serialnumber) LIKE '$var'";
1328     }
1329   }
1330
1331
1332   # items which were never bought, sold or on an order
1333   if ($form->{itemstatus} eq 'orphaned') {
1334     $form->{onhand}  = $form->{short}   = 0;
1335     $form->{bought}  = $form->{sold}    = 0;
1336     $form->{onorder} = $form->{ordered} = 0;
1337     $form->{rfq}     = $form->{quoted}  = 0;
1338
1339     $form->{transdatefrom} = $form->{transdateto} = "";
1340
1341     $where .= " AND p.onhand = 0
1342                 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
1343                                  WHERE p.id = i.parts_id)
1344                 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
1345                                  WHERE p.id = a.parts_id)
1346                 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
1347                                  WHERE p.id = o.parts_id)";
1348   }
1349
1350   if ($form->{itemstatus} eq 'active') {
1351     $where .= " AND p.obsolete = '0'";
1352   }
1353   if ($form->{itemstatus} eq 'obsolete') {
1354     $where .= " AND p.obsolete = '1'";
1355     $form->{onhand} = $form->{short} = 0;
1356   }
1357   if ($form->{itemstatus} eq 'onhand') {
1358     $where .= " AND p.onhand > 0";
1359   }
1360   if ($form->{itemstatus} eq 'short') {
1361     $where .= " AND p.onhand < p.rop";
1362   }
1363   if ($form->{make}) {
1364     $var = $form->like(lc $form->{make});
1365     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
1366                            FROM makemodel m WHERE lower(m.make) LIKE '$var')";
1367   }
1368   if ($form->{model}) {
1369     $var = $form->like(lc $form->{model});
1370     $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
1371                            FROM makemodel m WHERE lower(m.model) LIKE '$var')";
1372   }
1373   if ($form->{partsgroup}) {
1374     $var = $form->like(lc $form->{partsgroup});
1375     $where .= " AND lower(pg.partsgroup) LIKE '$var'";
1376   }
1377
1378
1379   # connect to database
1380   my $dbh = $form->dbconnect_noauto($myconfig);
1381
1382   if ($form->{"sellprice"} ne "") {
1383     my $update = "";
1384     my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"});
1385     if ($form->{"sellprice_type"} eq "percent") {
1386       my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1387       $update = "sellprice* $faktor";
1388     } else {
1389       $update = "sellprice+$faktor";
1390     }
1391   
1392     $query = qq|UPDATE parts set sellprice=$update WHERE id IN (SELECT p.id
1393                   FROM parts p
1394                   LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1395                   WHERE $where)|;
1396     $dbh->do($query);
1397   }
1398
1399   if ($form->{"listprice"} ne "") {
1400     my $update = "";
1401     my $faktor = $form->parse_amount($myconfig,$form->{"listprice"});
1402     if ($form->{"listprice_type"} eq "percent") {
1403       my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1404       $update = "listprice* $faktor";
1405     } else {
1406       $update = "listprice+$faktor";
1407     }
1408   
1409     $query = qq|UPDATE parts set listprice=$update WHERE id IN (SELECT p.id
1410                   FROM parts p
1411                   LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1412                   WHERE $where)|;
1413   
1414     $dbh->do($query);
1415   }
1416
1417
1418
1419
1420   for my $i (1 .. $form->{price_rows}) {
1421
1422     my $query = "";
1423     
1424   
1425     if ($form->{"price_$i"} ne "") {
1426       my $update = "";
1427       my $faktor = $form->parse_amount($myconfig,$form->{"price_$i"});
1428       if ($form->{"pricegroup_type_$i"} eq "percent") {
1429         my $faktor = $form->parse_amount($myconfig,$form->{"sellprice"})/100 +1;
1430         $update = "price* $faktor";
1431       } else {
1432         $update = "price+$faktor";
1433       }
1434     
1435       $query = qq|UPDATE prices set price=$update WHERE parts_id IN (SELECT p.id
1436                     FROM parts p
1437                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1438                     WHERE $where) AND pricegroup_id=$form->{"pricegroup_id_$i"}|;
1439     
1440       $dbh->do($query);
1441     }
1442   }
1443
1444
1445
1446   my $rc= $dbh->commit;
1447   $dbh->disconnect;
1448   $main::lxdebug->leave_sub();
1449
1450   return $rc;
1451 }
1452
1453 sub create_links {
1454   $main::lxdebug->enter_sub();
1455
1456   my ($self, $module, $myconfig, $form) = @_;
1457
1458   # connect to database
1459   my $dbh = $form->dbconnect($myconfig);
1460
1461   if ($form->{id}) {
1462     $query = qq|SELECT c.accno, c.description, c.link, c.id,
1463                         p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1464                         FROM chart c, parts p
1465                         WHERE c.link LIKE '%$module%'
1466                         AND p.id = $form->{id}
1467                         ORDER BY c.accno|;
1468   } else {
1469     $query = qq|SELECT c.accno, c.description, c.link, c.id,
1470                 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1471                 FROM chart c, defaults d
1472                 WHERE c.link LIKE '%$module%'
1473                 ORDER BY c.accno|;
1474   }
1475
1476   my $sth = $dbh->prepare($query);
1477   $sth->execute || $form->dberror($query);
1478   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1479     foreach my $key (split /:/, $ref->{link}) {
1480       if ($key =~ /$module/) {
1481         if (   ($ref->{id} eq $ref->{inventory_accno_id})
1482             || ($ref->{id} eq $ref->{income_accno_id})
1483             || ($ref->{id} eq $ref->{expense_accno_id})) {
1484           push @{ $form->{"${module}_links"}{$key} },
1485             { accno       => $ref->{accno},
1486               description => $ref->{description},
1487               selected    => "selected" };
1488           $form->{"${key}_default"} = "$ref->{accno}--$ref->{description}";
1489             } else {
1490           push @{ $form->{"${module}_links"}{$key} },
1491             { accno       => $ref->{accno},
1492               description => $ref->{description},
1493               selected    => "" };
1494         }
1495       }
1496     }
1497   }
1498   $sth->finish;
1499
1500   # get buchungsgruppen
1501   $query = qq|SELECT id, description
1502               FROM buchungsgruppen|;
1503   $sth = $dbh->prepare($query);
1504   $sth->execute || $form->dberror($query);
1505
1506   $form->{BUCHUNGSGRUPPEN} = [];
1507   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1508     push @{ $form->{BUCHUNGSGRUPPEN} }, $ref;
1509   }
1510   $sth->finish;
1511
1512   # get payment terms
1513   $query = qq|SELECT id, description
1514               FROM payment_terms
1515               ORDER BY 1|;
1516   $sth = $dbh->prepare($query);
1517   $sth->execute || $form->dberror($query);
1518
1519   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1520     push @{ $self->{payment_terms} }, $ref;
1521   }
1522   $sth->finish;
1523
1524   if ($form->{id}) {
1525     $query = qq|SELECT weightunit
1526                 FROM defaults|;
1527     $sth = $dbh->prepare($query);
1528     $sth->execute || $form->dberror($query);
1529
1530     ($form->{weightunit}) = $sth->fetchrow_array;
1531     $sth->finish;
1532
1533   } else {
1534     $query = qq|SELECT weightunit, current_date
1535                 FROM defaults|;
1536     $sth = $dbh->prepare($query);
1537     $sth->execute || $form->dberror($query);
1538
1539     ($form->{weightunit}, $form->{priceupdate}) = $sth->fetchrow_array;
1540     $sth->finish;
1541   }
1542
1543   $dbh->disconnect;
1544   $main::lxdebug->leave_sub();
1545 }
1546
1547 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1548 sub get_parts {
1549   $main::lxdebug->enter_sub();
1550
1551   my ($self, $myconfig, $form, $sortorder) = @_;
1552   my $dbh   = $form->dbconnect($myconfig);
1553   my $order = " p.partnumber";
1554   my $where = "1 = 1";
1555
1556   if ($sortorder eq "all") {
1557     $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1558     $where .= " AND p.description LIKE '%$form->{description}%'";
1559   } else {
1560     if ($sortorder eq "partnumber") {
1561       $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1562       $order = qq|p.$sortorder|;
1563     }
1564     if ($sortorder eq "description") {
1565       $where .= " AND p.description LIKE '%$form->{description}%'";
1566     }
1567   }
1568
1569   my $query =
1570     qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
1571   my $sth = $dbh->prepare($query);
1572   $sth->execute || $self->dberror($query);
1573   my $j = 0;
1574   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1575     if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1576     } else {
1577       $j++;
1578       $form->{"id_$j"}          = $ref->{id};
1579       $form->{"partnumber_$j"}  = $ref->{partnumber};
1580       $form->{"description_$j"} = $ref->{description};
1581       $form->{"unit_$j"}        = $ref->{unit};
1582       $form->{"sellprice_$j"}   = $ref->{sellprice};
1583       $form->{"soldtotal_$j"}   = get_soldtotal($dbh, $ref->{id});
1584     }    #fi
1585   }    #while
1586   $form->{rows} = $j;
1587   $sth->finish;
1588   $dbh->disconnect;
1589
1590   $main::lxdebug->leave_sub();
1591
1592   return $self;
1593 }    #end get_parts()
1594
1595 # gets sum of sold part with part_id
1596 sub get_soldtotal {
1597   $main::lxdebug->enter_sub();
1598
1599   my ($dbh, $id) = @_;
1600
1601   my $query =
1602     qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
1603
1604   my $sth = $dbh->prepare($query);
1605   $sth->execute || $form->dberror($query);
1606
1607   my $sum = 0;
1608   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1609
1610     $sum = $ref->{totalsold};
1611   }    #while
1612   $sth->finish;
1613
1614   if ($sum eq undef) {
1615     $sum = 0;
1616   }    #fi
1617
1618   $main::lxdebug->leave_sub();
1619
1620   return $sum;
1621 }    #end get_soldtotal
1622
1623 sub retrieve_item {
1624   $main::lxdebug->enter_sub();
1625
1626   my ($self, $myconfig, $form) = @_;
1627   my $i     = $form->{rowcount};
1628   my $where = "NOT p.obsolete = '1'";
1629
1630   if ($form->{"partnumber_$i"}) {
1631     my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1632     $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1633   }
1634   if ($form->{"description_$i"}) {
1635     my $description = $form->like(lc $form->{"description_$i"});
1636     $where .= " AND lower(p.description) LIKE '$description'";
1637   }
1638
1639   if ($form->{"partsgroup_$i"}) {
1640     my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1641     $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1642   }
1643
1644   if ($form->{"description_$i"}) {
1645     $where .= " ORDER BY description";
1646   } else {
1647     $where .= " ORDER BY partnumber";
1648   }
1649
1650   # connect to database
1651   my $dbh = $form->dbconnect($myconfig);
1652
1653   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1654                         p.listprice,
1655                         c1.accno AS inventory_accno,
1656                         c2.accno AS income_accno,
1657                         c3.accno AS expense_accno,
1658                  p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1659                  pg.partsgroup
1660                  FROM parts p
1661                  LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1662                  LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1663                  LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1664                  LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1665                  WHERE $where|;
1666   my $sth = $dbh->prepare($query);
1667   $sth->execute || $form->dberror($query);
1668
1669   #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1670
1671   # get tax rates and description
1672   #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
1673   #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1674   #           FROM chart c, tax t
1675   #           WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1676   #           ORDER BY accno|;
1677   # $stw = $dbh->prepare($query);
1678   #$stw->execute || $form->dberror($query);
1679
1680   #$ref->{taxaccounts} = "";
1681   #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1682
1683   #   $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1684   #  $form->{"$ptr->{accno}_description"} = $ptr->{description};
1685   #   $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1686   #   $form->{taxaccounts} .= "$ptr->{accno} ";
1687   #   $ref->{taxaccounts} .= "$ptr->{accno} ";
1688
1689   #}
1690
1691   #$stw->finish;
1692   #chop $ref->{taxaccounts};
1693
1694   push @{ $form->{item_list} }, $ref;
1695
1696   #}
1697   $sth->finish;
1698   $dbh->disconnect;
1699
1700   $main::lxdebug->leave_sub();
1701 }
1702
1703 sub retrieve_languages {
1704   $main::lxdebug->enter_sub();
1705
1706   my ($self, $myconfig, $form) = @_;
1707
1708   # connect to database
1709   my $dbh = $form->dbconnect($myconfig);
1710
1711   if ($form->{id}) {
1712     $where .= "tr.parts_id=$form->{id}";
1713   }
1714
1715
1716   if ($form->{language_values} ne "") {
1717   $query = qq|SELECT l.id, l.description, tr.translation, tr.longdescription
1718                  FROM language l LEFT OUTER JOIN translation tr ON (tr.language_id=l.id AND $where)|;
1719   } else {
1720   $query = qq|SELECT l.id, l.description
1721                  FROM language l|;
1722   }
1723   my $sth = $dbh->prepare($query);
1724   $sth->execute || $form->dberror($query);
1725
1726   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1727     push(@{$languages}, $ref);
1728   }
1729   $sth->finish;
1730
1731   $dbh->disconnect;
1732
1733   $main::lxdebug->leave_sub();
1734   return $languages;
1735
1736 }
1737
1738 1;