finish basic implemention of tax exemption by tax name hack, RT#5127
[freeside.git] / httemplate / search / report_tax.cgi
index dc93d51..e89c665 100755 (executable)
 % foreach my $region ( @regions ) {
 %
 %   my $link = '';
-%   if ( $region->{'label'} ne 'Total' ) {
-%     if ( $region->{'label'} eq $out ) {
-%       $link = ';out=1';
-%     } else {
-%       $link = ';'. $region->{'url_param'};
-%     }
+%   if ( $region->{'label'} eq $out ) {
+%     $link = ';out=1';
+%   } else {
+%     $link = ';'. $region->{'url_param'}
+%       if $region->{'url_param'};
 %   }
 %
 %   if ( $bgcolor eq $bgcolor1 ) {
       </TD>
 
 % unless ( $cgi->param('show_taxclasses') ) { 
+%       my $invlink = $region->{'url_param_inv'}
+%                       ? ';'. $region->{'url_param_inv'}
+%                       : $link;
+
         <<%$tdh%> ALIGN="right">
-          <A HREF="<% $baselink. $link %>;istax=1"
+          <A HREF="<% $baselink. $invlink %>;istax=1"
           ><% &$money_sprintf( $region->{'tax'} ) %></A>
         </TD>
 % } 
 %   foreach my $region ( @base_regions ) {
 %
 %     my $link = '';
-%     #if ( $region->{'label'} ne 'Total' ) {
-%       if ( $region->{'label'} eq $out ) {
-%         $link = ';out=1';
-%       } else {
-%         $link = ';'. $region->{'url_param'};
-%       }
-%     #}
+%     if ( $region->{'label'} eq $out ) {
+%       $link = ';out=1';
+%     } else {
+%       $link = ';'. $region->{'url_param'}
+%         if $region->{'url_param'};
+%     }
 %
 %     if ( $bgcolor eq $bgcolor1 ) {
 %       $bgcolor = $bgcolor2;
@@ -287,6 +289,9 @@ foreach my $r ( qsearch({ 'table'     => 'cust_main_county',
 
   my $label = getlabel($r);
   $regions{$label}->{'label'} = $label;
+
+  $regions{$label}->{$_} = $r->$_() for (qw( county state country )); #taxname?
+
   $regions{$label}->{'url_param'} =
     join(';', map "$_=".uri_escape($r->$_()),
                   qw( county state country taxname )
@@ -302,6 +307,8 @@ foreach my $r ( qsearch({ 'table'     => 'cust_main_county',
     $regions{$label}->{'url_param'} .= ';taxclass='. uri_escape($r->taxclass);
     #no, always#  if $cgi->param('show_taxclasses');
 
+    $regions{$label}->{'taxclass'} = $r->taxclass;
+
   } else {
 
     $regions{$label}->{'url_param'} .= ';taxclassNULL=1'
@@ -348,9 +355,25 @@ foreach my $r ( qsearch({ 'table'     => 'cust_main_county',
 #    );
 #  }
 
+  #false laziness -ish w/report_tax.cgi
+  my $cust_exempt;
+  if ( $r->taxname ) {
+    my $q_taxname = dbh->quote($r->taxname);
+    $cust_exempt =
+      "( tax = 'Y'
+         OR EXISTS ( SELECT 1 FROM cust_main_exemption
+                       WHERE cust_main_exemption.custnum = cust_main.custnum
+                         AND cust_main_exemption.taxname = $q_taxname
+                   )
+       )
+      ";
+  } else {
+    $cust_exempt = " tax = 'Y' ";
+  }
+
   my $x_cust = scalar_sql($r, \@param,
     "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)
-     $fromwhere AND $nottax AND tax = 'Y' "
+     $fromwhere AND $nottax AND $cust_exempt "
   );
 
   $regions{$label}->{'exempt_cust'} += $x_cust;
@@ -528,9 +551,13 @@ my @regions = keys %regions;
 @regions = grep &{$group_test}($_), @regions
   if $group_op;
 
-#now calculate totals
+#calculate totals
 my( $total, $tot_taxable, $tot_owed ) = ( 0, 0, 0 );
 my( $exempt_cust, $exempt_pkg, $exempt_monthly ) = ( 0, 0, 0 );
+my %taxclasses = ();
+my %county = ();
+my %state = ();
+my %country = ();
 foreach (@regions) {
   $total          += $regions{$_}->{'total'};
   $tot_taxable    += $regions{$_}->{'taxable'};
@@ -538,6 +565,35 @@ foreach (@regions) {
   $exempt_cust    += $regions{$_}->{'exempt_cust'};
   $exempt_pkg     += $regions{$_}->{'exempt_pkg'};
   $exempt_monthly += $regions{$_}->{'exempt_monthly'};
+  $taxclasses{$regions{$_}->{'taxclass'}} = 1
+    if $regions{$_}->{'taxclass'};
+  $county{$regions{$_}->{'county'}} = 1;
+  $state{$regions{$_}->{'state'}} = 1;
+  $country{$regions{$_}->{'country'}} = 1;
+}
+
+my $total_url_param = '';
+my $total_url_param_invoiced = '';
+if ( $group_op ) {
+
+  my @country = keys %country;
+  warn "WARNING: multiple countries on this grouped report; total links broken"
+    if scalar(@country) > 1;
+  my $country = $country[0];
+
+  my @state = keys %state;
+  warn "WARNING: multiple countries on this grouped report; total links broken"
+    if scalar(@state) > 1;
+  my $state = $state[0];
+
+  $total_url_param_invoiced =
+  $total_url_param =
+    'report_group='.uri_escape("$group_op $group_value").';'.
+    join(';', map 'taxclass='.uri_escape($_), keys %taxclasses );
+  $total_url_param .= ';'.
+    "country=$country;state=".uri_escape($state).';'.
+    join(';', map 'county='.uri_escape($_), keys %county ) ;
+
 }
 
 #ordering
@@ -551,9 +607,11 @@ my @base_regions =
   sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
   keys %base_regions;
 
+#add total line
 push @regions, {
   'label'          => 'Total',
-  'url_param'      => '',
+  'url_param'      => $total_url_param,
+  'url_param_inv'  => $total_url_param_invoiced,
   'total'          => $total,
   'exempt_cust'    => $exempt_cust,
   'exempt_pkg'     => $exempt_pkg,