simple reporting for new tax system
[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 #here is the agent virtualization
68 my $agentnums_sql =
69   $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
70
71 my @where = ( $agentnums_sql );
72
73 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
74 push @where, "_date >= $beginning",
75              "_date <= $ending";
76
77 push @where , " payby != 'COMP' "
78   unless $cgi->param('include_comp_cust');
79
80 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
81   push @where, "cust_main.agentnum = $1";
82 }
83
84 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
85   if ( $1 == 0 ) {
86     push @where, "classnum IS NULL";
87   } else {
88     push @where, "classnum = $1";
89   }
90 }
91
92 if ( $cgi->param('out') ) {
93
94   push @where, "
95     0 = (
96       SELECT COUNT(*) FROM cust_main_county
97       WHERE (    cust_main_county.county  = cust_main.county
98               OR ( cust_main_county.county IS NULL AND cust_main.county  =  '' )
99               OR ( cust_main_county.county  =  ''  AND cust_main.county IS NULL)
100               OR ( cust_main_county.county IS NULL AND cust_main.county IS NULL)
101             )
102         AND (    cust_main_county.state   = cust_main.state
103               OR ( cust_main_county.state  IS NULL AND cust_main.state  =  ''  )
104               OR ( cust_main_county.state   =  ''  AND cust_main.state IS NULL )
105               OR ( cust_main_county.state  IS NULL AND cust_main.state IS NULL )
106             )
107         AND cust_main_county.country = cust_main.country
108         AND cust_main_county.tax > 0
109     )
110   ";
111
112 } elsif ( $cgi->param('country' ) ) {
113
114   my $county  = dbh->quote( $cgi->param('county')  );
115   my $state   = dbh->quote( $cgi->param('state')   );
116   my $country = dbh->quote( $cgi->param('country') );
117   push @where, 
118     " ( county  = $county OR $county = '' ) ",
119     " ( state   = $state  OR $state  = '' ) ",
120     "   country = $country "
121   ;
122   push @where, ' taxclass = '. dbh->quote( $cgi->param('taxclass') )
123     if $cgi->param('taxclass');
124
125   if ( $cgi->param('taxclassNULL') ) {
126
127     my %hash = ( 'country' => scalar($cgi->param('country')) );
128     foreach (qw( state county )) {
129       $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
130     }
131     my $cust_main_county = qsearchs('cust_main_county', \%hash);
132     die "unknown base region for empty taxclass" unless $cust_main_county;
133
134     my $same_sql = $cust_main_county->sql_taxclass_sameregion;
135     push @where, $same_sql if $same_sql;
136
137   }
138
139 }
140
141 if ($cgi->param('itemdesc')) {
142   if ($cgi->param('itemdesc') eq 'Tax') {
143     push @where, "(itemdesc='Tax' OR itemdesc is null)";
144   }else{
145     push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
146   }
147 }
148 push @where, 'pkgnum != 0' if $cgi->param('nottax');
149 push @where, 'pkgnum  = 0' if $cgi->param('istax');
150
151 push @where, " tax = 'Y' " if $cgi->param('cust_tax');
152
153 my $count_query;
154 if ( $cgi->param('pkg_tax') ) {
155
156   $count_query =
157     "SELECT COUNT(*), SUM(
158                            ( CASE WHEN part_pkg.setuptax = 'Y'
159                                   THEN cust_bill_pkg.setup
160                                   ELSE 0
161                              END
162                            )
163                            +
164                            ( CASE WHEN part_pkg.recurtax = 'Y'
165                                   THEN cust_bill_pkg.recur
166                                   ELSE 0
167                              END
168                            )
169                          )
170     ";
171
172   push @where, "(    ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
173                   OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 ) )",
174                "( tax != 'Y' OR tax IS NULL )";
175
176 } else {
177
178   $count_query =
179     "SELECT COUNT(*), SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
180
181 }
182
183 my $where = ' WHERE '. join(' AND ', @where);
184
185 my $join_cust = "
186     JOIN cust_bill USING ( invnum ) 
187     LEFT JOIN cust_main USING ( custnum )
188 ";
189
190 my $join_pkg = "
191     LEFT JOIN cust_pkg USING ( pkgnum )
192     LEFT JOIN part_pkg USING ( pkgpart )
193 ";
194
195 $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where";
196
197 my $query = {
198   'table'     => 'cust_bill_pkg',
199   'addl_from' => "$join_cust $join_pkg",
200   'hashref'   => {},
201   'select'    => join(', ',
202                    'cust_bill_pkg.*',
203                    'cust_bill._date',
204                    'part_pkg.pkg',
205                    'cust_main.custnum',
206                    FS::UI::Web::cust_sql_fields(),
207                  ),
208   'extra_sql' => $where,
209   'order_by'  => 'ORDER BY _date, billpkgnum',
210 };
211
212 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
213 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
214
215 my $conf = new FS::Conf;
216 my $money_char = $conf->config('money_char') || '$';
217
218 </%init>