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 #======================================================================
39 $main::lxdebug->enter_sub();
41 my ($self, $myconfig, $form) = @_;
44 my $dbh = $form->dbconnect($myconfig);
46 my $query = qq|SELECT p.*,
47 c1.accno AS inventory_accno,
48 c2.accno AS income_accno,
49 c3.accno AS expense_accno,
52 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
53 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
54 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
55 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
56 WHERE p.id = $form->{id}|;
57 my $sth = $dbh->prepare($query);
58 $sth->execute || $form->dberror($query);
59 my $ref = $sth->fetchrow_hashref(NAME_lc);
61 # copy to $form variables
62 map { $form->{$_} = $ref->{$_} } (keys %{$ref});
66 my %oid = ('Pg' => 'a.oid',
67 'Oracle' => 'a.rowid');
69 # part or service item
70 $form->{item} = ($form->{inventory_accno}) ? 'part' : 'service';
71 if ($form->{assembly}) {
72 $form->{item} = 'assembly';
74 # retrieve assembly items
75 $query = qq|SELECT p.id, p.partnumber, p.description,
76 p.sellprice, p.weight, a.qty, a.bom, p.unit,
79 JOIN assembly a ON (a.parts_id = p.id)
80 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
81 WHERE a.id = $form->{id}
82 ORDER BY $oid{$myconfig->{dbdriver}}|;
84 $sth = $dbh->prepare($query);
85 $sth->execute || $form->dberror($query);
87 $form->{assembly_rows} = 0;
88 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
89 $form->{assembly_rows}++;
90 foreach my $key (keys %{$ref}) {
91 $form->{"${key}_$form->{assembly_rows}"} = $ref->{$key};
98 # setup accno hash for <option checked> {amount} is used in create_links
99 $form->{amount}{IC} = $form->{inventory_accno};
100 $form->{amount}{IC_income} = $form->{income_accno};
101 $form->{amount}{IC_sale} = $form->{income_accno};
102 $form->{amount}{IC_expense} = $form->{expense_accno};
103 $form->{amount}{IC_cogs} = $form->{expense_accno};
107 qq|SELECT p.parts_id, p.pricegroup_id, p.price, (SELECT pg.pricegroup FROM pricegroup pg WHERE pg.id=p.pricegroup_id) AS pricegroup FROM prices p
108 WHERE parts_id = $form->{id}
109 ORDER by pricegroup|;
111 $sth = $dbh->prepare($query);
112 $sth->execute || $form->dberror($query);
115 @pricegroups_not_used = ();
120 ($form->{"klass_$i"}, $form->{"pricegroup_id_$i"},
121 $form->{"price_$i"}, $form->{"pricegroup_$i"})
122 = $sth->fetchrow_array
124 $form->{"price_$i"} = $form->round_amount($form->{"price_$i"}, 5);
125 $form->{"price_$i"} =
126 $form->format_amount($myconfig, $form->{"price_$i"}, 5);
127 push @pricegroups, $form->{"pricegroup_id_$i"};
134 $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
136 $pkq = $dbh->prepare($query);
137 $pkq->execute || $form->dberror($query);
138 while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
139 push @{ $form->{PRICEGROUPS} }, $pkr;
143 #find not used pricegroups
144 while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
146 foreach $item (@pricegroups) {
147 if ($item eq $tmp->{id}) {
154 push @pricegroups_not_used, $tmp;
158 # if not used pricegroups are avaible
159 if (@pricegroups_not_used) {
161 foreach $name (@pricegroups_not_used) {
162 $form->{"klass_$i"} = "$name->{id}";
163 $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
164 $form->{"price_$i"} =
165 $form->format_amount($myconfig, $form->{"price_$i"}, 5);
166 $form->{"pricegroup_id_$i"} = "$name->{id}";
167 $form->{"pricegroup_$i"} = "$name->{pricegroup}";
173 $form->{price_rows} = $i - 1;
175 unless ($form->{item} eq 'service') {
178 if ($form->{makemodel}) {
179 $query = qq|SELECT m.make, m.model FROM makemodel m
180 WHERE m.parts_id = $form->{id}|;
182 $sth = $dbh->prepare($query);
183 $sth->execute || $form->dberror($query);
186 while (($form->{"make_$i"}, $form->{"model_$i"}) = $sth->fetchrow_array)
191 $form->{makemodel_rows} = $i - 1;
196 # now get accno for taxes
197 $query = qq|SELECT c.accno
198 FROM chart c, partstax pt
199 WHERE pt.chart_id = c.id
200 AND pt.parts_id = $form->{id}|;
202 $sth = $dbh->prepare($query);
203 $sth->execute || $form->dberror($query);
205 while (($key) = $sth->fetchrow_array) {
206 $form->{amount}{$key} = $key;
212 $query = qq|SELECT i.parts_id
214 WHERE i.parts_id = $form->{id}
218 WHERE o.parts_id = $form->{id}
222 WHERE a.parts_id = $form->{id}|;
223 $sth = $dbh->prepare($query);
224 $sth->execute || $form->dberror($query);
226 ($form->{orphaned}) = $sth->fetchrow_array;
227 $form->{orphaned} = !$form->{orphaned};
232 $main::lxdebug->leave_sub();
235 sub get_pricegroups {
236 $main::lxdebug->enter_sub();
238 my ($self, $myconfig, $form) = @_;
239 my $dbh = $form->dbconnect($myconfig);
241 my @pricegroups_not_used = ();
244 my $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
246 my $pkq = $dbh->prepare($query);
247 $pkq->execute || $form->dberror($query);
248 while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
249 push @{ $form->{PRICEGROUPS} }, $pkr;
253 #find not used pricegroups
254 while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
255 push @pricegroups_not_used, $tmp;
258 # if not used pricegroups are avaible
259 if (@pricegroups_not_used) {
261 foreach $name (@pricegroups_not_used) {
262 $form->{"klass_$i"} = "$name->{id}";
263 $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
264 $form->{"price_$i"} =
265 $form->format_amount($myconfig, $form->{"price_$i"}, 5);
266 $form->{"pricegroup_id_$i"} = "$name->{id}";
267 $form->{"pricegroup_$i"} = "$name->{pricegroup}";
273 $form->{price_rows} = $i - 1;
277 $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};
462 ( $form->{"price_$i"}
463 || $form->{"klass_$i"}
464 || $form->{"pricegroup_id_$i"})
465 and $form->{"price_$i"} != $form->{sellprice}
467 $klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
468 $price = $form->parse_amount($myconfig, $form->{"price_$i"});
470 $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
471 $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price)
472 VALUES($form->{id},$pricegroup_id,$price)|;
473 $dbh->do($query) || $form->dberror($query);
477 # insert makemodel records
478 unless ($form->{item} eq 'service') {
479 for my $i (1 .. $form->{makemodel_rows}) {
480 if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
481 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
483 $query = qq|INSERT INTO makemodel (parts_id, make, model)
485 '$form->{"make_$i"}', '$form->{"model_$i"}')|;
486 $dbh->do($query) || $form->dberror($query);
492 foreach $item (split / /, $form->{taxaccounts}) {
493 if ($form->{"IC_tax_$item"}) {
494 $query = qq|INSERT INTO partstax (parts_id, chart_id)
498 WHERE c.accno = '$item'))|;
499 $dbh->do($query) || $form->dberror($query);
503 # add assembly records
504 if ($form->{item} eq 'assembly') {
506 for my $i (1 .. $form->{assembly_rows}) {
507 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
509 if ($form->{"qty_$i"} != 0) {
510 $form->{"bom_$i"} *= 1;
511 $query = qq|INSERT INTO assembly (id, parts_id, qty, bom)
512 VALUES ($form->{id}, $form->{"id_$i"},
513 $form->{"qty_$i"}, '$form->{"bom_$i"}')|;
514 $dbh->do($query) || $form->dberror($query);
518 # adjust onhand for the parts
519 if ($form->{onhand} != 0) {
520 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
526 my $shippingdate = "$a[5]-$a[4]-$a[3]";
528 $form->get_employee($dbh);
530 # add inventory record
531 $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
532 shippingdate, employee_id) VALUES (
533 0, $form->{id}, $form->{stock}, '$shippingdate',
534 $form->{employee_id})|;
535 $dbh->do($query) || $form->dberror($query);
539 #set expense_accno=inventory_accno if they are different => bilanz
541 ($form->{expense_accno} != $form->{inventory_accno})
542 ? $form->{inventory_accno}
543 : $form->{expense_accno};
545 # get tax rates and description
547 ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
548 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
550 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
552 $stw = $dbh->prepare($query);
554 $stw->execute || $form->dberror($query);
556 $form->{taxaccount} = "";
557 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
559 # if ($customertax{$ref->{accno}}) {
560 $form->{taxaccount} .= "$ptr->{accno} ";
561 if (!($form->{taxaccount2} =~ /$ptr->{accno}/)) {
562 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
563 $form->{"$ptr->{accno}_description"} = $ptr->{description};
564 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
565 $form->{taxaccount2} .= " $ptr->{accno} ";
571 my $rc = $dbh->commit;
574 $main::lxdebug->leave_sub();
579 sub update_assembly {
580 $main::lxdebug->enter_sub();
582 my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
584 my $query = qq|SELECT a.id, a.qty
586 WHERE a.parts_id = $id|;
587 my $sth = $dbh->prepare($query);
588 $sth->execute || $form->dberror($query);
590 while (my ($pid, $aqty) = $sth->fetchrow_array) {
591 &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
595 $query = qq|UPDATE parts
596 SET sellprice = sellprice +
597 $qty * ($form->{sellprice} - $sellprice),
599 $qty * ($form->{weight} - $weight)
601 $dbh->do($query) || $form->dberror($query);
603 $main::lxdebug->leave_sub();
606 sub retrieve_assemblies {
607 $main::lxdebug->enter_sub();
609 my ($self, $myconfig, $form) = @_;
611 # connect to database
612 my $dbh = $form->dbconnect($myconfig);
616 if ($form->{partnumber}) {
617 my $partnumber = $form->like(lc $form->{partnumber});
618 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
621 if ($form->{description}) {
622 my $description = $form->like(lc $form->{description});
623 $where .= " AND lower(p.description) LIKE '$description'";
625 $where .= " AND NOT p.obsolete = '1'";
627 # retrieve assembly items
628 my $query = qq|SELECT p.id, p.partnumber, p.description,
629 p.bin, p.onhand, p.rop,
630 (SELECT sum(p2.inventory_accno_id)
631 FROM parts p2, assembly a
632 WHERE p2.id = a.parts_id
633 AND a.id = p.id) AS inventory
638 my $sth = $dbh->prepare($query);
639 $sth->execute || $form->dberror($query);
641 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
642 push @{ $form->{assembly_items} }, $ref if $ref->{inventory};
648 $main::lxdebug->leave_sub();
651 sub restock_assemblies {
652 $main::lxdebug->enter_sub();
654 my ($self, $myconfig, $form) = @_;
656 # connect to database
657 my $dbh = $form->dbconnect_noauto($myconfig);
659 for my $i (1 .. $form->{rowcount}) {
661 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
663 if ($form->{"qty_$i"} != 0) {
664 &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
669 my $rc = $dbh->commit;
672 $main::lxdebug->leave_sub();
677 sub adjust_inventory {
678 $main::lxdebug->enter_sub();
680 my ($dbh, $form, $id, $qty) = @_;
682 my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
683 FROM parts p, assembly a
684 WHERE a.parts_id = p.id
686 my $sth = $dbh->prepare($query);
687 $sth->execute || $form->dberror($query);
689 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
691 my $allocate = $qty * $ref->{qty};
693 # is it a service item, then loop
694 $ref->{inventory_accno_id} *= 1;
695 next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
697 # adjust parts onhand
698 $form->update_balance($dbh, "parts", "onhand",
706 my $rc = $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $qty);
708 $main::lxdebug->leave_sub();
714 $main::lxdebug->enter_sub();
716 my ($self, $myconfig, $form) = @_;
718 # connect to database, turn off AutoCommit
719 my $dbh = $form->dbconnect_noauto($myconfig);
721 my $query = qq|DELETE FROM parts
722 WHERE id = $form->{id}|;
723 $dbh->do($query) || $form->dberror($query);
725 $query = qq|DELETE FROM partstax
726 WHERE parts_id = $form->{id}|;
727 $dbh->do($query) || $form->dberror($query);
729 # check if it is a part, assembly or service
730 if ($form->{item} ne 'service') {
731 $query = qq|DELETE FROM makemodel
732 WHERE parts_id = $form->{id}|;
733 $dbh->do($query) || $form->dberror($query);
736 if ($form->{item} eq 'assembly') {
739 $query = qq|DELETE FROM inventory
740 WHERE parts_id = $form->{id}|;
741 $dbh->do($query) || $form->dberror($query);
743 $query = qq|DELETE FROM assembly
744 WHERE id = $form->{id}|;
745 $dbh->do($query) || $form->dberror($query);
748 if ($form->{item} eq 'alternate') {
749 $query = qq|DELETE FROM alternate
750 WHERE id = $form->{id}|;
751 $dbh->do($query) || $form->dberror($query);
755 my $rc = $dbh->commit;
758 $main::lxdebug->leave_sub();
764 $main::lxdebug->enter_sub();
766 my ($self, $myconfig, $form) = @_;
768 my $i = $form->{assembly_rows};
772 if ($form->{"partnumber_$i"}) {
773 $var = $form->like(lc $form->{"partnumber_$i"});
774 $where .= " AND lower(p.partnumber) LIKE '$var'";
776 if ($form->{"description_$i"}) {
777 $var = $form->like(lc $form->{"description_$i"});
778 $where .= " AND lower(p.description) LIKE '$var'";
780 if ($form->{"partsgroup_$i"}) {
781 $var = $form->like(lc $form->{"partsgroup_$i"});
782 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
786 $where .= " AND NOT p.id = $form->{id}";
790 $where .= " ORDER BY p.partnumber";
792 $where .= " ORDER BY p.description";
795 # connect to database
796 my $dbh = $form->dbconnect($myconfig);
798 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
799 p.weight, p.onhand, p.unit,
802 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
804 my $sth = $dbh->prepare($query);
805 $sth->execute || $form->dberror($query);
807 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
808 push @{ $form->{item_list} }, $ref;
814 $main::lxdebug->leave_sub();
818 $main::lxdebug->enter_sub();
820 my ($self, $myconfig, $form) = @_;
828 foreach my $item (qw(partnumber drawing microfiche make)) {
829 if ($form->{$item}) {
830 $var = $form->like(lc $form->{$item});
832 # make will build later Bugfix 145
833 # model will build later too - Bugfix 331
834 if ($item ne 'make') {
835 $where .= " AND lower(p.$item) LIKE '$var'";
840 # special case for description
841 if ($form->{description}) {
842 unless ( $form->{bought}
847 || $form->{quoted}) {
848 $var = $form->like(lc $form->{description});
849 $where .= " AND lower(p.description) LIKE '$var'";
853 # special case for serialnumber
854 if ($form->{l_serialnumber}) {
855 if ($form->{serialnumber}) {
856 $var = $form->like(lc $form->{serialnumber});
857 $where .= " AND lower(serialnumber) LIKE '$var'";
861 if ($form->{searchitems} eq 'part') {
862 $where .= " AND p.inventory_accno_id > 0";
864 if ($form->{searchitems} eq 'assembly') {
865 $form->{bought} = "";
866 $where .= " AND p.assembly = '1'";
868 if ($form->{searchitems} eq 'service') {
869 $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
871 # irrelevant for services
872 $form->{make} = $form->{model} = "";
875 # items which were never bought, sold or on an order
876 if ($form->{itemstatus} eq 'orphaned') {
877 $form->{onhand} = $form->{short} = 0;
878 $form->{bought} = $form->{sold} = 0;
879 $form->{onorder} = $form->{ordered} = 0;
880 $form->{rfq} = $form->{quoted} = 0;
882 $form->{transdatefrom} = $form->{transdateto} = "";
884 $where .= " AND p.onhand = 0
885 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
886 WHERE p.id = i.parts_id)
887 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
888 WHERE p.id = a.parts_id)
889 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
890 WHERE p.id = o.parts_id)";
893 if ($form->{itemstatus} eq 'active') {
894 $where .= " AND p.obsolete = '0'";
896 if ($form->{itemstatus} eq 'obsolete') {
897 $where .= " AND p.obsolete = '1'";
898 $form->{onhand} = $form->{short} = 0;
900 if ($form->{itemstatus} eq 'onhand') {
901 $where .= " AND p.onhand > 0";
903 if ($form->{itemstatus} eq 'short') {
904 $where .= " AND p.onhand < p.rop";
907 $var = $form->like(lc $form->{make});
908 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
909 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
911 if ($form->{model}) {
912 $var = $form->like(lc $form->{model});
913 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
914 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
916 if ($form->{partsgroup}) {
917 $var = $form->like(lc $form->{partsgroup});
918 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
920 if ($form->{l_soldtotal}) {
921 $where .= " AND p.id=i.parts_id AND i.qty >= 0";
923 " GROUP BY p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
925 if ($form->{top100}) {
926 $limit = " LIMIT 100";
930 if ($form->{revers} == 1) {
931 $form->{desc} = " DESC";
936 # connect to database
937 my $dbh = $form->dbconnect($myconfig);
939 my $sortorder = $form->{sort};
940 $sortorder .= $form->{desc};
941 $sortorder = $form->{sort} if $form->{sort};
945 if ($form->{l_soldtotal}) {
946 $form->{soldtotal} = 'soldtotal';
948 qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
949 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
950 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
956 $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
957 p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
958 p.priceupdate, p.image, p.drawing, p.microfiche,
961 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
964 ORDER BY $sortorder|;
967 # rebuild query for bought and sold items
973 || $form->{quoted}) {
975 my @a = qw(partnumber description bin priceupdate name);
977 push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
978 push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
979 push @a, "quonumber" if ($form->{rfq} || $form->{quoted});
984 if ($form->{bought} || $form->{sold}) {
986 my $invwhere = "$where";
987 $invwhere .= " AND i.assemblyitem = '0'";
988 $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'"
989 if $form->{transdatefrom};
990 $invwhere .= " AND a.transdate <= '$form->{transdateto}'"
991 if $form->{transdateto};
993 if ($form->{description}) {
994 $var = $form->like(lc $form->{description});
995 $invwhere .= " AND lower(i.description) LIKE '$var'";
998 my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
999 i.qty AS onhand, i.unit, p.bin, i.sellprice,
1000 p.listprice, p.lastcost, p.rop, p.weight,
1001 p.priceupdate, p.image, p.drawing, p.microfiche,
1003 a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
1004 ct.name, i.deliverydate|;
1006 if ($form->{bought}) {
1008 SELECT $flds, 'ir' AS module, '' AS type,
1011 JOIN parts p ON (p.id = i.parts_id)
1012 JOIN ap a ON (a.id = i.trans_id)
1013 JOIN vendor ct ON (a.vendor_id = ct.id)
1014 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1020 if ($form->{sold}) {
1022 SELECT $flds, 'is' AS module, '' AS type,
1025 JOIN parts p ON (p.id = i.parts_id)
1026 JOIN ar a ON (a.id = i.trans_id)
1027 JOIN customer ct ON (a.customer_id = ct.id)
1028 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1035 if ($form->{onorder} || $form->{ordered}) {
1036 my $ordwhere = "$where
1037 AND o.quotation = '0'";
1038 $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1039 if $form->{transdatefrom};
1040 $ordwhere .= " AND o.transdate <= '$form->{transdateto}'"
1041 if $form->{transdateto};
1043 if ($form->{description}) {
1044 $var = $form->like(lc $form->{description});
1045 $ordwhere .= " AND lower(oi.description) LIKE '$var'";
1049 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1050 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1051 p.listprice, p.lastcost, p.rop, p.weight,
1052 p.priceupdate, p.image, p.drawing, p.microfiche,
1054 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1057 if ($form->{ordered}) {
1059 SELECT $flds, 'oe' AS module, 'sales_order' AS type,
1060 (SELECT buy FROM exchangerate ex
1061 WHERE ex.curr = o.curr
1062 AND ex.transdate = o.transdate) AS exchangerate
1064 JOIN parts p ON (oi.parts_id = p.id)
1065 JOIN oe o ON (oi.trans_id = o.id)
1066 JOIN customer ct ON (o.customer_id = ct.id)
1067 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1069 AND o.customer_id > 0|;
1074 if ($form->{onorder}) {
1076 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1077 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1078 p.listprice, p.lastcost, p.rop, p.weight,
1079 p.priceupdate, p.image, p.drawing, p.microfiche,
1081 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1085 SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
1086 (SELECT sell FROM exchangerate ex
1087 WHERE ex.curr = o.curr
1088 AND ex.transdate = o.transdate) AS exchangerate
1090 JOIN parts p ON (oi.parts_id = p.id)
1091 JOIN oe o ON (oi.trans_id = o.id)
1092 JOIN vendor ct ON (o.vendor_id = ct.id)
1093 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1095 AND o.vendor_id > 0|;
1100 if ($form->{rfq} || $form->{quoted}) {
1101 my $quowhere = "$where
1102 AND o.quotation = '1'";
1103 $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'"
1104 if $form->{transdatefrom};
1105 $quowhere .= " AND o.transdate <= '$form->{transdateto}'"
1106 if $form->{transdateto};
1108 if ($form->{description}) {
1109 $var = $form->like(lc $form->{description});
1110 $quowhere .= " AND lower(oi.description) LIKE '$var'";
1114 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1115 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1116 p.listprice, p.lastcost, p.rop, p.weight,
1117 p.priceupdate, p.image, p.drawing, p.microfiche,
1119 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1122 if ($form->{quoted}) {
1124 SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
1125 (SELECT buy FROM exchangerate ex
1126 WHERE ex.curr = o.curr
1127 AND ex.transdate = o.transdate) AS exchangerate
1129 JOIN parts p ON (oi.parts_id = p.id)
1130 JOIN oe o ON (oi.trans_id = o.id)
1131 JOIN customer ct ON (o.customer_id = ct.id)
1132 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1134 AND o.customer_id > 0|;
1141 qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1142 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1143 p.listprice, p.lastcost, p.rop, p.weight,
1144 p.priceupdate, p.image, p.drawing, p.microfiche,
1146 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1150 SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
1151 (SELECT sell FROM exchangerate ex
1152 WHERE ex.curr = o.curr
1153 AND ex.transdate = o.transdate) AS exchangerate
1155 JOIN parts p ON (oi.parts_id = p.id)
1156 JOIN oe o ON (oi.trans_id = o.id)
1157 JOIN vendor ct ON (o.vendor_id = ct.id)
1158 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1160 AND o.vendor_id > 0|;
1165 ORDER BY $sortorder|;
1168 my $sth = $dbh->prepare($query);
1169 $sth->execute || $form->dberror($query);
1171 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1172 push @{ $form->{parts} }, $ref;
1177 # include individual items for assemblies
1178 if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1179 foreach $item (@{ $form->{parts} }) {
1180 push @assemblies, $item;
1181 $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1183 p.sellprice, p.listprice, p.lastcost,
1184 p.rop, p.weight, p.priceupdate,
1185 p.image, p.drawing, p.microfiche
1186 FROM parts p, assembly a
1187 WHERE p.id = a.parts_id
1188 AND a.id = $item->{id}|;
1190 $sth = $dbh->prepare($query);
1191 $sth->execute || $form->dberror($query);
1193 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1194 $ref->{assemblyitem} = 1;
1195 push @assemblies, $ref;
1199 push @assemblies, { id => $item->{id} };
1203 # copy assemblies to $form->{parts}
1204 @{ $form->{parts} } = @assemblies;
1208 $main::lxdebug->leave_sub();
1212 $main::lxdebug->enter_sub();
1214 my ($self, $module, $myconfig, $form) = @_;
1216 # connect to database
1217 my $dbh = $form->dbconnect($myconfig);
1220 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1221 p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1222 FROM chart c, parts p
1223 WHERE c.link LIKE '%$module%'
1224 AND p.id = $form->{id}
1227 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1228 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1229 FROM chart c, defaults d
1230 WHERE c.link LIKE '%$module%'
1234 my $sth = $dbh->prepare($query);
1235 $sth->execute || $form->dberror($query);
1236 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1237 foreach my $key (split /:/, $ref->{link}) {
1238 if ($key =~ /$module/) {
1239 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1240 || ($ref->{id} eq $ref->{income_accno_id})
1241 || ($ref->{id} eq $ref->{expense_accno_id})) {
1242 push @{ $form->{"${module}_links"}{$key} },
1243 { accno => $ref->{accno},
1244 description => $ref->{description},
1245 selected => "selected" };
1247 push @{ $form->{"${module}_links"}{$key} },
1248 { accno => $ref->{accno},
1249 description => $ref->{description},
1258 $query = qq|SELECT weightunit
1260 $sth = $dbh->prepare($query);
1261 $sth->execute || $form->dberror($query);
1263 ($form->{weightunit}) = $sth->fetchrow_array;
1267 $query = qq|SELECT weightunit, current_date
1269 $sth = $dbh->prepare($query);
1270 $sth->execute || $form->dberror($query);
1272 ($form->{weightunit}, $form->{priceupdate}) = $sth->fetchrow_array;
1277 $main::lxdebug->leave_sub();
1280 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1282 $main::lxdebug->enter_sub();
1284 my ($self, $myconfig, $form, $sortorder) = @_;
1285 my $dbh = $form->dbconnect($myconfig);
1286 my $order = " p.partnumber";
1287 my $where = "1 = 1";
1289 if ($sortorder eq "all") {
1290 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1291 $where .= " AND p.description LIKE '%$form->{description}%'";
1293 if ($sortorder eq "partnumber") {
1294 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1295 $order = qq|p.$sortorder|;
1297 if ($sortorder eq "description") {
1298 $where .= " AND p.description LIKE '%$form->{description}%'";
1303 qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
1304 my $sth = $dbh->prepare($query);
1305 $sth->execute || $self->dberror($query);
1307 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1308 if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1311 $form->{"id_$j"} = $ref->{id};
1312 $form->{"partnumber_$j"} = $ref->{partnumber};
1313 $form->{"description_$j"} = $ref->{description};
1314 $form->{"unit_$j"} = $ref->{unit};
1315 $form->{"sellprice_$j"} = $ref->{sellprice};
1316 $form->{"soldtotal_$j"} = get_soldtotal($dbh, $ref->{id});
1323 $main::lxdebug->leave_sub();
1328 # gets sum of sold part with part_id
1330 $main::lxdebug->enter_sub();
1332 my ($dbh, $id) = @_;
1335 qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
1337 my $sth = $dbh->prepare($query);
1338 $sth->execute || $form->dberror($query);
1341 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1343 $sum = $ref->{totalsold};
1347 if ($sum eq undef) {
1351 $main::lxdebug->leave_sub();
1354 } #end get_soldtotal
1357 $main::lxdebug->enter_sub();
1359 my ($self, $myconfig, $form) = @_;
1360 my $i = $form->{rowcount};
1361 my $where = "NOT p.obsolete = '1'";
1363 if ($form->{"partnumber_$i"}) {
1364 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1365 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1367 if ($form->{"description_$i"}) {
1368 my $description = $form->like(lc $form->{"description_$i"});
1369 $where .= " AND lower(p.description) LIKE '$description'";
1372 if ($form->{"partsgroup_$i"}) {
1373 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1374 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1377 if ($form->{"description_$i"}) {
1378 $where .= " ORDER BY description";
1380 $where .= " ORDER BY partnumber";
1383 # connect to database
1384 my $dbh = $form->dbconnect($myconfig);
1386 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1388 c1.accno AS inventory_accno,
1389 c2.accno AS income_accno,
1390 c3.accno AS expense_accno,
1391 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1394 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1395 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1396 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1397 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1399 my $sth = $dbh->prepare($query);
1400 $sth->execute || $form->dberror($query);
1402 #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1404 # get tax rates and description
1405 #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
1406 #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1407 # FROM chart c, tax t
1408 # WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1410 # $stw = $dbh->prepare($query);
1411 #$stw->execute || $form->dberror($query);
1413 #$ref->{taxaccounts} = "";
1414 #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1416 # $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1417 # $form->{"$ptr->{accno}_description"} = $ptr->{description};
1418 # $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1419 # $form->{taxaccounts} .= "$ptr->{accno} ";
1420 # $ref->{taxaccounts} .= "$ptr->{accno} ";
1425 #chop $ref->{taxaccounts};
1427 push @{ $form->{item_list} }, $ref;
1433 $main::lxdebug->leave_sub();