-#=====================================================================
+ #=====================================================================
# LX-Office ERP
# Copyright (C) 2004
# Based on SQL-Ledger Version 2.1.9
my ($query, $sth);
- $query =
- qq|SELECT c.new_chart_id, date($transdate) >= c.valid_from AS is_valid, | .
- qq| cnew.accno | .
- qq|FROM chart c | .
- qq|LEFT JOIN chart cnew ON c.new_chart_id = cnew.id | .
- qq|WHERE (c.id = ?) AND NOT c.new_chart_id IS NULL AND (c.new_chart_id > 0)|;
- $sth = prepare_query($form, $dbh, $query);
+ $form->{ACCOUNT_CHAIN_BY_ID} ||= {
+ map { $_->{id} => $_ }
+ selectall_hashref_query($form, $dbh, <<SQL, $transdate) };
+ SELECT c.id, c.new_chart_id, date(?) >= c.valid_from AS is_valid, cnew.accno
+ FROM chart c
+ LEFT JOIN chart cnew ON c.new_chart_id = cnew.id
+ WHERE NOT c.new_chart_id IS NULL AND (c.new_chart_id > 0)
+SQL
while (1) {
- do_statement($form, $sth, $query, $accno_id);
- my $ref = $sth->fetchrow_hashref();
+ my $ref = $form->{ACCOUNT_CHAIN_BY_ID}->{$accno_id};
last unless ($ref && $ref->{"is_valid"} &&
!grep({ $_ == $ref->{"new_chart_id"} } @visited_accno_ids));
$accno_id = $ref->{"new_chart_id"};
}
sub retrieve_accounts {
- $main::lxdebug->enter_sub(2);
+ $main::lxdebug->enter_sub;
my $self = shift;
my $myconfig = shift;
my $form = shift;
my $dbh = $form->get_standard_dbh;
- my %args = @_; # part_id => index
-
- my ($query, $sth);
+ my %args = @_; # index => part_id
$form->{taxzone_id} *= 1;
+ return unless grep $_, values %args; # shortfuse if no part_id supplied
+
# transdate madness.
my $transdate = "";
if ($form->{type} eq "invoice") {
$transdate = $dbh->quote($transdate);
}
#/transdate
+ my $inc_exp = $form->{"vc"} eq "customer" ? "income_accno_id" : "expense_accno_id";
- my $sth_accno = prepare_query($::form, $dbh, <<SQL);
+ my @part_ids = grep { $_ } values %args;
+ my $in = join ',', ('?') x @part_ids;
+
+ my %accno_by_part = map { $_->{id} => $_ }
+ selectall_hashref_query($form, $dbh, <<SQL, @part_ids);
SELECT
- p.inventory_accno_id AS is_part,
+ p.id, p.inventory_accno_id AS is_part,
bg.inventory_accno_id,
bg.income_accno_id_$form->{taxzone_id} AS income_accno_id,
bg.expense_accno_id_$form->{taxzone_id} AS expense_accno_id,
LEFT JOIN chart c1 ON bg.inventory_accno_id = c1.id
LEFT JOIN chart c2 ON bg.income_accno_id_$form->{taxzone_id} = c2.id
LEFT JOIN chart c3 ON bg.expense_accno_id_$form->{taxzone_id} = c3.id
- WHERE p.id = ?
+ WHERE p.id IN ($in)
SQL
- my $sth_tx = prepare_query($::form, $dbh, <<SQL);
+ my $sth_tax = prepare_query($::form, $dbh, <<SQL);
SELECT c.accno, t.taxdescription AS description, t.rate, t.taxnumber
FROM tax t
LEFT JOIN chart c ON c.id = t.chart_id
ORDER BY startdate DESC LIMIT 1)
SQL
- while (my ($part_id, $index) = each %args) {
- my $ref = $sth_accno->fetchrow_hashref($part_id) or next;
+ while (my ($index => $part_id) = each %args) {
+ my $ref = $accno_by_part{$part_id} or next;
$ref->{"inventory_accno_id"} = undef unless $ref->{"is_part"};
$form->{"${_}_accno_$index"} = $accounts{"${_}_accno"} for qw(inventory income expense);
- my $inc_exp = $form->{"vc"} eq "customer" ? "income" : "expense";
- $ref = $sth->fetchrow_hashref($accounts{"${inc_exp}_accno_id"}, quote_db_date($transdate)) or next;
+ $sth_tax->execute($accounts{$inc_exp}, quote_db_date($transdate));
+ $ref = $sth_tax->fetchrow_hashref or next;
$form->{"taxaccounts_$index"} = $ref->{"accno"};
$form->{"taxaccounts"} .= "$ref->{accno} "if $form->{"taxaccounts"} !~ /$ref->{accno}/;
$form->{"$ref->{accno}_${_}"} = $ref->{$_} for qw(rate description taxnumber);
}
- $::lxdebug->leave_sub(2);
+ $sth_tax->finish;
+
+ $::lxdebug->leave_sub;
}
sub get_basic_part_info {