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