- # get beginning balances
- $query =
- qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
- FROM acc_trans ac
- LEFT JOIN chart c ON (ac.chart_id = c.id)
- $dpt_join
- WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction $acc_cash_where
- $dpt_where
- $project
- GROUP BY c.accno, c.category, c.description |;
+ if ($options{beginning_balances}) {
+ foreach my $prefix (qw(from to)) {
+ next if ($form->{"${prefix}date"});
+
+ my $min_max = $prefix eq 'from' ? 'min' : 'max';
+ $query = qq|SELECT ${min_max}(transdate)
+ FROM acc_trans ac
+ $dpt_join
+ WHERE (1 = 1)
+ $dpt_where
+ $project|;
+ ($form->{"${prefix}date"}) = selectfirst_array_query($form, $dbh, $query);
+ }
+
+ # get beginning balances
+ $query =
+ qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
+ FROM acc_trans ac
+ LEFT JOIN chart c ON (ac.chart_id = c.id)
+ $dpt_join
+ WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction $acc_cash_where
+ $dpt_where
+ $project
+ GROUP BY c.accno, c.category, c.description |;