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