- foreach my $r ( qsearch({ 'table' => 'cust_main_county', })) {
- my $taxnum = $r->taxnum;
- # set up a %regions entry for this region's tax label
- my $label = $r->label(%label_opt);
- next if $label eq $out;
- $regions{$label} ||= { label => $label };
-
- $regions{$label}->{$_} = $r->get($_) foreach @loc_params;
- $regions{$label}->{taxnums} ||= [];
- push @{ $regions{$label}->{taxnums} }, $r->taxnum;
-
- my %x; # keys are data items (like 'tax', 'exempt_cust', etc.)
- foreach my $k (keys %data) {
- next unless exists($data{$k}{$classnum}{$taxnum});
- $x{$k} = $data{$k}{$classnum}{$taxnum};
- $regions{$label}{$k} += $x{$k};
- if ( $k eq 'taxable' or $k =~ /^exempt/ ) {
- $regions{$label}->{'sales'} += $x{$k};
- }
- }
-
- my $owed = $data{'taxable'}{$classnum}{$taxnum} * ($r->tax/100);
- $regions{$label}->{'owed'} += $owed;
- $total{'owed'}{$classnum} += $owed;
-
- if ( defined($regions{$label}->{'rate'})
- && $regions{$label}->{'rate'} != $r->tax.'%' ) {
- $regions{$label}->{'rate'} = 'variable';
- } else {
- $regions{$label}->{'rate'} = $r->tax.'%';
- }
-
- if ( $cgi->param('show_taxclasses') ) {
- my $base_label = $r->label(%label_opt, 'with_taxclass' => 0);
- $base_regions{$base_label} ||=
- {
- label => $base_label,
- tax => 0,
- credit => 0,
- };
- $base_regions{$base_label}->{tax} += $x{tax};
- $base_regions{$base_label}->{credit} += $x{credit};
- }
-
- }
-
- my @regions = map { $_->{label} }
- sort {
- ($b eq $out) <=> ($a eq $out)
- or $a->{country} cmp $b->{country}
- or $a->{state} cmp $b->{state}
- or $a->{county} cmp $b->{county}
- or $a->{city} cmp $b->{city}
- }
- grep { $_->{sales} > 0 or $_->{tax} > 0 or $_->{credit} > 0 }
- values %regions;
-
- #tax-report_groups filtering
- @regions = grep &{$group_test}($_), @regions
- if $group_op;
-
- #calculate totals
- my %taxclasses = ();
- my %county = ();
- my %state = ();
- my %country = ();
- foreach my $label (@regions) {
- $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
- @regions =
- map $regions{$_},
- sort { $a cmp $b }
- @regions;
-
- my @base_regions =
- map $base_regions{$_},
- sort { $a cmp $b }
- keys %base_regions;
-
- #add "Out of taxable" and total lines
- if ( $total{out_sales}{$classnum} ) {
- my %out = (
- 'sales' => $total{out_sales}{$classnum},
- 'label' => $out,
- 'rate' => ''
- );
- push @regions, \%out;
- push @base_regions, \%out;
- }
-
- if ( @regions ) {
- my %class_total = map { $_ => $total{$_}{$classnum} } keys(%total);
- $class_total{is_total} = 1;
- $class_total{sales} = sum(
- @class_total{ 'taxable',
- 'out_sales',
- grep(/^exempt/, keys %class_total) }
- );
-
- push @regions, \%class_total;
- push @base_regions, \%class_total;
- }
-
- $pkgclass_data{$classname} = {
- classnum => $classnum,
- classname => $classname,
- regions => \@regions,
- base_regions => \@base_regions,
- };
-}
-
-if ( $with_pkgclass ) {
- my $class_zero = delete( $pkgclass_data{'Unclassified'} );
- @pkgclasses = map { $pkgclass_data{$_} }
- sort { $a cmp $b }
- keys %pkgclass_data;
- push @pkgclasses, $class_zero;
-
- my %grand_total = map {
- $_ => sum( values(%{ $total{$_} }) )
- } keys(%total);
-
- $grand_total{sales} = $all_sales;
-
- push @pkgclasses, {
- classnum => '',
- classname => 'Total',
- regions => [ \%grand_total ],
- base_regions => [ \%grand_total ],
- }
-} else {
- @pkgclasses = $pkgclass_data{''};
-}
-
-#--