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