correctly void invoices with fees, #32862
[freeside.git] / httemplate / search / cust_credit_bill.html
1 <& elements/search.html,
2                 'title'       => $title,
3                 'name'        => 'net credits',
4                 'query'       => $sql_query,
5                 'count_query' => $count_query,
6                 'count_addl'  => [ '$%.2f total credited (net)', ],
7                 'header'      => [ 'Net applied',
8                                    'to Invoice',
9                                    'Credit',
10                                    'By',
11                                    'Reason',
12                                    FS::UI::Web::cust_header(),
13                                  ],
14                 'fields'      => [
15                    sub { $money_char. sprintf('%.2f', shift->amount ) },
16                    sub { my $ccb = shift;
17                          '#'.$ccb->invnum. ' '.
18                          time2str('%b %d %Y', $ccb->cust_bill_date ).
19                          " ($money_char".
20                            sprintf('%.2f', $ccb->cust_bill_amount).
21                          ")" 
22                        },
23                    sub { my $ccb = shift;
24                          time2str('%b %d %Y', $ccb->_date ).
25                          " ($money_char".
26                          sprintf('%.2f', $ccb->cust_credit_amount ).
27                          ")"
28                        },
29                    sub { shift->cust_credit->otaker },
30                    sub { shift->cust_credit->reason },
31                    \&FS::UI::Web::cust_fields,
32                 ],
33                 'sort_fields' => [
34                   'amount',
35                   'invnum',
36                   'cust_credit_amount', #?
37                   '', #'otaker' #this is usernum now
38                   '',
39                 ],
40                 'align' => 'rrrll'.FS::UI::Web::cust_aligns(),
41                 'links' => [
42                              '',
43                              $cust_bill_link,
44                              '',
45                              '',
46                              '',
47                              ( map { $_ ne 'Cust. Status' ? $cust_link : '' }
48                                    FS::UI::Web::cust_header()
49                              ),
50                            ],
51                 'color' => [ 
52                              '',
53                              '',
54                              '',
55                              '',
56                              '',
57                              FS::UI::Web::cust_colors(),
58                            ],
59                 'style' => [ 
60                              '',
61                              '',
62                              '',
63                              '',
64                              '',
65                              FS::UI::Web::cust_styles(),
66                            ],
67           
68 &>
69 <%init>
70
71 die "access denied"
72   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
73
74 my $conf = new FS::Conf;
75 my $money_char = $conf->config('money_char') || '$';
76
77 my $title = 'Net Credit Search Results';
78
79 my @search = ();
80
81 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
82   push @search, "agentnum = $1";
83   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
84   die "unknown agentnum $1" unless $agent;
85   $title = $agent->agent. " $title";
86 }
87
88 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
89   push @search, "refnum = $1";
90   my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
91   die "unknown refnum $1" unless $part_referral;
92   $title = $part_referral->referral. " $title";
93 }
94
95 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
96 push @search, "cust_bill._date >= $beginning ",
97               "cust_bill._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(amount)
105                    FROM cust_credit_bill
106                      LEFT JOIN cust_bill USING ( invnum  ) '.
107                   FS::UI::Web::join_cust_main('cust_bill') .
108                   $where;
109
110 my $sql_query   = {
111   'table'     => 'cust_credit_bill',
112   'select'    => join(', ',
113                    'cust_credit_bill.*',
114                    'cust_credit.amount  AS cust_credit_amount',
115                    'cust_bill._date     AS cust_bill_date',
116                    'cust_bill.charged   AS cust_bill_charged',
117                    'cust_credit.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_bill   USING ( invnum  )
124                   LEFT JOIN cust_credit USING ( crednum )'.
125                   FS::UI::Web::join_cust_main('cust_bill')
126 };
127
128 my $cust_bill_link = sub {
129   my $cust_credit_bill = shift;
130   $cust_credit_bill->invnum
131     ? [ "${p}view/cust_bill.cgi?", 'invnum' ]
132     : '';
133 };
134
135 #my $cust_credit_link = sub {
136 #  my $cust_credit_bill = shift;
137 #  $cust_credit_bill->crednum
138 #    ? [ "${p}view/cust_credit.cgi?", 'crednum' ]
139 #    : '';
140 #};
141
142 my $cust_link = sub {
143   my $cust_credit_bill = shift;
144   $cust_credit_bill->cust_main_custnum
145     ? [ "${p}view/cust_main.cgi?", 'cust_main_custnum' ]
146     : '';
147 };
148
149 </%init>