+ if (!$self->{id}) {
+
+ my $transdate = "current_date";
+ if ($self->{transdate}) {
+ $transdate = $dbh->quote($self->{transdate});
+ }
+
+ # now get the account numbers
+ $query = qq|
+ SELECT c.accno, c.description, c.link, c.taxkey_id, c.id AS chart_id, tk2.tax_id
+ FROM chart c
+ -- find newest entries in taxkeys
+ INNER JOIN (
+ SELECT chart_id, MAX(startdate) AS startdate
+ FROM taxkeys
+ WHERE (startdate <= $transdate)
+ GROUP BY chart_id
+ ) tk ON (c.id = tk.chart_id)
+ -- and load all of those entries
+ INNER JOIN taxkeys tk2
+ ON (tk.chart_id = tk2.chart_id AND tk.startdate = tk2.startdate)
+ WHERE (c.link LIKE ?)
+ ORDER BY c.accno|;
+
+ $sth = $dbh->prepare($query);
+
+ do_statement($self, $sth, $query, like($module));
+
+ $self->{accounts} = "";
+ while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
+
+ foreach my $key (split(/:/, $ref->{link})) {
+ if ($key =~ /\Q$module\E/) {
+
+ # cross reference for keys
+ $xkeyref{ $ref->{accno} } = $key;
+
+ push @{ $self->{"${module}_links"}{$key} },
+ { accno => $ref->{accno},
+ chart_id => $ref->{chart_id},
+ description => $ref->{description},
+ taxkey => $ref->{taxkey_id},
+ tax_id => $ref->{tax_id} };
+
+ $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
+ }
+ }
+ }
+ }
+
+ # get taxkeys and description
+ $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
+ $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
+
+ if (($module eq "AP") || ($module eq "AR")) {
+ # get tax rates and description
+ $query = qq|SELECT * FROM tax|;
+ $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
+ }
+
+ my $extra_columns = '';
+ $extra_columns .= 'a.direct_debit, ' if ($module eq 'AR') || ($module eq 'AP');
+
+ if ($self->{id}) {
+ $query =
+ qq|SELECT
+ a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid, a.deliverydate,
+ a.duedate, a.ordnumber, a.taxincluded, (SELECT cu.name FROM currencies cu WHERE cu.id=a.currency_id) AS currency, a.notes,
+ a.mtime, a.itime,
+ a.intnotes, a.department_id, a.amount AS oldinvtotal,
+ a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
+ a.globalproject_id, ${extra_columns}
+ c.name AS $table,
+ d.description AS department,
+ e.name AS employee
+ FROM $arap a
+ JOIN $table c ON (a.${table}_id = c.id)
+ LEFT JOIN employee e ON (e.id = a.employee_id)
+ LEFT JOIN department d ON (d.id = a.department_id)
+ WHERE a.id = ?|;
+ $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
+
+ foreach my $key (keys %$ref) {
+ $self->{$key} = $ref->{$key};
+ }
+ $self->{mtime} ||= $self->{itime};
+ $self->{lastmtime} = $self->{mtime};
+ my $transdate = "current_date";
+ if ($self->{transdate}) {
+ $transdate = $dbh->quote($self->{transdate});
+ }
+
+ # now get the account numbers
+ $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, c.id AS chart_id, tk.tax_id
+ FROM chart c
+ LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
+ WHERE c.link LIKE ?
+ AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
+ OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
+ ORDER BY c.accno|;