1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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 #======================================================================
31 # Inventory Control backend
33 #======================================================================
38 $main::lxdebug->enter_sub();
40 my ($self, $myconfig, $form) = @_;
43 my $dbh = $form->dbconnect($myconfig);
45 my $query = qq|SELECT p.*,
46 c1.accno AS inventory_accno,
47 c2.accno AS income_accno,
48 c3.accno AS expense_accno,
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);
60 # copy to $form variables
61 map { $form->{$_} = $ref->{$_} } (keys %{$ref});
65 my %oid = ('Pg' => 'a.oid',
66 'Oracle' => 'a.rowid');
68 # part or service item
69 $form->{item} = ($form->{inventory_accno}) ? 'part' : 'service';
70 if ($form->{assembly}) {
71 $form->{item} = 'assembly';
73 # retrieve assembly items
74 $query = qq|SELECT p.id, p.partnumber, p.description,
75 p.sellprice, p.weight, a.qty, a.bom, p.unit,
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}}|;
83 $sth = $dbh->prepare($query);
84 $sth->execute || $form->dberror($query);
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};
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};
106 qq|SELECT p.parts_id, p.pricegroup_id, p.price, (SELECT pg.pricegroup FROM pricegroup pg WHERE pg.id=p.pricegroup_id) AS pricegroup FROM prices p
107 WHERE parts_id = $form->{id}
108 ORDER by pricegroup|;
110 $sth = $dbh->prepare($query);
111 $sth->execute || $form->dberror($query);
114 @pricegroups_not_used = ();
119 ($form->{"klass_$i"}, $form->{"pricegroup_id_$i"},
120 $form->{"price_$i"}, $form->{"pricegroup_$i"})
121 = $sth->fetchrow_array
123 $form->{"price_$i"} = $form->round_amount($form->{"price_$i"}, 5);
124 $form->{"price_$i"} =
125 $form->format_amount($myconfig, $form->{"price_$i"}, 5);
126 push @pricegroups, $form->{"pricegroup_id_$i"};
133 $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
135 $pkq = $dbh->prepare($query);
136 $pkq->execute || $form->dberror($query);
137 while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
138 push @{ $form->{PRICEGROUPS} }, $pkr;
142 #find not used pricegroups
143 while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
145 foreach $item (@pricegroups) {
146 if ($item eq $tmp->{id}) {
153 push @pricegroups_not_used, $tmp;
157 # if not used pricegroups are avaible
158 if (@pricegroups_not_used) {
160 foreach $name (@pricegroups_not_used) {
161 $form->{"klass_$i"} = "$name->{id}";
162 $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
163 $form->{"price_$i"} =
164 $form->format_amount($myconfig, $form->{"price_$i"}, 5);
165 $form->{"pricegroup_id_$i"} = "$name->{id}";
166 $form->{"pricegroup_$i"} = "$name->{pricegroup}\n";
172 $form->{price_rows} = $i - 1;
174 unless ($form->{item} eq 'service') {
177 if ($form->{makemodel}) {
178 $query = qq|SELECT m.make, m.model FROM makemodel m
179 WHERE m.parts_id = $form->{id}|;
181 $sth = $dbh->prepare($query);
182 $sth->execute || $form->dberror($query);
185 while (($form->{"make_$i"}, $form->{"model_$i"}) = $sth->fetchrow_array)
190 $form->{makemodel_rows} = $i - 1;
195 # now get accno for taxes
196 $query = qq|SELECT c.accno
197 FROM chart c, partstax pt
198 WHERE pt.chart_id = c.id
199 AND pt.parts_id = $form->{id}|;
201 $sth = $dbh->prepare($query);
202 $sth->execute || $form->dberror($query);
204 while (($key) = $sth->fetchrow_array) {
205 $form->{amount}{$key} = $key;
211 $query = qq|SELECT i.parts_id
213 WHERE i.parts_id = $form->{id}
217 WHERE o.parts_id = $form->{id}
221 WHERE a.parts_id = $form->{id}|;
222 $sth = $dbh->prepare($query);
223 $sth->execute || $form->dberror($query);
225 ($form->{orphaned}) = $sth->fetchrow_array;
226 $form->{orphaned} = !$form->{orphaned};
231 $main::lxdebug->leave_sub();
235 $main::lxdebug->enter_sub();
237 my ($self, $myconfig, $form) = @_;
239 if ($form->{eur} && ($form->{item} ne 'service')) {
240 $form->{IC} = $form->{IC_expense};
243 ($form->{inventory_accno}) = split(/--/, $form->{IC});
244 ($form->{expense_accno}) = split(/--/, $form->{IC_expense});
245 ($form->{income_accno}) = split(/--/, $form->{IC_income});
247 # connect to database, turn off AutoCommit
248 my $dbh = $form->dbconnect_noauto($myconfig);
251 # make up a unique handle and store in partnumber field
252 # then retrieve the record based on the unique handle to get the id
253 # replace the partnumber field with the actual variable
254 # add records for makemodel
256 # if there is a $form->{id} then replace the old entry
257 # delete all makemodel entries and add the new ones
260 map { $form->{$_} =~ s/\'/\'\'/g } qw(partnumber description notes unit);
262 # undo amount formatting
263 map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) }
264 qw(rop weight listprice sellprice gv lastcost stock);
266 # set date to NULL if nothing entered
267 $form->{priceupdate} =
268 ($form->{priceupdate}) ? qq|'$form->{priceupdate}'| : "NULL";
270 $form->{makemodel} = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
272 $form->{alternate} = 0;
273 $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
274 $form->{obsolete} *= 1;
276 $form->{onhand} *= 1;
285 $query = qq|SELECT p.sellprice, p.weight
287 WHERE p.id = $form->{id}|;
288 $sth = $dbh->prepare($query);
289 $sth->execute || $form->dberror($query);
290 my ($sellprice, $weight) = $sth->fetchrow_array;
293 # if item is part of an assembly adjust all assemblies
294 $query = qq|SELECT a.id, a.qty
296 WHERE a.parts_id = $form->{id}|;
297 $sth = $dbh->prepare($query);
298 $sth->execute || $form->dberror($query);
299 while (my ($id, $qty) = $sth->fetchrow_array) {
300 &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
304 if ($form->{item} ne 'service') {
306 # delete makemodel records
307 $query = qq|DELETE FROM makemodel
308 WHERE parts_id = $form->{id}|;
309 $dbh->do($query) || $form->dberror($query);
312 if ($form->{item} eq 'assembly') {
313 if ($form->{onhand} != 0) {
314 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
317 # delete assembly records
318 $query = qq|DELETE FROM assembly
319 WHERE id = $form->{id}|;
320 $dbh->do($query) || $form->dberror($query);
322 $form->{onhand} += $form->{stock};
326 $query = qq|DELETE FROM partstax
327 WHERE parts_id = $form->{id}|;
328 $dbh->do($query) || $form->dberror($query);
331 my $uid = rand() . time;
332 $uid .= $form->{login};
334 $query = qq|SELECT p.id FROM parts p
335 WHERE p.partnumber = '$form->{partnumber}'|;
336 $sth = $dbh->prepare($query);
337 $sth->execute || $form->dberror($query);
338 ($form->{id}) = $sth->fetchrow_array;
341 if ($form->{id} ne "") {
342 $main::lxdebug->leave_sub();
345 $query = qq|INSERT INTO parts (partnumber, description)
346 VALUES ('$uid', 'dummy')|;
347 $dbh->do($query) || $form->dberror($query);
349 $query = qq|SELECT p.id FROM parts p
350 WHERE p.partnumber = '$uid'|;
351 $sth = $dbh->prepare($query);
352 $sth->execute || $form->dberror($query);
354 ($form->{id}) = $sth->fetchrow_array;
357 $form->{orphaned} = 1;
358 $form->{onhand} = $form->{stock} if $form->{item} eq 'assembly';
359 if ($form->{partnumber} eq "" && $form->{inventory_accno} eq "") {
360 $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
362 if ($form->{partnumber} eq "" && $form->{inventory_accno} ne "") {
363 $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
367 my $partsgroup_id = 0;
369 if ($form->{partsgroup}) {
370 ($partsgroup, $partsgroup_id) = split /--/, $form->{partsgroup};
373 $query = qq|UPDATE parts SET
374 partnumber = '$form->{partnumber}',
375 description = '$form->{description}',
376 makemodel = '$form->{makemodel}',
377 alternate = '$form->{alternate}',
378 assembly = '$form->{assembly}',
379 listprice = $form->{listprice},
380 sellprice = $form->{sellprice},
381 lastcost = $form->{lastcost},
382 weight = $form->{weight},
383 priceupdate = $form->{priceupdate},
384 unit = '$form->{unit}',
385 notes = '$form->{notes}',
387 bin = '$form->{bin}',
388 inventory_accno_id = (SELECT c.id FROM chart c
389 WHERE c.accno = '$form->{inventory_accno}'),
390 income_accno_id = (SELECT c.id FROM chart c
391 WHERE c.accno = '$form->{income_accno}'),
392 expense_accno_id = (SELECT c.id FROM chart c
393 WHERE c.accno = '$form->{expense_accno}'),
394 obsolete = '$form->{obsolete}',
395 image = '$form->{image}',
396 drawing = '$form->{drawing}',
397 shop = '$form->{shop}',
400 microfiche = '$form->{microfiche}',
401 partsgroup_id = $partsgroup_id
402 WHERE id = $form->{id}|;
403 $dbh->do($query) || $form->dberror($query);
405 # delete price records
406 $query = qq|DELETE FROM prices
407 WHERE parts_id = $form->{id}|;
408 $dbh->do($query) || $form->dberror($query);
410 # insert price records only if different to sellprice
411 for my $i (1 .. $form->{price_rows}) {
412 if (( $form->{"price_$i"}
413 || $form->{"klass_$i"}
414 || $form->{"pricegroup_id_$i"}) and $form->{"price_$i"} != $form->{sellprice}) {
415 $klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
416 $price = $form->parse_amount($myconfig, $form->{"price_$i"});
418 $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
419 $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price)
420 VALUES($form->{id},$pricegroup_id,$price)|;
421 $dbh->do($query) || $form->dberror($query);
425 # insert makemodel records
426 unless ($form->{item} eq 'service') {
427 for my $i (1 .. $form->{makemodel_rows}) {
428 if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
429 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
431 $query = qq|INSERT INTO makemodel (parts_id, make, model)
433 '$form->{"make_$i"}', '$form->{"model_$i"}')|;
434 $dbh->do($query) || $form->dberror($query);
440 foreach $item (split / /, $form->{taxaccounts}) {
441 if ($form->{"IC_tax_$item"}) {
442 $query = qq|INSERT INTO partstax (parts_id, chart_id)
446 WHERE c.accno = '$item'))|;
447 $dbh->do($query) || $form->dberror($query);
451 # add assembly records
452 if ($form->{item} eq 'assembly') {
454 for my $i (1 .. $form->{assembly_rows}) {
455 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
457 if ($form->{"qty_$i"} != 0) {
458 $form->{"bom_$i"} *= 1;
459 $query = qq|INSERT INTO assembly (id, parts_id, qty, bom)
460 VALUES ($form->{id}, $form->{"id_$i"},
461 $form->{"qty_$i"}, '$form->{"bom_$i"}')|;
462 $dbh->do($query) || $form->dberror($query);
466 # adjust onhand for the parts
467 if ($form->{onhand} != 0) {
468 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
474 my $shippingdate = "$a[5]-$a[4]-$a[3]";
476 $form->get_employee($dbh);
478 # add inventory record
479 $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
480 shippingdate, employee_id) VALUES (
481 0, $form->{id}, $form->{stock}, '$shippingdate',
482 $form->{employee_id})|;
483 $dbh->do($query) || $form->dberror($query);
487 #set expense_accno=inventory_accno if they are different => bilanz
489 ($form->{expense_accno} != $form->{inventory_accno})
490 ? $form->{inventory_accno}
491 : $form->{expense_accno};
493 # get tax rates and description
495 ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
496 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
498 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
500 $stw = $dbh->prepare($query);
502 $stw->execute || $form->dberror($query);
504 $form->{taxaccount} = "";
505 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
507 # if ($customertax{$ref->{accno}}) {
508 $form->{taxaccount} .= "$ptr->{accno} ";
509 if (!($form->{taxaccount2} =~ /$ptr->{accno}/)) {
510 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
511 $form->{"$ptr->{accno}_description"} = $ptr->{description};
512 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
513 $form->{taxaccount2} .= " $ptr->{accno} ";
519 my $rc = $dbh->commit;
522 $main::lxdebug->leave_sub();
527 sub update_assembly {
528 $main::lxdebug->enter_sub();
530 my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
532 my $query = qq|SELECT a.id, a.qty
534 WHERE a.parts_id = $id|;
535 my $sth = $dbh->prepare($query);
536 $sth->execute || $form->dberror($query);
538 while (my ($pid, $aqty) = $sth->fetchrow_array) {
539 &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
543 $query = qq|UPDATE parts
544 SET sellprice = sellprice +
545 $qty * ($form->{sellprice} - $sellprice),
547 $qty * ($form->{weight} - $weight)
549 $dbh->do($query) || $form->dberror($query);
551 $main::lxdebug->leave_sub();
554 sub retrieve_assemblies {
555 $main::lxdebug->enter_sub();
557 my ($self, $myconfig, $form) = @_;
559 # connect to database
560 my $dbh = $form->dbconnect($myconfig);
564 if ($form->{partnumber}) {
565 my $partnumber = $form->like(lc $form->{partnumber});
566 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
569 if ($form->{description}) {
570 my $description = $form->like(lc $form->{description});
571 $where .= " AND lower(p.description) LIKE '$description'";
573 $where .= " AND NOT p.obsolete = '1'";
575 # retrieve assembly items
576 my $query = qq|SELECT p.id, p.partnumber, p.description,
577 p.bin, p.onhand, p.rop,
578 (SELECT sum(p2.inventory_accno_id)
579 FROM parts p2, assembly a
580 WHERE p2.id = a.parts_id
581 AND a.id = p.id) AS inventory
586 my $sth = $dbh->prepare($query);
587 $sth->execute || $form->dberror($query);
589 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
590 push @{ $form->{assembly_items} }, $ref if $ref->{inventory};
596 $main::lxdebug->leave_sub();
599 sub restock_assemblies {
600 $main::lxdebug->enter_sub();
602 my ($self, $myconfig, $form) = @_;
604 # connect to database
605 my $dbh = $form->dbconnect_noauto($myconfig);
607 for my $i (1 .. $form->{rowcount}) {
609 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
611 if ($form->{"qty_$i"} != 0) {
612 &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
617 my $rc = $dbh->commit;
620 $main::lxdebug->leave_sub();
625 sub adjust_inventory {
626 $main::lxdebug->enter_sub();
628 my ($dbh, $form, $id, $qty) = @_;
630 my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
631 FROM parts p, assembly a
632 WHERE a.parts_id = p.id
634 my $sth = $dbh->prepare($query);
635 $sth->execute || $form->dberror($query);
637 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
639 my $allocate = $qty * $ref->{qty};
641 # is it a service item, then loop
642 $ref->{inventory_accno_id} *= 1;
643 next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
645 # adjust parts onhand
646 $form->update_balance($dbh, "parts", "onhand",
654 my $rc = $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $qty);
656 $main::lxdebug->leave_sub();
662 $main::lxdebug->enter_sub();
664 my ($self, $myconfig, $form) = @_;
666 # connect to database, turn off AutoCommit
667 my $dbh = $form->dbconnect_noauto($myconfig);
669 my $query = qq|DELETE FROM parts
670 WHERE id = $form->{id}|;
671 $dbh->do($query) || $form->dberror($query);
673 $query = qq|DELETE FROM partstax
674 WHERE parts_id = $form->{id}|;
675 $dbh->do($query) || $form->dberror($query);
677 # check if it is a part, assembly or service
678 if ($form->{item} ne 'service') {
679 $query = qq|DELETE FROM makemodel
680 WHERE parts_id = $form->{id}|;
681 $dbh->do($query) || $form->dberror($query);
684 if ($form->{item} eq 'assembly') {
687 $query = qq|DELETE FROM inventory
688 WHERE parts_id = $form->{id}|;
689 $dbh->do($query) || $form->dberror($query);
691 $query = qq|DELETE FROM assembly
692 WHERE id = $form->{id}|;
693 $dbh->do($query) || $form->dberror($query);
696 if ($form->{item} eq 'alternate') {
697 $query = qq|DELETE FROM alternate
698 WHERE id = $form->{id}|;
699 $dbh->do($query) || $form->dberror($query);
703 my $rc = $dbh->commit;
706 $main::lxdebug->leave_sub();
712 $main::lxdebug->enter_sub();
714 my ($self, $myconfig, $form) = @_;
716 my $i = $form->{assembly_rows};
720 if ($form->{"partnumber_$i"}) {
721 $var = $form->like(lc $form->{"partnumber_$i"});
722 $where .= " AND lower(p.partnumber) LIKE '$var'";
724 if ($form->{"description_$i"}) {
725 $var = $form->like(lc $form->{"description_$i"});
726 $where .= " AND lower(p.description) LIKE '$var'";
728 if ($form->{"partsgroup_$i"}) {
729 $var = $form->like(lc $form->{"partsgroup_$i"});
730 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
734 $where .= " AND NOT p.id = $form->{id}";
738 $where .= " ORDER BY p.partnumber";
740 $where .= " ORDER BY p.description";
743 # connect to database
744 my $dbh = $form->dbconnect($myconfig);
746 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
747 p.weight, p.onhand, p.unit,
750 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
752 my $sth = $dbh->prepare($query);
753 $sth->execute || $form->dberror($query);
755 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
756 push @{ $form->{item_list} }, $ref;
762 $main::lxdebug->leave_sub();
766 $main::lxdebug->enter_sub();
768 my ($self, $myconfig, $form) = @_;
776 foreach my $item (qw(partnumber drawing microfiche make model)) {
777 if ($form->{$item}) {
778 $var = $form->like(lc $form->{$item});
780 # make will build later Bugfix 145
781 if ($item ne 'make') {
782 $where .= " AND lower(p.$item) LIKE '$var'";
787 # special case for description
788 if ($form->{description}) {
789 unless ( $form->{bought}
794 || $form->{quoted}) {
795 $var = $form->like(lc $form->{description});
796 $where .= " AND lower(p.description) LIKE '$var'";
800 # special case for serialnumber
801 if ($form->{l_serialnumber}) {
802 if ($form->{serialnumber}) {
803 $var = $form->like(lc $form->{serialnumber});
804 $where .= " AND lower(serialnumber) LIKE '$var'";
808 if ($form->{searchitems} eq 'part') {
809 $where .= " AND p.inventory_accno_id > 0";
811 if ($form->{searchitems} eq 'assembly') {
812 $form->{bought} = "";
813 $where .= " AND p.assembly = '1'";
815 if ($form->{searchitems} eq 'service') {
816 $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
818 # irrelevant for services
819 $form->{make} = $form->{model} = "";
822 # items which were never bought, sold or on an order
823 if ($form->{itemstatus} eq 'orphaned') {
824 $form->{onhand} = $form->{short} = 0;
825 $form->{bought} = $form->{sold} = 0;
826 $form->{onorder} = $form->{ordered} = 0;
827 $form->{rfq} = $form->{quoted} = 0;
829 $form->{transdatefrom} = $form->{transdateto} = "";
831 $where .= " AND p.onhand = 0
832 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
833 WHERE p.id = i.parts_id)
834 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
835 WHERE p.id = a.parts_id)
836 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
837 WHERE p.id = o.parts_id)";
840 if ($form->{itemstatus} eq 'active') {
841 $where .= " AND p.obsolete = '0'";
843 if ($form->{itemstatus} eq 'obsolete') {
844 $where .= " AND p.obsolete = '1'";
845 $form->{onhand} = $form->{short} = 0;
847 if ($form->{itemstatus} eq 'onhand') {
848 $where .= " AND p.onhand > 0";
850 if ($form->{itemstatus} eq 'short') {
851 $where .= " AND p.onhand < p.rop";
854 $var = $form->like(lc $form->{make});
855 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
856 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
858 if ($form->{model}) {
859 $var = $form->like(lc $form->{model});
860 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
861 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
863 if ($form->{partsgroup}) {
864 $var = $form->like(lc $form->{partsgroup});
865 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
867 if ($form->{l_soldtotal}) {
868 $where .= " AND p.id=i.parts_id AND i.qty >= 0";
870 " GROUP BY p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
872 if ($form->{top100}) {
873 $limit = " LIMIT 100";
877 if ($form->{revers} == 1) {
878 $form->{desc} = " DESC";
883 # connect to database
884 my $dbh = $form->dbconnect($myconfig);
886 my $sortorder = $form->{sort};
887 $sortorder .= $form->{desc};
888 $sortorder = $form->{sort} unless $sortorder;
892 if ($form->{l_soldtotal}) {
893 $form->{soldtotal} = 'soldtotal';
895 qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
896 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
897 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
903 $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
904 p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
905 p.priceupdate, p.image, p.drawing, p.microfiche,
908 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
911 ORDER BY $sortorder|;
914 # rebuild query for bought and sold items
920 || $form->{quoted}) {
922 my @a = qw(partnumber description bin priceupdate name);
924 push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
925 push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
926 push @a, "quonumber" if ($form->{rfq} || $form->{quoted});
931 if ($form->{bought} || $form->{sold}) {
933 my $invwhere = "$where";
934 $invwhere .= " AND i.assemblyitem = '0'";
935 $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
936 if $form->{transdatefrom};
937 $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
938 if $form->{transdateto};
940 if ($form->{description}) {
941 $var = $form->like(lc $form->{description});
942 $invwhere .= " AND lower(i.description) LIKE '$var'";
945 my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
946 i.qty AS onhand, i.unit, p.bin, i.sellprice,
947 p.listprice, p.lastcost, p.rop, p.weight,
948 p.priceupdate, p.image, p.drawing, p.microfiche,
950 a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
953 if ($form->{bought}) {
955 SELECT $flds, 'ir' AS module, '' AS type,
958 JOIN parts p ON (p.id = i.parts_id)
959 JOIN ap a ON (a.id = i.trans_id)
960 JOIN vendor ct ON (a.vendor_id = ct.id)
961 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
969 SELECT $flds, 'is' AS module, '' AS type,
972 JOIN parts p ON (p.id = i.parts_id)
973 JOIN ar a ON (a.id = i.trans_id)
974 JOIN customer ct ON (a.customer_id = ct.id)
975 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
982 if ($form->{onorder} || $form->{ordered}) {
983 my $ordwhere = "$where
984 AND o.quotation = '0'";
985 $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
986 if $form->{transdatefrom};
987 $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
988 if $form->{transdateto};
990 if ($form->{description}) {
991 $var = $form->like(lc $form->{description});
992 $ordwhere .= " AND lower(oi.description) LIKE '$var'";
995 $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
996 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
997 p.listprice, p.lastcost, p.rop, p.weight,
998 p.priceupdate, p.image, p.drawing, p.microfiche,
1000 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1003 if ($form->{ordered}) {
1005 SELECT $flds, 'oe' AS module, 'sales_order' AS type,
1006 (SELECT buy FROM exchangerate ex
1007 WHERE ex.curr = o.curr
1008 AND ex.transdate = o.transdate) AS exchangerate
1010 JOIN parts p ON (oi.parts_id = p.id)
1011 JOIN oe o ON (oi.trans_id = o.id)
1012 JOIN customer ct ON (o.customer_id = ct.id)
1013 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1015 AND o.customer_id > 0|;
1020 if ($form->{onorder}) {
1021 $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
1022 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1023 p.listprice, p.lastcost, p.rop, p.weight,
1024 p.priceupdate, p.image, p.drawing, p.microfiche,
1026 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1030 SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
1031 (SELECT sell FROM exchangerate ex
1032 WHERE ex.curr = o.curr
1033 AND ex.transdate = o.transdate) AS exchangerate
1035 JOIN parts p ON (oi.parts_id = p.id)
1036 JOIN oe o ON (oi.trans_id = o.id)
1037 JOIN vendor ct ON (o.vendor_id = ct.id)
1038 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1040 AND o.vendor_id > 0|;
1045 if ($form->{rfq} || $form->{quoted}) {
1046 my $quowhere = "$where
1047 AND o.quotation = '1'";
1048 $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1049 if $form->{transdatefrom};
1050 $quowhere .= " AND o.transdate <= '$form->{transdateto}'"
1051 if $form->{transdateto};
1053 if ($form->{description}) {
1054 $var = $form->like(lc $form->{description});
1055 $quowhere .= " AND lower(oi.description) LIKE '$var'";
1058 $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
1059 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1060 p.listprice, p.lastcost, p.rop, p.weight,
1061 p.priceupdate, p.image, p.drawing, p.microfiche,
1063 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1066 if ($form->{quoted}) {
1068 SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
1069 (SELECT buy FROM exchangerate ex
1070 WHERE ex.curr = o.curr
1071 AND ex.transdate = o.transdate) AS exchangerate
1073 JOIN parts p ON (oi.parts_id = p.id)
1074 JOIN oe o ON (oi.trans_id = o.id)
1075 JOIN customer ct ON (o.customer_id = ct.id)
1076 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1078 AND o.customer_id > 0|;
1084 $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
1085 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1086 p.listprice, p.lastcost, p.rop, p.weight,
1087 p.priceupdate, p.image, p.drawing, p.microfiche,
1089 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1093 SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
1094 (SELECT sell FROM exchangerate ex
1095 WHERE ex.curr = o.curr
1096 AND ex.transdate = o.transdate) AS exchangerate
1098 JOIN parts p ON (oi.parts_id = p.id)
1099 JOIN oe o ON (oi.trans_id = o.id)
1100 JOIN vendor ct ON (o.vendor_id = ct.id)
1101 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1103 AND o.vendor_id > 0|;
1108 ORDER BY $sortorder|;
1111 my $sth = $dbh->prepare($query);
1112 $sth->execute || $form->dberror($query);
1114 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1115 push @{ $form->{parts} }, $ref;
1120 # include individual items for assemblies
1121 if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1122 foreach $item (@{ $form->{parts} }) {
1123 push @assemblies, $item;
1124 $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1126 p.sellprice, p.listprice, p.lastcost,
1127 p.rop, p.weight, p.priceupdate,
1128 p.image, p.drawing, p.microfiche
1129 FROM parts p, assembly a
1130 WHERE p.id = a.parts_id
1131 AND a.id = $item->{id}|;
1133 $sth = $dbh->prepare($query);
1134 $sth->execute || $form->dberror($query);
1136 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1137 $ref->{assemblyitem} = 1;
1138 push @assemblies, $ref;
1142 push @assemblies, { id => $item->{id} };
1146 # copy assemblies to $form->{parts}
1147 @{ $form->{parts} } = @assemblies;
1151 $main::lxdebug->leave_sub();
1155 $main::lxdebug->enter_sub();
1157 my ($self, $module, $myconfig, $form) = @_;
1159 # connect to database
1160 my $dbh = $form->dbconnect($myconfig);
1163 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1164 p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1165 FROM chart c, parts p
1166 WHERE c.link LIKE '%$module%'
1167 AND p.id = $form->{id}
1170 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1171 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1172 FROM chart c, defaults d
1173 WHERE c.link LIKE '%$module%'
1177 my $sth = $dbh->prepare($query);
1178 $sth->execute || $form->dberror($query);
1179 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1180 foreach my $key (split /:/, $ref->{link}) {
1181 if ($key =~ /$module/) {
1182 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1183 || ($ref->{id} eq $ref->{income_accno_id})
1184 || ($ref->{id} eq $ref->{expense_accno_id})) {
1185 push @{ $form->{"${module}_links"}{$key} },
1186 { accno => $ref->{accno},
1187 description => $ref->{description},
1188 selected => "selected" };
1190 push @{ $form->{"${module}_links"}{$key} },
1191 { accno => $ref->{accno},
1192 description => $ref->{description},
1201 $query = qq|SELECT weightunit
1203 $sth = $dbh->prepare($query);
1204 $sth->execute || $form->dberror($query);
1206 ($form->{weightunit}) = $sth->fetchrow_array;
1210 $query = qq|SELECT weightunit, current_date
1212 $sth = $dbh->prepare($query);
1213 $sth->execute || $form->dberror($query);
1215 ($form->{weightunit}, $form->{priceupdate}) = $sth->fetchrow_array;
1220 $main::lxdebug->leave_sub();
1223 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1225 $main::lxdebug->enter_sub();
1227 my ($self, $myconfig, $form, $sortorder) = @_;
1228 my $dbh = $form->dbconnect($myconfig);
1229 my $order = " p.partnumber";
1230 my $where = "1 = 1";
1232 if ($sortorder eq "all") {
1233 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1234 $where .= " AND p.description LIKE '%$form->{description}%'";
1236 if ($sortorder eq "partnumber") {
1237 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1238 $order = qq|p.$sortorder|;
1240 if ($sortorder eq "description") {
1241 $where .= " AND p.description LIKE '%$form->{description}%'";
1246 qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
1247 my $sth = $dbh->prepare($query);
1248 $sth->execute || $self->dberror($query);
1250 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1251 if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1254 $form->{"id_$j"} = $ref->{id};
1255 $form->{"partnumber_$j"} = $ref->{partnumber};
1256 $form->{"description_$j"} = $ref->{description};
1257 $form->{"unit_$j"} = $ref->{unit};
1258 $form->{"sellprice_$j"} = $ref->{sellprice};
1259 $form->{"soldtotal_$j"} = get_soldtotal($dbh, $ref->{id});
1266 $main::lxdebug->leave_sub();
1271 # gets sum of sold part with part_id
1273 $main::lxdebug->enter_sub();
1275 my ($dbh, $id) = @_;
1278 qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
1280 my $sth = $dbh->prepare($query);
1281 $sth->execute || $form->dberror($query);
1284 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1286 $sum = $ref->{totalsold};
1290 if ($sum eq undef) {
1294 $main::lxdebug->leave_sub();
1297 } #end get_soldtotal
1300 $main::lxdebug->enter_sub();
1302 my ($self, $myconfig, $form) = @_;
1303 my $i = $form->{rowcount};
1304 my $where = "NOT p.obsolete = '1'";
1306 if ($form->{"partnumber_$i"}) {
1307 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1308 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1310 if ($form->{"description_$i"}) {
1311 my $description = $form->like(lc $form->{"description_$i"});
1312 $where .= " AND lower(p.description) LIKE '$description'";
1315 if ($form->{"partsgroup_$i"}) {
1316 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1317 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1320 if ($form->{"description_$i"}) {
1321 $where .= " ORDER BY description";
1323 $where .= " ORDER BY partnumber";
1326 # connect to database
1327 my $dbh = $form->dbconnect($myconfig);
1329 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1331 c1.accno AS inventory_accno,
1332 c2.accno AS income_accno,
1333 c3.accno AS expense_accno,
1334 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1337 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1338 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1339 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1340 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1342 my $sth = $dbh->prepare($query);
1343 $sth->execute || $form->dberror($query);
1345 #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1347 # get tax rates and description
1348 #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
1349 #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1350 # FROM chart c, tax t
1351 # WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1353 # $stw = $dbh->prepare($query);
1354 #$stw->execute || $form->dberror($query);
1356 #$ref->{taxaccounts} = "";
1357 #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1359 # $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1360 # $form->{"$ptr->{accno}_description"} = $ptr->{description};
1361 # $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1362 # $form->{taxaccounts} .= "$ptr->{accno} ";
1363 # $ref->{taxaccounts} .= "$ptr->{accno} ";
1368 #chop $ref->{taxaccounts};
1370 push @{ $form->{item_list} }, $ref;
1376 $main::lxdebug->leave_sub();