This commit was generated by cvs2svn to compensate for changes in r8593,
[freeside.git] / httemplate / search / elements / cust_main_dayranges.html
1 <%doc>
2
3 Example:
4
5   include( 'elements/cust_main_dayranges.html',
6                  'title'       => 'Accounts Receivable Aging Summary',
7                  'range_sub'   => $mysub,
8          )
9
10   my $mysub = sub {
11     my( $start, $end ) = @_;
12
13     "SQL EXPRESSION BASED ON $start AND $end";
14   };
15
16 </%doc>
17 <% include( 'search.html',
18                  'name'        => 'customers',
19                  'query'       => $sql_query,
20                  'count_query' => $count_sql,
21                  'header'      => [
22                                     FS::UI::Web::cust_header(),
23                                     '0-30',
24                                     '30-60',
25                                     '60-90',
26                                     '90+',
27                                     'Total',
28                                   ],
29                  'footer'      => [
30                                     'Total',
31                                     ( map '',
32                                           ( 1 .. 
33                                             scalar(FS::UI::Web::cust_header()-1)
34                                           )
35                                     ),
36                                     sprintf( $money_char.'%.2f',
37                                              $row->{'rangecol_0_30'} ),
38                                     sprintf( $money_char.'%.2f',
39                                              $row->{'rangecol_30_60'} ),
40                                     sprintf( $money_char.'%.2f',
41                                              $row->{'rangecol_60_90'} ),
42                                     sprintf( $money_char.'%.2f',
43                                              $row->{'rangecol_90_0'} ),
44                                     sprintf( '<b>'. $money_char.'%.2f'. '</b>',
45                                              $row->{'rangecol_0_0'} ),
46                                   ],
47                  'fields'      => [
48                                     \&FS::UI::Web::cust_fields,
49                                     format_rangecol('0_30'),
50                                     format_rangecol('30_60'),
51                                     format_rangecol('60_90'),
52                                     format_rangecol('90_0'),
53                                     format_rangecol('0_0'),
54                                   ],
55                  'links'       => [
56                                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
57                                           FS::UI::Web::cust_header()
58                                     ),
59                                     '',
60                                     '',
61                                     '',
62                                     '',
63                                     '',
64                                   ],
65                  #'align'       => 'rlccrrrrr',
66                  'align'       => FS::UI::Web::cust_aligns(). 'rrrrr',
67                  #'size'        => [ '', '', '-1', '-1', '', '', '', '',  '', ],
68                  #'style'       => [ '', '',  'b',  'b', '', '', '', '', 'b', ],
69                  'size'        => [ ( map '', FS::UI::Web::cust_header() ),
70                                     #'-1', '', '', '', '',  '', ],
71                                     '', '', '', '',  '', ],
72                  'style'       => [ FS::UI::Web::cust_styles(),
73                                     #'b', '', '', '', '', 'b', ],
74                                     '', '', '', '', 'b', ],
75                  'color'       => [
76                                     FS::UI::Web::cust_colors(),
77                                     '',
78                                     '',
79                                     '',
80                                     '',
81                                     '',
82                                   ],
83                %opt,
84              )
85 %>
86 <%init>
87
88 my %opt = @_;
89
90 #actually need to auto-generate other things too for a passed-in ranges to work
91 my $ranges = $opt{'ranges'} ? delete($opt{'ranges'}) : [
92   [  0, 30 ],
93   [ 30, 60 ],
94   [ 60, 90 ],
95   [ 90,  0 ],
96   [  0,  0 ],
97 ];
98
99 my $range_sub = delete($opt{'range_sub'}); #or die
100
101 #my $range_cols = join(',', map &{$range_sub}( @$_ ), @ranges );
102 my $range_cols = join(',', map call_range_sub($range_sub, @$_ ), @$ranges );
103
104 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
105
106 my $active_sql    = FS::cust_pkg->active_sql;
107 my $inactive_sql  = FS::cust_pkg->inactive_sql;
108 my $suspended_sql = FS::cust_pkg->suspended_sql;
109 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
110
111 my $packages_cols = <<END;
112      ( $select_count_pkgs                    ) AS num_pkgs_sql,
113      ( $select_count_pkgs AND $active_sql    ) AS active_pkgs,
114      ( $select_count_pkgs AND $inactive_sql  ) AS inactive_pkgs,
115      ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
116      ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
117 END
118
119 my @where = ();
120
121 unless ( $cgi->param('all_customers') ) {
122
123   my $days = 0;
124   if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
125     $days = $1;
126   }
127
128   push @where,
129     call_range_sub($range_sub, $days, 0, 'no_as'=>1). ' > 0'; # != 0';
130 }
131
132 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
133   my $agentnum = $1;
134   push @where, "agentnum = $agentnum";
135 }
136
137 #status (false laziness w/cust_main::search_sql
138
139 #prospect active inactive suspended cancelled
140 if ( grep { $cgi->param('status') eq $_ } FS::cust_main->statuses() ) {
141   my $method = $cgi->param('status'). '_sql';
142   #push @where, $class->$method();
143   push @where, FS::cust_main->$method();
144 }
145
146 #here is the agent virtualization
147 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
148
149 my $where = join(' AND ', @where);
150 $where = "WHERE $where" if $where;
151
152 my $count_sql = "select count(*) from cust_main $where";
153
154 my $sql_query = {
155   'table'     => 'cust_main',
156   'hashref'   => {},
157   'select'    => join(',',
158                    #'cust_main.*',
159                    'custnum',
160                    $range_cols,
161                    $packages_cols,
162                    FS::UI::Web::cust_sql_fields(),
163                  ),
164   'extra_sql' => $where,
165   'order_by'  => "order by coalesce(lower(company), ''), lower(last)",
166 };
167
168 my $total_sql =
169   "SELECT ".
170       join(',', map call_range_sub( $range_sub, @$_, 'sum'=>1 ), @$ranges).
171     " FROM cust_main $where";
172
173 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
174 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
175 my $row = $total_sth->fetchrow_hashref();
176
177 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
178
179 </%init>
180 <%once>
181
182 my $conf = new FS::Conf;
183
184 my $money_char = $conf->config('money_char') || '$';
185
186 #Example:
187 #
188 # my $balance = balance(
189 #   $start, $end, 
190 #   'no_as'  => 1, #set to true when using in a WHERE clause (supress AS clause)
191 #                 #or 0 / omit when using in a SELECT clause as a column
192 #                 #  ("AS balance_$start_$end")
193 #   'sum'    => 1, #set to true to get a SUM() of the values, for totals
194 #
195 #   #obsolete? options for totals (passed to cust_main::balance_date_sql)
196 #   'total'  => 1, #set to true to remove all customer comparison clauses
197 #   'join'   => $join,   #JOIN clause
198 #   'where'  => \@where, #WHERE clause hashref (elements "AND"ed together)
199 # )
200
201 sub call_range_sub {
202   my($range_sub, $start, $end, %opt) = @_;
203
204   my $as = $opt{'no_as'} ? '' : " AS rangecol_${start}_$end";
205
206   my $sql = &{$range_sub}( $start, $end ); #%opt?
207
208   $sql = "SUM($sql)" if $opt{'sum'};
209
210   $sql.$as;
211
212 }
213
214 sub format_rangecol { #closures help alot
215   my $range = shift;
216   sub { sprintf( $money_char.'%.2f', shift->get("rangecol_$range") ) };
217 }
218
219 </%once>