add link to customer tax exemptions to customer view page
[freeside.git] / httemplate / search / cust_tax_exempt_pkg.cgi
1 <% include( '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 { $clink } FS::UI::Web::cust_header() ),
58                  ],
59                  'align' => 'rrrlrc', # 'rlrrrc',
60            )
61 %>
62 <%once>
63
64 my $join_cust = "
65     JOIN cust_bill USING ( invnum )
66     LEFT JOIN cust_main USING ( custnum )
67 ";
68
69 my $join_pkg = "
70     LEFT JOIN cust_pkg USING ( pkgnum )
71     LEFT JOIN part_pkg USING ( pkgpart )
72 ";
73
74 my $join = "
75     JOIN cust_bill_pkg USING ( billpkgnum )
76     $join_cust
77     $join_pkg
78 ";
79
80 </%once>
81 <%init>
82
83 my @where = ();
84
85 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
86 if ( $beginning || $ending ) {
87   push @where, "_date >= $beginning",
88                "_date <= $ending";
89                #"payby != 'COMP';
90 }
91
92 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
93   push @where, "agentnum = $1";
94 }
95
96 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
97   push @where,  "cust_main.custnum = $1";
98 }
99
100 if ( $cgi->param('out') ) {
101
102   push @where, "
103     0 = (
104       SELECT COUNT(*) FROM cust_main_county AS county_out
105       WHERE (    county_out.county  = cust_main.county
106               OR ( county_out.county IS NULL AND cust_main.county  =  '' )
107               OR ( county_out.county  =  ''  AND cust_main.county IS NULL)
108               OR ( county_out.county IS NULL AND cust_main.county IS NULL)
109             )
110         AND (    county_out.state   = cust_main.state
111               OR ( county_out.state  IS NULL AND cust_main.state  =  ''  )
112               OR ( county_out.state   =  ''  AND cust_main.state IS NULL )
113               OR ( county_out.state  IS NULL AND cust_main.state IS NULL )
114             )
115         AND county_out.country = cust_main.country
116         AND county_out.tax > 0
117     )
118   ";
119
120 } elsif ( $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 my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
134
135 my $count_query = "SELECT COUNT(*), SUM(amount)".
136                   "  FROM cust_tax_exempt_pkg $join $where";
137
138 my $query = {
139   'table'     => 'cust_tax_exempt_pkg',
140   'addl_from' => $join,
141   'hashref'   => {},
142   'select'    => join(', ',
143                    'cust_tax_exempt_pkg.*',
144                    'cust_bill_pkg.*',
145                    'cust_bill.*',
146                    'part_pkg.pkg',
147                    'cust_main.custnum',
148                    FS::UI::Web::cust_sql_fields(),
149                  ),
150   'extra_sql' => $where,
151 };
152
153 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
154 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
155
156 my $conf = new FS::Conf;
157 my $money_char = $conf->config('money_char') || '$';
158
159 </%init>