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 startdate - CDR startdate, if any
62 =item detail - detail description
72 Creates a new line item detail. To add the line item detail to the database,
75 Note that this stores the hash reference, not a distinct copy of the hash it
76 points to. You can ask the object for a copy with the I<hash> method.
80 # the new method can be inherited from FS::Record, if a table method is defined
82 sub table { 'cust_bill_pkg_detail'; }
86 Adds this record to the database. If there is an error, returns the error,
87 otherwise returns false.
91 # the insert method can be inherited from FS::Record
95 Delete this record from the database.
99 # the delete method can be inherited from FS::Record
101 =item replace OLD_RECORD
103 Replaces the OLD_RECORD with this one in the database. If there is an error,
104 returns the error, otherwise returns false.
108 # the replace method can be inherited from FS::Record
112 Checks all fields to make sure this is a valid line item detail. If there is
113 an error, returns the error, otherwise returns false. Called by the insert
118 # the check method should currently be supplied - FS::Record contains some
119 # data checking routines
124 my $conf = new FS::Conf;
126 my $phonenum = $self->phonenum;
127 my $phonenum_check_method;
128 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
129 $phonenum =~ s/\W//g;
130 $phonenum_check_method = 'ut_alphan';
132 $phonenum =~ s/\D//g;
133 $phonenum_check_method = 'ut_numbern';
135 $self->phonenum($phonenum);
137 $self->ut_numbern('detailnum')
138 || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
139 #|| $self->ut_moneyn('amount')
140 || $self->ut_floatn('amount')
141 || $self->ut_enum('format', [ '', 'C' ] )
142 || $self->ut_numbern('duration')
143 || $self->ut_textn('regionname')
144 || $self->ut_textn('accountcode')
145 || $self->ut_text('detail')
146 || $self->ut_foreign_keyn('classnum', 'usage_class', 'classnum')
147 || $self->$phonenum_check_method('phonenum')
148 || $self->SUPER::check
153 =item formatted [ OPTION => VALUE ... ]
155 Returns detail information for the invoice line item detail formatted for
158 Currently available options are: I<format> I<escape_function>
160 If I<format> is set to html or latex then the format is improved
161 for tabular appearance in those environments if possible.
163 If I<escape_function> is set then the format is processed by this
164 function before being returned.
166 If I<format_function> is set then the detail is handed to this callback
172 my ( $self, %opt ) = @_;
173 my $format = $opt{format} || '';
174 return () unless defined dbdef->table('cust_bill_pkg_detail');
176 eval "use Text::CSV_XS;";
178 my $csv = new Text::CSV_XS;
180 my $escape_function = sub { shift };
182 $escape_function = \&encode_entities
183 if $format eq 'html';
188 $value =~ s/([#\$%&~_\^{}])( )?/"\\$1". ( ( defined($2) && length($2) ) ? "\\$2" : '' )/ge;
189 $value =~ s/([<>])/\$$1\$/g;
192 if $format eq 'latex';
194 $escape_function = $opt{escape_function} if $opt{escape_function};
196 my $format_sub = sub { my $detail = shift;
197 $csv->parse($detail) or return "can't parse $detail";
198 join(' - ', map { &$escape_function($_) }
203 $format_sub = sub { my $detail = shift;
204 $csv->parse($detail) or return "can't parse $detail";
205 join('</TD><TD>', map { &$escape_function($_) }
209 if $format eq 'html';
211 $format_sub = sub { my $detail = shift;
212 $csv->parse($detail) or return "can't parse $detail";
213 #join(' & ', map { '\small{'. &$escape_function($_). '}' } # $csv->fields );
216 foreach ($csv->fields) {
217 $result .= ' & ' if $column > 1;
218 if ($column > 6) { # KLUDGE ALERT!
219 $result .= '\multicolumn{1}{l}{\scriptsize{'.
220 &$escape_function($_). '}}';
222 $result .= '\scriptsize{'. &$escape_function($_). '}';
228 if $format eq 'latex';
230 $format_sub = $opt{format_function} if $opt{format_function};
233 ? &{$format_sub}( $self->detail, $self )
234 : &{$escape_function}( $self->detail )
239 # Used by FS::Upgrade to migrate to a new database schema
240 sub _upgrade_schema { # class method
242 my ($class, %opts) = @_;
244 warn "$me upgrading $class\n" if $DEBUG;
246 my $classnum = dbdef->table($class->table)->column('classnum')
249 my $type = $classnum->type;
250 unless ( $type =~ /^int/i || $type =~ /int$/i ) {
253 if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
255 eval "use DBI::Const::GetInfoType;";
258 my $major_version = 0;
259 $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ) =~ /^(\d{2})/
260 && ( $major_version = sprintf("%d", $1) );
262 if ( $major_version > 7 ) {
264 # ideally this would be supported in DBIx-DBSchema and friends
266 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
268 warn "updating $table column classnum to integer\n" if $DEBUG;
269 my $sql = "ALTER TABLE $table ALTER classnum TYPE int USING ".
271 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
272 $sth->execute or die $sth->errstr;
276 } elsif ( $dbh->{pg_server_version} =~ /^704/ ) { # earlier?
278 # ideally this would be supported in DBIx-DBSchema and friends
280 # XXX_FIXME better locking
282 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
284 warn "updating $table column classnum to integer\n" if $DEBUG;
286 my $sql = "ALTER TABLE $table RENAME classnum TO old_classnum";
287 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
288 $sth->execute or die $sth->errstr;
290 my $def = dbdef->table($table)->column('classnum');
291 $def->type('integer');
293 $sql = "ALTER TABLE $table ADD COLUMN ". $def->line($dbh);
294 $sth = $dbh->prepare($sql) or die $dbh->errstr;
295 $sth->execute or die $sth->errstr;
297 $sql = "UPDATE $table SET classnum = int4( text( old_classnum ) )";
298 $sth = $dbh->prepare($sql) or die $dbh->errstr;
299 $sth->execute or die $sth->errstr;
301 $sql = "ALTER TABLE $table DROP old_classnum";
302 $sth = $dbh->prepare($sql) or die $dbh->errstr;
303 $sth->execute or die $sth->errstr;
309 die "cust_bill_pkg_detail classnum upgrade unsupported for this Pg version\n";
315 die "cust_bill_pkg_detail classnum upgrade only supported for Pg 8+\n";
323 # Used by FS::Upgrade to migrate to a new database
324 sub _upgrade_data { # class method
326 my ($class, %opts) = @_;
328 warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG;
330 my @cbpd = qsearch({ 'table' => $class->table,
332 'extra_sql' => 'WHERE invnum IS NOT NULL AND '.
333 'pkgnum IS NOT NULL',
337 warn "$me Found unmigrated invoice line item details\n" if $DEBUG;
339 foreach my $cbpd ( @cbpd ) {
340 my $detailnum = $cbpd->detailnum;
341 warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1;
343 qsearchs({ 'table' => 'cust_bill_pkg',
344 'hashref' => { 'invnum' => $cbpd->invnum,
345 'pkgnum' => $cbpd->pkgnum,
347 'order_by' => 'ORDER BY billpkgnum LIMIT 1',
349 if ($cust_bill_pkg) {
350 $cbpd->billpkgnum($cust_bill_pkg->billpkgnum);
353 my $error = $cbpd->replace;
355 warn "*** WARNING: error replacing line item detail ".
356 "(cust_bill_pkg_detail) $detailnum: $error ***\n"
359 warn "Found orphaned line item detail $detailnum during upgrade.\n";
376 L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from the base documentation.