v4 style
[freeside.git] / httemplate / search / cust_tax_exempt_pkg.cgi
1 <& elements/search.html,
2                  'title'       => 'Tax exemptions',
3                  'name'        => 'tax exemptions',
4                  'query'       => $query,
5                  'count_query' => $count_query,
6                  'count_addl'  => [ $money_char. '%.2f total', ],
7                  'header'      => [
8                    '#',
9                    'Month',
10                    'Amount',
11                    'Line item',
12                    'Invoice',
13                    'Date',
14                    FS::UI::Web::cust_header(),
15                  ],
16                  'fields'      => [
17                    'exemptpkgnum',
18                    sub { $_[0]->month. '/'. $_[0]->year; },
19                    sub { $money_char. $_[0]->amount; },
20
21                    sub {
22                      $_[0]->billpkgnum. ': '.
23                      ( $_[0]->pkgnum > 0
24                          ? $_[0]->get('pkg')
25                          : $_[0]->get('itemdesc')
26                      ).
27                      ' ('.
28                      ( $_[0]->setup > 0
29                          ? $money_char. $_[0]->setup. ' setup'
30                          : ''
31                      ).
32                      ( $_[0]->setup > 0 && $_[0]->recur > 0
33                        ? ' / '
34                        : ''
35                      ).
36                      ( $_[0]->recur > 0
37                          ? $money_char. $_[0]->recur. ' recur'
38                          : ''
39                      ).
40                      ')';
41                    },
42
43                    'invnum',
44                    sub { time2str('%b %d %Y', shift->_date ) },
45
46                    \&FS::UI::Web::cust_fields,
47                  ],
48                  'links'       => [
49                    '',
50                    '',
51                    '',
52
53                    '',
54                    $ilink,
55                    $ilink,
56
57                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
58                          FS::UI::Web::cust_header()
59                    ),
60                  ],
61                  'align' => 'rrrlrc'.FS::UI::Web::cust_aligns(), # 'rlrrrc',
62                  'color' => [ 
63                               '',
64                               '',
65                               '',
66                               '',
67                               '',
68                               '',
69                               FS::UI::Web::cust_colors(),
70                             ],
71                  'style' => [ 
72                               '',
73                               '',
74                               '',
75                               '',
76                               '',
77                               '',
78                               FS::UI::Web::cust_styles(),
79                             ],
80 &>
81 <%once>
82
83 my $join_cust = "
84     JOIN cust_bill USING ( invnum )" .
85     FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
86
87 my $join_pkg = "
88     LEFT JOIN cust_pkg USING ( pkgnum )
89     LEFT JOIN part_pkg USING ( pkgpart )
90 ";
91
92 my $join = "
93     JOIN cust_bill_pkg USING ( billpkgnum )
94     $join_pkg
95     $join_cust
96 ";
97
98 </%once>
99 <%init>
100
101 die "access denied"
102   unless $FS::CurrentUser::CurrentUser->access_right('View customer tax exemptions');
103
104 my @where = ( "exempt_monthly = 'Y'" );
105
106 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
107 if ( $beginning || $ending ) {
108   push @where, "_date >= $beginning",
109                "_date <= $ending";
110 }
111
112 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
113   push @where, "cust_main.agentnum = $1";
114 }
115
116 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
117   push @where,  "cust_main.custnum = $1";
118 }
119
120 if ( $cgi->param('country' ) ) {
121
122   my $county  = dbh->quote( $cgi->param('county')  );
123   my $state   = dbh->quote( $cgi->param('state')   );
124   my $country = dbh->quote( $cgi->param('country') );
125   push @where, "( county  = $county OR $county = '' )",
126                "( state   = $state  OR $state = ''  )",
127                "  country = $country";
128   push @where, 'taxclass = '. dbh->quote( $cgi->param('taxclass') )
129     if $cgi->param('taxclass');
130
131 }
132
133 if ( $cgi->param('taxnum') ) {
134
135   my @taxnums = grep /^\d+$/, map { split(',', $_) } $cgi->param('taxnum');
136   if ( $cgi->param('taxnum') =~ /^([\d,]+)$/) {
137     push @where, "cust_tax_exempt_pkg.taxnum IN ($1)";
138   }
139
140 }
141
142 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
143   push @where, "COALESCE(part_pkg.classnum,0) = $1";
144 }
145
146 # no reason ever to show the negative exemptions created by credits.
147 # they'll just confuse people.
148 push @where, "creditbillpkgnum IS NULL";
149
150 my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
151
152 my $count_query = "SELECT COUNT(*), SUM(amount)".
153                   "  FROM cust_tax_exempt_pkg $join $where";
154
155 my $query = {
156   'table'     => 'cust_tax_exempt_pkg',
157   'addl_from' => $join,
158   'hashref'   => {},
159   'select'    => join(', ',
160                    'cust_tax_exempt_pkg.*',
161                    'cust_bill_pkg.*',
162                    'cust_bill.*',
163                    'part_pkg.pkg',
164                    'cust_main.custnum',
165                    FS::UI::Web::cust_sql_fields(),
166                  ),
167   'extra_sql' => $where,
168 };
169
170 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
171 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
172
173 my $conf = new FS::Conf;
174 my $money_char = $conf->config('money_char') || '$';
175
176 </%init>