Merge branch 'github/pr/57'
[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 $query = {
40   'table'     => 'cust_pkg',
41   'addl_from' => FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg'),
42   'hashref'   => {
43     $col => { op => '!=', value => '' },
44     'cancel' => '',
45   },
46   'order_by' => "ORDER BY $col",
47 };
48
49 my $count_query =
50   "SELECT COUNT(*) FROM cust_pkg WHERE $col IS NOT NULL AND cancel IS NULL";
51
52 my $pkg_link = sub {
53   my $self = shift;
54   my $frag = 'cust_pkg'. $self->pkgnum;
55   [ "${p}view/cust_main.cgi?custnum=".$self->custnum.
56                            ";show=packages;fragment=$frag#cust_pkg",
57     'pkgnum'
58   ];
59 };
60
61 my @links = ( '', ($pkg_link) x 3,
62   FS::UI::Web::cust_links() );
63
64 my $date_color_sub = sub {
65   my $self = shift;
66   my $color;
67   my $interval = ($self->$col - time) / 86400;
68   if ( $interval > 30 ) {
69     $color = 'palegreen';
70   } elsif ( $interval > 0 ) {
71     $color = 'yellow';
72   } else {
73     $color = 'tomato';
74   }
75   "background-color: $color";
76 };
77
78 </%init>
79 <& elements/search.html,
80   'title'       => $title,
81   'name'        => 'packages',
82   'query'       => $query,
83   'count_query' => $count_query,
84   'header'      => \@header,
85   'fields'      => \@fields,
86   'align'       => 'rrrl'. FS::UI::Web::cust_aligns(),
87   'links'       => \@links,
88   'cell_style'  => [ $date_color_sub ],
89 &>
90