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