$query =
qq|SELECT a.id, a.reference, a.description, ac.transdate, ac.chart_id, | .
qq| $false AS invoice, ac.amount, 'gl' as module, | .
- qq§(SELECT accno||'--'||rate FROM tax LEFT JOIN chart ON (tax.chart_id=chart.id) WHERE tax.id = ac.tax_id) AS taxinfo, ac.source || ' ' || ac.memo AS memo § .
+ qq§(SELECT accno||'--'||rate FROM tax LEFT JOIN chart ON (tax.chart_id=chart.id) WHERE tax.id = (SELECT tax_id FROM taxkeys WHERE taxkey_id = ac.taxkey AND taxkeys.startdate <= ac.transdate ORDER BY taxkeys.startdate DESC LIMIT 1)) AS taxinfo, ac.source || ' ' || ac.memo AS memo § .
qq|FROM acc_trans ac, gl a | .
$dpt_join .
qq|WHERE | . $where . $dpt_where . $project .
qq|SELECT a.id, a.invnumber, c.name, ac.transdate, ac.chart_id, | .
qq| a.invoice, ac.amount, 'ar' as module, | .
- qq§(SELECT accno||'--'||rate FROM tax LEFT JOIN chart ON (tax.chart_id=chart.id) WHERE tax.id = ac.tax_id) AS taxinfo, ac.source || ' ' || ac.memo AS memo § .
+ qq§(SELECT accno||'--'||rate FROM tax LEFT JOIN chart ON (tax.chart_id=chart.id) WHERE tax.id = (SELECT tax_id FROM taxkeys WHERE taxkey_id = ac.taxkey AND taxkeys.startdate <= ac.transdate ORDER BY taxkeys.startdate DESC LIMIT 1)) AS taxinfo, ac.source || ' ' || ac.memo AS memo § .
qq|FROM acc_trans ac, customer c, ar a | .
$dpt_join .
qq|WHERE | . $where . $dpt_where . $project .
qq|SELECT a.id, a.invnumber, v.name, ac.transdate, ac.chart_id, | .
qq| a.invoice, ac.amount, 'ap' as module, | .
- qq§(SELECT accno||'--'||rate FROM tax LEFT JOIN chart ON (tax.chart_id=chart.id) WHERE tax.id = ac.tax_id) AS taxinfo, ac.source || ' ' || ac.memo AS memo § .
+ qq§(SELECT accno||'--'||rate FROM tax LEFT JOIN chart ON (tax.chart_id=chart.id) WHERE tax.id = (SELECT tax_id FROM taxkeys WHERE taxkey_id = ac.taxkey AND taxkeys.startdate <= ac.transdate ORDER BY taxkeys.startdate DESC LIMIT 1)) AS taxinfo, ac.source || ' ' || ac.memo AS memo § .
qq|FROM acc_trans ac, vendor v, ap a | .
$dpt_join .
qq|WHERE | . $where . $dpt_where . $project .
FROM acc_trans a
LEFT JOIN chart c ON (c.id = a.chart_id)
LEFT JOIN project p ON (p.id = a.project_id)
- LEFT JOIN tax t ON (t.id= a.tax_id)
+ LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
+ WHERE (tk.taxkey_id=a.taxkey) AND
+ ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
+ THEN tk.chart_id = a.chart_id
+ ELSE 1 = 1
+ END)
+ OR (c.link='%tax%')) AND
+ (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
WHERE a.trans_id = ?
AND a.fx_transaction = '0'
ORDER BY a.acc_trans_id, a.transdate|;
(SELECT c1.accno
FROM chart c1, tax t1
WHERE (t1.id = t.id) AND (c1.id = t.chart_id)) AS taxaccno,
- a.tax_id AS tax_id
+ (SELECT tk.tax_id
+ FROM taxkeys tk
+ WHERE (tk.chart_id = a.chart_id) AND (tk.startdate <= a.transdate)
+ ORDER BY tk.startdate desc LIMIT 1) AS tax_id
FROM acc_trans a
JOIN chart c ON (c.id = a.chart_id)
LEFT JOIN project p ON (p.id = a.project_id)
LEFT JOIN tax t ON
- (t.id = a.tax_id)
+ (t.id =
+ (SELECT tk.tax_id
+ FROM taxkeys tk
+ WHERE (tk.taxkey_id = a.taxkey) AND
+ ((CASE WHEN a.chart_id IN
+ (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
+ THEN tk.chart_id = a.chart_id
+ ELSE 1 = 1
+ END)
+ OR (c.link LIKE '%tax%'))
+ AND (startdate <= a.transdate)
+ ORDER BY startdate DESC LIMIT 1))
WHERE (a.trans_id = ?)
AND (a.fx_transaction = '0')
ORDER BY a.acc_trans_id, a.transdate|;