fix unapplied payment report, RT#73048, fallout from #25944
[freeside.git] / httemplate / search / report_tax-xls.cgi
index 773b403..30b32e8 100755 (executable)
@@ -13,9 +13,7 @@ my %params = (
   beginning => $beginning,
   ending    => $ending,
 );
-$params{country} = $cgi->param('country');
 $params{debug}   = $DEBUG;
-$params{breakdown} = { map { $_ => 1 } $cgi->param('breakdown') };
 
 my $agentname;
 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
@@ -24,14 +22,38 @@ if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
   $agentname = $agent->agentname;
 }
 
-if ( $cgi->param('taxname') =~ /^([\w ]+)$/ ) {
-  $params{taxname} = $1;
+# credit date behavior: limit by the date of the credit application, or
+# the invoice?
+if ( $cgi->param('credit_date') eq 'cust_credit_bill' ) {
+  $params{credit_date} = 'cust_credit_bill';
 } else {
-  die "taxname required";
+  $params{credit_date} = 'cust_bill';
+}
+
+my $all = $cgi->param('all');
+my $report_class;
+
+if ( $all ) {
+  $report_class = 'FS::Report::Tax::All';
+} else {
+  $report_class = 'FS::Report::Tax::ByName';
+  $params{country} = $cgi->param('country');
+  $params{breakdown} = { map { $_ => 1 } $cgi->param('breakdown') };
+
+  # allow anything in here; FS::Report::Tax will treat it as unsafe
+  if ( length($cgi->param('taxname')) ) {
+    $params{taxname} = $cgi->param('taxname');
+  } else {
+    die "taxname required";
+  }
+}
+
+if ($DEBUG) {
+  warn "REPORT: $report_class\nPARAMS:\n".Dumper(\%params)."\n\n";
 }
 
 # generate the report
-my $report = FS::Report::Tax->report_internal(%params);
+my $report = $report_class->report(%params);
 my @rows = $report->table; # array of hashrefs
 
 my %pkgclass_name = map { $_->classnum, $_->classname } qsearch('pkg_class');
@@ -207,11 +229,11 @@ foreach my $row (@rows) {
   $x++;
   $ws->write_string($y, $x, " \N{U+2212} ", $f->{bigmath}); # MINUS SIGN
   $x++;
-  $ws->write($y, $x, $row->{credit} || 0, $f->{currency});
+  $ws->write($y, $x, $row->{tax_credited} || 0, $f->{currency});
   $x++;
   $ws->write_string($y, $x, " = ", $f->{bigmath});
   $x++;
-  $ws->write($y, $x, $row->{tax} - $row->{credit}, $f->{currency});
+  $ws->write($y, $x, $row->{tax} - $row->{tax_credited}, $f->{currency});
   $x++;
   $ws->write($y, $x, $row->{tax_paid} || 0, $f->{currency});
 
@@ -221,11 +243,11 @@ foreach my $row (@rows) {
 }
 
 # at the end of everything
-if ( $report->{outside} > 0 ) {
+if ( $report->{out_sales} > 0 ) {
   my $f = $format[0];
   $ws->set_row($y, 30); # height
   $ws->write($y, 0, mt('Out of taxable region'), $f->{rowhead_outside});
-  $ws->write($y, 1, $report->{outside}, $f->{currency_outside});
+  $ws->write($y, 1, $report->{out_sales}, $f->{currency_outside});
   $y++;
 }
 
@@ -292,6 +314,15 @@ foreach my $row (@rows) {
   $prev_row = $row;
 }
 
+if ( $report->{out_credit} > 0 ) {
+  my $f = $format[0];
+  $ws->set_row($y, 30); # height
+  $ws->write($y, 0, mt('Out of taxable region'), $f->{rowhead_outside});
+  $ws->write($y, 1, $report->{out_credit}, $f->{currency_outside});
+  $y++;
+}
+
+
 for my $x (0..4) {
   $ws->set_column($x, $x, $widths[$x]);
 }