8cee68a14024c31c2a20522d8eb0c57b66e9b9e6
[freeside.git] / FS / FS / h_cust_svc.pm
1 package FS::h_cust_svc;
2
3 use strict;
4 use vars qw( @ISA $DEBUG );
5 use Carp;
6 use FS::Record qw(qsearchs);
7 use FS::h_Common;
8 use FS::cust_svc;
9
10 @ISA = qw( FS::h_Common FS::cust_svc );
11
12 $DEBUG = 0;
13
14 sub table { 'h_cust_svc'; }
15
16 =head1 NAME
17
18 FS::h_cust_svc - Object method for h_cust_svc objects
19
20 =head1 SYNOPSIS
21
22 =head1 DESCRIPTION
23
24 An FS::h_cust_svc object  represents a historical service.  FS::h_cust_svc
25 inherits from FS::h_Common and FS::cust_svc.
26
27 =head1 METHODS
28
29 =over 4
30
31 =item date_deleted
32
33 Returns the date this service was deleted, if any.
34
35 =cut
36
37 sub date_deleted {
38   my $self = shift;
39   $self->h_date('delete');
40 }
41
42 =item label END_TIMESTAMP [ START_TIMESTAMP ] 
43
44 Returns a list consisting of:
45 - The name of this historical service (from part_svc)
46 - A meaningful identifier (username, domain, or mail alias)
47 - The table name (i.e. svc_domain) for this historical service
48
49 =cut
50
51 sub label {
52   my $self = shift;
53   carp "FS::h_cust_svc::label called on $self" if $DEBUG;
54   my $svc_x = $self->h_svc_x(@_);
55   my $part_svc = $self->part_svc;
56
57   unless ($svc_x) {
58     carp "can't find h_". $self->part_svc->svcdb. '.svcnum '. $self->svcnum if $DEBUG;
59     return $part_svc->svc, 'n/a', $part_svc->svcdb;
60   }
61
62   my @label;
63   eval { @label = $self->_svc_label($svc_x, @_); };
64
65   if ($@) {
66     carp 'while resolving history record for svcdb/svcnum ' . 
67          $part_svc->svcdb . '/' . $self->svcnum . ': ' . $@ if $DEBUG;
68     return $part_svc->svc, 'n/a', $part_svc->svcdb;
69   } else {
70     return @label;
71   }
72
73 }
74
75 =item h_svc_x END_TIMESTAMP [ START_TIMESTAMP ] 
76
77 Returns the FS::h_svc_XXX object for this service as of END_TIMESTAMP (i.e. an
78 FS::h_svc_acct object or FS::h_svc_domain object, etc.) and (optionally) not
79 cancelled before START_TIMESTAMP.
80
81 =cut
82
83 #false laziness w/cust_pkg::h_cust_svc
84 sub h_svc_x {
85   my $self = shift;
86   my $svcdb = $self->part_svc->svcdb;
87
88   warn "requiring FS/h_$svcdb.pm" if $DEBUG;
89   require "FS/h_$svcdb.pm";
90   my $svc_x = qsearchs(
91     "h_$svcdb",
92     { 'svcnum' => $self->svcnum, },
93     "FS::h_$svcdb"->sql_h_searchs(@_),
94   ) || $self->SUPER::svc_x;
95
96   if ($svc_x) {
97     carp "Using $svcdb in place of missing h_${svcdb} record."
98       if ($svc_x->isa('FS::' . $svcdb) and $DEBUG);
99     return $svc_x;
100   } else {
101     return '';
102   }
103
104 }
105
106 =back
107
108 =head1 BUGS
109
110 =head1 SEE ALSO
111
112 L<FS::h_Common>, L<FS::cust_svc>, L<FS::Record>, schema.html from the base
113 documentation.
114
115 =cut
116
117 1;
118