1 <& elements/search.html,
2 'title' => emt("Unearned revenue - ".ucfirst($unearned_mode)) . ' (' .
3 time2str('%b %d %Y', $unearned) . ')',
4 'name' => emt('line items'),
6 'count_query' => $count_query,
7 'count_addl' => [ $money_char. '%.2f total',
8 $money_char. '%.2f unearned revenue'
10 'header' => [ map( {emt $_}
12 'Unearned', # depends on mode
13 'Recurring charge', #recur - usage
14 'Owed', #recur - usage - credits - payments
16 'Payment date', #of last payment
17 'Credit date', #of last credit
23 FS::UI::Web::cust_header(),
27 sub { $_[0]->pkgnum > 0
28 ? $_[0]->get('pkg') # possibly use override.pkg
29 : $_[0]->get('itemdesc') # but i think this correct
32 money_sub('unearned_revenue'),
34 money_sub('recur_no_usage'),
36 money_sub('owed_no_usage'),
38 money_sub('paid_no_usage'),
42 date_sub('last_credit'),
45 #Charge end, minus most of a day
46 date_sub('before_edate'),
51 \&FS::UI::Web::cust_fields,
55 # SQL expressions work as sort keys...
71 ( map { $_ ne 'Cust. Status' ? $clink : '' }
72 FS::UI::Web::cust_header()
75 'align' => 'lrrcrccrc'. FS::UI::Web::cust_aligns(),
78 FS::UI::Web::cust_colors(),
82 FS::UI::Web::cust_styles(),
87 # Separated from cust_bill_pkg.cgi to simplify things.
90 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
92 my $conf = new FS::Conf;
95 my $unearned_mode = '';
96 my $unearned_base = '';
97 my $unearned_sql = '';
99 my @select = ( 'cust_bill_pkg.*', 'cust_bill._date' );
100 my ($join_cust, $join_pkg ) = ('', '');
102 #here is the agent virtualization
104 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
106 my @where = ( $agentnums_sql );
108 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
109 push @where, "cust_main.agentnum = $1";
112 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
114 if ( $cgi->param('status') =~ /^([a-z]+)$/ ) {
115 push @where, FS::cust_main->cust_status_sql . " = '$1'";
118 push @where, "cust_bill._date >= $beginning",
119 "cust_bill._date <= $ending";
121 # cust_classnum (false laziness w/ elements/cust_main_dayranges.html, elements/cust_pay_or_refund.html, prepaid_income.html, cust_bill_pay.html, cust_bill_pkg.html, cust_bill_pkg_referral.html, cust_credit.html, cust_credit_refund.html, cust_main::Search::search_sql)
122 if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
123 my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
124 push @where, 'COALESCE( cust_main.classnum, 0) IN ( '.
125 join(',', map { $_ || '0' } @classnums ).
130 # no pkgclass, no taxclass, no tax location...
132 # unearned revenue mode
133 $cgi->param('date') =~ /^(\d+)$/
134 or die "date required";
137 $unearned_mode = $cgi->param('mode');
139 push @where, "cust_bill_pkg.sdate < $unearned",
140 "cust_bill_pkg.edate > $unearned",
141 "cust_bill_pkg.recur != 0",
142 "part_pkg.freq != '0'";
144 if ( !$cgi->param('include_monthly') ) {
146 "part_pkg.freq != '1'",
147 "part_pkg.freq NOT LIKE '%h'",
148 "part_pkg.freq NOT LIKE '%d'",
149 "part_pkg.freq NOT LIKE '%w'";
153 $unearned, #before this date
155 setuprecur => 'recur',
159 my $charged = FS::cust_bill_pkg->charged_sql(@opt);
160 push @select, "($charged) AS recur_no_usage";
162 my $owed_sql = FS::cust_bill_pkg->owed_sql(@opt);
163 push @select, "($owed_sql) AS owed_no_usage";
165 my $paid_sql = FS::cust_bill_pkg->paid_sql(@opt);
166 push @select, "$paid_sql AS paid_no_usage";
168 if ( $unearned_mode eq 'paid' ) {
169 # then use the amount paid, minus usage charges
170 $unearned_base = $paid_sql;
173 # use the amount billed, minus usage charges and credits
174 $unearned_base = "( $charged - " .
175 FS::cust_bill_pkg->credited_sql(@opt) . ' )';
177 # whatever we're using as the base, only show rows where it's positive
178 push @where, "$unearned_base > 0";
180 my $edate_zero = midnight_sql('edate');
181 my $sdate_zero = midnight_sql('sdate');
182 # $unearned is one second before midnight on the date requested for the report.
184 # suppress partial days for more accounting-like behavior
185 my $period = "CAST( ($edate_zero - $sdate_zero) / 86400.0 AS DECIMAL(10,0) )";
187 my $remaining = "GREATEST(
188 CAST( ($edate_zero - $unearned) / 86400.0 AS DECIMAL(10,0) ),
190 my $fraction = "$remaining / $period";
192 $unearned_sql = "CAST( $unearned_base * $fraction AS DECIMAL(10,2) )";
193 push @select, "$unearned_sql AS unearned_revenue";
195 # last payment/credit date
196 my %t = (pay => 'cust_bill_pay', credit => 'cust_credit_bill');
197 foreach my $x (qw(pay credit)) {
198 my $table = "cust_$x";
199 my $link_bill = $t{$x};
200 my $link_pkg = $link_bill.'_pkg';
201 my %pkey = map { $_ => dbdef->table($_)->primary_key }
202 ( $table, $link_bill );
204 my $last_date_sql = "SELECT MAX($table._date)
205 FROM $table JOIN $link_bill USING ($pkey{$table})
206 JOIN $link_pkg USING ($pkey{$link_bill})
207 WHERE $link_pkg.billpkgnum = cust_bill_pkg.billpkgnum
208 AND $table._date <= $unearned";
209 push @select, "($last_date_sql) AS last_$x";
212 push @select, '(edate - 82799) AS before_edate';
215 #no tax report group kludge
217 #usage always excluded
219 # always 'nottax', not 'istax'
220 $join_cust = ' JOIN cust_bill USING ( invnum ) '.
221 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
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 ';
228 my $where = ' WHERE '. join(' AND ', @where);
230 my $count_query = "SELECT COUNT(DISTINCT billpkgnum),
231 SUM( $unearned_base ), SUM( $unearned_sql )
232 FROM cust_bill_pkg $join_pkg $join_cust $where";
234 push @select, 'part_pkg.pkg',
237 FS::UI::Web::cust_sql_fields();
240 'table' => 'cust_bill_pkg',
241 'addl_from' => "$join_pkg $join_cust",
243 'select' => join(",\n", @select ),
244 'extra_sql' => $where,
245 'order_by' => 'ORDER BY cust_bill._date, billpkgnum',
248 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
249 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
251 my $conf = new FS::Conf;
252 my $money_char = $conf->config('money_char') || '$';
255 $conf ||= new FS::Conf;
256 $money_char ||= $conf->config('money_char') || '$';
259 $money_char . sprintf('%.2f', $_[0]->get($field));
266 my $value = $_[0]->get($field);
267 $value ? time2str('%b %d %Y', $value) : '';