credit for unused portion at cancellation, patch from pbowen
[freeside.git] / FS / FS / part_pkg / voip_sqlradacct.pm
index 7364fad..a6a3a38 100644 (file)
@@ -7,31 +7,35 @@ use FS::part_pkg;
 #use FS::rate;
 use FS::rate_prefix;
 
-@ISA = qw(FS::part_pkg);
+@ISA = qw(FS::part_pkg::flat);
 
-$DEBUG = 0;
+$DEBUG = 1;
 
 %info = (
-    'name' => 'VoIP rating by plan of CDR records in an SQL RADIUS radacct table',
-    'fields' => {
-      'setup_fee' => { 'name' => 'Setup fee for this package',
-                       'default' => 0,
-                     },
-      'recur_flat' => { 'name' => 'Base monthly charge for this package',
-                        'default' => 0,
-                      },
-      'ratenum'   => { 'name' => 'Rate plan',
-                       'type' => 'select',
-                       'select_table' => 'rate',
-                       'select_key'   => 'ratenum',
-                       'select_label' => 'ratename',
-                     },
-      'ignore_unrateable' => { 'name' => 'Ignore calls for which not rate prefix can be found (otherwise they are fatal)',
-                               'type' => 'checkbox',
-                             },
-    },
-    'fieldorder' => [qw( setup_fee recur_flat ratenum ignore_unrateable )],
-    'weight' => 40,
+  'name' => 'VoIP rating by plan of CDR records in an SQL RADIUS radacct table',
+  'fields' => {
+    'setup_fee'     => { 'name' => 'Setup fee for this package',
+                         'default' => 0,
+                       },
+    'recur_flat'     => { 'name' => 'Base monthly charge for this package',
+                          'default' => 0,
+                        },
+    'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
+                                   ' of service at cancellation',
+                         'type' => 'checkbox',
+                       },
+    'ratenum'   => { 'name' => 'Rate plan',
+                     'type' => 'select',
+                     'select_table' => 'rate',
+                     'select_key'   => 'ratenum',
+                     'select_label' => 'ratename',
+                   },
+    'ignore_unrateable' => { 'name' => 'Ignore calls for which not rate prefix can be found (otherwise they are fatal)',
+                             'type' => 'checkbox',
+                           },
+  },
+  'fieldorder' => [qw( setup_fee recur_flat unused_credit ratenum ignore_unrateable )],
+  'weight' => 40,
 );
 
 sub calc_setup {
@@ -90,6 +94,8 @@ sub calc_recur {
         $countrycode = '1';
       }
 
+      warn "rating call to +$countrycode $dest" if $DEBUG;
+
       #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
       # finally trying the country code only
       my $rate_prefix = '';
@@ -106,6 +112,7 @@ sub calc_recur {
 
       unless ( $rate_prefix ) {
         if ( $self->option('ignore_unrateable') ) {
+          warn "  skipping unrateable call to +$countrycode $dest";
           next;
         } else {
           die "Can't find rate for call to +$countrycode $dest\n"
@@ -119,6 +126,10 @@ sub calc_recur {
         'dest_regionnum' => $regionnum,
       } );
 
+      warn "  found rate for regionnum $regionnum ".
+           "and rate detail $rate_detail"
+        if $DEBUG;
+
       ###
       # find the price and add detail to the invoice
       ###
@@ -142,6 +153,15 @@ sub calc_recur {
         $charges += $charge;
       }
 
+      warn "  adding details on charge to invoice: ".
+           join(' - ', 
+             "+$countrycode $dest",
+             $rate_prefix->rate_region->regionname,
+             $minutes.'m',
+             '$'.$charge,
+           )
+        if $DEBUG;
+
       push @$details, 
         #[
         join(' - ', 
@@ -165,5 +185,10 @@ sub is_free {
   0;
 }
 
+sub base_recur {
+  my($self, $cust_pkg) = @_;
+  $self->option('recur_flat');
+}
+
 1;