39c97226782fba85a3f8ffc61991cbe5b0a9812e
[freeside.git] / httemplate / graph / cust_bill_pkg.cgi
1 <% include('elements/monthly.html',
2    #Dumper(
3                 'title'        => $title,
4                 'graph_type'   => 'Mountain',
5                 'items'        => \@items,
6                 'params'       => \@params,
7                 'labels'       => \@labels,
8                 'graph_labels' => \@labels,
9                 'colors'       => \@colors,
10                 'links'        => \@links,
11                 'no_graph'     => \@no_graph,
12                 'remove_empty' => 1,
13                 'bottom_total' => 1,
14                 'bottom_link'  => $bottom_link,
15                 'agentnum'     => $agentnum,
16                 'cust_classnum'=> \@cust_classnums,
17              )
18 %>
19 <%init>
20
21 die "access denied"
22   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
23
24 my $link = "${p}search/cust_bill_pkg.cgi?nottax=1";
25 my $bottom_link = "$link;";
26
27 my $use_usage = $cgi->param('use_usage') || 0;
28 my $use_setup = $cgi->param('use_setup') || 0;
29 my $use_override         = $cgi->param('use_override')         ? 1 : 0;
30 my $average_per_cust_pkg = $cgi->param('average_per_cust_pkg') ? 1 : 0;
31 my $distribute           = $cgi->param('distribute')           ? 1 : 0;
32
33 my %charge_labels = (
34   'SR' => 'setup + recurring',
35   'RU' => 'recurring',
36   'S'  => 'setup',
37   'R'  => 'recurring',
38   'U'  => 'usage',
39 );
40
41 #XXX or virtual
42 my( $agentnum, $sel_agent, $all_agent ) = ('', '', '');
43 if ( $cgi->param('agentnum') eq 'all' ) {
44   $agentnum = 0;
45   $all_agent = 'ALL';
46 }
47 elsif ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
48   $agentnum = $1;
49   $bottom_link .= "agentnum=$agentnum;";
50   $sel_agent = qsearchs('agent', { 'agentnum' => $agentnum } );
51   die "agentnum $agentnum not found!" unless $sel_agent;
52 }
53 my $title = $sel_agent ? $sel_agent->agent.' ' : '';
54
55 my( $refnum, $sel_part_referral, $all_part_referral ) = ('', '', '');
56 if ( $cgi->param('refnum') eq 'all' ) {
57   $refnum = 0;
58   $all_part_referral = 'ALL';
59 }
60 elsif ( $cgi->param('refnum') =~ /^(\d+)$/ ) {
61   $refnum = $1;
62   $bottom_link .= "refnum=$refnum;";
63   $sel_part_referral = qsearchs('part_referral', { 'refnum' => $refnum } );
64   die "part_referral $refnum not found!" unless $sel_part_referral;
65 }
66 $title .= $sel_part_referral->referral.' '
67   if $sel_part_referral;
68
69 $title .= 'Sales Report (Gross)';
70 $title .= ', average per customer package'  if $average_per_cust_pkg;
71
72 my @cust_classnums = grep /^\d+$/, $cgi->param('cust_classnum');
73 $bottom_link .= "cust_classnum=$_;" foreach @cust_classnums;
74
75 #classnum (here)
76 # not specified: no longer happens (unless you de-select all classes)
77 # 0: empty class
78 # N: classnum
79 #classnum (link)
80 # not specified: all classes
81 # 0: empty class
82 # N: classnum
83
84 #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
85
86 my ($class_table, $name_col, $value_col, $class_param);
87
88 if ( $cgi->param('class_mode') eq 'report' ) {
89   $class_param = 'report_optionnum'; # CGI param name, also used in the report engine
90   $class_table = 'part_pkg_report_option'; # table containing classes
91   $name_col = 'name'; # the column of that table containing the label
92   $value_col = 'num'; # the column containing the class number
93 } else {
94   $class_param = 'classnum';
95   $class_table = 'pkg_class';
96   $name_col = 'classname';
97   $value_col = 'classnum';
98 }
99
100 my @classnums = grep /^\d+$/, $cgi->param($class_param);
101 my @classnames = map { if ( $_ ) {
102                          my $class = qsearchs($class_table, {$value_col=>$_} );
103                          $class->$name_col;
104                        } else {
105                          '(empty class)';
106                        }
107                      }
108                    @classnums;
109
110 $bottom_link .= "$class_param=$_;" foreach @classnums;
111
112 if ( $cgi->param('class_agg_break') eq 'aggregate' ) {
113
114   $title .= ' '. join(', ', @classnames)
115     unless scalar(@classnames) > scalar(qsearch($class_table,{'disabled'=>''}));
116                                  #not efficient for lots of package classes
117
118 } elsif ( $cgi->param('class_agg_break') eq 'breakdown' ) {
119
120   if ( $cgi->param('mode') eq 'report' ) {
121     # In theory, a package can belong to any subset of the report classes,
122     # so the report groups should be all the _subsets_, but for now we're
123     # handling the simple case where each package belongs to one report
124     # class. Packages with multiple classes will go into one bin at the
125     # end.
126     push @classnames, '(multiple classes)';
127     push @classnums, 'multiple';
128   }
129
130 } else {
131   die "guru meditation #434";
132 }
133
134 #eslaf
135
136 my @items  = ();
137 my @params = ();
138 my @labels = ();
139 my @colors = ();
140 my @links  = ();
141 my @no_graph;
142
143 my @components = ( 'SRU' );
144 # split/omit components as appropriate
145 if ( $use_setup == 1 ) {
146   @components = ( 'S', 'RU' );
147 }
148 elsif ( $use_setup == 2 ) {
149   @components = ( 'RU' );
150 }
151 if ( $use_usage == 1 ) {
152   $components[-1] =~ s/U//; push @components, 'U';
153 }
154 elsif ( $use_usage == 2 ) {
155   $components[-1] =~ s/U//;
156 }
157
158 # Categorization of line items goes
159 # Agent -> Referral -> Package class -> Component (setup/recur/usage)
160 # If per-agent totals are enabled, they go under the Agent level.
161 # There aren't any other kinds of subtotals.
162
163 my $anum = 0;
164 foreach my $agent ( $all_agent || $sel_agent || $FS::CurrentUser::CurrentUser->agents ) {
165
166   my @agent_colors = map { my $col = $cgi->param("agent$anum-color$_");
167                            $col =~ s/^#//;
168                            $col;
169                          }
170                        (0 .. 5);
171   my @colorbuf = ();
172
173   ### fixup the color handling for package classes...
174   ### and usage
175
176   foreach my $part_referral (
177     $all_part_referral ||
178     $sel_part_referral ||
179     qsearch('part_referral', { 'disabled' => '' } ) 
180   ) {
181
182     my @base_params = (
183                         'use_override'         => $use_override,
184                         'average_per_cust_pkg' => $average_per_cust_pkg,
185                         'distribute'           => $distribute,
186                       );
187
188     if ( $cgi->param('class_agg_break') eq 'aggregate' ) {
189
190       foreach my $component ( @components ) {
191
192         push @items, 'cust_bill_pkg';
193
194         push @labels,
195           ( $all_agent || $sel_agent ? '' : $agent->agent.' ' ).
196           ( $all_part_referral || $sel_part_referral ? '' : $part_referral->referral.' ' ).
197           $charge_labels{$component};
198
199         my $row_agentnum = $all_agent || $agent->agentnum;
200         my $row_refnum = $all_part_referral || $part_referral->refnum;
201         push @params, [
202                         @base_params,
203                         $class_param => \@classnums,
204                         ($all_agent ? () : ('agentnum' => $row_agentnum) ),
205                         ($all_part_referral ? () : ('refnum' => $row_refnum) ),
206                         'charges'              => $component,
207                       ];
208
209         my $rowlink = "$link;".
210                       ($all_agent ? '' : "agentnum=$row_agentnum;").
211                       ($all_part_referral ? '' : "refnum=$row_refnum;").
212                       (join('',map {"cust_classnum=$_;"} @cust_classnums)).
213                       "distribute=$distribute;".
214                       "use_override=$use_override;charges=$component;";
215         $rowlink .= "$class_param=$_;" foreach @classnums;
216         push @links, $rowlink;
217
218         @colorbuf = @agent_colors unless @colorbuf;
219         push @colors, shift @colorbuf;
220         push @no_graph, 0;
221
222       } #foreach $component
223
224     } elsif ( $cgi->param('class_agg_break') eq 'breakdown' ) {
225
226       for (my $i = 0; $i < scalar @classnums; $i++) {
227         my $row_classnum = $classnums[$i];
228         my $row_classname = $classnames[$i];
229         foreach my $component ( @components ) {
230
231           push @items, 'cust_bill_pkg';
232
233           push @labels,
234             ( $all_agent || $sel_agent ? '' : $agent->agent.' ' ).
235             ( $all_part_referral || $sel_part_referral ? '' : $part_referral->referral.' ' ).
236             $row_classname .  ' ' . $charge_labels{$component};
237
238           my $row_agentnum = $all_agent || $agent->agentnum;
239           my $row_refnum = $all_part_referral || $part_referral->refnum;
240           push @params, [
241                           @base_params,
242                           $class_param => $row_classnum,
243                           ($all_agent ? () : ('agentnum' => $row_agentnum) ),
244                           ($all_part_referral ? () : ('refnum' => $row_refnum)),
245                           'charges'              => $component,
246                         ];
247
248           push @links, "$link;".
249                        ($all_agent ? '' : "agentnum=$row_agentnum;").
250                        ($all_part_referral ? '' : "refnum=$row_refnum;").
251                        (join('',map {"cust_classnum=$_;"} @cust_classnums)).
252                        "$class_param=$row_classnum;".
253                        "distribute=$distribute;".
254                        "use_override=$use_override;charges=$component;";
255
256           @colorbuf = @agent_colors unless @colorbuf;
257           push @colors, shift @colorbuf;
258           push @no_graph, 0;
259
260         } #foreach $component
261       } #foreach $row_classnum
262
263     } #$cgi->param('class_agg_break')
264
265   } #foreach $part_referral
266
267   if ( $cgi->param('agent_totals') and !$all_agent ) {
268     my $row_agentnum = $agent->agentnum;
269     # Include all components that are anywhere on this report
270     my $component = join('', @components);
271
272     my @row_params = (  'agentnum'              => $row_agentnum,
273                         'cust_classnum'         => \@cust_classnums,
274                         'use_override'          => $use_override,
275                         'average_per_cust_pkg'  => $average_per_cust_pkg,
276                         'distribute'            => $distribute,
277                         'charges'               => $component,
278                      );
279     my $row_link = "$link;".
280                    "agentnum=$row_agentnum;".
281                    "distribute=$distribute;".
282                    "charges=$component;";
283     
284     # package class filters
285     if ( $cgi->param('class_agg_break') eq 'aggregate' ) {
286       push @row_params, $class_param => \@classnums;
287       $row_link .= "$class_param=$_;" foreach @classnums;
288     }
289
290     # refnum filters
291     if ( $sel_part_referral ) {
292       push @row_params, 'refnum' => $sel_part_referral->refnum;
293       $row_link .= "refnum=;".$sel_part_referral->refnum;
294     }
295
296     # customer class filters
297     $row_link .= "cust_classnum=$_;" foreach @cust_classnums;
298
299     push @items, 'cust_bill_pkg';
300     push @labels, mt('[_1] - Subtotal', $agent->agent);
301     push @params, \@row_params;
302     push @links, $row_link;
303     push @colors, '000000'; # better idea?
304     push @no_graph, 1;
305   }
306
307   $anum++;
308
309 }
310
311 #use Data::Dumper;
312 if ( $cgi->param('debug') == 1 ) {
313   $FS::Report::Table::DEBUG = 1;
314 }
315 </%init>