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',
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};
106 unless ($form->{item} eq 'service') {
108 if ($form->{makemodel}) {
109 $query = qq|SELECT m.make, m.model FROM makemodel m
110 WHERE m.parts_id = $form->{id}|;
112 $sth = $dbh->prepare($query);
113 $sth->execute || $form->dberror($query);
116 while (($form->{"make_$i"}, $form->{"model_$i"}) = $sth->fetchrow_array) {
120 $form->{makemodel_rows} = $i - 1;
125 # now get accno for taxes
126 $query = qq|SELECT c.accno
127 FROM chart c, partstax pt
128 WHERE pt.chart_id = c.id
129 AND pt.parts_id = $form->{id}|;
131 $sth = $dbh->prepare($query);
132 $sth->execute || $form->dberror($query);
134 while (($key) = $sth->fetchrow_array) {
135 $form->{amount}{$key} = $key;
141 $query = qq|SELECT i.parts_id
143 WHERE i.parts_id = $form->{id}
147 WHERE o.parts_id = $form->{id}
151 WHERE a.parts_id = $form->{id}|;
152 $sth = $dbh->prepare($query);
153 $sth->execute || $form->dberror($query);
155 ($form->{orphaned}) = $sth->fetchrow_array;
156 $form->{orphaned} = !$form->{orphaned};
161 $main::lxdebug->leave_sub();
166 $main::lxdebug->enter_sub();
169 my ($self, $myconfig, $form) = @_;
171 if ($form->{eur} && ($form->{item} ne 'service')) {
172 $form->{IC} = $form->{IC_expense};
175 ($form->{inventory_accno}) = split(/--/, $form->{IC});
176 ($form->{expense_accno}) = split(/--/, $form->{IC_expense});
177 ($form->{income_accno}) = split(/--/, $form->{IC_income});
179 # connect to database, turn off AutoCommit
180 my $dbh = $form->dbconnect_noauto($myconfig);
183 # make up a unique handle and store in partnumber field
184 # then retrieve the record based on the unique handle to get the id
185 # replace the partnumber field with the actual variable
186 # add records for makemodel
188 # if there is a $form->{id} then replace the old entry
189 # delete all makemodel entries and add the new ones
192 map { $form->{$_} =~ s/\'/\'\'/g } qw(partnumber description notes unit);
194 # undo amount formatting
195 map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) } qw(rop weight listprice sellprice gv lastcost stock);
197 # set date to NULL if nothing entered
198 $form->{priceupdate} = ($form->{priceupdate}) ? qq|'$form->{priceupdate}'| : "NULL";
200 $form->{makemodel} = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
202 $form->{alternate} = 0;
203 $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
204 $form->{obsolete} *= 1;
206 $form->{onhand} *= 1;
215 $query = qq|SELECT p.sellprice, p.weight
217 WHERE p.id = $form->{id}|;
218 $sth = $dbh->prepare($query);
219 $sth->execute || $form->dberror($query);
220 my ($sellprice, $weight) = $sth->fetchrow_array;
223 # if item is part of an assembly adjust all assemblies
224 $query = qq|SELECT a.id, a.qty
226 WHERE a.parts_id = $form->{id}|;
227 $sth = $dbh->prepare($query);
228 $sth->execute || $form->dberror($query);
229 while (my ($id, $qty) = $sth->fetchrow_array) {
230 &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
235 if ($form->{item} ne 'service') {
236 # delete makemodel records
237 $query = qq|DELETE FROM makemodel
238 WHERE parts_id = $form->{id}|;
239 $dbh->do($query) || $form->dberror($query);
242 if ($form->{item} eq 'assembly') {
243 if ($form->{onhand} != 0) {
244 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
247 # delete assembly records
248 $query = qq|DELETE FROM assembly
249 WHERE id = $form->{id}|;
250 $dbh->do($query) || $form->dberror($query);
252 $form->{onhand} += $form->{stock};
256 $query = qq|DELETE FROM partstax
257 WHERE parts_id = $form->{id}|;
258 $dbh->do($query) || $form->dberror($query);
262 $uid .= $form->{login};
264 $query = qq|SELECT p.id FROM parts p
265 WHERE p.partnumber = '$form->{partnumber}'|;
266 $sth = $dbh->prepare($query);
267 $sth->execute || $form->dberror($query);
268 ($form->{id}) = $sth->fetchrow_array;
271 if ($form->{id} ne "") {
272 $main::lxdebug->leave_sub();
275 $query = qq|INSERT INTO parts (partnumber)
277 $dbh->do($query) || $form->dberror($query);
279 $query = qq|SELECT p.id FROM parts p
280 WHERE p.partnumber = '$uid'|;
281 $sth = $dbh->prepare($query);
282 $sth->execute || $form->dberror($query);
284 ($form->{id}) = $sth->fetchrow_array;
287 $form->{orphaned} = 1;
288 $form->{onhand} = $form->{stock} if $form->{item} eq 'assembly';
289 if ($form->{partnumber} eq "" && $form->{inventory_accno} eq "") {
290 $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
292 if ($form->{partnumber} eq "" && $form->{inventory_accno} ne "") {
293 $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
297 my $partsgroup_id = 0;
299 if ($form->{partsgroup}) {
300 ($partsgroup, $partsgroup_id) = split /--/, $form->{partsgroup};
303 $query = qq|UPDATE parts SET
304 partnumber = '$form->{partnumber}',
305 description = '$form->{description}',
306 makemodel = '$form->{makemodel}',
307 alternate = '$form->{alternate}',
308 assembly = '$form->{assembly}',
309 listprice = $form->{listprice},
310 sellprice = $form->{sellprice},
311 lastcost = $form->{lastcost},
312 weight = $form->{weight},
313 priceupdate = $form->{priceupdate},
314 unit = '$form->{unit}',
315 notes = '$form->{notes}',
317 bin = '$form->{bin}',
318 inventory_accno_id = (SELECT c.id FROM chart c
319 WHERE c.accno = '$form->{inventory_accno}'),
320 income_accno_id = (SELECT c.id FROM chart c
321 WHERE c.accno = '$form->{income_accno}'),
322 expense_accno_id = (SELECT c.id FROM chart c
323 WHERE c.accno = '$form->{expense_accno}'),
324 obsolete = '$form->{obsolete}',
325 image = '$form->{image}',
326 drawing = '$form->{drawing}',
327 shop = '$form->{shop}',
330 microfiche = '$form->{microfiche}',
331 partsgroup_id = $partsgroup_id
332 WHERE id = $form->{id}|;
333 $dbh->do($query) || $form->dberror($query);
336 # insert makemodel records
337 unless ($form->{item} eq 'service') {
338 for my $i (1 .. $form->{makemodel_rows}) {
339 if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
340 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
342 $query = qq|INSERT INTO makemodel (parts_id, make, model)
344 '$form->{"make_$i"}', '$form->{"model_$i"}')|;
345 $dbh->do($query) || $form->dberror($query);
352 foreach $item (split / /, $form->{taxaccounts}) {
353 if ($form->{"IC_tax_$item"}) {
354 $query = qq|INSERT INTO partstax (parts_id, chart_id)
358 WHERE c.accno = '$item'))|;
359 $dbh->do($query) || $form->dberror($query);
363 # add assembly records
364 if ($form->{item} eq 'assembly') {
366 for my $i (1 .. $form->{assembly_rows}) {
367 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
369 if ($form->{"qty_$i"} != 0) {
370 $form->{"bom_$i"} *= 1;
371 $query = qq|INSERT INTO assembly (id, parts_id, qty, bom)
372 VALUES ($form->{id}, $form->{"id_$i"},
373 $form->{"qty_$i"}, '$form->{"bom_$i"}')|;
374 $dbh->do($query) || $form->dberror($query);
378 # adjust onhand for the parts
379 if ($form->{onhand} != 0) {
380 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
383 @a = localtime; $a[5] += 1900; $a[4]++;
384 my $shippingdate = "$a[5]-$a[4]-$a[3]";
386 $form->get_employee($dbh);
388 # add inventory record
389 $query = qq|INSERT INTO inventory (warehouse_id, parts_id, qty,
390 shippingdate, employee_id) VALUES (
391 0, $form->{id}, $form->{stock}, '$shippingdate',
392 $form->{employee_id})|;
393 $dbh->do($query) || $form->dberror($query);
396 #set expense_accno=inventory_accno if they are different => bilanz
397 $vendor_accno = ($form->{expense_accno}!=$form->{inventory_accno}) ? $form->{inventory_accno} :$form->{expense_accno};
400 # get tax rates and description
401 $accno_id = ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
402 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
404 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
406 $stw = $dbh->prepare($query);
408 $stw->execute || $form->dberror($query);
410 $form->{taxaccount} = "";
411 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
412 # if ($customertax{$ref->{accno}}) {
413 $form->{taxaccount} .= "$ptr->{accno} ";
414 if (!($form->{taxaccount2}=~/$ptr->{accno}/)) {
415 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
416 $form->{"$ptr->{accno}_description"} = $ptr->{description};
417 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
418 $form->{taxaccount2} .= " $ptr->{accno} ";
424 my $rc = $dbh->commit;
427 $main::lxdebug->leave_sub();
434 sub update_assembly {
435 $main::lxdebug->enter_sub();
437 my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
439 my $query = qq|SELECT a.id, a.qty
441 WHERE a.parts_id = $id|;
442 my $sth = $dbh->prepare($query);
443 $sth->execute || $form->dberror($query);
445 while (my ($pid, $aqty) = $sth->fetchrow_array) {
446 &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
450 $query = qq|UPDATE parts
451 SET sellprice = sellprice +
452 $qty * ($form->{sellprice} - $sellprice),
454 $qty * ($form->{weight} - $weight)
456 $dbh->do($query) || $form->dberror($query);
458 $main::lxdebug->leave_sub();
463 sub retrieve_assemblies {
464 $main::lxdebug->enter_sub();
466 my ($self, $myconfig, $form) = @_;
468 # connect to database
469 my $dbh = $form->dbconnect($myconfig);
473 if ($form->{partnumber}) {
474 my $partnumber = $form->like(lc $form->{partnumber});
475 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
478 if ($form->{description}) {
479 my $description = $form->like(lc $form->{description});
480 $where .= " AND lower(p.description) LIKE '$description'";
482 $where .= " AND NOT p.obsolete = '1'";
484 # retrieve assembly items
485 my $query = qq|SELECT p.id, p.partnumber, p.description,
486 p.bin, p.onhand, p.rop,
487 (SELECT sum(p2.inventory_accno_id)
488 FROM parts p2, assembly a
489 WHERE p2.id = a.parts_id
490 AND a.id = p.id) AS inventory
495 my $sth = $dbh->prepare($query);
496 $sth->execute || $form->dberror($query);
498 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
499 push @{ $form->{assembly_items} }, $ref if $ref->{inventory};
505 $main::lxdebug->leave_sub();
509 sub restock_assemblies {
510 $main::lxdebug->enter_sub();
512 my ($self, $myconfig, $form) = @_;
514 # connect to database
515 my $dbh = $form->dbconnect_noauto($myconfig);
517 for my $i (1 .. $form->{rowcount}) {
519 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
521 if ($form->{"qty_$i"} != 0) {
522 &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
527 my $rc = $dbh->commit;
530 $main::lxdebug->leave_sub();
536 sub adjust_inventory {
537 $main::lxdebug->enter_sub();
539 my ($dbh, $form, $id, $qty) = @_;
541 my $query = qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
542 FROM parts p, assembly a
543 WHERE a.parts_id = p.id
545 my $sth = $dbh->prepare($query);
546 $sth->execute || $form->dberror($query);
548 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
550 my $allocate = $qty * $ref->{qty};
552 # is it a service item, then loop
553 $ref->{inventory_accno_id} *= 1;
554 next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
556 # adjust parts onhand
557 $form->update_balance($dbh,
567 my $rc = $form->update_balance($dbh,
573 $main::lxdebug->leave_sub();
580 $main::lxdebug->enter_sub();
582 my ($self, $myconfig, $form) = @_;
584 # connect to database, turn off AutoCommit
585 my $dbh = $form->dbconnect_noauto($myconfig);
587 my $query = qq|DELETE FROM parts
588 WHERE id = $form->{id}|;
589 $dbh->do($query) || $form->dberror($query);
591 $query = qq|DELETE FROM partstax
592 WHERE parts_id = $form->{id}|;
593 $dbh->do($query) || $form->dberror($query);
595 # check if it is a part, assembly or service
596 if ($form->{item} ne 'service') {
597 $query = qq|DELETE FROM makemodel
598 WHERE parts_id = $form->{id}|;
599 $dbh->do($query) || $form->dberror($query);
602 if ($form->{item} eq 'assembly') {
604 $query = qq|DELETE FROM inventory
605 WHERE parts_id = $form->{id}|;
606 $dbh->do($query) || $form->dberror($query);
608 $query = qq|DELETE FROM assembly
609 WHERE id = $form->{id}|;
610 $dbh->do($query) || $form->dberror($query);
613 if ($form->{item} eq 'alternate') {
614 $query = qq|DELETE FROM alternate
615 WHERE id = $form->{id}|;
616 $dbh->do($query) || $form->dberror($query);
620 my $rc = $dbh->commit;
623 $main::lxdebug->leave_sub();
630 $main::lxdebug->enter_sub();
632 my ($self, $myconfig, $form) = @_;
634 my $i = $form->{assembly_rows};
638 if ($form->{"partnumber_$i"}) {
639 $var = $form->like(lc $form->{"partnumber_$i"});
640 $where .= " AND lower(p.partnumber) LIKE '$var'";
642 if ($form->{"description_$i"}) {
643 $var = $form->like(lc $form->{"description_$i"});
644 $where .= " AND lower(p.description) LIKE '$var'";
646 if ($form->{"partsgroup_$i"}) {
647 $var = $form->like(lc $form->{"partsgroup_$i"});
648 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
652 $where .= " AND NOT p.id = $form->{id}";
656 $where .= " ORDER BY p.partnumber";
658 $where .= " ORDER BY p.description";
661 # connect to database
662 my $dbh = $form->dbconnect($myconfig);
664 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
665 p.weight, p.onhand, p.unit,
668 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
670 my $sth = $dbh->prepare($query);
671 $sth->execute || $form->dberror($query);
673 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
674 push @{ $form->{item_list} }, $ref;
680 $main::lxdebug->leave_sub();
685 $main::lxdebug->enter_sub();
687 my ($self, $myconfig, $form) = @_;
695 foreach my $item (qw(partnumber drawing microfiche make model)) {
696 if ($form->{$item}) {
697 $var = $form->like(lc $form->{$item});
698 # make will build later Bugfix 145
699 if ($item ne 'make'){
700 $where .= " AND lower(p.$item) LIKE '$var'";
704 # special case for description
705 if ($form->{description}) {
706 unless ($form->{bought} || $form->{sold} || $form->{onorder} || $form->{ordered} || $form->{rfq} || $form->{quoted}) {
707 $var = $form->like(lc $form->{description});
708 $where .= " AND lower(p.description) LIKE '$var'";
711 # special case for serialnumber
712 if ($form->{l_serialnumber}) {
713 if ($form->{serialnumber}) {
714 $var = $form->like(lc $form->{serialnumber});
715 $where .= " AND lower(serialnumber) LIKE '$var'";
719 if ($form->{searchitems} eq 'part') {
720 $where .= " AND p.inventory_accno_id > 0";
722 if ($form->{searchitems} eq 'assembly') {
723 $form->{bought} = "";
724 $where .= " AND p.assembly = '1'";
726 if ($form->{searchitems} eq 'service') {
727 $where .= " AND p.inventory_accno_id IS NULL AND NOT p.assembly = '1'";
728 # irrelevant for services
729 $form->{make} = $form->{model} = "";
732 # items which were never bought, sold or on an order
733 if ($form->{itemstatus} eq 'orphaned') {
734 $form->{onhand} = $form->{short} = 0;
735 $form->{bought} = $form->{sold} = 0;
736 $form->{onorder} = $form->{ordered} = 0;
737 $form->{rfq} = $form->{quoted} = 0;
739 $form->{transdatefrom} = $form->{transdateto} = "";
741 $where .= " AND p.onhand = 0
742 AND p.id NOT IN (SELECT p.id FROM parts p, invoice i
743 WHERE p.id = i.parts_id)
744 AND p.id NOT IN (SELECT p.id FROM parts p, assembly a
745 WHERE p.id = a.parts_id)
746 AND p.id NOT IN (SELECT p.id FROM parts p, orderitems o
747 WHERE p.id = o.parts_id)";
750 if ($form->{itemstatus} eq 'active') {
751 $where .= " AND p.obsolete = '0'";
753 if ($form->{itemstatus} eq 'obsolete') {
754 $where .= " AND p.obsolete = '1'";
755 $form->{onhand} = $form->{short} = 0;
757 if ($form->{itemstatus} eq 'onhand') {
758 $where .= " AND p.onhand > 0";
760 if ($form->{itemstatus} eq 'short') {
761 $where .= " AND p.onhand < p.rop";
764 $var = $form->like(lc $form->{make});
765 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
766 FROM makemodel m WHERE lower(m.make) LIKE '$var')";
768 if ($form->{model}) {
769 $var = $form->like(lc $form->{model});
770 $where .= " AND p.id IN (SELECT DISTINCT ON (m.parts_id) m.parts_id
771 FROM makemodel m WHERE lower(m.model) LIKE '$var')";
773 if ($form->{partsgroup}) {
774 $var = $form->like(lc $form->{partsgroup});
775 $where .= " AND lower(pg.partsgroup) LIKE '$var'";
777 if ($form->{l_soldtotal}) {
778 $where .= " AND p.id=i.parts_id AND i.qty >= 0";
779 $group = " GROUP BY p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin, p.sellprice,p.listprice,p.lastcost,p.priceupdate,pg.partsgroup";
783 $limit = " LIMIT 100";
786 if ($form->{revers}==1)
788 $form->{desc} = " DESC";
795 # connect to database
796 my $dbh = $form->dbconnect($myconfig);
798 my $sortorder = $form->{sort};
799 $sortorder .= $form->{desc};
800 $sortorder = $form->{sort} unless $sortorder;
804 if ($form->{l_soldtotal})
806 $form->{soldtotal}='soldtotal';
807 $query = qq|SELECT p.id,p.partnumber,p.description,p.onhand,p.unit,p.bin,p.sellprice,p.listprice,
808 p.lastcost,p.priceupdate,pg.partsgroup,sum(i.qty) as soldtotal FROM parts
809 p LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
817 $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
818 p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
819 p.priceupdate, p.image, p.drawing, p.microfiche,
822 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
825 ORDER BY $sortorder|;
828 # rebuild query for bought and sold items
829 if ($form->{bought} || $form->{sold} || $form->{onorder} || $form->{ordered} || $form->{rfq} || $form->{quoted}) {
831 my @a = qw(partnumber description bin priceupdate name);
833 push @a, qw(invnumber serialnumber) if ($form->{bought} || $form->{sold});
834 push @a, "ordnumber" if ($form->{onorder} || $form->{ordered});
835 push @a, "quonumber" if ($form->{rfq} || $form->{quoted});
840 if ($form->{bought} || $form->{sold}) {
842 my $invwhere = "$where";
843 $invwhere .= " AND i.assemblyitem = '0'";
844 $invwhere .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
845 $invwhere .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
847 if ($form->{description}) {
848 $var = $form->like(lc $form->{description});
849 $invwhere .= " AND lower(i.description) LIKE '$var'";
852 my $flds = qq|p.id, p.partnumber, i.description, i.serialnumber,
853 i.qty AS onhand, i.unit, p.bin, i.sellprice,
854 p.listprice, p.lastcost, p.rop, p.weight,
855 p.priceupdate, p.image, p.drawing, p.microfiche,
857 a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
860 if ($form->{bought}) {
862 SELECT $flds, 'ir' AS module, '' AS type,
865 JOIN parts p ON (p.id = i.parts_id)
866 JOIN ap a ON (a.id = i.trans_id)
867 JOIN vendor ct ON (a.vendor_id = ct.id)
868 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
876 SELECT $flds, 'is' AS module, '' AS type,
879 JOIN parts p ON (p.id = i.parts_id)
880 JOIN ar a ON (a.id = i.trans_id)
881 JOIN customer ct ON (a.customer_id = ct.id)
882 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
889 if ($form->{onorder} || $form->{ordered}) {
890 my $ordwhere = "$where
891 AND o.quotation = '0'";
892 $ordwhere .= " AND o.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
893 $ordwhere .= " AND o.transdate <= '$form->{transdateto}'" if $form->{transdateto};
895 if ($form->{description}) {
896 $var = $form->like(lc $form->{description});
897 $ordwhere .= " AND lower(oi.description) LIKE '$var'";
900 $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
901 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
902 p.listprice, p.lastcost, p.rop, p.weight,
903 p.priceupdate, p.image, p.drawing, p.microfiche,
905 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
908 if ($form->{ordered}) {
910 SELECT $flds, 'oe' AS module, 'sales_order' AS type,
911 (SELECT buy FROM exchangerate ex
912 WHERE ex.curr = o.curr
913 AND ex.transdate = o.transdate) AS exchangerate
915 JOIN parts p ON (oi.parts_id = p.id)
916 JOIN oe o ON (oi.trans_id = o.id)
917 JOIN customer ct ON (o.customer_id = ct.id)
918 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
920 AND o.customer_id > 0|;
925 if ($form->{onorder}) {
926 $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
927 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
928 p.listprice, p.lastcost, p.rop, p.weight,
929 p.priceupdate, p.image, p.drawing, p.microfiche,
931 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
935 SELECT $flds, 'oe' AS module, 'purchase_order' AS type,
936 (SELECT sell FROM exchangerate ex
937 WHERE ex.curr = o.curr
938 AND ex.transdate = o.transdate) AS exchangerate
940 JOIN parts p ON (oi.parts_id = p.id)
941 JOIN oe o ON (oi.trans_id = o.id)
942 JOIN vendor ct ON (o.vendor_id = ct.id)
943 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
945 AND o.vendor_id > 0|;
950 if ($form->{rfq} || $form->{quoted}) {
951 my $quowhere = "$where
952 AND o.quotation = '1'";
953 $quowhere .= " AND o.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
954 $quowhere .= " AND o.transdate <= '$form->{transdateto}'" if $form->{transdateto};
956 if ($form->{description}) {
957 $var = $form->like(lc $form->{description});
958 $quowhere .= " AND lower(oi.description) LIKE '$var'";
961 $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
962 oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
963 p.listprice, p.lastcost, p.rop, p.weight,
964 p.priceupdate, p.image, p.drawing, p.microfiche,
966 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
969 if ($form->{quoted}) {
971 SELECT $flds, 'oe' AS module, 'sales_quotation' AS type,
972 (SELECT buy FROM exchangerate ex
973 WHERE ex.curr = o.curr
974 AND ex.transdate = o.transdate) AS exchangerate
976 JOIN parts p ON (oi.parts_id = p.id)
977 JOIN oe o ON (oi.trans_id = o.id)
978 JOIN customer ct ON (o.customer_id = ct.id)
979 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
981 AND o.customer_id > 0|;
987 $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
988 oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
989 p.listprice, p.lastcost, p.rop, p.weight,
990 p.priceupdate, p.image, p.drawing, p.microfiche,
992 '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
996 SELECT $flds, 'oe' AS module, 'request_quotation' AS type,
997 (SELECT sell FROM exchangerate ex
998 WHERE ex.curr = o.curr
999 AND ex.transdate = o.transdate) AS exchangerate
1001 JOIN parts p ON (oi.parts_id = p.id)
1002 JOIN oe o ON (oi.trans_id = o.id)
1003 JOIN vendor ct ON (o.vendor_id = ct.id)
1004 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1006 AND o.vendor_id > 0|;
1011 ORDER BY $sortorder|;
1014 my $sth = $dbh->prepare($query);
1015 $sth->execute || $form->dberror($query);
1017 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1018 push @{ $form->{parts} }, $ref;
1024 # include individual items for assemblies
1025 if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1026 foreach $item (@{ $form->{parts} }) {
1027 push @assemblies, $item;
1028 $query = qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1030 p.sellprice, p.listprice, p.lastcost,
1031 p.rop, p.weight, p.priceupdate,
1032 p.image, p.drawing, p.microfiche
1033 FROM parts p, assembly a
1034 WHERE p.id = a.parts_id
1035 AND a.id = $item->{id}|;
1037 $sth = $dbh->prepare($query);
1038 $sth->execute || $form->dberror($query);
1040 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1041 $ref->{assemblyitem} = 1;
1042 push @assemblies, $ref;
1046 push @assemblies, {id => $item->{id}};
1050 # copy assemblies to $form->{parts}
1051 @{ $form->{parts} } = @assemblies;
1055 $main::lxdebug->leave_sub();
1060 $main::lxdebug->enter_sub();
1062 my ($self, $module, $myconfig, $form) = @_;
1063 # connect to database
1064 my $dbh = $form->dbconnect($myconfig);
1067 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1068 p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1069 FROM chart c, parts p
1070 WHERE c.link LIKE '%$module%'
1071 AND p.id = $form->{id}
1074 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1075 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1076 FROM chart c, defaults d
1077 WHERE c.link LIKE '%$module%'
1081 my $sth = $dbh->prepare($query);
1082 $sth->execute || $form->dberror($query);
1083 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1084 foreach my $key (split /:/, $ref->{link}) {
1085 if ($key =~ /$module/) {
1086 if (($ref->{id} eq $ref->{inventory_accno_id}) || ($ref->{id} eq $ref->{income_accno_id}) || ($ref->{id} eq $ref->{expense_accno_id})) {
1087 push @{ $form->{"${module}_links"}{$key} }, { accno => $ref->{accno},
1088 description => $ref->{description}, selected =>"selected" };
1090 push @{ $form->{"${module}_links"}{$key} }, { accno => $ref->{accno},
1091 description => $ref->{description}, selected =>"" };
1099 $query = qq|SELECT weightunit
1101 $sth = $dbh->prepare($query);
1102 $sth->execute || $form->dberror($query);
1104 ($form->{weightunit}) = $sth->fetchrow_array;
1108 $query = qq|SELECT weightunit, current_date
1110 $sth = $dbh->prepare($query);
1111 $sth->execute || $form->dberror($query);
1113 ($form->{weightunit}, $form->{priceupdate}) = $sth->fetchrow_array;
1118 $main::lxdebug->leave_sub();
1121 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1123 $main::lxdebug->enter_sub();
1125 my ($self, $myconfig, $form, $sortorder) = @_;
1126 my $dbh = $form->dbconnect($myconfig);
1127 my $order = " p.partnumber";
1128 my $where = "1 = 1";
1130 if ($sortorder eq "all") {
1131 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1132 $where .= " AND p.description LIKE '%$form->{description}%'";
1135 if ($sortorder eq "partnumber") {
1136 $where .= " AND p.partnumber LIKE '%$form->{partnumber}%'";
1137 $order = qq|p.$sortorder|;
1139 if ($sortorder eq "description") {
1140 $where .= " AND p.description LIKE '%$form->{description}%'";
1144 my $query = qq|SELECT p.id, p.partnumber, p.description, p.unit, p.sellprice FROM parts p WHERE $where ORDER BY $order|;
1145 my $sth = $dbh->prepare($query);
1146 $sth->execute || $self->dberror($query);
1148 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1149 if (($ref->{partnumber}eq "*")&&($ref->{description}eq ""))
1155 $form->{"id_$j"}= $ref->{id};
1156 $form->{"partnumber_$j"} = $ref->{partnumber};
1157 $form->{"description_$j"} = $ref->{description};
1158 $form->{"unit_$j"} = $ref->{unit};
1159 $form->{"sellprice_$j"} = $ref->{sellprice};
1160 $form->{"soldtotal_$j"} = get_soldtotal($dbh, $ref->{id});
1167 $main::lxdebug->leave_sub();
1172 # gets sum of sold part with part_id
1174 $main::lxdebug->enter_sub();
1178 my $query = qq|SELECT sum(i.qty) as totalsold FROM invoice i WHERE i.parts_id = $id|;
1180 my $sth = $dbh->prepare($query);
1181 $sth->execute || $form->dberror($query);
1184 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1186 $sum = $ref->{totalsold};
1195 $main::lxdebug->leave_sub();
1202 $main::lxdebug->enter_sub();
1204 my ($self, $myconfig, $form) = @_;
1205 my $i = $form->{rowcount};
1206 my $where = "NOT p.obsolete = '1'";
1208 if ($form->{"partnumber_$i"}) {
1209 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1210 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1212 if ($form->{"description_$i"}) {
1213 my $description = $form->like(lc $form->{"description_$i"});
1214 $where .= " AND lower(p.description) LIKE '$description'";
1217 if ($form->{"partsgroup_$i"}) {
1218 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1219 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1222 if ($form->{"description_$i"}) {
1223 $where .= " ORDER BY description";
1225 $where .= " ORDER BY partnumber";
1228 # connect to database
1229 my $dbh = $form->dbconnect($myconfig);
1231 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1233 c1.accno AS inventory_accno,
1234 c2.accno AS income_accno,
1235 c3.accno AS expense_accno,
1236 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1239 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1240 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1241 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1242 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1244 my $sth = $dbh->prepare($query);
1245 $sth->execute || $form->dberror($query);
1247 #while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1249 # get tax rates and description
1250 #$accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{inventory_accno};
1251 #$query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1252 # FROM chart c, tax t
1253 # WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1255 # $stw = $dbh->prepare($query);
1256 #$stw->execute || $form->dberror($query);
1258 #$ref->{taxaccounts} = "";
1259 #while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1261 # $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1262 # $form->{"$ptr->{accno}_description"} = $ptr->{description};
1263 # $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1264 # $form->{taxaccounts} .= "$ptr->{accno} ";
1265 # $ref->{taxaccounts} .= "$ptr->{accno} ";
1270 #chop $ref->{taxaccounts};
1272 push @{ $form->{item_list} }, $ref;
1278 $main::lxdebug->leave_sub();