X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fcust_bill_pkg.pm;h=8d79ed587b6b04f8bb15d781fb89480fc972629b;hp=52b29f2d34d42583617852ab89202a03b7572668;hb=395cc72629d31c8dcd138acf423e66d2d73d89d2;hpb=bda8c33f9b346ba6cd7aa4174ce0d3e37db7bd49 diff --git a/FS/FS/cust_bill_pkg.pm b/FS/FS/cust_bill_pkg.pm index 52b29f2d3..8d79ed587 100644 --- a/FS/FS/cust_bill_pkg.pm +++ b/FS/FS/cust_bill_pkg.pm @@ -3,6 +3,7 @@ package FS::cust_bill_pkg; use strict; use vars qw( @ISA $DEBUG $me ); use Carp; +use Text::CSV_XS; use FS::Record qw( qsearch qsearchs dbdef dbh ); use FS::cust_main_Mixin; use FS::cust_pkg; @@ -146,18 +147,29 @@ sub insert { if ( $self->get('details') ) { foreach my $detail ( @{$self->get('details')} ) { - my $cust_bill_pkg_detail = new FS::cust_bill_pkg_detail { - 'billpkgnum' => $self->billpkgnum, - 'format' => (ref($detail) ? $detail->[0] : '' ), - 'detail' => (ref($detail) ? $detail->[1] : $detail ), - 'amount' => (ref($detail) ? $detail->[2] : '' ), - 'classnum' => (ref($detail) ? $detail->[3] : '' ), - 'phonenum' => (ref($detail) ? $detail->[4] : '' ), - 'accountcode' => (ref($detail) ? $detail->[5] : '' ), - 'startdate' => (ref($detail) ? $detail->[6] : '' ), - 'duration' => (ref($detail) ? $detail->[7] : '' ), - 'regionname' => (ref($detail) ? $detail->[8] : '' ), - }; + my %hash = (); + if ( ref($detail) ) { + if ( ref($detail) eq 'ARRAY' ) { + #carp "this way sucks, use a hash"; #but more useful/friendly + $hash{'format'} = $detail->[0]; + $hash{'detail'} = $detail->[1]; + $hash{'amount'} = $detail->[2]; + $hash{'classnum'} = $detail->[3]; + $hash{'phonenum'} = $detail->[4]; + $hash{'accountcode'} = $detail->[5]; + $hash{'startdate'} = $detail->[6]; + $hash{'duration'} = $detail->[7]; + $hash{'regionname'} = $detail->[8]; + } elsif ( ref($detail) eq 'HASH' ) { + %hash = %$detail; + } else { + die "unknow detail type ". ref($detail); + } + } else { + $hash{'detail'} = $detail; + } + $hash{'billpkgnum'} = $self->billpkgnum; + my $cust_bill_pkg_detail = new FS::cust_bill_pkg_detail \%hash; $error = $cust_bill_pkg_detail->insert; if ( $error ) { $dbh->rollback if $oldAutoCommit; @@ -433,61 +445,100 @@ to skip usage detail: sub details { my ( $self, %opt ) = @_; - my $format = $opt{format} || ''; my $escape_function = $opt{escape_function} || sub { shift }; - return () unless defined dbdef->table('cust_bill_pkg_detail'); - eval "use Text::CSV_XS;"; - die $@ if $@; my $csv = new Text::CSV_XS; - my $format_sub = sub { my $detail = shift; - $csv->parse($detail) or return "can't parse $detail"; - join(' - ', map { &$escape_function($_) } - $csv->fields - ); - }; - - $format_sub = sub { my $detail = shift; - $csv->parse($detail) or return "can't parse $detail"; - join('', map { &$escape_function($_) } - $csv->fields - ); - } - if $format eq 'html'; - - $format_sub = sub { my $detail = shift; - $csv->parse($detail) or return "can't parse $detail"; - #join(' & ', map { '\small{'. &$escape_function($_). '}' } - # $csv->fields ); - my $result = ''; - my $column = 1; - foreach ($csv->fields) { - $result .= ' & ' if $column > 1; - if ($column > 6) { # KLUDGE ALERT! - $result .= '\multicolumn{1}{l}{\scriptsize{'. - &$escape_function($_). '}}'; - }else{ - $result .= '\scriptsize{'. &$escape_function($_). '}'; - } - $column++; - } - $result; - } - if $format eq 'latex'; - - $format_sub = $opt{format_function} if $opt{format_function}; - - map { ( $_->format eq 'C' - ? &{$format_sub}( $_->detail, $_ ) - : &{$escape_function}( $_->detail ) - ) - } - qsearch ({ 'table' => 'cust_bill_pkg_detail', - 'hashref' => { 'billpkgnum' => $self->billpkgnum }, - 'order_by' => 'ORDER BY detailnum', - }); - #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum }); + if ( $opt{format_function} ) { + + #this still expects to be passed a cust_bill_pkg_detail object as the + #second argument, which is expensive + carp "deprecated format_function passed to cust_bill_pkg->details"; + my $format_sub = $opt{format_function} if $opt{format_function}; + + map { ( $_->format eq 'C' + ? &{$format_sub}( $_->detail, $_ ) + : &{$escape_function}( $_->detail ) + ) + } + qsearch ({ 'table' => 'cust_bill_pkg_detail', + 'hashref' => { 'billpkgnum' => $self->billpkgnum }, + 'order_by' => 'ORDER BY detailnum', + }); + + } elsif ( $opt{'no_usage'} ) { + + my $sql = "SELECT detail FROM cust_bill_pkg_detail ". + " WHERE billpkgnum = ". $self->billpkgnum. + " AND ( format IS NULL OR format != 'C' ) ". + " ORDER BY detailnum"; + my $sth = dbh->prepare($sql) or die dbh->errstr; + $sth->execute or die $sth->errstr; + + map &{$escape_function}( $_->[0] ), @{ $sth->fetchall_arrayref }; + + } else { + + my $format_sub; + my $format = $opt{format} || ''; + if ( $format eq 'html' ) { + + $format_sub = sub { my $detail = shift; + $csv->parse($detail) or return "can't parse $detail"; + join('', map { &$escape_function($_) } + $csv->fields + ); + }; + + } elsif ( $format eq 'latex' ) { + + $format_sub = sub { + my $detail = shift; + $csv->parse($detail) or return "can't parse $detail"; + #join(' & ', map { '\small{'. &$escape_function($_). '}' } + # $csv->fields ); + my $result = ''; + my $column = 1; + foreach ($csv->fields) { + $result .= ' & ' if $column > 1; + if ($column > 6) { # KLUDGE ALERT! + $result .= '\multicolumn{1}{l}{\scriptsize{'. + &$escape_function($_). '}}'; + }else{ + $result .= '\scriptsize{'. &$escape_function($_). '}'; + } + $column++; + } + $result; + }; + + } else { + + $format_sub = sub { my $detail = shift; + $csv->parse($detail) or return "can't parse $detail"; + join(' - ', map { &$escape_function($_) } + $csv->fields + ); + }; + + } + + my $sql = "SELECT format, detail FROM cust_bill_pkg_detail ". + " WHERE billpkgnum = ". $self->billpkgnum. + " ORDER BY detailnum"; + my $sth = dbh->prepare($sql) or die dbh->errstr; + $sth->execute or die $sth->errstr; + + #avoid the fetchall_arrayref and loop for less memory usage? + + map { (defined($_->[0]) && $_->[0] eq 'C') + ? &{$format_sub}( $_->[1] ) + : &{$escape_function}( $_->[1] ); + } + @{ $sth->fetchall_arrayref }; + + } + } =item details_header [ OPTION => VALUE ... ] @@ -503,8 +554,6 @@ sub details_header { my $self = shift; return '' unless defined dbdef->table('cust_bill_pkg_detail'); - eval "use Text::CSV_XS;"; - die $@ if $@; my $csv = new Text::CSV_XS; my @detail = @@ -814,12 +863,11 @@ usage. sub usage { my( $self, $classnum ) = @_; - my $sum = 0; - my @values = (); if ( $self->get('details') ) { - @values = + my $sum = 0; + foreach my $value ( map { ref($_) eq 'HASH' ? $_->{'amount'} : $_->[2] @@ -832,20 +880,26 @@ sub usage { : 1 ) } - @{ $self->get('details') }; + @{ $self->get('details') } + ) { + $sum += $value if $value; + } + + return $sum; } else { - my $hashref = { 'billpkgnum' => $self->billpkgnum }; - $hashref->{ 'classnum' } = $classnum if defined($classnum); - @values = map { $_->amount } qsearch('cust_bill_pkg_detail', $hashref); + my $sql = 'SELECT SUM(COALESCE(amount,0)) FROM cust_bill_pkg_detail '. + ' WHERE billpkgnum = '. $self->billpkgnum; + $sql .= " AND classnum = $classnum" if defined($classnum); - } + my $sth = dbh->prepare($sql) or die dbh->errstr; + $sth->execute or die $sth->errstr; + + return $sth->fetchrow_arrayref->[0]; - foreach ( @values ) { - $sum += $_ if $_; } - $sum; + } =item usage_classes @@ -862,9 +916,9 @@ sub usage_classes { my %seen = (); foreach my $detail ( grep { ref($_) } @{$self->get('details')} ) { - $seen{ ref($detail) eq 'HASH' + $seen{ (ref($detail) eq 'HASH' ? $detail->{'classnum'} - : $detail->[3] + : $detail->[3]) || '' } = 1; } keys %seen;