include elements/search.html the right way to avoid problems with XLS download, ...
[freeside.git] / httemplate / search / cust_credit_refund.html
1 <& elements/search.html,
2                 'title'       => $title,
3                 'name'        => 'net refunds',
4                 'query'       => $sql_query,
5                 'count_query' => $count_query,
6                 'count_addl'  => [ '$%.2f total refunded (net)', ],
7                 'header'      => [ 'Net applied',
8                                    'to Credit',
9                                    'Refund',
10                                    'By',
11                                    FS::UI::Web::cust_header(),
12                                  ],
13                'fields'      => [
14                    sub { $money_char. sprintf('%.2f', shift->amount ) },
15                    sub { my $ccr = shift;
16                          '#'.$ccr->crednum. ' '.
17                          time2str('%b %d %Y', $ccr->cust_credit_date ).
18                          " ($money_char".
19                            sprintf('%.2f', $ccr->cust_credit_amount).
20                          ")" 
21                        },
22                    sub { my $ccr = shift;
23                          time2str('%b %d %Y', $ccr->_date ).
24                          " ($money_char".
25                          sprintf('%.2f', $ccr->cust_refund_refund ).
26                          ")"
27                        },
28                    sub { shift->cust_refund->otaker },
29                    \&FS::UI::Web::cust_fields,
30                 ],
31                 'sort_fields' => [
32                   'amount',
33                   '', #cust_credit_amount?  cust_credit_date? prolly split field
34                   '', #_date?  cust_refund_refund?  also split
35                 ],
36                 'align' => 'rrrl'.FS::UI::Web::cust_aligns(),
37                 'links' => [
38                              '',
39                              '',
40                              '',
41                              '',
42                              ( map { $_ ne 'Cust. Status' ? $cust_link : '' }
43                                    FS::UI::Web::cust_header()
44                              ),
45                            ],
46                 'color' => [ 
47                              '',
48                              '',
49                              '',
50                              '',
51                              FS::UI::Web::cust_colors(),
52                            ],
53                 'style' => [ 
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 $conf = new FS::Conf;
68 my $money_char = $conf->config('money_char') || '$';
69
70 my $title = 'Net Refund Search Results';
71
72 my @search = ();
73
74 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
75   push @search, "agentnum = $1";
76   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
77   die "unknown agentnum $1" unless $agent;
78   $title = $agent->agent. " $title";
79 }
80
81 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
82   push @search, "refnum = $1";
83   my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
84   die "unknown refnum $1" unless $part_referral;
85   $title = $part_referral->referral. " $title";
86 }
87
88 if ( $cgi->param('cust_classnum') ) {
89   my @classnums = grep /^\d+$/, $cgi->param('cust_classnum');
90   push @search, 'cust_main.classnum IN('.join(',',@classnums).')'
91     if @classnums;
92 }
93
94
95 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
96 push @search, "cust_credit._date >= $beginning ",
97               "cust_credit._date <= $ending";
98
99 #here is the agent virtualization
100 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
101
102 my $where = 'WHERE '. join(' AND ', @search);
103 #
104 my $count_query = 'SELECT COUNT(*), SUM(cust_credit_refund.amount)
105                    FROM cust_credit_refund
106                      LEFT JOIN cust_credit USING ( crednum ) '.
107                   FS::UI::Web::join_cust_main('cust_credit') .
108                   $where;
109
110 my $sql_query   = {
111   'table'     => 'cust_credit_refund',
112   'select'    => join(', ',
113                    'cust_credit_refund.*',
114                    'cust_refund.refund  AS cust_refund_refund',
115                    'cust_credit._date   AS cust_credit_date',
116                    'cust_credit.amount  AS cust_credit_amnount',
117                    'cust_refund.custnum AS custnum',
118                    'cust_main.custnum   AS cust_main_custnum',
119                    FS::UI::Web::cust_sql_fields(),
120                  ),
121   'hashref'   => {},
122   'extra_sql' => $where,
123   'addl_from' => 'LEFT JOIN cust_credit USING ( crednum   )
124                   LEFT JOIN cust_refund USING ( refundnum )'.
125                   FS::UI::Web::join_cust_main('cust_credit')
126 };
127
128 #my $cust_credit_link = sub {
129 #  my $cust_credit_refund = shift;
130 #  $cust_credit_refund->crednum
131 #    ? [ "${p}view/cust_credit.cgi?", 'credum' ]
132 #    : '';
133 #};
134
135 #my $cust_refund_link = sub {
136 #  my $cust_credit_refund = shift;
137 #  $cust_credit_refund->refundnum
138 #    ? [ "${p}view/cust_refund.cgi?", 'refundnum' ]
139 #    : '';
140 #};
141
142 my $cust_link = sub {
143   my $cust_credit_refund = shift;
144   $cust_credit_refund->cust_main_custnum
145     ? [ "${p}view/cust_main.cgi?", 'cust_main_custnum' ]
146     : '';
147 };
148
149 </%init>