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