1 <& elements/search.html,
2 'title' => emt('Sales with advertising source'),
3 'name' => emt('line items'),
5 'count_query' => $count_query,
7 ($setup ? $money_char. '%.2f setup' : ()),
8 ($recur ? $money_char. '%.2f recurring' : ()),
9 ($usage ? $money_char. '%.2f usage' : ()),
13 ($setup ? emt('Setup') : ()),
14 ($recur ? emt('Recurring') : ()),
15 ($usage ? emt('Usage') : ()),
30 ($setup ? money_sub('setup') : ()),
31 ($recur ? money_sub('recur_no_usage') : ()),
32 ($usage ? money_sub('recur_usage') : ()),
38 my $cust_pkg = shift->cust_pkg;
39 $cust_pkg ? ucfirst($cust_pkg->status) : '';
43 my $cust_bill_pkg = shift;
44 my $pkgpart = $cust_bill_pkg->pkgpart_override
45 || $cust_bill_pkg->cust_pkg->pkgpart;
46 if ( !exists($report_classes{$pkgpart}) ) {
47 my $part_pkg = FS::part_pkg->by_key($pkgpart);
48 my %opts = $part_pkg->options;
49 $report_classes{$pkgpart} = [
50 map { /^report_option_(\d+)/ ?
51 $report_option_name{$1} :
56 join( '<BR>', @{ $report_classes{$pkgpart} });
60 'referral', # from query
65 ($setup ? 'setup' : ()),
66 ($recur ? 'recur_no_usage' : ()),
67 ($usage ? 'recur_usage' : ()),
81 '', #package/item desc
82 ('') x $x, #setup/recur/usage
91 $clink, #customer name
95 #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
96 'align' => 'l' . ('r' x $x) . 'rcrccccrlll',
97 'color' => [ ('') x (5 + $x),
99 my $cust_pkg = shift->cust_pkg;
100 $cust_pkg ? ucfirst($cust_pkg->statuscolor) : '';
113 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
115 my $conf = new FS::Conf;
117 my $setup = $cgi->param('setup') ? 1 : 0;
118 my $recur = $cgi->param('recur') ? 1 : 0;
119 my $usage = $cgi->param('usage') ? 1 : 0;
121 my $x = $setup + $recur + $usage;
123 my @select = ( 'cust_bill_pkg.*', 'cust_bill._date' );
124 my ($join_cust, $join_pkg ) = ('', '');
126 #here is the agent virtualization
128 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
130 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
132 my @where = ( $agentnums_sql,
133 'cust_bill_pkg.pkgnum != 0', # exclude taxes
134 "cust_bill._date >= $beginning",
135 "cust_bill._date <= $ending",
139 foreach my $status ($cgi->param('status')) {
140 if ( $status =~ /^([- a-z]+)$/ ) { #"one-time charge"
141 push @status_where, "'$status'";
144 if ( @status_where ) {
145 push @where, '('. FS::cust_pkg->status_sql.
146 ') IN (' . join(',', @status_where) .')';
150 foreach my $refnum ($cgi->param('refnum')) {
151 if ( $refnum =~ /^\d+$/ ) {
152 push @refnum, $refnum;
156 push @where, 'cust_main.refnum IN ('.join(',', @refnum).')';
159 my @cust_classnums = grep /^\d+$/, $cgi->param('cust_classnum');
160 if ( @cust_classnums ) {
161 push @where, 'cust_main.classnum IN ('.join(',', @cust_classnums).')';
164 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
165 push @where, "cust_main.agentnum = $1";
169 # not specified: all classes
172 my $use_override = 1; #$cgi->param('use_override');
173 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
176 $comparison = "IS NULL";
178 $comparison = "= $1";
181 if ( $use_override ) {
183 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
184 override.classnum $comparison AND pkgpart_override IS NOT NULL
187 push @where, "part_pkg.classnum $comparison";
192 my @report_option = grep /^\d+$/, ( $cgi->param('report_option') );
193 if ( @report_option ) {
194 @report_option = map { "'report_option_$_'" } @report_option;
195 push @where, "EXISTS(
196 SELECT 1 FROM part_pkg_option WHERE optionname IN (".
197 join(',', @report_option).") AND (
198 part_pkg_option.pkgpart = cust_pkg.pkgpart AND pkgpart_override IS NULL
199 OR part_pkg_option.pkgpart = pkgpart_override
205 FS::cust_bill_pkg->charged_sql('', '', setuprecur => 'setup');
207 FS::cust_bill_pkg->charged_sql('', '', setuprecur => 'recur', no_usage => 1);
208 my $usage_sql = FS::cust_bill_pkg->usage_sql;
210 # exclude zero-amount items
212 push @orwhere, "(cust_bill_pkg.setup > 0)" if $setup;
213 push @orwhere, "($recur_sql > 0)" if $recur;
214 push @orwhere, "($usage_sql > 0)" if $usage;
215 push @where, '('.join(' OR ', @orwhere).')' if @orwhere;
217 $join_cust = ' JOIN cust_bill USING ( invnum )
218 LEFT JOIN cust_main USING ( custnum )
219 LEFT JOIN part_referral USING ( refnum )
220 LEFT JOIN agent ON cust_main.agentnum = agent.agentnum
223 $join_pkg .= ' LEFT JOIN cust_pkg USING ( pkgnum )
224 LEFT JOIN part_pkg USING ( pkgpart )
225 LEFT JOIN part_pkg AS override
226 ON pkgpart_override = override.pkgpart
227 LEFT JOIN pkg_class ON '; #...
229 if ( $use_override ) {
230 # join to whichever pkgpart is appropriate
232 ( pkgpart_override IS NULL AND part_pkg.classnum = pkg_class.classnum )
233 OR ( pkgpart_override IS NOT NULL AND override.classnum = pkg_class.classnum )';
235 $join_pkg .= 'part_pkg.classnum = pkg_class.classnum';
238 my $where = ' WHERE '. join(' AND ', @where);
240 # setup and recurring only
241 my $count_query = "SELECT
243 ($setup ? ", SUM($setup_sql)" : '').
244 ($recur ? ", SUM($recur_sql)" : '').
245 ($usage ? ", SUM($usage_sql)" : '').
252 my $paid_sql = FS::cust_bill_pkg->paid_sql('', '');
253 my $last_pay_sql = "SELECT MAX(_date)
254 FROM cust_bill_pay JOIN cust_bill_pay_pkg USING (billpaynum)
255 WHERE cust_bill_pay_pkg.billpkgnum = cust_bill_pkg.billpkgnum";
257 push @select, 'part_pkg.pkg',
263 'part_referral.referral',
264 "($paid_sql) AS paid",
265 "($last_pay_sql) AS last_pay",
266 "($recur_sql) AS recur_no_usage",
267 "($usage_sql) AS recur_usage",
268 'pkg_class.classname',
273 'table' => 'cust_bill_pkg',
274 'addl_from' => "$join_cust $join_pkg",
276 'select' => join(",\n", @select ),
277 'extra_sql' => $where,
278 'order_by' => 'ORDER BY cust_bill._date, billpkgnum',
281 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
282 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
284 my $conf = new FS::Conf;
285 my $money_char = $conf->config('money_char') || '$';
287 my %report_classes; #cache
288 my %report_option_name =
289 map { $_->num => $_->name } qsearch('part_pkg_report_option', {});
291 # should this be in Mason.pm or something?
293 $conf ||= new FS::Conf;
294 $money_char ||= $conf->config('money_char') || '$';
297 $money_char . sprintf('%.2f', $_[0]->get($field));
304 my $value = $_[0]->get($field);
305 $value ? time2str('%b %d %Y', $value) : '';