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 #here is the agent virtualization
69 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
71 my @where = ( $agentnums_sql );
73 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
74 push @where, "_date >= $beginning",
77 push @where , " payby != 'COMP' "
78 unless $cgi->param('include_comp_cust');
80 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
81 push @where, "cust_main.agentnum = $1";
84 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
86 push @where, "classnum IS NULL";
88 push @where, "classnum = $1";
92 if ( $cgi->param('out') ) {
96 SELECT COUNT(*) FROM cust_main_county
97 WHERE ( cust_main_county.county = cust_main.county
98 OR ( cust_main_county.county IS NULL AND cust_main.county = '' )
99 OR ( cust_main_county.county = '' AND cust_main.county IS NULL)
100 OR ( cust_main_county.county IS NULL AND cust_main.county IS NULL)
102 AND ( cust_main_county.state = cust_main.state
103 OR ( cust_main_county.state IS NULL AND cust_main.state = '' )
104 OR ( cust_main_county.state = '' AND cust_main.state IS NULL )
105 OR ( cust_main_county.state IS NULL AND cust_main.state IS NULL )
107 AND cust_main_county.country = cust_main.country
108 AND cust_main_county.tax > 0
112 } elsif ( $cgi->param('country' ) ) {
114 my $county = dbh->quote( $cgi->param('county') );
115 my $state = dbh->quote( $cgi->param('state') );
116 my $country = dbh->quote( $cgi->param('country') );
118 " ( county = $county OR $county = '' ) ",
119 " ( state = $state OR $state = '' ) ",
120 " country = $country "
122 if ( $cgi->param('taxname') ) {
123 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
124 #} elsif ( $cgi->param('taxnameNULL') {
126 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
129 push @where, ' taxclass = '. dbh->quote( $cgi->param('taxclass') )
130 if $cgi->param('taxclass');
132 if ( $cgi->param('taxclassNULL') ) {
134 my %hash = ( 'country' => scalar($cgi->param('country')) );
135 foreach (qw( state county )) {
136 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
138 my $cust_main_county = qsearchs('cust_main_county', \%hash);
139 die "unknown base region for empty taxclass" unless $cust_main_county;
141 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
142 push @where, $same_sql if $same_sql;
148 if ($cgi->param('itemdesc')) {
149 if ($cgi->param('itemdesc') eq 'Tax') {
150 push @where, "(itemdesc='Tax' OR itemdesc is null)";
152 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
155 push @where, 'pkgnum != 0' if $cgi->param('nottax');
156 push @where, 'pkgnum = 0' if $cgi->param('istax');
158 push @where, " tax = 'Y' " if $cgi->param('cust_tax');
161 if ( $cgi->param('pkg_tax') ) {
164 "SELECT COUNT(*), SUM(
165 ( CASE WHEN part_pkg.setuptax = 'Y'
166 THEN cust_bill_pkg.setup
171 ( CASE WHEN part_pkg.recurtax = 'Y'
172 THEN cust_bill_pkg.recur
179 push @where, "( ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
180 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
181 "( tax != 'Y' OR tax IS NULL )";
186 "SELECT COUNT(*), SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
190 my $where = ' WHERE '. join(' AND ', @where);
193 JOIN cust_bill USING ( invnum )
194 LEFT JOIN cust_main USING ( custnum )
198 LEFT JOIN cust_pkg USING ( pkgnum )
199 LEFT JOIN part_pkg USING ( pkgpart )
202 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
205 'table' => 'cust_bill_pkg',
206 'addl_from' => "$join_cust $join_pkg",
208 'select' => join(', ',
213 FS::UI::Web::cust_sql_fields(),
215 'extra_sql' => $where,
216 'order_by' => 'ORDER BY _date, billpkgnum',
219 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
220 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
222 my $conf = new FS::Conf;
223 my $money_char = $conf->config('money_char') || '$';