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',
19 'inherit_fields' => [ 'global_Mixin' ],
21 #'cdr_column' => { 'name' => 'Column from CDR records',
23 # 'select_enum' => [qw(
39 #false laziness w/flat.pm
40 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
42 'select_options' => \%temporalities,
45 'cutoff_day' => { 'name' => 'Billing Day (1 - 28) for prorating or '.
49 'add_full_period'=> { 'name' => 'When prorating first month, also bill '.
50 'for one full period after that',
54 'recur_method' => { 'name' => 'Recurring fee method',
56 #'options' => \%recur_method,
58 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
61 #false laziness w/voip_cdr.pm
62 'output_format' => { 'name' => 'CDR invoice display format',
64 'select_options' => { FS::cdr::invoice_formats() },
65 'default' => 'simple2', #XXX test
68 'usage_section' => { 'name' => 'Section in which to place separate usage charges',
71 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
75 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
83 recur_temporality recur_method cutoff_day
85 output_format usage_section summarize_usage usage_mandate
94 my($self, $cust_pkg ) = @_;
95 $self->option('setup_fee');
100 my($cust_pkg, $sdate, $details, $param ) = @_;
102 #my $last_bill = $cust_pkg->last_bill;
103 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
106 if $self->option('recur_temporality', 1) eq 'preceding'
107 && ( $last_bill eq '' || $last_bill == 0 );
109 # termination calculations
111 my $term_percent = $cust_pkg->cust_main->cdr_termination_percentage;
112 die "no customer termination percentage" unless $term_percent;
114 my $output_format = $self->option('output_format', 'Hush!') || 'simple2';
118 #find an svc_external record
119 my @svc_external = map { $_->svc_x }
120 grep { $_->part_svc->svcdb eq 'svc_external' }
123 die "cdr_termination package has no svc_external service"
124 unless @svc_external;
125 die "cdr_termination package has multiple svc_external services"
126 if scalar(@svc_external) > 1;
128 my $svc_external = $svc_external[0];
131 # - matching our customer via svc_external.id/title? (and via what field?)
133 #let's try carrierid for now, can always make it configurable or rewrite
134 my $cdr_column = 'carrierid';
136 my %hashref = ( 'freesidestatus' => 'done' );
138 # try matching on svc_external.id for now... (or title? if ints don't cut it)
139 $hashref{$cdr_column} = $svc_external[0]->id;
141 # - with no cdr_termination.status
143 my $termpart = 1; #or from an option
145 #false lazienss w/search/cdr.html (i should be a part_termination method)
147 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
148 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
150 "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
152 #may need to process in batches if there's waaay too many
155 #'addl_from' => $join_term,
156 'hashref' => \%hashref,
157 'extra_sql' => "$extra_sql FOR UPDATE",
160 foreach my $cdr (@cdrs) {
162 #add a cdr_termination record and the charges
165 #my $term_price = sprintf('%.2f', $cdr->rated_price * $term_percent / 100 );
166 my $term_price = sprintf('%.4f', $cdr->rated_price * $term_percent / 100 );
168 my $cdr_termination = new FS::cdr_termination {
169 'acctid' => $cdr->acctid,
170 'termpart' => $termpart,
171 'rated_price' => $term_price,
175 my $error = $cdr_termination->insert;
176 die $error if $error; #next if $error; #or just skip this one??? why?
178 $charges += $term_price;
180 # and add a line to the invoice
182 my $call_details = $cdr->downstream_csv( 'format' => $output_format,
183 'charge' => $term_price,
186 my $classnum = ''; #usage class?
188 #option to turn off? or just use squelch_cdr for the customer probably
189 push @$details, [ 'C', $call_details, $term_price, $classnum ];
193 # eotermiation calculation
195 $charges += $self->calc_recur_Common(@_);