summaryrefslogtreecommitdiff
path: root/httemplate/view/cust_main/change_history.html
blob: f28196a5dba91827c760ae24deb5e30ce3532d7e (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
% if ( int( time - (keys %years)[0] * 31556736 ) > $start ) {
    Show:
%   my $chy = $cgi->param('change_history-years');
%   foreach my $y (keys %years) {
%     if ( $y == $years ) {
        <FONT SIZE="+1"><% $years{$y} %></FONT>
%     } else {
%       $cgi->param('change_history-years', $y);
        <A HREF="<% $cgi->self_url %>"><% $years{$y} %></A>
%     }
%     last if int( time - $y * 31556736 ) < $start;
%   }
%   $cgi->param('change_history-years', $chy);
% }

<% include("/elements/change_history_common.html",
        'history'   => \@history,
        'tables'    => \%tables,
        'single_cust'   => 1,
    ) %>

<%init>

tie my %years, 'Tie::IxHash',
    .5 => '6 months',
   1  => '1 year',
   2  => '2 years',
   5  => '5 years',
  39  => 'all history',
;

tie my %tables, 'Tie::IxHash',
  'cust_main'         => 'Customer',
  'cust_main_invoice' => 'Invoice destination',
  'cust_main_note'    => 'Note',
  'cust_pkg'          => 'Package',
  #? or just svc_* ? 'cust_svc' => 
  'svc_acct'          => 'Account',
  'radius_usergroup'  => 'RADIUS group',
  'svc_domain'        => 'Domain',
  'svc_www'           => 'Hosting',
  'svc_forward'       => 'Mail forward',
  'svc_broadband'     => 'Broadband',
  'svc_external'      => 'External service',
  'svc_phone'         => 'Phone',
  'svc_cable'         => 'Cable',
  'phone_device'      => 'Phone device',
  'cust_pkg_discount' => 'Discount',
  #? it gets provisioned anyway 'phone_avail'         => 'Phone',
  'cust_tag'          => 'Tag',
;

my $pkg_join = "JOIN cust_pkg USING ( pkgnum )";
my $svc_join = "JOIN cust_svc USING ( svcnum ) $pkg_join";

my @svc_tables = qw(
  svc_acct
  svc_domain
  svc_www
  svc_forward
  svc_broadband
  svc_external
  svc_phone
  svc_cable
);

my %table_join = (
  'radius_usergroup' => $svc_join,
  'phone_device'     => $svc_join,
  'cust_pkg_discount'=> $pkg_join,
);

%table_join = (%table_join, map { $_ => $svc_join } @svc_tables);


# cust_main
# cust_main_invoice

# cust_pkg
# cust_pkg_option?
# cust_pkg_detail
# cust_pkg_reason?  no

#cust_svc
#cust_svc_option?
#svc_*
# svc_acct
#  radius_usergroup
#  acct_snarf?  is this even used? it is now, for communigate RPOP
# svc_domain
#  domain_record
#  registrar
# svc_forward
# svc_www
# svc_broadband
#  (virtual fields?  eh... maybe when they're real)
# svc_external
# svc_phone
#  phone_device
#  phone_avail

# future:

# inventory_item (from services)
# pkg_referral? (changed?)

#random others:

# cust_location?
# cust_main-exemption?? (295.ca named tax exemptions)


my( $cust_main ) = @_;

my $conf = new FS::Conf;

my $curuser = $FS::CurrentUser::CurrentUser;

die "access denied"
  unless $curuser->access_right('View customer history');

# find out the beginning of this customer history, if possible
my $h_insert = qsearchs({
  'table'     => 'h_cust_main',
  'hashref'   => { 'custnum'        => $cust_main->custnum,
                   'history_action' => 'insert',
                 },
  'extra_sql' => 'ORDER BY historynum LIMIT 1',
});
my $start = $h_insert ? $h_insert->history_date : 0;

# retreive the history

my @history = ();

my $years = $conf->config('change_history-years') || .5;
if ( $cgi->param('change_history-years') =~ /^([\d\.]+)$/ ) {
  $years = $1;
}
my $newer_than = int( time - $years * 31556736 ); #60*60*24*365.24

local($FS::Record::nowarn_classload) = 1;

my %foundsvcs;
foreach my $table ( keys %tables ) {
  my @items = qsearch({
    'table'     => "h_$table",
    'addl_from' => $table_join{$table},
    'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than }, },
    'extra_sql' => ' AND custnum = '. $cust_main->custnum,
  });
  %foundsvcs = (%foundsvcs, map { $_->svcnum => 1 } @items)
    if $table =~ /^svc/;
  push @history, @items;
}

### Load svcs that are no longer linked (cust_svc has been deleted)

# get list of all svcs for this customer from h_cust_svc
# could also load svcdb here by joining to part_svc on svcpart,
#   but it would spoil database optimizations on this lookup
my @svcnumobj = qsearch({
  'select' => 'DISTINCT svcnum',
  'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than } },
  'table'  => 'h_cust_svc',
  'addl_from' => 'JOIN cust_pkg USING (pkgnum)',
  'extra_sql' => ' AND custnum = '. $cust_main->custnum,
});

# now grab those svcs explicitly 
foreach my $svcnum ( map { $_->get('svcnum') } @svcnumobj ) {
  # can skip the services we already found
  next if $foundsvcs{$svcnum};
  # grab any given h_cust_svc record for this svcnum to get svcdb
  my $svcdbobj = qsearchs({
    'select' => 'svcdb',
    'hashref' => { svcnum => $svcnum },
    'table'  => 'h_cust_svc',
    'addl_from' => 'JOIN part_svc USING (svcpart)',
    'extra_sql' => 'LIMIT 1',
  });
  unless ($svcdbobj && $svcdbobj->get('svcdb')) {
    # don't think this ever happens, but just in case, don't break history page over it
    warn "Could not load svcdb for svcnum $svcnum";
    next;
  }
  my @items = qsearch({
    'table'     => "h_".$svcdbobj->get('svcdb'),
    'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than },
                     'svcnum' => $svcnum },
  });
  push @history, @items;
}

## legacy history

my @legacy_items = qsearch({
  'table'     => 'legacy_cust_history',
  'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than }, },
  'extra_sql' => ' AND custnum = '. $cust_main->custnum,
});
push @history, @legacy_items;

</%init>