improve CDR usage presentation
[freeside.git] / FS / FS / cust_bill_pkg_detail.pm
1 package FS::cust_bill_pkg_detail;
2
3 use strict;
4 use vars qw( @ISA $me $DEBUG );
5 use FS::Record qw( qsearch qsearchs dbdef );
6 use FS::cust_bill_pkg;
7
8 @ISA = qw(FS::Record);
9 $me = '[ FS::cust_bill_pkg_detail ]';
10 $DEBUG = 0;
11
12 =head1 NAME
13
14 FS::cust_bill_pkg_detail - Object methods for cust_bill_pkg_detail records
15
16 =head1 SYNOPSIS
17
18   use FS::cust_bill_pkg_detail;
19
20   $record = new FS::cust_bill_pkg_detail \%hash;
21   $record = new FS::cust_bill_pkg_detail { 'column' => 'value' };
22
23   $error = $record->insert;
24
25   $error = $new_record->replace($old_record);
26
27   $error = $record->delete;
28
29   $error = $record->check;
30
31 =head1 DESCRIPTION
32
33 An FS::cust_bill_pkg_detail object represents additional detail information for
34 an invoice line item (see L<FS::cust_bill_pkg>).  FS::cust_bill_pkg_detail
35 inherits from FS::Record.  The following fields are currently supported:
36
37 =over 4
38
39 =item detailnum - primary key
40
41 =item billpkgnum - link to cust_bill_pkg
42
43 =item detail - detail description
44
45 =back
46
47 =head1 METHODS
48
49 =over 4
50
51 =item new HASHREF
52
53 Creates a new line item detail.  To add the line item detail to the database,
54 see L<"insert">.
55
56 Note that this stores the hash reference, not a distinct copy of the hash it
57 points to.  You can ask the object for a copy with the I<hash> method.
58
59 =cut
60
61 # the new method can be inherited from FS::Record, if a table method is defined
62
63 sub table { 'cust_bill_pkg_detail'; }
64
65 =item insert
66
67 Adds this record to the database.  If there is an error, returns the error,
68 otherwise returns false.
69
70 =cut
71
72 # the insert method can be inherited from FS::Record
73
74 =item delete
75
76 Delete this record from the database.
77
78 =cut
79
80 # the delete method can be inherited from FS::Record
81
82 =item replace OLD_RECORD
83
84 Replaces the OLD_RECORD with this one in the database.  If there is an error,
85 returns the error, otherwise returns false.
86
87 =cut
88
89 # the replace method can be inherited from FS::Record
90
91 =item check
92
93 Checks all fields to make sure this is a valid line item detail.  If there is
94 an error, returns the error, otherwise returns false.  Called by the insert
95 and replace methods.
96
97 =cut
98
99 # the check method should currently be supplied - FS::Record contains some
100 # data checking routines
101
102 sub check {
103   my $self = shift;
104
105   $self->ut_numbern('detailnum')
106     || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
107     || $self->ut_enum('format', [ '', 'C' ] )
108     || $self->ut_text('detail')
109     || $self->SUPER::check
110     ;
111
112 }
113
114 # _upgrade_data
115 #
116 # Used by FS::Upgrade to migrate to a new database.
117
118 sub _upgrade_data { # class method
119
120   my ($class, %opts) = @_;
121
122   warn "$me upgrading $class\n" if $DEBUG;
123
124   if ( defined( dbdef->table($class->table)->column('billpkgnum') ) &&
125        defined( dbdef->table($class->table)->column('invnum') ) &&
126        defined( dbdef->table($class->table)->column('pkgnum') ) 
127   ) {
128
129     warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG;
130
131     my @cbpd = qsearch({ 'table'   => $class->table,
132                          'hashref' => {},
133                          'extra_sql' => 'WHERE invnum IS NOT NULL AND '.
134                                         'pkgnum IS NOT NULL',
135                       });
136
137     if (scalar(@cbpd)) {
138       warn "$me Found unmigrated invoice line item details\n" if $DEBUG;
139
140       foreach my $cbpd ( @cbpd ) {
141         my $detailnum = $cbpd->detailnum;
142         warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1;
143         my $cust_bill_pkg =
144           qsearchs({ 'table' => 'cust_bill_pkg',
145                      'hashref' => { 'invnum' => $cbpd->invnum,
146                                     'pkgnum' => $cbpd->pkgnum,
147                                   },
148                      'order_by' => 'ORDER BY billpkgnum LIMIT 1',
149                   });
150         if ($cust_bill_pkg) {
151           $cbpd->billpkgnum($cust_bill_pkg->billpkgnum);
152           $cbpd->invnum('');
153           $cbpd->pkgnum('');
154           my $error = $cbpd->replace;
155
156           warn "*** WARNING: error replacing line item detail ".
157                "(cust_bill_pkg_detail) $detailnum: $error ***\n"
158             if $error;
159         } else {
160           warn "Found orphaned line item detail $detailnum during upgrade.\n";
161         }
162
163       } # foreach $cbpd
164
165     } # if @cbpd
166
167   } # if billpkgnum, invnum, and pkgnum columns defined
168
169   '';
170
171 }                         
172
173 =back
174
175 =head1 BUGS
176
177 =head1 SEE ALSO
178
179 L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from the base documentation.
180
181 =cut
182
183 1;
184