1 package FS::part_pkg::cdr_termination;
4 use base qw( FS::part_pkg::recur_Common );
5 use vars qw( $DEBUG %info );
7 use FS::Record qw( qsearch ); #qsearchs );
9 use FS::cdr_termination;
11 tie my %temporalities, 'Tie::IxHash',
12 'upcoming' => "Upcoming (future)",
13 'preceding' => "Preceding (past)",
17 'name' => 'VoIP rating of CDR records for termination partners.',
18 'shortname' => 'VoIP/telco CDR termination',
21 'setup_fee' => { 'name' => 'Setup fee for this package',
24 'recur_fee' => { 'name' => 'Base recurring fee for this package',
28 #'cdr_column' => { 'name' => 'Column from CDR records',
30 # 'select_enum' => [qw(
46 #false laziness w/flat.pm
47 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
49 'select_options' => \%temporalities,
52 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
53 ' of service at cancellation',
57 'cutoff_day' => { 'name' => 'Billing Day (1 - 28) for prorating or '.
62 'recur_method' => { 'name' => 'Recurring fee method',
64 #'options' => \%recur_method,
66 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
69 #false laziness w/cdr_termination.pm
70 'output_format' => { 'name' => 'CDR invoice display format',
72 'select_options' => { FS::cdr::invoice_formats() },
73 'default' => 'simple2', #XXX test
76 'usage_section' => { 'name' => 'Section in which to place separate usage charges',
79 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
87 recur_temporality unused_credit recur_method cutoff_day
88 output_format usage_section summarize_usage
97 my($self, $cust_pkg ) = @_;
98 $self->option('setup_fee');
103 my($cust_pkg, $sdate, $details, $param ) = @_;
105 #my $last_bill = $cust_pkg->last_bill;
106 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
109 if $self->option('recur_temporality', 1) eq 'preceding'
110 && ( $last_bill eq '' || $last_bill == 0 );
112 # termination calculations
114 my $term_percent = $cust_pkg->cust_main->cdr_termination_percentage;
115 die "no customer termination percentage" unless $term_percent;
117 my $output_format = $self->option('output_format', 'Hush!') || 'simple2';
121 #find an svc_external record
122 my @svc_external = map { $_->svc_x }
123 grep { $_->part_svc->svcdb eq 'svc_external' }
126 die "cdr_termination package has no svc_external service"
127 unless @svc_external;
128 die "cdr_termination package has multiple svc_external services"
129 if scalar(@svc_external) > 1;
131 my $svc_external = $svc_external[0];
134 # - matching our customer via svc_external.id/title? (and via what field?)
136 #let's try carrierid for now, can always make it configurable or rewrite
137 my $cdr_column = 'carrierid';
139 my %hashref = ( 'freesidestatus' => 'done' );
141 # try matching on svc_external.id for now... (or title? if ints don't cut it)
142 $hashref{$cdr_column} = $svc_external[0]->id;
144 # - with no cdr_termination.status
146 my $termpart = 1; #or from an option
148 #false lazienss w/search/cdr.html (i should be a part_termination method)
150 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
151 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
153 "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
155 #may need to process in batches if there's waaay too many
158 #'addl_from' => $join_term,
159 'hashref' => \%hashref,
160 'extra_sql' => "$extra_sql FOR UPDATE",
163 foreach my $cdr (@cdrs) {
165 #add a cdr_termination record and the charges
168 #my $term_price = sprintf('%.2f', $cdr->rated_price * $term_percent / 100 );
169 my $term_price = sprintf('%.4f', $cdr->rated_price * $term_percent / 100 );
171 my $cdr_termination = new FS::cdr_termination {
172 'acctid' => $cdr->acctid,
173 'termpart' => $termpart,
174 'rated_price' => $term_price,
178 my $error = $cdr_termination->insert;
179 die $error if $error; #next if $error; #or just skip this one??? why?
181 $charges += $term_price;
183 # and add a line to the invoice
185 my $call_details = $cdr->downstream_csv( 'format' => $output_format,
186 'charge' => $term_price,
189 my $classnum = ''; #usage class?
191 #option to turn off? or just use squelch_cdr for the customer probably
192 push @$details, [ 'C', $call_details, $term_price, $classnum ];
196 # eotermiation calculation
198 $charges += $self->calc_recur_Common(@_);