fix top subtotals on refund reports
[freeside.git] / httemplate / search / agent_commission_pkg.html
1 %# still not a good way to do rows grouped by some field in a search.html 
2 %# report
3 % if ( $type eq 'xls' ) {
4 <% $data %>\
5 % } else {
6 <& /elements/header.html, $title &>
7 <P ALIGN="right" CLASS="noprint">
8 Download full results<BR>
9 as <A HREF="<% $cgi->self_url %>;_type=xls">Excel spreadsheet</A></P>
10 <BR>
11 <STYLE TYPE="text/css">
12 td.cust_head {
13   border-left: none;
14   border-right: none;
15   padding-top: 0.5em;
16   font-weight: bold;
17   background-color: #ffffff;
18 }
19 td.money { text-align: right; }
20 td.money:before { content: '<% $money_char %>'; }
21 .row0 { background-color: #eeeeee; }
22 .row1 { background-color: #ffffff; }
23 </STYLE>
24 <& /elements/table-grid.html &>
25   <TR STYLE="background-color: #cccccc">
26     <TH CLASS="grid">Package</TH>
27     <TH CLASS="grid">Sales</TH>
28     <TH CLASS="grid">Percentage</TH>
29     <TH CLASS="grid">Commission</TH>
30   </TR>
31 % my ($custnum, $sales, $commission, $row, $bgcolor) = (0, 0, 0, 0);
32 % foreach my $cust_pkg ( @cust_pkg ) {
33 %   if ( $custnum ne $cust_pkg->custnum ) {
34 %     # start of a new customer section
35 %     my $cust_main = $cust_pkg->cust_main;
36 %     $bgcolor = 0;
37   <TR>
38     <TD COLSPAN=4 CLASS="cust_head">
39       <A HREF="<%$p%>view/cust_main.cgi?<%$cust_main->custnum%>"><% $cust_main->display_custnum %>: <% $cust_main->name |h %></A>
40     </TD>
41   </TR>
42 %   }
43   <TR CLASS="row<% $bgcolor %>">
44     <TD CLASS="grid"><% $cust_pkg->pkg_label %></TD>
45     <TD CLASS="money"><% sprintf('%.2f', $cust_pkg->sum_charged) %></TD>
46     <TD ALIGN="right"><% $cust_pkg->percent %>%</TD>
47     <TD CLASS="money"><% sprintf('%.2f',
48                       $cust_pkg->sum_charged * $cust_pkg->percent / 100) %></TD>
49   </TR>
50 %   $sales += $cust_pkg->sum_charged;
51 %   $commission += $cust_pkg->sum_charged * $cust_pkg->percent / 100;
52 %   $row++;
53 %   $bgcolor = 1-$bgcolor;
54 %   $custnum = $cust_pkg->custnum;
55 % }
56   <TR STYLE="background-color: #f5f6be">
57     <TD CLASS="grid">
58       <% emt('[quant,_1,package] with commission', $row) %>
59     </TD>
60     <TD CLASS="money"><% sprintf('%.2f', $sales) %></TD>
61     <TD></TD>
62     <TD CLASS="money"><% sprintf('%.2f', $commission) %></TD>
63   </TR>
64 </TABLE>
65 <& /elements/footer.html &>
66 % }
67 <%init>
68 die "access denied" 
69   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
70
71 my ($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
72 $cgi->param('agentnum') =~ /^(\d+)$/ or die "bad agentnum";
73 my $agentnum = $1;
74 my $agent = FS::agent->by_key($agentnum);
75
76 my $title = $agent->agent . ' commissions';
77
78 my $sum_charged =
79   '(SELECT SUM(setup + recur) FROM cust_bill_pkg JOIN cust_bill USING (invnum)'.
80     'WHERE cust_bill_pkg.pkgnum = cust_pkg.pkgnum AND '.
81     "cust_bill._date >= $begin AND cust_bill._date < $end)";
82
83 my @select = (
84   'cust_pkg.*',
85   'agent_pkg_class.commission_percent AS percent',
86   "$sum_charged AS sum_charged",
87 );
88
89 my $query = {
90   'table'       => 'cust_pkg',
91   'select'      => join(',', @select),
92   'addl_from'   => 'JOIN cust_main  USING (custnum) '.
93                    'JOIN part_pkg   USING (pkgpart) '.
94                    'JOIN agent_pkg_class ON (  '.
95                      'cust_main.agentnum = agent_pkg_class.agentnum AND '.
96                      '( agent_pkg_class.classnum = part_pkg.classnum OR '.
97                      '(agent_pkg_class IS NULL AND part_pkg.classnum IS NULL)'.
98                      ' )  ) ',
99   'extra_sql'   => "WHERE cust_main.agentnum = $agentnum AND ".
100                    'agent_pkg_class.commission_percent > 0 AND '.
101                    "$sum_charged > 0",
102   'order_by'    => 'ORDER BY cust_pkg.custnum ASC',
103 };
104
105 my @cust_pkg = qsearch($query);
106
107 my $money_char = FS::Conf->new->config('money_char') || '$';
108
109 my $data = '';
110 my $type = $cgi->param('_type');
111 if ( $type eq 'xls') {
112   # some false laziness with the above...
113   my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format;
114   my $filename = 'agent_commission' . $format->{extension}; 
115   http_header('Content-Type' => $format->{mime_type});
116   http_header('Content-Disposition' => qq!attachment;filename="$filename"!);
117   my $XLS = IO::Scalar->new(\$data);
118   my $workbook = $format->{class}->new($XLS);
119   my $worksheet = $workbook->add_worksheet(substr($title, 0, 31));
120
121   my $cust_head_format = $workbook->add_format(
122     bold      => 1,
123     underline => 1,
124     text_wrap => 0,
125     bg_color  => 'white',
126   );
127
128   my $col_head_format = $workbook->add_format(
129     bold      => 1,
130     align     => 'center',
131     bg_color  => 'silver'
132   );
133
134   my @format;
135   foreach (0, 1) {
136     my %bg = (bg_color => $_ ? 'white' : 'silver');
137     $format[$_] = {
138       'text'    => $workbook->add_format(%bg),
139       'money'   => $workbook->add_format(%bg, num_format => $money_char.'#0.00'),
140       'percent' => $workbook->add_format(%bg, num_format => '0.00%'),
141     };
142   }
143   my $total_format = $workbook->add_format(
144     bg_color    => 'yellow',
145     num_format  => $money_char.'#0.00',
146     top         => 1
147   );
148
149   my ($r, $c) = (0, 0);
150   foreach (qw(Package Sales Percentage Commission)) {
151     $worksheet->write($r, $c++, $_, $col_head_format);
152   }
153   $r++;
154
155   my ($custnum, $sales, $commission, $row, $bgcolor) = (0, 0, 0, 0);
156   my $label_length = 0;
157   foreach my $cust_pkg ( @cust_pkg ) {
158     if ( $custnum ne $cust_pkg->custnum ) {
159       # start of a new customer section
160       my $cust_main = $cust_pkg->cust_main;
161       my $label = $cust_main->custnum . ': '. $cust_main->name;
162       $bgcolor = 0;
163       $worksheet->set_row($r, 20);
164       $worksheet->merge_range($r, 0, $r, 3, $label, $cust_head_format);
165       $r++;
166     }
167     $c = 0;
168     my $percent = $cust_pkg->percent / 100;
169     $worksheet->write($r, $c++, $cust_pkg->pkg_label, $format[$bgcolor]{text});
170     $worksheet->write($r, $c++, $cust_pkg->sum_charged, $format[$bgcolor]{money});
171     $worksheet->write($r, $c++, $percent, $format[$bgcolor]{percent});
172     $worksheet->write($r, $c++, ($cust_pkg->sum_charged * $percent),
173                                 $format[$bgcolor]{money});
174
175     $label_length = max($label_length, length($cust_pkg->pkg_label));
176     $sales += $cust_pkg->sum_charged;
177     $commission += $cust_pkg->sum_charged * $cust_pkg->percent / 100;
178     $row++;
179     $bgcolor = 1-$bgcolor;
180     $custnum = $cust_pkg->custnum;
181     $r++;
182   }
183
184   $c = 0;
185   $label_length = max($label_length, 20);
186   $worksheet->set_column($c, $c, $label_length);
187   $worksheet->write($r, $c++, mt('[quant,_1,package] with commission', $row),
188                                   $total_format);
189   $worksheet->set_column($c, $c + 2, 11);
190   $worksheet->write($r, $c++, $sales, $total_format);
191   $worksheet->write($r, $c++, '', $total_format);
192   $worksheet->write($r, $c++, $commission, $total_format);
193
194   $workbook->close;
195 }
196 </%init>