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