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