1 %# still not a good way to do rows grouped by some field in a search.html
3 % if ( $type eq 'xls' ) {
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>
11 <STYLE TYPE="text/css">
17 background-color: #ffffff;
19 td.money { text-align: right; }
20 td.money:before { content: '<% $money_char %>'; }
21 .row0 { background-color: #eeeeee; }
22 .row1 { background-color: #ffffff; }
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>
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;
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>
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>
50 % $sales += $cust_pkg->sum_charged;
51 % $commission += $cust_pkg->sum_charged * $cust_pkg->percent / 100;
53 % $bgcolor = 1-$bgcolor;
54 % $custnum = $cust_pkg->custnum;
56 <TR STYLE="background-color: #f5f6be">
58 <% emt('[quant,_1,package] with commission', $row) %>
60 <TD CLASS="money"><% sprintf('%.2f', $sales) %></TD>
62 <TD CLASS="money"><% sprintf('%.2f', $commission) %></TD>
65 <& /elements/footer.html &>
69 #pretty bad false laziness w/agent_commission.html, lots of s/agent/sales/ :/
72 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
74 my ($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
76 $cgi->param('salesnum') =~ /^(\d+)$/ or die "No sales person selected"; #better error handling of this case (or, better javascript that doesn't let you submit)
78 my $sales = FS::sales->by_key($salesnum);
80 my $title = $sales->salesperson . ' commissions';
83 '(SELECT SUM(setup + recur) FROM cust_bill_pkg JOIN cust_bill USING (invnum)'.
84 'WHERE cust_bill_pkg.pkgnum = cust_pkg.pkgnum AND '.
85 "cust_bill._date >= $begin AND cust_bill._date < $end)";
89 'sales_pkg_class.commission_percent AS percent',
90 "$sum_charged AS sum_charged",
94 'table' => 'cust_pkg',
95 'select' => join(',', @select),
97 JOIN cust_main USING (custnum)
98 JOIN part_pkg USING (pkgpart)
99 JOIN sales_pkg_class ON (
100 COALESCE(cust_pkg.salesnum,cust_main.salesnum) = sales_pkg_class.salesnum
102 ( sales_pkg_class.classnum = part_pkg.classnum
103 OR (sales_pkg_class IS NULL AND part_pkg.classnum IS NULL)
108 WHERE COALESCE(cust_pkg.salesnum,cust_main.salesnum) = $salesnum
109 AND sales_pkg_class.commission_percent > 0
112 'order_by' => 'ORDER BY cust_pkg.custnum ASC',
115 my @cust_pkg = qsearch($query);
117 my $money_char = FS::Conf->new->config('money_char') || '$';
120 my $type = $cgi->param('_type');
121 if ( $type eq 'xls') {
122 # some false laziness with the above...
123 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format;
124 my $filename = 'sales_commission_pkg' . $format->{extension};
125 http_header('Content-Type' => $format->{mime_type});
126 http_header('Content-Disposition' => qq!attachment;filename="$filename"!);
127 my $XLS = IO::Scalar->new(\$data);
128 my $workbook = $format->{class}->new($XLS);
129 my $worksheet = $workbook->add_worksheet(substr($title, 0, 31));
131 my $cust_head_format = $workbook->add_format(
138 my $col_head_format = $workbook->add_format(
146 my %bg = (bg_color => $_ ? 'white' : 'silver');
148 'text' => $workbook->add_format(%bg),
149 'money' => $workbook->add_format(%bg, num_format => $money_char.'#0.00'),
150 'percent' => $workbook->add_format(%bg, num_format => '0.00%'),
153 my $total_format = $workbook->add_format(
154 bg_color => 'yellow',
155 num_format => $money_char.'#0.00',
159 my ($r, $c) = (0, 0);
160 foreach (qw(Package Sales Percentage Commission)) {
161 $worksheet->write($r, $c++, $_, $col_head_format);
165 my ($custnum, $sales, $commission, $row, $bgcolor) = (0, 0, 0, 0);
166 my $label_length = 0;
167 foreach my $cust_pkg ( @cust_pkg ) {
168 if ( $custnum ne $cust_pkg->custnum ) {
169 # start of a new customer section
170 my $cust_main = $cust_pkg->cust_main;
171 my $label = $cust_main->custnum . ': '. $cust_main->name;
173 $worksheet->set_row($r, 20);
174 $worksheet->merge_range($r, 0, $r, 3, $label, $cust_head_format);
178 my $percent = $cust_pkg->percent / 100;
179 $worksheet->write($r, $c++, $cust_pkg->pkg_label, $format[$bgcolor]{text});
180 $worksheet->write($r, $c++, $cust_pkg->sum_charged, $format[$bgcolor]{money});
181 $worksheet->write($r, $c++, $percent, $format[$bgcolor]{percent});
182 $worksheet->write($r, $c++, ($cust_pkg->sum_charged * $percent),
183 $format[$bgcolor]{money});
185 $label_length = max($label_length, length($cust_pkg->pkg_label));
186 $sales += $cust_pkg->sum_charged;
187 $commission += $cust_pkg->sum_charged * $cust_pkg->percent / 100;
189 $bgcolor = 1-$bgcolor;
190 $custnum = $cust_pkg->custnum;
195 $label_length = max($label_length, 20);
196 $worksheet->set_column($c, $c, $label_length);
197 $worksheet->write($r, $c++, mt('[quant,_1,package] with commission', $row),
199 $worksheet->set_column($c, $c + 2, 11);
200 $worksheet->write($r, $c++, $sales, $total_format);
201 $worksheet->write($r, $c++, '', $total_format);
202 $worksheet->write($r, $c++, $commission, $total_format);