whew, FINALLY can fix monthly exemption columns to work correctly. also make them...
[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     LEFT 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('agentnum') =~ /^(\d+)$/ ) {
21   $where .= " AND agentnum = $1 ";
22 }
23
24 if ( $cgi->param('out') ) {
25
26   $where .= "
27     AND 0 = (
28       SELECT COUNT(*) FROM cust_main_county
29       WHERE (    cust_main_county.county  = cust_main.county
30               OR ( cust_main_county.county IS NULL AND cust_main.county  =  '' )
31               OR ( cust_main_county.county  =  ''  AND cust_main.county IS NULL)
32               OR ( cust_main_county.county IS NULL AND cust_main.county IS NULL)
33             )
34         AND (    cust_main_county.state   = cust_main.state
35               OR ( cust_main_county.state  IS NULL AND cust_main.state  =  ''  )
36               OR ( cust_main_county.state   =  ''  AND cust_main.state IS NULL )
37               OR ( cust_main_county.state  IS NULL AND cust_main.state IS NULL )
38             )
39         AND cust_main_county.country = cust_main.country
40         AND cust_main_county.tax > 0
41     )
42   ";
43
44 } elsif ( $cgi->param('country' ) ) {
45
46   my $county  = dbh->quote( $cgi->param('county')  );
47   my $state   = dbh->quote( $cgi->param('state')   );
48   my $country = dbh->quote( $cgi->param('country') );
49   $where .= "
50     AND ( county  = $county OR $county = '' )
51     AND ( state   = $state  OR $state = '' )
52     AND   country = $country
53   ";
54   $where .= ' AND taxclass = '. dbh->quote( $cgi->param('taxclass') )
55     if $cgi->param('taxclass');
56
57 }
58
59 $where .= ' AND pkgnum != 0' if $cgi->param('nottax');
60
61 $where .= ' AND pkgnum = 0' if $cgi->param('istax');
62
63 $where .= " AND tax = 'Y'" if $cgi->param('cust_tax');
64
65 my $count_query;
66 if ( $cgi->param('pkg_tax') ) {
67
68   $count_query =
69     "SELECT COUNT(*), SUM(
70                            ( CASE WHEN part_pkg.setuptax = 'Y'
71                                   THEN cust_bill_pkg.setup
72                                   ELSE 0
73                              END
74                            )
75                            +
76                            ( CASE WHEN part_pkg.recurtax = 'Y'
77                                   THEN cust_bill_pkg.recur
78                                   ELSE 0
79                              END
80                            )
81                          )
82     ";
83
84   $where .= " AND (
85                        ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
86                     OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
87                   )
88               AND ( tax != 'Y' OR tax IS NULL )
89             ";
90
91 } else {
92
93   $count_query =
94     "SELECT COUNT(*), SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
95
96 }
97 $count_query .= " FROM cust_bill_pkg $join_cust  $join_pkg $where";
98
99 my $query = {
100   'table'     => 'cust_bill_pkg',
101   'addl_from' => "$join_cust $join_pkg",
102   'hashref'   => {},
103   'select'    => join(', ',
104                    'cust_bill_pkg.*',
105                    'cust_bill._date',
106                    'part_pkg.pkg',
107                    'cust_main.custnum',
108                    FS::UI::Web::cust_sql_fields(),
109                  ),
110   'extra_sql' => $where,
111 };
112
113 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
114 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
115
116 my $conf = new FS::Conf;
117 my $money_char = $conf->config('money_char') || '$';
118
119 %><%= include( 'elements/search.html',
120                  'title'       => 'Line items',
121                  'name'        => 'line items',
122                  'query'       => $query,
123                  'count_query' => $count_query,
124                  'count_addl'  => [ $money_char. '%.2f total', ],
125                  'header'      => [
126                    '#',
127                    'Description',
128                    'Setup charge',
129                    'Recurring charge',
130                    'Invoice',
131                    'Date',
132                    FS::UI::Web::cust_header(),
133                  ],
134                  'fields'      => [
135                    'billpkgnum',
136                    sub { $_[0]->pkgnum > 0
137                            ? $_[0]->get('pkg')
138                            : $_[0]->get('itemdesc')
139                        },
140                    #strikethrough or "N/A ($amount)" or something these when
141                    # they're not applicable to pkg_tax search
142                    sub { sprintf($money_char.'%.2f', shift->setup ) },
143                    sub { sprintf($money_char.'%.2f', shift->recur ) },
144                    'invnum',
145                    sub { time2str('%b %d %Y', shift->_date ) },
146                    \&FS::UI::Web::cust_fields,
147                  ],
148                  'links'       => [
149                    '',
150                    '',
151                    '',
152                    '',
153                    $ilink,
154                    $ilink,
155                    ( map { $clink } FS::UI::Web::cust_header() ),
156                  ],
157                  'align' => 'rlrrrc',
158            )
159 %>
160