1 package FS::cust_bill_pkg_detail;
4 use vars qw( @ISA $me $DEBUG %GetInfoType );
6 use FS::Record qw( qsearch qsearchs dbdef dbh );
11 @ISA = qw(FS::Record);
12 $me = '[ FS::cust_bill_pkg_detail ]';
17 FS::cust_bill_pkg_detail - Object methods for cust_bill_pkg_detail records
21 use FS::cust_bill_pkg_detail;
23 $record = new FS::cust_bill_pkg_detail \%hash;
24 $record = new FS::cust_bill_pkg_detail { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::cust_bill_pkg_detail object represents additional detail information for
37 an invoice line item (see L<FS::cust_bill_pkg>). FS::cust_bill_pkg_detail
38 inherits from FS::Record. The following fields are currently supported:
42 =item detailnum - primary key
44 =item billpkgnum - link to cust_bill_pkg
46 =item amount - price of this line item detail
48 =item format - '' for straight text and 'C' for CSV in detail
50 =item classnum - link to usage_class
52 =item duration - granularized number of seconds for this call
58 =item accountcode - accountcode
60 =item detail - detail description
70 Creates a new line item detail. To add the line item detail to the database,
73 Note that this stores the hash reference, not a distinct copy of the hash it
74 points to. You can ask the object for a copy with the I<hash> method.
78 # the new method can be inherited from FS::Record, if a table method is defined
80 sub table { 'cust_bill_pkg_detail'; }
84 Adds this record to the database. If there is an error, returns the error,
85 otherwise returns false.
89 # the insert method can be inherited from FS::Record
93 Delete this record from the database.
97 # the delete method can be inherited from FS::Record
99 =item replace OLD_RECORD
101 Replaces the OLD_RECORD with this one in the database. If there is an error,
102 returns the error, otherwise returns false.
106 # the replace method can be inherited from FS::Record
110 Checks all fields to make sure this is a valid line item detail. If there is
111 an error, returns the error, otherwise returns false. Called by the insert
116 # the check method should currently be supplied - FS::Record contains some
117 # data checking routines
122 my $conf = new FS::Conf;
124 my $phonenum = $self->phonenum;
125 my $phonenum_check_method;
126 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
127 $phonenum =~ s/\W//g;
128 $phonenum_check_method = 'ut_alphan';
130 $phonenum =~ s/\D//g;
131 $phonenum_check_method = 'ut_numbern';
133 $self->phonenum($phonenum);
135 $self->ut_numbern('detailnum')
136 || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
137 #|| $self->ut_moneyn('amount')
138 || $self->ut_floatn('amount')
139 || $self->ut_enum('format', [ '', 'C' ] )
140 || $self->ut_numbern('duration')
141 || $self->ut_textn('regionname')
142 || $self->ut_textn('accountcode')
143 || $self->ut_text('detail')
144 || $self->ut_foreign_keyn('classnum', 'usage_class', 'classnum')
145 || $self->$phonenum_check_method('phonenum')
146 || $self->SUPER::check
151 =item formatted [ OPTION => VALUE ... ]
153 Returns detail information for the invoice line item detail formatted for
156 Currently available options are: I<format> I<escape_function>
158 If I<format> is set to html or latex then the format is improved
159 for tabular appearance in those environments if possible.
161 If I<escape_function> is set then the format is processed by this
162 function before being returned.
164 If I<format_function> is set then the detail is handed to this callback
170 my ( $self, %opt ) = @_;
171 my $format = $opt{format} || '';
172 return () unless defined dbdef->table('cust_bill_pkg_detail');
174 eval "use Text::CSV_XS;";
176 my $csv = new Text::CSV_XS;
178 my $escape_function = sub { shift };
180 $escape_function = \&encode_entities
181 if $format eq 'html';
186 $value =~ s/([#\$%&~_\^{}])( )?/"\\$1". ( ( defined($2) && length($2) ) ? "\\$2" : '' )/ge;
187 $value =~ s/([<>])/\$$1\$/g;
190 if $format eq 'latex';
192 $escape_function = $opt{escape_function} if $opt{escape_function};
194 my $format_sub = sub { my $detail = shift;
195 $csv->parse($detail) or return "can't parse $detail";
196 join(' - ', map { &$escape_function($_) }
201 $format_sub = sub { my $detail = shift;
202 $csv->parse($detail) or return "can't parse $detail";
203 join('</TD><TD>', map { &$escape_function($_) }
207 if $format eq 'html';
209 $format_sub = sub { my $detail = shift;
210 $csv->parse($detail) or return "can't parse $detail";
211 #join(' & ', map { '\small{'. &$escape_function($_). '}' } # $csv->fields );
214 foreach ($csv->fields) {
215 $result .= ' & ' if $column > 1;
216 if ($column > 6) { # KLUDGE ALERT!
217 $result .= '\multicolumn{1}{l}{\scriptsize{'.
218 &$escape_function($_). '}}';
220 $result .= '\scriptsize{'. &$escape_function($_). '}';
226 if $format eq 'latex';
228 $format_sub = $opt{format_function} if $opt{format_function};
231 ? &{$format_sub}( $self->detail, $self )
232 : &{$escape_function}( $self->detail )
237 # Used by FS::Upgrade to migrate to a new database schema
238 sub _upgrade_schema { # class method
240 my ($class, %opts) = @_;
242 warn "$me upgrading $class\n" if $DEBUG;
244 my $classnum = dbdef->table($class->table)->column('classnum')
247 my $type = $classnum->type;
248 unless ( $type =~ /^int/i || $type =~ /int$/i ) {
251 if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
253 eval "use DBI::Const::GetInfoType;";
256 my $major_version = 0;
257 $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ) =~ /^(\d{2})/
258 && ( $major_version = sprintf("%d", $1) );
260 if ( $major_version > 7 ) {
262 # ideally this would be supported in DBIx-DBSchema and friends
264 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
266 warn "updating $table column classnum to integer\n" if $DEBUG;
267 my $sql = "ALTER TABLE $table ALTER classnum TYPE int USING ".
269 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
270 $sth->execute or die $sth->errstr;
274 } elsif ( $dbh->{pg_server_version} =~ /^704/ ) { # earlier?
276 # ideally this would be supported in DBIx-DBSchema and friends
278 # XXX_FIXME better locking
280 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
282 warn "updating $table column classnum to integer\n" if $DEBUG;
284 my $sql = "ALTER TABLE $table RENAME classnum TO old_classnum";
285 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
286 $sth->execute or die $sth->errstr;
288 my $def = dbdef->table($table)->column('classnum');
289 $def->type('integer');
291 $sql = "ALTER TABLE $table ADD COLUMN ". $def->line($dbh);
292 $sth = $dbh->prepare($sql) or die $dbh->errstr;
293 $sth->execute or die $sth->errstr;
295 $sql = "UPDATE $table SET classnum = int4( text( old_classnum ) )";
296 $sth = $dbh->prepare($sql) or die $dbh->errstr;
297 $sth->execute or die $sth->errstr;
299 $sql = "ALTER TABLE $table DROP old_classnum";
300 $sth = $dbh->prepare($sql) or die $dbh->errstr;
301 $sth->execute or die $sth->errstr;
307 die "cust_bill_pkg_detail classnum upgrade unsupported for this Pg version\n";
313 die "cust_bill_pkg_detail classnum upgrade only supported for Pg 8+\n";
321 # Used by FS::Upgrade to migrate to a new database
322 sub _upgrade_data { # class method
324 my ($class, %opts) = @_;
326 warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG;
328 my @cbpd = qsearch({ 'table' => $class->table,
330 'extra_sql' => 'WHERE invnum IS NOT NULL AND '.
331 'pkgnum IS NOT NULL',
335 warn "$me Found unmigrated invoice line item details\n" if $DEBUG;
337 foreach my $cbpd ( @cbpd ) {
338 my $detailnum = $cbpd->detailnum;
339 warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1;
341 qsearchs({ 'table' => 'cust_bill_pkg',
342 'hashref' => { 'invnum' => $cbpd->invnum,
343 'pkgnum' => $cbpd->pkgnum,
345 'order_by' => 'ORDER BY billpkgnum LIMIT 1',
347 if ($cust_bill_pkg) {
348 $cbpd->billpkgnum($cust_bill_pkg->billpkgnum);
351 my $error = $cbpd->replace;
353 warn "*** WARNING: error replacing line item detail ".
354 "(cust_bill_pkg_detail) $detailnum: $error ***\n"
357 warn "Found orphaned line item detail $detailnum during upgrade.\n";
374 L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from the base documentation.