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