quiet warnings about CGI::param in list context
[freeside.git] / httemplate / search / cust_pkg-date.html
1 <& elements/search.html,
2   'title'       => $title,
3   'name'        => 'packages',
4   'query'       => $query,
5   'count_query' => $count_query,
6   'header'      => \@header,
7   'fields'      => \@fields,
8   'sort_fields' => [],
9   'align'       => 'rrrl'. FS::UI::Web::cust_aligns(),
10   'color'       => \@color,
11   'style'       => \@style,
12   'links'       => \@links,
13   'cell_style'  => [ $date_color_sub ],
14 &>
15 <%init>
16 my $curuser = $FS::CurrentUser::CurrentUser;
17 die 'access denied' unless $curuser->access_right('List packages');
18
19 my %cols = (
20   'contract_end' => 'Contract end',
21   # We could put any of the date fields in cust_pkg in here, but keep in
22   # mind:
23   # - for start_date, setup, and bill, make sure to include rows where
24   #   the field is null, as that's effectively "right now".
25   # - for cancel and susp, and maybe expire, adjourn, and resume, add a
26   #   column for the cancel or suspend reason.
27   # - for expire, also figure out if there's a future change scheduled.
28   # - for change_date, should probably show what it was changed from.
29 );
30
31 my $col = $cgi->param('date');
32 die "invalid date column" unless $cols{$col};
33
34 my $title = 'Packages by ' . lc($cols{$col}) . ' date';
35 my @header = ( $cols{$col},
36                emt('#'),
37                emt('Quan.'),
38                emt('Package'),
39                # anything else? package status, maybe?
40              );
41 my @fields = ( sub { time2str('%b %d %Y', $_[0]->$col) },
42                'pkgnum',
43                'quantity',
44                'pkg_label',
45              );
46 my @color = ( map '', @fields );
47 my @style = ( map '', @fields );
48
49 my $pkg_link = sub {
50   my $self = shift;
51   my $frag = 'cust_pkg'. $self->pkgnum;
52   [ "${p}view/cust_main.cgi?custnum=".$self->custnum.
53                            ";show=packages;fragment=$frag#cust_pkg",
54     'pkgnum'
55   ];
56 };
57
58 my @links = ( '', ($pkg_link) x 3 );
59
60 push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
61 push @fields, \&FS::UI::Web::cust_fields;
62 push @color,  FS::UI::Web::cust_colors();
63 push @style,  FS::UI::Web::cust_styles();
64 push @links,  FS::UI::Web::cust_links();
65
66 my $agentnums_sql = $curuser->agentnums_sql('table' => 'cust_main');
67 if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
68   $agentnums_sql .= " AND agentnum = $1";
69 }
70
71 my $query = {
72   'select'    => join(',', 'cust_pkg.*', FS::UI::Web::cust_sql_fields() ),
73   'table'     => 'cust_pkg',
74   'addl_from' => FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg'),
75   'hashref'   => {
76     $col => { op => '!=', value => '' },
77     'cancel' => '',
78   },
79   'extra_sql' => ' AND '.$agentnums_sql,
80   'order_by'  => "ORDER BY $col",
81 };
82
83 my $count_query =
84   "SELECT COUNT(*) FROM cust_pkg JOIN cust_main USING (custnum) ".
85   "WHERE $col IS NOT NULL AND cancel IS NULL AND $agentnums_sql";
86
87
88 my $date_color_sub = sub {
89   my $self = shift;
90   my $color;
91   my $interval = ($self->$col - time) / 86400;
92   if ( $interval > 30 ) {
93     $color = 'palegreen';
94   } elsif ( $interval > 0 ) {
95     $color = 'yellow';
96   } else {
97     $color = 'tomato';
98   }
99   "background-color: $color";
100 };
101
102 </%init>
103