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 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
160 push @where, "cust_main.agentnum = $1";
164 # not specified: all classes
167 my $use_override = 1; #$cgi->param('use_override');
168 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
171 $comparison = "IS NULL";
173 $comparison = "= $1";
176 if ( $use_override ) {
178 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
179 override.classnum $comparison AND pkgpart_override IS NOT NULL
182 push @where, "part_pkg.classnum $comparison";
187 my @report_option = grep /^\d+$/, ( $cgi->param('report_option') );
188 if ( @report_option ) {
189 @report_option = map { "'report_option_$_'" } @report_option;
190 push @where, "EXISTS(
191 SELECT 1 FROM part_pkg_option WHERE optionname IN (".
192 join(',', @report_option).") AND (
193 part_pkg_option.pkgpart = cust_pkg.pkgpart AND pkgpart_override IS NULL
194 OR part_pkg_option.pkgpart = pkgpart_override
200 FS::cust_bill_pkg->charged_sql('', '', setuprecur => 'setup');
202 FS::cust_bill_pkg->charged_sql('', '', setuprecur => 'recur', no_usage => 1);
203 my $usage_sql = FS::cust_bill_pkg->usage_sql;
205 # exclude zero-amount items
207 push @orwhere, "(cust_bill_pkg.setup > 0)" if $setup;
208 push @orwhere, "($recur_sql > 0)" if $recur;
209 push @orwhere, "($usage_sql > 0)" if $usage;
210 push @where, '('.join(' OR ', @orwhere).')' if @orwhere;
212 $join_cust = ' JOIN cust_bill USING ( invnum )
213 LEFT JOIN cust_main USING ( custnum )
214 LEFT JOIN part_referral USING ( refnum )
215 LEFT JOIN agent ON cust_main.agentnum = agent.agentnum
218 $join_pkg .= ' LEFT JOIN cust_pkg USING ( pkgnum )
219 LEFT JOIN part_pkg USING ( pkgpart )
220 LEFT JOIN part_pkg AS override
221 ON pkgpart_override = override.pkgpart
222 LEFT JOIN pkg_class ON '; #...
224 if ( $use_override ) {
225 # join to whichever pkgpart is appropriate
227 ( pkgpart_override IS NULL AND part_pkg.classnum = pkg_class.classnum )
228 OR ( pkgpart_override IS NOT NULL AND override.classnum = pkg_class.classnum )';
230 $join_pkg .= 'part_pkg.classnum = pkg_class.classnum';
233 my $where = ' WHERE '. join(' AND ', @where);
235 # setup and recurring only
236 my $count_query = "SELECT
238 ($setup ? ", SUM($setup_sql)" : '').
239 ($recur ? ", SUM($recur_sql)" : '').
240 ($usage ? ", SUM($usage_sql)" : '').
247 my $paid_sql = FS::cust_bill_pkg->paid_sql('', '');
248 my $last_pay_sql = "SELECT MAX(_date)
249 FROM cust_bill_pay JOIN cust_bill_pay_pkg USING (billpaynum)
250 WHERE cust_bill_pay_pkg.billpkgnum = cust_bill_pkg.billpkgnum";
252 push @select, 'part_pkg.pkg',
258 'part_referral.referral',
259 "($paid_sql) AS paid",
260 "($last_pay_sql) AS last_pay",
261 "($recur_sql) AS recur_no_usage",
262 "($usage_sql) AS recur_usage",
263 'pkg_class.classname',
268 'table' => 'cust_bill_pkg',
269 'addl_from' => "$join_cust $join_pkg",
271 'select' => join(",\n", @select ),
272 'extra_sql' => $where,
273 'order_by' => 'ORDER BY cust_bill._date, billpkgnum',
276 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
277 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
279 my $conf = new FS::Conf;
280 my $money_char = $conf->config('money_char') || '$';
282 my %report_classes; #cache
283 my %report_option_name =
284 map { $_->num => $_->name } qsearch('part_pkg_report_option', {});
286 # should this be in Mason.pm or something?
288 $conf ||= new FS::Conf;
289 $money_char ||= $conf->config('money_char') || '$';
292 $money_char . sprintf('%.2f', $_[0]->get($field));
299 my $value = $_[0]->get($field);
300 $value ? time2str('%b %d %Y', $value) : '';