correct "out of taxable region" flag on new line item report - NULLs need to be compa...
[freeside.git] / httemplate / search / cust_bill_pkg.cgi
1 <%
2
3 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
4
5 my $join_cust = "
6     JOIN cust_bill USING ( invnum ) 
7     JOIN cust_main USING ( custnum )
8 ";
9
10 my $join_pkg = "
11     LEFT JOIN cust_pkg USING ( pkgnum )
12     LEFT JOIN part_pkg USING ( pkgpart )
13 ";
14
15 my $where = "
16   WHERE _date >= $beginning AND _date <= $ending
17     AND payby != 'COMP'
18 ";
19
20 if ( $cgi->param('out') ) {
21
22   $where .= "
23     AND 0 = (
24       SELECT COUNT(*) FROM cust_main_county
25       WHERE (    cust_main_county.county  = cust_main.county
26               OR ( cust_main_county.county IS NULL AND cust_main.county  =  '' )
27               OR ( cust_main_county.county  =  ''  AND cust_main.county IS NULL)
28               OR ( cust_main_county.county IS NULL AND cust_main.county IS NULL)
29             )
30         AND (    cust_main_county.state   = cust_main.state
31               OR ( cust_main_county.state  IS NULL AND cust_main.state  =  ''  )
32               OR ( cust_main_county.state   =  ''  AND cust_main.state IS NULL )
33               OR ( cust_main_county.state  IS NULL AND cust_main.state IS NULL )
34             )
35         AND cust_main_county.country = cust_main.country
36         AND cust_main_county.tax > 0
37     )
38   ";
39
40 } elsif ( $cgi->param('country' ) ) {
41
42   my $county  = dbh->quote( $cgi->param('county')  );
43   my $state   = dbh->quote( $cgi->param('state')   );
44   my $country = dbh->quote( $cgi->param('country') );
45   $where .= "
46     AND ( county  = $county OR $county = '' )
47     AND ( state   = $state  OR $state = '' )
48     AND   country = $country
49   ";
50   $where .= ' AND taxclass = '. dbh->quote( $cgi->param('taxclass') )
51     if $cgi->param('taxclass');
52
53 }
54
55 $where .= ' AND pkgnum != 0' if $cgi->param('nottax');
56
57 $where .= ' AND pkgnum = 0' if $cgi->param('istax');
58
59 $where .= " AND tax = 'Y'" if $cgi->param('cust_tax');
60
61 my $count_query;
62 if ( $cgi->param('pkg_tax') ) {
63
64   $count_query =
65     "SELECT COUNT(*), SUM( ( CASE WHEN part_pkg.setuptax = 'Y'
66                                  THEN cust_bill_pkg.setup
67                                  ELSE 0 )
68                           +
69                           ( CASE WHEN part_pkg.recurtax = 'Y'
70                                  THEN cust_bill_pkg.recur
71                                  ELSE 0 )
72                         )";
73
74   $where .= " AND (
75                        ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
76                     OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
77                   )";
78
79 } else {
80
81   $count_query =
82     "SELECT COUNT(*), SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
83
84 }
85 $count_query .= " FROM cust_bill_pkg $join_cust  $join_pkg $where";
86
87 my $query = {
88   'table'     => 'cust_bill_pkg',
89   'addl_from' => "$join_cust $join_pkg",
90   'hashref'   => {},
91   'select'    => join(', ',
92                    'cust_bill_pkg.*',
93                    'cust_bill._date',
94                    'part_pkg.pkg',
95                    'cust_main.custnum',
96                    FS::UI::Web::cust_sql_fields(),
97                  ),
98   'extra_sql' => $where,
99 };
100
101 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
102 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
103
104 my $conf = new FS::Conf;
105 my $money_char = $conf->config('money_char') || '$';
106
107 %><%= include( 'elements/search.html',
108                  'title'       => 'Line items',
109                  'name'        => 'line items',
110                  'query'       => $query,
111                  'count_query' => $count_query,
112                  'count_addl'  => [ $money_char. '%.2f total', ],
113                  'header'      => [
114                    '#',
115                    'Description',
116                    'Setup charge',
117                    'Recurring charge',
118                    'Invoice',
119                    'Date',
120                    FS::UI::Web::cust_header(),
121                  ],
122                  'fields'      => [
123                    'billpkgnum',
124                    sub { $_[0]->pkgnum > 0
125                            ? $_[0]->get('pkg')
126                            : $_[0]->get('itemdesc')
127                        },
128                    #strikethrough or "N/A ($amount)" or something these when
129                    # they're not applicable to pkg_tax search
130                    sub { sprintf($money_char.'%.2f', shift->setup ) },
131                    sub { sprintf($money_char.'%.2f', shift->recur ) },
132                    'invnum',
133                    sub { time2str('%b %d %Y', shift->_date ) },
134                    \&FS::UI::Web::cust_fields,
135                  ],
136                  'links'       => [
137                    '',
138                    '',
139                    '',
140                    '',
141                    $ilink,
142                    $ilink,
143                    ( map { $clink } FS::UI::Web::cust_header() ),
144                  ],
145                  'align' => 'rlrrrc',
146            )
147 %>
148