summaryrefslogtreecommitdiff
path: root/httemplate/search/cust_pkg-date.html
blob: 22a67402b3587fcacb310ca6b89dbbcc2401692f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<& elements/search.html,
  'title'       => $title,
  'name'        => 'packages',
  'query'       => $query,
  'count_query' => $count_query,
  'header'      => \@header,
  'fields'      => \@fields,
  'sort_fields' => [],
  'align'       => 'rrrl'. FS::UI::Web::cust_aligns(),
  'color'       => \@color,
  'style'       => \@style,
  'links'       => \@links,
  'cell_style'  => [ $date_color_sub ],
&>
<%init>
my $curuser = $FS::CurrentUser::CurrentUser;
die 'access denied' unless $curuser->access_right('List packages');

my %cols = (
  'contract_end' => 'Contract end',
  # We could put any of the date fields in cust_pkg in here, but keep in
  # mind:
  # - for start_date, setup, and bill, make sure to include rows where
  #   the field is null, as that's effectively "right now".
  # - for cancel and susp, and maybe expire, adjourn, and resume, add a
  #   column for the cancel or suspend reason.
  # - for expire, also figure out if there's a future change scheduled.
  # - for change_date, should probably show what it was changed from.
);

my $col = $cgi->param('date');
die "invalid date column" unless $cols{$col};

my $title = 'Packages by ' . lc($cols{$col}) . ' date';
my @header = ( $cols{$col},
               emt('#'),
               emt('Quan.'),
               emt('Package'),
               # anything else? package status, maybe?
             );
my @fields = ( sub { time2str('%b %d %Y', $_[0]->$col) },
               'pkgnum',
               'quantity',
               'pkg_label',
             );
my @color = ( map '', @fields );
my @style = ( map '', @fields );

my $pkg_link = sub {
  my $self = shift;
  my $frag = 'cust_pkg'. $self->pkgnum;
  [ "${p}view/cust_main.cgi?custnum=".$self->custnum.
                           ";show=packages;fragment=$frag#cust_pkg",
    'pkgnum'
  ];
};

my @links = ( '', ($pkg_link) x 3 );

push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
push @fields, \&FS::UI::Web::cust_fields;
push @color,  FS::UI::Web::cust_colors();
push @style,  FS::UI::Web::cust_styles();
push @links,  FS::UI::Web::cust_links();

my $agentnums_sql = $curuser->agentnums_sql('table' => 'cust_main');
if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
  $agentnums_sql .= " AND agentnum = $1";
}

my $query = {
  'select'    => join(',', 'cust_pkg.*', FS::UI::Web::cust_sql_fields() ),
  'table'     => 'cust_pkg',
  'addl_from' => FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg'),
  'hashref'   => {
    $col => { op => '!=', value => '' },
    'cancel' => '',
  },
  'extra_sql' => ' AND '.$agentnums_sql,
  'order_by'  => "ORDER BY $col",
};

my $count_query =
  "SELECT COUNT(*) FROM cust_pkg JOIN cust_main USING (custnum) ".
  "WHERE $col IS NOT NULL AND cancel IS NULL AND $agentnums_sql";


my $date_color_sub = sub {
  my $self = shift;
  my $color;
  my $interval = ($self->$col - time) / 86400;
  if ( $interval > 30 ) {
    $color = 'palegreen';
  } elsif ( $interval > 0 ) {
    $color = 'yellow';
  } else {
    $color = 'tomato';
  }
  "background-color: $color";
};

</%init>