correct bad schema bug in cust_bill_pkg_detail
[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 %GetInfoType );
5 use FS::Record qw( qsearch qsearchs dbdef dbh );
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   my $columndef = dbdef->table($class->table)->column('classnum');
125   unless ($columndef->type eq 'int4') {
126
127     my $dbh = dbh;
128     if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
129
130       eval "use DBI::Const::GetInfoType;";
131       die $@ if $@;
132
133       my $major_version = 0;
134       $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ) =~ /^(\d{2})/
135         && ( $major_version = sprintf("%d", $1) );
136
137       if ( $major_version > 7 ) {
138
139         # ideally this would be supported in DBIx-DBSchema and friends
140
141         foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
142
143           warn "updating $table column classnum to integer\n" if $DEBUG;
144           my $sql = "ALTER TABLE $table ALTER classnum TYPE int USING ".
145             "int4(classnum)";
146           my $sth = $dbh->prepare($sql) or die $dbh->errstr;
147           $sth->execute or die $sth->errstr;
148
149         }
150
151       } else {
152
153         die "cust_bill_pkg_detail classnum upgrade unsupported for this Pg version\n";
154
155       }
156
157     } else {
158
159       die "cust_bill_pkg_detail classnum upgrade only supported for Pg 8+\n";
160
161     }
162
163   }
164
165
166   if ( defined( dbdef->table($class->table)->column('billpkgnum') ) &&
167        defined( dbdef->table($class->table)->column('invnum') ) &&
168        defined( dbdef->table($class->table)->column('pkgnum') ) 
169   ) {
170
171     warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG;
172
173     my @cbpd = qsearch({ 'table'   => $class->table,
174                          'hashref' => {},
175                          'extra_sql' => 'WHERE invnum IS NOT NULL AND '.
176                                         'pkgnum IS NOT NULL',
177                       });
178
179     if (scalar(@cbpd)) {
180       warn "$me Found unmigrated invoice line item details\n" if $DEBUG;
181
182       foreach my $cbpd ( @cbpd ) {
183         my $detailnum = $cbpd->detailnum;
184         warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1;
185         my $cust_bill_pkg =
186           qsearchs({ 'table' => 'cust_bill_pkg',
187                      'hashref' => { 'invnum' => $cbpd->invnum,
188                                     'pkgnum' => $cbpd->pkgnum,
189                                   },
190                      'order_by' => 'ORDER BY billpkgnum LIMIT 1',
191                   });
192         if ($cust_bill_pkg) {
193           $cbpd->billpkgnum($cust_bill_pkg->billpkgnum);
194           $cbpd->invnum('');
195           $cbpd->pkgnum('');
196           my $error = $cbpd->replace;
197
198           warn "*** WARNING: error replacing line item detail ".
199                "(cust_bill_pkg_detail) $detailnum: $error ***\n"
200             if $error;
201         } else {
202           warn "Found orphaned line item detail $detailnum during upgrade.\n";
203         }
204
205       } # foreach $cbpd
206
207     } # if @cbpd
208
209   } # if billpkgnum, invnum, and pkgnum columns defined
210
211   '';
212
213 }                         
214
215 =back
216
217 =head1 BUGS
218
219 =head1 SEE ALSO
220
221 L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from the base documentation.
222
223 =cut
224
225 1;
226