1 <% include( 'elements/search.html',
2 'title' => 'Line items',
3 'name' => 'line items',
5 'count_query' => $count_query,
6 'count_addl' => [ $money_char. '%.2f total', ],
14 FS::UI::Web::cust_header(),
18 sub { $_[0]->pkgnum > 0
20 : $_[0]->get('itemdesc')
22 #strikethrough or "N/A ($amount)" or something these when
23 # they're not applicable to pkg_tax search
24 sub { sprintf($money_char.'%.2f', shift->setup ) },
25 sub { sprintf($money_char.'%.2f', shift->recur ) },
27 sub { time2str('%b %d %Y', shift->_date ) },
28 \&FS::UI::Web::cust_fields,
37 ( map { $_ ne 'Cust. Status' ? $clink : '' }
38 FS::UI::Web::cust_header()
41 'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
49 FS::UI::Web::cust_colors(),
58 FS::UI::Web::cust_styles(),
65 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
67 my $conf = new FS::Conf;
69 #here is the agent virtualization
71 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
73 my @where = ( $agentnums_sql );
75 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
76 push @where, "_date >= $beginning",
79 push @where , " payby != 'COMP' "
80 unless $cgi->param('include_comp_cust');
82 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
83 push @where, "cust_main.agentnum = $1";
87 # not specified: all classes
90 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
92 push @where, "classnum IS NULL";
94 push @where, "classnum = $1";
98 push @where, map ' taxclass = '.dbh->quote($_), $cgi->param('taxclass')
99 if $cgi->param('taxclass')
100 && ! $cgi->param('istax'); #no part_pkg.taxclass in this case
101 #(should we save a taxclass or a link to taxnum
102 # in cust_bill_pkg or something like
103 # cust_bill_pkg_tax_location?)
107 # my $prefix = @_ ? shift : '';
109 # ( cust_main_county.county = $table.${prefix}.county
110 # OR ( cust_main_county.county IS NULL AND $table.${prefix}.county = '' )
111 # OR ( cust_main_county.county = '' AND $table.${prefix}.county IS NULL)
112 # OR ( cust_main_county.county IS NULL AND $table.${prefix}.county IS NULL)
114 # AND ( cust_main_county.state = $table.${prefix}.state
115 # OR ( cust_main_county.state IS NULL AND $table.${prefix}.state = '' )
116 # OR ( cust_main_county.state = '' AND $table.${prefix}.state IS NULL )
117 # OR ( cust_main_county.state IS NULL AND $table.${prefix}.state IS NULL )
119 # AND cust_main_county.country = $table.${prefix}.country
124 if ( $cgi->param('out') ) {
126 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
127 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
128 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
131 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
132 if $cgi->param('istax');
136 SELECT COUNT(*) FROM cust_main_county
137 WHERE cust_main_county.tax > 0
142 #not linked to by anything, but useful for debugging "out of taxable region"
143 if ( grep $cgi->param($_), qw( county state country ) ) {
145 my %ph = map { $_ => dbh->quote( $cgi->param($_) ) }
146 qw( county state country );
148 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
149 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
150 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
153 push @where, $loc_sql;
157 } elsif ( $cgi->param('country') ) {
159 my %ph = map { $_ => dbh->quote( $cgi->param($_) ) }
160 qw( county state country );
162 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
163 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
164 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
167 push @where, $loc_sql;
169 if ( $cgi->param('istax') ) {
170 if ( $cgi->param('taxname') ) {
171 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
172 #} elsif ( $cgi->param('taxnameNULL') {
174 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
176 } elsif ( $cgi->param('nottax') ) {
177 #what can we usefully do with "taxname" ???? look up a class???
179 #warn "neither nottax nor istax parameters specified";
182 if ( $cgi->param('taxclassNULL') ) {
184 my %hash = ( 'country' => scalar($cgi->param('country')) );
185 foreach (qw( state county )) {
186 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
188 my $cust_main_county = qsearchs('cust_main_county', \%hash);
189 die "unknown base region for empty taxclass" unless $cust_main_county;
191 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
192 push @where, $same_sql if $same_sql;
196 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
198 # this should really be shoved out to FS::cust_pkg->location_sql or something
199 # along with the code in report_newtax.cgi
202 'county' => 'tax_rate_location.county',
203 'state' => 'tax_rate_location.state',
204 'city' => 'tax_rate_location.city',
205 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
208 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
209 qw( county state city locationtaxid );
212 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
218 if ( $cgi->param('itemdesc') ) {
219 if ( $cgi->param('itemdesc') eq 'Tax' ) {
220 push @where, "(itemdesc='Tax' OR itemdesc is null)";
222 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
226 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
227 my ( $group_op, $group_value ) = ( $1, $2 );
228 if ( $group_op eq '=' ) {
229 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
230 push @where, 'itemdesc = '. dbh->quote($group_value);
231 } elsif ( $group_op eq '!=' ) {
232 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
234 die "guru meditation #00de: group_op $group_op\n";
239 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
240 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
242 push @where, " tax = 'Y' " if $cgi->param('cust_tax');
245 if ( $cgi->param('pkg_tax') ) {
250 ( CASE WHEN part_pkg.setuptax = 'Y'
251 THEN cust_bill_pkg.setup
256 ( CASE WHEN part_pkg.recurtax = 'Y'
257 THEN cust_bill_pkg.recur
264 push @where, "( ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
265 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
266 "( tax != 'Y' OR tax IS NULL )";
268 } elsif ( $cgi->param('taxable') ) {
270 my $setup_taxable = "(
271 CASE WHEN part_pkg.setuptax = 'Y'
273 ELSE cust_bill_pkg.setup
277 my $recur_taxable = "(
278 CASE WHEN part_pkg.recurtax = 'Y'
280 ELSE cust_bill_pkg.recur
285 SELECT COALESCE( SUM(amount), 0 ) FROM cust_tax_exempt_pkg
286 WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum
290 "SELECT COUNT(*), SUM( $setup_taxable + $recur_taxable - $exempt )";
293 #not tax-exempt package (setup or recur)
295 ( ( part_pkg.setuptax != 'Y' OR part_pkg.setuptax IS NULL )
296 AND cust_bill_pkg.setup > 0 )
298 ( ( part_pkg.recurtax != 'Y' OR part_pkg.recurtax IS NULL )
299 AND cust_bill_pkg.recur > 0 )
301 #not a tax_exempt customer
302 "( tax != 'Y' OR tax IS NULL )";
303 #not covered in full by a monthly tax exemption (texas tax)
304 "0 < ( $setup_taxable + $recur_taxable - $exempt )",
309 "SELECT COUNT(*), SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
313 my $where = ' WHERE '. join(' AND ', @where);
315 my $join_cust = ' JOIN cust_bill USING ( invnum )
316 LEFT JOIN cust_main USING ( custnum ) ';
320 if ( $cgi->param('nottax') ) {
322 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
323 LEFT JOIN part_pkg USING ( pkgpart ) ';
324 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
325 if $conf->exists('tax-pkg_address');
327 } elsif ( $cgi->param('istax') ) {
329 #false laziness w/report_tax.cgi $taxfromwhere
330 if ( $conf->exists('tax-pkg_address') ) {
331 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
332 LEFT JOIN cust_location USING ( locationnum ) ';
334 #quelle kludge, false laziness w/report_tax.cgi
335 $where =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g;
336 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
338 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ) '.
339 ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
345 warn "neiether nottax nor istax parameters specified";
347 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
348 LEFT JOIN part_pkg USING ( pkgpart ) ';
352 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
358 push @select, 'part_pkg.pkg' unless $cgi->param('istax');
359 push @select, 'cust_main.custnum',
360 FS::UI::Web::cust_sql_fields();
363 'table' => 'cust_bill_pkg',
364 'addl_from' => "$join_cust $join_pkg",
366 'select' => join(', ', @select ),
367 'extra_sql' => $where,
368 'order_by' => 'ORDER BY _date, billpkgnum',
371 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
372 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
374 my $conf = new FS::Conf;
375 my $money_char = $conf->config('money_char') || '$';