This commit was generated by cvs2svn to compensate for changes in r8690,
[freeside.git] / httemplate / search / cust_bill_pay.html
1 <% include( 'elements/search.html',
2                 'title'       => $title,
3                 'name'        => 'net payments',
4                 'query'       => $sql_query,
5                 'count_query' => $count_query,
6                 'count_addl'  => [ '$%.2f total paid (net)', ],
7                 'header'      => [ 'Net applied',
8                                    'to Invoice',
9                                    'Payment',
10                                    'By',
11                                    FS::UI::Web::cust_header(),
12                                  ],
13                 'fields'      => [
14                    sub { $money_char. sprintf('%.2f', shift->amount ) },
15                    sub { my $cbp = shift;
16                          '#'.$cbp->invnum. ' '.
17                          time2str('%b %d %Y', $cbp->cust_bill_date ).
18                          " ($money_char".
19                            sprintf('%.2f', $cbp->cust_bill_amount).
20                          ")" 
21                        },
22                    sub { my $cbp = shift;
23                          $cbp->cust_pay->payby_payinfo_pretty. ' '.
24                          time2str('%b %d %Y', $cbp->_date ).
25                          " ($money_char".
26                          sprintf('%.2f', $cbp->cust_pay_paid ).
27                          ")"
28                        },
29                    sub { shift->cust_pay->otaker },
30                    \&FS::UI::Web::cust_fields,
31                 ],
32                 'align' => 'rrrl'.FS::UI::Web::cust_aligns(),
33                 'links' => [
34                              '',
35                              $cust_bill_link,
36                              $cust_pay_link,
37                              '',
38                              ( map { $_ ne 'Cust. Status' ? $cust_link : '' }
39                                    FS::UI::Web::cust_header()
40                              ),
41                            ],
42                 'color' => [ 
43                              '',
44                              '',
45                              '',
46                              '',
47                              FS::UI::Web::cust_colors(),
48                            ],
49                 'style' => [ 
50                              '',
51                              '',
52                              '',
53                              '',
54                              FS::UI::Web::cust_styles(),
55                            ],
56           )
57 %>
58 <%init>
59
60 die "access denied"
61   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
62
63 my $conf = new FS::Conf;
64 my $money_char = $conf->config('money_char') || '$';
65
66 my $title = 'Net Payment Search Results';
67
68 my @search = ();
69
70 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
71   push @search, "agentnum = $1";
72   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
73   die "unknown agentnum $1" unless $agent;
74   $title = $agent->agent. " $title";
75 }
76
77 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
78 push @search, "cust_bill._date >= $beginning ",
79               "cust_bill._date <= $ending";
80
81 #here is the agent virtualization
82 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
83
84 my $where = 'WHERE '. join(' AND ', @search);
85 #
86 my $count_query = 'SELECT COUNT(*), SUM(amount)
87                    FROM cust_bill_pay
88                      LEFT JOIN cust_bill USING ( invnum  )
89                      LEFT JOIN cust_main USING ( custnum ) '.
90                   $where;
91
92 my $sql_query   = {
93   'table'     => 'cust_bill_pay',
94   'select'    => join(', ',
95                    'cust_bill_pay.*',
96                    'cust_pay.paid       AS cust_pay_paid',
97                    'cust_bill._date     AS cust_bill_date',
98                    #'cust_bill.charged   AS cust_bill_charged',
99                    'cust_pay.custnum    AS custnum',
100                    'cust_main.custnum   AS cust_main_custnum',
101                    FS::UI::Web::cust_sql_fields(),
102                  ),
103   'hashref'   => {},
104   'extra_sql' => $where,
105   'addl_from' => 'LEFT JOIN cust_bill   USING ( invnum  )
106                   LEFT JOIN cust_pay    USING ( paynum )
107                   LEFT JOIN cust_main ON ( cust_bill.custnum = cust_main.custnum )',
108 };
109
110 my $cust_bill_link = sub {
111   my $cust_bill_pay = shift;
112   $cust_bill_pay->invnum
113     ? [ "${p}view/cust_bill.cgi?", 'invnum' ]
114     : '';
115 };
116
117 my $cust_pay_link = sub {
118   my $cust_bill_pay = shift;
119   $cust_bill_pay->paynum
120     ? [ "${p}view/cust_pay.html?paynum=", 'paynum' ]
121     : '';
122 };
123
124 my $cust_link = sub {
125   my $cust_credit_bill = shift;
126   $cust_credit_bill->cust_main_custnum
127     ? [ "${p}view/cust_main.cgi?", 'cust_main_custnum' ]
128     : '';
129 };
130
131 </%init>