bundled package presentation improvements
[freeside.git] / FS / FS / part_pkg / voip_cdr.pm
index f7db685..a7c1903 100644 (file)
@@ -7,6 +7,7 @@ use Tie::IxHash;
 use FS::Conf;
 use FS::Record qw(qsearchs qsearch);
 use FS::part_pkg::flat;
+use FS::cdr;
 #use FS::rate;
 #use FS::rate_prefix;
 
@@ -79,6 +80,15 @@ tie my %rating_method, 'Tie::IxHash',
                            'type' => 'checkbox',
                          },
 
+    'output_format' => { 'name' => 'Simple output format',
+                         'type' => 'select',
+                         'select_options' => { FS::cdr::invoice_formats() },
+                       },
+
+    'separate_usage' => { 'name' => 'Separate usage charges from recurring charges',
+                          'type' => 'checkbox',
+                        },
+
     #XXX also have option for an external db
 #    'cdr_location' => { 'name' => 'CDR database location'
 #                        'type' => 'select',
@@ -109,7 +119,8 @@ tie my %rating_method, 'Tie::IxHash',
                        default_prefix
                        disable_src
                        domestic_prefix international_prefix
-                       use_amaflags use_disposition
+                       use_amaflags use_disposition output_format
+                       separate_usage
                      )
                   ],
   'weight' => 40,
@@ -120,8 +131,16 @@ sub calc_setup {
   $self->option('setup_fee');
 }
 
-#false laziness w/voip_sqlradacct... resolve it if that one ever gets used again
 sub calc_recur {
+  my $self = shift;
+  my $charges = 0;
+  $charges = $self->calc_usage(@_)
+    unless $self->option('separate_usage', 'Hush!');
+  $self->option('recur_fee') + $charges;
+}
+
+#false laziness w/voip_sqlradacct calc_recur resolve it if that one ever gets used again
+sub calc_usage {
   my($self, $cust_pkg, $sdate, $details, $param ) = @_;
 
   my $last_bill = $cust_pkg->last_bill;
@@ -136,12 +155,21 @@ sub calc_recur {
 
   my $downstream_cdr = '';
 
+  my $output_format = $self->option('output_format', 'Hush!')
+                      || 'simple';
+
+  eval "use Text::CSV_XS;";
+  die $@ if $@;
+  my $csv = new Text::CSV_XS;
+
   foreach my $cust_svc (
     grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc
   ) {
 
     foreach my $cdr (
-      $cust_svc->get_cdrs_for_update()  # $last_bill, $$sdate )
+      $cust_svc->get_cdrs_for_update( 'disable_src'    => $self->option('disable_src'),
+                                      'default_prefix' => $self->option('default_prefix'),
+                                    )  # $last_bill, $$sdate )
     ) {
       if ( $DEBUG > 1 ) {
         warn "rating CDR $cdr\n".
@@ -274,7 +302,7 @@ sub calc_recur {
 
           $charge = sprintf('%.2f', $cdr->upstream_price);
           $charges += $charge;
-
           @call_details = (
             #time2str("%Y %b %d - %r", $cdr->calldate_unix ),
             time2str("%c", $cdr->calldate_unix),  #XXX this should probably be a config option dropdown so they can select US vs- rest of world dates or whatnot
@@ -292,15 +320,7 @@ sub calc_recur {
         $charge = sprintf('%.3f', $cdr->upstream_price);
         $charges += $charge;
 
-        @call_details = (
-          #time2str("%Y %b %d - %r", $cdr->calldate_unix ),
-          time2str("%c", $cdr->calldate_unix),  #XXX this should probably be a config option dropdown so they can select US vs- rest of world dates or whatnot
-          sprintf('%.2f', $cdr->billsec / 60 ).'m',
-          '$'.$charge, #XXX $money_char
-          #$pretty_destnum,
-          $cdr->userfield, #$rate_region->regionname,
-          $cdr->dst,
-        );
+        @call_details = ($cdr->downstream_csv( 'format' => $output_format ));
 
       } else {
         die "don't know how to rate CDRs using method: ".
@@ -363,9 +383,16 @@ sub calc_recur {
         }
 
         if ( $charge > 0 ) {
-          my $call_details = join(' - ', @call_details );
-          warn "  adding details on charge to invoice: $call_details"
-            if $DEBUG;
+          my $call_details;
+          if ( $self->option('rating_method') eq 'upstream_simple' ) {
+            $call_details = [ 'C', $call_details[0] ];
+          }else{
+            $csv->combine(@call_details);
+            $call_details = [ 'C', $csv->string ];
+          }
+          warn "  adding details on charge to invoice: [ ".
+              join(', ', @{$call_details} ). " ]"
+            if ( $DEBUG && ref($call_details) );
           push @$details, $call_details; #\@call_details,
         }
 
@@ -382,6 +409,9 @@ sub calc_recur {
 
     } # $cdr
 
+    unshift @$details, [ 'C', FS::cdr::invoice_header( $output_format) ]
+      if (@$details && $self->option('rating_method') eq 'upstream_simple' );
+
   } # $cust_svc
 
   if ( $spool_cdr && length($downstream_cdr) ) {
@@ -411,7 +441,7 @@ sub calc_recur {
 
   } #if ( $spool_cdr && length($downstream_cdr) )
 
-  $self->option('recur_fee') + $charges;
+  $charges;
 
 }
 
@@ -424,5 +454,37 @@ sub base_recur {
   $self->option('recur_fee');
 }
 
+#  This equates svc_phone records; perhaps svc_phone should have a field
+#  to indicate it represents a line
+sub calc_units {    
+  my($self, $cust_pkg ) = @_;
+  scalar(grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc);
+}
+
+sub append_cust_bill_pkgs {
+  my $self = shift;
+  my($cust_pkg, $sdate, $details, $param ) = @_;
+  return []
+    unless $self->option('separate_usage', 'Hush!');
+
+  my @details = ();
+  my $charges = $self->calc_usage($cust_pkg, $sdate, \@details, $param);
+
+  my $cust_bill_pkg = new FS::cust_bill_pkg {
+    'pkgnum'    => $cust_pkg->pkgnum,
+    'setup'     => 0,
+    'unitsetup' => 0,
+    'recur'     => sprintf( "%.2f", $charges),  # hmmm
+    'unitrecur' => 0,
+    'quantity'  => $cust_pkg->quantity,
+    'sdate'     => $$sdate,
+    'edate'     => $cust_pkg->bill,             # already fiddled
+    'itemdesc'  => 'Usage charges',             # configurable?
+    'details'   => \@details,
+  };
+
+  return [ $cust_bill_pkg ];
+}
+
 1;