This commit was generated by cvs2svn to compensate for changes in r3921,
[freeside.git] / sql-ledger / SL / IC.pm
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2000
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #  Contributors:
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
24 #
25 # Inventory Control backend
26 #
27 #======================================================================
28
29 package IC;
30
31
32 sub get_part {
33   my ($self, $myconfig, $form) = @_;
34
35   # connect to db
36   my $dbh = $form->dbconnect($myconfig);
37   my $i;
38
39   my $query = qq|SELECT p.*,
40                  c1.accno AS inventory_accno,
41                  c2.accno AS income_accno,
42                  c3.accno AS expense_accno,
43                  pg.partsgroup
44                  FROM parts p
45                  LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
46                  LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
47                  LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
48                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
49                  WHERE p.id = $form->{id}|;
50   my $sth = $dbh->prepare($query);
51   $sth->execute || $form->dberror($query);
52   my $ref = $sth->fetchrow_hashref(NAME_lc);
53
54   # copy to $form variables
55   map { $form->{$_} = $ref->{$_} } ( keys %{ $ref } );
56   
57   $sth->finish;
58   
59   my %oid = ('Pg'       => 'a.oid',
60              'PgPP'     => 'a.oid',
61              'Oracle'   => 'a.rowid',
62              'DB2'      => '1=1'
63             );
64
65   # part, service item or labor
66   $form->{item} = ($form->{inventory_accno}) ? 'part' : 'service';
67   $form->{item} = 'labor' if ! $form->{income_accno};
68     
69   if ($form->{assembly}) {
70     $form->{item} = 'assembly';
71
72     # retrieve assembly items
73     $query = qq|SELECT p.id, p.partnumber, p.description,
74                 p.sellprice, p.weight, a.qty, a.bom, a.adj, p.unit,
75                 p.lastcost, p.listprice,
76                 pg.partsgroup, p.assembly, p.partsgroup_id
77                 FROM parts p
78                 JOIN assembly a ON (a.parts_id = p.id)
79                 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
80                 WHERE a.id = $form->{id}
81                 ORDER BY $oid{$myconfig->{dbdriver}}|;
82
83     $sth = $dbh->prepare($query);
84     $sth->execute || $form->dberror($query);
85     
86     $form->{assembly_rows} = 0;
87     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
88       $form->{assembly_rows}++;
89       foreach my $key ( keys %{ $ref } ) {
90         $form->{"${key}_$form->{assembly_rows}"} = $ref->{$key};
91       }
92     }
93     $sth->finish;
94
95   }
96
97   # setup accno hash for <option checked> {amount} is used in create_links
98   $form->{amount}{IC} = $form->{inventory_accno};
99   $form->{amount}{IC_income} = $form->{income_accno};
100   $form->{amount}{IC_sale} = $form->{income_accno};
101   $form->{amount}{IC_expense} = $form->{expense_accno};
102   $form->{amount}{IC_cogs} = $form->{expense_accno};
103   
104
105   if ($form->{item} =~ /(part|assembly)/) {
106     # get makes
107     if ($form->{makemodel}) {
108       $query = qq|SELECT make, model
109                   FROM makemodel
110                   WHERE parts_id = $form->{id}|;
111
112       $sth = $dbh->prepare($query);
113       $sth->execute || $form->dberror($query);
114       
115       while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
116         push @{ $form->{makemodels} }, $ref;
117       }
118       $sth->finish;
119     }
120   }
121
122   # now get accno for taxes
123   $query = qq|SELECT c.accno
124               FROM chart c, partstax pt
125               WHERE pt.chart_id = c.id
126               AND pt.parts_id = $form->{id}|;
127   
128   $sth = $dbh->prepare($query);
129   $sth->execute || $form->dberror($query);
130
131   while (($key) = $sth->fetchrow_array) {
132     $form->{amount}{$key} = $key;
133   }
134
135   $sth->finish;
136
137   # is it an orphan
138   $query = qq|SELECT parts_id
139               FROM invoice
140               WHERE parts_id = $form->{id}
141             UNION
142               SELECT parts_id
143               FROM orderitems
144               WHERE parts_id = $form->{id}
145             UNION
146               SELECT parts_id
147               FROM assembly
148               WHERE parts_id = $form->{id}|;
149   $sth = $dbh->prepare($query);
150   $sth->execute || $form->dberror($query);
151
152   ($form->{orphaned}) = $sth->fetchrow_array;
153   $form->{orphaned} = !$form->{orphaned};
154   $sth->finish;
155
156
157   if ($form->{item} =~ /(part|service)/) {
158     # get vendors
159     $query = qq|SELECT v.id, v.name, pv.partnumber,
160                 pv.lastcost, pv.leadtime, pv.curr AS vendorcurr
161                 FROM partsvendor pv
162                 JOIN vendor v ON (v.id = pv.vendor_id)
163                 WHERE pv.parts_id = $form->{id}
164                 ORDER BY 2|;
165     
166     $sth = $dbh->prepare($query);
167     $sth->execute || $form->dberror($query);
168     
169     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
170       push @{ $form->{vendormatrix} }, $ref;
171     }
172     $sth->finish;
173   }
174  
175   # get matrix
176   if ($form->{item} ne 'labor') {
177     $query = qq|SELECT pc.pricebreak, pc.sellprice AS customerprice,
178                 pc.curr AS customercurr,
179                 pc.validfrom, pc.validto,
180                 c.name, c.id AS cid, g.pricegroup, g.id AS gid
181                 FROM partscustomer pc
182                 LEFT JOIN customer c ON (c.id = pc.customer_id)
183                 LEFT JOIN pricegroup g ON (g.id = pc.pricegroup_id)
184                 WHERE pc.parts_id = $form->{id}
185                 ORDER BY c.name, g.pricegroup, pc.pricebreak|;
186     $sth = $dbh->prepare($query);
187     $sth->execute || $form->dberror($query);
188
189     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
190       push @{ $form->{customermatrix} }, $ref;
191     }
192     $sth->finish;
193   }
194  
195   $dbh->disconnect;
196   
197 }
198
199
200 sub save {
201   my ($self, $myconfig, $form) = @_;
202
203   ($form->{inventory_accno}) = split(/--/, $form->{IC});
204   ($form->{expense_accno}) = split(/--/, $form->{IC_expense});
205   ($form->{income_accno}) = split(/--/, $form->{IC_income});
206
207   # connect to database, turn off AutoCommit
208   my $dbh = $form->dbconnect_noauto($myconfig);
209
210   # save the part
211   # make up a unique handle and store in partnumber field
212   # then retrieve the record based on the unique handle to get the id
213   # replace the partnumber field with the actual variable
214   # add records for makemodel
215
216   # if there is a $form->{id} then replace the old entry
217   # delete all makemodel entries and add the new ones
218
219   # undo amount formatting
220   map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) } qw(rop weight listprice sellprice lastcost stock);
221   
222   $form->{lastcost} = $form->{sellprice} if $form->{item} eq 'labor';
223   
224   $form->{makemodel} = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
225
226   $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
227   map { $form->{$_} *= 1 } qw(alternate obsolete onhand);
228   
229   my $query;
230   my $sth;
231   my $i;
232   my $null;
233   my $vendor_id;
234   my $customer_id;
235   
236   if ($form->{id}) {
237
238     # get old price
239     $query = qq|SELECT listprice, sellprice, lastcost, weight
240                 FROM parts
241                 WHERE id = $form->{id}|;
242     $sth = $dbh->prepare($query);
243     $sth->execute || $form->dberror($query);
244
245     my ($listprice, $sellprice, $lastcost, $weight) = $sth->fetchrow_array;
246     $sth->finish;
247
248     # if item is part of an assembly adjust all assemblies
249     $query = qq|SELECT id, qty, adj
250                 FROM assembly
251                 WHERE parts_id = $form->{id}|;
252     $sth = $dbh->prepare($query);
253     $sth->execute || $form->dberror($query);
254
255     while (my ($id, $qty, $adj) = $sth->fetchrow_array) {
256       &update_assembly($dbh, $form, $id, $qty, $adj, $listprice * 1, $sellprice * 1, $lastcost * 1, $weight * 1);
257     }
258     $sth->finish;
259
260     if ($form->{item} =~ /(part|service)/) {
261       # delete partsvendor records
262       $query = qq|DELETE FROM partsvendor
263                   WHERE parts_id = $form->{id}|;
264       $dbh->do($query) || $form->dberror($query);
265     }
266      
267     if ($form->{item} !~ /(service|labor)/) {
268       # delete makemodel records
269       $query = qq|DELETE FROM makemodel
270                   WHERE parts_id = $form->{id}|;
271       $dbh->do($query) || $form->dberror($query);
272     }
273
274     if ($form->{item} eq 'assembly') {
275       if ($form->{onhand} != 0) {
276         &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
277       }
278       
279       if ($form->{orphaned}) {
280         # delete assembly records
281         $query = qq|DELETE FROM assembly
282                     WHERE id = $form->{id}|;
283         $dbh->do($query) || $form->dberror($query);
284       } else {
285         # update BOM, A only
286         $query = qq|UPDATE assembly
287                     SET bom = ?, adj = ?
288                     WHERE id = ?
289                     AND parts_id = ?|;
290         $sth = $dbh->prepare($query);
291
292         for $i (1 .. $form->{assembly_rows} - 1) {
293           $sth->execute(($form->{"bom_$i"}) ? '1' : '0', ($form->{"adj_$i"}) ? '1' : '0', $form->{id}, $form->{"id_$i"});
294           $sth->finish;
295         }
296       }
297
298       $form->{onhand} += $form->{stock};
299       
300     }
301     
302     # delete tax records
303     $query = qq|DELETE FROM partstax
304                 WHERE parts_id = $form->{id}|;
305     $dbh->do($query) || $form->dberror($query);
306
307     # delete matrix
308     $query = qq|DELETE FROM partscustomer
309                 WHERE parts_id = $form->{id}|;
310     $dbh->do($query) || $form->dberror($query);
311
312   } else {
313     my $uid = time;
314     $uid .= $form->{login};
315
316     $query = qq|INSERT INTO parts (partnumber)
317                 VALUES ('$uid')|;
318     $dbh->do($query) || $form->dberror($query);
319
320     $query = qq|SELECT id FROM parts
321                 WHERE partnumber = '$uid'|;
322     $sth = $dbh->prepare($query);
323     $sth->execute || $form->dberror($query);
324
325     ($form->{id}) = $sth->fetchrow_array;
326     $sth->finish;
327
328     $form->{orphaned} = 1;
329     $form->{onhand} = ($form->{stock} * 1) if $form->{item} eq 'assembly';
330     
331   }
332
333   my $partsgroup_id;
334   ($null, $partsgroup_id) = split /--/, $form->{partsgroup};
335   $partsgroup_id *= 1;
336
337   $form->{partnumber} = $form->update_defaults($myconfig, "partnumber", $dbh) if ! $form->{partnumber};
338
339   $query = qq|UPDATE parts SET
340               partnumber = |.$dbh->quote($form->{partnumber}).qq|,
341               description = |.$dbh->quote($form->{description}).qq|,
342               makemodel = '$form->{makemodel}',
343               alternate = '$form->{alternate}',
344               assembly = '$form->{assembly}',
345               listprice = $form->{listprice},
346               sellprice = $form->{sellprice},
347               lastcost = $form->{lastcost},
348               weight = $form->{weight},
349               priceupdate = |.$form->dbquote($form->{priceupdate}, SQL_DATE).qq|,
350               unit = |.$dbh->quote($form->{unit}).qq|,
351               notes = |.$dbh->quote($form->{notes}).qq|,
352               rop = $form->{rop},
353               bin = |.$dbh->quote($form->{bin}).qq|,
354               inventory_accno_id = (SELECT id FROM chart
355                                     WHERE accno = '$form->{inventory_accno}'),
356               income_accno_id = (SELECT id FROM chart
357                                  WHERE accno = '$form->{income_accno}'),
358               expense_accno_id = (SELECT id FROM chart
359                                   WHERE accno = '$form->{expense_accno}'),
360               obsolete = '$form->{obsolete}',
361               image = '$form->{image}',
362               drawing = '$form->{drawing}',
363               microfiche = '$form->{microfiche}',
364               partsgroup_id = $partsgroup_id
365               WHERE id = $form->{id}|;
366   $dbh->do($query) || $form->dberror($query);
367
368  
369   # insert makemodel records
370   if ($form->{item} =~ /(part|assembly)/) {
371     for $i (1 .. $form->{makemodel_rows}) {
372       if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
373         $query = qq|INSERT INTO makemodel (parts_id, make, model)
374                     VALUES ($form->{id},|
375                     .$dbh->quote($form->{"make_$i"}).qq|, |
376                     .$dbh->quote($form->{"model_$i"}).qq|)|;
377         $dbh->do($query) || $form->dberror($query);
378       }
379     }
380   }
381
382
383   # insert taxes
384   foreach $item (split / /, $form->{taxaccounts}) {
385     if ($form->{"IC_tax_$item"}) {
386       $query = qq|INSERT INTO partstax (parts_id, chart_id)
387                   VALUES ($form->{id}, 
388                           (SELECT id
389                            FROM chart
390                            WHERE accno = '$item'))|;
391       $dbh->do($query) || $form->dberror($query);
392     }
393   }
394
395   # add assembly records
396   if ($form->{item} eq 'assembly') {
397     
398     if ($form->{orphaned}) {
399       for $i (1 .. $form->{assembly_rows}) {
400         $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
401         
402         if ($form->{"qty_$i"} != 0) {
403           map { $form->{"${_}_$i"} *= 1 } qw(bom adj);
404           $query = qq|INSERT INTO assembly (id, parts_id, qty, bom, adj)
405                       VALUES ($form->{id}, $form->{"id_$i"},
406                       $form->{"qty_$i"}, '$form->{"bom_$i"}',
407                       '$form->{"adj_$i"}')|;
408           $dbh->do($query) || $form->dberror($query);
409         }
410       }
411     }
412     
413     # adjust onhand for the parts
414     if ($form->{onhand} != 0) {
415       &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
416     }
417
418     @a = localtime; $a[5] += 1900; $a[4]++;
419     my $shippingdate = "$a[5]-$a[4]-$a[3]";
420     
421     ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
422     
423     # add inventory record
424     if ($form->{stock} != 0) {
425       $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
426                   shippingdate, employee_id) VALUES (
427                   0, $form->{id}, $form->{stock}, '$shippingdate',
428                   $form->{employee_id})|;
429       $dbh->do($query) || $form->dberror($query);
430     }
431
432   }
433
434
435   # add vendors
436   if ($form->{item} ne 'assembly') {
437     for $i (1 .. $form->{vendor_rows}) {
438       if ($form->{"vendor_$i"} && $form->{"lastcost_$i"}) {
439
440         ($null, $vendor_id) = split /--/, $form->{"vendor_$i"};
441         
442         map { $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"})} qw(lastcost leadtime);
443         
444         $query = qq|INSERT INTO partsvendor (vendor_id, parts_id, partnumber,
445                     lastcost, leadtime, curr)
446                     VALUES ($vendor_id, $form->{id},|
447                     .$dbh->quote($form->{"partnumber_$i"}).qq|,
448                     $form->{"lastcost_$i"},
449                     $form->{"leadtime_$i"}, '$form->{"vendorcurr_$i"}')|;
450         $dbh->do($query) || $form->dberror($query);
451       }
452     }
453   }
454   
455   
456   # add pricematrix
457   for $i (1 .. $form->{customer_rows}) {
458
459     map { $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"})} qw(pricebreak customerprice);
460
461     if ($form->{"customerprice_$i"}) {
462
463       ($null, $customer_id) = split /--/, $form->{"customer_$i"};
464       $customer_id *= 1;
465       
466       ($null, $pricegroup_id) = split /--/, $form->{"pricegroup_$i"};
467       $pricegroup_id *= 1;
468       
469       $query = qq|INSERT INTO partscustomer (parts_id, customer_id,
470                   pricegroup_id, pricebreak, sellprice, curr,
471                   validfrom, validto)
472                   VALUES ($form->{id}, $customer_id,
473                   $pricegroup_id, $form->{"pricebreak_$i"},
474                   $form->{"customerprice_$i"}, '$form->{"customercurr_$i"}',|
475                   .$form->dbquote($form->{"validfrom_$i"}, SQL_DATE).qq|, |
476                   .$form->dbquote($form->{"validto_$i"}, SQL_DATE).qq|)|;
477       $dbh->do($query) || $form->dberror($query);
478     }
479   }
480
481   # commit
482   my $rc = $dbh->commit;
483   $dbh->disconnect;
484
485   $rc;
486   
487 }
488
489
490
491 sub update_assembly {
492   my ($dbh, $form, $id, $qty, $adj, $listprice, $sellprice, $lastcost, $weight) = @_;
493
494   my $formlistprice = $form->{listprice};
495   my $formsellprice = $form->{sellprice};
496   
497   if (!$adj) {
498     $formlistprice = $listprice;
499     $formsellprice = $sellprice;
500   }
501   
502   my $query = qq|SELECT id, qty, adj
503                  FROM assembly
504                  WHERE parts_id = $id|;
505   my $sth = $dbh->prepare($query);
506   $sth->execute || $form->dberror($query);
507
508   $form->{$id} = 1;
509   
510   while (my ($pid, $aqty, $aadj) = $sth->fetchrow_array) {
511     &update_assembly($dbh, $form, $pid, $aqty * $qty, $aadj, $listprice, $sellprice, $lastcost, $weight) if !$form->{$pid};
512   }
513   $sth->finish;
514
515   $query = qq|UPDATE parts
516               SET listprice = listprice +
517                   $qty * ($formlistprice - $listprice),
518                   sellprice = sellprice +
519                   $qty * ($formsellprice - $sellprice),
520                   lastcost = lastcost +
521                   $qty * ($form->{lastcost} - $lastcost),
522                   weight = weight +
523                   $qty * ($form->{weight} - $weight)
524               WHERE id = $id|;
525   $dbh->do($query) || $form->dberror($query);
526
527   delete $form->{$id};
528   
529 }
530
531
532
533 sub retrieve_assemblies {
534   my ($self, $myconfig, $form) = @_;
535   
536   # connect to database
537   my $dbh = $form->dbconnect($myconfig);
538
539   my $where = '1 = 1';
540   
541   if ($form->{partnumber}) {
542     my $partnumber = $form->like(lc $form->{partnumber});
543     $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
544   }
545   
546   if ($form->{description}) {
547     my $description = $form->like(lc $form->{description});
548     $where .= " AND lower(p.description) LIKE '$description'";
549   }
550   $where .= " AND NOT p.obsolete = '1'";
551
552   my %ordinal = ( 'partnumber' => 2,
553                   'description' => 3,
554                   'bin' => 4
555                 );
556
557   my @a = qw(partnumber description bin);
558   my $sortorder = $form->sort_order(\@a, \%ordinal);
559   
560   
561   # retrieve assembly items
562   my $query = qq|SELECT p.id, p.partnumber, p.description,
563                  p.bin, p.onhand, p.rop,
564                    (SELECT sum(p2.inventory_accno_id)
565                     FROM parts p2, assembly a
566                     WHERE p2.id = a.parts_id
567                     AND a.id = p.id) AS inventory
568                  FROM parts p
569                  WHERE $where
570                  AND assembly = '1'
571                  ORDER BY $sortorder|;
572
573   my $sth = $dbh->prepare($query);
574   $sth->execute || $form->dberror($query);
575   
576   my $inh;
577   if ($form->{checkinventory}) {
578     $query = qq|SELECT p.id, p.onhand, a.qty FROM parts p
579                 JOIN assembly a ON (a.parts_id = p.id)
580                 WHERE a.id = ?|;
581     $inh = $dbh->prepare($query) || $form->dberror($query);
582   }
583   
584   my $onhand = ();
585   my $ref;
586   my $aref;
587   my $stock;
588   my $howmany;
589   my $ok;
590   
591   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
592     if ($ref->{inventory}) {
593       $ok = 1;
594       if ($form->{checkinventory}) {
595         $inh->execute($ref->{id}) || $form->dberror($query);;
596         $ok = 0;
597         while ($aref = $inh->fetchrow_hashref(NAME_lc)) {
598           $onhand{$aref->{id}} = (exists $onhand{$aref->{id}}) ? $onhand{$aref->{id}} : $aref->{onhand};
599           
600           if ($aref->{onhand} >= $aref->{qty}) {
601             
602             $howmany = ($aref->{qty}) ? $aref->{onhand}/$aref->{qty} : 1;
603             if ($stock) {
604               $stock = ($stock > $howmany) ? $howmany : $stock;
605             } else {
606               $stock = $howmany;
607             }
608             $ok = 1;
609
610             $onhand{$aref->{id}} -= ($aref->{qty} * $stock);
611
612           } else {
613             $ok = 0;
614             last;
615           }
616         }
617         $inh->finish;
618         $ref->{stock} = (($ref->{rop} - $ref->{qty}) > $stock) ? int $stock : $ref->{rop};
619       }
620       push @{ $form->{assembly_items} }, $ref if $ok;
621     }
622   }
623   $sth->finish;
624
625   $dbh->disconnect;
626   
627 }
628
629
630 sub restock_assemblies {
631   my ($self, $myconfig, $form) = @_;
632
633   # connect to database
634   my $dbh = $form->dbconnect_noauto($myconfig);
635    
636   @a = localtime; $a[5] += 1900; $a[4]++;
637   my $shippingdate = "$a[5]-$a[4]-$a[3]";
638   
639   ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
640   
641   for my $i (1 .. $form->{rowcount}) {
642
643     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
644
645     if ($form->{"qty_$i"} != 0) {
646       &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
647     }
648  
649     # add inventory record
650     if ($form->{"qty_$i"} != 0) {
651       $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
652                   shippingdate, employee_id) VALUES (
653                   0, $form->{"id_$i"}, $form->{"qty_$i"}, '$shippingdate',
654                   $form->{employee_id})|;
655       $dbh->do($query) || $form->dberror($query);
656     }
657
658   }
659
660   my $rc = $dbh->commit;
661   $dbh->disconnect;
662
663   $rc;
664
665 }
666
667
668 sub adjust_inventory {
669   my ($dbh, $form, $id, $qty) = @_;
670
671   my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
672                  FROM parts p, assembly a
673                  WHERE a.parts_id = p.id
674                  AND a.id = $id|;
675   my $sth = $dbh->prepare($query);
676   $sth->execute || $form->dberror($query);
677
678   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
679
680     my $allocate = $qty * $ref->{qty};
681     
682     # is it a service item then loop
683     if (($ref->{inventory_accno_id} *= 1) == 0) {
684       next unless $ref->{assembly};              # assembly
685     }
686     
687     # adjust parts onhand
688     $form->update_balance($dbh,
689                           "parts",
690                           "onhand",
691                           qq|id = $ref->{id}|,
692                           $allocate * -1);
693   }
694
695   $sth->finish;
696
697   # update assembly
698   $form->update_balance($dbh,
699                         "parts",
700                         "onhand",
701                         qq|id = $id|,
702                         $qty);
703
704 }
705
706
707 sub delete {
708   my ($self, $myconfig, $form) = @_;
709
710   # connect to database, turn off AutoCommit
711   my $dbh = $form->dbconnect_noauto($myconfig);
712
713   my $query = qq|DELETE FROM parts
714                  WHERE id = $form->{id}|;
715   $dbh->do($query) || $form->dberror($query);
716
717   $query = qq|DELETE FROM partstax
718               WHERE parts_id = $form->{id}|;
719   $dbh->do($query) || $form->dberror($query);
720
721
722   if ($form->{item} ne 'assembly') {
723     $query = qq|DELETE FROM partsvendor
724                 WHERE parts_id = $form->{id}|;
725     $dbh->do($query) || $form->dberror($query);
726   }
727
728   # check if it is a part, assembly or service
729   if ($form->{item} ne 'service') {
730     $query = qq|DELETE FROM makemodel
731                 WHERE parts_id = $form->{id}|;
732     $dbh->do($query) || $form->dberror($query);
733   }
734
735   if ($form->{item} eq 'assembly') {
736     # delete inventory
737     $query = qq|DELETE FROM inventory
738                 WHERE parts_id = $form->{id}|;
739     $dbh->do($query) || $form->dberror($query);
740     
741     $query = qq|DELETE FROM assembly
742                 WHERE id = $form->{id}|;
743     $dbh->do($query) || $form->dberror($query);
744   }
745   
746   if ($form->{item} eq 'alternate') {
747     $query = qq|DELETE FROM alternate
748                 WHERE id = $form->{id}|;
749     $dbh->do($query) || $form->dberror($query);
750   }
751   
752   $query = qq|DELETE FROM partscustomer
753               WHERE parts_id = $form->{id}|;
754   $dbh->do($query) || $form->dberror($query);
755   
756   $query = qq|DELETE FROM translation
757               WHERE trans_id = $form->{id}|;
758   $dbh->do($query) || $form->dberror($query);
759
760   # commit
761   my $rc = $dbh->commit;
762   $dbh->disconnect;
763
764   $rc;
765   
766 }
767
768
769 sub assembly_item {
770   my ($self, $myconfig, $form) = @_;
771
772   my $i = $form->{assembly_rows};
773   my $var;
774   my $null;
775   my $where = "p.obsolete = '0'";
776
777   if ($form->{"partnumber_$i"}) {
778     $var = $form->like(lc $form->{"partnumber_$i"});
779     $where .= " AND lower(p.partnumber) LIKE '$var'";
780   }
781   if ($form->{"description_$i"}) {
782     $var = $form->like(lc $form->{"description_$i"});
783     $where .= " AND lower(p.description) LIKE '$var'";
784   }
785   if ($form->{"partsgroup_$i"}) {
786     ($null, $var) = split /--/, $form->{"partsgroup_$i"};
787     $where .= qq| AND p.partsgroup_id = $var|;
788   }
789
790   if ($form->{id}) {
791     $where .= " AND p.id != $form->{id}";
792   }
793
794   if ($partnumber) {
795     $where .= " ORDER BY p.partnumber";
796   } else {
797     $where .= " ORDER BY p.description";
798   }
799
800   # connect to database
801   my $dbh = $form->dbconnect($myconfig);
802
803   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
804                  p.weight, p.onhand, p.unit, p.lastcost,
805                  pg.partsgroup, p.partsgroup_id
806                  FROM parts p
807                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
808                  WHERE $where|;
809   my $sth = $dbh->prepare($query);
810   $sth->execute || $form->dberror($query);
811
812   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
813     push @{ $form->{item_list} }, $ref;
814   }
815   
816   $sth->finish;
817   $dbh->disconnect;
818   
819 }
820
821
822 sub all_parts {
823   my ($self, $myconfig, $form) = @_;
824
825   my $where = '1 = 1';
826   my $null;
827   my $var;
828   my $ref;
829   my $item;
830   
831   foreach $item (qw(partnumber drawing microfiche)) {
832     if ($form->{$item}) {
833       $var = $form->like(lc $form->{$item});
834       $where .= " AND lower(p.$item) LIKE '$var'";
835     }
836   }
837   # special case for description
838   if ($form->{description}) {
839     unless ($form->{bought} || $form->{sold} || $form->{onorder} || $form->{ordered} || $form->{rfq} || $form->{quoted}) {
840       $var = $form->like(lc $form->{description});
841       $where .= " AND lower(p.description) LIKE '$var'";
842     }
843   }
844   
845   # assembly components
846   my $assemblyflds;
847   if ($form->{searchitems} eq 'component') {
848     $assemblyflds = qq|, p1.partnumber AS assemblypartnumber, a.id AS assembly_id|;
849   }
850
851   # special case for serialnumber
852   if ($form->{l_serialnumber}) {
853     if ($form->{serialnumber}) {
854       $var = $form->like(lc $form->{serialnumber});
855       $where .= " AND lower(i.serialnumber) LIKE '$var'";
856     }
857   }
858
859   if ($form->{warehouse} || $form->{l_warehouse}) {
860     $form->{l_warehouse} = 1;
861   }
862   
863   if ($form->{searchitems} eq 'part') {
864     $where .= " AND p.inventory_accno_id > 0 AND p.assembly = '0' AND p.income_accno_id > 0";
865   }
866   if ($form->{searchitems} eq 'assembly') {
867     $form->{bought} = "";
868     $where .= " AND p.assembly = '1'";
869   }
870   if ($form->{searchitems} eq 'service') {
871     $where .= " AND p.inventory_accno_id IS NULL AND p.assembly = '0'";
872   }
873   if ($form->{searchitems} eq 'labor') {
874     $where .= " AND p.inventory_accno_id > 0 AND p.income_accno_id IS NULL";
875   }
876
877   # items which were never bought, sold or on an order
878   if ($form->{itemstatus} eq 'orphaned') {
879     $where .= " AND p.onhand = 0
880                 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
881                                  WHERE p.id = i.parts_id)
882                 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
883                                  WHERE p.id = a.parts_id)
884                 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
885                                  WHERE p.id = o.parts_id)";
886   }
887   
888   if ($form->{itemstatus} eq 'active') {
889     $where .= " AND p.obsolete = '0'";
890   }
891   if ($form->{itemstatus} eq 'obsolete') {
892     $where .= " AND p.obsolete = '1'";
893   }
894   if ($form->{itemstatus} eq 'onhand') {
895     $where .= " AND p.onhand > 0";
896   }
897   if ($form->{itemstatus} eq 'short') {
898     $where .= " AND p.onhand < p.rop";
899   }
900
901   my $makemodelflds = qq|, '', ''|;;
902   my $makemodeljoin;
903   
904   if ($form->{make} || $form->{l_make} || $form->{model} || $form->{l_model}) {
905     $makemodelflds = qq|, m.make, m.model|;
906     $makemodeljoin = qq|LEFT JOIN makemodel m ON (m.parts_id = p.id)|;
907     
908     if ($form->{make}) {
909       $var = $form->like(lc $form->{make});
910       $where .= " AND lower(m.make) LIKE '$var'";
911     }
912     if ($form->{model}) {
913       $var = $form->like(lc $form->{model});
914       $where .= " AND lower(m.model) LIKE '$var'";
915     }
916   }
917   if ($form->{partsgroup}) {
918     ($null, $var) = split /--/, $form->{partsgroup};
919     $where .= qq| AND p.partsgroup_id = $var|;
920   }
921
922   # connect to database
923   my $dbh = $form->dbconnect($myconfig);
924
925   my %ordinal = ( 'partnumber' => 2,
926                   'description' => 3,
927                   'bin' => 6,
928                   'priceupdate' => 12,
929                   'drawing' => 14,
930                   'microfiche' => 15,
931                   'partsgroup' => 17,
932                   'make' => 19,
933                   'model' => 20,
934                   'assemblypartnumber' => 21
935                 );
936   
937   my @a = qw(partnumber description);
938   my $sortorder = $form->sort_order(\@a, \%ordinal);
939
940   my $query = qq|SELECT curr FROM defaults|;
941   my ($curr) = $dbh->selectrow_array($query);
942   $curr =~ s/:.*//;
943   
944   my $flds = qq|p.id, p.partnumber, p.description, p.onhand, p.unit,
945                 p.bin, p.sellprice, p.listprice, p.lastcost, p.rop,
946                 p.weight, p.priceupdate, p.image, p.drawing, p.microfiche,
947                 p.assembly, pg.partsgroup, '$curr' AS curr
948                 $makemodelflds $assemblyflds
949                 |;
950
951   $query = qq|SELECT $flds
952               FROM parts p
953               LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
954               $makemodeljoin
955               WHERE $where
956               ORDER BY $sortorder|;
957
958   # redo query for components report
959   if ($form->{searchitems} eq 'component') {
960     
961     $query = qq|SELECT $flds
962                 FROM assembly a
963                 JOIN parts p ON (a.parts_id = p.id)
964                 JOIN parts p1 ON (a.id = p1.id)
965                 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
966                 $makemodeljoin
967                 WHERE $where
968                 ORDER BY $sortorder|;
969                 
970   }
971
972
973   # rebuild query for bought and sold items
974   if ($form->{bought} || $form->{sold} || $form->{onorder} || $form->{ordered} || $form->{rfq} || $form->{quoted}) {
975
976     $form->sort_order();
977     my @a = qw(partnumber description employee);
978     
979     push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
980     push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
981     push @a, "quonumber" if ($form->{rfq} || $form->{quoted});
982
983     %ordinal = ( 'partnumber' => 2,
984                  'description' => 3,
985                  'serialnumber' => 4,
986                  'bin' => 7,
987                  'priceupdate' => 13,
988                  'partsgroup' => 18,
989                  'invnumber' => 19,
990                  'ordnumber' => 20,
991                  'quonumber' => 21,
992                  'name' => 23,
993                  'employee' => 24,
994                  'make' => 27,
995                  'model' => 28
996                );
997     
998     $sortorder = $form->sort_order(\@a, \%ordinal);
999
1000     my $union = "";
1001     $query = "";
1002   
1003     if ($form->{bought} || $form->{sold}) {
1004       
1005       my $invwhere = "$where";
1006       my $transdate = ($form->{method} eq 'accrual') ? "transdate" : "datepaid";
1007       
1008       $invwhere .= " AND i.assemblyitem = '0'";
1009       $invwhere .= " AND a.$transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
1010       $invwhere .= " AND a.$transdate <= '$form->{transdateto}'" if $form->{transdateto};
1011
1012       if ($form->{description}) {
1013         $var = $form->like(lc $form->{description});
1014         $invwhere .= " AND lower(i.description) LIKE '$var'";
1015       }
1016
1017       if ($form->{open} || $form->{closed}) {
1018         if ($form->{open} && $form->{closed}) {
1019           if ($form->{method} eq 'cash') {
1020             $invwhere .= " AND a.amount = a.paid";
1021           }
1022         } else {
1023           if ($form->{open}) {
1024             if ($form->{method} eq 'cash') {
1025               $invwhere .= " AND a.id = 0";
1026             } else {
1027               $invwhere .= " AND NOT a.amount = a.paid";
1028             }
1029           } else {
1030             $invwhere .= " AND a.amount = a.paid";
1031           }
1032         }
1033       } else {
1034         $invwhere .= " AND a.id = 0";
1035       }
1036
1037       my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
1038                     i.qty AS onhand, i.unit, p.bin, i.sellprice,
1039                     p.listprice, p.lastcost, p.rop, p.weight,
1040                     p.priceupdate, p.image, p.drawing, p.microfiche,
1041                     p.assembly,
1042                     pg.partsgroup, a.invnumber, a.ordnumber, a.quonumber,
1043                     i.trans_id, ct.name, e.name AS employee, a.curr, a.till
1044                     $makemodelfld|;
1045
1046
1047       if ($form->{bought}) {
1048         $query = qq|
1049                     SELECT $flds, 'ir' AS module, '' AS type,
1050                     (SELECT sell FROM exchangerate ex
1051                      WHERE ex.curr = a.curr
1052                      AND ex.transdate = a.$transdate) AS exchangerate,
1053                      i.discount
1054                     FROM invoice i
1055                     JOIN parts p ON (p.id = i.parts_id)
1056                     JOIN ap a ON (a.id = i.trans_id)
1057                     JOIN vendor ct ON (a.vendor_id = ct.id)
1058                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1059                     LEFT JOIN employee e ON (a.employee_id = e.id)
1060                     $makemodeljoin
1061                     WHERE $invwhere|;
1062         $union = "
1063                   UNION";
1064       }
1065
1066       if ($form->{sold}) {
1067         $query .= qq|$union
1068                      SELECT $flds, 'is' AS module, '' AS type,
1069                     (SELECT buy FROM exchangerate ex
1070                      WHERE ex.curr = a.curr
1071                      AND ex.transdate = a.$transdate) AS exchangerate,
1072                      i.discount
1073                      FROM invoice i
1074                      JOIN parts p ON (p.id = i.parts_id)
1075                      JOIN ar a ON (a.id = i.trans_id)
1076                      JOIN customer ct ON (a.customer_id = ct.id)
1077                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1078                      LEFT JOIN employee e ON (a.employee_id = e.id)
1079                      $makemodeljoin
1080                      WHERE $invwhere|;
1081         $union = "
1082                   UNION";
1083       }
1084     }
1085
1086     if ($form->{onorder} || $form->{ordered}) {
1087       my $ordwhere = "$where
1088                      AND a.quotation = '0'";
1089       $ordwhere .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
1090       $ordwhere .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
1091
1092       if ($form->{description}) {
1093         $var = $form->like(lc $form->{description});
1094         $ordwhere .= " AND lower(i.description) LIKE '$var'";
1095       }
1096       
1097       if ($form->{open} || $form->{closed}) {
1098         unless ($form->{open} && $form->{closed}) {
1099           $ordwhere .= " AND a.closed = '0'" if $form->{open};
1100           $ordwhere .= " AND a.closed = '1'" if $form->{closed};
1101         }
1102       } else {
1103         $ordwhere .= " AND a.id = 0";
1104       }
1105
1106       $flds = qq|p.id, p.partnumber, i.description, '' AS serialnumber,
1107                  i.qty AS onhand, i.unit, p.bin, i.sellprice,
1108                  p.listprice, p.lastcost, p.rop, p.weight,
1109                  p.priceupdate, p.image, p.drawing, p.microfiche,
1110                  p.assembly,
1111                  pg.partsgroup, '' AS invnumber, a.ordnumber, a.quonumber,
1112                  i.trans_id, ct.name,e.name AS employee, a.curr, '0' AS till
1113                  $makemodelfld|;
1114
1115       if ($form->{ordered}) {
1116         $query .= qq|$union
1117                      SELECT $flds, 'oe' AS module, 'sales_order' AS type,
1118                     (SELECT buy FROM exchangerate ex
1119                      WHERE ex.curr = a.curr
1120                      AND ex.transdate = a.transdate) AS exchangerate,
1121                      i.discount
1122                      FROM orderitems i
1123                      JOIN parts p ON (i.parts_id = p.id)
1124                      JOIN oe a ON (i.trans_id = a.id)
1125                      JOIN customer ct ON (a.customer_id = ct.id)
1126                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1127                      LEFT JOIN employee e ON (a.employee_id = e.id)
1128                      $makemodeljoin
1129                      WHERE $ordwhere
1130                      AND a.customer_id > 0|;
1131         $union = "
1132                   UNION";
1133       }
1134       
1135       if ($form->{onorder}) {
1136         $flds = qq|p.id, p.partnumber, i.description, '' AS serialnumber,
1137                    i.qty AS onhand, i.unit, p.bin, i.sellprice,
1138                    p.listprice, p.lastcost, p.rop, p.weight,
1139                    p.priceupdate, p.image, p.drawing, p.microfiche,
1140                    p.assembly,
1141                    pg.partsgroup, '' AS invnumber, a.ordnumber, a.quonumber,
1142                    i.trans_id, ct.name,e.name AS employee, a.curr, '0' AS till
1143                    $makemodelfld|;
1144
1145         $query .= qq|$union
1146                     SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
1147                     (SELECT sell FROM exchangerate ex
1148                      WHERE ex.curr = a.curr
1149                      AND ex.transdate = a.transdate) AS exchangerate,
1150                      i.discount
1151                     FROM orderitems i
1152                     JOIN parts p ON (i.parts_id = p.id)
1153                     JOIN oe a ON (i.trans_id = a.id)
1154                     JOIN vendor ct ON (a.vendor_id = ct.id)
1155                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1156                     LEFT JOIN employee e ON (a.employee_id = e.id)
1157                     $makemodeljoin
1158                     WHERE $ordwhere
1159                     AND a.vendor_id > 0|;
1160       }
1161     
1162     }
1163       
1164     if ($form->{rfq} || $form->{quoted}) {
1165       my $quowhere = "$where
1166                      AND a.quotation = '1'";
1167       $quowhere .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
1168       $quowhere .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
1169
1170       if ($form->{description}) {
1171         $var = $form->like(lc $form->{description});
1172         $quowhere .= " AND lower(i.description) LIKE '$var'";
1173       }
1174       
1175       if ($form->{open} || $form->{closed}) {
1176         unless ($form->{open} && $form->{closed}) {
1177           $ordwhere .= " AND a.closed = '0'" if $form->{open};
1178           $ordwhere .= " AND a.closed = '1'" if $form->{closed};
1179         }
1180       } else {
1181         $ordwhere .= " AND a.id = 0";
1182       }
1183
1184
1185       $flds = qq|p.id, p.partnumber, i.description, '' AS serialnumber,
1186                  i.qty AS onhand, i.unit, p.bin, i.sellprice,
1187                  p.listprice, p.lastcost, p.rop, p.weight,
1188                  p.priceupdate, p.image, p.drawing, p.microfiche,
1189                  p.assembly,
1190                  pg.partsgroup, '' AS invnumber, a.ordnumber, a.quonumber,
1191                  i.trans_id, ct.name, e.name AS employee, a.curr, '0' AS till
1192                  $makemodelfld|;
1193
1194       if ($form->{quoted}) {
1195         $query .= qq|$union
1196                      SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
1197                     (SELECT buy FROM exchangerate ex
1198                      WHERE ex.curr = a.curr
1199                      AND ex.transdate = a.transdate) AS exchangerate,
1200                      i.discount
1201                      FROM orderitems i
1202                      JOIN parts p ON (i.parts_id = p.id)
1203                      JOIN oe a ON (i.trans_id = a.id)
1204                      JOIN customer ct ON (a.customer_id = ct.id)
1205                      LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1206                      LEFT JOIN employee e ON (a.employee_id = e.id)
1207                      $makemodeljoin
1208                      WHERE $quowhere
1209                      AND a.customer_id > 0|;
1210         $union = "
1211                   UNION";
1212       }
1213       
1214       if ($form->{rfq}) {
1215         $flds = qq|p.id, p.partnumber, i.description, '' AS serialnumber,
1216                    i.qty AS onhand, i.unit, p.bin, i.sellprice,
1217                    p.listprice, p.lastcost, p.rop, p.weight,
1218                    p.priceupdate, p.image, p.drawing, p.microfiche,
1219                    p.assembly,
1220                    pg.partsgroup, '' AS invnumber, a.ordnumber, a.quonumber,
1221                    i.trans_id, ct.name, e.name AS employee, a.curr, '0' AS till
1222                    $makemodelfld|;
1223
1224         $query .= qq|$union
1225                     SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
1226                     (SELECT sell FROM exchangerate ex
1227                      WHERE ex.curr = a.curr
1228                      AND ex.transdate = a.transdate) AS exchangerate,
1229                      i.discount
1230                     FROM orderitems i
1231                     JOIN parts p ON (i.parts_id = p.id)
1232                     JOIN oe a ON (i.trans_id = a.id)
1233                     JOIN vendor ct ON (a.vendor_id = ct.id)
1234                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1235                     LEFT JOIN employee e ON (a.employee_id = e.id)
1236                     $makemodeljoin
1237                     WHERE $quowhere
1238                     AND a.vendor_id > 0|;
1239       }
1240
1241     }
1242
1243     $query .= qq|
1244                  ORDER BY $sortorder|;
1245
1246   }
1247
1248   
1249   my $sth = $dbh->prepare($query);
1250   $sth->execute || $form->dberror($query);
1251
1252   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1253     push @{ $form->{parts} }, $ref;
1254   }
1255   $sth->finish;
1256
1257   my @a = ();
1258   
1259   # include individual items for assembly
1260   if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1261
1262     if ($form->{sold} || $form->{ordered} || $form->{quoted}) {
1263       $flds = qq|p.id, p.partnumber, p.description, a.qty AS onhand, p.unit,
1264                  p.bin, p.sellprice, p.listprice, p.lastcost, p.rop,
1265                  p.weight, p.priceupdate, p.image, p.drawing, p.microfiche,
1266                  p.assembly, pg.partsgroup
1267                  $makemodelflds $assemblyflds
1268                  |;
1269     } else {
1270       # replace p.onhand with a.qty AS onhand
1271       $flds =~ s/p.onhand/a.qty AS onhand/;
1272     }
1273         
1274     while ($item = shift @{ $form->{parts} }) {
1275       push @a, $item;
1276       $flds =~ s/a\.qty.*AS onhand/a\.qty * $item->{onhand} AS onhand/;
1277       push @a, &include_assembly($dbh, $form, $item->{id}, $flds, $makemodeljoin);
1278       push @a, {id => $item->{id}};
1279     }
1280
1281     # copy assemblies to $form->{parts}
1282     @{ $form->{parts} } = @a;
1283     
1284   }
1285     
1286   
1287   @a = ();
1288   if ($form->{l_warehouse} || $form->{l_warehouse}) {
1289     
1290     if ($form->{warehouse}) {
1291       ($null, $var) = split /--/, $form->{warehouse};
1292       $var *= 1;
1293       $query = qq|SELECT SUM(qty) AS onhand, '$null' AS description
1294                   FROM inventory
1295                   WHERE warehouse_id = $var
1296                   AND parts_id = ?|;
1297     } else {
1298
1299       $query = qq|SELECT SUM(i.qty) AS onhand, w.description AS warehouse
1300                   FROM inventory i
1301                   JOIN warehouse w ON (w.id = i.warehouse_id)
1302                   WHERE i.parts_id = ?
1303                   GROUP BY w.description|;
1304     }
1305
1306     $sth = $dbh->prepare($query) || $form->dberror($query);
1307
1308     foreach $item (@{ $form->{parts} }) {
1309
1310       if ($item->{onhand} <= 0 && ! $form->{warehouse}) {
1311         push @a, $item;
1312         next;
1313       }
1314
1315       $sth->execute($item->{id}) || $form->dberror($query);
1316       
1317       if ($form->{warehouse}) {
1318         
1319         $ref = $sth->fetchrow_hashref(NAME_lc);
1320         if ($ref->{onhand} > 0) {
1321           $item->{onhand} = $ref->{onhand};
1322           push @a, $item;
1323         }
1324
1325       } else {
1326
1327         push @a, $item;
1328         
1329         while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1330           if ($ref->{onhand} > 0) {
1331             push @a, $ref;
1332           }
1333         }
1334       }
1335       
1336       $sth->finish;
1337     }
1338
1339     @{ $form->{parts} } = @a;
1340
1341   }
1342
1343   $dbh->disconnect;
1344
1345 }
1346
1347
1348 sub include_assembly {
1349   my ($dbh, $form, $id, $flds, $makemodeljoin) = @_;
1350   
1351   $form->{stagger}++;
1352   if ($form->{stagger} > $form->{pncol}) {
1353     $form->{pncol} = $form->{stagger};
1354   }
1355  
1356   $form->{$id} = 1;
1357   
1358   my @a = ();
1359   my $query = qq|SELECT $flds
1360                  FROM parts p
1361                  JOIN assembly a ON (a.parts_id = p.id)
1362                  LEFT JOIN partsgroup pg ON (pg.id = p.id)
1363                  $makemodeljoin
1364                  WHERE a.id = $id|;
1365   my $sth = $dbh->prepare($query);
1366   $sth->execute || $form->dberror($query);
1367
1368   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1369     $ref->{assemblyitem} = 1;
1370     $ref->{stagger} = $form->{stagger};
1371     push @a, $ref;
1372     if ($ref->{assembly} && !$form->{$ref->{id}}) {
1373       push @a, &include_assembly($dbh, $form, $ref->{id}, $flds, $makemodeljoin);
1374       if ($form->{stagger} > $form->{pncol}) {
1375         $form->{pncol} = $form->{stagger};
1376       }
1377     }
1378   }
1379   $sth->finish;
1380
1381   $form->{$id} = 0;
1382   $form->{stagger}--;
1383
1384   @a;
1385
1386 }
1387
1388
1389 sub create_links {
1390   my ($self, $module, $myconfig, $form) = @_;
1391
1392   # connect to database
1393   my $dbh = $form->dbconnect($myconfig);
1394   
1395   my $ref;
1396
1397   my $query = qq|SELECT accno, description, link
1398                  FROM chart
1399                  WHERE link LIKE '%$module%'
1400                  ORDER BY accno|;
1401   my $sth = $dbh->prepare($query);
1402   $sth->execute || $form->dberror($query);
1403
1404   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1405     foreach my $key (split /:/, $ref->{link}) {
1406       if ($key =~ /$module/) {
1407         push @{ $form->{"${module}_links"}{$key} }, { accno => $ref->{accno},
1408                                       description => $ref->{description} };
1409       }
1410     }
1411   }
1412   $sth->finish;
1413
1414   if ($form->{item} ne 'assembly') {
1415     $query = qq|SELECT count(*) FROM vendor|;
1416     $sth = $dbh->prepare($query);
1417     $sth->execute || $form->dberror($query);
1418     my ($count) = $sth->fetchrow_array;
1419     $sth->finish;
1420
1421     if ($count < $myconfig->{vclimit}) {
1422       $query = qq|SELECT id, name
1423                   FROM vendor
1424                   ORDER BY name|;
1425       $sth = $dbh->prepare($query);
1426       $sth->execute || $form->dberror($query);
1427
1428       while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1429         push @{ $form->{all_vendor} }, $ref;
1430       }
1431       $sth->finish;
1432     }
1433   }
1434
1435
1436   # pricegroups, customers
1437   $query = qq|SELECT count(*) FROM customer|;
1438   $sth = $dbh->prepare($query);
1439   $sth->execute || $form->dberror($query);
1440   my ($count) = $sth->fetchrow_array;
1441   $sth->finish;
1442
1443   if ($count < $myconfig->{vclimit}) {
1444     $query = qq|SELECT id, name
1445                 FROM customer
1446                 ORDER BY name|;
1447     $sth = $dbh->prepare($query);
1448     $sth->execute || $form->dberror($query);
1449
1450     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1451       push @{ $form->{all_customer} }, $ref;
1452     }
1453     $sth->finish;
1454   }
1455
1456   $query = qq|SELECT id, pricegroup
1457               FROM pricegroup
1458               ORDER BY pricegroup|;
1459   $sth = $dbh->prepare($query);
1460   $sth->execute || $form->dberror($query);
1461
1462   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1463     push @{ $form->{all_pricegroup} }, $ref;
1464   }
1465   $sth->finish;
1466
1467
1468   if ($form->{id}) {
1469     $query = qq|SELECT weightunit, curr AS currencies
1470                 FROM defaults|;
1471     $sth = $dbh->prepare($query);
1472     $sth->execute || $form->dberror($query);
1473
1474     ($form->{weightunit}, $form->{currencies}) = $sth->fetchrow_array;
1475     $sth->finish;
1476
1477   } else {
1478     $query = qq|SELECT weightunit, current_date, curr AS currencies
1479                 FROM defaults|;
1480     $sth = $dbh->prepare($query);
1481     $sth->execute || $form->dberror($query);
1482
1483     ($form->{weightunit}, $form->{priceupdate}, $form->{currencies}) = $sth->fetchrow_array;
1484     $sth->finish;
1485   }
1486   
1487   $dbh->disconnect;
1488
1489 }
1490
1491
1492 sub get_warehouses {
1493   my ($self, $myconfig, $form) = @_;
1494
1495   my $dbh = $form->dbconnect($myconfig);
1496
1497   my $query = qq|SELECT id, description
1498                  FROM warehouse|;
1499
1500   my $sth = $dbh->prepare($query);
1501   $sth->execute || $form->dberror($query);
1502
1503   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1504     push @{ $form->{all_warehouses} }, $ref;
1505   }
1506   $sth->finish;
1507
1508   $dbh->disconnect;
1509
1510 }
1511
1512 1;
1513