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 #======================================================================
43 $main::lxdebug->enter_sub();
45 my ($self, $myconfig, $form) = @_;
48 my $dbh = $form->dbconnect($myconfig);
54 c1.accno AS inventory_accno,
55 c2.accno AS income_accno,
56 c3.accno AS expense_accno,
59 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
60 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
61 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
62 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
64 my $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
66 # copy to $form variables
67 map { $form->{$_} = $ref->{$_} } (keys %{$ref});
71 my %oid = ('Pg' => 'a.oid',
72 'Oracle' => 'a.rowid');
74 # part or service item
75 $form->{item} = ($form->{inventory_accno}) ? 'part' : 'service';
76 if ($form->{assembly}) {
77 $form->{item} = 'assembly';
79 # retrieve assembly items
81 qq|SELECT p.id, p.partnumber, p.description,
82 p.sellprice, p.lastcost, p.weight, a.qty, a.bom, p.unit,
85 JOIN assembly a ON (a.parts_id = p.id)
86 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
88 ORDER BY $oid{$myconfig->{dbdriver}}|;
89 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
91 $form->{assembly_rows} = 0;
92 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
93 $form->{assembly_rows}++;
94 foreach my $key (keys %{$ref}) {
95 $form->{"${key}_$form->{assembly_rows}"} = $ref->{$key};
102 # setup accno hash for <option checked> {amount} is used in create_links
103 $form->{amount}{IC} = $form->{inventory_accno};
104 $form->{amount}{IC_income} = $form->{income_accno};
105 $form->{amount}{IC_sale} = $form->{income_accno};
106 $form->{amount}{IC_expense} = $form->{expense_accno};
107 $form->{amount}{IC_cogs} = $form->{expense_accno};
109 my @pricegroups = ();
110 my @pricegroups_not_used = ();
114 qq|SELECT p.parts_id, p.pricegroup_id, p.price,
115 (SELECT pg.pricegroup
117 WHERE pg.id = p.pricegroup_id) AS pricegroup
120 ORDER BY pricegroup|;
121 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
125 while (($form->{"klass_$i"}, $form->{"pricegroup_id_$i"},
126 $form->{"price_$i"}, $form->{"pricegroup_$i"})
127 = $sth->fetchrow_array()) {
128 $form->{"price_$i"} = $form->round_amount($form->{"price_$i"}, 5);
129 $form->{"price_$i"} = $form->format_amount($myconfig, $form->{"price_$i"}, -2);
130 push @pricegroups, $form->{"pricegroup_id_$i"};
137 $query = qq|SELECT id, pricegroup FROM pricegroup|;
138 $form->{PRICEGROUPS} = selectall_hashref_query($form, $dbh, $query);
140 #find not used pricegroups
141 while ($tmp = pop(@{ $form->{PRICEGROUPS} })) {
143 foreach my $item (@pricegroups) {
144 if ($item eq $tmp->{id}) {
149 push(@pricegroups_not_used, $tmp) unless ($in_use);
152 # if not used pricegroups are avaible
153 if (@pricegroups_not_used) {
155 foreach $name (@pricegroups_not_used) {
156 $form->{"klass_$i"} = "$name->{id}";
157 $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
158 $form->{"price_$i"} = $form->format_amount($myconfig, $form->{"price_$i"}, -2);
159 $form->{"pricegroup_id_$i"} = "$name->{id}";
160 $form->{"pricegroup_$i"} = "$name->{pricegroup}";
166 $form->{price_rows} = $i - 1;
168 unless ($form->{item} eq 'service') {
171 if ($form->{makemodel}) {
172 $query = qq|SELECT m.make, m.model FROM makemodel m | .
173 qq|WHERE m.parts_id = ?|;
174 @values = ($form->{id});
175 $sth = $dbh->prepare($query);
176 $sth->execute(@values) || $form->dberror("$query (" . join(', ', @values) . ")");
179 while (($form->{"make_$i"}, $form->{"model_$i"}) = $sth->fetchrow_array)
184 $form->{makemodel_rows} = $i - 1;
190 $form->{language_values} = "";
191 $query = qq|SELECT language_id, translation, longdescription
194 my $trq = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
195 while (my $tr = $trq->fetchrow_hashref(NAME_lc)) {
196 $form->{language_values} .= "---+++---" . join('--++--', @{$tr}{qw(language_id translation longdescription)});
200 # now get accno for taxes
203 FROM chart c, partstax pt
204 WHERE (pt.chart_id = c.id) AND (pt.parts_id = ?)|;
205 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
206 while (($key) = $sth->fetchrow_array) {
207 $form->{amount}{$key} = $key;
213 my @referencing_tables = qw(invoice orderitems inventory rmaitems);
214 my %column_map = ( );
215 my $parts_id = conv_i($form->{id});
217 $form->{orphaned} = 1;
219 foreach my $table (@referencing_tables) {
220 my $column = $column_map{$table} || 'parts_id';
221 $query = qq|SELECT $column FROM $table WHERE $column = ? LIMIT 1|;
222 my ($found) = selectrow_query($form, $dbh, $query, $parts_id);
225 $form->{orphaned} = 0;
230 $form->{"unit_changeable"} = $form->{orphaned};
234 $main::lxdebug->leave_sub();
237 sub get_pricegroups {
238 $main::lxdebug->enter_sub();
240 my ($self, $myconfig, $form) = @_;
242 my $dbh = $form->dbconnect($myconfig);
245 my $query = qq|SELECT id, pricegroup FROM pricegroup|;
246 my $pricegroups = selectall_hashref_query($form, $dbh, $query);
249 foreach $pg (@{ $pricegroups }) {
250 $form->{"klass_$i"} = "$pg->{id}";
251 $form->{"price_$i"} = $form->format_amount($myconfig, $form->{"price_$i"}, -2);
252 $form->{"pricegroup_id_$i"} = "$pg->{id}";
253 $form->{"pricegroup_$i"} = "$pg->{pricegroup}";
258 $form->{price_rows} = $i - 1;
262 $main::lxdebug->leave_sub();
267 sub retrieve_buchungsgruppen {
268 $main::lxdebug->enter_sub();
270 my ($self, $myconfig, $form) = @_;
274 my $dbh = $form->dbconnect($myconfig);
276 # get buchungsgruppen
277 $query = qq|SELECT id, description FROM buchungsgruppen ORDER BY sortkey|;
278 $form->{BUCHUNGSGRUPPEN} = selectall_hashref_query($form, $dbh, $query);
280 $main::lxdebug->leave_sub();
284 $main::lxdebug->enter_sub();
286 my ($self, $myconfig, $form) = @_;
288 # connect to database, turn off AutoCommit
289 my $dbh = $form->dbconnect_noauto($myconfig);
292 # make up a unique handle and store in partnumber field
293 # then retrieve the record based on the unique handle to get the id
294 # replace the partnumber field with the actual variable
295 # add records for makemodel
297 # if there is a $form->{id} then replace the old entry
298 # delete all makemodel entries and add the new ones
300 # undo amount formatting
301 map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) }
302 qw(rop weight listprice sellprice gv lastcost);
304 my $makemodel = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
306 $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
313 $query = qq|SELECT sellprice, weight FROM parts WHERE id = ?|;
314 my ($sellprice, $weight) = selectrow_query($form, $dbh, $query, conv_i($form->{id}));
316 # if item is part of an assembly adjust all assemblies
317 $query = qq|SELECT id, qty FROM assembly WHERE parts_id = ?|;
318 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
319 while (my ($id, $qty) = $sth->fetchrow_array) {
320 &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
324 if ($form->{item} ne 'service') {
325 # delete makemodel records
326 do_query($form, $dbh, qq|DELETE FROM makemodel WHERE parts_id = ?|, conv_i($form->{id}));
329 if ($form->{item} eq 'assembly') {
330 # delete assembly records
331 do_query($form, $dbh, qq|DELETE FROM assembly WHERE id = ?|, conv_i($form->{id}));
335 do_query($form, $dbh, qq|DELETE FROM partstax WHERE parts_id = ?|, conv_i($form->{id}));
337 # delete translations
338 do_query($form, $dbh, qq|DELETE FROM translation WHERE parts_id = ?|, conv_i($form->{id}));
341 my ($count) = selectrow_query($form, $dbh, qq|SELECT COUNT(*) FROM parts WHERE partnumber = ?|, $form->{partnumber});
343 $main::lxdebug->leave_sub();
347 ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
348 do_query($form, $dbh, qq|INSERT INTO parts (id, partnumber) VALUES (?, '')|, $form->{id});
350 $form->{orphaned} = 1;
351 if ($form->{partnumber} eq "" && $form->{"item"} eq "service") {
352 $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
354 if ($form->{partnumber} eq "" && $form->{"item"} ne "service") {
355 $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
359 my $partsgroup_id = 0;
361 if ($form->{partsgroup}) {
362 ($partsgroup, $partsgroup_id) = split(/--/, $form->{partsgroup});
365 my ($subq_inventory, $subq_expense, $subq_income);
366 if ($form->{"item"} eq "part") {
368 qq|(SELECT bg.inventory_accno_id
369 FROM buchungsgruppen bg
370 WHERE bg.id = | . conv_i($form->{"buchungsgruppen_id"}, 'NULL') . qq|)|;
372 $subq_inventory = "NULL";
375 if ($form->{"item"} ne "assembly") {
377 qq|(SELECT bg.expense_accno_id_0
378 FROM buchungsgruppen bg
379 WHERE bg.id = | . conv_i($form->{"buchungsgruppen_id"}, 'NULL') . qq|)|;
381 $subq_expense = "NULL";
401 buchungsgruppen_id = ?,
403 inventory_accno_id = $subq_inventory,
404 income_accno_id = (SELECT bg.income_accno_id_0 FROM buchungsgruppen bg WHERE bg.id = ?),
405 expense_accno_id = $subq_expense,
413 not_discountable = ?,
418 @values = ($form->{partnumber},
419 $form->{description},
420 $makemodel ? 't' : 'f',
421 $form->{assembly} ? 't' : 'f',
426 conv_date($form->{priceupdate}),
432 conv_i($form->{buchungsgruppen_id}),
433 conv_i($form->{payment_id}),
434 conv_i($form->{buchungsgruppen_id}),
435 $form->{obsolete} ? 't' : 'f',
438 $form->{shop} ? 't' : 'f',
442 $form->{not_discountable} ? 't' : 'f',
444 conv_i($partsgroup_id),
445 conv_i($form->{price_factor_id}),
448 do_query($form, $dbh, $query, @values);
450 # delete translation records
451 do_query($form, $dbh, qq|DELETE FROM translation WHERE parts_id = ?|, conv_i($form->{id}));
453 if ($form->{language_values} ne "") {
454 foreach $item (split(/---\+\+\+---/, $form->{language_values})) {
455 my ($language_id, $translation, $longdescription) = split(/--\+\+--/, $item);
456 if ($translation ne "") {
457 $query = qq|INSERT into translation (parts_id, language_id, translation, longdescription)
458 VALUES ( ?, ?, ?, ? )|;
459 @values = (conv_i($form->{id}), conv_i($language_id), $translation, $longdescription);
460 do_query($form, $dbh, $query, @values);
465 # delete price records
466 do_query($form, $dbh, qq|DELETE FROM prices WHERE parts_id = ?|, conv_i($form->{id}));
468 # insert price records only if different to sellprice
469 for my $i (1 .. $form->{price_rows}) {
470 if ($form->{"price_$i"} eq "0") {
471 $form->{"price_$i"} = $form->{sellprice};
474 ( $form->{"price_$i"}
475 || $form->{"klass_$i"}
476 || $form->{"pricegroup_id_$i"})
477 and $form->{"price_$i"} != $form->{sellprice}
479 #$klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
480 $price = $form->parse_amount($myconfig, $form->{"price_$i"});
482 $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
483 $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price) | .
485 @values = (conv_i($form->{id}), conv_i($pricegroup_id), $price);
486 do_query($form, $dbh, $query, @values);
490 # insert makemodel records
491 unless ($form->{item} eq 'service') {
492 for my $i (1 .. $form->{makemodel_rows}) {
493 if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
495 $query = qq|INSERT INTO makemodel (parts_id, make, model) | .
496 qq|VALUES (?, ?, ?)|;
497 @values = (conv_i($form->{id}), conv_i($form->{"make_$i"}), $form->{"model_$i"});
499 do_query($form, $dbh, $query, @values);
505 foreach $item (split(/ /, $form->{taxaccounts})) {
506 if ($form->{"IC_tax_$item"}) {
508 qq|INSERT INTO partstax (parts_id, chart_id)
509 VALUES (?, (SELECT id FROM chart WHERE accno = ?))|;
510 @values = (conv_i($form->{id}), $item);
511 do_query($form, $dbh, $query, @values);
515 # add assembly records
516 if ($form->{item} eq 'assembly') {
518 for my $i (1 .. $form->{assembly_rows}) {
519 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
521 if ($form->{"qty_$i"} != 0) {
522 $form->{"bom_$i"} *= 1;
523 $query = qq|INSERT INTO assembly (id, parts_id, qty, bom) | .
524 qq|VALUES (?, ?, ?, ?)|;
525 @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}), conv_i($form->{"qty_$i"}), $form->{"bom_$i"} ? 't' : 'f');
526 do_query($form, $dbh, $query, @values);
533 my $shippingdate = "$a[5]-$a[4]-$a[3]";
535 $form->get_employee($dbh);
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;
549 qq|SELECT c.accno, c.description, t.rate, t.taxnumber
551 WHERE (c.id = t.chart_id) AND (t.taxkey IN (SELECT taxkey_id FROM chart where accno = ?))
553 $stw = prepare_execute_query($form, $dbh, $query, $accno_id);
555 $form->{taxaccount} = "";
556 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
557 $form->{taxaccount} .= "$ptr->{accno} ";
558 if (!($form->{taxaccount2} =~ /\Q$ptr->{accno}\E/)) {
559 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
560 $form->{"$ptr->{accno}_description"} = $ptr->{description};
561 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
562 $form->{taxaccount2} .= " $ptr->{accno} ";
567 my $rc = $dbh->commit;
570 $main::lxdebug->leave_sub();
575 sub update_assembly {
576 $main::lxdebug->enter_sub();
578 my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
580 my $query = qq|SELECT id, qty FROM assembly WHERE parts_id = ?|;
581 my $sth = prepare_execute_query($form, $dbh, $query, conv_i($id));
583 while (my ($pid, $aqty) = $sth->fetchrow_array) {
584 &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
589 qq|UPDATE parts SET sellprice = sellprice + ?, weight = weight + ?
591 @values = ($qty * ($form->{sellprice} - $sellprice),
592 $qty * ($form->{weight} - $weight), conv_i($id));
593 do_query($form, $dbh, $query, @values);
595 $main::lxdebug->leave_sub();
598 sub retrieve_assemblies {
599 $main::lxdebug->enter_sub();
601 my ($self, $myconfig, $form) = @_;
603 # connect to database
604 my $dbh = $form->dbconnect($myconfig);
606 my $where = qq|NOT p.obsolete|;
609 if ($form->{partnumber}) {
610 $where .= qq| AND (p.partnumber ILIKE ?)|;
611 push(@values, '%' . $form->{partnumber} . '%');
614 if ($form->{description}) {
615 $where .= qq| AND (p.description ILIKE ?)|;
616 push(@values, '%' . $form->{description} . '%');
619 # retrieve assembly items
621 qq|SELECT p.id, p.partnumber, p.description,
622 p.bin, p.onhand, p.rop,
623 (SELECT sum(p2.inventory_accno_id)
624 FROM parts p2, assembly a
625 WHERE (p2.id = a.parts_id) AND (a.id = p.id)) AS inventory
627 WHERE NOT p.obsolete AND p.assembly $where|;
629 $form->{assembly_items} = selectall_hashref_query($form, $dbh, $query, @values);
633 $main::lxdebug->leave_sub();
637 $main::lxdebug->enter_sub();
639 my ($self, $myconfig, $form) = @_;
640 my @values = (conv_i($form->{id}));
641 # connect to database, turn off AutoCommit
642 my $dbh = $form->dbconnect_noauto($myconfig);
644 my %columns = ( "assembly" => "id", "parts" => "id" );
646 for my $table (qw(prices partstax makemodel inventory assembly license translation parts)) {
647 my $column = defined($columns{$table}) ? $columns{$table} : "parts_id";
648 do_query($form, $dbh, qq|DELETE FROM $table WHERE $column = ?|, @values);
652 my $rc = $dbh->commit;
655 $main::lxdebug->leave_sub();
661 $main::lxdebug->enter_sub();
663 my ($self, $myconfig, $form) = @_;
665 my $i = $form->{assembly_rows};
667 my $where = qq|1 = 1|;
670 my %columns = ("partnumber" => "p", "description" => "p", "partsgroup" => "pg");
672 while (my ($column, $table) = each(%columns)) {
673 next unless ($form->{"${column}_$i"});
674 $where .= qq| AND ${table}.${column} ILIKE ?|;
675 push(@values, '%' . $form->{"${column}_$i"} . '%');
679 $where .= qq| AND NOT (p.id = ?)|;
680 push(@values, conv_i($form->{id}));
684 $where .= qq| ORDER BY p.partnumber|;
686 $where .= qq| ORDER BY p.description|;
689 # connect to database
690 my $dbh = $form->dbconnect($myconfig);
693 qq|SELECT p.id, p.partnumber, p.description, p.sellprice, p.weight, p.onhand, p.unit, pg.partsgroup
695 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
697 $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
701 $main::lxdebug->leave_sub();
706 # Warning, deep magic ahead.
707 # This function gets all parts from the database according to the filters specified
710 # sort revers - sorting field + direction
713 # simple filter strings (every one of those also has a column flag prefixed with 'l_' associated):
714 # partnumber ean description partsgroup microfiche drawing
717 # 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
720 # itemstatus = active | onhand | short | obsolete | orphaned
721 # searchitems = part | assembly | service
724 # make model - makemodel
725 # serialnumber transdatefrom transdateto - invoice/orderitems
728 # bought sold onorder ordered rfq quoted - aggreg joins with invoices/orders
729 # l_linetotal l_subtotal - aggreg joins to display totals (complicated) - NOT IMPLEMENTED here, implementation at frontend
730 # l_soldtotal - aggreg join to display total of sold quantity
731 # onhand - as above, but masking the simple itemstatus results (doh!)
732 # short - NOT IMPLEMENTED as form filter, only as itemstatus option
733 # l_serialnumber - belonges to serialnumber filter
734 # l_deliverydate - displays deliverydate is sold etc. flags are active
735 # l_soldtotal - aggreg join to display total of sold quantity, works as long as there's no bullshit in soldtotal
738 # onhand - as above, but masking the simple itemstatus results (doh!)
739 # masking of onhand in bsooqr mode - ToDO: fixme
741 # disabled sanity checks and changes:
742 # - searchitems = assembly will no longer disable bought
743 # - searchitems = service will no longer disable make and model, although services don't have make/model, it doesn't break the query
744 # - itemstatus = orphaned will no longer disable onhand short bought sold onorder ordered rfq quoted transdate[from|to]
745 # - itemstatus = obsolete will no longer disable onhand, short
746 # - allow sorting by ean
747 # - serialnumber filter also works if l_serialnumber isn't ticked
748 # - onhand doesn't get masked by it's oi or invoice counterparts atm. ToDO: fix this
749 # - sorting will now change sorting if the requested sorting column isn't checked and doesn't get checked as a side effect
752 $main::lxdebug->enter_sub();
754 my ($self, $myconfig, $form) = @_;
755 my $dbh = $form->get_standard_dbh($myconfig);
757 $form->{parts} = +{ };
758 $form->{soldtotal} = undef if $form->{l_soldtotal}; # security fix. top100 insists on putting strings in there...
760 my @simple_filters = qw(partnumber ean description partsgroup microfiche drawing onhand);
761 my @makemodel_filters = qw(make model);
762 my @invoice_oi_filters = qw(serialnumber soldtotal);
763 my @apoe_filters = qw(transdate);
764 my @all_columns = (@simple_filters, @makemodel_filters, @apoe_filters, qw(serialnumber));
765 my @simple_l_switches = (@all_columns, qw(listprice sellprice lastcost priceupdate weight unit bin rop image));
766 my @oe_flags = qw(bought sold onorder ordered rfq quoted);
767 my @qsooqr_flags = qw(invnumber ordnumber quonumber trans_id name module);
768 my @deliverydate_flags = qw(deliverydate);
769 # my @other_flags = qw(onhand); # ToDO: implement these
770 # my @inactive_flags = qw(l_subtotal short l_linetotal);
773 partsgroup => 'LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)',
774 makemodel => 'LEFT JOIN makemodel mm ON (mm.parts_id = p.id)',
775 pfac => 'LEFT JOIN price_factors pfac ON (pfac.id = p.price_factor_id)',
778 SELECT parts_id, description, serialnumber, trans_id, unit, sellprice, qty, assemblyitem, 'invoice' AS ioi FROM invoice UNION
779 SELECT parts_id, description, serialnumber, trans_id, unit, sellprice, qty, FALSE AS assemblyitem, 'orderitems' AS ioi FROM orderitems
780 ) AS ioi ON ioi.parts_id = p.id|,
783 SELECT id, transdate, 'ir' AS module, ordnumber, quonumber, invnumber, FALSE AS quotation, NULL AS customer_id, vendor_id, NULL AS deliverydate, 'invoice' AS ioi FROM ap UNION
784 SELECT id, transdate, 'is' AS module, ordnumber, quonumber, invnumber, FALSE AS quotation, customer_id, NULL AS vendor_id, deliverydate, 'invoice' AS ioi FROM ar UNION
785 SELECT id, transdate, 'oe' AS module, ordnumber, quonumber, NULL AS invnumber, quotation, customer_id, vendor_id, NULL AS deliverydate, 'orderitems' AS ioi FROM oe
786 ) AS apoe ON ((ioi.trans_id = apoe.id) AND (ioi.ioi = apoe.ioi))|,
789 SELECT id, name, 'customer' AS cv FROM customer UNION
790 SELECT id, name, 'vendor' AS cv FROM vendor
791 ) AS cv ON cv.id = apoe.customer_id OR cv.id = apoe.vendor_id|,
793 my @join_order = qw(partsgroup makemodel invoice_oi apoe cv pfac);
796 if (($form->{searchitems} eq 'assembly') && $form->{l_lastcost}) {
797 @simple_l_switches = grep { $_ ne 'lastcost' } @simple_l_switches;
800 #===== switches and simple filters ========#
802 my @select_tokens = qw(id factor);
803 my @where_tokens = qw(1=1);
804 my @group_tokens = ();
806 # special case transdate
807 if (grep { $form->{$_} } qw(transdatefrom transdateto)) {
808 $form->{"l_transdate"} = 1;
809 push @select_tokens, 'transdate';
810 for (qw(transdatefrom transdateto)) {
811 next unless $form->{$_};
812 push @where_tokens, sprintf "transdate %s ?", /from$/ ? '>=' : '<=';
813 push @bind_vars, $form->{$_};
817 my %simple_filter_table_prefix = (
821 foreach (@simple_filters, @makemodel_filters, @invoice_oi_filters) {
822 next unless $form->{$_};
823 $form->{"l_$_"} = '1'; # show the column
824 push @where_tokens, "$simple_filter_table_prefix{$_}$_ ILIKE ?";
825 push @bind_vars, "%$form->{$_}%";
828 foreach (@simple_l_switches) {
829 next unless $form->{"l_$_"};
830 push @select_tokens, $_;
833 for ($form->{searchitems}) {
834 push @where_tokens, 'p.inventory_accno_id > 0' if /part/;
835 push @where_tokens, 'p.inventory_accno_id IS NULL' if /service/;
836 push @where_tokens, 'NOT p.assembly' if /service/;
837 push @where_tokens, ' p.assembly' if /assembly/;
840 for ($form->{itemstatus}) {
841 push @where_tokens, 'p.id NOT IN
842 (SELECT DISTINCT parts_id FROM invoice UNION
843 SELECT DISTINCT parts_id FROM assembly UNION
844 SELECT DISTINCT parts_id FROM orderitems)' if /orphaned/;
845 push @where_tokens, 'p.onhand = 0' if /orphaned/;
846 push @where_tokens, 'NOT p.obsolete' if /active/;
847 push @where_tokens, ' p.obsolete', if /obsolete/;
848 push @where_tokens, 'p.onhand > 0', if /onhand/;
849 push @where_tokens, 'p.onhand < p.rop', if /short/;
852 my $q_assembly_lastcost =
853 qq|(SELECT SUM(a_lc.qty * p_lc.lastcost / COALESCE(pfac_lc.factor, 1))
855 LEFT JOIN parts p_lc ON (a_lc.parts_id = p_lc.id)
856 LEFT JOIN price_factors pfac_lc ON (p_lc.price_factor_id = pfac_lc.id)
857 WHERE (a_lc.id = p.id)) AS lastcost|;
859 my @sort_cols = (@simple_filters, qw(id bin priceupdate onhand invnumber ordnumber quonumber name serialnumber soldtotal deliverydate));
860 $form->{sort} = 'id' unless grep { $form->{"l_$_"} } grep { $form->{sort} eq $_ } @sort_cols;
862 my $sort_order = ($form->{revers} ? ' DESC' : ' ASC');
864 # special case: sorting by partnumber
865 # since partnumbers are expected to be prefixed integers, a special sorting is implemented sorting first lexically by prefix and then by suffix.
866 # and yes, that expression is designed to hold that array of regexes only once, so the map is kinda messy, sorry about that.
867 # ToDO: implement proper functional sorting
868 $form->{sort} = join ', ', map { push @select_tokens, $_; ($table_prefix{$_} = "substring(partnumber,'[") . $_ } qw|^[:digit:]]+') [:digit:]]+')::INTEGER|
869 if $form->{sort} eq 'partnumber';
871 my $order_clause = " ORDER BY $form->{sort} $sort_order";
873 my $limit_clause = " LIMIT 100" if $form->{top100};
875 #=== joins and complicated filters ========#
877 my $bsooqr = $form->{bought} || $form->{sold}
878 || $form->{ordered} || $form->{onorder}
879 || $form->{quoted} || $form->{rfq};
882 push @select_tokens, @qsooqr_flags if $bsooqr;
883 push @select_tokens, @deliverydate_flags if $bsooqr && $form->{l_deliverydate};
884 push @select_tokens, $q_assembly_lastcost if ($form->{searchitems} eq 'assembly') && $form->{l_lastcost};
885 push @bsooqr_tokens, q|module = 'ir' AND NOT ioi.assemblyitem| if $form->{bought};
886 push @bsooqr_tokens, q|module = 'is' AND NOT ioi.assemblyitem| if $form->{sold};
887 push @bsooqr_tokens, q|module = 'oe' AND NOT quotation AND cv = 'customer'| if $form->{ordered};
888 push @bsooqr_tokens, q|module = 'oe' AND NOT quotation AND cv = 'vendor'| if $form->{onorder};
889 push @bsooqr_tokens, q|module = 'oe' AND quotation AND cv = 'customer'| if $form->{quoted};
890 push @bsooqr_tokens, q|module = 'oe' AND quotation AND cv = 'vendor'| if $form->{rfq};
891 push @where_tokens, join ' OR ', map { "($_)" } @bsooqr_tokens if $bsooqr;
893 $joins_needed{partsgroup} = 1;
894 $joins_needed{pfac} = 1;
895 $joins_needed{makemodel} = 1 if grep { $form->{$_} || $form->{"l_$_"} } @makemodel_filters;
896 $joins_needed{cv} = 1 if $bsooqr;
897 $joins_needed{apoe} = 1 if $joins_needed{cv} || grep { $form->{$_} || $form->{"l_$_"} } @apoe_filters;
898 $joins_needed{invoice_oi} = 1 if $joins_needed{apoe} || grep { $form->{$_} || $form->{"l_$_"} } @invoice_oi_filters;
900 # special case for description search.
901 # up in the simple filter section the description filter got interpreted as something like: WHERE description ILIKE '%$form->{description}%'
902 # now we'd like to search also for the masked description entered in orderitems and invoice, so...
903 # find the old entries in of @where_tokens and @bind_vars, and adjust them
904 if ($joins_needed{invoice_oi}) {
905 for (my ($wi, $bi) = (0)x2; $wi <= $#where_tokens; $bi++ if $where_tokens[$wi++] =~ /\?/) {
906 next unless $where_tokens[$wi] =~ /^description ILIKE/;
907 splice @where_tokens, $wi, 1, 'p.description ILIKE ? OR ioi.description ILIKE ?';
908 splice @bind_vars, $bi, 0, $bind_vars[$bi];
913 # now the master trick: soldtotal.
914 if ($form->{l_soldtotal}) {
915 push @where_tokens, 'ioi.qty >= 0';
916 push @group_tokens, @select_tokens;
917 map { s/.*\sAS\s+//si } @group_tokens;
918 push @select_tokens, 'SUM(ioi.qty)';
921 #============= build query ================#
925 deliverydate => 'apoe.', serialnumber => 'ioi.',
926 transdate => 'apoe.', trans_id => 'ioi.',
927 module => 'apoe.', name => 'cv.',
928 ordnumber => 'apoe.', make => 'mm.',
929 quonumber => 'apoe.', model => 'mm.',
930 invnumber => 'apoe.', partsgroup => 'pg.',
933 'SUM(ioi.qty)' => ' ',
936 $table_prefix{$q_assembly_lastcost} = ' ';
938 my %renamed_columns = (
939 'factor' => 'price_factor',
940 'SUM(ioi.qty)' => 'soldtotal',
943 map { $table_prefix{$_} = 'ioi.' } qw(description serialnumber qty unit) if $joins_needed{invoice_oi};
944 map { $renamed_columns{$_} = ' AS ' . $renamed_columns{$_} } keys %renamed_columns;
946 my $select_clause = join ', ', map { ($table_prefix{$_} || "p.") . $_ . $renamed_columns{$_} } @select_tokens;
947 my $join_clause = join ' ', @joins{ grep $joins_needed{$_}, @join_order };
948 my $where_clause = join ' AND ', map { "($_)" } @where_tokens;
949 my $group_clause = ' GROUP BY ' . join ', ', map { ($table_prefix{$_} || "p.") . $_ } @group_tokens if scalar @group_tokens;
951 my $query = qq|SELECT DISTINCT $select_clause FROM parts p $join_clause WHERE $where_clause $group_clause $order_clause $limit_clause|;
953 $form->{parts} = selectall_hashref_query($form, $dbh, $query, @bind_vars);
955 map { $_->{onhand} *= 1 } @{ $form->{parts} };
958 # include individual items for assemblies
959 if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
961 qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
963 p.sellprice, p.listprice, p.lastcost,
964 p.rop, p.weight, p.priceupdate,
965 p.image, p.drawing, p.microfiche
966 FROM parts p, assembly a
967 WHERE (p.id = a.parts_id) AND (a.id = ?)|;
968 $sth = prepare_query($form, $dbh, $query);
970 foreach $item (@{ $form->{parts} }) {
971 push(@assemblies, $item);
972 do_statement($form, $sth, $query, conv_i($item->{id}));
974 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
975 $ref->{assemblyitem} = 1;
976 push(@assemblies, $ref);
981 # copy assemblies to $form->{parts}
982 $form->{parts} = \@assemblies;
985 $main::lxdebug->leave_sub();
989 $main::lxdebug->enter_sub();
991 my ($self, $myconfig, $form) = @_;
999 if ($item ne 'make') {
1000 foreach my $item (qw(partnumber drawing microfiche make model pg.partsgroup)) {
1002 $column =~ s/.*\.//;
1003 next unless ($form->{$column});
1004 $where .= qq| AND $item ILIKE ?|;
1005 push(@where_values, '%' . $form->{$column} . '%');
1009 # special case for description
1010 if ($form->{description}
1011 && !( $form->{bought} || $form->{sold} || $form->{onorder}
1012 || $form->{ordered} || $form->{rfq} || $form->{quoted})) {
1013 $where .= qq| AND (p.description ILIKE ?)|;
1014 push(@where_values, '%' . $form->{description} . '%');
1017 # special case for serialnumber
1018 if ($form->{l_serialnumber} && $form->{serialnumber}) {
1019 $where .= qq| AND serialnumber ILIKE ?|;
1020 push(@where_values, '%' . $form->{serialnumber} . '%');
1024 # items which were never bought, sold or on an order
1025 if ($form->{itemstatus} eq 'orphaned') {
1026 $form->{onhand} = $form->{short} = 0;
1027 $form->{bought} = $form->{sold} = 0;
1028 $form->{onorder} = $form->{ordered} = 0;
1029 $form->{rfq} = $form->{quoted} = 0;
1031 $form->{transdatefrom} = $form->{transdateto} = "";
1034 qq| AND (p.onhand = 0)
1037 SELECT DISTINCT parts_id FROM invoice
1039 SELECT DISTINCT parts_id FROM assembly
1041 SELECT DISTINCT parts_id FROM orderitems
1045 if ($form->{itemstatus} eq 'active') {
1046 $where .= qq| AND p.obsolete = '0'|;
1049 if ($form->{itemstatus} eq 'obsolete') {
1050 $where .= qq| AND p.obsolete = '1'|;
1051 $form->{onhand} = $form->{short} = 0;
1054 if ($form->{itemstatus} eq 'onhand') {
1055 $where .= qq| AND p.onhand > 0|;
1058 if ($form->{itemstatus} eq 'short') {
1059 $where .= qq| AND p.onhand < p.rop|;
1062 foreach my $column (qw(make model)) {
1063 next unless ($form->{$colum});
1064 $where .= qq| AND p.id IN (SELECT DISTINCT parts_id FROM makemodel WHERE $column ILIKE ?|;
1065 push(@where_values, '%' . $form->{$column} . '%');
1068 # connect to database
1069 my $dbh = $form->dbconnect_noauto($myconfig);
1071 for my $column (qw(sellprice listprice)) {
1072 next if ($form->{$column} eq "");
1074 my $value = $form->parse_amount($myconfig, $form->{$column});
1077 if ($form->{"${column}_type"} eq "percent") {
1078 $value = ($value / 100) + 1;
1083 qq|UPDATE parts SET $column = $column $operator ?
1087 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1089 do_query($from, $dbh, $query, $value, @where_values);
1093 qq|UPDATE prices SET price = price + ?
1097 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1098 WHERE $where) AND (pricegroup_id = ?)|;
1099 my $sth_add = prepare_query($form, $dbh, $q_add);
1102 qq|UPDATE prices SET price = price * ?
1106 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1107 WHERE $where) AND (pricegroup_id = ?)|;
1108 my $sth_multiply = prepare_query($form, $dbh, $q_multiply);
1110 for my $i (1 .. $form->{price_rows}) {
1111 next if ($form->{"price_$i"} eq "");
1113 my $value = $form->parse_amount($myconfig, $form->{"price_$i"});
1115 if ($form->{"pricegroup_type_$i"} eq "percent") {
1116 do_statement($form, $sth_multiply, $q_multiply, ($value / 100) + 1, @where_values, conv_i($form->{"pricegroup_id_$i"}));
1118 do_statement($form, $sth_add, $q_add, $value, @where_values, conv_i($form->{"pricegroup_id_$i"}));
1123 $sth_multiply->finish();
1125 my $rc= $dbh->commit;
1128 $main::lxdebug->leave_sub();
1134 $main::lxdebug->enter_sub();
1136 my ($self, $module, $myconfig, $form) = @_;
1138 # connect to database
1139 my $dbh = $form->dbconnect($myconfig);
1141 my @values = ('%' . $module . '%');
1145 qq|SELECT c.accno, c.description, c.link, c.id,
1146 p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1147 FROM chart c, parts p
1148 WHERE (c.link LIKE ?) AND (p.id = ?)
1150 push(@values, conv_i($form->{id}));
1154 qq|SELECT c.accno, c.description, c.link, c.id,
1155 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1156 FROM chart c, defaults d
1161 my $sth = prepare_execute_query($form, $dbh, $query, @values);
1162 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1163 foreach my $key (split(/:/, $ref->{link})) {
1164 if ($key =~ /\Q$module\E/) {
1165 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1166 || ($ref->{id} eq $ref->{income_accno_id})
1167 || ($ref->{id} eq $ref->{expense_accno_id})) {
1168 push @{ $form->{"${module}_links"}{$key} },
1169 { accno => $ref->{accno},
1170 description => $ref->{description},
1171 selected => "selected" };
1172 $form->{"${key}_default"} = "$ref->{accno}--$ref->{description}";
1174 push @{ $form->{"${module}_links"}{$key} },
1175 { accno => $ref->{accno},
1176 description => $ref->{description},
1184 # get buchungsgruppen
1185 $form->{BUCHUNGSGRUPPEN} = selectall_hashref_query($form, $dbh, qq|SELECT id, description FROM buchungsgruppen|);
1188 $form->{payment_terms} = selectall_hashref_query($form, $dbh, qq|SELECT id, description FROM payment_terms ORDER BY sortkey|);
1191 ($form->{priceupdate}) = selectrow_query($form, $dbh, qq|SELECT current_date|);
1195 $main::lxdebug->leave_sub();
1198 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1200 $main::lxdebug->enter_sub();
1202 my ($self, $myconfig, $form, $sortorder) = @_;
1203 my $dbh = $form->dbconnect($myconfig);
1204 my $order = qq| p.partnumber|;
1205 my $where = qq|1 = 1|;
1208 if ($sortorder eq "all") {
1209 $where .= qq| AND (partnumber ILIKE ?) AND (description ILIKE ?)|;
1210 push(@values, '%' . $form->{partnumber} . '%', '%' . $form->{description} . '%');
1212 } elsif ($sortorder eq "partnumber") {
1213 $where .= qq| AND (partnumber ILIKE ?)|;
1214 push(@values, '%' . $form->{partnumber} . '%');
1216 } elsif ($sortorder eq "description") {
1217 $where .= qq| AND (description ILIKE ?)|;
1218 push(@values, '%' . $form->{description} . '%');
1219 $order = "description";
1224 qq|SELECT id, partnumber, description, unit, sellprice
1226 WHERE $where ORDER BY $order|;
1228 my $sth = prepare_execute_query($form, $dbh, $query, @values);
1231 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1232 if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1237 $form->{"id_$j"} = $ref->{id};
1238 $form->{"partnumber_$j"} = $ref->{partnumber};
1239 $form->{"description_$j"} = $ref->{description};
1240 $form->{"unit_$j"} = $ref->{unit};
1241 $form->{"sellprice_$j"} = $ref->{sellprice};
1242 $form->{"soldtotal_$j"} = get_soldtotal($dbh, $ref->{id});
1248 $main::lxdebug->leave_sub();
1253 # gets sum of sold part with part_id
1255 $main::lxdebug->enter_sub();
1257 my ($dbh, $id) = @_;
1259 my $query = qq|SELECT sum(qty) FROM invoice WHERE parts_id = ?|;
1260 my ($sum) = selectrow_query($form, $dbh, $query, conv_i($id));
1263 $main::lxdebug->leave_sub();
1266 } #end get_soldtotal
1268 sub retrieve_languages {
1269 $main::lxdebug->enter_sub();
1271 my ($self, $myconfig, $form) = @_;
1273 # connect to database
1274 my $dbh = $form->dbconnect($myconfig);
1279 if ($form->{language_values} ne "") {
1281 qq|SELECT l.id, l.description, tr.translation, tr.longdescription
1283 LEFT OUTER JOIN translation tr ON (tr.language_id = l.id) AND (tr.parts_id = ?)
1284 ORDER BY lower(l.description)|;
1285 @values = (conv_i($form->{id}));
1288 $query = qq|SELECT id, description
1290 ORDER BY lower(description)|;
1293 my $languages = selectall_hashref_query($form, $dbh, $query, @values);
1297 $main::lxdebug->leave_sub();
1302 sub follow_account_chain {
1303 $main::lxdebug->enter_sub(2);
1305 my ($self, $form, $dbh, $transdate, $accno_id, $accno) = @_;
1307 my @visited_accno_ids = ($accno_id);
1312 qq|SELECT c.new_chart_id, date($transdate) >= c.valid_from AS is_valid, | .
1315 qq|LEFT JOIN chart cnew ON c.new_chart_id = cnew.id | .
1316 qq|WHERE (c.id = ?) AND NOT c.new_chart_id ISNULL AND (c.new_chart_id > 0)|;
1317 $sth = prepare_query($form, $dbh, $query);
1320 do_statement($form, $sth, $query, $accno_id);
1321 $ref = $sth->fetchrow_hashref();
1322 last unless ($ref && $ref->{"is_valid"} &&
1323 !grep({ $_ == $ref->{"new_chart_id"} } @visited_accno_ids));
1324 $accno_id = $ref->{"new_chart_id"};
1325 $accno = $ref->{"accno"};
1326 push(@visited_accno_ids, $accno_id);
1329 $main::lxdebug->leave_sub(2);
1331 return ($accno_id, $accno);
1334 sub retrieve_accounts {
1335 $main::lxdebug->enter_sub(2);
1337 my ($self, $myconfig, $form, $parts_id, $index) = @_;
1339 my ($query, $sth, $dbh);
1341 $form->{"taxzone_id"} *= 1;
1343 $dbh = $form->get_standard_dbh($myconfig);
1346 if ($form->{type} eq "invoice") {
1347 if (($form->{vc} eq "vendor") || !$form->{deliverydate}) {
1348 $transdate = $form->{invdate};
1350 $transdate = $form->{deliverydate};
1352 } elsif (($form->{type} eq "credit_note") || ($form->{script} eq 'ir.pl')) {
1353 $transdate = $form->{invdate};
1355 $transdate = $form->{transdate};
1358 if ($transdate eq "") {
1359 $transdate = "current_date";
1361 $transdate = $dbh->quote($transdate);
1366 qq| p.inventory_accno_id AS is_part, | .
1367 qq| bg.inventory_accno_id, | .
1368 qq| bg.income_accno_id_$form->{taxzone_id} AS income_accno_id, | .
1369 qq| bg.expense_accno_id_$form->{taxzone_id} AS expense_accno_id, | .
1370 qq| c1.accno AS inventory_accno, | .
1371 qq| c2.accno AS income_accno, | .
1372 qq| c3.accno AS expense_accno | .
1374 qq|LEFT JOIN buchungsgruppen bg ON p.buchungsgruppen_id = bg.id | .
1375 qq|LEFT JOIN chart c1 ON bg.inventory_accno_id = c1.id | .
1376 qq|LEFT JOIN chart c2 ON bg.income_accno_id_$form->{taxzone_id} = c2.id | .
1377 qq|LEFT JOIN chart c3 ON bg.expense_accno_id_$form->{taxzone_id} = c3.id | .
1379 my $ref = selectfirst_hashref_query($form, $dbh, $query, $parts_id);
1381 return $main::lxdebug->leave_sub(2) if (!$ref);
1383 $ref->{"inventory_accno_id"} = undef unless ($ref->{"is_part"});
1386 foreach my $type (qw(inventory income expense)) {
1387 next unless ($ref->{"${type}_accno_id"});
1388 ($accounts{"${type}_accno_id"}, $accounts{"${type}_accno"}) =
1389 $self->follow_account_chain($form, $dbh, $transdate,
1390 $ref->{"${type}_accno_id"},
1391 $ref->{"${type}_accno"});
1394 map({ $form->{"${_}_accno_$index"} = $accounts{"${_}_accno"} }
1395 qw(inventory income expense));
1397 my $inc_exp = $form->{"vc"} eq "customer" ? "income" : "expense";
1398 my $accno_id = $accounts{"${inc_exp}_accno_id"};
1401 qq|SELECT c.accno, t.taxdescription AS description, t.rate, t.taxnumber | .
1403 qq|LEFT JOIN chart c ON c.id = t.chart_id | .
1404 qq|WHERE t.id IN | .
1405 qq| (SELECT tk.tax_id | .
1406 qq| FROM taxkeys tk | .
1407 qq| WHERE tk.chart_id = ? AND startdate <= | . quote_db_date($transdate) .
1408 qq| ORDER BY startdate DESC LIMIT 1) |;
1409 $ref = selectfirst_hashref_query($form, $dbh, $query, $accno_id);
1412 $main::lxdebug->leave_sub(2);
1416 $form->{"taxaccounts_$index"} = $ref->{"accno"};
1417 if ($form->{"taxaccounts"} !~ /$ref->{accno}/) {
1418 $form->{"taxaccounts"} .= "$ref->{accno} ";
1420 map({ $form->{"$ref->{accno}_${_}"} = $ref->{$_}; }
1421 qw(rate description taxnumber));
1423 # $main::lxdebug->message(0, "formvars: rate " . $form->{"$ref->{accno}_rate"} .
1424 # " description " . $form->{"$ref->{accno}_description"} .
1425 # " taxnumber " . $form->{"$ref->{accno}_taxnumber"} .
1426 # " || taxaccounts_$index " . $form->{"taxaccounts_$index"} .
1427 # " || taxaccounts " . $form->{"taxaccounts"});
1429 $main::lxdebug->leave_sub(2);
1432 sub get_basic_part_info {
1433 $main::lxdebug->enter_sub();
1438 Common::check_params(\%params, qw(id));
1440 my @ids = 'ARRAY' eq ref $params{id} ? @{ $params{id} } : ($params{id});
1443 $main::lxdebug->leave_sub();
1447 my $myconfig = \%main::myconfig;
1448 my $form = $main::form;
1450 my $dbh = $form->get_standard_dbh($myconfig);
1452 my $query = qq|SELECT id, partnumber, description, unit FROM parts WHERE id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)|;
1454 my $info = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
1456 if ('' eq ref $params{id}) {
1457 $info = $info->[0] || { };
1459 $main::lxdebug->leave_sub();
1463 my %info_map = map { $_->{id} => $_ } @{ $info };
1465 $main::lxdebug->leave_sub();
1470 sub prepare_parts_for_printing {
1471 $main::lxdebug->enter_sub();
1476 my $myconfig = \%main::myconfig;
1477 my $form = $main::form;
1479 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1481 my $prefix = $params{prefix} || 'id_';
1482 my $rowcount = defined $params{rowcount} ? $params{rowcount} : $form->{rowcount};
1484 my @part_ids = keys %{ { map { $_ => 1 } grep { $_ } map { $form->{"${prefix}${_}"} } (1 .. $rowcount) } };
1487 $main::lxdebug->leave_sub();
1491 my $placeholders = join ', ', ('?') x scalar(@part_ids);
1492 my $query = qq|SELECT mm.parts_id, mm.model, v.name AS make
1494 LEFT JOIN vendor v ON (mm.make = cast (v.id as text))
1495 WHERE mm.parts_id IN ($placeholders)|;
1499 my $sth = prepare_execute_query($form, $dbh, $query, @part_ids);
1501 while (my $ref = $sth->fetchrow_hashref()) {
1502 $makemodel{$ref->{parts_id}} ||= [];
1503 push @{ $makemodel{$ref->{parts_id}} }, $ref;
1508 my @columns = qw(ean);
1510 $query = qq|SELECT id, | . join(', ', @columns) . qq|
1512 WHERE id IN ($placeholders)|;
1514 my %data = selectall_as_map($form, $dbh, $query, 'id', \@columns, @part_ids);
1516 map { $form->{$_} = [] } (qw(make model), @columns);
1518 foreach my $i (1 .. $rowcount) {
1519 my $id = $form->{"${prefix}${i}"};
1523 foreach (@columns) {
1524 push @{ $form->{$_} }, $data{$id}->{$_};
1527 push @{ $form->{make} }, [];
1528 push @{ $form->{model} }, [];
1530 next if (!$makemodel{$id});
1532 foreach my $ref (@{ $makemodel{$id} }) {
1533 map { push @{ $form->{$_}->[-1] }, $ref->{$_} } qw(make model);
1537 $main::lxdebug->leave_sub();