bugfix for sales report, #27078, from #25459
[freeside.git] / httemplate / graph / cust_bill_pkg.cgi
index 9660e5b..151b295 100644 (file)
@@ -84,20 +84,26 @@ $bottom_link .= "cust_classnum=$_;" foreach @cust_classnums;
 #started out as false lazinessish w/FS::cust_pkg::search_sql (previously search/cust_pkg.cgi), but not much left the sane now after #24776
 
 my ($class_table, $name_col, $value_col, $class_param);
+my $all_report_options;
 
 if ( $cgi->param('class_mode') eq 'report' ) {
   $class_param = 'report_optionnum'; # CGI param name, also used in the report engine
   $class_table = 'part_pkg_report_option'; # table containing classes
   $name_col = 'name'; # the column of that table containing the label
   $value_col = 'num'; # the column containing the class number
-} else {
+  # in 'exact' mode we want to run the query in ALL mode.
+  # in 'breakdown' mode want to run the query in ALL mode but using the 
+  # power set of the classes selected.
+  $all_report_options = 1
+    unless $cgi->param('class_agg_break') eq 'aggregate';
+} else { # class_mode eq 'pkg'
   $class_param = 'classnum';
   $class_table = 'pkg_class';
   $name_col = 'classname';
   $value_col = 'classnum';
 }
 
