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 #======================================================================
40 $main::lxdebug->enter_sub();
42 my ($self, $myconfig, $form) = @_;
45 my $dbh = $form->dbconnect($myconfig);
51 c1.accno AS inventory_accno,
52 c2.accno AS income_accno,
53 c3.accno AS expense_accno,
56 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
57 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
58 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
59 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
61 my $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
63 # copy to $form variables
64 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
76 qq|SELECT p.id, p.partnumber, p.description,
77 p.sellprice, p.weight, a.qty, a.bom, p.unit,
80 JOIN assembly a ON (a.parts_id = p.id)
81 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
83 ORDER BY $oid{$myconfig->{dbdriver}}|;
84 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
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};
104 my @pricegroups = ();
105 my @pricegroups_not_used = ();
109 qq|SELECT p.parts_id, p.pricegroup_id, p.price,
110 (SELECT pg.pricegroup
112 WHERE pg.id = p.pricegroup_id) AS pricegroup
115 ORDER BY pricegroup|;
116 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
120 while (($form->{"klass_$i"}, $form->{"pricegroup_id_$i"},
121 $form->{"price_$i"}, $form->{"pricegroup_$i"})
122 = $sth->fetchrow_array()) {
123 $form->{"price_$i"} = $form->round_amount($form->{"price_$i"}, 5);
124 $form->{"price_$i"} = $form->format_amount($myconfig, $form->{"price_$i"}, -2);
125 push @pricegroups, $form->{"pricegroup_id_$i"};
132 $query = qq|SELECT id, pricegroup FROM pricegroup|;
133 $form->{PRICEGROUPS} = selectall_hashref_query($form, $dbh, $query);
135 #find not used pricegroups
136 while ($tmp = pop(@{ $form->{PRICEGROUPS} })) {
138 foreach my $item (@pricegroups) {
139 if ($item eq $tmp->{id}) {
144 push(@pricegroups_not_used, $tmp) unless ($in_use);
147 # if not used pricegroups are avaible
148 if (@pricegroups_not_used) {
150 foreach $name (@pricegroups_not_used) {
151 $form->{"klass_$i"} = "$name->{id}";
152 $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
153 $form->{"price_$i"} = $form->format_amount($myconfig, $form->{"price_$i"}, -2);
154 $form->{"pricegroup_id_$i"} = "$name->{id}";
155 $form->{"pricegroup_$i"} = "$name->{pricegroup}";
161 $form->{price_rows} = $i - 1;
163 unless ($form->{item} eq 'service') {
166 if ($form->{makemodel}) {
167 $query = qq|SELECT m.make, m.model FROM makemodel m | .
168 qq|WHERE m.parts_id = ?|;
169 @values = ($form->{id});
170 $sth = $dbh->prepare($query);
171 $sth->execute(@values) || $form->dberror("$query (" . join(', ', @values) . ")");
174 while (($form->{"make_$i"}, $form->{"model_$i"}) = $sth->fetchrow_array)
179 $form->{makemodel_rows} = $i - 1;
185 $form->{language_values} = "";
186 $query = qq|SELECT language_id, translation FROM translation WHERE parts_id = ?|;
187 my $trq = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
188 while ($tr = $trq->fetchrow_hashref(NAME_lc)) {
189 $form->{language_values} .= "---+++---".$tr->{language_id}."--++--".$tr->{translation};
193 # now get accno for taxes
196 FROM chart c, partstax pt
197 WHERE (pt.chart_id = c.id) AND (pt.parts_id = ?)|;
198 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
199 while (($key) = $sth->fetchrow_array) {
200 $form->{amount}{$key} = $key;
209 WHERE (i.parts_id = ?)
215 WHERE (o.parts_id = ?)
221 WHERE (a.parts_id = ?)|;
222 @values = (conv_i($form->{id}), conv_i($form->{id}), conv_i($form->{id}));
223 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
224 $form->{orphaned} = !$form->{orphaned};
226 $form->{"unit_changeable"} = 1;
227 foreach my $table (qw(invoice assembly orderitems inventory license)) {
228 $query = qq|SELECT COUNT(*) FROM $table WHERE parts_id = ?|;
229 my ($count) = selectrow_query($form, $dbh, $query, conv_i($form->{"id"}));
232 $form->{"unit_changeable"} = 0;
239 $main::lxdebug->leave_sub();
242 sub get_pricegroups {
243 $main::lxdebug->enter_sub();
245 my ($self, $myconfig, $form) = @_;
247 my $dbh = $form->dbconnect($myconfig);
250 my $query = qq|SELECT id, pricegroup FROM pricegroup|;
251 my $pricegroups = selectall_hashref_query($form, $dbh, $query);
254 foreach $pg (@{ $pricegroups }) {
255 $form->{"klass_$i"} = "$pg->{id}";
256 $form->{"price_$i"} = $form->format_amount($myconfig, $form->{"price_$i"}, -2);
257 $form->{"pricegroup_id_$i"} = "$pg->{id}";
258 $form->{"pricegroup_$i"} = "$pg->{pricegroup}";
263 $form->{price_rows} = $i - 1;
267 $main::lxdebug->leave_sub();
270 sub retrieve_buchungsgruppen {
271 $main::lxdebug->enter_sub();
273 my ($self, $myconfig, $form) = @_;
277 my $dbh = $form->dbconnect($myconfig);
279 # get buchungsgruppen
280 $query = qq|SELECT id, description FROM buchungsgruppen ORDER BY sortkey|;
281 $form->{BUCHUNGSGRUPPEN} = selectall_hashref_query($form, $dbh, $query);
283 $main::lxdebug->leave_sub();
287 $main::lxdebug->enter_sub();
289 my ($self, $myconfig, $form) = @_;
291 # connect to database, turn off AutoCommit
292 my $dbh = $form->dbconnect_noauto($myconfig);
295 # make up a unique handle and store in partnumber field
296 # then retrieve the record based on the unique handle to get the id
297 # replace the partnumber field with the actual variable
298 # add records for makemodel
300 # if there is a $form->{id} then replace the old entry
301 # delete all makemodel entries and add the new ones
303 # undo amount formatting
304 map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) }
305 qw(rop weight listprice sellprice gv lastcost stock);
307 my $makemodel = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
309 $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
316 $query = qq|SELECT sellprice, weight FROM parts WHERE id = ?|;
317 my ($sellprice, $weight) = selectrow_query($form, $dbh, $query, conv_i($form->{id}));
319 # if item is part of an assembly adjust all assemblies
320 $query = qq|SELECT id, qty FROM assembly WHERE parts_id = ?|;
321 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
322 while (my ($id, $qty) = $sth->fetchrow_array) {
323 &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
327 if ($form->{item} ne 'service') {
328 # delete makemodel records
329 do_query($form, $dbh, qq|DELETE FROM makemodel WHERE parts_id = ?|, conv_i($form->{id}));
332 if ($form->{item} eq 'assembly') {
333 if ($form->{onhand} != 0) {
334 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand} * -1);
337 # delete assembly records
338 do_query($form, $dbh, qq|DELETE FROM assembly WHERE id = ?|, conv_i($form->{id}));
340 $form->{onhand} += $form->{stock};
344 do_query($form, $dbh, qq|DELETE FROM partstax WHERE parts_id = ?|, conv_i($form->{id}));
346 # delete translations
347 do_query($form, $dbh, qq|DELETE FROM translation WHERE parts_id = ?|, conv_i($form->{id}));
350 my ($count) = selectrow_query($form, $dbh, qq|SELECT COUNT(*) FROM parts WHERE partnumber = ?|, $form->{partnumber});
352 $main::lxdebug->leave_sub();
356 ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
357 do_query($form, $dbh, qq|INSERT INTO parts (id, partnumber) VALUES (?, '')|, $form->{id});
359 $form->{orphaned} = 1;
360 $form->{onhand} = $form->{stock} if $form->{item} eq 'assembly';
361 if ($form->{partnumber} eq "" && $form->{"item"} eq "service") {
362 $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
364 if ($form->{partnumber} eq "" && $form->{"item"} ne "service") {
365 $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
369 my $partsgroup_id = 0;
371 if ($form->{partsgroup}) {
372 ($partsgroup, $partsgroup_id) = split(/--/, $form->{partsgroup});
375 my ($subq_inventory, $subq_expense, $subq_income);
376 if ($form->{"item"} eq "part") {
378 qq|(SELECT bg.inventory_accno_id
379 FROM buchungsgruppen bg
380 WHERE bg.id = | . conv_i($form->{"buchungsgruppen_id"}, 'NULL') . qq|)|;
382 $subq_inventory = "NULL";
385 if ($form->{"item"} ne "assembly") {
387 qq|(SELECT bg.expense_accno_id_0
388 FROM buchungsgruppen bg
389 WHERE bg.id = | . conv_i($form->{"buchungsgruppen_id"}, 'NULL') . qq|)|;
391 $subq_expense = "NULL";
411 buchungsgruppen_id = ?,
413 inventory_accno_id = $subq_inventory,
414 income_accno_id = (SELECT bg.income_accno_id_0 FROM buchungsgruppen bg WHERE bg.id = ?),
415 expense_accno_id = $subq_expense,
423 not_discountable = ?,
427 @values = ($form->{partnumber},
428 $form->{description},
429 $makemodel ? 't' : 'f',
430 $form->{assembly} ? 't' : 'f',
435 conv_date($form->{priceupdate}),
441 conv_i($form->{buchungsgruppen_id}),
442 conv_i($form->{payment_id}),
443 conv_i($form->{buchungsgruppen_id}),
444 $form->{obsolete} ? 't' : 'f',
447 $form->{shop} ? 't' : 'f',
451 $form->{not_discountable} ? 't' : 'f',
453 conv_i($partsgroup_id),
456 do_query($form, $dbh, $query, @values);
458 # delete translation records
459 do_query($form, $dbh, qq|DELETE FROM translation WHERE parts_id = ?|, conv_i($form->{id}));
461 if ($form->{language_values} ne "") {
462 foreach $item (split(/---\+\+\+---/, $form->{language_values})) {
463 my ($language_id, $translation, $longdescription) = split(/--\+\+--/, $item);
464 if ($translation ne "") {
465 $query = qq|INSERT into translation (parts_id, language_id, translation, longdescription)
466 VALUES ( ?, ?, ?, ? )|;
467 @values = (conv_i($form->{id}), conv_i($language_id), $translation, $longdescription);
468 do_query($form, $dbh, $query, @values);
473 # delete price records
474 do_query($form, $dbh, qq|DELETE FROM prices WHERE parts_id = ?|, conv_i($form->{id}));
476 # insert price records only if different to sellprice
477 for my $i (1 .. $form->{price_rows}) {
478 if ($form->{"price_$i"} eq "0") {
479 $form->{"price_$i"} = $form->{sellprice};
482 ( $form->{"price_$i"}
483 || $form->{"klass_$i"}
484 || $form->{"pricegroup_id_$i"})
485 and $form->{"price_$i"} != $form->{sellprice}
487 #$klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
488 $price = $form->parse_amount($myconfig, $form->{"price_$i"});
490 $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
491 $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price) | .
493 @values = (conv_i($form->{id}), conv_i($pricegroup_id), $price);
494 do_query($form, $dbh, $query, @values);
498 # insert makemodel records
499 unless ($form->{item} eq 'service') {
500 for my $i (1 .. $form->{makemodel_rows}) {
501 if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
502 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(make model);
504 $query = qq|INSERT INTO makemodel (parts_id, make, model) | .
505 qq|VALUES (?, ?, ?)|;
506 @values = (conv_i($form->{id}), $form->{"make_$i"}, $form->{"model_$i"});
507 do_query($form, $dbh, $query, @values);
513 foreach $item (split(/ /, $form->{taxaccounts})) {
514 if ($form->{"IC_tax_$item"}) {
516 qq|INSERT INTO partstax (parts_id, chart_id)
517 VALUES (?, (SELECT id FROM chart WHERE accno = ?))|;
518 @values = (conv_i($form->{id}), $item);
519 do_query($form, $dbh, $query, @values);
523 # add assembly records
524 if ($form->{item} eq 'assembly') {
526 for my $i (1 .. $form->{assembly_rows}) {
527 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
529 if ($form->{"qty_$i"} != 0) {
530 $form->{"bom_$i"} *= 1;
531 $query = qq|INSERT INTO assembly (id, parts_id, qty, bom) | .
532 qq|VALUES (?, ?, ?, ?)|;
533 @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}), conv_i($form->{"qty_$i"}), $form->{"bom_$i"} ? 't' : 'f');
534 do_query($form, $dbh, $query, @values);
538 # adjust onhand for the parts
539 if ($form->{onhand} != 0) {
540 &adjust_inventory($dbh, $form, $form->{id}, $form->{onhand});
546 my $shippingdate = "$a[5]-$a[4]-$a[3]";
548 $form->get_employee($dbh);
550 # add inventory record
552 qq|INSERT INTO inventory (warehouse_id, parts_id, qty, shippingdate, employee_id)
553 VALUES (0, ?, ?, '$shippingdate', ?)|;
554 @values = (conv_i($form->{id}), $form->{stock}, conv_i($form->{employee_id}));
555 do_query($form, $dbh, $query, @values);
559 #set expense_accno=inventory_accno if they are different => bilanz
561 ($form->{expense_accno} != $form->{inventory_accno})
562 ? $form->{inventory_accno}
563 : $form->{expense_accno};
565 # get tax rates and description
567 ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
569 qq|SELECT c.accno, c.description, t.rate, t.taxnumber
571 WHERE (c.id = t.chart_id) AND (t.taxkey IN (SELECT taxkey_id FROM chart where accno = ?))
573 $stw = prepare_execute_query($form, $dbh, $query, $accno_id);
575 $form->{taxaccount} = "";
576 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
577 $form->{taxaccount} .= "$ptr->{accno} ";
578 if (!($form->{taxaccount2} =~ /$ptr->{accno}/)) {
579 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
580 $form->{"$ptr->{accno}_description"} = $ptr->{description};
581 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
582 $form->{taxaccount2} .= " $ptr->{accno} ";
587 my $rc = $dbh->commit;
590 $main::lxdebug->leave_sub();
595 sub update_assembly {
596 $main::lxdebug->enter_sub();
598 my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
600 my $query = qq|SELECT id, qty FROM assembly WHERE parts_id = ?|;
601 my $sth = prepare_execute_query($form, $dbh, $query, conv_i($id));
603 while (my ($pid, $aqty) = $sth->fetchrow_array) {
604 &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
609 qq|UPDATE parts SET sellprice = sellprice + ?, weight = weight + ?
611 @values = ($qty * ($form->{sellprice} - $sellprice),
612 $qty * ($form->{weight} - $weight), conv_i($id));
613 do_query($form, $dbh, $query, @values);
615 $main::lxdebug->leave_sub();
618 sub retrieve_assemblies {
619 $main::lxdebug->enter_sub();
621 my ($self, $myconfig, $form) = @_;
623 # connect to database
624 my $dbh = $form->dbconnect($myconfig);
626 my $where = qq|NOT p.obsolete|;
629 if ($form->{partnumber}) {
630 $where .= qq| AND (p.partnumber ILIKE ?)|;
631 push(@values, '%' . $form->{partnumber} . '%');
634 if ($form->{description}) {
635 $where .= qq| AND (p.description ILIKE ?)|;
636 push(@values, '%' . $form->{description} . '%');
639 # retrieve assembly items
641 qq|SELECT p.id, p.partnumber, p.description,
642 p.bin, p.onhand, p.rop,
643 (SELECT sum(p2.inventory_accno_id)
644 FROM parts p2, assembly a
645 WHERE (p2.id = a.parts_id) AND (a.id = p.id)) AS inventory
647 WHERE NOT p.obsolete AND p.assembly $where|;
649 $form->{assembly_items} = selectall_hashref_query($form, $dbh, $query, @values);
653 $main::lxdebug->leave_sub();
656 sub restock_assemblies {
657 $main::lxdebug->enter_sub();
659 my ($self, $myconfig, $form) = @_;
661 # connect to database
662 my $dbh = $form->dbconnect_noauto($myconfig);
664 for my $i (1 .. $form->{rowcount}) {
666 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
668 if ($form->{"qty_$i"} != 0) {
669 &adjust_inventory($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
674 my $rc = $dbh->commit;
677 $main::lxdebug->leave_sub();
682 sub adjust_inventory {
683 $main::lxdebug->enter_sub();
685 my ($dbh, $form, $id, $qty) = @_;
688 qq|SELECT p.id, p.inventory_accno_id, p.assembly, a.qty
689 FROM parts p, assembly a
690 WHERE (a.parts_id = p.id) AND (a.id = ?)|;
691 my $sth = prepare_execute_query($form, $dbh, $query, conv_i($id));
693 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
695 my $allocate = $qty * $ref->{qty};
697 # is it a service item, then loop
698 $ref->{inventory_accno_id} *= 1;
699 next if (($ref->{inventory_accno_id} == 0) && !$ref->{assembly});
701 # adjust parts onhand
702 $form->update_balance($dbh, "parts", "onhand",
710 my $rc = $form->update_balance($dbh, "parts", "onhand", qq|id = ?|, $qty, $id);
712 $main::lxdebug->leave_sub();
718 $main::lxdebug->enter_sub();
720 my ($self, $myconfig, $form) = @_;
721 my @values = (conv_i($form->{id}));
722 # connect to database, turn off AutoCommit
723 my $dbh = $form->dbconnect_noauto($myconfig);
725 my %columns = ( "assembly" => "id", "alternate" => "id", "parts" => "id" );
727 for my $table (qw(prices partstax makemodel inventory assembly parts)) {
728 my $column = defined($columns{$table}) ? $columns{$table} : "parts_id";
729 do_query($form, $dbh, qq|DELETE FROM $table WHERE $column = ?|, @values);
733 my $rc = $dbh->commit;
736 $main::lxdebug->leave_sub();
742 $main::lxdebug->enter_sub();
744 my ($self, $myconfig, $form) = @_;
746 my $i = $form->{assembly_rows};
748 my $where = qq|1 = 1|;
751 my %columns = ("partnumber" => "p", "description" => "p", "partsgroup" => "pg");
753 while (my ($column, $table) = each(%columns)) {
754 next unless ($form->{"${column}_$i"});
755 $where .= qq| AND ${table}.${column} ILIKE ?|;
756 push(@values, '%' . $form->{"${column}_$i"} . '%');
760 $where .= qq| AND NOT (p.id = ?)|;
761 push(@values, conv_i($form->{id}));
765 $where .= qq| ORDER BY p.partnumber|;
767 $where .= qq| ORDER BY p.description|;
770 # connect to database
771 my $dbh = $form->dbconnect($myconfig);
774 qq|SELECT p.id, p.partnumber, p.description, p.sellprice, p.weight, p.onhand, p.unit, pg.partsgroup
776 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
778 $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
782 $main::lxdebug->leave_sub();
787 # Warning, deep magic ahead.
788 # This function gets all parts from the database according to the filters specified
791 # sort revers - sorting field + direction
794 # simple filter strings (every one of those also has a column flag prefixed with 'l_' associated):
795 # partnumber ean description partsgroup microfiche drawing
798 # l_partnumber l_description l_listprice l_sellprice l_lastcost l_priceupdate l_weight l_unit l_bin l_rop l_image l_drawing l_microfiche l_partsgroup
801 # itemstatus = active | onhand | short | obsolete | orphaned
802 # searchitems = part | assembly | service
805 # make model - makemodel
806 # serialnumber transdatefrom transdateto - invoice/orderitems
809 # bought sold onorder ordered rfq quoted - aggreg joins with invoices/orders
810 # l_linetotal l_subtotal - aggreg joins to display totals (complicated) - NOT IMPLEMENTED here, implementation at frontend
811 # l_soldtotal - aggreg join to display total of sold quantity
812 # onhand - as above, but masking the simple itemstatus results (doh!)
813 # short - NOT IMPLEMENTED as form filter, only as itemstatus option
814 # l_serialnumber - belonges to serialnumber filter
815 # l_deliverydate - displays deliverydate is sold etc. flags are active
818 # l_soldtotal - aggreg join to display total of sold quantity
819 # onhand - as above, but masking the simple itemstatus results (doh!)
820 # masking of onhand in bsooqr mode - ToDO: fixme
822 # disabled sanity checks and changes:
823 # - searchitems = assembly will no longer disable bought
824 # - searchitems = service will no longer disable make and model, although services don't have make/model, it doesn't break the query
825 # - itemstatus = orphaned will no longer disable onhand short bought sold onorder ordered rfq quoted transdate[from|to]
826 # - itemstatus = obsolete will no longer disable onhand, short
827 # - allow sorting by ean
828 # - serialnumber filter also works if l_serialnumber isn't ticked
829 # - onhand doesn't get masked by it's oi or invoice counterparts atm. ToDO: fix this
830 # - sorting will now change sorting if the requested sorting column isn't checked and doesn't get checked as a side effect
833 $main::lxdebug->enter_sub();
835 my ($self, $myconfig, $form) = @_;
836 my $dbh = $form->get_standard_dbh($myconfig);
838 $form->{parts} = +{ };
840 my @simple_filters = qw(partnumber ean description partsgroup microfiche drawing);
841 my @makemodel_filters = qw(make model);
842 my @invoice_oi_filters = qw(serialnumber soldtotal);
843 my @apoe_filters = qw(transdate);
844 my @all_columns = (@simple_filters, @makemodel_filters, @apoe_filters, qw(serialnumber));
845 my @simple_l_switches = (@all_columns, qw(listprice sellprice lastcost priceupdate weight unit bin rop image));
846 my @oe_flags = qw(bought sold onorder ordered rfq quoted);
847 my @qsooqr_flags = qw(invnumber ordnumber quonumber trans_id name module);
848 my @deliverydate_flags = qw(deliverydate);
849 # my @other_flags = qw(onhand); # ToDO: implement these
850 # my @inactive_flags = qw(l_subtotal short l_linetotal);
853 partsgroup => 'LEFT JOIN partsgroup pg ON p.partsgroup_id = pg.id',
854 makemodel => 'LEFT JOIN makemodel mm ON mm.parts_id = p.id',
857 SELECT parts_id, description, serialnumber, trans_id, unit, sellprice, qty, assemblyitem, 'invoice' AS ioi FROM invoice UNION
858 SELECT parts_id, description, serialnumber, trans_id, unit, sellprice, qty, FALSE AS assemblyitem, 'orderitems' AS ioi FROM orderitems
859 ) AS ioi ON ioi.parts_id = p.id|,
862 SELECT id, transdate, 'ir' AS module, ordnumber, quonumber, invnumber, FALSE AS quotation, NULL AS customer_id, vendor_id, NULL AS deliverydate FROM ap UNION
863 SELECT id, transdate, 'is' AS module, ordnumber, quonumber, invnumber, FALSE AS quotation, customer_id, NULL AS vendor_id, deliverydate FROM ar UNION
864 SELECT id, transdate, 'oe' AS module, ordnumber, quonumber, NULL AS invnumber, quotation, customer_id, vendor_id, NULL AS deliverydate FROM oe
865 ) AS apoe ON ioi.trans_id = apoe.id|,
868 SELECT id, name, 'customer' AS cv FROM customer UNION
869 SELECT id, name, 'vendor' AS cv FROM vendor
870 ) AS cv ON cv.id = apoe.customer_id OR cv.id = apoe.vendor_id|,
872 my @join_order = qw(partsgroup makemodel invoice_oi apoe cv);
873 my %joins_needed = (0) x scalar keys %joins;
875 #===== switches and simple filters ========#
877 my @select_tokens = qw(id);
878 my @where_tokens = qw(1=1);
879 my @group_tokens = ();
881 # special case transdate
882 if (grep { $form->{$_} } qw(transdatefrom transdateto)) {
883 $form->{"l_transdate"} = 1;
884 push @select_tokens, 'transdate';
885 for (qw(transdatefrom transdateto)) {
886 next unless $form->{$_};
887 push @where_tokens, sprintf "transdate %s ?", /from$/ ? '>=' : '<=';
888 push @bind_vars, $form->{$_};
892 foreach (@simple_filters, @makemodel_filters, @invoice_oi_filters) {
893 next unless $form->{$_};
894 $form->{"l_$_"} = '1'; # show the column
895 push @where_tokens, "$_ ILIKE ?";
896 push @bind_vars, "%$form->{$_}%";
899 foreach (@simple_l_switches) {
900 next unless $form->{"l_$_"};
901 push @select_tokens, $_;
904 for ($form->{searchitems}) {
905 push @where_tokens, 'p.inventory_accno_id > 0' if /part/;
906 push @where_tokens, 'p.inventory_accno_id IS NULL' if /service/;
907 push @where_tokens, 'NOT p.assembly' if /service/;
908 push @where_tokens, ' p.assembly' if /assembly/;
911 for ($form->{itemstatus}) {
912 push @where_tokens, 'p.id NOT IN
913 (SELECT DISTINCT parts_id FROM invoice UNION
914 SELECT DISTINCT parts_id FROM assembly UNION
915 SELECT DISTINCT parts_id FROM orderitems)' if /orphaned/;
916 push @where_tokens, 'p.onhand = 0' if /orphaned/;
917 push @where_tokens, 'NOT p.obsolete' if /active/;
918 push @where_tokens, ' p.obsolete', if /obsolete/;
919 push @where_tokens, 'p.onhand > 0', if /onhand/;
920 push @where_tokens, 'p.onhand < p.rop', if /short/;
924 my @sort_cols = (@simple_filters, qw(id bin priceupdate onhand invnumber ordnumber quonumber name serialnumber soldtotal deliverydate));
925 $form->{sort} = 'id' unless grep { $form->{"l_$_"} } grep { $form->{sort} eq $_ } @sort_cols;
926 my $order_clause = " ORDER BY $form->{sort} " . ($form->{revers} ? 'DESC' : 'ASC');
928 my $limit_clause = " LIMIT 100" if $form->{top100};
930 #=== joins and complicated filters ========#
932 my $bsooqr = $form->{bought} || $form->{sold}
933 || $form->{ordered} || $form->{onorder}
934 || $form->{quoted} || $form->{rfq};
937 push @select_tokens, @qsooqr_flags if $bsooqr;
938 push @select_tokens, @deliverydate_flags if $bsooqr && $form->{l_deliverydate};
939 push @bsooqr_tokens, q|module = 'ir' AND NOT ioi.assemblyitem| if $form->{bought};
940 push @bsooqr_tokens, q|module = 'is' AND NOT ioi.assemblyitem| if $form->{sold};
941 push @bsooqr_tokens, q|module = 'oe' AND NOT quotation AND cv = 'customer'| if $form->{ordered};
942 push @bsooqr_tokens, q|module = 'oe' AND NOT quotation AND cv = 'vendor'| if $form->{onorder};
943 push @bsooqr_tokens, q|module = 'oe' AND quotation AND cv = 'customer'| if $form->{quoted};
944 push @bsooqr_tokens, q|module = 'oe' AND quotation AND cv = 'vendor'| if $form->{rfq};
945 push @where_tokens, join ' OR ', map { "($_)" } @bsooqr_tokens if $bsooqr;
947 $joins_needed{partsgroup} = 1;
948 $joins_needed{makemodel} = 1 if grep { $form->{$_} || $form->{"l_$_"} } @makemodel_filters;
949 $joins_needed{cv} = 1 if $bsooqr;
950 $joins_needed{apoe} = 1 if $joins_needed{cv} || grep { $form->{$_} || $form->{"l_$_"} } @apoe_filters;
951 $joins_needed{invoice_oi} = 1 if $joins_needed{apoe} || grep { $form->{$_} || $form->{"l_$_"} } @invoice_oi_filters;
953 # special case for description search.
954 # up in the simple filter section the description filter got interpreted as something like: WHERE description ILIKE '%$form->{description}%'
955 # now we'd like to search also for the masked description entered in orderitems and invoice, so...
956 # find the old entries in of @where_tokens and @bind_vars, and adjust them
957 if ($joins_needed{invoice_oi}) {
958 for (my ($wi, $bi) = (0)x2; $wi <= $#where_tokens; $bi++ if $where_tokens[$wi++] =~ /\?/) {
959 next unless $where_tokens[$wi] =~ /^description ILIKE/;
960 splice @where_tokens, $wi, 1, 'p.description ILIKE ? OR ioi.description ILIKE ?';
961 splice @bind_vars, $bi, 0, $bind_vars[$bi];
966 # now the master trick: soldtotal.
967 if ($form->{l_soldtotal}) {
968 push @where_tokens, 'ioi.qty >= 0';
969 push @group_tokens, @select_tokens;
970 push @select_tokens, 'SUM(ioi.qty) AS soldtotal';
973 #============= build query ================#
976 deliverydate => 'apoe.', serialnumber => 'ioi.',
977 transdate => 'apoe.', trans_id => 'ioi.',
978 module => 'apoe.', name => 'cv.',
979 ordnumber => 'apoe.', make => 'mm.',
980 quonumber => 'apoe.', model => 'mm.',
981 invnumber => 'apoe.', partsgroup => 'pg.',
982 'SUM(ioi.qty) AS soldtotal' => ' ',
985 map { $table_prefix{$_} = 'ioi.' } qw(description serialnumber qty unit) if $joins_needed{invoice_oi};
987 my $select_clause = join ', ', map { ($table_prefix{$_} || "p.") . $_ } @select_tokens;
988 my $join_clause = join ' ', @joins{ grep $joins_needed{$_}, @join_order };
989 my $where_clause = join ' AND ', map { "($_)" } @where_tokens;
990 my $group_clause = ' GROUP BY ' . join ', ', map { ($table_prefix{$_} || "p.") . $_ } @group_tokens if scalar @group_tokens;
992 my $query = qq|SELECT DISTINCT $select_clause FROM parts p $join_clause WHERE $where_clause $group_clause $order_clause $limit_clause|;
993 $form->{parts} = selectall_hashref_query($form, $dbh, $query, @bind_vars);
995 ## my $where = qq|1 = 1|;
996 ## my (@values, $var, $flds, $group, $limit);
998 ## foreach my $item (qw(partnumber drawing microfiche ean pg.partsgroup)) {
999 ## my $column = $item;
1000 ## $column =~ s/.*\.//; # get rid of table prefixes
1001 ## if ($form->{$column}) {
1002 ## $where .= qq| AND ($item ILIKE ?)|;
1003 ## push(@values, "%$form->{$column}%");
1007 ## # special case for description
1008 ## if ($form->{description}
1009 ## && !( $form->{bought} || $form->{sold} || $form->{onorder}
1010 ## || $form->{ordered} || $form->{rfq} || $form->{quoted})) {
1011 ## $where .= qq| AND (p.description ILIKE ?)|;
1012 ## push(@values, "%$form->{description}%");
1015 ## # special case for serialnumber
1016 ## if ($form->{l_serialnumber} && $form->{serialnumber}) {
1017 ## $where .= qq| AND (serialnumber ILIKE ?)|;
1018 ## push(@values, "%$form->{serialnumber}%");
1021 ## if ($form->{searchitems} eq 'part') {
1022 ## $where .= qq| AND (p.inventory_accno_id > 0) |;
1025 ## if ($form->{searchitems} eq 'assembly') {
1026 ## $form->{bought} = "";
1027 ## $where .= qq| AND p.assembly|;
1030 ## if ($form->{searchitems} eq 'service') {
1031 ## $where .= qq| AND (p.inventory_accno_id IS NULL) AND NOT (p.assembly = '1')|;
1032 ## # irrelevant for services
1033 ## map { $form->{$_} = '' } qw(make model);
1036 ## # items which were never bought, sold or on an order
1037 ## if ($form->{itemstatus} eq 'orphaned') {
1038 ## map { $form->{$_} = 0 } qw(onhand short bought sold onorder ordered rfq quoted);
1039 ## map { $form->{$_} = '' } qw(transdatefrom transdateto);
1042 ## qq| AND (p.onhand = 0)
1045 ## SELECT DISTINCT parts_id FROM invoice
1047 ## SELECT DISTINCT parts_id FROM assembly
1049 ## SELECT DISTINCT parts_id FROM orderitems
1053 ## my %status2condition = (
1054 ## active => " AND (p.obsolete = '0')",
1055 ## obsolete => " AND (p.obsolete = '1')",
1056 ## onhand => " AND (p.onhand > 0)",
1057 ## short => " AND (p.onhand < p.rop)",
1059 ## $where .= $status2condition{$form->{itemstatus}};
1061 ## $form->{onhand} = $form->{short} = 0 if ($form->{itemstatus} eq 'obsolete');
1064 ## foreach my $column (qw(make model)) {
1065 ## push @subcolumns, $column if $form->{$column};
1067 ## if (@subcolumns) {
1068 ## $where .= qq| AND p.id IN (SELECT DISTINCT parts_id FROM makemodel WHERE | . (join " AND ", map { "($_ ILIKE ?)"; } @subcolumns) . ")";
1069 ## push @values, map { '%' . $form->{$_} . '%' } @subcolumns;
1072 ## if ($form->{l_soldtotal}) {
1073 ## $where .= qq| AND (p.id = i.parts_id) AND (i.qty >= 0)|;
1074 ## $group = qq| GROUP BY p.id, p.partnumber, p.description, p.onhand, p.unit, p.bin, p.sellprice, p.listprice, p.lastcost, p.priceupdate, pg.partsgroup|;
1077 ## $limit = qq| LIMIT 100| if ($form->{top100});
1079 ## # connect to database
1080 ## my $dbh = $form->dbconnect($myconfig);
1082 ## my @sort_cols = qw(id partnumber description partsgroup bin priceupdate onhand
1083 ## invnumber ordnumber quonumber name drawing microfiche
1084 ## serialnumber soldtotal deliverydate);
1086 ## my $sortorder = "partnumber";
1087 ## $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } @sort_cols));
1088 ## $sortorder .= " DESC" if ($form->{revers});
1092 ## if ($form->{l_soldtotal}) {
1093 ## $form->{soldtotal} = 'soldtotal';
1095 ## qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
1096 ## p.bin, p.sellprice, p.listprice, p.lastcost,
1097 ## p.priceupdate, pg.partsgroup,sum(i.qty) AS soldtotal
1099 ## LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id), invoice i
1102 ## ORDER BY $sortorder $limit|;
1105 ## qq|SELECT p.id, p.partnumber, p.description, p.onhand, p.unit,
1106 ## p.bin, p.sellprice, p.listprice, p.lastcost, p.rop, p.weight,
1107 ## p.priceupdate, p.image, p.drawing, p.microfiche,
1110 ## LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1113 ## ORDER BY $sortorder $limit|;
1116 ## my @all_values = @values;
1118 ## # rebuild query for bought and sold items
1119 ## if ( $form->{bought}
1121 ## || $form->{onorder}
1122 ## || $form->{ordered}
1124 ## || $form->{quoted}) {
1127 ## @all_values = ();
1129 ## if ($form->{bought} || $form->{sold}) {
1131 ## my @invvalues = @values;
1132 ## my $invwhere = "$where";
1133 # $invwhere .= qq| AND i.assemblyitem = '0'|;
1135 ## if ($form->{transdatefrom}) {
1136 ## $invwhere .= qq| AND a.transdate >= ?|;
1137 ## push(@invvalues, $form->{transdatefrom});
1140 ## if ($form->{transdateto}) {
1141 ## $invwhere .= qq| AND a.transdate <= ?|;
1142 ## push(@invvalues, $form->{transdateto});
1145 ## if ($form->{description}) {
1146 ## $invwhere .= qq| AND i.description ILIKE ?|;
1147 ## push(@invvalues, '%' . $form->{description} . '%');
1151 ## qq|p.id, p.partnumber, i.description, i.serialnumber,
1152 # i.qty AS onhand, i.unit, p.bin, i.sellprice,
1153 ## p.listprice, p.lastcost, p.rop, p.weight,
1154 ## p.priceupdate, p.image, p.drawing, p.microfiche,
1156 ## a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
1157 ## ct.name, i.deliverydate|;
1159 ## if ($form->{bought}) {
1161 ## qq|SELECT $flds, 'ir' AS module, '' AS type, 1 AS exchangerate
1163 ## JOIN parts p ON (p.id = i.parts_id)
1164 ## JOIN ap a ON (a.id = i.trans_id)
1165 ## JOIN vendor ct ON (a.vendor_id = ct.id)
1166 ## LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1167 ## WHERE $invwhere|;
1169 ## $union = qq| UNION |;
1171 ## push(@all_values, @invvalues);
1174 ## if ($form->{sold}) {
1178 ## SELECT $flds, 'is' AS module, '' AS type, 1 As exchangerate
1180 ## JOIN parts p ON (p.id = i.parts_id)
1181 ## JOIN ar a ON (a.id = i.trans_id)
1182 ## JOIN customer ct ON (a.customer_id = ct.id)
1183 ## LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1184 ## WHERE $invwhere|;
1185 ## $union = qq| UNION |;
1187 ## push(@all_values, @invvalues);
1191 ## if ($form->{onorder} || $form->{ordered}) {
1192 ## my @ordvalues = @values;
1193 ## my $ordwhere = $where . qq| AND o.quotation = '0'|;
1195 ## if ($form->{transdatefrom}) {
1196 ## $ordwhere .= qq| AND o.transdate >= ?|;
1197 ## push(@ordvalues, $form->{transdatefrom});
1200 ## if ($form->{transdateto}) {
1201 ## $ordwhere .= qq| AND o.transdate <= ?|;
1202 ## push(@ordvalues, $form->{transdateto});
1205 ## if ($form->{description}) {
1206 ## $ordwhere .= qq| AND oi.description ILIKE ?|;
1207 ## push(@ordvalues, '%' . $form->{description} . '%');
1210 ## if ($form->{ordered}) {
1214 ## SELECT p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1215 ## oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1216 ## p.listprice, p.lastcost, p.rop, p.weight,
1217 ## p.priceupdate, p.image, p.drawing, p.microfiche,
1219 ## '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1220 ## ct.name, NULL AS deliverydate,
1221 ## 'oe' AS module, 'sales_order' AS type,
1222 ## (SELECT buy FROM exchangerate ex
1223 ## WHERE ex.curr = o.curr AND ex.transdate = o.transdate) AS exchangerate
1224 ## FROM orderitems oi
1225 ## JOIN parts p ON (oi.parts_id = p.id)
1226 ## JOIN oe o ON (oi.trans_id = o.id)
1227 ## JOIN customer ct ON (o.customer_id = ct.id)
1228 ## LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1229 ## WHERE $ordwhere AND (o.customer_id > 0)|;
1230 ## $union = qq| UNION |;
1232 ## push(@all_values, @ordvalues);
1235 ## if ($form->{onorder}) {
1239 ## SELECT p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1240 ## oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1241 ## p.listprice, p.lastcost, p.rop, p.weight,
1242 ## p.priceupdate, p.image, p.drawing, p.microfiche,
1244 ## '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1245 ## ct.name, NULL AS deliverydate,
1246 ## 'oe' AS module, 'purchase_order' AS type,
1247 ## (SELECT sell FROM exchangerate ex
1248 ## WHERE ex.curr = o.curr AND (ex.transdate = o.transdate)) AS exchangerate
1249 ## FROM orderitems oi
1250 ## JOIN parts p ON (oi.parts_id = p.id)
1251 ## JOIN oe o ON (oi.trans_id = o.id)
1252 ## JOIN vendor ct ON (o.vendor_id = ct.id)
1253 ## LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1254 ## WHERE $ordwhere AND (o.vendor_id > 0)|;
1255 ## $union = qq| UNION |;
1257 ## push(@all_values, @ordvalues);
1262 ## if ($form->{rfq} || $form->{quoted}) {
1263 ## my $quowhere = $where . qq| AND o.quotation = '1'|;
1264 ## my @quovalues = @values;
1266 ## if ($form->{transdatefrom}) {
1267 ## $quowhere .= qq| AND o.transdate >= ?|;
1268 ## push(@quovalues, $form->{transdatefrom});
1271 ## if ($form->{transdateto}) {
1272 ## $quowhere .= qq| AND o.transdate <= ?|;
1273 ## push(@quovalues, $form->{transdateto});
1276 ## if ($form->{description}) {
1277 ## $quowhere .= qq| AND oi.description ILIKE ?|;
1278 ## push(@quovalues, '%' . $form->{description} . '%');
1281 ## if ($form->{quoted}) {
1286 ## p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1287 ## oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
1288 ## p.listprice, p.lastcost, p.rop, p.weight,
1289 ## p.priceupdate, p.image, p.drawing, p.microfiche,
1291 ## '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1292 ## ct.name, NULL AS deliverydate, 'oe' AS module, 'sales_quotation' AS type,
1293 ## (SELECT buy FROM exchangerate ex
1294 ## WHERE (ex.curr = o.curr) AND (ex.transdate = o.transdate)) AS exchangerate
1295 ## FROM orderitems oi
1296 ## JOIN parts p ON (oi.parts_id = p.id)
1297 ## JOIN oe o ON (oi.trans_id = o.id)
1298 ## JOIN customer ct ON (o.customer_id = ct.id)
1299 ## LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1301 ## AND o.customer_id > 0|;
1302 ## $union = qq| UNION |;
1304 ## push(@all_values, @quovalues);
1307 ## if ($form->{rfq}) {
1311 ## SELECT p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
1312 ## oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
1313 ## p.listprice, p.lastcost, p.rop, p.weight,
1314 ## p.priceupdate, p.image, p.drawing, p.microfiche,
1316 ## '' AS invnumber, o.ordnumber, o.quonumber, oi.trans_id,
1317 ## ct.name, NULL AS deliverydate,
1318 ## 'oe' AS module, 'request_quotation' AS type,
1319 ## (SELECT sell FROM exchangerate ex
1320 ## WHERE (ex.curr = o.curr) AND (ex.transdate = o.transdate)) AS exchangerate
1321 ## FROM orderitems oi
1322 ## JOIN parts p ON (oi.parts_id = p.id)
1323 ## JOIN oe o ON (oi.trans_id = o.id)
1324 ## JOIN vendor ct ON (o.vendor_id = ct.id)
1325 ## LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1327 ## AND o.vendor_id > 0|;
1329 ## push(@all_values, @quovalues);
1333 ## $query .= qq| ORDER BY | . $sortorder;
1337 ## $form->{parts} = selectall_hashref_query($form, $dbh, $query, @all_values);
1340 # include individual items for assemblies
1341 if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
1343 qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
1345 p.sellprice, p.listprice, p.lastcost,
1346 p.rop, p.weight, p.priceupdate,
1347 p.image, p.drawing, p.microfiche
1348 FROM parts p, assembly a
1349 WHERE (p.id = a.parts_id) AND (a.id = ?)|;
1350 $sth = prepare_query($form, $dbh, $query);
1352 foreach $item (@{ $form->{parts} }) {
1353 push(@assemblies, $item);
1354 do_statement($form, $sth, $query, conv_i($item->{id}));
1356 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1357 $ref->{assemblyitem} = 1;
1358 push(@assemblies, $ref);
1363 # copy assemblies to $form->{parts}
1364 $form->{parts} = \@assemblies;
1368 $main::lxdebug->leave_sub();
1372 $main::lxdebug->enter_sub();
1374 my ($self, $myconfig, $form) = @_;
1376 my $where = '1 = 1';
1384 if ($item ne 'make') {
1385 foreach my $item (qw(partnumber drawing microfiche make model pg.partsgroup)) {
1387 $column =~ s/.*\.//;
1388 next unless ($form->{$column});
1389 $where .= qq| AND $item ILIKE ?|;
1390 push(@where_values, '%' . $form->{$column} . '%');
1394 # special case for description
1395 if ($form->{description}
1396 && !( $form->{bought} || $form->{sold} || $form->{onorder}
1397 || $form->{ordered} || $form->{rfq} || $form->{quoted})) {
1398 $where .= qq| AND (p.description ILIKE ?)|;
1399 push(@where_values, '%' . $form->{description} . '%');
1402 # special case for serialnumber
1403 if ($form->{l_serialnumber} && $form->{serialnumber}) {
1404 $where .= qq| AND serialnumber ILIKE ?|;
1405 push(@where_values, '%' . $form->{serialnumber} . '%');
1409 # items which were never bought, sold or on an order
1410 if ($form->{itemstatus} eq 'orphaned') {
1411 $form->{onhand} = $form->{short} = 0;
1412 $form->{bought} = $form->{sold} = 0;
1413 $form->{onorder} = $form->{ordered} = 0;
1414 $form->{rfq} = $form->{quoted} = 0;
1416 $form->{transdatefrom} = $form->{transdateto} = "";
1419 qq| AND (p.onhand = 0)
1422 SELECT DISTINCT parts_id FROM invoice
1424 SELECT DISTINCT parts_id FROM assembly
1426 SELECT DISTINCT parts_id FROM orderitems
1430 if ($form->{itemstatus} eq 'active') {
1431 $where .= qq| AND p.obsolete = '0'|;
1434 if ($form->{itemstatus} eq 'obsolete') {
1435 $where .= qq| AND p.obsolete = '1'|;
1436 $form->{onhand} = $form->{short} = 0;
1439 if ($form->{itemstatus} eq 'onhand') {
1440 $where .= qq| AND p.onhand > 0|;
1443 if ($form->{itemstatus} eq 'short') {
1444 $where .= qq| AND p.onhand < p.rop|;
1447 foreach my $column (qw(make model)) {
1448 next unless ($form->{$colum});
1449 $where .= qq| AND p.id IN (SELECT DISTINCT parts_id FROM makemodel WHERE $column ILIKE ?|;
1450 push(@where_values, '%' . $form->{$column} . '%');
1453 # connect to database
1454 my $dbh = $form->dbconnect_noauto($myconfig);
1456 for my $column (qw(sellprice listprice)) {
1457 next if ($form->{$column} eq "");
1459 my $value = $form->parse_amount($myconfig, $form->{$column});
1462 if ($form->{"${column}_type"} eq "percent") {
1463 $value = ($value / 100) + 1;
1468 qq|UPDATE parts SET $column = $column $operator ?
1472 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1474 do_query($from, $dbh, $query, $value, @where_values);
1478 qq|UPDATE prices SET price = price + ?
1482 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1483 WHERE $where) AND (pricegroup_id = ?)|;
1484 my $sth_add = prepare_query($form, $dbh, $q_add);
1487 qq|UPDATE prices SET price = price * ?
1491 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1492 WHERE $where) AND (pricegroup_id = ?)|;
1493 my $sth_multiply = prepare_query($form, $dbh, $q_multiply);
1495 for my $i (1 .. $form->{price_rows}) {
1496 next if ($form->{"price_$i"} eq "");
1498 my $value = $form->parse_amount($myconfig, $form->{"price_$i"});
1500 if ($form->{"pricegroup_type_$i"} eq "percent") {
1501 do_statement($form, $sth_multiply, $q_multiply, ($value / 100) + 1, @where_values, conv_i($form->{"pricegroup_id_$i"}));
1503 do_statement($form, $sth_add, $q_add, $value, @where_values, conv_i($form->{"pricegroup_id_$i"}));
1508 $sth_multiply->finish();
1510 my $rc= $dbh->commit;
1513 $main::lxdebug->leave_sub();
1519 $main::lxdebug->enter_sub();
1521 my ($self, $module, $myconfig, $form) = @_;
1523 # connect to database
1524 my $dbh = $form->dbconnect($myconfig);
1526 my @values = ('%' . $module . '%');
1530 qq|SELECT c.accno, c.description, c.link, c.id,
1531 p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1532 FROM chart c, parts p
1533 WHERE (c.link LIKE ?) AND (p.id = ?)
1535 push(@values, conv_i($form->{id}));
1539 qq|SELECT c.accno, c.description, c.link, c.id,
1540 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1541 FROM chart c, defaults d
1546 my $sth = prepare_execute_query($form, $dbh, $query, @values);
1547 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1548 foreach my $key (split(/:/, $ref->{link})) {
1549 if ($key =~ /$module/) {
1550 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1551 || ($ref->{id} eq $ref->{income_accno_id})
1552 || ($ref->{id} eq $ref->{expense_accno_id})) {
1553 push @{ $form->{"${module}_links"}{$key} },
1554 { accno => $ref->{accno},
1555 description => $ref->{description},
1556 selected => "selected" };
1557 $form->{"${key}_default"} = "$ref->{accno}--$ref->{description}";
1559 push @{ $form->{"${module}_links"}{$key} },
1560 { accno => $ref->{accno},
1561 description => $ref->{description},
1569 # get buchungsgruppen
1570 $form->{BUCHUNGSGRUPPEN} = selectall_hashref_query($form, $dbh, qq|SELECT id, description FROM buchungsgruppen|);
1573 $form->{payment_terms} = selectall_hashref_query($form, $dbh, qq|SELECT id, description FROM payment_terms ORDER BY sortkey|);
1576 ($form->{priceupdate}) = selectrow_query($form, $dbh, qq|SELECT current_date|);
1580 $main::lxdebug->leave_sub();
1583 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1585 $main::lxdebug->enter_sub();
1587 my ($self, $myconfig, $form, $sortorder) = @_;
1588 my $dbh = $form->dbconnect($myconfig);
1589 my $order = qq| p.partnumber|;
1590 my $where = qq|1 = 1|;
1593 if ($sortorder eq "all") {
1594 $where .= qq| AND (partnumber ILIKE ?) AND (description ILIKE ?)|;
1595 push(@values, '%' . $form->{partnumber} . '%', '%' . $form->{description} . '%');
1597 } elsif ($sortorder eq "partnumber") {
1598 $where .= qq| AND (partnumber ILIKE ?)|;
1599 push(@values, '%' . $form->{partnumber} . '%');
1601 } elsif ($sortorder eq "description") {
1602 $where .= qq| AND (description ILIKE ?)|;
1603 push(@values, '%' . $form->{description} . '%');
1604 $order = "description";
1609 qq|SELECT id, partnumber, description, unit, sellprice
1611 WHERE $where ORDER BY $order|;
1613 my $sth = prepare_execute_query($form, $dbh, $query, @values);
1616 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1617 if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1622 $form->{"id_$j"} = $ref->{id};
1623 $form->{"partnumber_$j"} = $ref->{partnumber};
1624 $form->{"description_$j"} = $ref->{description};
1625 $form->{"unit_$j"} = $ref->{unit};
1626 $form->{"sellprice_$j"} = $ref->{sellprice};
1627 $form->{"soldtotal_$j"} = get_soldtotal($dbh, $ref->{id});
1633 $main::lxdebug->leave_sub();
1638 # gets sum of sold part with part_id
1640 $main::lxdebug->enter_sub();
1642 my ($dbh, $id) = @_;
1644 my $query = qq|SELECT sum(qty) FROM invoice WHERE parts_id = ?|;
1645 my ($sum) = selectrow_query($form, $dbh, $query, conv_i($id));
1648 $main::lxdebug->leave_sub();
1651 } #end get_soldtotal
1653 sub retrieve_languages {
1654 $main::lxdebug->enter_sub();
1656 my ($self, $myconfig, $form) = @_;
1658 # connect to database
1659 my $dbh = $form->dbconnect($myconfig);
1664 if ($form->{language_values} ne "") {
1666 qq|SELECT l.id, l.description, tr.translation, tr.longdescription
1668 LEFT OUTER JOIN translation tr ON (tr.language_id = l.id) AND (tr.parts_id = ?)|;
1669 @values = (conv_i($form->{id}));
1672 $query = qq|SELECT id, description FROM language|;
1675 my $languages = selectall_hashref_query($form, $dbh, $query, @values);
1679 $main::lxdebug->leave_sub();
1684 sub follow_account_chain {
1685 $main::lxdebug->enter_sub(2);
1687 my ($self, $form, $dbh, $transdate, $accno_id, $accno) = @_;
1689 my @visited_accno_ids = ($accno_id);
1694 qq|SELECT c.new_chart_id, date($transdate) >= c.valid_from AS is_valid, | .
1697 qq|LEFT JOIN chart cnew ON c.new_chart_id = cnew.id | .
1698 qq|WHERE (c.id = ?) AND NOT c.new_chart_id ISNULL AND (c.new_chart_id > 0)|;
1699 $sth = prepare_query($form, $dbh, $query);
1702 do_statement($form, $sth, $query, $accno_id);
1703 $ref = $sth->fetchrow_hashref();
1704 last unless ($ref && $ref->{"is_valid"} &&
1705 !grep({ $_ == $ref->{"new_chart_id"} } @visited_accno_ids));
1706 $accno_id = $ref->{"new_chart_id"};
1707 $accno = $ref->{"accno"};
1708 push(@visited_accno_ids, $accno_id);
1711 $main::lxdebug->leave_sub(2);
1713 return ($accno_id, $accno);
1716 sub retrieve_accounts {
1717 $main::lxdebug->enter_sub(2);
1719 my ($self, $myconfig, $form, $parts_id, $index) = @_;
1721 my ($query, $sth, $dbh);
1723 $form->{"taxzone_id"} *= 1;
1725 $dbh = $form->get_standard_dbh($myconfig);
1728 if ($form->{type} eq "invoice") {
1729 if (($form->{vc} eq "vendor") || !$form->{deliverydate}) {
1730 $transdate = $form->{invdate};
1732 $transdate = $form->{deliverydate};
1734 } elsif (($form->{type} eq "credit_note") || ($form->{script} eq 'ir.pl')) {
1735 $transdate = $form->{invdate};
1737 $transdate = $form->{transdate};
1740 if ($transdate eq "") {
1741 $transdate = "current_date";
1743 $transdate = $dbh->quote($transdate);
1748 qq| p.inventory_accno_id AS is_part, | .
1749 qq| bg.inventory_accno_id, | .
1750 qq| bg.income_accno_id_$form->{taxzone_id} AS income_accno_id, | .
1751 qq| bg.expense_accno_id_$form->{taxzone_id} AS expense_accno_id, | .
1752 qq| c1.accno AS inventory_accno, | .
1753 qq| c2.accno AS income_accno, | .
1754 qq| c3.accno AS expense_accno | .
1756 qq|LEFT JOIN buchungsgruppen bg ON p.buchungsgruppen_id = bg.id | .
1757 qq|LEFT JOIN chart c1 ON bg.inventory_accno_id = c1.id | .
1758 qq|LEFT JOIN chart c2 ON bg.income_accno_id_$form->{taxzone_id} = c2.id | .
1759 qq|LEFT JOIN chart c3 ON bg.expense_accno_id_$form->{taxzone_id} = c3.id | .
1761 my $ref = selectfirst_hashref_query($form, $dbh, $query, $parts_id);
1763 return $main::lxdebug->leave_sub(2) if (!$ref);
1765 $ref->{"inventory_accno_id"} = undef unless ($ref->{"is_part"});
1768 foreach my $type (qw(inventory income expense)) {
1769 next unless ($ref->{"${type}_accno_id"});
1770 ($accounts{"${type}_accno_id"}, $accounts{"${type}_accno"}) =
1771 $self->follow_account_chain($form, $dbh, $transdate,
1772 $ref->{"${type}_accno_id"},
1773 $ref->{"${type}_accno"});
1776 map({ $form->{"${_}_accno_$index"} = $accounts{"${_}_accno"} }
1777 qw(inventory income expense));
1779 my $inc_exp = $form->{"vc"} eq "customer" ? "income" : "expense";
1780 my $accno_id = $accounts{"${inc_exp}_accno_id"};
1783 qq|SELECT c.accno, t.taxdescription AS description, t.rate, t.taxnumber | .
1785 qq|LEFT JOIN chart c ON c.id = t.chart_id | .
1786 qq|WHERE t.id IN | .
1787 qq| (SELECT tk.tax_id | .
1788 qq| FROM taxkeys tk | .
1789 qq| WHERE tk.chart_id = ? AND startdate <= | . quote_db_date($transdate) .
1790 qq| ORDER BY startdate DESC LIMIT 1) |;
1791 $ref = selectfirst_hashref_query($form, $dbh, $query, $accno_id);
1794 $main::lxdebug->leave_sub(2);
1798 $form->{"taxaccounts_$index"} = $ref->{"accno"};
1799 if ($form->{"taxaccounts"} !~ /$ref->{accno}/) {
1800 $form->{"taxaccounts"} .= "$ref->{accno} ";
1802 map({ $form->{"$ref->{accno}_${_}"} = $ref->{$_}; }
1803 qw(rate description taxnumber));
1805 # $main::lxdebug->message(0, "formvars: rate " . $form->{"$ref->{accno}_rate"} .
1806 # " description " . $form->{"$ref->{accno}_description"} .
1807 # " taxnumber " . $form->{"$ref->{accno}_taxnumber"} .
1808 # " || taxaccounts_$index " . $form->{"taxaccounts_$index"} .
1809 # " || taxaccounts " . $form->{"taxaccounts"});
1811 $main::lxdebug->leave_sub(2);