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