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 DEPRECATED? (mostly unused, expensive)
167 If I<format_function> is set then the detail is handed to this callback
172 #totally false laziness w/cust_bill_pkg->detail
174 my ( $self, %opt ) = @_;
175 my $format = $opt{format} || '';
176 return () unless defined dbdef->table('cust_bill_pkg_detail');
178 eval "use Text::CSV_XS;";
180 my $csv = new Text::CSV_XS;
182 my $escape_function = sub { shift };
184 $escape_function = \&encode_entities
185 if $format eq 'html';
190 $value =~ s/([#\$%&~_\^{}])( )?/"\\$1". ( ( defined($2) && length($2) ) ? "\\$2" : '' )/ge;
191 $value =~ s/([<>])/\$$1\$/g;
194 if $format eq 'latex';
196 $escape_function = $opt{escape_function} if $opt{escape_function};
198 my $format_sub = sub { my $detail = shift;
199 $csv->parse($detail) or return "can't parse $detail";
200 join(' - ', map { &$escape_function($_) }
205 $format_sub = sub { my $detail = shift;
206 $csv->parse($detail) or return "can't parse $detail";
207 join('</TD><TD>', map { &$escape_function($_) }
211 if $format eq 'html';
213 $format_sub = sub { my $detail = shift;
214 $csv->parse($detail) or return "can't parse $detail";
215 #join(' & ', map { '\small{'. &$escape_function($_). '}' } # $csv->fields );
218 foreach ($csv->fields) {
219 $result .= ' & ' if $column > 1;
220 if ($column > 6) { # KLUDGE ALERT!
221 $result .= '\multicolumn{1}{l}{\scriptsize{'.
222 &$escape_function($_). '}}';
224 $result .= '\scriptsize{'. &$escape_function($_). '}';
230 if $format eq 'latex';
232 $format_sub = $opt{format_function} if $opt{format_function};
235 ? &{$format_sub}( $self->detail, $self )
236 : &{$escape_function}( $self->detail )
241 # Used by FS::Upgrade to migrate to a new database schema
242 sub _upgrade_schema { # class method
244 my ($class, %opts) = @_;
246 warn "$me upgrading $class\n" if $DEBUG;
248 my $classnum = dbdef->table($class->table)->column('classnum')
251 my $type = $classnum->type;
252 unless ( $type =~ /^int/i || $type =~ /int$/i ) {
255 if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
257 eval "use DBI::Const::GetInfoType;";
260 my $major_version = 0;
261 $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ) =~ /^(\d{2})/
262 && ( $major_version = sprintf("%d", $1) );
264 if ( $major_version > 7 ) {
266 # ideally this would be supported in DBIx-DBSchema and friends
268 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
270 warn "updating $table column classnum to integer\n" if $DEBUG;
271 my $sql = "ALTER TABLE $table ALTER classnum TYPE int USING ".
273 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
274 $sth->execute or die $sth->errstr;
278 } elsif ( $dbh->{pg_server_version} =~ /^704/ ) { # earlier?
280 # ideally this would be supported in DBIx-DBSchema and friends
282 # XXX_FIXME better locking
284 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
286 warn "updating $table column classnum to integer\n" if $DEBUG;
288 my $sql = "ALTER TABLE $table RENAME classnum TO old_classnum";
289 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
290 $sth->execute or die $sth->errstr;
292 my $def = dbdef->table($table)->column('classnum');
293 $def->type('integer');
295 $sql = "ALTER TABLE $table ADD COLUMN ". $def->line($dbh);
296 $sth = $dbh->prepare($sql) or die $dbh->errstr;
297 $sth->execute or die $sth->errstr;
299 $sql = "UPDATE $table SET classnum = int4( text( old_classnum ) )";
300 $sth = $dbh->prepare($sql) or die $dbh->errstr;
301 $sth->execute or die $sth->errstr;
303 $sql = "ALTER TABLE $table DROP old_classnum";
304 $sth = $dbh->prepare($sql) or die $dbh->errstr;
305 $sth->execute or die $sth->errstr;
311 die "cust_bill_pkg_detail classnum upgrade unsupported for this Pg version\n";
317 die "cust_bill_pkg_detail classnum upgrade only supported for Pg 8+\n";
325 # Used by FS::Upgrade to migrate to a new database
326 sub _upgrade_data { # class method
328 my ($class, %opts) = @_;
330 warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG;
332 my @cbpd = qsearch({ 'table' => $class->table,
334 'extra_sql' => 'WHERE invnum IS NOT NULL AND '.
335 'pkgnum IS NOT NULL',
339 warn "$me Found unmigrated invoice line item details\n" if $DEBUG;
341 foreach my $cbpd ( @cbpd ) {
342 my $detailnum = $cbpd->detailnum;
343 warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1;
345 qsearchs({ 'table' => 'cust_bill_pkg',
346 'hashref' => { 'invnum' => $cbpd->invnum,
347 'pkgnum' => $cbpd->pkgnum,
349 'order_by' => 'ORDER BY billpkgnum LIMIT 1',
351 if ($cust_bill_pkg) {
352 $cbpd->billpkgnum($cust_bill_pkg->billpkgnum);
355 my $error = $cbpd->replace;
357 warn "*** WARNING: error replacing line item detail ".
358 "(cust_bill_pkg_detail) $detailnum: $error ***\n"
361 warn "Found orphaned line item detail $detailnum during upgrade.\n";
378 L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from the base documentation.