2b5f2bb0a700decb10da3fcfb4f0d600d343dba3
[freeside.git] / httemplate / search / sales_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
69 #pretty bad false laziness w/agent_commission.html, lots of s/agent/sales/ :/
70 # everything above is completely untouched
71
72 die "access denied" 
73   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
74
75 my ($begin, $end) = FS::UI::Web::parse_beginning_ending($cgi);
76
77 $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 $salesnum = $1;
79 my $sales = FS::sales->by_key($salesnum);
80
81 my $title = $sales->salesperson . ' commissions';
82
83 my $sum_charged =
84   '(SELECT SUM(setup + recur) FROM cust_bill_pkg JOIN cust_bill USING (invnum)'.
85     'WHERE cust_bill_pkg.pkgnum = cust_pkg.pkgnum AND '.
86     "cust_bill._date >= $begin AND cust_bill._date < $end)";
87
88 my @select = (
89   'cust_pkg.*',
90   'sales_pkg_class.commission_percent AS percent',
91   "$sum_charged AS sum_charged",
92 );
93
94 my $query = {
95   'table'       => 'cust_pkg',
96   'select'      => join(',', @select),
97   'addl_from'   => '
98      JOIN cust_main  USING (custnum)
99      JOIN part_pkg   USING (pkgpart)
100      JOIN sales_pkg_class ON (
101        COALESCE(cust_pkg.salesnum,cust_main.salesnum) = sales_pkg_class.salesnum
102        AND
103        ( sales_pkg_class.classnum = part_pkg.classnum
104          OR (sales_pkg_class IS NULL AND part_pkg.classnum IS NULL)
105        )
106      )
107   ',
108   'extra_sql'   => "
109     WHERE COALESCE(cust_pkg.salesnum,cust_main.salesnum) = $salesnum
110       AND sales_pkg_class.commission_percent > 0
111       AND $sum_charged > 0
112   ",
113   'order_by'    => 'ORDER BY cust_pkg.custnum ASC',
114 };
115
116 my @cust_pkg = qsearch($query);
117
118 my $money_char = FS::Conf->new->config('money_char') || '$';
119
120 my $data = '';
121 my $type = $cgi->param('_type');
122 if ( $type eq 'xls') {
123   # some false laziness with the above...
124   my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format;
125   my $filename = 'sales_commission_pkg' . $format->{extension}; 
126   http_header('Content-Type' => $format->{mime_type});
127   http_header('Content-Disposition' => qq!attachment;filename="$filename"!);
128   my $XLS = IO::Scalar->new(\$data);
129   my $workbook = $format->{class}->new($XLS);
130   my $worksheet = $workbook->add_worksheet(substr($title, 0, 31));
131
132   my $cust_head_format = $workbook->add_format(
133     bold      => 1,
134     underline => 1,
135     text_wrap => 0,
136     bg_color  => 'white',
137   );
138
139   my $col_head_format = $workbook->add_format(
140     bold      => 1,
141     align     => 'center',
142     bg_color  => 'silver'
143   );
144
145   my @format;
146   foreach (0, 1) {
147     my %bg = (bg_color => $_ ? 'white' : 'silver');
148     $format[$_] = {
149       'text'    => $workbook->add_format(%bg),
150       'money'   => $workbook->add_format(%bg, num_format => $money_char.'#0.00'),
151       'percent' => $workbook->add_format(%bg, num_format => '0.00%'),
152     };
153   }
154   my $total_format = $workbook->add_format(
155     bg_color    => 'yellow',
156     num_format  => $money_char.'#0.00',
157     top         => 1
158   );
159
160   my ($r, $c) = (0, 0);
161   foreach (qw(Package Sales Percentage Commission)) {
162     $worksheet->write($r, $c++, $_, $col_head_format);
163   }
164   $r++;
165
166   my ($custnum, $sales, $commission, $row, $bgcolor) = (0, 0, 0, 0);
167   my $label_length = 0;
168   foreach my $cust_pkg ( @cust_pkg ) {
169     if ( $custnum ne $cust_pkg->custnum ) {
170       # start of a new customer section
171       my $cust_main = $cust_pkg->cust_main;
172       my $label = $cust_main->custnum . ': '. $cust_main->name;
173       $bgcolor = 0;
174       $worksheet->set_row($r, 20);
175       $worksheet->merge_range($r, 0, $r, 3, $label, $cust_head_format);
176       $r++;
177     }
178     $c = 0;
179     my $percent = $cust_pkg->percent / 100;
180     $worksheet->write($r, $c++, $cust_pkg->pkg_label, $format[$bgcolor]{text});
181     $worksheet->write($r, $c++, $cust_pkg->sum_charged, $format[$bgcolor]{money});
182     $worksheet->write($r, $c++, $percent, $format[$bgcolor]{percent});
183     $worksheet->write($r, $c++, ($cust_pkg->sum_charged * $percent),
184                                 $format[$bgcolor]{money});
185
186     $label_length = max($label_length, length($cust_pkg->pkg_label));
187     $sales += $cust_pkg->sum_charged;
188     $commission += $cust_pkg->sum_charged * $cust_pkg->percent / 100;
189     $row++;
190     $bgcolor = 1-$bgcolor;
191     $custnum = $cust_pkg->custnum;
192     $r++;
193   }
194
195   $c = 0;
196   $label_length = max($label_length, 20);
197   $worksheet->set_column($c, $c, $label_length);
198   $worksheet->write($r, $c++, mt('[quant,_1,package] with commission', $row),
199                                   $total_format);
200   $worksheet->set_column($c, $c + 2, 11);
201   $worksheet->write($r, $c++, $sales, $total_format);
202   $worksheet->write($r, $c++, '', $total_format);
203   $worksheet->write($r, $c++, $commission, $total_format);
204
205   $workbook->close;
206 }
207 </%init>