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