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}";
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();
234 sub get_pricegroups {
235 $main::lxdebug->enter_sub();
237 my ($self, $myconfig, $form) = @_;
238 my $dbh = $form->dbconnect($myconfig);
240 my @pricegroups_not_used = ();
243 my $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
245 my $pkq = $dbh->prepare($query);
246 $pkq->execute || $form->dberror($query);
247 while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
248 push @{ $form->{PRICEGROUPS} }, $pkr;
252 #find not used pricegroups
253 while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
254 push @pricegroups_not_used, $tmp;
257 # if not used pricegroups are avaible
258 if (@pricegroups_not_used) {
260 foreach $name (@pricegroups_not_used) {
261 $form->{"klass_$i"} = "$name->{id}";
262 $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
263 $form->{"price_$i"} =
264 $form->format_amount($myconfig, $form->{"price_$i"}, 5);
265 $form->{"pricegroup_id_$i"} = "$name->{id}";
266 $form->{"pricegroup_$i"} = "$name->{pricegroup}";
272 $form->{price_rows} = $i - 1;
276 $main::lxdebug->leave_sub();
281 $main::lxdebug->enter_sub();
283 my ($self, $myconfig, $form) = @_;
285 if ($form->{eur} && ($form->{item} ne 'service')) {
286 $form->{IC} = $form->{IC_expense};
289 ($form->{inventory_accno}) = split(/--/, $form->{IC});
290 ($form->{expense_accno}) = split(/--/, $form->{IC_expense});
291 ($form->{income_accno}) = split(/--/, $form->{IC_income});
293 # connect to database, turn off AutoCommit
294 my $dbh = $form->dbconnect_noauto($myconfig);
297 # make up a unique handle and store in partnumber field
298 # then retrieve the record based on the unique handle to get the id
299 # replace the partnumber field with the actual variable
300 # add records for makemodel
302 # if there is a $form->{id} then replace the old entry
303 # delete all makemodel entries and add the new ones
306 map { $form->{$_} =~ s/\'/\'\'/g } qw(partnumber description notes unit);
308 # undo amount formatting
309 map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) }
310 qw(rop weight listprice sellprice gv lastcost stock);
312 # set date to NULL if nothing entered
313 $form->{priceupdate} =
314 ($form->{priceupdate}) ? qq|'$form->{priceupdate}'| : "NULL";
316 $form->{makemodel} = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
318 $form->{alternate} = 0;
319 $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
320 $form->{obsolete} *= 1;
322 $form->{onhand} *= 1;
331 $query = qq|SELECT p.sellprice, p.weight
333 WHERE p.id = $form->{id}|;
334 $sth = $dbh->prepare($query);
335 $sth->execute || $form->dberror($query);
336 my ($sellprice, $weight) = $sth->fetchrow_array;
339 # if item is part of an assembly adjust all assemblies
340 $query = qq|SELECT a.id, a.qty
342 WHERE a.parts_id = $form->{id}|;
343 $sth = $dbh->prepare($query);
344 $sth->execute || $form->dberror($query);
345 while (my ($id, $qty) = $sth->fetchrow_array) {
346 &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
350 if ($form->{item} ne 'service') {
352 # delete makemodel records
353 $query = qq|DELETE FROM makemodel
354 WHERE parts_id = $form->{id}|;
355 $dbh->do($query) || $form->dberror($query);
358 if ($form->{item} eq 'assembly') {
359 if ($form->{onhand} != 0) {
360 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
363 # delete assembly records
364 $query = qq|DELETE FROM assembly
365 WHERE id = $form->{id}|;
366 $dbh->do($query) || $form->dberror($query);
368 $form->{onhand} += $form->{stock};
372 $query = qq|DELETE FROM partstax
373 WHERE parts_id = $form->{id}|;
374 $dbh->do($query) || $form->dberror($query);
377 my $uid = rand() . time;
378 $uid .= $form->{login};
380 $query = qq|SELECT p.id FROM parts p
381 WHERE p.partnumber = '$form->{partnumber}'|;
382 $sth = $dbh->prepare($query);
383 $sth->execute || $form->dberror($query);
384 ($form->{id}) = $sth->fetchrow_array;
387 if ($form->{id} ne "") {
388 $main::lxdebug->leave_sub();
391 $query = qq|INSERT INTO parts (partnumber, description)
392 VALUES ('$uid', 'dummy')|;
393 $dbh->do($query) || $form->dberror($query);
395 $query = qq|SELECT p.id FROM parts p
396 WHERE p.partnumber = '$uid'|;
397 $sth = $dbh->prepare($query);
398 $sth->execute || $form->dberror($query);
400 ($form->{id}) = $sth->fetchrow_array;
403 $form->{orphaned} = 1;
404 $form->{onhand} = $form->{stock} if $form->{item} eq 'assembly';
405 if ($form->{partnumber} eq "" && $form->{inventory_accno} eq "") {
406 $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
408 if ($form->{partnumber} eq "" && $form->{inventory_accno} ne "") {
409 $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
413 my $partsgroup_id = 0;
415 if ($form->{partsgroup}) {
416 ($partsgroup, $partsgroup_id) = split /--/, $form->{partsgroup};
419 $query = qq|UPDATE parts SET
420 partnumber = '$form->{partnumber}',
421 description = '$form->{description}',
422 makemodel = '$form->{makemodel}',
423 alternate = '$form->{alternate}',
424 assembly = '$form->{assembly}',
425 listprice = $form->{listprice},
426 sellprice = $form->{sellprice},
427 lastcost = $form->{lastcost},
428 weight = $form->{weight},
429 priceupdate = $form->{priceupdate},
430 unit = '$form->{unit}',
431 notes = '$form->{notes}',
433 bin = '$form->{bin}',
434 inventory_accno_id = (SELECT c.id FROM chart c
435 WHERE c.accno = '$form->{inventory_accno}'),
436 income_accno_id = (SELECT c.id FROM chart c
437 WHERE c.accno = '$form->{income_accno}'),
438 expense_accno_id = (SELECT c.id FROM chart c
439 WHERE c.accno = '$form->{expense_accno}'),
440 obsolete = '$form->{obsolete}',
441 image = '$form->{image}',
442 drawing = '$form->{drawing}',
443 shop = '$form->{shop}',
446 microfiche = '$form->{microfiche}',
447 partsgroup_id = $partsgroup_id
448 WHERE id = $form->{id}|;
449 $dbh->do($query) || $form->dberror($query);
451 # delete price records
452 $query = qq|DELETE FROM prices
453 WHERE parts_id = $form->{id}|;
454 $dbh->do($query) || $form->dberror($query);
456 # insert price records only if different to sellprice
457 for my $i (1 .. $form->{price_rows}) {
458 if ($form->{"price_$i"} eq "0") {
459 $form->{"price_$i"} = $form->{sellprice};
461 if (( $form->{"price_$i"}
462 || $form->{"klass_$i"}
463 || $form->{"pricegroup_id_$i"}) and $form->{"price_$i"} != $form->{sellprice}) {
464 $klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
465 $price = $form->parse_amount($myconfig, $form->{"price_$i"});
467 $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
468 $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price)
469 VALUES($form->{id},$pricegroup_id,$price)|;
470 $dbh->do($query) || $form->dberror($query);
474 # insert makemodel records
475 unless ($form->{item} eq 'service') {
476 for my $i (1 .. $form->{makemodel_rows}) {
477 if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
478 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
480 $query = qq|INSERT INTO makemodel (parts_id, make, model)
482 '$form->{"make_$i"}', '$form->{"model_$i"}')|;
483 $dbh->do($query) || $form->dberror($query);
489 foreach $item (split / /, $form->{taxaccounts}) {
490 if ($form->{"IC_tax_$item"}) {
491 $query = qq|INSERT INTO partstax (parts_id, chart_id)
495 WHERE c.accno = '$item'))|;
496 $dbh->do($query) || $form->dberror($query);
500 # add assembly records
501 if ($form->{item} eq 'assembly') {
503 for my $i (1 .. $form->{assembly_rows}) {
504 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
506 if ($form->{"qty_$i"} != 0) {
507 $form->{"bom_$i"} *= 1;
508 $query = qq|INSERT INTO assembly (id, parts_id, qty, bom)
509 VALUES ($form->{id}, $form->{"id_$i"},
510 $form->{"qty_$i"}, '$form->{"bom_$i"}')|;
511 $dbh->do($query) || $form->dberror($query);
515 # adjust onhand for the parts
516 if ($form->{onhand} != 0) {
517 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
523 my $shippingdate = "$a[5]-$a[4]-$a[3]";
525 $form->get_employee($dbh);
527 # add inventory record
528 $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
529 shippingdate, employee_id) VALUES (
530 0, $form->{id}, $form->{stock}, '$shippingdate',
531 $form->{employee_id})|;
532 $dbh->do($query) || $form->dberror($query);
536 #set expense_accno=inventory_accno if they are different => bilanz
538 ($form->{expense_accno} != $form->{inventory_accno})
539 ? $form->{inventory_accno}
540 : $form->{expense_accno};
542 # get tax rates and description
544 ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
545 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
547 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
549 $stw = $dbh->prepare($query);
551 $stw->execute || $form->dberror($query);
553 $form->{taxaccount} = "";
554 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
556 # if ($customertax{$ref->{accno}}) {
557 $form->{taxaccount} .= "$ptr->{accno} ";
558 if (!($form->{taxaccount2} =~ /$ptr->{accno}/)) {
559 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
560 $form->{"$ptr->{accno}_description"} = $ptr->{description};
561 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
562 $form->{taxaccount2} .= " $ptr->{accno} ";
568 my $rc = $dbh->commit;
571 $main::lxdebug->leave_sub();
576 sub update_assembly {
577 $main::lxdebug->enter_sub();
579 my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
581 my $query = qq|SELECT a.id, a.qty
583 WHERE a.parts_id = $id|;
584 my $sth = $dbh->prepare($query);
585 $sth->execute || $form->dberror($query);
587 while (my ($pid, $aqty) = $sth->fetchrow_array) {
588 &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
592 $query = qq|UPDATE parts
593 SET sellprice = sellprice +
594 $qty * ($form->{sellprice} - $sellprice),
596 $qty * ($form->{weight} - $weight)
598 $dbh->do($query) || $form->dberror($query);
600 $main::lxdebug->leave_sub();
603 sub retrieve_assemblies {
604 $main::lxdebug->enter_sub();
606 my ($self, $myconfig, $form) = @_;
608 # connect to database
609 my $dbh = $form->dbconnect($myconfig);
613 if ($form->{partnumber}) {
614 my $partnumber = $form->like(lc $form->{partnumber});
615 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
618 if ($form->{description}) {
619 my $description = $form->like(lc $form->{description});
620 $where .= " AND lower(p.description) LIKE '$description'";
622 $where .= " AND NOT p.obsolete = '1'";
624 # retrieve assembly items
625 my $query = qq|SELECT p.id, p.partnumber, p.description,
626 p.bin, p.onhand, p.rop,
627 (SELECT sum(p2.inventory_accno_id)
628 FROM parts p2, assembly a
629 WHERE p2.id = a.parts_id
630 AND a.id = p.id) AS inventory
635 my $sth = $dbh->prepare($query);
636 $sth->execute || $form->dberror($query);
638 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
639 push @{ $form->{assembly_items} }, $ref if $ref->{inventory};
645 $main::lxdebug->leave_sub();
648 sub restock_assemblies {
649 $main::lxdebug->enter_sub();
651 my ($self, $myconfig, $form) = @_;
653 # connect to database
654 my $dbh = $form->dbconnect_noauto($myconfig);
656 for my $i (1 .. $form->{rowcount}) {
658 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
660 if ($form->{"qty_$i"} != 0) {
661 &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
666 my $rc = $dbh->commit;
669 $main::lxdebug->leave_sub();
674 sub adjust_inventory {
675 $main::lxdebug->enter_sub();
677 my ($dbh, $form, $id, $qty) = @_;
679 my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
680 FROM parts p, assembly a
681 WHERE a.parts_id = p.id
683 my $sth = $dbh->prepare($query);
684 $sth->execute || $form->dberror($query);
686 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
688 my $allocate = $qty * $ref->{qty};
690 # is it a service item, then loop
691 $ref->{inventory_accno_id} *= 1;
692 next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
694 # adjust parts onhand
695 $form->update_balance($dbh, "parts", "onhand",
703 my $rc = $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $qty);
705 $main::lxdebug->leave_sub();
711 $main::lxdebug->enter_sub();
713 my ($self, $myconfig, $form) = @_;
715 # connect to database, turn off AutoCommit
716 my $dbh = $form->dbconnect_noauto($myconfig);
718 my $query = qq|DELETE FROM parts
719 WHERE id = $form->{id}|;
720 $dbh->do($query) || $form->dberror($query);
722 $query = qq|DELETE FROM partstax
723 WHERE parts_id = $form->{id}|;
724 $dbh->do($query) || $form->dberror($query);
726 # check if it is a part, assembly or service
727 if ($form->{item} ne 'service') {
728 $query = qq|DELETE FROM makemodel
729 WHERE parts_id = $form->{id}|;
730 $dbh->do($query) || $form->dberror($query);
733 if ($form->{item} eq 'assembly') {
736 $query = qq|DELETE FROM inventory
737 WHERE parts_id = $form->{id}|;
738 $dbh->do($query) || $form->dberror($query);
740 $query = qq|DELETE FROM assembly
741 WHERE id = $form->{id}|;
742 $dbh->do($query) || $form->dberror($query);
745 if ($form->{item} eq 'alternate') {
746 $query = qq|DELETE FROM alternate
747 WHERE id = $form->{id}|;
748 $dbh->do($query) || $form->dberror($query);
752 my $rc = $dbh->commit;
755 $main::lxdebug->leave_sub();
761 $main::lxdebug->enter_sub();
763 my ($self, $myconfig, $form) = @_;
765 my $i = $form->{assembly_rows};
769 if ($form->{"partnumber_$i"}) {
770 $var = $form->like(lc $form->{"partnumber_$i"});
771 $where .= " AND lower(p.partnumber) LIKE '$var'";
773 if ($form->{"description_$i"}) {
774 $var = $form->like(lc $form->{"description_$i"});
775 $where .= " AND lower(p.description) LIKE '$var'";
777 if ($form->{"partsgroup_$i"}) {
778 $var = $form->like(lc $form->{"partsgroup_$i"});
779 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
783 $where .= " AND NOT p.id = $form->{id}";
787 $where .= " ORDER BY p.partnumber";
789 $where .= " ORDER BY p.description";
792 # connect to database
793 my $dbh = $form->dbconnect($myconfig);
795 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
796 p.weight, p.onhand, p.unit,
799 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
801 my $sth = $dbh->prepare($query);
802 $sth->execute || $form->dberror($query);
804 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
805 push @{ $form->{item_list} }, $ref;
811 $main::lxdebug->leave_sub();
815 $main::lxdebug->enter_sub();
817 my ($self, $myconfig, $form) = @_;
825 foreach my $item (qw(partnumber drawing microfiche make model)) {
826 if ($form->{$item}) {
827 $var = $form->like(lc $form->{$item});
829 # make will build later Bugfix 145
830 if ($item ne 'make') {
831 $where .= " AND lower(p.$item) LIKE '$var'";
836 # special case for description
837 if ($form->{description}) {
838 unless ( $form->{bought}
843 || $form->{quoted}) {
844 $var = $form->like(lc $form->{description});
845 $where .= " AND lower(p.description) LIKE '$var'";
849 # special case for serialnumber
850 if ($form->{l_serialnumber}) {
851 if ($form->{serialnumber}) {
852 $var = $form->like(lc $form->{serialnumber});
853 $where .= " AND lower(serialnumber) LIKE '$var'";
857 if ($form->{searchitems} eq 'part') {
858 $where .= " AND p.inventory_accno_id > 0";
860 if ($form->{searchitems} eq 'assembly') {
861 $form->{bought} = "";
862 $where .= " AND p.assembly = '1'";
864 if ($form->{searchitems} eq 'service') {
865 $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
867 # irrelevant for services
868 $form->{make} = $form->{model} = "";
871 # items which were never bought, sold or on an order
872 if ($form->{itemstatus} eq 'orphaned') {
873 $form->{onhand} = $form->{short} = 0;
874 $form->{bought} = $form->{sold} = 0;
875 $form->{onorder} = $form->{ordered} = 0;
876 $form->{rfq} = $form->{quoted} = 0;
878 $form->{transdatefrom} = $form->{transdateto} = "";
880 $where .= " AND p.onhand = 0
881 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
882 WHERE p.id = i.parts_id)
883 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
884 WHERE p.id = a.parts_id)
885 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
886 WHERE p.id = o.parts_id)";
889 if ($form->{itemstatus} eq 'active') {
890 $where .= " AND p.obsolete = '0'";
892 if ($form->{itemstatus} eq 'obsolete') {
893 $where .= " AND p.obsolete = '1'";
894 $form->{onhand} = $form->{short} = 0;
896 if ($form->{itemstatus} eq 'onhand') {
897 $where .= " AND p.onhand > 0";
899 if ($form->{itemstatus} eq 'short') {
900 $where .= " AND p.onhand < p.rop";
903 $var = $form->like(lc $form->{make});
904 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
905 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
907 if ($form->{model}) {
908 $var = $form->like(lc $form->{model});
909 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
910 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
912 if ($form->{partsgroup}) {
913 $var = $form->like(lc $form->{partsgroup});
914 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
916 if ($form->{l_soldtotal}) {
917 $where .= " AND p.id=i.parts_id AND i.qty >= 0";
919 " GROUP BY p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
921 if ($form->{top100}) {
922 $limit = " LIMIT 100";
926 if ($form->{revers} == 1) {
927 $form->{desc} = " DESC";
932 # connect to database
933 my $dbh = $form->dbconnect($myconfig);
935 my $sortorder = $form->{sort};
936 $sortorder .= $form->{desc};
937 $sortorder = $form->{sort} if $form->{sort};
941 if ($form->{l_soldtotal}) {
942 $form->{soldtotal} = 'soldtotal';
944 qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
945 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
946 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
952 $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
953 p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
954 p.priceupdate, p.image, p.drawing, p.microfiche,
957 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
960 ORDER BY $sortorder|;
963 # rebuild query for bought and sold items
969 || $form->{quoted}) {
971 my @a = qw(partnumber description bin priceupdate name);
973 push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
974 push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
975 push @a, "quonumber" if ($form->{rfq} || $form->{quoted});
980 if ($form->{bought} || $form->{sold}) {
982 my $invwhere = "$where";
983 $invwhere .= " AND i.assemblyitem = '0'";
984 $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
985 if $form->{transdatefrom};
986 $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
987 if $form->{transdateto};
989 if ($form->{description}) {
990 $var = $form->like(lc $form->{description});
991 $invwhere .= " AND lower(i.description) LIKE '$var'";
994 my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
995 i.qty AS onhand, i.unit, p.bin, i.sellprice,
996 p.listprice, p.lastcost, p.rop, p.weight,
997 p.priceupdate, p.image, p.drawing, p.microfiche,
999 a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
1002 if ($form->{bought}) {
1004 SELECT $flds, 'ir' AS module, '' AS type,
1007 JOIN parts p ON (p.id = i.parts_id)
1008 JOIN ap a ON (a.id = i.trans_id)
1009 JOIN vendor ct ON (a.vendor_id = ct.id)
1010 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1016 if ($form->{sold}) {
1018 SELECT $flds, 'is' AS module, '' AS type,
1021 JOIN parts p ON (p.id = i.parts_id)
1022 JOIN ar a ON (a.id = i.trans_id)
1023 JOIN customer ct ON (a.customer_id = ct.id)
1024 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1031 if ($form->{onorder} || $form->{ordered}) {
1032 my $ordwhere = "$where
1033 AND o.quotation = '0'";
1034 $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1035 if $form->{transdatefrom};
1036 $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
1037 if $form->{transdateto};
1039 if ($form->{description}) {
1040 $var = $form->like(lc $form->{description});
1041 $ordwhere .= " AND lower(oi.description) LIKE '$var'";
1044 $flds = qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1045 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1046 p.listprice, p.lastcost, p.rop, p.weight,
1047 p.priceupdate, p.image, p.drawing, p.microfiche,
1049 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1052 if ($form->{ordered}) {
1054 SELECT $flds, 'oe' AS module, 'sales_order' AS type,
1055 (SELECT buy FROM exchangerate ex
1056 WHERE ex.curr = o.curr
1057 AND ex.transdate = o.transdate) AS exchangerate
1059 JOIN parts p ON (oi.parts_id = p.id)
1060 JOIN oe o ON (oi.trans_id = o.id)
1061 JOIN customer ct ON (o.customer_id = ct.id)
1062 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1064 AND o.customer_id > 0|;
1069 if ($form->{onorder}) {
1070 $flds = qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1071 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1072 p.listprice, p.lastcost, p.rop, p.weight,
1073 p.priceupdate, p.image, p.drawing, p.microfiche,
1075 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1079 SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
1080 (SELECT sell FROM exchangerate ex
1081 WHERE ex.curr = o.curr
1082 AND ex.transdate = o.transdate) AS exchangerate
1084 JOIN parts p ON (oi.parts_id = p.id)
1085 JOIN oe o ON (oi.trans_id = o.id)
1086 JOIN vendor ct ON (o.vendor_id = ct.id)
1087 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1089 AND o.vendor_id > 0|;
1094 if ($form->{rfq} || $form->{quoted}) {
1095 my $quowhere = "$where
1096 AND o.quotation = '1'";
1097 $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1098 if $form->{transdatefrom};
1099 $quowhere .= " AND o.transdate <= '$form->{transdateto}'"
1100 if $form->{transdateto};
1102 if ($form->{description}) {
1103 $var = $form->like(lc $form->{description});
1104 $quowhere .= " AND lower(oi.description) LIKE '$var'";
1107 $flds = qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1108 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1109 p.listprice, p.lastcost, p.rop, p.weight,
1110 p.priceupdate, p.image, p.drawing, p.microfiche,
1112 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1115 if ($form->{quoted}) {
1117 SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
1118 (SELECT buy FROM exchangerate ex
1119 WHERE ex.curr = o.curr
1120 AND ex.transdate = o.transdate) AS exchangerate
1122 JOIN parts p ON (oi.parts_id = p.id)
1123 JOIN oe o ON (oi.trans_id = o.id)
1124 JOIN customer ct ON (o.customer_id = ct.id)
1125 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1127 AND o.customer_id > 0|;
1133 $flds = qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1134 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1135 p.listprice, p.lastcost, p.rop, p.weight,
1136 p.priceupdate, p.image, p.drawing, p.microfiche,
1138 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1142 SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
1143 (SELECT sell FROM exchangerate ex
1144 WHERE ex.curr = o.curr
1145 AND ex.transdate = o.transdate) AS exchangerate
1147 JOIN parts p ON (oi.parts_id = p.id)
1148 JOIN oe o ON (oi.trans_id = o.id)
1149 JOIN vendor ct ON (o.vendor_id = ct.id)
1150 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1152 AND o.vendor_id > 0|;
1157 ORDER BY $sortorder|;
1160 my $sth = $dbh->prepare($query);
1161 $sth->execute || $form->dberror($query);
1163 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1164 push @{ $form->{parts} }, $ref;
1169 # include individual items for assemblies
1170 if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1171 foreach $item (@{ $form->{parts} }) {
1172 push @assemblies, $item;
1173 $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1175 p.sellprice, p.listprice, p.lastcost,
1176 p.rop, p.weight, p.priceupdate,
1177 p.image, p.drawing, p.microfiche
1178 FROM parts p, assembly a
1179 WHERE p.id = a.parts_id
1180 AND a.id = $item->{id}|;
1182 $sth = $dbh->prepare($query);
1183 $sth->execute || $form->dberror($query);
1185 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1186 $ref->{assemblyitem} = 1;
1187 push @assemblies, $ref;
1191 push @assemblies, { id => $item->{id} };
1195 # copy assemblies to $form->{parts}
1196 @{ $form->{parts} } = @assemblies;
1200 $main::lxdebug->leave_sub();
1204 $main::lxdebug->enter_sub();
1206 my ($self, $module, $myconfig, $form) = @_;
1208 # connect to database
1209 my $dbh = $form->dbconnect($myconfig);
1212 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1213 p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1214 FROM chart c, parts p
1215 WHERE c.link LIKE '%$module%'
1216 AND p.id = $form->{id}
1219 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1220 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1221 FROM chart c, defaults d
1222 WHERE c.link LIKE '%$module%'
1226 my $sth = $dbh->prepare($query);
1227 $sth->execute || $form->dberror($query);
1228 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1229 foreach my $key (split /:/, $ref->{link}) {
1230 if ($key =~ /$module/) {
1231 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1232 || ($ref->{id} eq $ref->{income_accno_id})
1233 || ($ref->{id} eq $ref->{expense_accno_id})) {
1234 push @{ $form->{"${module}_links"}{$key} },
1235 { accno => $ref->{accno},
1236 description => $ref->{description},
1237 selected => "selected" };
1239 push @{ $form->{"${module}_links"}{$key} },
1240 { accno => $ref->{accno},
1241 description => $ref->{description},
1250 $query = qq|SELECT weightunit
1252 $sth = $dbh->prepare($query);
1253 $sth->execute || $form->dberror($query);
1255 ($form->{weightunit}) = $sth->fetchrow_array;
1259 $query = qq|SELECT weightunit, current_date
1261 $sth = $dbh->prepare($query);
1262 $sth->execute || $form->dberror($query);
1264 ($form->{weightunit}, $form->{priceupdate}) = $sth->fetchrow_array;
1269 $main::lxdebug->leave_sub();
1272 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1274 $main::lxdebug->enter_sub();
1276 my ($self, $myconfig, $form, $sortorder) = @_;
1277 my $dbh = $form->dbconnect($myconfig);
1278 my $order = " p.partnumber";
1279 my $where = "1 = 1";
1281 if ($sortorder eq "all") {
1282 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1283 $where .= " AND p.description LIKE '%$form->{description}%'";
1285 if ($sortorder eq "partnumber") {
1286 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1287 $order = qq|p.$sortorder|;
1289 if ($sortorder eq "description") {
1290 $where .= " AND p.description LIKE '%$form->{description}%'";
1295 qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
1296 my $sth = $dbh->prepare($query);
1297 $sth->execute || $self->dberror($query);
1299 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1300 if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1303 $form->{"id_$j"} = $ref->{id};
1304 $form->{"partnumber_$j"} = $ref->{partnumber};
1305 $form->{"description_$j"} = $ref->{description};
1306 $form->{"unit_$j"} = $ref->{unit};
1307 $form->{"sellprice_$j"} = $ref->{sellprice};
1308 $form->{"soldtotal_$j"} = get_soldtotal($dbh, $ref->{id});
1315 $main::lxdebug->leave_sub();
1320 # gets sum of sold part with part_id
1322 $main::lxdebug->enter_sub();
1324 my ($dbh, $id) = @_;
1327 qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
1329 my $sth = $dbh->prepare($query);
1330 $sth->execute || $form->dberror($query);
1333 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1335 $sum = $ref->{totalsold};
1339 if ($sum eq undef) {
1343 $main::lxdebug->leave_sub();
1346 } #end get_soldtotal
1349 $main::lxdebug->enter_sub();
1351 my ($self, $myconfig, $form) = @_;
1352 my $i = $form->{rowcount};
1353 my $where = "NOT p.obsolete = '1'";
1355 if ($form->{"partnumber_$i"}) {
1356 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1357 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1359 if ($form->{"description_$i"}) {
1360 my $description = $form->like(lc $form->{"description_$i"});
1361 $where .= " AND lower(p.description) LIKE '$description'";
1364 if ($form->{"partsgroup_$i"}) {
1365 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1366 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1369 if ($form->{"description_$i"}) {
1370 $where .= " ORDER BY description";
1372 $where .= " ORDER BY partnumber";
1375 # connect to database
1376 my $dbh = $form->dbconnect($myconfig);
1378 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1380 c1.accno AS inventory_accno,
1381 c2.accno AS income_accno,
1382 c3.accno AS expense_accno,
1383 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1386 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1387 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1388 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1389 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1391 my $sth = $dbh->prepare($query);
1392 $sth->execute || $form->dberror($query);
1394 #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1396 # get tax rates and description
1397 #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
1398 #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1399 # FROM chart c, tax t
1400 # WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1402 # $stw = $dbh->prepare($query);
1403 #$stw->execute || $form->dberror($query);
1405 #$ref->{taxaccounts} = "";
1406 #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1408 # $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1409 # $form->{"$ptr->{accno}_description"} = $ptr->{description};
1410 # $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1411 # $form->{taxaccounts} .= "$ptr->{accno} ";
1412 # $ref->{taxaccounts} .= "$ptr->{accno} ";
1417 #chop $ref->{taxaccounts};
1419 push @{ $form->{item_list} }, $ref;
1425 $main::lxdebug->leave_sub();