This commit was generated by cvs2svn to compensate for changes in r5562,
[freeside.git] / httemplate / search / cust_bill_pkg.cgi
1 <% include( 'elements/search.html',
2                  'title'       => 'Line items',
3                  'name'        => 'line items',
4                  'query'       => $query,
5                  'count_query' => $count_query,
6                  'count_addl'  => [ $money_char. '%.2f total', ],
7                  'header'      => [
8                    '#',
9                    'Description',
10                    'Setup charge',
11                    'Recurring charge',
12                    'Invoice',
13                    'Date',
14                    FS::UI::Web::cust_header(),
15                  ],
16                  'fields'      => [
17                    'billpkgnum',
18                    sub { $_[0]->pkgnum > 0
19                            ? $_[0]->get('pkg')
20                            : $_[0]->get('itemdesc')
21                        },
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 ) },
26                    'invnum',
27                    sub { time2str('%b %d %Y', shift->_date ) },
28                    \&FS::UI::Web::cust_fields,
29                  ],
30                  'links'       => [
31                    '',
32                    '',
33                    '',
34                    '',
35                    $ilink,
36                    $ilink,
37                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
38                          FS::UI::Web::cust_header()
39                    ),
40                  ],
41                  'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
42                  'color' => [ 
43                               '',
44                               '',
45                               '',
46                               '',
47                               '',
48                               '',
49                               FS::UI::Web::cust_colors(),
50                             ],
51                  'style' => [ 
52                               '',
53                               '',
54                               '',
55                               '',
56                               '',
57                               '',
58                               FS::UI::Web::cust_styles(),
59                             ],
60            )
61 %>
62 <%init>
63
64 die "access denied"
65   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
66
67 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
68
69 my $join_cust = "
70     JOIN cust_bill USING ( invnum ) 
71     LEFT JOIN cust_main USING ( custnum )
72 ";
73
74 my $join_pkg = "
75     LEFT JOIN cust_pkg USING ( pkgnum )
76     LEFT JOIN part_pkg USING ( pkgpart )
77 ";
78
79 my $where = " WHERE _date >= $beginning AND _date <= $ending ";
80
81 $where .= " AND payby != 'COMP' "
82   unless $cgi->param('include_comp_cust');
83
84 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
85   $where .= " AND agentnum = $1 ";
86 }
87
88 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
89   if ( $1 == 0 ) {
90     $where .= " AND classnum IS NULL ";
91   } else {
92     $where .= " AND classnum = $1 ";
93   }
94 }
95
96 if ( $cgi->param('out') ) {
97
98   $where .= "
99     AND 0 = (
100       SELECT COUNT(*) FROM cust_main_county
101       WHERE (    cust_main_county.county  = cust_main.county
102               OR ( cust_main_county.county IS NULL AND cust_main.county  =  '' )
103               OR ( cust_main_county.county  =  ''  AND cust_main.county IS NULL)
104               OR ( cust_main_county.county IS NULL AND cust_main.county IS NULL)
105             )
106         AND (    cust_main_county.state   = cust_main.state
107               OR ( cust_main_county.state  IS NULL AND cust_main.state  =  ''  )
108               OR ( cust_main_county.state   =  ''  AND cust_main.state IS NULL )
109               OR ( cust_main_county.state  IS NULL AND cust_main.state IS NULL )
110             )
111         AND cust_main_county.country = cust_main.country
112         AND cust_main_county.tax > 0
113     )
114   ";
115
116 } elsif ( $cgi->param('country' ) ) {
117
118   my $county  = dbh->quote( $cgi->param('county')  );
119   my $state   = dbh->quote( $cgi->param('state')   );
120   my $country = dbh->quote( $cgi->param('country') );
121   $where .= "
122     AND ( county  = $county OR $county = '' )
123     AND ( state   = $state  OR $state = '' )
124     AND   country = $country
125   ";
126   $where .= ' AND taxclass = '. dbh->quote( $cgi->param('taxclass') )
127     if $cgi->param('taxclass');
128
129 }
130
131 $where .= ' AND pkgnum != 0' if $cgi->param('nottax');
132
133 $where .= ' AND pkgnum = 0' if $cgi->param('istax');
134
135 $where .= " AND tax = 'Y'" if $cgi->param('cust_tax');
136
137 my $count_query;
138 if ( $cgi->param('pkg_tax') ) {
139
140   $count_query =
141     "SELECT COUNT(*), SUM(
142                            ( CASE WHEN part_pkg.setuptax = 'Y'
143                                   THEN cust_bill_pkg.setup
144                                   ELSE 0
145                              END
146                            )
147                            +
148                            ( CASE WHEN part_pkg.recurtax = 'Y'
149                                   THEN cust_bill_pkg.recur
150                                   ELSE 0
151                              END
152                            )
153                          )
154     ";
155
156   $where .= " AND (
157                        ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
158                     OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
159                   )
160               AND ( tax != 'Y' OR tax IS NULL )
161             ";
162
163 } else {
164
165   $count_query =
166     "SELECT COUNT(*), SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
167
168 }
169 $count_query .= " FROM cust_bill_pkg $join_cust  $join_pkg $where";
170
171 my $query = {
172   'table'     => 'cust_bill_pkg',
173   'addl_from' => "$join_cust $join_pkg",
174   'hashref'   => {},
175   'select'    => join(', ',
176                    'cust_bill_pkg.*',
177                    'cust_bill._date',
178                    'part_pkg.pkg',
179                    'cust_main.custnum',
180                    FS::UI::Web::cust_sql_fields(),
181                  ),
182   'extra_sql' => $where,
183 };
184
185 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
186 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
187
188 my $conf = new FS::Conf;
189 my $money_char = $conf->config('money_char') || '$';
190
191 </%init>