reports with row grouping for payment/refund search, #25944
[freeside.git] / httemplate / search / cust_bill_pay.html
1 <& 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                 'sort_fields' => [
29                   'amount',
30                   'invnum',
31                   'cust_bill_charged',
32                   'cust_bill_date',
33                   '',
34                   'cust_pay_paid',
35                   'cust_pay_date',
36                   '', 
37                 ],
38                 'align' => 'rrrrlrrl'.FS::UI::Web::cust_aligns(),
39                 'links' => [
40                              '',
41                              $cust_bill_link,
42                              $cust_bill_link,
43                              $cust_bill_link,
44                              $cust_pay_link,
45                              $cust_pay_link,
46                              $cust_pay_link,
47                              '',
48                              ( map { $_ ne 'Cust. Status' ? $cust_link : '' }
49                                    FS::UI::Web::cust_header()
50                              ),
51                            ],
52                 'color' => [ 
53                              '',
54                              '',
55                              '',
56                              '',
57                              '',
58                              '',
59                              '',
60                              '',
61                              FS::UI::Web::cust_colors(),
62                            ],
63                 'style' => [ 
64                              '',
65                              '',
66                              '',
67                              '',
68                              '',
69                              '',
70                              '',
71                              '',
72                              FS::UI::Web::cust_styles(),
73                            ],
74           
75 &>
76 <%init>
77
78 die "access denied"
79   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
80
81 my $conf = new FS::Conf;
82 my $money_char = $conf->config('money_char') || '$';
83
84 my $title = 'Net Payment Search Results';
85
86 my @search = ();
87
88 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
89   push @search, "agentnum = $1";
90   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
91   die "unknown agentnum $1" unless $agent;
92   $title = $agent->agent. " $title";
93 }
94
95 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
96   push @search, "refnum = $1";
97   my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
98   die "unknown refnum $1" unless $part_referral;
99   $title = $part_referral->referral. " $title";
100 }
101
102 # cust_classnum (false laziness w/ elements/cust_main_dayranges.html, prepaid_income.html, cust_bill_pkg.html, cust_bill_pkg_referral.html, unearned_detail.html, cust_credit.html, cust_credit_refund.html, cust_main::Search::search_sql)
103 if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
104   my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
105   push @search, 'COALESCE( cust_main.classnum, 0) IN ( '.
106                     join(',', map { $_ || '0' } @classnums ).
107                 ' )'
108     if @classnums;
109 }
110
111
112 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
113 push @search, "cust_bill._date >= $beginning ",
114               "cust_bill._date <= $ending";
115
116 #here is the agent virtualization
117 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
118
119 my $where = 'WHERE '. join(' AND ', @search);
120 #
121 my $count_query = 'SELECT COUNT(*), SUM(amount)
122                    FROM cust_bill_pay
123                      LEFT JOIN cust_bill USING ( invnum  ) '.
124                      FS::UI::Web::join_cust_main('cust_bill') .
125                   $where;
126
127 my $sql_query   = {
128   'table'     => 'cust_bill_pay',
129   'select'    => join(', ',
130                    'cust_bill_pay.*',
131                    'cust_bill._date     AS cust_bill_date',
132                    'cust_bill.charged   AS cust_bill_charged',
133                    'cust_pay.paid       AS cust_pay_paid',
134                    'cust_pay._date      AS cust_pay_date',
135                    'cust_pay.otaker     AS cust_pay_otaker',
136                    'cust_pay.custnum    AS custnum',
137                    'cust_main.custnum   AS cust_main_custnum',
138                    FS::UI::Web::cust_sql_fields(),
139                  ),
140   'hashref'   => {},
141   'extra_sql' => $where,
142   'addl_from' => 'LEFT JOIN cust_bill   USING ( invnum  )
143                   LEFT JOIN cust_pay    USING ( paynum ) '.
144                   FS::UI::Web::join_cust_main('cust_bill')
145 };
146
147 my $cust_bill_link = sub {
148   my $cust_bill_pay = shift;
149   $cust_bill_pay->invnum
150     ? [ "${p}view/cust_bill.cgi?", 'invnum' ]
151     : '';
152 };
153
154 my $cust_pay_link = sub {
155   my $cust_bill_pay = shift;
156   $cust_bill_pay->paynum
157     ? [ "${p}view/cust_pay.html?paynum=", 'paynum' ]
158     : '';
159 };
160
161 my $cust_link = sub {
162   my $cust_credit_bill = shift;
163   $cust_credit_bill->cust_main_custnum
164     ? [ "${p}view/cust_main.cgi?", 'cust_main_custnum' ]
165     : '';
166 };
167
168 </%init>