-my @classnums = grep /^\d+$/, $cgi->param($class_param);
+my @classnums = sort {$a <=> $b} grep /^\d+$/, $cgi->param($class_param);
 my @classnames = map { if ( $_ ) {
                          my $class = qsearchs($class_table, {$value_col=>$_} );
                          $class->$name_col;
@@ -106,10 +112,12 @@ my @classnames = map { if ( $_ ) {
                        }
                      }
                    @classnums;
+my @not_classnums;
 
 $bottom_link .= "$class_param=$_;" foreach @classnums;
 
-if ( $cgi->param('class_agg_break') eq 'aggregate' ) {
+if ( $cgi->param('class_agg_break') eq 'aggregate' or
+     $cgi->param('class_agg_break') eq 'exact' ) {
 
   $title .= ' '. join(', ', @classnames)
     unless scalar(@classnames) > scalar(qsearch($class_table,{'disabled'=>''}));
@@ -117,15 +125,28 @@ if ( $cgi->param('class_agg_break') eq 'aggregate' ) {
 
 } elsif ( $cgi->param('class_agg_break') eq 'breakdown' ) {
 
-  if ( $cgi->param('mode') eq 'report' ) {
-    # In theory, a package can belong to any subset of the report classes,
-    # so the report groups should be all the _subsets_, but for now we're
-    # handling the simple case where each package belongs to one report
-    # class. Packages with multiple classes will go into one bin at the
-    # end.
-    push @classnames, '(multiple classes)';
-    push @classnums, 'multiple';
+  if ( $cgi->param('class_mode') eq 'report' ) {
+    # The new way:
+    # Actually break down all subsets of the (selected) report classes.
+    my @subsets = FS::part_pkg_report_option->subsets(@classnums);
+    my @classnum_space = @classnums;
+    @classnums = @classnames = ();
+    while(@subsets) {
+      my $these = shift @subsets;
+      # applied topology!
+      my $not_these = [ @classnum_space ];
+      my $i = 0;
+      foreach (@$these) {
+        $i++ until $not_these->[$i] == $_;
+        splice(@$not_these, $i, 1);
+      }
+      push @classnums, $these;
+      push @not_classnums, $not_these;
+      push @classnames, shift @subsets;
+    } #while subsets
   }
+  # else it's 'pkg', i.e. part_pkg.classnum, which is singular on pkgpart
+  # and much simpler
 
 } else {
   die "guru meditation #434";
@@ -133,11 +154,6 @@ if ( $cgi->param('class_agg_break') eq 'aggregate' ) {
 
 #eslaf
 
-my $hue = 0;
-#my $hue_increment = 170;
-#my $hue_increment = 145;
-my $hue_increment = 125;
-
 my @items  = ();
 my @params = ();
 my @labels = ();
@@ -165,14 +181,15 @@ elsif ( $use_usage == 2 ) {
 # If per-agent totals are enabled, they go under the Agent level.
 # There aren't any other kinds of subtotals.
 
-foreach my $agent ( $all_agent || $sel_agent || qsearch('agent', { 'disabled' => '' } ) ) {
+my $anum = 0;
+foreach my $agent ( $all_agent || $sel_agent || $FS::CurrentUser::CurrentUser->agents ) {
 
-  my $col_scheme = Color::Scheme->new
-                     ->from_hue($hue) #->from_hex($agent->color)
-                     ->scheme('analogic')
-                   ;
-  my @recur_colors = ();
-  my @onetime_colors = ();
+  my @agent_colors = map { my $col = $cgi->param("agent$anum-color$_");
+                           $col =~ s/^#//;
+                           $col;
+                         }
+                       (0 .. 5);
+  my @colorbuf = ();
 
   ### fixup the color handling for package classes...
   ### and usage
@@ -189,7 +206,10 @@ foreach my $agent ( $all_agent || $sel_agent || qsearch('agent', { 'disabled' =>
                         'distribute'           => $distribute,
                       );
 
-    if ( $cgi->param('class_agg_break') eq 'aggregate' ) {
+    if ( $cgi->param('class_agg_break') eq 'aggregate' or
+         $cgi->param('class_agg_break') eq 'exact' ) {
+      # the only difference between 'aggregate' and 'exact' is whether
+      # we pass the 'all_report_options' flag.
 
       foreach my $component ( @components ) {
 
@@ -202,14 +222,16 @@ foreach my $agent ( $all_agent || $sel_agent || qsearch('agent', { 'disabled' =>
 
         my $row_agentnum = $all_agent || $agent->agentnum;
         my $row_refnum = $all_part_referral || $part_referral->refnum;
-        push @params, [
+        my @row_params = (
                         @base_params,
                         $class_param => \@classnums,
                         ($all_agent ? () : ('agentnum' => $row_agentnum) ),
                         ($all_part_referral ? () : ('refnum' => $row_refnum) ),
-                        'charges'              => $component,
-                      ];
+                        'charges'               => $component,
+        );
 
+        # XXX this is very silly.  we should cache it server-side and 
+        # just put a cache identifier in the link
         my $rowlink = "$link;".
                       ($all_agent ? '' : "agentnum=$row_agentnum;").
                       ($all_part_referral ? '' : "refnum=$row_refnum;").
@@ -217,13 +239,15 @@ foreach my $agent ( $all_agent || $sel_agent || qsearch('agent', { 'disabled' =>
                       "distribute=$distribute;".
                       "use_override=$use_override;charges=$component;";
         $rowlink .= "$class_param=$_;" foreach @classnums;
+        if ( $all_report_options ) {
+          push @row_params, 'all_report_options', 1;
+          $rowlink .= 'all_report_options=1';
+        }
+        push @params, \@row_params;
         push @links, $rowlink;
 
-        @recur_colors = ($col_scheme->colors)[0,4,8,1,5,9]
-          unless @recur_colors;
-        @onetime_colors = ($col_scheme->colors)[2,6,10,3,7,11]
-          unless @onetime_colors;
-        push @colors, shift @recur_colors;
+        @colorbuf = @agent_colors unless @colorbuf;
+        push @colors, shift @colorbuf;
         push @no_graph, 0;
 
       } #foreach $component
@@ -233,6 +257,14 @@ foreach my $agent ( $all_agent || $sel_agent || qsearch('agent', { 'disabled' =>
       for (my $i = 0; $i < scalar @classnums; $i++) {
         my $row_classnum = $classnums[$i];
         my $row_classname = $classnames[$i];
+        my $not_row_classnum = '';
+        if ( $class_param eq 'report_optionnum' ) {
+          # if we're working with report options, @classnums here contains 
+          # arrays of multiple classnums
+          $row_classnum = join(',', @$row_classnum);
+          $row_classname = join(', ', @$row_classname);
+          $not_row_classnum = join(',', @{ $not_classnums[$i] });
+        }
         foreach my $component ( @components ) {
 
           push @items, 'cust_bill_pkg';
@@ -244,27 +276,33 @@ foreach my $agent ( $all_agent || $sel_agent || qsearch('agent', { 'disabled' =>
 
           my $row_agentnum = $all_agent || $agent->agentnum;
           my $row_refnum = $all_part_referral || $part_referral->refnum;
-          push @params, [
+          my @row_params = (
                           @base_params,
                           $class_param => $row_classnum,
                           ($all_agent ? () : ('agentnum' => $row_agentnum) ),
                           ($all_part_referral ? () : ('refnum' => $row_refnum)),
                           'charges'              => $component,
-                        ];
-
-          push @links, "$link;".
+          );
+          my $row_link = "$link;".
                        ($all_agent ? '' : "agentnum=$row_agentnum;").
                        ($all_part_referral ? '' : "refnum=$row_refnum;").
                        (join('',map {"cust_classnum=$_;"} @cust_classnums)).
                        "$class_param=$row_classnum;".
                        "distribute=$distribute;".
                        "use_override=$use_override;charges=$component;";
-
-          @recur_colors = ($col_scheme->colors)[0,4,8,1,5,9]
-            unless @recur_colors;
-          @onetime_colors = ($col_scheme->colors)[2,6,10,3,7,11]
-            unless @onetime_colors;
-          push @colors, shift @recur_colors;
+          if ( $class_param eq 'report_optionnum' ) {
+            push @row_params,
+                          'all_report_options' => 1,
+                          'not_report_optionnum' => $not_row_classnum,
+            ;
+            $row_link .= "all_report_options=1;".
+                         "not_report_optionnum=$not_row_classnum;";
+          }
+          push @params, \@row_params;
+          push @links, $row_link;
+
+          @colorbuf = @agent_colors unless @colorbuf;
+          push @colors, shift @colorbuf;
           push @no_graph, 0;
 
         } #foreach $component
@@ -314,7 +352,7 @@ foreach my $agent ( $all_agent || $sel_agent || qsearch('agent', { 'disabled' =>
     push @no_graph, 1;
   }
 
-  $hue += $hue_increment;
+  $anum++;
 
 }