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";
86 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
88 push @where, "classnum IS NULL";
90 push @where, "classnum = $1";
96 my $prefix = @_ ? shift : '';
98 ( cust_main_county.county = $table.${prefix}.county
99 OR ( cust_main_county.county IS NULL AND $table.${prefix}.county = '' )
100 OR ( cust_main_county.county = '' AND $table.${prefix}.county IS NULL)
101 OR ( cust_main_county.county IS NULL AND $table.${prefix}.county IS NULL)
103 AND ( cust_main_county.state = $table.${prefix}.state
104 OR ( cust_main_county.state IS NULL AND $table.${prefix}.state = '' )
105 OR ( cust_main_county.state = '' AND $table.${prefix}.state IS NULL )
106 OR ( cust_main_county.state IS NULL AND $table.${prefix}.state IS NULL )
108 AND cust_main_county.country = $table.${prefix}.country
113 if ( $cgi->param('out') ) {
115 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
116 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
117 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
120 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
121 if $cgi->param('istax');
125 SELECT COUNT(*) FROM cust_main_county
126 WHERE cust_main_county.tax > 0
131 } elsif ( $cgi->param('country' ) ) {
133 my %ph = map { $_ => dbh->quote( $cgi->param($_) ) }
134 qw( county state country );
136 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
137 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
138 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
141 push @where, $loc_sql;
143 if ( $cgi->param('istax') ) {
144 if ( $cgi->param('taxname') ) {
145 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
146 #} elsif ( $cgi->param('taxnameNULL') {
148 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
150 } elsif ( $cgi->param('nottax') ) {
151 #what can we usefully do with "taxname" ???? look up a class???
153 #warn "neither nottax nor istax parameters specified";
156 push @where, ' taxclass = '. dbh->quote( $cgi->param('taxclass') )
157 if $cgi->param('taxclass');
159 if ( $cgi->param('taxclassNULL') ) {
161 my %hash = ( 'country' => scalar($cgi->param('country')) );
162 foreach (qw( state county )) {
163 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
165 my $cust_main_county = qsearchs('cust_main_county', \%hash);
166 die "unknown base region for empty taxclass" unless $cust_main_county;
168 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
169 push @where, $same_sql if $same_sql;
175 if ($cgi->param('itemdesc')) {
176 if ($cgi->param('itemdesc') eq 'Tax') {
177 push @where, "(itemdesc='Tax' OR itemdesc is null)";
179 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
182 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
183 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
185 push @where, " tax = 'Y' " if $cgi->param('cust_tax');
188 if ( $cgi->param('pkg_tax') ) {
191 "SELECT COUNT(*), SUM(
192 ( CASE WHEN part_pkg.setuptax = 'Y'
193 THEN cust_bill_pkg.setup
198 ( CASE WHEN part_pkg.recurtax = 'Y'
199 THEN cust_bill_pkg.recur
206 push @where, "( ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
207 OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
208 "( tax != 'Y' OR tax IS NULL )";
213 "SELECT COUNT(*), SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
217 my $where = ' WHERE '. join(' AND ', @where);
219 my $join_cust = ' JOIN cust_bill USING ( invnum )
220 LEFT JOIN cust_main USING ( custnum ) ';
224 if ( $cgi->param('nottax') ) {
226 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
227 LEFT JOIN part_pkg USING ( pkgpart ) ';
228 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
229 if $conf->exists('tax-pkg_address');
231 } elsif ( $cgi->param('istax') ) {
233 #false laziness w/report_tax.cgi $taxfromwhere
234 if ( $conf->exists('tax-pkg_address') ) {
235 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
236 LEFT JOIN cust_location USING ( locationnum ) ';
238 #quelle kludge, false laziness w/report_tax.cgi
239 $where =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g;
245 warn "neiether nottax nor istax parameters specified";
247 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
248 LEFT JOIN part_pkg USING ( pkgpart ) ';
252 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
258 push @select, 'part_pkg.pkg' unless $cgi->param('istax');
259 push @select, 'cust_main.custnum',
260 FS::UI::Web::cust_sql_fields();
263 'table' => 'cust_bill_pkg',
264 'addl_from' => "$join_cust $join_pkg",
266 'select' => join(', ', @select ),
267 'extra_sql' => $where,
268 'order_by' => 'ORDER BY _date, billpkgnum',
271 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
272 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
274 my $conf = new FS::Conf;
275 my $money_char = $conf->config('money_char') || '$';