1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
31 # Inventory Control backend
33 #======================================================================
38 use List::MoreUtils qw(all);
45 $main::lxdebug->enter_sub();
47 my ($self, $myconfig, $form) = @_;
50 my $dbh = $form->dbconnect($myconfig);
56 c1.accno AS inventory_accno,
57 c2.accno AS income_accno,
58 c3.accno AS expense_accno,
61 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
62 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
63 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
64 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
66 my $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
68 # copy to $form variables
69 map { $form->{$_} = $ref->{$_} } (keys %{$ref});
73 my %oid = ('Pg' => 'a.oid',
74 'Oracle' => 'a.rowid');
76 # part or service item
77 $form->{item} = ($form->{inventory_accno}) ? 'part' : 'service';
78 if ($form->{assembly}) {
79 $form->{item} = 'assembly';
81 # retrieve assembly items
83 qq|SELECT p.id, p.partnumber, p.description,
84 p.sellprice, p.lastcost, p.weight, a.qty, a.bom, p.unit,
85 pg.partsgroup, p.price_factor_id, pfac.factor AS price_factor
87 JOIN assembly a ON (a.parts_id = p.id)
88 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
89 LEFT JOIN price_factors pfac ON pfac.id = p.price_factor_id
91 ORDER BY $oid{$myconfig->{dbdriver}}|;
92 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
94 $form->{assembly_rows} = 0;
95 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
96 $form->{assembly_rows}++;
97 foreach my $key (keys %{$ref}) {
98 $form->{"${key}_$form->{assembly_rows}"} = $ref->{$key};
105 # setup accno hash for <option checked> {amount} is used in create_links
106 $form->{amount}{IC} = $form->{inventory_accno};
107 $form->{amount}{IC_income} = $form->{income_accno};
108 $form->{amount}{IC_sale} = $form->{income_accno};
109 $form->{amount}{IC_expense} = $form->{expense_accno};
110 $form->{amount}{IC_cogs} = $form->{expense_accno};
112 my @pricegroups = ();
113 my @pricegroups_not_used = ();
117 qq|SELECT p.parts_id, p.pricegroup_id, p.price,
118 (SELECT pg.pricegroup
120 WHERE pg.id = p.pricegroup_id) AS pricegroup
123 ORDER BY pricegroup|;
124 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
128 while (($form->{"klass_$i"}, $form->{"pricegroup_id_$i"},
129 $form->{"price_$i"}, $form->{"pricegroup_$i"})
130 = $sth->fetchrow_array()) {
131 push @pricegroups, $form->{"pricegroup_id_$i"};
138 $query = qq|SELECT id, pricegroup FROM pricegroup|;
139 $form->{PRICEGROUPS} = selectall_hashref_query($form, $dbh, $query);
141 #find not used pricegroups
142 while ($tmp = pop(@{ $form->{PRICEGROUPS} })) {
144 foreach my $item (@pricegroups) {
145 if ($item eq $tmp->{id}) {
150 push(@pricegroups_not_used, $tmp) unless ($in_use);
153 # if not used pricegroups are avaible
154 if (@pricegroups_not_used) {
156 foreach $name (@pricegroups_not_used) {
157 $form->{"klass_$i"} = "$name->{id}";
158 $form->{"pricegroup_id_$i"} = "$name->{id}";
159 $form->{"pricegroup_$i"} = "$name->{pricegroup}";
165 $form->{price_rows} = $i - 1;
167 unless ($form->{item} eq 'service') {
170 if ($form->{makemodel}) {
171 $query = qq|SELECT m.make, m.model FROM makemodel m | .
172 qq|WHERE m.parts_id = ?|;
173 @values = ($form->{id});
174 $sth = $dbh->prepare($query);
175 $sth->execute(@values) || $form->dberror("$query (" . join(', ', @values) . ")");
178 while (($form->{"make_$i"}, $form->{"model_$i"}) = $sth->fetchrow_array)
183 $form->{makemodel_rows} = $i - 1;
189 $form->{language_values} = "";
190 $query = qq|SELECT language_id, translation, longdescription
193 my $trq = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
194 while (my $tr = $trq->fetchrow_hashref(NAME_lc)) {
195 $form->{language_values} .= "---+++---" . join('--++--', @{$tr}{qw(language_id translation longdescription)});
199 # now get accno for taxes
202 FROM chart c, partstax pt
203 WHERE (pt.chart_id = c.id) AND (pt.parts_id = ?)|;
204 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
205 while (($key) = $sth->fetchrow_array) {
206 $form->{amount}{$key} = $key;
212 my @referencing_tables = qw(invoice orderitems inventory rmaitems);
213 my %column_map = ( );
214 my $parts_id = conv_i($form->{id});
216 $form->{orphaned} = 1;
218 foreach my $table (@referencing_tables) {
219 my $column = $column_map{$table} || 'parts_id';
220 $query = qq|SELECT $column FROM $table WHERE $column = ? LIMIT 1|;
221 my ($found) = selectrow_query($form, $dbh, $query, $parts_id);
224 $form->{orphaned} = 0;
229 $form->{"unit_changeable"} = $form->{orphaned};
233 $main::lxdebug->leave_sub();
236 sub get_pricegroups {
237 $main::lxdebug->enter_sub();
239 my ($self, $myconfig, $form) = @_;
241 my $dbh = $form->dbconnect($myconfig);
244 my $query = qq|SELECT id, pricegroup FROM pricegroup|;
245 my $pricegroups = selectall_hashref_query($form, $dbh, $query);
248 foreach $pg (@{ $pricegroups }) {
249 $form->{"klass_$i"} = "$pg->{id}";
250 $form->{"price_$i"} = $form->format_amount($myconfig, $form->{"price_$i"}, -2);
251 $form->{"pricegroup_id_$i"} = "$pg->{id}";
252 $form->{"pricegroup_$i"} = "$pg->{pricegroup}";
257 $form->{price_rows} = $i - 1;
261 $main::lxdebug->leave_sub();
266 sub retrieve_buchungsgruppen {
267 $main::lxdebug->enter_sub();
269 my ($self, $myconfig, $form) = @_;
273 my $dbh = $form->dbconnect($myconfig);
275 # get buchungsgruppen
276 $query = qq|SELECT id, description FROM buchungsgruppen ORDER BY sortkey|;
277 $form->{BUCHUNGSGRUPPEN} = selectall_hashref_query($form, $dbh, $query);
279 $main::lxdebug->leave_sub();
283 $main::lxdebug->enter_sub();
285 my ($self, $myconfig, $form) = @_;
287 # connect to database, turn off AutoCommit
288 my $dbh = $form->dbconnect_noauto($myconfig);
291 # make up a unique handle and store in partnumber field
292 # then retrieve the record based on the unique handle to get the id
293 # replace the partnumber field with the actual variable
294 # add records for makemodel
296 # if there is a $form->{id} then replace the old entry
297 # delete all makemodel entries and add the new ones
299 # undo amount formatting
300 map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) }
301 qw(rop weight listprice sellprice gv lastcost);
303 my $makemodel = (($form->{make_1}) || ($form->{model_1})) ? 1 : 0;
305 $form->{assembly} = ($form->{item} eq 'assembly') ? 1 : 0;
309 my $priceupdate = ', priceupdate = current_date';
314 $query = qq|SELECT sellprice, weight FROM parts WHERE id = ?|;
315 my ($sellprice, $weight) = selectrow_query($form, $dbh, $query, conv_i($form->{id}));
317 # if item is part of an assembly adjust all assemblies
318 $query = qq|SELECT id, qty FROM assembly WHERE parts_id = ?|;
319 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
320 while (my ($id, $qty) = $sth->fetchrow_array) {
321 &update_assembly($dbh, $form, $id, $qty, $sellprice * 1, $weight * 1);
325 if ($form->{item} ne 'service') {
326 # delete makemodel records
327 do_query($form, $dbh, qq|DELETE FROM makemodel WHERE parts_id = ?|, conv_i($form->{id}));
330 if ($form->{item} eq 'assembly') {
331 # delete assembly records
332 do_query($form, $dbh, qq|DELETE FROM assembly WHERE id = ?|, conv_i($form->{id}));
336 do_query($form, $dbh, qq|DELETE FROM partstax WHERE parts_id = ?|, conv_i($form->{id}));
338 # delete translations
339 do_query($form, $dbh, qq|DELETE FROM translation WHERE parts_id = ?|, conv_i($form->{id}));
341 # Check whether or not the prices have changed. If they haven't
342 # then 'priceupdate' should not be updated.
343 my $previous_values = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM parts WHERE id = ?|, conv_i($form->{id})) || {};
344 $priceupdate = '' if (all { $previous_values->{$_} == $form->{$_} } qw(sellprice lastcost listprice));
347 my ($count) = selectrow_query($form, $dbh, qq|SELECT COUNT(*) FROM parts WHERE partnumber = ?|, $form->{partnumber});
349 $main::lxdebug->leave_sub();
353 ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
354 do_query($form, $dbh, qq|INSERT INTO parts (id, partnumber) VALUES (?, '')|, $form->{id});
356 $form->{orphaned} = 1;
357 if ($form->{partnumber} eq "" && $form->{"item"} eq "service") {
358 $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
360 if ($form->{partnumber} eq "" && $form->{"item"} ne "service") {
361 $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
365 my $partsgroup_id = 0;
367 if ($form->{partsgroup}) {
368 ($partsgroup, $partsgroup_id) = split(/--/, $form->{partsgroup});
371 my ($subq_inventory, $subq_expense, $subq_income);
372 if ($form->{"item"} eq "part") {
374 qq|(SELECT bg.inventory_accno_id
375 FROM buchungsgruppen bg
376 WHERE bg.id = | . conv_i($form->{"buchungsgruppen_id"}, 'NULL') . qq|)|;
378 $subq_inventory = "NULL";
381 if ($form->{"item"} ne "assembly") {
383 qq|(SELECT bg.expense_accno_id_0
384 FROM buchungsgruppen bg
385 WHERE bg.id = | . conv_i($form->{"buchungsgruppen_id"}, 'NULL') . qq|)|;
387 $subq_expense = "NULL";
406 buchungsgruppen_id = ?,
408 inventory_accno_id = $subq_inventory,
409 income_accno_id = (SELECT bg.income_accno_id_0 FROM buchungsgruppen bg WHERE bg.id = ?),
410 expense_accno_id = $subq_expense,
418 not_discountable = ?,
424 @values = ($form->{partnumber},
425 $form->{description},
426 $makemodel ? 't' : 'f',
427 $form->{assembly} ? 't' : 'f',
437 conv_i($form->{buchungsgruppen_id}),
438 conv_i($form->{payment_id}),
439 conv_i($form->{buchungsgruppen_id}),
440 $form->{obsolete} ? 't' : 'f',
443 $form->{shop} ? 't' : 'f',
447 $form->{not_discountable} ? 't' : 'f',
449 conv_i($partsgroup_id),
450 conv_i($form->{price_factor_id}),
453 do_query($form, $dbh, $query, @values);
455 # delete translation records
456 do_query($form, $dbh, qq|DELETE FROM translation WHERE parts_id = ?|, conv_i($form->{id}));
458 if ($form->{language_values} ne "") {
459 foreach $item (split(/---\+\+\+---/, $form->{language_values})) {
460 my ($language_id, $translation, $longdescription) = split(/--\+\+--/, $item);
461 if ($translation ne "") {
462 $query = qq|INSERT into translation (parts_id, language_id, translation, longdescription)
463 VALUES ( ?, ?, ?, ? )|;
464 @values = (conv_i($form->{id}), conv_i($language_id), $translation, $longdescription);
465 do_query($form, $dbh, $query, @values);
470 # delete price records
471 do_query($form, $dbh, qq|DELETE FROM prices WHERE parts_id = ?|, conv_i($form->{id}));
473 # insert price records only if different to sellprice
474 for my $i (1 .. $form->{price_rows}) {
475 my $price = $form->parse_amount($myconfig, $form->{"price_$i"});
477 $form->{"price_$i"} = $form->{sellprice};
481 || $form->{"klass_$i"}
482 || $form->{"pricegroup_id_$i"})
483 and $price != $form->{sellprice}
485 #$klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
486 $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price) | .
488 @values = (conv_i($form->{id}), conv_i($form->{"pricegroup_id_$i"}), $price);
489 do_query($form, $dbh, $query, @values);
493 # insert makemodel records
494 unless ($form->{item} eq 'service') {
495 for my $i (1 .. $form->{makemodel_rows}) {
496 if (($form->{"make_$i"}) || ($form->{"model_$i"})) {
498 $query = qq|INSERT INTO makemodel (parts_id, make, model) | .
499 qq|VALUES (?, ?, ?)|;
500 @values = (conv_i($form->{id}), conv_i($form->{"make_$i"}), $form->{"model_$i"});
502 do_query($form, $dbh, $query, @values);
508 foreach $item (split(/ /, $form->{taxaccounts})) {
509 if ($form->{"IC_tax_$item"}) {
511 qq|INSERT INTO partstax (parts_id, chart_id)
512 VALUES (?, (SELECT id FROM chart WHERE accno = ?))|;
513 @values = (conv_i($form->{id}), $item);
514 do_query($form, $dbh, $query, @values);
518 # add assembly records
519 if ($form->{item} eq 'assembly') {
521 for my $i (1 .. $form->{assembly_rows}) {
522 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
524 if ($form->{"qty_$i"} != 0) {
525 $form->{"bom_$i"} *= 1;
526 $query = qq|INSERT INTO assembly (id, parts_id, qty, bom) | .
527 qq|VALUES (?, ?, ?, ?)|;
528 @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}), conv_i($form->{"qty_$i"}), $form->{"bom_$i"} ? 't' : 'f');
529 do_query($form, $dbh, $query, @values);
536 my $shippingdate = "$a[5]-$a[4]-$a[3]";
538 $form->get_employee($dbh);
542 #set expense_accno=inventory_accno if they are different => bilanz
544 ($form->{expense_accno} != $form->{inventory_accno})
545 ? $form->{inventory_accno}
546 : $form->{expense_accno};
548 # get tax rates and description
550 ($form->{vc} eq "customer") ? $form->{income_accno} : $vendor_accno;
552 qq|SELECT c.accno, c.description, t.rate, t.taxnumber
554 WHERE (c.id = t.chart_id) AND (t.taxkey IN (SELECT taxkey_id FROM chart where accno = ?))
556 $stw = prepare_execute_query($form, $dbh, $query, $accno_id);
558 $form->{taxaccount} = "";
559 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
560 $form->{taxaccount} .= "$ptr->{accno} ";
561 if (!($form->{taxaccount2} =~ /\Q$ptr->{accno}\E/)) {
562 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
563 $form->{"$ptr->{accno}_description"} = $ptr->{description};
564 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
565 $form->{taxaccount2} .= " $ptr->{accno} ";
569 CVar->save_custom_variables('dbh' => $dbh,
571 'trans_id' => $form->{id},
572 'variables' => $form);
575 my $rc = $dbh->commit;
578 $main::lxdebug->leave_sub();
583 sub update_assembly {
584 $main::lxdebug->enter_sub();
586 my ($dbh, $form, $id, $qty, $sellprice, $weight) = @_;
588 my $query = qq|SELECT id, qty FROM assembly WHERE parts_id = ?|;
589 my $sth = prepare_execute_query($form, $dbh, $query, conv_i($id));
591 while (my ($pid, $aqty) = $sth->fetchrow_array) {
592 &update_assembly($dbh, $form, $pid, $aqty * $qty, $sellprice, $weight);
597 qq|UPDATE parts SET sellprice = sellprice + ?, weight = weight + ?
599 @values = ($qty * ($form->{sellprice} - $sellprice),
600 $qty * ($form->{weight} - $weight), conv_i($id));
601 do_query($form, $dbh, $query, @values);
603 $main::lxdebug->leave_sub();
606 sub retrieve_assemblies {
607 $main::lxdebug->enter_sub();
609 my ($self, $myconfig, $form) = @_;
611 # connect to database
612 my $dbh = $form->dbconnect($myconfig);
614 my $where = qq|NOT p.obsolete|;
617 if ($form->{partnumber}) {
618 $where .= qq| AND (p.partnumber ILIKE ?)|;
619 push(@values, '%' . $form->{partnumber} . '%');
622 if ($form->{description}) {
623 $where .= qq| AND (p.description ILIKE ?)|;
624 push(@values, '%' . $form->{description} . '%');
627 # retrieve assembly items
629 qq|SELECT p.id, p.partnumber, p.description,
630 p.bin, p.onhand, p.rop,
631 (SELECT sum(p2.inventory_accno_id)
632 FROM parts p2, assembly a
633 WHERE (p2.id = a.parts_id) AND (a.id = p.id)) AS inventory
635 WHERE NOT p.obsolete AND p.assembly $where|;
637 $form->{assembly_items} = selectall_hashref_query($form, $dbh, $query, @values);
641 $main::lxdebug->leave_sub();
645 $main::lxdebug->enter_sub();
647 my ($self, $myconfig, $form) = @_;
648 my @values = (conv_i($form->{id}));
649 # connect to database, turn off AutoCommit
650 my $dbh = $form->dbconnect_noauto($myconfig);
652 my %columns = ( "assembly" => "id", "parts" => "id" );
654 for my $table (qw(prices partstax makemodel inventory assembly license translation parts)) {
655 my $column = defined($columns{$table}) ? $columns{$table} : "parts_id";
656 do_query($form, $dbh, qq|DELETE FROM $table WHERE $column = ?|, @values);
660 my $rc = $dbh->commit;
663 $main::lxdebug->leave_sub();
669 $main::lxdebug->enter_sub();
671 my ($self, $myconfig, $form) = @_;
673 my $i = $form->{assembly_rows};
675 my $where = qq|1 = 1|;
678 my %columns = ("partnumber" => "p", "description" => "p", "partsgroup" => "pg");
680 while (my ($column, $table) = each(%columns)) {
681 next unless ($form->{"${column}_$i"});
682 $where .= qq| AND ${table}.${column} ILIKE ?|;
683 push(@values, '%' . $form->{"${column}_$i"} . '%');
687 $where .= qq| AND NOT (p.id = ?)|;
688 push(@values, conv_i($form->{id}));
692 $where .= qq| ORDER BY p.partnumber|;
694 $where .= qq| ORDER BY p.description|;
697 # connect to database
698 my $dbh = $form->dbconnect($myconfig);
701 qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
702 p.weight, p.onhand, p.unit, pg.partsgroup, p.lastcost,
703 p.price_factor_id, pfac.factor AS price_factor
705 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
706 LEFT JOIN price_factors pfac ON pfac.id = p.price_factor_id
708 $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
712 $main::lxdebug->leave_sub();
717 # Warning, deep magic ahead.
718 # This function gets all parts from the database according to the filters specified
721 # sort revers - sorting field + direction
724 # simple filter strings (every one of those also has a column flag prefixed with 'l_' associated):
725 # partnumber ean description partsgroup microfiche drawing
728 # 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
731 # itemstatus = active | onhand | short | obsolete | orphaned
732 # searchitems = part | assembly | service
735 # make model - makemodel
736 # serialnumber transdatefrom transdateto - invoice/orderitems
739 # bought sold onorder ordered rfq quoted - aggreg joins with invoices/orders
740 # l_linetotal l_subtotal - aggreg joins to display totals (complicated) - NOT IMPLEMENTED here, implementation at frontend
741 # l_soldtotal - aggreg join to display total of sold quantity
742 # onhand - as above, but masking the simple itemstatus results (doh!)
743 # short - NOT IMPLEMENTED as form filter, only as itemstatus option
744 # l_serialnumber - belonges to serialnumber filter
745 # l_deliverydate - displays deliverydate is sold etc. flags are active
746 # l_soldtotal - aggreg join to display total of sold quantity, works as long as there's no bullshit in soldtotal
749 # onhand - as above, but masking the simple itemstatus results (doh!)
750 # masking of onhand in bsooqr mode - ToDO: fixme
752 # disabled sanity checks and changes:
753 # - searchitems = assembly will no longer disable bought
754 # - searchitems = service will no longer disable make and model, although services don't have make/model, it doesn't break the query
755 # - itemstatus = orphaned will no longer disable onhand short bought sold onorder ordered rfq quoted transdate[from|to]
756 # - itemstatus = obsolete will no longer disable onhand, short
757 # - allow sorting by ean
758 # - serialnumber filter also works if l_serialnumber isn't ticked
759 # - onhand doesn't get masked by it's oi or invoice counterparts atm. ToDO: fix this
760 # - sorting will now change sorting if the requested sorting column isn't checked and doesn't get checked as a side effect
763 $main::lxdebug->enter_sub();
765 my ($self, $myconfig, $form) = @_;
766 my $dbh = $form->get_standard_dbh($myconfig);
768 $form->{parts} = +{ };
769 $form->{soldtotal} = undef if $form->{l_soldtotal}; # security fix. top100 insists on putting strings in there...
771 my @simple_filters = qw(partnumber ean description partsgroup microfiche drawing onhand);
772 my @makemodel_filters = qw(make model);
773 my @invoice_oi_filters = qw(serialnumber soldtotal);
774 my @apoe_filters = qw(transdate);
775 my @all_columns = (@simple_filters, @makemodel_filters, @apoe_filters, qw(serialnumber));
776 my @simple_l_switches = (@all_columns, qw(listprice sellprice lastcost priceupdate weight unit bin rop image));
777 my @oe_flags = qw(bought sold onorder ordered rfq quoted);
778 my @qsooqr_flags = qw(invnumber ordnumber quonumber trans_id name module);
779 my @deliverydate_flags = qw(deliverydate);
780 # my @other_flags = qw(onhand); # ToDO: implement these
781 # my @inactive_flags = qw(l_subtotal short l_linetotal);
784 partsgroup => 'LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)',
785 makemodel => 'LEFT JOIN makemodel mm ON (mm.parts_id = p.id)',
786 pfac => 'LEFT JOIN price_factors pfac ON (pfac.id = p.price_factor_id)',
789 SELECT parts_id, description, serialnumber, trans_id, unit, sellprice, qty, assemblyitem, 'invoice' AS ioi FROM invoice UNION
790 SELECT parts_id, description, serialnumber, trans_id, unit, sellprice, qty, FALSE AS assemblyitem, 'orderitems' AS ioi FROM orderitems
791 ) AS ioi ON ioi.parts_id = p.id|,
794 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
795 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
796 SELECT id, transdate, 'oe' AS module, ordnumber, quonumber, NULL AS invnumber, quotation, customer_id, vendor_id, NULL AS deliverydate, 'orderitems' AS ioi FROM oe
797 ) AS apoe ON ((ioi.trans_id = apoe.id) AND (ioi.ioi = apoe.ioi))|,
800 SELECT id, name, 'customer' AS cv FROM customer UNION
801 SELECT id, name, 'vendor' AS cv FROM vendor
802 ) AS cv ON cv.id = apoe.customer_id OR cv.id = apoe.vendor_id|,
804 my @join_order = qw(partsgroup makemodel invoice_oi apoe cv pfac);
807 if (($form->{searchitems} eq 'assembly') && $form->{l_lastcost}) {
808 @simple_l_switches = grep { $_ ne 'lastcost' } @simple_l_switches;
811 #===== switches and simple filters ========#
813 my @select_tokens = qw(id factor);
814 my @where_tokens = qw(1=1);
815 my @group_tokens = ();
817 # special case transdate
818 if (grep { $form->{$_} } qw(transdatefrom transdateto)) {
819 $form->{"l_transdate"} = 1;
820 push @select_tokens, 'transdate';
821 for (qw(transdatefrom transdateto)) {
822 next unless $form->{$_};
823 push @where_tokens, sprintf "transdate %s ?", /from$/ ? '>=' : '<=';
824 push @bind_vars, $form->{$_};
828 my %simple_filter_table_prefix = (
832 foreach (@simple_filters, @makemodel_filters, @invoice_oi_filters) {
833 next unless $form->{$_};
834 $form->{"l_$_"} = '1'; # show the column
835 push @where_tokens, "$simple_filter_table_prefix{$_}$_ ILIKE ?";
836 push @bind_vars, "%$form->{$_}%";
839 foreach (@simple_l_switches) {
840 next unless $form->{"l_$_"};
841 push @select_tokens, $_;
844 for ($form->{searchitems}) {
845 push @where_tokens, 'p.inventory_accno_id > 0' if /part/;
846 push @where_tokens, 'p.inventory_accno_id IS NULL' if /service/;
847 push @where_tokens, 'NOT p.assembly' if /service/;
848 push @where_tokens, ' p.assembly' if /assembly/;
851 for ($form->{itemstatus}) {
852 push @where_tokens, 'p.id NOT IN
853 (SELECT DISTINCT parts_id FROM invoice UNION
854 SELECT DISTINCT parts_id FROM assembly UNION
855 SELECT DISTINCT parts_id FROM orderitems)' if /orphaned/;
856 push @where_tokens, 'p.onhand = 0' if /orphaned/;
857 push @where_tokens, 'NOT p.obsolete' if /active/;
858 push @where_tokens, ' p.obsolete', if /obsolete/;
859 push @where_tokens, 'p.onhand > 0', if /onhand/;
860 push @where_tokens, 'p.onhand < p.rop', if /short/;
863 my $q_assembly_lastcost =
864 qq|(SELECT SUM(a_lc.qty * p_lc.lastcost / COALESCE(pfac_lc.factor, 1))
866 LEFT JOIN parts p_lc ON (a_lc.parts_id = p_lc.id)
867 LEFT JOIN price_factors pfac_lc ON (p_lc.price_factor_id = pfac_lc.id)
868 WHERE (a_lc.id = p.id)) AS lastcost|;
870 my @sort_cols = (@simple_filters, qw(id bin priceupdate onhand invnumber ordnumber quonumber name serialnumber soldtotal deliverydate));
871 $form->{sort} = 'id' unless grep { $form->{"l_$_"} } grep { $form->{sort} eq $_ } @sort_cols;
873 my $sort_order = ($form->{revers} ? ' DESC' : ' ASC');
875 my $order_clause = " ORDER BY $form->{sort} " . ($form->{revers} ? 'DESC' : 'ASC');
877 # special case: sorting by partnumber
878 # since partnumbers are expected to be prefixed integers, a special sorting is implemented sorting first lexically by prefix and then by suffix.
879 # and yes, that expression is designed to hold that array of regexes only once, so the map is kinda messy, sorry about that.
880 # ToDO: implement proper functional sorting
881 # Nette Idee von Sven, gibt aber Probleme wenn die Artikelnummern groesser als 32bit sind. Korrekt waere es, dass Sort-Natural-Modul zu nehmen
882 # Ich lass das mal hier drin, damit die Idee erhalten bleibt jb 28.5.2009 bug 1018
883 #$form->{sort} = join ', ', map { push @select_tokens, $_; ($table_prefix{$_} = "substring(partnumber,'[") . $_ } qw|^[:digit:]]+') [:digit:]]+')::INTEGER|
884 # if $form->{sort} eq 'partnumber';
886 #my $order_clause = " ORDER BY $form->{sort} $sort_order";
888 my $limit_clause = " LIMIT 100" if $form->{top100};
890 #=== joins and complicated filters ========#
892 my $bsooqr = $form->{bought} || $form->{sold}
893 || $form->{ordered} || $form->{onorder}
894 || $form->{quoted} || $form->{rfq};
897 push @select_tokens, @qsooqr_flags if $bsooqr;
898 push @select_tokens, @deliverydate_flags if $bsooqr && $form->{l_deliverydate};
899 push @select_tokens, $q_assembly_lastcost if ($form->{searchitems} eq 'assembly') && $form->{l_lastcost};
900 push @bsooqr_tokens, q|module = 'ir' AND NOT ioi.assemblyitem| if $form->{bought};
901 push @bsooqr_tokens, q|module = 'is' AND NOT ioi.assemblyitem| if $form->{sold};
902 push @bsooqr_tokens, q|module = 'oe' AND NOT quotation AND cv = 'customer'| if $form->{ordered};
903 push @bsooqr_tokens, q|module = 'oe' AND NOT quotation AND cv = 'vendor'| if $form->{onorder};
904 push @bsooqr_tokens, q|module = 'oe' AND quotation AND cv = 'customer'| if $form->{quoted};
905 push @bsooqr_tokens, q|module = 'oe' AND quotation AND cv = 'vendor'| if $form->{rfq};
906 push @where_tokens, join ' OR ', map { "($_)" } @bsooqr_tokens if $bsooqr;
908 $joins_needed{partsgroup} = 1;
909 $joins_needed{pfac} = 1;
910 $joins_needed{makemodel} = 1 if grep { $form->{$_} || $form->{"l_$_"} } @makemodel_filters;
911 $joins_needed{cv} = 1 if $bsooqr;
912 $joins_needed{apoe} = 1 if $joins_needed{cv} || grep { $form->{$_} || $form->{"l_$_"} } @apoe_filters;
913 $joins_needed{invoice_oi} = 1 if $joins_needed{apoe} || grep { $form->{$_} || $form->{"l_$_"} } @invoice_oi_filters;
915 # special case for description search.
916 # up in the simple filter section the description filter got interpreted as something like: WHERE description ILIKE '%$form->{description}%'
917 # now we'd like to search also for the masked description entered in orderitems and invoice, so...
918 # find the old entries in of @where_tokens and @bind_vars, and adjust them
919 if ($joins_needed{invoice_oi}) {
920 for (my ($wi, $bi) = (0)x2; $wi <= $#where_tokens; $bi++ if $where_tokens[$wi++] =~ /\?/) {
921 next unless $where_tokens[$wi] =~ /^description ILIKE/;
922 splice @where_tokens, $wi, 1, 'p.description ILIKE ? OR ioi.description ILIKE ?';
923 splice @bind_vars, $bi, 0, $bind_vars[$bi];
928 # now the master trick: soldtotal.
929 if ($form->{l_soldtotal}) {
930 push @where_tokens, 'ioi.qty >= 0';
931 push @group_tokens, @select_tokens;
932 map { s/.*\sAS\s+//si } @group_tokens;
933 push @select_tokens, 'SUM(ioi.qty)';
936 #============= build query ================#
940 deliverydate => 'apoe.', serialnumber => 'ioi.',
941 transdate => 'apoe.', trans_id => 'ioi.',
942 module => 'apoe.', name => 'cv.',
943 ordnumber => 'apoe.', make => 'mm.',
944 quonumber => 'apoe.', model => 'mm.',
945 invnumber => 'apoe.', partsgroup => 'pg.',
948 'SUM(ioi.qty)' => ' ',
951 $table_prefix{$q_assembly_lastcost} = ' ';
953 my %renamed_columns = (
954 'factor' => 'price_factor',
955 'SUM(ioi.qty)' => 'soldtotal',
958 map { $table_prefix{$_} = 'ioi.' } qw(description serialnumber qty unit) if $joins_needed{invoice_oi};
959 map { $renamed_columns{$_} = ' AS ' . $renamed_columns{$_} } keys %renamed_columns;
961 my $select_clause = join ', ', map { ($table_prefix{$_} || "p.") . $_ . $renamed_columns{$_} } @select_tokens;
962 my $join_clause = join ' ', @joins{ grep $joins_needed{$_}, @join_order };
963 my $where_clause = join ' AND ', map { "($_)" } @where_tokens;
964 my $group_clause = ' GROUP BY ' . join ', ', map { ($table_prefix{$_} || "p.") . $_ } @group_tokens if scalar @group_tokens;
966 my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'IC',
967 'trans_id_field' => 'p.id',
971 $where_clause .= qq| AND ($cvar_where)|;
972 push @bind_vars, @cvar_values;
975 my $query = qq|SELECT DISTINCT $select_clause FROM parts p $join_clause WHERE $where_clause $group_clause $order_clause $limit_clause|;
977 $form->{parts} = selectall_hashref_query($form, $dbh, $query, @bind_vars);
979 map { $_->{onhand} *= 1 } @{ $form->{parts} };
981 # post processing for assembly parts lists (bom)
982 # for each part get the assembly parts and add them into the partlist.
984 if ($form->{searchitems} eq 'assembly' && $form->{bom}) {
986 qq|SELECT p.id, p.partnumber, p.description, a.qty AS onhand,
988 p.sellprice, p.listprice, p.lastcost,
989 p.rop, p.weight, p.priceupdate,
990 p.image, p.drawing, p.microfiche,
993 INNER JOIN assembly a ON (p.id = a.parts_id)
996 $sth = prepare_query($form, $dbh, $query);
998 foreach $item (@{ $form->{parts} }) {
999 push(@assemblies, $item);
1000 do_statement($form, $sth, $query, conv_i($item->{id}));
1002 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1003 $ref->{assemblyitem} = 1;
1004 map { $ref->{$_} /= $ref->{factor} || 1 } qw(sellprice listprice lastcost);
1005 push(@assemblies, $ref);
1010 # copy assemblies to $form->{parts}
1011 $form->{parts} = \@assemblies;
1014 $main::lxdebug->leave_sub();
1017 sub _create_filter_for_priceupdate {
1018 $main::lxdebug->enter_sub();
1021 my $myconfig = \%main::myconfig;
1022 my $form = $main::form;
1025 my $where = '1 = 1';
1027 foreach my $item (qw(partnumber drawing microfiche make model pg.partsgroup)) {
1029 $column =~ s/.*\.//;
1030 next unless ($form->{$column});
1032 $where .= qq| AND $item ILIKE ?|;
1033 push(@where_values, '%' . $form->{$column} . '%');
1036 foreach my $item (qw(description serialnumber)) {
1037 next unless ($form->{$item});
1039 $where .= qq| AND (${item} ILIKE ?)|;
1040 push(@where_values, '%' . $form->{$item} . '%');
1044 # items which were never bought, sold or on an order
1045 if ($form->{itemstatus} eq 'orphaned') {
1047 qq| AND (p.onhand = 0)
1050 SELECT DISTINCT parts_id FROM invoice
1052 SELECT DISTINCT parts_id FROM assembly
1054 SELECT DISTINCT parts_id FROM orderitems
1057 } elsif ($form->{itemstatus} eq 'active') {
1058 $where .= qq| AND p.obsolete = '0'|;
1060 } elsif ($form->{itemstatus} eq 'obsolete') {
1061 $where .= qq| AND p.obsolete = '1'|;
1063 } elsif ($form->{itemstatus} eq 'onhand') {
1064 $where .= qq| AND p.onhand > 0|;
1066 } elsif ($form->{itemstatus} eq 'short') {
1067 $where .= qq| AND p.onhand < p.rop|;
1071 foreach my $column (qw(make model)) {
1072 next unless ($form->{$colum});
1073 $where .= qq| AND p.id IN (SELECT DISTINCT parts_id FROM makemodel WHERE $column ILIKE ?|;
1074 push(@where_values, '%' . $form->{$column} . '%');
1077 $main::lxdebug->leave_sub();
1079 return ($where, @where_values);
1082 sub get_num_matches_for_priceupdate {
1083 $main::lxdebug->enter_sub();
1087 my $myconfig = \%main::myconfig;
1088 my $form = $main::form;
1090 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1092 my ($where, @where_values) = $self->_create_filter_for_priceupdate();
1094 my $num_updated = 0;
1097 for my $column (qw(sellprice listprice)) {
1098 next if ($form->{$column} eq "");
1106 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1108 my ($result) = selectfirst_array_query($from, $dbh, $query, @where_values);
1109 $num_updated += $result if (0 <= $result);
1118 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1119 WHERE $where) AND (pricegroup_id = ?)|;
1120 my $sth = prepare_query($form, $dbh, $query);
1122 for my $i (1 .. $form->{price_rows}) {
1123 next if ($form->{"price_$i"} eq "");
1125 my ($result) = do_statement($form, $sth, $query, @where_values, conv_i($form->{"pricegroup_id_$i"}));
1126 $num_updated += $result if (0 <= $result);
1130 $main::lxdebug->leave_sub();
1132 return $num_updated;
1136 $main::lxdebug->enter_sub();
1138 my ($self, $myconfig, $form) = @_;
1140 my ($where, @where_values) = $self->_create_filter_for_priceupdate();
1141 my $num_updated = 0;
1143 # connect to database
1144 my $dbh = $form->dbconnect_noauto($myconfig);
1146 for my $column (qw(sellprice listprice)) {
1147 next if ($form->{$column} eq "");
1149 my $value = $form->parse_amount($myconfig, $form->{$column});
1152 if ($form->{"${column}_type"} eq "percent") {
1153 $value = ($value / 100) + 1;
1158 qq|UPDATE parts SET $column = $column $operator ?
1162 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1164 my $result = do_query($from, $dbh, $query, $value, @where_values);
1165 $num_updated += $result if (0 <= $result);
1169 qq|UPDATE prices SET price = price + ?
1173 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1174 WHERE $where) AND (pricegroup_id = ?)|;
1175 my $sth_add = prepare_query($form, $dbh, $q_add);
1178 qq|UPDATE prices SET price = price * ?
1182 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1183 WHERE $where) AND (pricegroup_id = ?)|;
1184 my $sth_multiply = prepare_query($form, $dbh, $q_multiply);
1186 for my $i (1 .. $form->{price_rows}) {
1187 next if ($form->{"price_$i"} eq "");
1189 my $value = $form->parse_amount($myconfig, $form->{"price_$i"});
1192 if ($form->{"pricegroup_type_$i"} eq "percent") {
1193 $result = do_statement($form, $sth_multiply, $q_multiply, ($value / 100) + 1, @where_values, conv_i($form->{"pricegroup_id_$i"}));
1195 $result = do_statement($form, $sth_add, $q_add, $value, @where_values, conv_i($form->{"pricegroup_id_$i"}));
1198 $num_updated += $result if (0 <= $result);
1202 $sth_multiply->finish();
1204 my $rc= $dbh->commit;
1207 $main::lxdebug->leave_sub();
1209 return $num_updated;
1213 $main::lxdebug->enter_sub();
1215 my ($self, $module, $myconfig, $form) = @_;
1217 # connect to database
1218 my $dbh = $form->dbconnect($myconfig);
1220 my @values = ('%' . $module . '%');
1224 qq|SELECT c.accno, c.description, c.link, c.id,
1225 p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1226 FROM chart c, parts p
1227 WHERE (c.link LIKE ?) AND (p.id = ?)
1229 push(@values, conv_i($form->{id}));
1233 qq|SELECT c.accno, c.description, c.link, c.id,
1234 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1235 FROM chart c, defaults d
1240 my $sth = prepare_execute_query($form, $dbh, $query, @values);
1241 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1242 foreach my $key (split(/:/, $ref->{link})) {
1243 if ($key =~ /\Q$module\E/) {
1244 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1245 || ($ref->{id} eq $ref->{income_accno_id})
1246 || ($ref->{id} eq $ref->{expense_accno_id})) {
1247 push @{ $form->{"${module}_links"}{$key} },
1248 { accno => $ref->{accno},
1249 description => $ref->{description},
1250 selected => "selected" };
1251 $form->{"${key}_default"} = "$ref->{accno}--$ref->{description}";
1253 push @{ $form->{"${module}_links"}{$key} },
1254 { accno => $ref->{accno},
1255 description => $ref->{description},
1263 # get buchungsgruppen
1264 $form->{BUCHUNGSGRUPPEN} = selectall_hashref_query($form, $dbh, qq|SELECT id, description FROM buchungsgruppen|);
1267 $form->{payment_terms} = selectall_hashref_query($form, $dbh, qq|SELECT id, description FROM payment_terms ORDER BY sortkey|);
1270 ($form->{priceupdate}) = selectrow_query($form, $dbh, qq|SELECT current_date|);
1274 $main::lxdebug->leave_sub();
1277 # get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
1279 $main::lxdebug->enter_sub();
1281 my ($self, $myconfig, $form, $sortorder) = @_;
1282 my $dbh = $form->dbconnect($myconfig);
1283 my $order = qq| p.partnumber|;
1284 my $where = qq|1 = 1|;
1287 if ($sortorder eq "all") {
1288 $where .= qq| AND (partnumber ILIKE ?) AND (description ILIKE ?)|;
1289 push(@values, '%' . $form->{partnumber} . '%', '%' . $form->{description} . '%');
1291 } elsif ($sortorder eq "partnumber") {
1292 $where .= qq| AND (partnumber ILIKE ?)|;
1293 push(@values, '%' . $form->{partnumber} . '%');
1295 } elsif ($sortorder eq "description") {
1296 $where .= qq| AND (description ILIKE ?)|;
1297 push(@values, '%' . $form->{description} . '%');
1298 $order = "description";
1303 qq|SELECT id, partnumber, description, unit, sellprice
1305 WHERE $where ORDER BY $order|;
1307 my $sth = prepare_execute_query($form, $dbh, $query, @values);
1310 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1311 if (($ref->{partnumber} eq "*") && ($ref->{description} eq "")) {
1316 $form->{"id_$j"} = $ref->{id};
1317 $form->{"partnumber_$j"} = $ref->{partnumber};
1318 $form->{"description_$j"} = $ref->{description};
1319 $form->{"unit_$j"} = $ref->{unit};
1320 $form->{"sellprice_$j"} = $ref->{sellprice};
1321 $form->{"soldtotal_$j"} = get_soldtotal($dbh, $ref->{id});
1327 $main::lxdebug->leave_sub();
1332 # gets sum of sold part with part_id
1334 $main::lxdebug->enter_sub();
1336 my ($dbh, $id) = @_;
1338 my $query = qq|SELECT sum(qty) FROM invoice WHERE parts_id = ?|;
1339 my ($sum) = selectrow_query($form, $dbh, $query, conv_i($id));
1342 $main::lxdebug->leave_sub();
1345 } #end get_soldtotal
1347 sub retrieve_languages {
1348 $main::lxdebug->enter_sub();
1350 my ($self, $myconfig, $form) = @_;
1352 # connect to database
1353 my $dbh = $form->dbconnect($myconfig);
1358 if ($form->{language_values} ne "") {
1360 qq|SELECT l.id, l.description, tr.translation, tr.longdescription
1362 LEFT OUTER JOIN translation tr ON (tr.language_id = l.id) AND (tr.parts_id = ?)
1363 ORDER BY lower(l.description)|;
1364 @values = (conv_i($form->{id}));
1367 $query = qq|SELECT id, description
1369 ORDER BY lower(description)|;
1372 my $languages = selectall_hashref_query($form, $dbh, $query, @values);
1376 $main::lxdebug->leave_sub();
1381 sub follow_account_chain {
1382 $main::lxdebug->enter_sub(2);
1384 my ($self, $form, $dbh, $transdate, $accno_id, $accno) = @_;
1386 my @visited_accno_ids = ($accno_id);
1391 qq|SELECT c.new_chart_id, date($transdate) >= c.valid_from AS is_valid, | .
1394 qq|LEFT JOIN chart cnew ON c.new_chart_id = cnew.id | .
1395 qq|WHERE (c.id = ?) AND NOT c.new_chart_id ISNULL AND (c.new_chart_id > 0)|;
1396 $sth = prepare_query($form, $dbh, $query);
1399 do_statement($form, $sth, $query, $accno_id);
1400 $ref = $sth->fetchrow_hashref();
1401 last unless ($ref && $ref->{"is_valid"} &&
1402 !grep({ $_ == $ref->{"new_chart_id"} } @visited_accno_ids));
1403 $accno_id = $ref->{"new_chart_id"};
1404 $accno = $ref->{"accno"};
1405 push(@visited_accno_ids, $accno_id);
1408 $main::lxdebug->leave_sub(2);
1410 return ($accno_id, $accno);
1413 sub retrieve_accounts {
1414 $main::lxdebug->enter_sub(2);
1416 my ($self, $myconfig, $form, $parts_id, $index) = @_;
1418 my ($query, $sth, $dbh);
1420 $form->{"taxzone_id"} *= 1;
1422 $dbh = $form->get_standard_dbh($myconfig);
1425 if ($form->{type} eq "invoice") {
1426 if (($form->{vc} eq "vendor") || !$form->{deliverydate}) {
1427 $transdate = $form->{invdate};
1429 $transdate = $form->{deliverydate};
1431 } elsif (($form->{type} eq "credit_note") || ($form->{script} eq 'ir.pl')) {
1432 $transdate = $form->{invdate};
1434 $transdate = $form->{transdate};
1437 if ($transdate eq "") {
1438 $transdate = "current_date";
1440 $transdate = $dbh->quote($transdate);
1445 qq| p.inventory_accno_id AS is_part, | .
1446 qq| bg.inventory_accno_id, | .
1447 qq| bg.income_accno_id_$form->{taxzone_id} AS income_accno_id, | .
1448 qq| bg.expense_accno_id_$form->{taxzone_id} AS expense_accno_id, | .
1449 qq| c1.accno AS inventory_accno, | .
1450 qq| c2.accno AS income_accno, | .
1451 qq| c3.accno AS expense_accno | .
1453 qq|LEFT JOIN buchungsgruppen bg ON p.buchungsgruppen_id = bg.id | .
1454 qq|LEFT JOIN chart c1 ON bg.inventory_accno_id = c1.id | .
1455 qq|LEFT JOIN chart c2 ON bg.income_accno_id_$form->{taxzone_id} = c2.id | .
1456 qq|LEFT JOIN chart c3 ON bg.expense_accno_id_$form->{taxzone_id} = c3.id | .
1458 my $ref = selectfirst_hashref_query($form, $dbh, $query, $parts_id);
1460 return $main::lxdebug->leave_sub(2) if (!$ref);
1462 $ref->{"inventory_accno_id"} = undef unless ($ref->{"is_part"});
1465 foreach my $type (qw(inventory income expense)) {
1466 next unless ($ref->{"${type}_accno_id"});
1467 ($accounts{"${type}_accno_id"}, $accounts{"${type}_accno"}) =
1468 $self->follow_account_chain($form, $dbh, $transdate,
1469 $ref->{"${type}_accno_id"},
1470 $ref->{"${type}_accno"});
1473 map({ $form->{"${_}_accno_$index"} = $accounts{"${_}_accno"} }
1474 qw(inventory income expense));
1476 my $inc_exp = $form->{"vc"} eq "customer" ? "income" : "expense";
1477 my $accno_id = $accounts{"${inc_exp}_accno_id"};
1480 qq|SELECT c.accno, t.taxdescription AS description, t.rate, t.taxnumber | .
1482 qq|LEFT JOIN chart c ON c.id = t.chart_id | .
1483 qq|WHERE t.id IN | .
1484 qq| (SELECT tk.tax_id | .
1485 qq| FROM taxkeys tk | .
1486 qq| WHERE tk.chart_id = ? AND startdate <= | . quote_db_date($transdate) .
1487 qq| ORDER BY startdate DESC LIMIT 1) |;
1488 $ref = selectfirst_hashref_query($form, $dbh, $query, $accno_id);
1491 $main::lxdebug->leave_sub(2);
1495 $form->{"taxaccounts_$index"} = $ref->{"accno"};
1496 if ($form->{"taxaccounts"} !~ /$ref->{accno}/) {
1497 $form->{"taxaccounts"} .= "$ref->{accno} ";
1499 map({ $form->{"$ref->{accno}_${_}"} = $ref->{$_}; }
1500 qw(rate description taxnumber));
1502 # $main::lxdebug->message(0, "formvars: rate " . $form->{"$ref->{accno}_rate"} .
1503 # " description " . $form->{"$ref->{accno}_description"} .
1504 # " taxnumber " . $form->{"$ref->{accno}_taxnumber"} .
1505 # " || taxaccounts_$index " . $form->{"taxaccounts_$index"} .
1506 # " || taxaccounts " . $form->{"taxaccounts"});
1508 $main::lxdebug->leave_sub(2);
1511 sub get_basic_part_info {
1512 $main::lxdebug->enter_sub();
1517 Common::check_params(\%params, qw(id));
1519 my @ids = 'ARRAY' eq ref $params{id} ? @{ $params{id} } : ($params{id});
1522 $main::lxdebug->leave_sub();
1526 my $myconfig = \%main::myconfig;
1527 my $form = $main::form;
1529 my $dbh = $form->get_standard_dbh($myconfig);
1531 my $query = qq|SELECT id, partnumber, description, unit FROM parts WHERE id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)|;
1533 my $info = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
1535 if ('' eq ref $params{id}) {
1536 $info = $info->[0] || { };
1538 $main::lxdebug->leave_sub();
1542 my %info_map = map { $_->{id} => $_ } @{ $info };
1544 $main::lxdebug->leave_sub();
1549 sub prepare_parts_for_printing {
1550 $main::lxdebug->enter_sub();
1555 my $myconfig = \%main::myconfig;
1556 my $form = $main::form;
1558 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1560 my $prefix = $params{prefix} || 'id_';
1561 my $rowcount = defined $params{rowcount} ? $params{rowcount} : $form->{rowcount};
1563 my @part_ids = keys %{ { map { $_ => 1 } grep { $_ } map { $form->{"${prefix}${_}"} } (1 .. $rowcount) } };
1566 $main::lxdebug->leave_sub();
1570 my $placeholders = join ', ', ('?') x scalar(@part_ids);
1571 my $query = qq|SELECT mm.parts_id, mm.model, v.name AS make
1573 LEFT JOIN vendor v ON (mm.make = cast (v.id as text))
1574 WHERE mm.parts_id IN ($placeholders)|;
1578 my $sth = prepare_execute_query($form, $dbh, $query, @part_ids);
1580 while (my $ref = $sth->fetchrow_hashref()) {
1581 $makemodel{$ref->{parts_id}} ||= [];
1582 push @{ $makemodel{$ref->{parts_id}} }, $ref;
1587 my @columns = qw(ean image microfiche drawing weight);
1589 $query = qq|SELECT id, | . join(', ', @columns) . qq|
1591 WHERE id IN ($placeholders)|;
1593 my %data = selectall_as_map($form, $dbh, $query, 'id', \@columns, @part_ids);
1595 map { $form->{TEMPLATE_ARRAYS}{$_} = [] } (qw(make model), @columns);
1597 foreach my $i (1 .. $rowcount) {
1598 my $id = $form->{"${prefix}${i}"};
1602 foreach (@columns) {
1603 push @{ $form->{TEMPLATE_ARRAYS}{$_} }, $data{$id}->{$_};
1606 push @{ $form->{TEMPLATE_ARRAYS}{make} }, [];
1607 push @{ $form->{TEMPLATE_ARRAYS}{model} }, [];
1609 next if (!$makemodel{$id});
1611 foreach my $ref (@{ $makemodel{$id} }) {
1612 map { push @{ $form->{TEMPLATE_ARRAYS}{$_}->[-1] }, $ref->{$_} } qw(make model);
1616 $main::lxdebug->leave_sub();